File size: 4,963 Bytes
88b9d23 346a42c 88b9d23 346a42c 88b9d23 346a42c 88b9d23 346a42c 88b9d23 346a42c 88b9d23 346a42c 88b9d23 346a42c 88b9d23 346a42c 88b9d23 346a42c 88b9d23 346a42c 88b9d23 346a42c 88b9d23 346a42c 88b9d23 346a42c 88b9d23 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | // 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.
/// @docImport 'matchers.dart';
library;
import 'dart:convert';
import 'dart:js_interop';
import 'dart:typed_data';
import 'package:matcher/expect.dart' show fail;
import 'goldens.dart';
import 'web.dart' as web;
/// An unsupported [GoldenFileComparator] that exists for API compatibility.
class LocalFileComparator extends GoldenFileComparator {
@override
Future<bool> compare(Uint8List imageBytes, Uri golden) {
throw UnsupportedError('LocalFileComparator is not supported on the web.');
}
@override
Future<void> update(Uri golden, Uint8List imageBytes) {
throw UnsupportedError('LocalFileComparator is not supported on the web.');
}
}
/// Returns whether [test] and [master] are pixel by pixel identical.
///
/// This method is not supported on the web and throws an [UnsupportedError]
/// when called.
Future<ComparisonResult> compareLists(List<int> test, List<int> master) async {
throw UnsupportedError('Golden testing is not supported on the web.');
}
/// Implements [GoldenFileComparator] by proxying calls to an HTTP service `/flutter_goldens`.
final class HttpProxyGoldenComparator extends GoldenFileComparator {
/// Creates a comparator with the given test file being executed.
///
/// Golden file keys will be interpreted as file paths relative to the
/// directory in which this file resides.
HttpProxyGoldenComparator(this._testUri);
final Uri _testUri;
@override
Future<bool> compare(Uint8List bytes, Uri golden) async {
final String key = golden.toString();
final String bytesEncoded = base64.encode(bytes);
final web.Response response =
await web.window
.fetch(
'flutter_goldens'.toJS,
web.RequestInit(
method: 'POST',
body:
json.encode(<String, Object>{
'testUri': _testUri.toString(),
'key': key,
'bytes': bytesEncoded,
}).toJS,
),
)
.toDart;
final String responseText = (await response.text().toDart).toDart;
if (responseText == 'true') {
return true;
}
fail(responseText);
}
@override
Future<void> update(Uri golden, Uint8List bytes) async {
// Update is handled on the server side, just use the same logic here
await compare(bytes, golden);
}
}
/// The default [WebGoldenComparator] implementation for `flutter test`.
///
/// This comparator will send a request to the test server for golden comparison
/// which will then defer the comparison to [goldenFileComparator].
///
/// See also:
///
/// * [matchesGoldenFile], the function that invokes the comparator.
@Deprecated(
'Use goldenFileComparator instead. '
'This feature was deprecated after v3.28.0-0.1.pre.',
)
class DefaultWebGoldenComparator extends WebGoldenComparator {
/// Creates a new [DefaultWebGoldenComparator] for the specified [testUri].
///
/// Golden file keys will be interpreted as file paths relative to the
/// directory in which [testUri] resides.
///
/// The [testUri] must represent a file.
@Deprecated(
'Use an implementation of GoldenFileComparator instead. '
'This feature was deprecated after v3.28.0-0.1.pre.',
)
DefaultWebGoldenComparator(Uri testUri) : _comparatorImpl = HttpProxyGoldenComparator(testUri);
// TODO(matanlurey): Refactor as part of https://github.com/flutter/flutter/issues/160261.
final HttpProxyGoldenComparator _comparatorImpl;
@override
Future<bool> compare(double width, double height, Uri golden) async {
final String key = golden.toString();
final web.Response response =
await web.window
.fetch(
'flutter_goldens'.toJS,
web.RequestInit(
method: 'POST',
body:
json.encode(<String, Object>{
'testUri': _comparatorImpl._testUri.toString(),
'key': key,
'width': width.round(),
'height': height.round(),
}).toJS,
),
)
.toDart;
final String responseText = (await response.text().toDart).toDart;
if (responseText == 'true') {
return true;
}
fail(responseText);
}
@override
Future<void> update(double width, double height, Uri golden) async {
// Update is handled on the server side, just use the same logic here
await compare(width, height, golden);
}
@override
Future<bool> compareBytes(Uint8List bytes, Uri golden) async {
return _comparatorImpl.compare(bytes, golden);
}
@override
Future<void> updateBytes(Uint8List bytes, Uri golden) async {
await _comparatorImpl.update(golden, bytes);
}
}
|