transitions_test.dart 14.4 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/rendering.dart';
9
import 'package:flutter_test/flutter_test.dart';
10 11 12

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

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

50
    late AnimationController controller;
51 52

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

56 57 58 59 60
    testWidgets('decoration test', (WidgetTester tester) async {
      final DecoratedBoxTransition transitionUnderTest =
      DecoratedBoxTransition(
        decoration: decorationTween.animate(controller),
        child: const Text(
61
          "Doesn't matter",
62 63 64 65 66 67 68 69 70
          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));
71 72 73
      expect(actualDecoration.boxShadow![0].blurRadius, 10.0);
      expect(actualDecoration.boxShadow![0].spreadRadius, 4.0);
      expect(actualDecoration.boxShadow![0].color, const Color(0x66000000));
74 75 76 77 78 79 80 81

      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
82
      expect(actualDecoration.border, isA<Border>());
83
      final Border border = actualDecoration.border! as Border;
84 85 86 87 88
      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);
89 90
      expect(actualDecoration.boxShadow![0].blurRadius, 5.0);
      expect(actualDecoration.boxShadow![0].spreadRadius, 2.0);
91
      // Scaling a shadow doesn't change the color.
92
      expect(actualDecoration.boxShadow![0].color, const Color(0x66000000));
93 94 95 96 97 98 99 100 101 102

      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);
    });
103 104

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

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

      await tester.pumpWidget(transitionUnderTest);
121 122

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

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

      controller.value = 0.5;

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

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

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

    await tester.pumpWidget(widget);

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

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

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

  testWidgets('AlignTransition keeps width and height factors', (WidgetTester tester) async {
178 179
    final AnimationController controller = AnimationController(vsync: const TestVSync());
    final Animation<Alignment> alignmentTween = AlignmentTween(
180 181
      begin: Alignment.centerLeft,
      end: Alignment.bottomRight,
182
    ).animate(controller);
183
    final Widget widget = AlignTransition(
184 185 186 187 188 189 190 191 192 193 194 195 196
      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);
  });
197 198

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

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

    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 {
230 231
    final AnimationController controller = AnimationController(vsync: const TestVSync());
    final Animation<double> animation = Tween<double>(begin: -1.0, end: 1.0).animate(controller);
232

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

    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);
  });
259 260

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

    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));
  });

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

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

    controller.value = 0.5;
    await tester.pump();
    actualRotatedBox = tester.widget(find.byType(RotationTransition));
    actualAlignment = actualRotatedBox.alignment;
306
    expect(actualAlignment, Alignment.topRight);
307
  });
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

  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);
    });
  });
410
}