forked from comigor/showcase
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request comigor#2 from Igor1201/poc/multiple-screenshots
[POC] Multiple screenshots
- Loading branch information
Showing
7 changed files
with
134 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,29 @@ | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:showcase/components/my_widget.dart'; | ||
import '../helpers/golden_boundary.dart'; | ||
import '../helpers/showcase.dart'; | ||
|
||
void main() { | ||
testWidgets('MyWidget golden', (WidgetTester tester) async { | ||
await tester.pumpWidget(GoldenBoundary( | ||
child: MyWidget(), | ||
)); | ||
group('Showcase widgets', () { | ||
final widgetList = [ | ||
MyWidget(), | ||
MyWidget(anotherBuild: true), | ||
MyWidget(sliderValue: 5.0), | ||
]; | ||
|
||
await expectLater( | ||
find.byType(MyWidget), | ||
matchesGoldenFile('golden.png'), | ||
showcaseWidgets(widgetList, customContainerBuilder: (child) => Container( | ||
padding: EdgeInsets.all(12.0), | ||
decoration: BoxDecoration( | ||
color: Colors.amberAccent, | ||
border: Border.all( | ||
color: Colors.amber, | ||
width: 2.0, | ||
), | ||
), | ||
width: 800.0, | ||
height: 240.0, | ||
child: child, | ||
), | ||
); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
import 'package:showcase/components/my_widget.dart'; | ||
import '../helpers/golden_boundary.dart'; | ||
|
||
void main() { | ||
testWidgets('MyWidget golden', (WidgetTester tester) async { | ||
await tester.pumpWidget(GoldenBoundary( | ||
child: MyWidget(), | ||
)); | ||
|
||
await expectLater( | ||
find.byType(MyWidget), | ||
matchesGoldenFile('golden.png'), | ||
); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export './showcase_widgets.dart' show showcaseWidgets; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import 'dart:async'; | ||
import 'dart:typed_data'; | ||
import 'dart:io'; | ||
import 'dart:ui' as ui; | ||
|
||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter/rendering.dart'; | ||
|
||
import './golden_boundary.dart'; | ||
|
||
Future<Uint8List> capturePng(WidgetTester tester, GlobalKey key) async { | ||
RenderRepaintBoundary boundary = key.currentContext.findRenderObject(); | ||
ui.Image image = await boundary.toImage(); | ||
ByteData byteData = | ||
await image.toByteData(format: ui.ImageByteFormat.png); | ||
return byteData.buffer.asUint8List(); | ||
} | ||
|
||
Future<File> writeToFile(String path, Uint8List imageBytes) async { | ||
final File output = File(path); | ||
output.createSync(recursive: true); | ||
return await output.writeAsBytes(imageBytes); | ||
} | ||
|
||
void makeTest(Widget widget, int index, {ContainerBuilder customContainerBuilder, String outDir}) async { | ||
final strIndex = index.toString().padLeft(3, '0'); | ||
testWidgets('[$strIndex] Showcasing ${widget.toString()}', (WidgetTester tester) async { | ||
// https://github.com/flutter/flutter/issues/17738#issuecomment-392237064 | ||
await tester.runAsync(() async { | ||
final key = GlobalKey(); | ||
|
||
await tester.pumpWidget(GoldenBoundary( | ||
globalKey: key, | ||
child: widget, | ||
customContainerBuilder: customContainerBuilder, | ||
)); | ||
|
||
final screenshotBytes = await capturePng(tester, key); | ||
await writeToFile('${outDir ?? 'showcase'}/${strIndex}_${widget.toString()}.png', screenshotBytes); | ||
}); | ||
}); | ||
} | ||
|
||
showcaseWidgets(List<Widget> widgets, {ContainerBuilder customContainerBuilder, String outDir}) { | ||
widgets | ||
.asMap() | ||
.forEach((index, widget) => makeTest(widget, index, customContainerBuilder: customContainerBuilder, outDir: outDir)); | ||
} |