transitions_test.dart 9.44 KB
Newer Older
1 2 3 4 5
// 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';
6
import 'package:flutter/rendering.dart';
7 8 9 10
import 'package:flutter/widgets.dart';

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

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

    AnimationController controller;

    setUp(() {
      controller = new AnimationController(vsync: const TestVSync());
    });

    testWidgets(
55
      'decoration test',
56
      (WidgetTester tester) async {
57
        final DecoratedBoxTransition transitionUnderTest =
58 59
            new DecoratedBoxTransition(
              decoration: decorationTween.animate(controller),
Ian Hickson's avatar
Ian Hickson committed
60
              child: const Text('Doesn\'t matter', textDirection: TextDirection.ltr),
61
            );
62

63
        await tester.pumpWidget(transitionUnderTest);
64
        RenderDecoratedBox actualBox =
65 66 67
            tester.renderObject(find.byType(DecoratedBox));
        BoxDecoration actualDecoration = actualBox.decoration;

68
        expect(actualDecoration.color, const Color(0xFFFFFFFF));
69 70 71 72 73 74 75 76 77 78
        expect(actualDecoration.boxShadow[0].blurRadius, 10.0);
        expect(actualDecoration.boxShadow[0].spreadRadius, 4.0);
        expect(actualDecoration.boxShadow[0].color, const Color(0x66000000));

        controller.value = 0.5;

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

79
        expect(actualDecoration.color, const Color(0xFF7F7F7F));
Ian Hickson's avatar
Ian Hickson committed
80 81 82 83 84
        expect(actualDecoration.border, const isInstanceOf<Border>());
        final Border border = actualDecoration.border;
        expect(border.left.width, 2.5);
        expect(border.left.style, BorderStyle.solid);
        expect(border.left.color, const Color(0xFF101010));
85 86 87 88 89 90 91 92 93 94 95 96 97
        expect(actualDecoration.borderRadius, new BorderRadius.circular(5.0));
        expect(actualDecoration.shape, BoxShape.rectangle);
        expect(actualDecoration.boxShadow[0].blurRadius, 5.0);
        expect(actualDecoration.boxShadow[0].spreadRadius, 2.0);
        // Scaling a shadow doesn't change the color.
        expect(actualDecoration.boxShadow[0].color, const Color(0x66000000));

        controller.value = 1.0;

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

98
        expect(actualDecoration.color, const Color(0xFF000000));
99 100 101 102 103
        expect(actualDecoration.boxShadow, null);
      }
    );

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

      final DecoratedBoxTransition transitionUnderTest =
111 112 113
          new DecoratedBoxTransition(
            decoration: curvedDecorationAnimation,
            position: DecorationPosition.foreground,
Ian Hickson's avatar
Ian Hickson committed
114
            child: const Text('Doesn\'t matter', textDirection: TextDirection.ltr),
115 116 117
          );

      await tester.pumpWidget(transitionUnderTest);
118
      RenderDecoratedBox actualBox =
119 120 121
          tester.renderObject(find.byType(DecoratedBox));
      BoxDecoration actualDecoration = actualBox.decoration;

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

      controller.value = 0.5;

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

133
      // Same as the test above but the values should be much closer to the
134
      // tween's end values given the easeOut curve.
135
      expect(actualDecoration.color, const Color(0xFF505050));
Ian Hickson's avatar
Ian Hickson committed
136 137 138 139 140
      expect(actualDecoration.border, const isInstanceOf<Border>());
      final Border border = actualDecoration.border;
      expect(border.left.width, closeTo(1.9, 0.1));
      expect(border.left.style, BorderStyle.solid);
      expect(border.left.color, const Color(0xFF151515));
141
      expect(actualDecoration.borderRadius.resolve(TextDirection.ltr).topLeft.x, closeTo(6.8, 0.1));
142 143 144 145 146 147 148
      expect(actualDecoration.shape, BoxShape.rectangle);
      expect(actualDecoration.boxShadow[0].blurRadius, closeTo(3.1, 0.1));
      expect(actualDecoration.boxShadow[0].spreadRadius, closeTo(1.2, 0.1));
      // Scaling a shadow doesn't change the color.
      expect(actualDecoration.boxShadow[0].color, const Color(0x66000000));
    });
  });
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193

  testWidgets('AlignTransition animates', (WidgetTester tester) async {
    final AnimationController controller = new AnimationController(vsync: const TestVSync());
    final Animation<Alignment> alignmentTween = new AlignmentTween(
      begin: const Alignment(-1.0, 0.0),
      end: const Alignment(1.0, 1.0),
    ).animate(controller);
    final Widget widget = new AlignTransition(
      alignment: alignmentTween,
      child: const Text('Ready', textDirection: TextDirection.ltr),
    );

    await tester.pumpWidget(widget);

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

    Alignment actualAlignment = actualPositionedBox.alignment;
    expect(actualAlignment, const Alignment(-1.0, 0.0));

    controller.value = 0.5;
    await tester.pump();
    actualAlignment = actualPositionedBox.alignment;
    expect(actualAlignment, const Alignment(0.0, 0.5));
  });

  testWidgets('AlignTransition keeps width and height factors', (WidgetTester tester) async {
    final AnimationController controller = new AnimationController(vsync: const TestVSync());
    final Animation<Alignment> alignmentTween = new AlignmentTween(
      begin: const Alignment(-1.0, 0.0),
      end: const Alignment(1.0, 1.0),
    ).animate(controller);
    final Widget widget = new AlignTransition(
      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);
  });
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255

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

    final Widget widget =  new Directionality(
        textDirection: TextDirection.ltr,
        child: new SizeTransition(
          axis: Axis.vertical,
          sizeFactor: animation,
          child: const Text('Ready'),
        ),
      );

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

    final Widget widget =  new Directionality(
        textDirection: TextDirection.ltr,
        child: new SizeTransition(
          axis: Axis.horizontal,
          sizeFactor: animation,
          child: const Text('Ready'),
        ),
      );

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