animated_size_test.dart 5.18 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
// 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 'package:flutter_test/flutter_test.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';

class TestPaintingContext implements PaintingContext {
  final List<Invocation> invocations = <Invocation>[];

  @override
    void noSuchMethod(Invocation invocation) {
      invocations.add(invocation);
    }
}

void main() {
  testWidgets('AnimatedSize test', (WidgetTester tester) async {
    await tester.pumpWidget(
      new Center(
        child: new AnimatedSize(
          duration: const Duration(milliseconds: 200),
24
          vsync: tester,
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
          child: new SizedBox(
            width: 100.0,
            height: 100.0
          )
        )
      )
    );

    RenderBox box = tester.renderObject(find.byType(AnimatedSize));
    expect(box.size.width, equals(100.0));
    expect(box.size.height, equals(100.0));

    await tester.pumpWidget(
      new Center(
        child: new AnimatedSize(
          duration: new Duration(milliseconds: 200),
41
          vsync: tester,
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
          child: new SizedBox(
            width: 200.0,
            height: 200.0
          )
        )
      )
    );

    await tester.pump(const Duration(milliseconds: 100));
    box = tester.renderObject(find.byType(AnimatedSize));
    expect(box.size.width, equals(150.0));
    expect(box.size.height, equals(150.0));

    TestPaintingContext context = new TestPaintingContext();
    box.paint(context, Offset.zero);
    expect(context.invocations.first.memberName, equals(#pushClipRect));

    await tester.pump(const Duration(milliseconds: 100));
    box = tester.renderObject(find.byType(AnimatedSize));
    expect(box.size.width, equals(200.0));
    expect(box.size.height, equals(200.0));

    await tester.pumpWidget(
      new Center(
        child: new AnimatedSize(
          duration: new Duration(milliseconds: 200),
68
          vsync: tester,
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
          child: new SizedBox(
            width: 100.0,
            height: 100.0
          )
        )
      )
    );

    await tester.pump(const Duration(milliseconds: 100));
    box = tester.renderObject(find.byType(AnimatedSize));
    expect(box.size.width, equals(150.0));
    expect(box.size.height, equals(150.0));

    context = new TestPaintingContext();
    box.paint(context, Offset.zero);
    expect(context.invocations.first.memberName, equals(#paintChild));

    await tester.pump(const Duration(milliseconds: 100));
    box = tester.renderObject(find.byType(AnimatedSize));
    expect(box.size.width, equals(100.0));
    expect(box.size.height, equals(100.0));
  });

  testWidgets('AnimatedSize constrained test', (WidgetTester tester) async {
    await tester.pumpWidget(
      new Center(
        child: new SizedBox (
          width: 100.0,
          height: 100.0,
          child: new AnimatedSize(
            duration: const Duration(milliseconds: 200),
100
            vsync: tester,
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
            child: new SizedBox(
              width: 100.0,
              height: 100.0
            )
          )
        )
      )
    );

    RenderBox box = tester.renderObject(find.byType(AnimatedSize));
    expect(box.size.width, equals(100.0));
    expect(box.size.height, equals(100.0));

    await tester.pumpWidget(
      new Center(
        child: new SizedBox (
          width: 100.0,
          height: 100.0,
          child: new AnimatedSize(
            duration: const Duration(milliseconds: 200),
121
            vsync: tester,
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
            child: new SizedBox(
              width: 200.0,
              height: 200.0
            )
          )
        )
      )
    );

    await tester.pump(const Duration(milliseconds: 100));
    box = tester.renderObject(find.byType(AnimatedSize));
    expect(box.size.width, equals(100.0));
    expect(box.size.height, equals(100.0));
  });

  testWidgets('AnimatedSize with AnimatedContainer', (WidgetTester tester) async {
    await tester.pumpWidget(
      new Center(
        child: new AnimatedSize(
          duration: const Duration(milliseconds: 200),
142
          vsync: tester,
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
          child: new AnimatedContainer(
            duration: const Duration(milliseconds: 100),
            width: 100.0,
            height: 100.0
          )
        )
      )
    );

    RenderBox box = tester.renderObject(find.byType(AnimatedSize));
    expect(box.size.width, equals(100.0));
    expect(box.size.height, equals(100.0));

    await tester.pumpWidget(
      new Center(
        child: new AnimatedSize(
          duration: const Duration(milliseconds: 200),
160
          vsync: tester,
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
          child: new AnimatedContainer(
            duration: const Duration(milliseconds: 100),
            width: 200.0,
            height: 200.0
          )
        )
      )
    );

    await tester.pump(const Duration(milliseconds: 1)); // register change
    await tester.pump(const Duration(milliseconds: 49));
    expect(box.size.width, equals(150.0));
    expect(box.size.height, equals(150.0));
    await tester.pump(const Duration(milliseconds: 50));
    box = tester.renderObject(find.byType(AnimatedSize));
    expect(box.size.width, equals(200.0));
    expect(box.size.height, equals(200.0));
  });
}