animated_icons_private_test.dart 13.9 KB
Newer Older
1 2 3 4 5
// Copyright 2017 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.

// This is the test for the private implementation of animated icons.
6
// To make the private API accessible from the test we do not import the
7 8 9 10 11 12 13 14 15 16
// material material_animated_icons library, but instead, this test file is an
// implementation of that library, using some of the parts of the real
// material_animated_icons, this give the test access to the private APIs.
library material_animated_icons;

import 'dart:math' as math show pi;
import 'dart:ui' show lerpDouble;
import 'dart:ui' as ui show Paint, Path, Canvas;

import 'package:flutter/animation.dart';
17
import 'package:flutter/widgets.dart';
18
import 'package:meta/meta.dart';
19
import 'package:mockito/mockito.dart';
20
import 'package:test/test.dart';
21

22 23
part 'package:flutter/src/material/animated_icons/animated_icons.dart';
part 'package:flutter/src/material/animated_icons/animated_icons_data.dart';
24 25 26 27 28

// We have to import all the generated files in the material library to avoid
// analysis errors (as the generated constants are all referenced in the
// animated_icons library).
part 'package:flutter/src/material/animated_icons/data/add_event.g.dart';
29
part 'package:flutter/src/material/animated_icons/data/arrow_menu.g.dart';
30 31 32 33 34
part 'package:flutter/src/material/animated_icons/data/close_menu.g.dart';
part 'package:flutter/src/material/animated_icons/data/ellipsis_search.g.dart';
part 'package:flutter/src/material/animated_icons/data/event_add.g.dart';
part 'package:flutter/src/material/animated_icons/data/home_menu.g.dart';
part 'package:flutter/src/material/animated_icons/data/list_view.g.dart';
35
part 'package:flutter/src/material/animated_icons/data/menu_arrow.g.dart';
36 37 38 39 40 41
part 'package:flutter/src/material/animated_icons/data/menu_close.g.dart';
part 'package:flutter/src/material/animated_icons/data/menu_home.g.dart';
part 'package:flutter/src/material/animated_icons/data/pause_play.g.dart';
part 'package:flutter/src/material/animated_icons/data/play_pause.g.dart';
part 'package:flutter/src/material/animated_icons/data/search_ellipsis.g.dart';
part 'package:flutter/src/material/animated_icons/data/view_list.g.dart';
42 43 44 45 46 47 48

class MockCanvas extends Mock implements ui.Canvas {}
class MockPath extends Mock implements ui.Path {}

