transitions_test.dart 14.5 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
import 'dart:math' as math;

7
import 'package:flutter/material.dart';
8
import 'package:flutter_test/flutter_test.dart';
9
import 'package:flutter/rendering.dart';
10
import 'package:flutter/widgets.dart';
11 12 13

void main() {
  testWidgets('toString control test', (WidgetTester tester) async {
14
    const Widget widget = FadeTransition(
15
      opacity: kAlwaysCompleteAnimation,
16
      child: Text('Ready', textDirection: TextDirection.ltr),
17 18 19
    );
    expect(widget.toString, isNot(throwsException));
  });
20

21
  group('DecoratedBoxTransition test', () {
22 23
    final DecorationTween decorationTween = DecorationTween(
      begin: BoxDecoration(
24
        color: const Color(0xFFFFFFFF),
25
        border: Border.all(
26 27 28 29 30 31
          color: const Color(0xFF000000),
          style: BorderStyle.solid,
          width: 4.0,
        ),
        borderRadius: BorderRadius.zero,
        shape: BoxShape.rectangle,
32 33
        boxShadow: const <BoxShadow> [BoxShadow(
          color: Color(0x66000000),
34 35 36 37
          blurRadius: 10.0,
          spreadRadius: 4.0,
        )],
      ),
38
      end: BoxDecoration(
39
        color: const Color(0xFF000000),
40
        border: Border.all(
41 42 43 44
          color: const Color(0xFF202020),
          style: BorderStyle.solid,
          width: 1.0,
        ),
45
        borderRadius: BorderRadius.circular(10.0),
46 47 48 49 50
        shape: BoxShape.rectangle,
        // No shadow.
      ),
    );

51
    late AnimationController controller;
52 53

    setUp(() {
54
      controller = AnimationController(vsync: const TestVSync());
55 56
    });

57 58 59 60 61
    testWidgets('decoration test', (WidgetTester tester) async {
      final DecoratedBoxTransition transitionUnderTest =
      DecoratedBoxTransition(
        decoration: decorationTween.animate(controller),
        child: const Text(
62
          "Doesn't matter",
63 64 65 66 67 68 69 70 71
          textDirection: TextDirection.ltr,
        ),
      );

      await tester.pumpWidget(transitionUnderTest);
      RenderDecoratedBox actualBox = tester.renderObject(find.byType(DecoratedBox));
      BoxDecoration actualDecoration = actualBox.decoration as BoxDecoration;

      expect(actualDecoration.color, const Color(0xFFFFFFFF));
72 73 74
      expect(actualDecoration.boxShadow![0].blurRadius, 10.0);
      expect(actualDecoration.boxShadow![0].spreadRadius, 4.0);
      expect(actualDecoration.boxShadow![0].color, const Color(0x66000000));
75 76 77 78 79 80 81 82

      controller.value = 0.5;

      await tester.pump();
      actualBox = tester.renderObject(find.byType(DecoratedBox));
      actualDecoration = actualBox.decoration as BoxDecoration;

      expect(actualDecoration.color, const Color(0xFF7F7F7F));
Dan Field's avatar
Dan Field committed
83
      expect(actualDecoration.border, isA<Border>());
84
      final Border border = actualDecoration.border! as Border;
85 86 87 88 89
      expect(border.left.width, 2.5);
      expect(border.left.style, BorderStyle.solid);
      expect(border.left.color, const Color(0xFF101010));
      expect(actualDecoration.borderRadius, BorderRadius.circular(5.0));
      expect(actualDecoration.shape, BoxShape.rectangle);
90 91
      expect(actualDecoration.boxShadow![0].blurRadius, 5.0);
      expect(actualDecoration.boxShadow![0].spreadRadius, 2.0);
92
      // Scaling a shadow doesn't change the color.
93
      expect(actualDecoration.boxShadow![0].color, const Color(0x66000000));
94 95 96 97 98 99 100 101 102 103

      controller.value = 1.0;

      await tester.pump();
      actualBox = tester.renderObject(find.byType(DecoratedBox));
      actualDecoration = actualBox.decoration as BoxDecoration;

      expect(actualDecoration.color, const Color(0xFF000000));
      expect(actualDecoration.boxShadow, null);
    });
104 105

    testWidgets('animations work with curves test', (WidgetTester tester) async {
106
      final Animation<Decoration> curvedDecorationAnimation =
107 108 109 110 111 112 113 114 115
        decorationTween.animate(CurvedAnimation(
        parent: controller,
        curve: Curves.easeOut,
      ));

      final DecoratedBoxTransition transitionUnderTest = DecoratedBoxTransition(
        decoration: curvedDecorationAnimation,
        position: DecorationPosition.foreground,
        child: const Text(
116
          "Doesn't matter",
117 118 119
          textDirection: TextDirection.ltr,
        ),
      );
120 121

      await tester.pumpWidget(transitionUnderTest);
122 123

      RenderDecoratedBox actualBox = tester.renderObject(find.byType(DecoratedBox));
124
      BoxDecoration actualDecoration = actualBox.decoration as BoxDecoration;
125

126
      expect(actualDecoration.color, const Color(0xFFFFFFFF));
127 128 129
      expect(actualDecoration.boxShadow![0].blurRadius, 10.0);
      expect(actualDecoration.boxShadow![0].spreadRadius, 4.0);
      expect(actualDecoration.boxShadow![0].color, const Color(0x66000000));
130 131 132 133 134

      controller.value = 0.5;

      await tester.pump();
      actualBox = tester.renderObject(find.byType(DecoratedBox));
135
      actualDecoration = actualBox.decoration as BoxDecoration;
136

137
      // Same as the test above but the values should be much closer to the
138
      // tween's end values given the easeOut curve.
139
      expect(actualDecoration.color, const Color(0xFF505050));
Dan Field's avatar
Dan Field committed
140
      expect(actualDecoration.border, isA<Border>());
141
      final Border border = actualDecoration.border! as Border;
142
      expect(border.left.width, moreOrLessEquals(1.9, epsilon: 0.1));
Ian Hickson's avatar
Ian Hickson committed
143 144
      expect(border.left.style, BorderStyle.solid);
      expect(border.left.color, const Color(0xFF151515));
145
      expect(actualDecoration.borderRadius!.resolve(TextDirection.ltr).topLeft.x, moreOrLessEquals(6.8, epsilon: 0.1));
146
      expect(actualDecoration.shape, BoxShape.rectangle);
147 148
      expect(actualDecoration.boxShadow![0].blurRadius, moreOrLessEquals(3.1, epsilon: 0.1));
      expect(actualDecoration.boxShadow![0].spreadRadius, moreOrLessEquals(1.2, epsilon: 0.1));
149
      // Scaling a shadow doesn't change the color.
150
      expect(actualDecoration.boxShadow![0].color, const Color(0x66000000));
151 152
    });
  });
153 154

  testWidgets('AlignTransition animates', (WidgetTester tester) async {
155 156
    final AnimationController controller = AnimationController(vsync: const TestVSync());
    final Animation<Alignment> alignmentTween = AlignmentTween(
157 158
      begin: Alignment.centerLeft,
      end: Alignment.bottomRight,
159
    ).animate(controller);
160
    final Widget widget = AlignTransition(
161 162 163 164 165 166 167 168
      alignment: alignmentTween,
      child: const Text('Ready', textDirection: TextDirection.ltr),
    );

    await tester.pumpWidget(widget);

    final RenderPositionedBox actualPositionedBox = tester.renderObject(find.byType(Align));

169
    Alignment actualAlignment = actualPositionedBox.alignment as Alignment;
170
    expect(actualAlignment, Alignment.centerLeft);
171 172 173

    controller.value = 0.5;
    await tester.pump();
174
    actualAlignment = actualPositionedBox.alignment as Alignment;
175 176 177 178
    expect(actualAlignment, const Alignment(0.0, 0.5));
  });

  testWidgets('AlignTransition keeps width and height factors', (WidgetTester tester) async {
179 180
    final AnimationController controller = AnimationController(vsync: const TestVSync());
    final Animation<Alignment> alignmentTween = AlignmentTween(
181 182
      begin: Alignment.centerLeft,
      end: Alignment.bottomRight,
183
    ).animate(controller);
184
    final Widget widget = AlignTransition(
185 186 187 188 189 190 191 192 193 194 195 196 197
      alignment: alignmentTween,
      child: const Text('Ready', textDirection: TextDirection.ltr),
      widthFactor: 0.3,
      heightFactor: 0.4,
    );

    await tester.pumpWidget(widget);

    final Align actualAlign = tester.widget(find.byType(Align));

    expect(actualAlign.widthFactor, 0.3);
    expect(actualAlign.heightFactor, 0.4);
  });
198 199

  testWidgets('SizeTransition clamps negative size factors - vertical axis', (WidgetTester tester) async {
200 201
    final AnimationController controller = AnimationController(vsync: const TestVSync());
    final Animation<double> animation = Tween<double>(begin: -1.0, end: 1.0).animate(controller);
202

203
    final Widget widget =  Directionality(
204 205 206 207 208 209 210
      textDirection: TextDirection.ltr,
      child: SizeTransition(
        axis: Axis.vertical,
        sizeFactor: animation,
        child: const Text('Ready'),
      ),
    );
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230

    await tester.pumpWidget(widget);

    final RenderPositionedBox actualPositionedBox = tester.renderObject(find.byType(Align));
    expect(actualPositionedBox.heightFactor, 0.0);

    controller.value = 0.0;
    await tester.pump();
    expect(actualPositionedBox.heightFactor, 0.0);

    controller.value = 0.75;
    await tester.pump();
    expect(actualPositionedBox.heightFactor, 0.5);

    controller.value = 1.0;
    await tester.pump();
    expect(actualPositionedBox.heightFactor, 1.0);
  });

  testWidgets('SizeTransition clamps negative size factors - horizontal axis', (WidgetTester tester) async {
231 232
    final AnimationController controller = AnimationController(vsync: const TestVSync());
    final Animation<double> animation = Tween<double>(begin: -1.0, end: 1.0).animate(controller);
233

234
    final Widget widget =  Directionality(
235 236 237 238 239 240 241
      textDirection: TextDirection.ltr,
      child: SizeTransition(
        axis: Axis.horizontal,
        sizeFactor: animation,
        child: const Text('Ready'),
      ),
    );
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259

    await tester.pumpWidget(widget);

    final RenderPositionedBox actualPositionedBox = tester.renderObject(find.byType(Align));
    expect(actualPositionedBox.widthFactor, 0.0);

    controller.value = 0.0;
    await tester.pump();
    expect(actualPositionedBox.widthFactor, 0.0);

    controller.value = 0.75;
    await tester.pump();
    expect(actualPositionedBox.widthFactor, 0.5);

    controller.value = 1.0;
    await tester.pump();
    expect(actualPositionedBox.widthFactor, 1.0);
  });
260 261

  testWidgets('RotationTransition animates', (WidgetTester tester) async {
262 263
    final AnimationController controller = AnimationController(vsync: const TestVSync());
    final Widget widget = RotationTransition(
264 265
      alignment: Alignment.topRight,
      turns: controller,
266 267 268 269
      child: const Text(
        'Rotation',
        textDirection: TextDirection.ltr,
      ),
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
    );

    await tester.pumpWidget(widget);
    Transform actualRotatedBox = tester.widget(find.byType(Transform));
    Matrix4 actualTurns = actualRotatedBox.transform;
    expect(actualTurns, equals(Matrix4.rotationZ(0.0)));

    controller.value = 0.5;
    await tester.pump();
    actualRotatedBox = tester.widget(find.byType(Transform));
    actualTurns = actualRotatedBox.transform;
    expect(actualTurns, Matrix4.rotationZ(math.pi));

    controller.value = 0.75;
    await tester.pump();
    actualRotatedBox = tester.widget(find.byType(Transform));
    actualTurns = actualRotatedBox.transform;
    expect(actualTurns, Matrix4.rotationZ(math.pi * 1.5));
  });

290
  testWidgets('RotationTransition maintains chosen alignment during animation', (WidgetTester tester) async {
291 292
    final AnimationController controller = AnimationController(vsync: const TestVSync());
    final Widget widget = RotationTransition(
293 294 295 296 297 298
      alignment: Alignment.topRight,
      turns: controller,
      child: const Text('Rotation', textDirection: TextDirection.ltr),
    );

    await tester.pumpWidget(widget);
299
    RotationTransition actualRotatedBox = tester.widget(find.byType(RotationTransition));
300
    Alignment actualAlignment = actualRotatedBox.alignment;
301
    expect(actualAlignment, Alignment.topRight);
302 303 304 305 306

    controller.value = 0.5;
    await tester.pump();
    actualRotatedBox = tester.widget(find.byType(RotationTransition));
    actualAlignment = actualRotatedBox.alignment;
307
    expect(actualAlignment, Alignment.topRight);
308
  });
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410

  group('FadeTransition', () {
    double _getOpacity(WidgetTester tester, String textValue) {
      final FadeTransition opacityWidget = tester.widget<FadeTransition>(
        find.ancestor(
          of: find.text(textValue),
          matching: find.byType(FadeTransition),
        ).first,
      );
      return opacityWidget.opacity.value;
    }
    testWidgets('animates', (WidgetTester tester) async {
      final AnimationController controller = AnimationController(vsync: const TestVSync());
      final Animation<double> animation = Tween<double>(begin: 0.0, end: 1.0).animate(controller);
      final Widget widget =  Directionality(
        textDirection: TextDirection.ltr,
        child: FadeTransition(
          opacity: animation,
          child: const Text('Fade In'),
        ),
      );

      await tester.pumpWidget(widget);

      expect(_getOpacity(tester, 'Fade In'), 0.0);

      controller.value = 0.25;
      await tester.pump();
      expect(_getOpacity(tester, 'Fade In'), 0.25);

      controller.value = 0.5;
      await tester.pump();
      expect(_getOpacity(tester, 'Fade In'), 0.5);

      controller.value = 0.75;
      await tester.pump();
      expect(_getOpacity(tester, 'Fade In'), 0.75);

      controller.value = 1.0;
      await tester.pump();
      expect(_getOpacity(tester, 'Fade In'), 1.0);
    });
  });

  group('SliverFadeTransition', () {
    double _getOpacity(WidgetTester tester, String textValue) {
      final SliverFadeTransition opacityWidget = tester.widget<SliverFadeTransition>(
        find.ancestor(
          of: find.text(textValue),
          matching: find.byType(SliverFadeTransition),
        ).first,
      );
      return opacityWidget.opacity.value;
    }
    testWidgets('animates', (WidgetTester tester) async {
      final AnimationController controller = AnimationController(vsync: const TestVSync());
      final Animation<double> animation = Tween<double>(begin: 0.0, end: 1.0).animate(controller);
      final Widget widget = Localizations(
        locale: const Locale('en', 'us'),
        delegates: const <LocalizationsDelegate<dynamic>>[
          DefaultWidgetsLocalizations.delegate,
          DefaultMaterialLocalizations.delegate,
        ],
        child: Directionality(
          textDirection: TextDirection.ltr,
          child: MediaQuery(
            data: const MediaQueryData(),
            child: CustomScrollView(
              slivers: <Widget>[
                SliverFadeTransition(
                  opacity: animation,
                  sliver: const SliverToBoxAdapter(
                    child: Text('Fade In'),
                  ),
                ),
              ]
            )
          )
        )
      );

      await tester.pumpWidget(widget);

      expect(_getOpacity(tester, 'Fade In'), 0.0);

      controller.value = 0.25;
      await tester.pump();
      expect(_getOpacity(tester, 'Fade In'), 0.25);

      controller.value = 0.5;
      await tester.pump();
      expect(_getOpacity(tester, 'Fade In'), 0.5);

      controller.value = 0.75;
      await tester.pump();
      expect(_getOpacity(tester, 'Fade In'), 0.75);

      controller.value = 1.0;
      await tester.pump();
      expect(_getOpacity(tester, 'Fade In'), 1.0);
    });
  });
411
}