Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
comigor committed Oct 5, 2018
1 parent 2d72bcd commit 2377a4d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 61 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,6 @@ build/
!**/ios/**/default.pbxuser
!**/ios/**/default.perspectivev3
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages

showcase/

80 changes: 19 additions & 61 deletions test/components/my_widget_test.dart
Original file line number Diff line number Diff line change
@@ -1,86 +1,49 @@
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 'dart:async';
import 'dart:typed_data';
import 'dart:ui';
import 'dart:io';

import 'dart:convert';
import 'dart:ui' as ui;

import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:path/path.dart' as path;

import 'package:quiver/testing/async.dart';
import 'package:showcase/components/my_widget.dart';
import '../helpers/golden_boundary.dart';

Future<Uint8List> capturePng(WidgetTester tester, GlobalKey key) async {
// try {
// print('inside');
// RenderRepaintBoundary boundary = tester.allRenderObjects.firstWhere((o) => o is RenderRepaintBoundary);
// RenderRepaintBoundary boundary = tester.firstRenderObject(find.byKey(key));
RenderRepaintBoundary boundary = key.currentContext.findRenderObject();
// RenderRepaintBoundary boundary = _globalKey.currentContext.findRenderObject();
ui.Image image = await boundary.toImage(pixelRatio: 3.0);
ByteData byteData =
await image.toByteData(format: ui.ImageByteFormat.png);
// return byteData.buffer.asUint8List();
// return null;
var pngBytes = byteData.buffer.asUint8List();
var bs64 = base64Encode(pngBytes);
// print(pngBytes.length);
// print(bs64);
return pngBytes;
// // setState(() {});
// return pngBytes;
// } catch (e) {
// print(e);
// return null;
// }
RenderRepaintBoundary boundary = key.currentContext.findRenderObject();
ui.Image image = await boundary.toImage(pixelRatio: 3.0);
ByteData byteData =
await image.toByteData(format: ui.ImageByteFormat.png);
return byteData.buffer.asUint8List();
}

Future<void> write(Uint8List u, int index) async {
final String pathName = 'golden_$index.png';

Future<void> writeToFile(String pathName, Uint8List u) async {
final Directory goldenDir = Directory(path.dirname(pathName));
if (!goldenDir.existsSync()) {
goldenDir.createSync(recursive: true);
}
final File output = File(pathName);

// print('before');
// final aaa = await capturePng(tester, key);
// print('middle');
await output.writeAsBytes(u);
}

void makeTest(Widget w, int index) async {
void makeTest(Widget widget, int index) async {
testWidgets('MyWidget golden $index', (WidgetTester tester) async {
// https://github.com/flutter/flutter/issues/17738#issuecomment-392237064
await tester.runAsync(() async {
final key = GlobalKey();

final TestWidgetsFlutterBinding binding = tester.binding;
if (binding is AutomatedTestWidgetsFlutterBinding)
binding.addTime(const Duration(seconds: 7));

await tester.pumpWidget(GoldenBoundary(
globalKey: key,
child: w,
child: widget,
));

// print('before');
final aaa = await capturePng(tester, key);
await write(aaa, index);
final screenshotBytes = await capturePng(tester, key);
await writeToFile('showcase/golden_$index.png', screenshotBytes);
expect(1, equals(1));
return Future.value(1);
});
// print('end');
// await write(aaa, index);
// print('after');
});
});
}

Expand All @@ -92,13 +55,8 @@ void main() {
];

int index = 0;

makeTest(MyWidget(), 0);

// widgetList.forEach((widget) async {
// makeTest(widget, index);

// index++;
// });

widgetList.forEach((widget) async {
makeTest(widget, index);
index++;
});
}

0 comments on commit 2377a4d

Please sign in to comment.