• amirh's avatar
    Don't keep image stream listeners when TickerMode is disabled. (#13106) · 155a4cab
    amirh authored
    This makes sure that we don't decode frames when we don't need to run
    the animation.
    
    For example, prior to this CL: an animated image in a page that is not
    currently at the top of the page stack (it is covered by another page),
    will keep the the image stream listener, and thus frames will still be
    decoded.
    155a4cab
obscured_animated_image.dart 1.78 KB
// Copyright 2017 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:typed_data';
import 'dart:ui' as ui show Image;

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';

import '../services/fake_codec.dart';
import '../services/fake_image_provider.dart';
import '../services/image_data.dart';

Future<Null> main() async {
  final FakeCodec fakeCodec = await FakeCodec.fromData(new Uint8List.fromList(kAnimatedGif));
  final FakeImageProvider fakeImageProvider = new FakeImageProvider(fakeCodec);

  testWidgets('Obscured image does not animate', (WidgetTester tester) async {
    final GlobalKey imageKey = new GlobalKey();
    await tester.pumpWidget(
      new MaterialApp(
        home: new Image(image: fakeImageProvider, key: imageKey),
        routes: <String, WidgetBuilder> {
          '/page': (BuildContext context) => new Container()
        }
      )
    );
    final RenderImage renderImage = tester.renderObject(find.byType(Image));
    final ui.Image image1 = renderImage.image;
    await tester.pump(const Duration(milliseconds: 100));
    final ui.Image image2 = renderImage.image;
    expect(image1, isNot(same(image2)));


    Navigator.pushNamed(imageKey.currentContext, '/page');
    await tester.pump(); // Starts the page animation.
    await tester.pump(const Duration(seconds: 1)); // Let the page animation complete.

    // The image is now obscured by another page, it should not be changing
    // frames.
    final ui.Image image3 = renderImage.image;
    await tester.pump(const Duration(milliseconds: 100));
    final ui.Image image4 = renderImage.image;
    expect(image3, same(image4));
  });
}