Skip to content

Commit

Permalink
Add test "Live timeline with added screenshot event"
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmolnar committed May 22, 2024
1 parent 1afb4bd commit eba534f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 35 deletions.
24 changes: 12 additions & 12 deletions lib/src/timeline/timeline.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,20 @@ final Map<LiveTest, Timeline> _timelines = {};

/// Starts the timeline recording and prints events as they happen.
Timeline liveTimeline() {
// ignore: avoid_print
print('🔴 - Recording timeline with live output');
final timeline = currentTimeline();
timeline.startLiveTimeline();
timeline.startWithLiveOutput();
return timeline;
}

/// Records the timeline but only prints it in case of an error.
Timeline startOnErrorTimeline() {
// ignore: avoid_print
print('🔴 - Recording timeline for error output');
Timeline onErrorTimeline() {
final timeline = currentTimeline();
timeline.startOnErrorTimeLine();
timeline.startWithErrorOutput();
return timeline;
}

/// Stops the timeline recording.
Timeline stopTimeline() {
// ignore: avoid_print
print('⏸︎ - Timeline stopped');
Timeline stoppedTimeLine() {
final timeline = currentTimeline();
timeline.stopTimeLine();
return timeline;
Expand Down Expand Up @@ -75,16 +69,22 @@ class Timeline {

/// Stops the timeline recording and printing.
void stopTimeLine() {
// ignore: avoid_print
print('⏸︎ - Timeline stopped');
_mode = TimelineMode.off;
}

/// Starts the timeline recording and prints events as they happen.
void startLiveTimeline() {
void startWithLiveOutput() {
// ignore: avoid_print
print('🔴 - Recording timeline with live output');
_mode = TimelineMode.live;
}

/// Records the timeline but only prints it in case of an error.
void startOnErrorTimeLine() {
void startWithErrorOutput() {
// ignore: avoid_print
print('🔴 - Recording timeline for error output');
_mode = TimelineMode.record;
}

Expand Down
72 changes: 49 additions & 23 deletions test/timeline/timeline_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,64 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:spot/spot.dart';
import 'package:spot/src/timeline/timeline.dart';

import '../spot/screenshot_test.dart';

void main() {
testWidgets('Automatically take screenshots for timeline', (tester) async {
startLiveTimeline();
stopTimeline();
startOnErrorTimeline();
int counter = 0;
testWidgets('Live timeline with added screenshot event', (tester) async {
final Timeline timeLine = liveTimeline();
tester.view.physicalSize = const Size(210, 210);
tester.view.devicePixelRatio = 1.0;
int i = 0;
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
_TimelineTestWidget(
onTap: () {
i++;
},
),
);

final button = spotIcon(Icons.home)..existsOnce();
await act.tap(button);
expect(i, 1);
final shot = timeLine.events.first.screenshot!;
expect(shot.file.existsSync(), isTrue);

final cyanPixelCoverage =
await percentageOfPixelsWithColor(shot.file, const Color(0xFF00FFFF));
expect(cyanPixelCoverage, greaterThan(0.0));
});
}

class _TimelineTestWidget extends StatelessWidget {
const _TimelineTestWidget({
required this.onTap,
});

final void Function() onTap;

@override
Widget build(BuildContext context) {
return MaterialApp(
builder: (context, child) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.blueAccent,
title: const Text('Home'),
actions: [
IconButton(
icon: const Icon(Icons.home),
onPressed: () {
counter++;
},
onPressed: onTap,
),
],
),
),
),
body: Center(
child: Container(
height: 200,
width: 200,
color: Colors.red,
),
),
);
},
);
print('Before tap');
await act.tap(spotIcon(Icons.home));
print('After tap');

print('Before tap2');
await act.tap(spot<AppBar>());
print('After tap2');

expect(counter, 2);
});
}
}

0 comments on commit eba534f

Please sign in to comment.