Skip to content

Commit

Permalink
Add test "Throws when global mode is changed during a test"
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmolnar committed Jun 26, 2024
1 parent 4766fe0 commit 2cb12e1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
8 changes: 6 additions & 2 deletions lib/src/timeline/timeline.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ TimelineMode _globalTimelineMode =
TimelineMode get globalTimelineMode => _globalTimelineMode;

set globalTimelineMode(TimelineMode value) {
_globalTimelineMode = value;
final test = Invoker.current?.liveTest;
if (test != null) {
timeline.mode = value;
throw StateError('''
Cannot change global timeline mode within a test.
Use "timeline.mode" instead.
Example: timeline.mode = $value;
''');
}
_globalTimelineMode = value;
}

/// Use --dart-define=SPOT_TIMELINE_MODE=live|record|off to set the [TimlineMode]
Expand Down
8 changes: 8 additions & 0 deletions test/timeline/tap/local/local_timeline_tap_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:spot/src/timeline/timeline.dart';
import '../timeline_tap_test_bodies.dart' as body;

void main() {
Expand Down Expand Up @@ -29,4 +30,11 @@ void main() {
await body.liveWithErrorNoDuplicatesPrintsHtml();
});
});

test('Throws when global mode is changed during test', () async {
await body.throwOnGlobalTimelineChange(
initialGlobalMode: TimelineMode.live,
globalTimelineModeToSwitch: TimelineMode.record,
);
});
}
43 changes: 37 additions & 6 deletions test/timeline/tap/timeline_tap_test_bodies.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,27 @@ Future<void> recordWithoutError({
_testTimeLineContent(output: output, eventCount: 0);
}

Future<void> throwOnGlobalTimelineChange({
bool isGlobalMode = false,
required TimelineMode initialGlobalMode,
required TimelineMode globalTimelineModeToSwitch,
}) async {
final stdout = await _outputFromTapTestProcess(
title: 'Throws if global timeline mode is changed mid test',
timelineMode: initialGlobalMode,
shouldFail: true,
isGlobalMode: isGlobalMode,
captureStart: 'The following StateError was thrown running a test:',
globalTimelineModeToSwitch: globalTimelineModeToSwitch,
);
final expectedErrorMessage = '''
Cannot change global timeline mode within a test.
Use "timeline.mode" instead.
Example: timeline.mode = $globalTimelineModeToSwitch;
''';
expect(stdout, contains(expectedErrorMessage));
}

Future<void> recordWithError({
bool isGlobalMode = false,
}) async {
Expand Down Expand Up @@ -331,7 +352,13 @@ String _tapTestAsString({
required TimelineMode timelineMode,
bool shouldFail = false,
bool isGlobalMode = false,
TimelineMode? globalTimelineModeToSwitch,
}) {
final switchPart = globalTimelineModeToSwitch != null
? '''
globalTimelineMode = TimelineMode.${globalTimelineModeToSwitch.toString().split('.').last};
'''
: '';
final testTitle = '${isGlobalMode ? 'Global: ' : 'Local: '}$title';

final globalInitiator =
Expand All @@ -352,6 +379,7 @@ void main() async {
final addButtonSelector = spotIcon(Icons.add);
final subtractButtonSelector = spotIcon(Icons.remove);
testWidgets("$testTitle", (WidgetTester tester) async {
$switchPart
$localInitiator
await tester.pumpWidget(const TimelineTestWidget());
addButtonSelector.existsOnce();
Expand All @@ -372,15 +400,18 @@ Future<String> _outputFromTapTestProcess({
String captureStart = shared.timelineHeader,
bool shouldFail = false,
bool isGlobalMode = false,
TimelineMode? globalTimelineModeToSwitch,
}) async {
final testAsString = _tapTestAsString(
title: title,
timelineMode: timelineMode,
shouldFail: shouldFail,
isGlobalMode: isGlobalMode,
globalTimelineModeToSwitch: globalTimelineModeToSwitch,
);
return process.runTestInProcessAndCaptureOutPut(
shouldFail: shouldFail,
testAsString: _tapTestAsString(
title: title,
timelineMode: timelineMode,
shouldFail: shouldFail,
isGlobalMode: isGlobalMode,
),
testAsString: testAsString,
captureStart: captureStart,
);
}

0 comments on commit 2cb12e1

Please sign in to comment.