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

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

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
12
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
13 14
import '../widgets/semantics_tester.dart';

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

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

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

  @override
29
  void scale(double sx, [double? sy]) {
30
    capturedSx = sx;
31
    capturedSy = sy!;
32
    invocations.add(RecordedScale(sx, sy));
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 78
  }

  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
79
  int get hashCode => Object.hash(dx, dy);
80
}
81

82 83 84 85 86 87 88 89 90 91 92 93
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
94
  int get hashCode => Object.hash(sx, sy);
95 96
}

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

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

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

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

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

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

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

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

    semantics.dispose();
230 231
  });

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

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

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

314
  testWidgetsWithLeakTracking('Direction has no effect on position of widget', (WidgetTester tester) async {
315 316 317 318 319 320 321 322 323 324 325 326 327 328
    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);
  });
329 330
}

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

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