Skip to content

Commit

Permalink
Add test "Global: Live timeline - Live timeline - with error, no dupl…
Browse files Browse the repository at this point in the history
…icates, prints HTML"
  • Loading branch information
danielmolnar committed Jun 26, 2024
1 parent 907b47c commit 91705f5
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 108 deletions.
7 changes: 7 additions & 0 deletions test/timeline/tap/global/live_timeline_tap_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,11 @@ void main() {
isGlobalMode: true,
);
});
test(
'Global: Live timeline - Live timeline - with error, no duplicates, prints HTML',
() async {
await TimelineTestHelpers.liveTimelineWithErrorNoDuplicatesPrintsHtml(
isGlobalMode: true,
);
});
}
109 changes: 1 addition & 108 deletions test/timeline/tap/local/timeline_tap_test.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import 'dart:io';

import 'package:dartx/dartx.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:spot/spot.dart';
import 'package:test_process/test_process.dart';
import '../../../util/timeline_test_helpers.dart';
import '../timeline_tap_test_widget.dart';

Expand All @@ -13,37 +9,6 @@ final _subtractButtonSelector = spotIcon(Icons.remove);
final _clearButtonSelector = spotIcon(Icons.clear);

const _header = '==================== Timeline Event ====================';
const _separator = '========================================================';

String _testAsString({
required String title,
required TimelineMode timelineMode,
bool shouldFail = false,
}) {
final widgetPart = File('test/timeline/tap/timeline_tap_test_widget.dart')
.readAsStringSync();
return '''
import 'package:flutter_test/flutter_test.dart';
import 'package:spot/spot.dart';
import 'package:spot/src/timeline/timeline.dart';\n
$widgetPart\n
void main() async {
final addButtonSelector = spotIcon(Icons.add);
final subtractButtonSelector = spotIcon(Icons.remove);
testWidgets("$title", (WidgetTester tester) async {
${TimelineTestHelpers.localTimelineInitiator(timelineMode)};
await tester.pumpWidget(const TimelineTestWidget());
addButtonSelector.existsOnce();
spotText('Counter: 3').existsOnce();
await act.tap(addButtonSelector);
spotText('Counter: 4').existsOnce();
await act.tap(subtractButtonSelector);
spotText('Counter: 3').existsOnce();
${shouldFail ? 'spotText("Counter: 99").existsOnce();' : ''}
});
}
''';
}

void main() {
group('Initial Values', () {
Expand Down Expand Up @@ -110,79 +75,7 @@ void main() {
await TimelineTestHelpers.liveTimelineWithoutErrorPrintsHtml();
});
test('Live timeline - with error, no duplicates, prints HTML', () async {
final tempDir = Directory.systemTemp.createTempSync();
final tempTestFile = File('${tempDir.path}/temp_test.dart');
await tempTestFile.writeAsString(
_testAsString(
title: 'Live timeline - with error, no duplicates, prints HTML',
timelineMode: TimelineMode.live,
shouldFail: true,
),
);

final testProcess =
await TestProcess.start('flutter', ['test', tempTestFile.path]);

final stdoutBuffer = StringBuffer();

bool write = false;
await for (final line in testProcess.stdoutStream()) {
if (line.isEmpty) continue;

if (!write) {
if (line == _header) {
write = true;
}
}

if (write) {
stdoutBuffer.writeln(line);
}
}

// Error does not happen
await testProcess.shouldExit(1);

if (tempDir.existsSync()) {
tempDir.deleteSync(recursive: true);
}

final stdout = stdoutBuffer.toString();
final timeline = stdout.split('\n');
// Does not start with 'Timeline', this only happens on error
expect(timeline.first, _header);
expect(
timeline.second,
'Event: Tap Icon Widget with icon: "IconData(U+0E047)"',
);
expect(
timeline[2].startsWith('Caller: at main.<fn> file:///'),
isTrue,
);
expect(
timeline[3].startsWith(
'Screenshot: file:///',
),
isTrue,
);
expect(
timeline[4].startsWith(
'Timestamp:',
),
isTrue,
);
expect(
timeline[5],
_separator,
);
final htmlLine = timeline
.firstWhere((line) => line.startsWith('View time line here:'));
expect(
htmlLine.endsWith(
'live-timeline-with-error-no-duplicates-prints-html.html',
),
isTrue,
);
await TimelineTestHelpers.liveTimelineWithErrorNoDuplicatesPrintsHtml();
});
});
}
Expand Down
82 changes: 82 additions & 0 deletions test/util/timeline_test_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,88 @@ class TimelineTestHelpers {
);
}

static Future<void> liveTimelineWithErrorNoDuplicatesPrintsHtml({
bool isGlobalMode = false,
}) async {
final tempDir = Directory.systemTemp.createTempSync();
final tempTestFile = File('${tempDir.path}/temp_test.dart');
final testTitle =
'${isGlobalMode ? 'Global: ' : 'Local: '}Live timeline - with error, no duplicates, prints HTML';
await tempTestFile.writeAsString(
testAsString(
title: testTitle,
timelineMode: TimelineMode.live,
shouldFail: true,
isGlobalMode: isGlobalMode,
),
);

final testProcess =
await TestProcess.start('flutter', ['test', tempTestFile.path]);

final stdoutBuffer = StringBuffer();

bool write = false;
await for (final line in testProcess.stdoutStream()) {
if (line.isEmpty) continue;

if (!write) {
if (line == header) {
write = true;
}
}

if (write) {
stdoutBuffer.writeln(line);
}
}

// Error does not happen
await testProcess.shouldExit(1);

if (tempDir.existsSync()) {
tempDir.deleteSync(recursive: true);
}

final stdout = stdoutBuffer.toString();
final timeline = stdout.split('\n');
// Does not start with 'Timeline', this only happens on error
expect(timeline.first, header);
expect(
timeline.second,
'Event: Tap Icon Widget with icon: "IconData(U+0E047)"',
);
expect(
timeline[2].startsWith('Caller: at main.<fn> file:///'),
isTrue,
);
expect(
timeline[3].startsWith(
'Screenshot: file:///',
),
isTrue,
);
expect(
timeline[4].startsWith(
'Timestamp:',
),
isTrue,
);
expect(
timeline[5],
separator,
);
final prefix = isGlobalMode ? 'global' : 'local';
final htmlLine =
timeline.firstWhere((line) => line.startsWith('View time line here:'));
expect(
htmlLine.endsWith(
'timeline-$prefix-live-timeline-with-error-no-duplicates-prints-html.html',
),
isTrue,
);
}

static Future<void> offTimelineTest({
required WidgetTester tester,
bool isGlobalMode = false,
Expand Down

0 comments on commit 91705f5

Please sign in to comment.