animated_icons_test.dart 10.1 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
@Tags(<String>['reduced-test-set'])

7 8 9 10 11 12 13
import 'dart:math' as math show pi;

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

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

14
class MockCanvas extends Fake implements Canvas {
15 16
  late Path capturedPath;
  late Paint capturedPaint;
17 18 19 20 21 22 23

  @override
  void drawPath(Path path, Paint paint) {
    capturedPath = path;
    capturedPaint = paint;
  }

24 25
  late double capturedSx;
  late double capturedSy;
26 27

  @override
28
  void scale(double sx, [double? sy]) {
29
    capturedSx = sx;
30
    capturedSy = sy!;
31
    invocations.add(RecordedScale(sx, sy));
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  }

  final List<RecordedCanvasCall> invocations = <RecordedCanvasCall>[];

  @override
  void rotate(double radians) {
    invocations.add(RecordedRotate(radians));
  }

  @override
  void translate(double dx, double dy) {
    invocations.add(RecordedTranslate(dx, dy));
  }
}

@immutable
abstract class RecordedCanvasCall {
  const RecordedCanvasCall();
}

class RecordedRotate extends RecordedCanvasCall {
  const RecordedRotate(this.radians);

  final double radians;

  @override
  bool operator ==(Object other) {
    return other is RecordedRotate && other.radians == radians;
  }

  @override
  int get hashCode => radians.hashCode;
}

class RecordedTranslate extends RecordedCanvasCall {
  const RecordedTranslate(this.dx, this.dy);

  final double dx;
  final double dy;

  @override
  bool operator ==(Object other) {
    return other is RecordedTranslate && other.dx == dx && other.dy == dy;
  }

  @override
78
  int get hashCode => Object.hash(dx, dy);
79
}
80

81 82 83 84 85 86 87 88 89 90 91 92
class RecordedScale extends RecordedCanvasCall {
  const RecordedScale(this.sx, this.sy);

  final double sx;
  final double sy;

  @override
  bool operator ==(Object other) {
    return other is RecordedScale && other.sx == sx && other.sy == sy;
  }

  @override
93
  int get hashCode => Object.hash(sx, sy);
94 95
}

96 97 98 99 100
void main() {
  testWidgets('IconTheme color', (WidgetTester tester) async {
    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
101 102 103
        child: IconTheme(
          data: IconThemeData(
            color: Color(0xFF666666),
104
          ),
105 106
          child: AnimatedIcon(
            progress: AlwaysStoppedAnimation<double>(0.0),
107
            icon: AnimatedIcons.arrow_menu,
108
          ),
109 110 111 112
        ),
      ),
    );
    final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
113
    final MockCanvas canvas = MockCanvas();
114
    customPaint.painter!.paint(canvas, const Size(48.0, 48.0));
115
    expect(canvas.capturedPaint, hasColor(0xFF666666));
116 117 118 119 120 121
  });

  testWidgets('IconTheme opacity', (WidgetTester tester) async {
    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
122 123 124
        child: IconTheme(
          data: IconThemeData(
            color: Color(0xFF666666),
125
            opacity: 0.5,
126
          ),
127 128
          child: AnimatedIcon(
            progress: AlwaysStoppedAnimation<double>(0.0),
129
            icon: AnimatedIcons.arrow_menu,
130
          ),
131 132 133 134
        ),
      ),
    );
    final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
135
    final MockCanvas canvas = MockCanvas();
136
    customPaint.painter!.paint(canvas, const Size(48.0, 48.0));
137
    expect(canvas.capturedPaint, hasColor(0x80666666));
138 139 140 141 142 143
  });

  testWidgets('color overrides IconTheme color', (WidgetTester tester) async {
    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
144 145 146
        child: IconTheme(
          data: IconThemeData(
            color: Color(0xFF666666),
147
          ),
148 149
          child: AnimatedIcon(
            progress: AlwaysStoppedAnimation<double>(0.0),
150
            icon: AnimatedIcons.arrow_menu,
151
            color: Color(0xFF0000FF),
152
          ),
153 154 155 156
        ),
      ),
    );
    final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
157
    final MockCanvas canvas = MockCanvas();
158
    customPaint.painter!.paint(canvas, const Size(48.0, 48.0));
159
    expect(canvas.capturedPaint, hasColor(0xFF0000FF));
160 161 162 163 164 165
  });

  testWidgets('IconTheme size', (WidgetTester tester) async {
    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
166 167 168
        child: IconTheme(
          data: IconThemeData(
            color: Color(0xFF666666),
169 170
            size: 12.0,
          ),
171 172
          child: AnimatedIcon(
            progress: AlwaysStoppedAnimation<double>(0.0),
173
            icon: AnimatedIcons.arrow_menu,
174
          ),
175 176 177 178
        ),
      ),
    );
    final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
179
    final MockCanvas canvas = MockCanvas();
180
    customPaint.painter!.paint(canvas, const Size(12.0, 12.0));
181
    // arrow_menu default size is 48x48 so we expect it to be scaled by 0.25.
182 183
    expect(canvas.capturedSx, 0.25);
    expect(canvas.capturedSy, 0.25);
