_goldens_web.dart 3.44 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6 7
import 'dart:convert';
import 'dart:html' as html;
import 'dart:typed_data';
8

9
import 'package:matcher/expect.dart' show fail;
10

11 12 13
import 'goldens.dart';

/// An unsupported [GoldenFileComparator] that exists for API compatibility.
14 15 16 17 18 19
class LocalFileComparator extends GoldenFileComparator {
  @override
  Future<bool> compare(Uint8List imageBytes, Uri golden) {
    throw UnsupportedError('LocalFileComparator is not supported on the web.');
  }

20
  @override
21 22 23 24 25
  Future<void> update(Uri golden, Uint8List imageBytes) {
    throw UnsupportedError('LocalFileComparator is not supported on the web.');
  }
}

26
/// Returns whether [test] and [master] are pixel by pixel identical.
27 28 29
///
/// This method is not supported on the web and throws an [UnsupportedError]
/// when called.
30
Future<ComparisonResult> compareLists(List<int> test, List<int> master) async {
31 32
  throw UnsupportedError('Golden testing is not supported on the web.');
}
33 34 35 36 37 38 39 40 41 42 43

/// 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 from [flutter_test] that invokes the
///    comparator.
class DefaultWebGoldenComparator extends WebGoldenComparator {
44
  /// Creates a new [DefaultWebGoldenComparator] for the specified [testFile].
45 46
  ///
  /// Golden file keys will be interpreted as file paths relative to the
47
  /// directory in which [testFile] resides.
48
  ///
49
  /// The [testFile] URL must represent a file.
50 51 52 53 54 55 56 57 58
  DefaultWebGoldenComparator(this.testUri);

  /// The test file currently being executed.
  ///
  /// Golden file keys will be interpreted as file paths relative to the
  /// directory in which this file resides.
  Uri testUri;

  @override
59
  Future<bool> compare(double width, double height, Uri golden) async {
60 61 62 63 64 65
    final String key = golden.toString();
    final html.HttpRequest request = await html.HttpRequest.request(
      'flutter_goldens',
      method: 'POST',
      sendData: json.encode(<String, Object>{
        'testUri': testUri.toString(),
66
        'key': key,
67 68
        'width': width.round(),
        'height': height.round(),
69 70 71 72 73 74
      }),
    );
    final String response = request.response as String;
    if (response == 'true') {
      return true;
    }
75
    fail(response);
76 77 78
  }

  @override
79
  Future<void> update(double width, double height, Uri golden) async {
80
    // Update is handled on the server side, just use the same logic here
81
    await compare(width, height, golden);
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

  @override
  Future<bool> compareBytes(Uint8List bytes, Uri golden) async {
    final String key = golden.toString();
    final String bytesEncoded = base64.encode(bytes);
    final html.HttpRequest request = await html.HttpRequest.request(
      'flutter_goldens',
      method: 'POST',
      sendData: json.encode(<String, Object>{
        'testUri': testUri.toString(),
        'key': key,
        'bytes': bytesEncoded,
      }),
    );
    final String response = request.response as String;
    if (response == 'true') {
      return true;
    }
    fail(response);
  }

  @override
  Future<void> updateBytes(Uint8List bytes, Uri golden) async {
    // Update is handled on the server side, just use the same logic here
    await compareBytes(bytes, golden);
  }
109
}