animated_icons_test.dart 7.68 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 24 25 26 27 28 29 30 31 32 33
// 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.

import 'dart:math' as math show pi;

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';

import '../widgets/semantics_tester.dart';

class MockCanvas extends Mock implements Canvas {}

void main() {
  testWidgets('IconTheme color', (WidgetTester tester) async {
    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
        child: const IconTheme(
          data: const IconThemeData(
            color: const Color(0xFF666666),
          ),
          child: const AnimatedIcon(
            progress: const AlwaysStoppedAnimation<double>(0.0),
            icon: AnimatedIcons.arrow_menu,
          )
        ),
      ),
    );
    final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
    final MockCanvas canvas = new MockCanvas();
    customPaint.painter.paint(canvas, const Size(48.0, 48.0));
34
    verify(canvas.drawPath(typed(any), typed(argThat(hasColor(0xFF666666)))));
35 36 37 38 39 40 41 42 43
  });

  testWidgets('IconTheme opacity', (WidgetTester tester) async {
    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
        child: const IconTheme(
          data: const IconThemeData(
            color: const Color(0xFF666666),
44
            opacity: 0.5,
45 46 47 48 49 50 51 52 53 54 55
          ),
          child: const AnimatedIcon(
            progress: const AlwaysStoppedAnimation<double>(0.0),
            icon: AnimatedIcons.arrow_menu,
          )
        ),
      ),
    );
    final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
    final MockCanvas canvas = new MockCanvas();
    customPaint.painter.paint(canvas, const Size(48.0, 48.0));
56
    verify(canvas.drawPath(typed(any), typed(argThat(hasColor(0x80666666)))));
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  });

  testWidgets('color overrides IconTheme color', (WidgetTester tester) async {
    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
        child: const IconTheme(
          data: const IconThemeData(
            color: const Color(0xFF666666),
          ),
          child: const AnimatedIcon(
            progress: const AlwaysStoppedAnimation<double>(0.0),
            icon: AnimatedIcons.arrow_menu,
            color: const Color(0xFF0000FF),
          )
        ),
      ),
    );
    final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
    final MockCanvas canvas = new MockCanvas();
    customPaint.painter.paint(canvas, const Size(48.0, 48.0));
78
    verify(canvas.drawPath(typed(any), typed(argThat(hasColor(0xFF0000FF)))));
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
  });

  testWidgets('IconTheme size', (WidgetTester tester) async {
    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
        child: const IconTheme(
          data: const IconThemeData(
            color: const Color(0xFF666666),
            size: 12.0,
          ),
          child: const AnimatedIcon(
            progress: const AlwaysStoppedAnimation<double>(0.0),
            icon: AnimatedIcons.arrow_menu,
          )
        ),
      ),
    );
    final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
    final MockCanvas canvas = new MockCanvas();
    customPaint.painter.paint(canvas, const Size(12.0, 12.0));
    // arrow_menu default size is 48x48 so we expect it to be scaled by 0.25.
    verify(canvas.scale(0.25, 0.25));
  });

  testWidgets('size overridesIconTheme size', (WidgetTester tester) async {
    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
        child: const IconTheme(
          data: const IconThemeData(
            color: const Color(0xFF666666),
            size: 12.0,
          ),
          child: const AnimatedIcon(
            progress: const AlwaysStoppedAnimation<double>(0.0),
            icon: AnimatedIcons.arrow_menu,
            size: 96.0,
          )
        ),
      ),
    );
    final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
    final MockCanvas canvas = new MockCanvas();
    customPaint.painter.paint(canvas, const Size(12.0, 12.0));
    // arrow_menu default size is 48x48 so we expect it to be scaled by 2.
    verify(canvas.scale(2.0, 2.0));
  });

  testWidgets('Semantic label', (WidgetTester tester) async {
    final SemanticsTester semantics = new SemanticsTester(tester);

    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
        child: const AnimatedIcon(
          progress: const AlwaysStoppedAnimation<double>(0.0),
          icon: AnimatedIcons.arrow_menu,
          size: 96.0,
          semanticLabel: 'a label',
        ),
      ),
    );

    expect(semantics, includesNodeWith(label: 'a label'));
144 145

    semantics.dispose();
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
  });

  testWidgets('Inherited text direction rtl', (WidgetTester tester) async {
    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.rtl,
        child: const IconTheme(
          data: const IconThemeData(
            color: const Color(0xFF666666),
          ),
          child: const AnimatedIcon(
            progress: const AlwaysStoppedAnimation<double>(0.0),
            icon: AnimatedIcons.arrow_menu,
          )
        ),
      ),
    );
    final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
    final MockCanvas canvas = new MockCanvas();
    customPaint.painter.paint(canvas, const Size(48.0, 48.0));
166
    verifyInOrder(<void>[
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
      canvas.rotate(math.pi),
      canvas.translate(-48.0, -48.0)
    ]);
  });

  testWidgets('Inherited text direction ltr', (WidgetTester tester) async {
    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
        child: const IconTheme(
          data: const IconThemeData(
            color: const Color(0xFF666666),
          ),
          child: const AnimatedIcon(
            progress: const AlwaysStoppedAnimation<double>(0.0),
            icon: AnimatedIcons.arrow_menu,
          )
        ),
      ),
    );
    final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
    final MockCanvas canvas = new MockCanvas();
    customPaint.painter.paint(canvas, const Size(48.0, 48.0));
190 191
    verifyNever(canvas.rotate(typed(any)));
    verifyNever(canvas.translate(typed(any), typed(any)));
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
  });

  testWidgets('Inherited text direction overridden', (WidgetTester tester) async {
    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
        child: const IconTheme(
          data: const IconThemeData(
            color: const Color(0xFF666666),
          ),
          child: const AnimatedIcon(
            progress: const AlwaysStoppedAnimation<double>(0.0),
            icon: AnimatedIcons.arrow_menu,
            textDirection: TextDirection.rtl,
          )
        ),
      ),
    );
    final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
    final MockCanvas canvas = new MockCanvas();
    customPaint.painter.paint(canvas, const Size(48.0, 48.0));
213
    verifyInOrder(<void>[
214 215 216 217 218 219
      canvas.rotate(math.pi),
      canvas.translate(-48.0, -48.0)
    ]);
  });
}

220
PaintColorMatcher hasColor(int color) {
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
  return new PaintColorMatcher(color);
}

class PaintColorMatcher extends Matcher {
  const PaintColorMatcher(this.expectedColor);

  final int expectedColor;

  @override
  Description describe(Description description) =>
    description.add('color was not $expectedColor');

  @override
  bool matches(dynamic item, Map<dynamic, dynamic> matchState) {
    final Paint actualPaint = item;
    return actualPaint.color == new Color(expectedColor);
  }
}