image_test.dart 6.54 KB
Newer Older
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
// Copyright 2016 The Chromium 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:async';
import 'dart:ui' as ui show Image;

import 'package:mojo/core.dart' as core;
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/services.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:test/test.dart';

void main() {
  test('Verify NetworkImage sets an ObjectKey on its ImageResource if it doesn\'t have a key', () {
    testWidgets((WidgetTester tester) {
      final String testUrl = 'https://foo.bar/baz1.png';
      tester.pumpWidget(
        new NetworkImage(
          scale: 1.0,
          src: testUrl
        )
      );

      ImageResource imageResource = imageCache.load(testUrl, scale: 1.0);
      expect(tester.findElementByKey(new ObjectKey(imageResource)), isNotNull);
    });
  });

  test('Verify NetworkImage doesn\'t set an ObjectKey on its ImageResource if it has a key', () {
    testWidgets((WidgetTester tester) {
      final String testUrl = 'https://foo.bar/baz2.png';
      tester.pumpWidget(
        new NetworkImage(
          key: new GlobalKey(),
          scale: 1.0,
          src: testUrl
        )
      );

      ImageResource imageResource = imageCache.load(testUrl, scale: 1.0);
      expect(tester.findElementByKey(new ObjectKey(imageResource)), isNull);
    });
  });

  test('Verify AsyncImage sets an ObjectKey on its ImageResource if it doesn\'t have a key', () {
    testWidgets((WidgetTester tester) {
      ImageProvider imageProvider = new TestImageProvider();
      tester.pumpWidget(new AsyncImage(provider: imageProvider));

      ImageResource imageResource = imageCache.loadProvider(imageProvider);
      expect(tester.findElementByKey(new ObjectKey(imageResource)), isNotNull);
    });
  });

  test('Verify AsyncImage doesn\'t set an ObjectKey on its ImageResource if it has a key', () {
    testWidgets((WidgetTester tester) {
      ImageProvider imageProvider = new TestImageProvider();
      tester.pumpWidget(
        new AsyncImage(
          key: new GlobalKey(),
          provider: imageProvider
        )
      );

      ImageResource imageResource = imageCache.loadProvider(imageProvider);
      expect(tester.findElementByKey(new ObjectKey(imageResource)), isNull);
    });
  });

  test('Verify AssetImage sets an ObjectKey on its ImageResource if it doesn\'t have a key', () {
    testWidgets((WidgetTester tester) {
      final String name = 'foo';
      final AssetBundle assetBundle = new TestAssetBundle();
      tester.pumpWidget(
        new AssetImage(
          name: name,
          bundle: assetBundle
        )
      );

      ImageResource imageResource = assetBundle.loadImage(name);
      expect(tester.findElementByKey(new ObjectKey(imageResource)), isNotNull);
    });
  });

  test('Verify AssetImage doesn\'t set an ObjectKey on its ImageResource if it has a key', () {
    testWidgets((WidgetTester tester) {
      final String name = 'foo';
      final AssetBundle assetBundle = new TestAssetBundle();
      tester.pumpWidget(
        new AssetImage(
          key: new GlobalKey(),
          name: name,
          bundle: assetBundle
        )
      );

      ImageResource imageResource = assetBundle.loadImage(name);
      expect(tester.findElementByKey(new ObjectKey(imageResource)), isNull);
    });
  });

  test('Verify AsyncImage resets its RenderImage when changing providers if it doesn\'t have a key', () {
    testWidgets((WidgetTester tester) {
      final GlobalKey key = new GlobalKey();
      TestImageProvider imageProvider1 = new TestImageProvider();
      tester.pumpWidget(
        new Container(
          key: key,
          child: new AsyncImage(
            provider: imageProvider1
          )
115 116 117
        ),
        null,
        EnginePhase.layout
118 119 120 121 122
      );
      RenderImage renderImage = key.currentContext.findRenderObject();
      expect(renderImage.image, isNull);

      imageProvider1.complete();
123 124
      tester.async.flushMicrotasks(); // resolve the future from the image provider
      tester.pump(null, EnginePhase.layout);
125 126 127 128 129 130 131 132 133 134 135

      renderImage = key.currentContext.findRenderObject();
      expect(renderImage.image, isNotNull);

      TestImageProvider imageProvider2 = new TestImageProvider();
      tester.pumpWidget(
        new Container(
          key: key,
          child: new AsyncImage(
            provider: imageProvider2
          )
136 137 138
        ),
        null,
        EnginePhase.layout
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
      );

      renderImage = key.currentContext.findRenderObject();
      expect(renderImage.image, isNull);

    });
  });

  test('Verify AsyncImage doesn\'t reset its RenderImage when changing providers if it has a key', () {
    testWidgets((WidgetTester tester) {
      final GlobalKey key = new GlobalKey();
      TestImageProvider imageProvider1 = new TestImageProvider();
      tester.pumpWidget(
        new AsyncImage(
            key: key,
            provider: imageProvider1
155 156 157
        ),
        null,
        EnginePhase.layout
158 159 160 161 162
      );
      RenderImage renderImage = key.currentContext.findRenderObject();
      expect(renderImage.image, isNull);

      imageProvider1.complete();
163 164
      tester.async.flushMicrotasks(); // resolve the future from the image provider
      tester.pump(null, EnginePhase.layout);
165 166 167 168 169 170 171 172 173

      renderImage = key.currentContext.findRenderObject();
      expect(renderImage.image, isNotNull);

      TestImageProvider imageProvider2 = new TestImageProvider();
      tester.pumpWidget(
        new AsyncImage(
          key: key,
          provider: imageProvider2
174 175 176
        ),
        null,
        EnginePhase.layout
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
      );

      renderImage = key.currentContext.findRenderObject();
      expect(renderImage.image, isNotNull);
    });
  });

}

class TestImageProvider extends ImageProvider {
  final Completer<ImageInfo> _completer = new Completer<ImageInfo>();

  @override
  Future<ImageInfo> loadImage() => _completer.future;

  void complete() {
    _completer.complete(new ImageInfo(image:new TestImage()));
  }
}

class TestAssetBundle extends AssetBundle {
  final ImageResource _imageResource = new ImageResource(new Completer<ImageInfo>().future);

  @override
  ImageResource loadImage(String key) => _imageResource;

  @override
  Future<String> loadString(String key) => new Completer<String>().future;

  @override
  Future<core.MojoDataPipeConsumer> load(String key) => new Completer<core.MojoDataPipeConsumer>().future;
}

class TestImage extends ui.Image {
  @override
  int get width => 100;

  @override
  int get height => 100;

  @override
  void dispose() {
  }
}