_matchers_io.dart 4.1 KB
Newer Older
1 2 3 4 5 6 7 8
// 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.

import 'dart:typed_data';
import 'dart:ui' as ui;

import 'package:flutter/rendering.dart';
9
import 'package:flutter/widgets.dart';
10
import 'package:test_api/src/expect/async_matcher.dart'; // ignore: implementation_imports
11
import 'package:test_api/test_api.dart'; // ignore: deprecated_member_use
12 13 14 15 16 17 18 19 20 21 22

import 'binding.dart';
import 'finders.dart';
import 'goldens.dart';

/// Render the closest [RepaintBoundary] of the [element] into an image.
///
/// See also:
///
///  * [OffsetLayer.toImage] which is the actual method being called.
Future<ui.Image> captureImage(Element element) {
23 24
  assert(element.renderObject != null);
  RenderObject renderObject = element.renderObject!;
25
  while (!renderObject.isRepaintBoundary) {
26
    renderObject = renderObject.parent! as RenderObject;
27 28
  }
  assert(!renderObject.debugNeedsPaint);
29
  final OffsetLayer layer = renderObject.debugLayer! as OffsetLayer;
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
  return layer.toImage(renderObject.paintBounds);
}

/// The matcher created by [matchesGoldenFile]. This class is enabled when the
/// test is running on a VM using conditional import.
class MatchesGoldenFile extends AsyncMatcher {
  /// Creates an instance of [MatchesGoldenFile]. Called by [matchesGoldenFile].
  const MatchesGoldenFile(this.key, this.version);

  /// Creates an instance of [MatchesGoldenFile]. Called by [matchesGoldenFile].
  MatchesGoldenFile.forStringPath(String path, this.version) : key = Uri.parse(path);

  /// The [key] to the golden image.
  final Uri key;

  /// The [version] of the golden image.
46
  final int? version;
47 48

  @override
49
  Future<String?> matchAsync(dynamic item) async {
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    final Uri testNameUri = goldenFileComparator.getTestUri(key, version);

    Uint8List? buffer;
    if (item is Future<List<int>>) {
      buffer = Uint8List.fromList(await item);
    } else if (item is List<int>) {
      buffer = Uint8List.fromList(item);
    }
    if (buffer != null) {
      if (autoUpdateGoldenFiles) {
        await goldenFileComparator.update(testNameUri, buffer);
        return null;
      }
      try {
        final bool success = await goldenFileComparator.compare(buffer, testNameUri);
        return success ? null : 'does not match';
      } on TestFailure catch (ex) {
        return ex.message;
      }
    }
70 71
    Future<ui.Image?> imageFuture;
    if (item is Future<ui.Image?>) {
72 73 74
      imageFuture = item;
    } else if (item is ui.Image) {
      imageFuture = Future<ui.Image>.value(item);
75 76
    } else if (item is Finder) {
      final Iterable<Element> elements = item.evaluate();
77 78 79 80 81 82
      if (elements.isEmpty) {
        return 'could not be rendered because no widget was found';
      } else if (elements.length > 1) {
        return 'matched too many widgets';
      }
      imageFuture = captureImage(elements.single);
83
    } else {
84
      throw AssertionError('must provide a Finder, Image, Future<Image>, List<int>, or Future<List<int>>');
85 86
    }

87
    final TestWidgetsFlutterBinding binding = TestWidgetsFlutterBinding.ensureInitialized();
88
    return binding.runAsync<String?>(() async {
89 90
      final ui.Image? image = await imageFuture;
      if (image == null) {
91
        throw AssertionError('Future<Image> completed to null');
92
      }
93
      final ByteData? bytes = await image.toByteData(format: ui.ImageByteFormat.png);
94
      if (bytes == null) {
95
        return 'could not encode screenshot.';
96
      }
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
      if (autoUpdateGoldenFiles) {
        await goldenFileComparator.update(testNameUri, bytes.buffer.asUint8List());
        return null;
      }
      try {
        final bool success = await goldenFileComparator.compare(bytes.buffer.asUint8List(), testNameUri);
        return success ? null : 'does not match';
      } on TestFailure catch (ex) {
        return ex.message;
      }
    }, additionalTime: const Duration(minutes: 1));
  }

  @override
  Description describe(Description description) {
    final Uri testNameUri = goldenFileComparator.getTestUri(key, version);
    return description.add('one widget whose rasterized image matches golden image "$testNameUri"');
  }
}