_goldens_web.dart 2.81 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
// @dart = 2.10
6 7 8
import 'dart:convert';
import 'dart:html' as html;
import 'dart:typed_data';
9

10 11
// ignore: deprecated_member_use
import 'package:test_api/test_api.dart' as test_package show TestFailure;
12

13 14 15
import 'goldens.dart';

/// An unsupported [GoldenFileComparator] that exists for API compatibility.
16 17 18 19 20 21 22 23 24 25 26 27
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.');
  }
}

28
/// Returns whether [test] and [master] are pixel by pixel identical.
29 30 31
///
/// This method is not supported on the web and throws an [UnsupportedError]
/// when called.
32
Future<ComparisonResult> compareLists(List<int> test, List<int> master) async {
33 34
  throw UnsupportedError('Golden testing is not supported on the web.');
}
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

/// 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 {
  /// Creates a new [DefaultWebGoldenComparator] for the specified [testFile].
  ///
  /// Golden file keys will be interpreted as file paths relative to the
  /// directory in which [testFile] resides.
  ///
  /// The [testFile] URL must represent a file.
  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
61
  Future<bool> compare(double width, double height, Uri golden) async {
62 63 64 65 66 67 68
    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(),
        'key': key.toString(),
69 70
        'width': width.round(),
        'height': height.round(),
71 72 73 74 75 76 77 78 79 80 81
      }),
    );
    final String response = request.response as String;
    if (response == 'true') {
      return true;
    } else {
      throw test_package.TestFailure(response);
    }
  }

  @override
82
  Future<void> update(double width, double height, Uri golden) async {
83
    // Update is handled on the server side, just use the same logic here
84
    await compare(width, height, golden);
85 86
  }
}