From 9e3e44ee3d5bb390beb5af7fc88909238f607234 Mon Sep 17 00:00:00 2001 From: Kate Lovett Date: Fri, 19 Apr 2019 11:32:16 -0700 Subject: [PATCH] Committing test progress. --- .../flutter_goldens/lib/flutter_goldens.dart | 7 +- .../test/flutter_goldens_test.dart | 106 ++++++++++-------- .../flutter_goldens_client/lib/client.dart | 16 +-- packages/flutter_test/test/matchers_test.dart | 82 ++++++++++++++ 4 files changed, 152 insertions(+), 59 deletions(-) diff --git a/packages/flutter_goldens/lib/flutter_goldens.dart b/packages/flutter_goldens/lib/flutter_goldens.dart index 5ee18ead54..90a5f8bea8 100644 --- a/packages/flutter_goldens/lib/flutter_goldens.dart +++ b/packages/flutter_goldens/lib/flutter_goldens.dart @@ -1,6 +1,7 @@ // Copyright 2018 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. + import 'dart:async'; import 'dart:io'; import 'dart:typed_data'; @@ -13,7 +14,7 @@ import 'package:test_api/test_api.dart' as test_package show TestFailure; import 'package:flutter_goldens_client/client.dart'; export 'package:flutter_goldens_client/client.dart'; -//TODO(katelovett): Tests [flutter_goldens_test.dart] and inline documentation +//TODO(katelovett): Tests const String _kFlutterRootKey = 'FLUTTER_ROOT'; /// Main method that can be used in a `flutter_test_config.dart` file to set @@ -80,6 +81,9 @@ class FlutterGoldenFileComparator implements GoldenFileComparator { Future compare(Uint8List imageBytes, Uri golden) async { final bool authorized = await _skiaClient.auth(fs.directory(basedir)); final File goldenFile = _getGoldenFile(golden); + if(!goldenFile.existsSync()) { + throw test_package.TestFailure('Could not be compared against non-existent file: "$golden"'); + } if (!authorized) throw test_package.TestFailure('Could not authorize golctl.'); @@ -96,5 +100,4 @@ class FlutterGoldenFileComparator implements GoldenFileComparator { File _getGoldenFile(Uri uri) { return fs.directory(basedir).childFile(fs.file(uri).path); } - } diff --git a/packages/flutter_goldens/test/flutter_goldens_test.dart b/packages/flutter_goldens/test/flutter_goldens_test.dart index 19645edc8f..bdfe6305be 100644 --- a/packages/flutter_goldens/test/flutter_goldens_test.dart +++ b/packages/flutter_goldens/test/flutter_goldens_test.dart @@ -14,49 +14,44 @@ import 'package:platform/platform.dart'; import 'package:process/process.dart'; const String _kFlutterRoot = '/flutter'; -const String _kRepositoryRoot = '$_kFlutterRoot/bin/cache/pkg/goldens'; -const String _kVersionFile = '$_kFlutterRoot/bin/internal/goldens.version'; -const String _kGoldensVersion = '123456abcdef'; +const String _kGoldenRoot = '$_kFlutterRoot/bin/cache/pkg/goldens'; +//const String _kVersionFile = '$_kFlutterRoot/bin/internal/goldens.version'; +//const String _kGoldensVersion = '123456abcdef'; void main() { MemoryFileSystem fs; FakePlatform platform; MockProcessManager process; + Directory flutter; + Directory golden; - setUp(() { + setUp(() async { fs = MemoryFileSystem(); platform = FakePlatform(environment: {'FLUTTER_ROOT': _kFlutterRoot}); process = MockProcessManager(); - fs.directory(_kFlutterRoot).createSync(recursive: true); - fs.directory(_kRepositoryRoot).createSync(recursive: true); - fs.file(_kVersionFile).createSync(recursive: true); - fs.file(_kVersionFile).writeAsStringSync(_kGoldensVersion); + flutter = await fs.directory(_kFlutterRoot).create(recursive: true); + golden = await fs.directory(_kGoldenRoot).create(recursive: true); + //fs.file(_kVersionFile).createSync(recursive: true); + //fs.file(_kVersionFile).writeAsStringSync(_kGoldensVersion); }); - group('GoldensClient', () { - GoldensClient goldens; + group('SkiaGoldClient', () { + SkiaGoldClient skiaGold; setUp(() { - goldens = GoldensClient( + skiaGold = SkiaGoldClient( fs: fs, platform: platform, process: process, ); }); - group('prepare', () { - test('performs minimal work if versions match', () async { - when(process.run(any, workingDirectory: anyNamed('workingDirectory'))) - .thenAnswer((_) => Future.value(io.ProcessResult(123, 0, _kGoldensVersion, ''))); - await goldens.prepare(); + group('auth', () { + + }); + + group('imgtest', () { - // Verify that we only spawned `git rev-parse HEAD` - final VerificationResult verifyProcessRun = - verify(process.run(captureAny, workingDirectory: captureAnyNamed('workingDirectory'))); - verifyProcessRun.called(1); - expect(verifyProcessRun.captured.first, ['git', 'rev-parse', 'HEAD']); - expect(verifyProcessRun.captured.last, _kRepositoryRoot); - }); }); }); @@ -74,21 +69,34 @@ void main() { group('fromDefaultComparator', () { test('calculates the basedir correctly', () async { - final MockGoldensClient goldens = MockGoldensClient(); - final MockLocalFileComparator defaultComparator = MockLocalFileComparator(); - final Directory flutterRoot = fs.directory('/foo')..createSync(recursive: true); - final Directory goldensRoot = flutterRoot.childDirectory('bar')..createSync(recursive: true); - when(goldens.fs).thenReturn(fs); - when(goldens.flutterRoot).thenReturn(flutterRoot); - when(goldens.repositoryRoot).thenReturn(goldensRoot); - when(defaultComparator.basedir).thenReturn(flutterRoot.childDirectory('baz').uri); - comparator = await FlutterGoldenFileComparator.fromDefaultComparator( - goldens: goldens, defaultComparator: defaultComparator); - expect(comparator.basedir, fs.directory('/foo/bar/baz').uri); +// final MockSkiaGoldClient skiaGold = MockSkiaGoldClient(); +// final MockLocalFileComparator defaultComparator = MockLocalFileComparator(); +// final Directory flutterRoot = fs.directory('/foo')..createSync(recursive: true); +// final Directory skiaGoldRoot = flutterRoot.childDirectory('bar')..createSync(recursive: true); +// when(skiaGold.fs).thenReturn(fs); +// when(skiaGold.flutterRoot).thenReturn(flutterRoot); +// when(skiaGold.repositoryRoot).thenReturn(skiaGoldRoot); +// when(defaultComparator.basedir).thenReturn(flutterRoot.childDirectory('baz').uri); +// comparator = await FlutterGoldenFileComparator.fromDefaultComparator( +// goldens: goldens, defaultComparator: defaultComparator); +// expect(comparator.basedir, fs.directory('/foo/bar/baz').uri); }); }); group('compare', () { + + test('throws if goldctl has not been authorized', () async { + // Create file + final File goldenFile = fs.file('/path/to/flutter/bin/cache/goldens/test/foo/bar/test.png') + ..createSync(recursive: true); + try { + await comparator.compare(Uint8List.fromList([1, 2, 3]), Uri.parse('test.png')); + fail('TestFailure expected but not thrown'); + } on TestFailure catch (error) { + expect(error.message, contains('Could not authorize goldctl.')); + } + }); + test('throws if golden file is not found', () async { try { await comparator.compare(Uint8List.fromList([1, 2, 3]), Uri.parse('test.png')); @@ -98,21 +106,21 @@ void main() { } }); - test('returns false if golden bytes do not match', () async { - final File goldenFile = fs.file('/path/to/flutter/bin/cache/goldens/test/foo/bar/test.png') - ..createSync(recursive: true); - goldenFile.writeAsBytesSync([4, 5, 6], flush: true); - final bool result = await comparator.compare(Uint8List.fromList([1, 2, 3]), Uri.parse('test.png')); - expect(result, isFalse); - }); +// test('returns false if skia gold test fails', () async { +// final File goldenFile = fs.file('/path/to/flutter/bin/cache/goldens/test/foo/bar/test.png') +// ..createSync(recursive: true); +// goldenFile.writeAsBytesSync([4, 5, 6], flush: true); +// final bool result = await comparator.compare(Uint8List.fromList([1, 2, 3]), Uri.parse('test.png')); +// expect(result, isFalse); +// }); - test('returns true if golden bytes match', () async { - final File goldenFile = fs.file('/path/to/flutter/bin/cache/goldens/test/foo/bar/test.png') - ..createSync(recursive: true); - goldenFile.writeAsBytesSync([1, 2, 3], flush: true); - final bool result = await comparator.compare(Uint8List.fromList([1, 2, 3]), Uri.parse('test.png')); - expect(result, isTrue); - }); +// test('returns true if skia gold test passes', () async { +// final File goldenFile = fs.file('/path/to/flutter/bin/cache/goldens/test/foo/bar/test.png') +// ..createSync(recursive: true); +// goldenFile.writeAsBytesSync([1, 2, 3], flush: true); +// final bool result = await comparator.compare(Uint8List.fromList([1, 2, 3]), Uri.parse('test.png')); +// expect(result, isTrue); +// }); }); group('update', () { @@ -136,5 +144,5 @@ void main() { } class MockProcessManager extends Mock implements ProcessManager {} -class MockGoldensClient extends Mock implements GoldensClient {} +class MockSkiaGoldClient extends Mock implements SkiaGoldClient {} class MockLocalFileComparator extends Mock implements LocalFileComparator {} diff --git a/packages/flutter_goldens_client/lib/client.dart b/packages/flutter_goldens_client/lib/client.dart index 29e724d21e..6e461f1935 100644 --- a/packages/flutter_goldens_client/lib/client.dart +++ b/packages/flutter_goldens_client/lib/client.dart @@ -1,7 +1,7 @@ // Copyright 2018 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -//TODO(katelovett): Change to Skia Gold Client + import 'dart:async'; import 'dart:convert' as convert; import 'dart:io' as io; @@ -14,7 +14,7 @@ import 'package:process/process.dart'; // If you are here trying to figure out how to use golden files in the Flutter // repo itself, consider reading this wiki page: // https://github.com/flutter/flutter/wiki/Writing-a-golden-file-test-for-package%3Aflutter -//TODO(katelovett): Tests [flutter_goldens_test.dart] and inline documentation +//TODO(katelovett): Tests const String _kFlutterRootKey = 'FLUTTER_ROOT'; const String _kGoldctlKey = 'GOLDCTL'; const String _kServiceAccountKey = 'GOLD_SERVICE_ACCOUNT'; @@ -31,7 +31,7 @@ class SkiaGoldClient { /// The file system to use for storing local files for running imgtests. /// - /// This is usefule in tests, where a local file system (the default) can be + /// This is useful in tests, where a local file system (the default) can be /// replaced by a memory file system. final FileSystem fs; @@ -41,11 +41,11 @@ class SkiaGoldClient { /// replaced by a mock platform instance. final Platform platform; - /// A controller for launching subprocesses. + /// A controller for launching sub-processes. /// /// This is useful in tests, where the real process manager (the default) can /// be replaced by a mock process manager that doesn't really create - /// subprocesses. + /// sub-processes. final ProcessManager process; Directory _workDirectory; @@ -70,7 +70,7 @@ class SkiaGoldClient { /// The local [Directory] where the Flutter repository is hosted. /// /// Uses the [fs] file system. - Directory get _flutterRoot => fs.directory(platform.environment[_kFlutterRootKey]); + Directory get flutterRoot => fs.directory(platform.environment[_kFlutterRootKey]); /// Prepares the local work space for golden file testing and initializes the /// goldctl authorization for executing tests. @@ -145,12 +145,12 @@ class SkiaGoldClient { } Future _getCommitHash() async { - if (!_flutterRoot.existsSync()) { + if (!flutterRoot.existsSync()) { return null; } else { final io.ProcessResult revParse = await process.run( ['git', 'rev-parse', 'HEAD'], - workingDirectory: _flutterRoot.path, + workingDirectory: flutterRoot.path, ); return revParse.exitCode == 0 ? revParse.stdout.trim() : null; } diff --git a/packages/flutter_test/test/matchers_test.dart b/packages/flutter_test/test/matchers_test.dart index 29f0f72684..e0255fab2e 100644 --- a/packages/flutter_test/test/matchers_test.dart +++ b/packages/flutter_test/test/matchers_test.dart @@ -384,6 +384,88 @@ void main() { }); }); + group('matchesSkiaGoldFile', () { + _FakeComparator comparator; + + Widget boilerplate(Widget child) { + return Directionality( + textDirection: TextDirection.ltr, + child: child, + ); + } + + setUp(() { + comparator = _FakeComparator(); + goldenFileComparator = comparator; + }); + + group('matches', () { + testWidgets('if comparator succeeds', (WidgetTester tester) async { + await tester.pumpWidget(boilerplate(const Text('hello'))); + final Finder finder = find.byType(Text); + await expectLater(finder, matchesSkiaGoldFile('foo.png')); + expect(comparator.invocation, _ComparatorInvocation.compare); + expect(comparator.imageBytes, null); + expect(comparator.golden, Uri.parse('foo.png')); + }); + }); + + group('does not match', () { + testWidgets('if comparator returns false', (WidgetTester tester) async { + comparator.behavior = _ComparatorBehavior.returnFalse; + await tester.pumpWidget(boilerplate(const Text('hello'))); + final Finder finder = find.byType(Text); + try { + await expectLater(finder, matchesSkiaGoldFile('foo.png')); + fail('TestFailure expected but not thrown'); + } on TestFailure catch (error) { + expect(comparator.invocation, _ComparatorInvocation.compare); + expect(error.message, contains('does not match')); + } + }); + + testWidgets('if comparator throws', (WidgetTester tester) async { + comparator.behavior = _ComparatorBehavior.throwTestFailure; + await tester.pumpWidget(boilerplate(const Text('hello'))); + final Finder finder = find.byType(Text); + try { + await expectLater(finder, matchesSkiaGoldFile('foo.png')); + fail('TestFailure expected but not thrown'); + } on TestFailure catch (error) { + expect(comparator.invocation, _ComparatorInvocation.compare); + expect(error.message, contains('fake message')); + } + }); + + testWidgets('if finder finds no widgets', (WidgetTester tester) async { + await tester.pumpWidget(boilerplate(Container())); + final Finder finder = find.byType(Text); + try { + await expectLater(finder, matchesSkiaGoldFile('foo.png')); + fail('TestFailure expected but not thrown'); + } on TestFailure catch (error) { + expect(comparator.invocation, isNull); + expect(error.message, contains('no widget was found')); + } + }); + + testWidgets( + 'if finder finds multiple widgets', (WidgetTester tester) async { + await tester.pumpWidget(boilerplate(Column( + children: const [Text('hello'), Text('world')], + ))); + final Finder finder = find.byType(Text); + try { + await expectLater(finder, matchesSkiaGoldFile('foo.png')); + fail('TestFailure expected but not thrown'); + } on TestFailure catch (error) { + expect(comparator.invocation, isNull); + expect(error.message, contains('too many widgets')); + } + }); + }); + }); + group('matchesSemanticsData', () { testWidgets('matches SemanticsData', (WidgetTester tester) async { final SemanticsHandle handle = tester.ensureSemantics();