void main () {
  group('Interpolate points', () {
    test('- single point', () {
49
      const List<Offset> points = const <Offset>[
50 51 52 53 54 55 56 57
        const Offset(25.0, 1.0),
      ];
      expect(_interpolate(points, 0.0, Offset.lerp), const Offset(25.0, 1.0));
      expect(_interpolate(points, 0.5, Offset.lerp), const Offset(25.0, 1.0));
      expect(_interpolate(points, 1.0, Offset.lerp), const Offset(25.0, 1.0));
    });

    test('- two points', () {
58
      const List<Offset> points = const <Offset>[
59 60 61 62 63 64 65 66 67
        const Offset(25.0, 1.0),
        const Offset(12.0, 12.0),
      ];
      expect(_interpolate(points, 0.0, Offset.lerp), const Offset(25.0, 1.0));
      expect(_interpolate(points, 0.5, Offset.lerp), const Offset(18.5, 6.5));
      expect(_interpolate(points, 1.0, Offset.lerp), const Offset(12.0, 12.0));
    });

    test('- three points', () {
68
      const List<Offset> points = const <Offset>[
69 70 71 72 73 74 75 76 77 78 79 80 81
        const Offset(25.0, 1.0),
        const Offset(12.0, 12.0),
        const Offset(23.0, 9.0),
      ];
      expect(_interpolate(points, 0.0, Offset.lerp), const Offset(25.0, 1.0));
      expect(_interpolate(points, 0.25, Offset.lerp), const Offset(18.5, 6.5));
      expect(_interpolate(points, 0.5, Offset.lerp), const Offset(12.0, 12.0));
      expect(_interpolate(points, 0.75, Offset.lerp), const Offset(17.5, 10.5));
      expect(_interpolate(points, 1.0, Offset.lerp), const Offset(23.0, 9.0));
    });
  });

  group('_AnimatedIconPainter', () {
82
    const Size size = const Size(48.0, 48.0);
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    final MockCanvas mockCanvas = new MockCanvas();
    List<MockPath> generatedPaths;
    final _UiPathFactory pathFactory = () {
      final MockPath path = new MockPath();
      generatedPaths.add(path);
      return path;
    };

    setUp(() {
      generatedPaths = <MockPath> [];
    });

    test('progress 0', () {
      final _AnimatedIconPainter painter = new _AnimatedIconPainter(
        paths: movingBar.paths,
        progress: const AlwaysStoppedAnimation<double>(0.0),
        color: const Color(0xFF00FF00),
        scale: 1.0,
        shouldMirror: false,
        uiPathFactory: pathFactory
      );
104
      painter.paint(mockCanvas, size);
105 106
      expect(generatedPaths.length, 1);

107
      verifyInOrder(<void>[
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
        generatedPaths[0].moveTo(0.0, 0.0),
        generatedPaths[0].lineTo(48.0, 0.0),
        generatedPaths[0].lineTo(48.0, 10.0),
        generatedPaths[0].lineTo(0.0, 10.0),
        generatedPaths[0].lineTo(0.0, 0.0),
        generatedPaths[0].close(),
      ]);
    });

    test('progress 1', () {
      final _AnimatedIconPainter painter = new _AnimatedIconPainter(
        paths: movingBar.paths,
        progress: const AlwaysStoppedAnimation<double>(1.0),
        color: const Color(0xFF00FF00),
        scale: 1.0,
        shouldMirror: false,
        uiPathFactory: pathFactory
      );
126
      painter.paint(mockCanvas, size);
127 128
      expect(generatedPaths.length, 1);

129
      verifyInOrder(<void>[
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
        generatedPaths[0].moveTo(0.0, 38.0),
        generatedPaths[0].lineTo(48.0, 38.0),
        generatedPaths[0].lineTo(48.0, 48.0),
        generatedPaths[0].lineTo(0.0, 48.0),
        generatedPaths[0].lineTo(0.0, 38.0),
        generatedPaths[0].close(),
      ]);
    });

    test('clamped progress', () {
      final _AnimatedIconPainter painter = new _AnimatedIconPainter(
        paths: movingBar.paths,
        progress: const AlwaysStoppedAnimation<double>(1.5),
        color: const Color(0xFF00FF00),
        scale: 1.0,
        shouldMirror: false,
        uiPathFactory: pathFactory
      );
148
      painter.paint(mockCanvas, size);
149 150
      expect(generatedPaths.length, 1);

151
      verifyInOrder(<void>[
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
        generatedPaths[0].moveTo(0.0, 38.0),
        generatedPaths[0].lineTo(48.0, 38.0),
        generatedPaths[0].lineTo(48.0, 48.0),
        generatedPaths[0].lineTo(0.0, 48.0),
        generatedPaths[0].lineTo(0.0, 38.0),
        generatedPaths[0].close(),
      ]);
    });

    test('scale', () {
      final _AnimatedIconPainter painter = new _AnimatedIconPainter(
        paths: movingBar.paths,
        progress: const AlwaysStoppedAnimation<double>(0.0),
        color: const Color(0xFF00FF00),
        scale: 0.5,
        shouldMirror: false,
        uiPathFactory: pathFactory
      );
170
      painter.paint(mockCanvas, size);
171 172 173 174 175 176 177 178 179 180 181 182
      verify(mockCanvas.scale(0.5, 0.5));
    });

    test('mirror', () {
      final _AnimatedIconPainter painter = new _AnimatedIconPainter(
        paths: movingBar.paths,
        progress: const AlwaysStoppedAnimation<double>(0.0),
        color: const Color(0xFF00FF00),
        scale: 1.0,
        shouldMirror: true,
        uiPathFactory: pathFactory
      );
183
      painter.paint(mockCanvas, size);
184
      verifyInOrder(<void>[
185 186 187 188 189 190 191 192 193 194 195 196 197 198
        mockCanvas.rotate(math.pi),
        mockCanvas.translate(-48.0, -48.0)
      ]);
    });

    test('interpolated frame', () {
      final _AnimatedIconPainter painter = new _AnimatedIconPainter(
        paths: movingBar.paths,
        progress: const AlwaysStoppedAnimation<double>(0.5),
        color: const Color(0xFF00FF00),
        scale: 1.0,
        shouldMirror: false,
        uiPathFactory: pathFactory
      );
199
      painter.paint(mockCanvas, size);
200 201
      expect(generatedPaths.length, 1);

202
      verifyInOrder(<void>[
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
        generatedPaths[0].moveTo(0.0, 19.0),
        generatedPaths[0].lineTo(48.0, 19.0),
        generatedPaths[0].lineTo(48.0, 29.0),
        generatedPaths[0].lineTo(0.0, 29.0),
        generatedPaths[0].lineTo(0.0, 19.0),
        generatedPaths[0].close(),
      ]);
    });

    test('curved frame', () {
      final _AnimatedIconPainter painter = new _AnimatedIconPainter(
        paths: bow.paths,
        progress: const AlwaysStoppedAnimation<double>(1.0),
        color: const Color(0xFF00FF00),
        scale: 1.0,
        shouldMirror: false,
        uiPathFactory: pathFactory
      );
221
      painter.paint(mockCanvas, size);
222 223
      expect(generatedPaths.length, 1);

224
      verifyInOrder(<void>[
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
        generatedPaths[0].moveTo(0.0, 24.0),
        generatedPaths[0].cubicTo(16.0, 48.0, 32.0, 48.0, 48.0, 24.0),
        generatedPaths[0].lineTo(0.0, 24.0),
        generatedPaths[0].close(),
      ]);
    });

    test('interpolated curved frame', () {
      final _AnimatedIconPainter painter = new _AnimatedIconPainter(
        paths: bow.paths,
        progress: const AlwaysStoppedAnimation<double>(0.25),
        color: const Color(0xFF00FF00),
        scale: 1.0,
        shouldMirror: false,
        uiPathFactory: pathFactory
      );
241
      painter.paint(mockCanvas, size);
242 243
      expect(generatedPaths.length, 1);

244
      verifyInOrder(<void>[
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 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 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
        generatedPaths[0].moveTo(0.0, 24.0),
        generatedPaths[0].cubicTo(16.0, 17.0, 32.0, 17.0, 48.0, 24.0),
        generatedPaths[0].lineTo(0.0, 24.0),
        generatedPaths[0].close(),
      ]);
    });

    test('should not repaint same values', () {
      final _AnimatedIconPainter painter1 = new _AnimatedIconPainter(
        paths: bow.paths,
        progress: const AlwaysStoppedAnimation<double>(0.0),
        color: const Color(0xFF00FF00),
        scale: 1.0,
        shouldMirror: false,
        uiPathFactory: pathFactory
      );

      final _AnimatedIconPainter painter2 = new _AnimatedIconPainter(
        paths: bow.paths,
        progress: const AlwaysStoppedAnimation<double>(0.0),
        color: const Color(0xFF00FF00),
        scale: 1.0,
        shouldMirror: false,
        uiPathFactory: pathFactory
      );

      expect(painter1.shouldRepaint(painter2), false);
    });

    test('should repaint on progress change', () {
      final _AnimatedIconPainter painter1 = new _AnimatedIconPainter(
        paths: bow.paths,
        progress: const AlwaysStoppedAnimation<double>(0.0),
        color: const Color(0xFF00FF00),
        scale: 1.0,
        shouldMirror: false,
        uiPathFactory: pathFactory
      );

      final _AnimatedIconPainter painter2 = new _AnimatedIconPainter(
        paths: bow.paths,
        progress: const AlwaysStoppedAnimation<double>(0.1),
        color: const Color(0xFF00FF00),
        scale: 1.0,
        shouldMirror: false,
        uiPathFactory: pathFactory
      );

      expect(painter1.shouldRepaint(painter2), true);
    });

    test('should repaint on color change', () {
      final _AnimatedIconPainter painter1 = new _AnimatedIconPainter(
        paths: bow.paths,
        progress: const AlwaysStoppedAnimation<double>(0.0),
        color: const Color(0xFF00FF00),
        scale: 1.0,
        shouldMirror: false,
        uiPathFactory: pathFactory
      );

      final _AnimatedIconPainter painter2 = new _AnimatedIconPainter(
        paths: bow.paths,
        progress: const AlwaysStoppedAnimation<double>(0.0),
        color: const Color(0xFFFF0000),
        scale: 1.0,
        shouldMirror: false,
        uiPathFactory: pathFactory
      );

      expect(painter1.shouldRepaint(painter2), true);
    });

    test('should repaint on paths change', () {
      final _AnimatedIconPainter painter1 = new _AnimatedIconPainter(
        paths: bow.paths,
        progress: const AlwaysStoppedAnimation<double>(0.0),
        color: const Color(0xFF0000FF),
        scale: 1.0,
        shouldMirror: false,
        uiPathFactory: pathFactory
      );

      final _AnimatedIconPainter painter2 = new _AnimatedIconPainter(
        paths: const <_PathFrames> [],
        progress: const AlwaysStoppedAnimation<double>(0.0),
        color: const Color(0xFF0000FF),
        scale: 1.0,
        shouldMirror: false,
        uiPathFactory: pathFactory
      );

      expect(painter1.shouldRepaint(painter2), true);
    });

  });
}