184 185 186 187 188 189
  });

  testWidgets('size overridesIconTheme size', (WidgetTester tester) async {
    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
190 191 192
        child: IconTheme(
          data: IconThemeData(
            color: Color(0xFF666666),
193 194
            size: 12.0,
          ),
195 196
          child: AnimatedIcon(
            progress: AlwaysStoppedAnimation<double>(0.0),
197 198
            icon: AnimatedIcons.arrow_menu,
            size: 96.0,
199
          ),
200 201 202 203
        ),
      ),
    );
    final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
204
    final MockCanvas canvas = MockCanvas();
205
    customPaint.painter!.paint(canvas, const Size(12.0, 12.0));
206
    // arrow_menu default size is 48x48 so we expect it to be scaled by 2.
207 208
    expect(canvas.capturedSx, 2);
    expect(canvas.capturedSy, 2);
209 210 211
  });

  testWidgets('Semantic label', (WidgetTester tester) async {
212
    final SemanticsTester semantics = SemanticsTester(tester);
213 214 215 216

    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
217 218
        child: AnimatedIcon(
          progress: AlwaysStoppedAnimation<double>(0.0),
219 220 221 222 223 224 225 226
          icon: AnimatedIcons.arrow_menu,
          size: 96.0,
          semanticLabel: 'a label',
        ),
      ),
    );

    expect(semantics, includesNodeWith(label: 'a label'));
227 228

    semantics.dispose();
229 230 231 232 233 234
  });

  testWidgets('Inherited text direction rtl', (WidgetTester tester) async {
    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.rtl,
235 236 237
        child: IconTheme(
          data: IconThemeData(
            color: Color(0xFF666666),
238
          ),
239 240 241 242 243
          child: RepaintBoundary(
            child: AnimatedIcon(
              progress: AlwaysStoppedAnimation<double>(0.0),
              icon: AnimatedIcons.arrow_menu,
            ),
244
          ),
245 246 247 248
        ),
      ),
    );
    final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
249
    final MockCanvas canvas = MockCanvas();
250
    customPaint.painter!.paint(canvas, const Size(48.0, 48.0));
251 252 253
    expect(canvas.invocations, const <RecordedCanvasCall>[
      RecordedRotate(math.pi),
      RecordedTranslate(-48, -48),
254
      RecordedScale(0.5, 0.5),
255
    ]);
256 257
    await expectLater(find.byType(AnimatedIcon),
        matchesGoldenFile('animated_icons_test.icon.rtl.png'));
258 259 260 261 262 263
  });

  testWidgets('Inherited text direction ltr', (WidgetTester tester) async {
    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
264 265 266
        child: IconTheme(
          data: IconThemeData(
            color: Color(0xFF666666),
267
          ),
268 269 270 271 272
          child: RepaintBoundary(
            child: AnimatedIcon(
              progress: AlwaysStoppedAnimation<double>(0.0),
              icon: AnimatedIcons.arrow_menu,
            ),
273
          ),
274 275 276 277
        ),
      ),
    );
    final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
278
    final MockCanvas canvas = MockCanvas();
279
    customPaint.painter!.paint(canvas, const Size(48.0, 48.0));
280 281 282 283 284
    expect(canvas.invocations, const <RecordedCanvasCall>[
      RecordedScale(0.5, 0.5),
    ]);
    await expectLater(find.byType(AnimatedIcon),
        matchesGoldenFile('animated_icons_test.icon.ltr.png'));
285 286 287 288 289 290
  });

  testWidgets('Inherited text direction overridden', (WidgetTester tester) async {
    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
291 292 293
        child: IconTheme(
          data: IconThemeData(
            color: Color(0xFF666666),
294
          ),
295 296
          child: AnimatedIcon(
            progress: AlwaysStoppedAnimation<double>(0.0),
297 298
            icon: AnimatedIcons.arrow_menu,
            textDirection: TextDirection.rtl,
299
          ),
300 301 302 303
        ),
      ),
    );
    final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
304
    final MockCanvas canvas = MockCanvas();
305
    customPaint.painter!.paint(canvas, const Size(48.0, 48.0));
306 307 308
    expect(canvas.invocations, const <RecordedCanvasCall>[
      RecordedRotate(math.pi),
      RecordedTranslate(-48, -48),
309
      RecordedScale(0.5, 0.5),
310 311
    ]);
  });
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327

  testWidgets('Direction has no effect on position of widget', (WidgetTester tester) async {
    const AnimatedIcon icon = AnimatedIcon(
      progress: AlwaysStoppedAnimation<double>(0.0),
      icon: AnimatedIcons.arrow_menu,
    );
    await tester.pumpWidget(
      const Directionality(textDirection: TextDirection.rtl, child: icon),
    );
    final Rect rtlRect = tester.getRect(find.byType(AnimatedIcon));
    await tester.pumpWidget(
      const Directionality(textDirection: TextDirection.ltr, child: icon),
    );
    final Rect ltrRect = tester.getRect(find.byType(AnimatedIcon));
    expect(rtlRect, ltrRect);
  });
328 329
}

330
PaintColorMatcher hasColor(int color) {
331
  return PaintColorMatcher(color);
332 333 334 335 336 337 338 339 340 341 342 343 344
}

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) {
345
    final Paint actualPaint = item as Paint;
346
    return actualPaint.color == Color(expectedColor);
347 348
  }
}