golden_comparator_test.dart 7.49 KB
Newer Older
1 2 3 4
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

7
import 'dart:async';
8 9 10
import 'dart:convert';
import 'dart:typed_data';

11 12 13
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/logger.dart';
14
import 'package:flutter_tools/src/test/flutter_web_goldens.dart';
15
import 'package:flutter_tools/src/test/test_compiler.dart';
16
import 'package:flutter_tools/src/web/compile.dart';
17
import 'package:test/fake.dart';
18 19

import '../../src/common.dart';
20 21 22 23 24 25 26
import '../../src/context.dart';

final Uri goldenKey = Uri.parse('file://golden_key');
final Uri goldenKey2 = Uri.parse('file://second_golden_key');
final Uri testUri = Uri.parse('file://test_uri');
final Uri testUri2  = Uri.parse('file://second_test_uri');
final Uint8List imageBytes = Uint8List.fromList(<int>[1, 2, 3, 4, 5]);
27 28 29 30

void main() {

  group('Test that TestGoldenComparator', () {
31
    FakeProcessManager processManager;
32 33

    setUp(() {
34
      processManager = FakeProcessManager.empty();
35 36
    });

37
    testWithoutContext('succeed when golden comparison succeed', () async {
38 39 40 41
      final Map<String, dynamic> expectedResponse = <String, dynamic>{
        'success': true,
        'message': 'some message',
      };
42 43 44 45 46 47 48
      processManager.addCommand(FakeCommand(
        command: const <String>[
          'shell',
          '--disable-observatory',
          '--non-interactive',
          '--packages=.dart_tool/package_config.json',
          'compiler_output'
49 50 51 52 53 54
        ],
        stdout: '${jsonEncode(expectedResponse)}\n',
        environment: const <String, String>{
          'FLUTTER_TEST_BROWSER': 'chrome',
          'FLUTTER_WEB_RENDERER': 'html',
        },
55
      ));
56 57 58

      final TestGoldenComparator comparator = TestGoldenComparator(
        'shell',
59 60 61 62
        () => FakeTestCompiler(),
        processManager: processManager,
        fileSystem: MemoryFileSystem.test(),
        logger: BufferLogger.test(),
63
        webRenderer: WebRendererMode.html,
64 65 66 67
      );

      final String result = await comparator.compareGoldens(testUri, imageBytes, goldenKey, false);
      expect(result, null);
68
    });
69

70
    testWithoutContext('fail with error message when golden comparison failed', () async {
71 72 73 74 75
      final Map<String, dynamic> expectedResponse = <String, dynamic>{
        'success': false,
        'message': 'some message',
      };

76 77 78 79 80 81 82
      processManager.addCommand(FakeCommand(
        command: const <String>[
          'shell',
          '--disable-observatory',
          '--non-interactive',
          '--packages=.dart_tool/package_config.json',
          'compiler_output'
83
        ], stdout: '${jsonEncode(expectedResponse)}\n',
84
      ));
85 86 87

      final TestGoldenComparator comparator = TestGoldenComparator(
        'shell',
88 89 90 91
        () => FakeTestCompiler(),
        processManager: processManager,
        fileSystem: MemoryFileSystem.test(),
        logger: BufferLogger.test(),
92
        webRenderer: WebRendererMode.canvaskit,
93 94 95 96
      );

      final String result = await comparator.compareGoldens(testUri, imageBytes, goldenKey, false);
      expect(result, 'some message');
97
    });
98

99
    testWithoutContext('reuse the process for the same test file', () async {
100 101 102 103 104 105 106 107 108
      final Map<String, dynamic> expectedResponse1 = <String, dynamic>{
        'success': false,
        'message': 'some message',
      };
      final Map<String, dynamic> expectedResponse2 = <String, dynamic>{
        'success': false,
        'message': 'some other message',
      };

109 110 111 112 113 114 115
      processManager.addCommand(FakeCommand(
        command: const <String>[
          'shell',
          '--disable-observatory',
          '--non-interactive',
          '--packages=.dart_tool/package_config.json',
          'compiler_output'
116
        ], stdout: '${jsonEncode(expectedResponse1)}\n${jsonEncode(expectedResponse2)}\n',
117
      ));
118 119 120

      final TestGoldenComparator comparator = TestGoldenComparator(
        'shell',
121 122 123 124
        () => FakeTestCompiler(),
        processManager: processManager,
        fileSystem: MemoryFileSystem.test(),
        logger: BufferLogger.test(),
125
        webRenderer: WebRendererMode.html,
126 127 128 129
      );

      final String result1 = await comparator.compareGoldens(testUri, imageBytes, goldenKey, false);
      expect(result1, 'some message');
130

131 132
      final String result2 = await comparator.compareGoldens(testUri, imageBytes, goldenKey2, false);
      expect(result2, 'some other message');
133
    });
134

135
    testWithoutContext('does not reuse the process for different test file', () async {
136 137 138 139 140 141 142 143 144
      final Map<String, dynamic> expectedResponse1 = <String, dynamic>{
        'success': false,
        'message': 'some message',
      };
      final Map<String, dynamic> expectedResponse2 = <String, dynamic>{
        'success': false,
        'message': 'some other message',
      };

145 146 147 148 149 150 151
      processManager.addCommand(FakeCommand(
        command: const <String>[
          'shell',
          '--disable-observatory',
          '--non-interactive',
          '--packages=.dart_tool/package_config.json',
          'compiler_output'
152
        ], stdout: '${jsonEncode(expectedResponse1)}\n',
153 154 155 156 157 158 159 160
      ));
      processManager.addCommand(FakeCommand(
        command: const <String>[
          'shell',
          '--disable-observatory',
          '--non-interactive',
          '--packages=.dart_tool/package_config.json',
          'compiler_output'
161
        ], stdout: '${jsonEncode(expectedResponse2)}\n',
162
      ));
163 164 165

      final TestGoldenComparator comparator = TestGoldenComparator(
        'shell',
166 167 168 169
        () => FakeTestCompiler(),
        processManager: processManager,
        fileSystem: MemoryFileSystem.test(),
        logger: BufferLogger.test(),
170
        webRenderer: WebRendererMode.canvaskit,
171 172 173 174
      );

      final String result1 = await comparator.compareGoldens(testUri, imageBytes, goldenKey, false);
      expect(result1, 'some message');
175

176
      final String result2 = await comparator.compareGoldens(testUri2, imageBytes, goldenKey2, false);
177 178
      expect(result2, 'some other message');
    });
179

180 181
    testWithoutContext('removes all temporary files when closed', () async {
      final FileSystem fileSystem = MemoryFileSystem.test();
182 183 184 185
      final Map<String, dynamic> expectedResponse = <String, dynamic>{
        'success': true,
        'message': 'some message',
      };
186 187 188 189 190 191 192 193 194
      final StreamController<List<int>> controller = StreamController<List<int>>();
      final IOSink stdin = IOSink(controller.sink);
      processManager.addCommand(FakeCommand(
        command: const <String>[
          'shell',
          '--disable-observatory',
          '--non-interactive',
          '--packages=.dart_tool/package_config.json',
          'compiler_output'
195
        ], stdout: '${jsonEncode(expectedResponse)}\n',
196 197
        stdin: stdin,
      ));
198 199 200

      final TestGoldenComparator comparator = TestGoldenComparator(
        'shell',
201 202 203 204
        () => FakeTestCompiler(),
        processManager: processManager,
        fileSystem: fileSystem,
        logger: BufferLogger.test(),
205
        webRenderer: WebRendererMode.html,
206 207 208 209 210 211
      );

      final String result = await comparator.compareGoldens(testUri, imageBytes, goldenKey, false);
      expect(result, null);

      await comparator.close();
212 213
      expect(fileSystem.systemTempDirectory.listSync(recursive: true), isEmpty);
    });
214 215 216
  });
}

217 218 219 220 221
class FakeTestCompiler extends Fake implements TestCompiler {
  @override
  Future<String> compile(Uri mainDart) {
    return Future<String>.value('compiler_output');
  }
222

223 224 225
  @override
  Future<void> dispose() async { }
}