const _AnimatedIconData movingBar = const _AnimatedIconData(
  const Size(48.0, 48.0),
  const <_PathFrames> [
    const _PathFrames(
      opacities: const <double> [1.0, 0.2],
      commands: const <_PathCommand> [
        const _PathMoveTo(
          const <Offset> [
            const Offset(0.0, 0.0),
            const Offset(0.0, 38.0),
          ],
        ),
        const _PathLineTo(
          const <Offset> [
            const Offset(48.0, 0.0),
            const Offset(48.0, 38.0),
          ],
        ),
        const _PathLineTo(
          const <Offset> [
            const Offset(48.0, 10.0),
            const Offset(48.0, 48.0),
          ],
        ),
        const _PathLineTo(
          const <Offset> [
            const Offset(0.0, 10.0),
            const Offset(0.0, 48.0),
          ],
        ),
        const _PathLineTo(
          const <Offset> [
            const Offset(0.0, 0.0),
            const Offset(0.0, 38.0),
          ],
        ),
        const _PathClose(),
      ],
    ),
  ],
);

const _AnimatedIconData bow = const _AnimatedIconData(
  const Size(48.0, 48.0),
  const <_PathFrames> [
    const _PathFrames(
      opacities: const <double> [1.0, 1.0],
      commands: const <_PathCommand> [
        const _PathMoveTo(
          const <Offset> [
            const Offset(0.0, 24.0),
            const Offset(0.0, 24.0),
            const Offset(0.0, 24.0),
          ],
        ),
        const _PathCubicTo(
          const <Offset> [
            const Offset(16.0, 24.0),
            const Offset(16.0, 10.0),
            const Offset(16.0, 48.0),
          ],
          const <Offset> [
            const Offset(32.0, 24.0),
            const Offset(32.0, 10.0),
            const Offset(32.0, 48.0),
          ],
          const <Offset> [
            const Offset(48.0, 24.0),
            const Offset(48.0, 24.0),
            const Offset(48.0, 24.0),
          ],
        ),
        const _PathLineTo(
          const <Offset> [
            const Offset(0.0, 24.0),
            const Offset(0.0, 24.0),
            const Offset(0.0, 24.0),
          ],
        ),
        const _PathClose(),
      ],
    ),
  ],
);