animated_opacity_repaint_test.dart 3.8 KB
Newer Older
1 2 3 4 5 6 7
// 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 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
8
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
9 10

void main() {
11
  testWidgetsWithLeakTracking('RenderAnimatedOpacityMixin does not drop layer when animating to 1', (WidgetTester tester) async {
12 13 14 15
    RenderTestObject.paintCount = 0;
    final AnimationController controller = AnimationController(vsync: const TestVSync(), duration: const Duration(seconds: 1));
    final Tween<double> opacityTween = Tween<double>(begin: 0, end: 1);
    await tester.pumpWidget(
16
      ColoredBox(
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
        color: Colors.red,
        child: FadeTransition(
          opacity: controller.drive(opacityTween),
          child: const TestWidget(),
        ),
      )
    );

    expect(RenderTestObject.paintCount, 0);
    controller.forward();

    await tester.pump();
    await tester.pump(const Duration(milliseconds: 500));

    expect(RenderTestObject.paintCount, 1);

    await tester.pump();
    await tester.pump(const Duration(milliseconds: 500));

36
    expect(RenderTestObject.paintCount, 1);
37 38 39 40

    controller.stop();
    await tester.pump();

41
    expect(RenderTestObject.paintCount, 1);
42 43
  });

44
  testWidgetsWithLeakTracking('RenderAnimatedOpacityMixin avoids repainting child as it animates', (WidgetTester tester) async {
45 46 47 48
    RenderTestObject.paintCount = 0;
    final AnimationController controller = AnimationController(vsync: const TestVSync(), duration: const Duration(seconds: 1));
    final Tween<double> opacityTween = Tween<double>(begin: 0, end: 0.99); // Layer is dropped at 1
    await tester.pumpWidget(
49
      ColoredBox(
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
        color: Colors.red,
        child: FadeTransition(
          opacity: controller.drive(opacityTween),
          child: const TestWidget(),
        ),
      )
    );

    expect(RenderTestObject.paintCount, 0);
    controller.forward();

    await tester.pump();
    await tester.pump(const Duration(milliseconds: 500));

    expect(RenderTestObject.paintCount, 1);

    await tester.pump();
    await tester.pump(const Duration(milliseconds: 500));

69 70 71 72 73 74 75 76
    expect(RenderTestObject.paintCount, 1);

    controller.stop();
    await tester.pump();

    expect(RenderTestObject.paintCount, 1);
  });

77
  testWidgetsWithLeakTracking('RenderAnimatedOpacityMixin allows opacity layer to be disposed when animating to 0 opacity', (WidgetTester tester) async {
78 79
    RenderTestObject.paintCount = 0;
    final AnimationController controller = AnimationController(vsync: const TestVSync(), duration: const Duration(seconds: 1));
80
    final Tween<double> opacityTween = Tween<double>(begin: 0.99, end: 0);
81 82 83

    addTearDown(controller.dispose);

84
    await tester.pumpWidget(
85
      ColoredBox(
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 115 116 117 118 119 120 121 122 123 124 125 126 127
        color: Colors.red,
        child: FadeTransition(
          opacity: controller.drive(opacityTween),
          child: const TestWidget(),
        ),
      )
    );

    expect(RenderTestObject.paintCount, 1);
    expect(tester.layers, contains(isA<OpacityLayer>()));
    controller.forward();

    await tester.pump();
    await tester.pump(const Duration(seconds: 2));

    expect(RenderTestObject.paintCount, 1);

    controller.stop();
    await tester.pump();

    expect(tester.layers, isNot(contains(isA<OpacityLayer>())));
  });
}

class TestWidget extends SingleChildRenderObjectWidget {
  const TestWidget({super.key, super.child});

  @override
  RenderObject createRenderObject(BuildContext context) {
    return RenderTestObject();
  }
}

class RenderTestObject extends RenderProxyBox {
  static int paintCount = 0;

  @override
  void paint(PaintingContext context, Offset offset) {
    paintCount += 1;
    super.paint(context, offset);
  }
}