material_test.dart 14.4 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/material.dart';
6
import 'package:flutter/painting.dart';
7
import 'package:flutter/rendering.dart';
8 9
import 'package:flutter_test/flutter_test.dart';

10 11
import '../rendering/mock_canvas.dart';

12 13 14 15 16 17 18 19
class NotifyMaterial extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    new LayoutChangedNotification().dispatch(context);
    return new Container();
  }
}

20
Widget buildMaterial(
21
    {double elevation = 0.0, Color shadowColor = const Color(0xFF00FF00)}) {
22 23 24 25 26
  return new Center(
    child: new SizedBox(
      height: 100.0,
      width: 100.0,
      child: new Material(
27
        shadowColor: shadowColor,
28
        elevation: elevation,
29
        shape: const CircleBorder(),
30 31 32 33 34
      ),
    ),
  );
}

35 36
RenderPhysicalShape getShadow(WidgetTester tester) {
  return tester.renderObject(find.byType(PhysicalShape));
37 38
}

39 40 41
class PaintRecorder extends CustomPainter {
  PaintRecorder(this.log);

42
  final List<Size> log;
43 44 45 46 47

  @override
  void paint(Canvas canvas, Size size) {
    log.add(size);
    final Paint paint = new Paint()..color = const Color(0xFF0000FF);
48
    canvas.drawRect(Offset.zero & size, paint);
49 50 51 52 53 54
  }

  @override
  bool shouldRepaint(PaintRecorder oldDelegate) => false;
}

55
void main() {
56
  testWidgets('LayoutChangedNotification test', (WidgetTester tester) async {
57 58
    await tester.pumpWidget(
      new Material(
59 60
        child: new NotifyMaterial(),
      ),
61 62
    );
  });
63 64

  testWidgets('ListView scroll does not repaint', (WidgetTester tester) async {
65
    final List<Size> log = <Size>[];
66 67

    await tester.pumpWidget(
68 69 70 71 72 73 74 75 76 77
      new Directionality(
        textDirection: TextDirection.ltr,
        child: new Column(
          children: <Widget>[
            new SizedBox(
              width: 150.0,
              height: 150.0,
              child: new CustomPaint(
                painter: new PaintRecorder(log),
              ),
78
            ),
79 80 81 82 83 84 85 86 87 88 89 90 91
            new Expanded(
              child: new Material(
                child: new Column(
                  children: <Widget>[
                    new Expanded(
                      child: new ListView(
                        children: <Widget>[
                          new Container(
                            height: 2000.0,
                            color: const Color(0xFF00FF00),
                          ),
                        ],
                      ),
92
                    ),
93 94 95 96 97 98
                    new SizedBox(
                      width: 100.0,
                      height: 100.0,
                      child: new CustomPaint(
                        painter: new PaintRecorder(log),
                      ),
99
                    ),
100 101
                  ],
                ),
102 103
              ),
            ),
104 105
          ],
        ),
106 107 108 109 110 111 112 113 114 115 116
      ),
    );

    // We paint twice because we have two CustomPaint widgets in the tree above
    // to test repainting both inside and outside the Material widget.
    expect(log, equals(<Size>[
      const Size(150.0, 150.0),
      const Size(100.0, 100.0),
    ]));
    log.clear();

117
    await tester.drag(find.byType(ListView), const Offset(0.0, -300.0));
118 119 120 121
    await tester.pump();

    expect(log, isEmpty);
  });
122 123

  testWidgets('Shadows animate smoothly', (WidgetTester tester) async {
124 125
    // This code verifies that the PhysicalModel's elevation animates over
    // a kThemeChangeDuration time interval.
126

127
    await tester.pumpWidget(buildMaterial(elevation: 0.0));
128
    final RenderPhysicalShape modelA = getShadow(tester);
129 130
    expect(modelA.elevation, equals(0.0));

131
    await tester.pumpWidget(buildMaterial(elevation: 9.0));
132
    final RenderPhysicalShape modelB = getShadow(tester);
133
    expect(modelB.elevation, equals(0.0));
134 135

    await tester.pump(const Duration(milliseconds: 1));
136
    final RenderPhysicalShape modelC = getShadow(tester);
137
    expect(modelC.elevation, closeTo(0.0, 0.001));
138 139

    await tester.pump(kThemeChangeDuration ~/ 2);
140
    final RenderPhysicalShape modelD = getShadow(tester);
141
    expect(modelD.elevation, isNot(closeTo(0.0, 0.001)));
142 143

    await tester.pump(kThemeChangeDuration);
144
    final RenderPhysicalShape modelE = getShadow(tester);
145
    expect(modelE.elevation, equals(9.0));
146
  });
147 148

  testWidgets('Shadow colors animate smoothly', (WidgetTester tester) async {
149
    // This code verifies that the PhysicalModel's shadowColor animates over
150 151 152
    // a kThemeChangeDuration time interval.

    await tester.pumpWidget(buildMaterial(shadowColor: const Color(0xFF00FF00)));
153
    final RenderPhysicalShape modelA = getShadow(tester);
154 155 156
    expect(modelA.shadowColor, equals(const Color(0xFF00FF00)));

    await tester.pumpWidget(buildMaterial(shadowColor: const Color(0xFFFF0000)));
157
    final RenderPhysicalShape modelB = getShadow(tester);
158 159 160
    expect(modelB.shadowColor, equals(const Color(0xFF00FF00)));

    await tester.pump(const Duration(milliseconds: 1));
161
    final RenderPhysicalShape modelC = getShadow(tester);
162
    expect(modelC.shadowColor, within<Color>(distance: 1, from: const Color(0xFF00FF00)));
163 164

    await tester.pump(kThemeChangeDuration ~/ 2);
165
    final RenderPhysicalShape modelD = getShadow(tester);
166
    expect(modelD.shadowColor, isNot(within<Color>(distance: 1, from: const Color(0xFF00FF00))));
167 168

    await tester.pump(kThemeChangeDuration);
169
    final RenderPhysicalShape modelE = getShadow(tester);
170 171
    expect(modelE.shadowColor, equals(const Color(0xFFFF0000)));
  });
172 173 174 175 176 177 178 179 180 181 182 183

  group('Transparency clipping', () {
    testWidgets('clips to bounding rect by default', (WidgetTester tester) async {
      final GlobalKey materialKey = new GlobalKey();
      await tester.pumpWidget(
        new Material(
          key: materialKey,
          type: MaterialType.transparency,
          child: const SizedBox(width: 100.0, height: 100.0)
        )
      );

184
      expect(find.byKey(materialKey), clipsWithBoundingRect);
185 186 187 188 189 190 191 192
    });

    testWidgets('clips to rounded rect when borderRadius provided', (WidgetTester tester) async {
      final GlobalKey materialKey = new GlobalKey();
      await tester.pumpWidget(
        new Material(
          key: materialKey,
          type: MaterialType.transparency,
193
          borderRadius: const BorderRadius.all(Radius.circular(10.0)),
194 195 196 197 198 199 200
          child: const SizedBox(width: 100.0, height: 100.0)
        )
      );

      expect(
        find.byKey(materialKey),
        clipsWithBoundingRRect(
201
          borderRadius: const BorderRadius.all(Radius.circular(10.0))
202 203 204
        ),
      );
    });
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223

    testWidgets('clips to shape when provided', (WidgetTester tester) async {
      final GlobalKey materialKey = new GlobalKey();
      await tester.pumpWidget(
        new Material(
          key: materialKey,
          type: MaterialType.transparency,
          shape: const StadiumBorder(),
          child: const SizedBox(width: 100.0, height: 100.0)
        )
      );

      expect(
        find.byKey(materialKey),
        clipsWithShapeBorder(
          shape: const StadiumBorder(),
        ),
      );
    });
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
  });

  group('PhysicalModels', () {
    testWidgets('canvas', (WidgetTester tester) async {
      final GlobalKey materialKey = new GlobalKey();
      await tester.pumpWidget(
        new Material(
          key: materialKey,
          type: MaterialType.canvas,
          child: const SizedBox(width: 100.0, height: 100.0)
        )
      );

      expect(find.byKey(materialKey), rendersOnPhysicalModel(
          shape: BoxShape.rectangle,
          borderRadius: BorderRadius.zero,
          elevation: 0.0,
      ));
    });

    testWidgets('canvas with borderRadius and elevation', (WidgetTester tester) async {
      final GlobalKey materialKey = new GlobalKey();
      await tester.pumpWidget(
        new Material(
          key: materialKey,
          type: MaterialType.canvas,
250
          borderRadius: const BorderRadius.all(Radius.circular(5.0)),
251 252 253 254 255 256 257
          child: const SizedBox(width: 100.0, height: 100.0),
          elevation: 1.0,
        )
      );

      expect(find.byKey(materialKey), rendersOnPhysicalModel(
          shape: BoxShape.rectangle,
258
          borderRadius: const BorderRadius.all(Radius.circular(5.0)),
259 260 261 262
          elevation: 1.0,
      ));
    });

263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
    testWidgets('canvas with shape and elevation', (WidgetTester tester) async {
      final GlobalKey materialKey = new GlobalKey();
      await tester.pumpWidget(
        new Material(
          key: materialKey,
          type: MaterialType.canvas,
          shape: const StadiumBorder(),
          child: const SizedBox(width: 100.0, height: 100.0),
          elevation: 1.0,
        )
      );

      expect(find.byKey(materialKey), rendersOnPhysicalShape(
          shape: const StadiumBorder(),
          elevation: 1.0,
      ));
    });

281 282 283 284 285 286 287 288 289 290 291 292
    testWidgets('card', (WidgetTester tester) async {
      final GlobalKey materialKey = new GlobalKey();
      await tester.pumpWidget(
        new Material(
          key: materialKey,
          type: MaterialType.card,
          child: const SizedBox(width: 100.0, height: 100.0),
        )
      );

      expect(find.byKey(materialKey), rendersOnPhysicalModel(
          shape: BoxShape.rectangle,
293
          borderRadius: const BorderRadius.all(Radius.circular(2.0)),
294 295 296 297 298 299 300 301 302 303
          elevation: 0.0,
      ));
    });

    testWidgets('card with borderRadius and elevation', (WidgetTester tester) async {
      final GlobalKey materialKey = new GlobalKey();
      await tester.pumpWidget(
        new Material(
          key: materialKey,
          type: MaterialType.card,
304
          borderRadius: const BorderRadius.all(Radius.circular(5.0)),
305 306 307 308 309 310 311
          elevation: 5.0,
          child: const SizedBox(width: 100.0, height: 100.0),
        )
      );

      expect(find.byKey(materialKey), rendersOnPhysicalModel(
          shape: BoxShape.rectangle,
312
          borderRadius: const BorderRadius.all(Radius.circular(5.0)),
313 314 315 316
          elevation: 5.0,
      ));
    });

317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
    testWidgets('card with shape and elevation', (WidgetTester tester) async {
      final GlobalKey materialKey = new GlobalKey();
      await tester.pumpWidget(
        new Material(
          key: materialKey,
          type: MaterialType.card,
          shape: const StadiumBorder(),
          elevation: 5.0,
          child: const SizedBox(width: 100.0, height: 100.0),
        )
      );

      expect(find.byKey(materialKey), rendersOnPhysicalShape(
          shape: const StadiumBorder(),
          elevation: 5.0,
      ));
    });

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
    testWidgets('circle', (WidgetTester tester) async {
      final GlobalKey materialKey = new GlobalKey();
      await tester.pumpWidget(
        new Material(
          key: materialKey,
          type: MaterialType.circle,
          child: const SizedBox(width: 100.0, height: 100.0),
          color: const Color(0xFF0000FF),
        )
      );

      expect(find.byKey(materialKey), rendersOnPhysicalModel(
          shape: BoxShape.circle,
          elevation: 0.0,
      ));
    });

    testWidgets('button', (WidgetTester tester) async {
      final GlobalKey materialKey = new GlobalKey();
      await tester.pumpWidget(
        new Material(
          key: materialKey,
          type: MaterialType.button,
          child: const SizedBox(width: 100.0, height: 100.0),
          color: const Color(0xFF0000FF),
        )
      );

      expect(find.byKey(materialKey), rendersOnPhysicalModel(
          shape: BoxShape.rectangle,
365
          borderRadius: const BorderRadius.all(Radius.circular(2.0)),
366 367 368 369 370 371 372 373 374 375 376 377
          elevation: 0.0,
      ));
    });

    testWidgets('button with elevation and borderRadius', (WidgetTester tester) async {
      final GlobalKey materialKey = new GlobalKey();
      await tester.pumpWidget(
        new Material(
          key: materialKey,
          type: MaterialType.button,
          child: const SizedBox(width: 100.0, height: 100.0),
          color: const Color(0xFF0000FF),
378
          borderRadius: const BorderRadius.all(Radius.circular(6.0)),
379 380 381 382 383 384
          elevation: 4.0,
        )
      );

      expect(find.byKey(materialKey), rendersOnPhysicalModel(
          shape: BoxShape.rectangle,
385
          borderRadius: const BorderRadius.all(Radius.circular(6.0)),
386 387 388
          elevation: 4.0,
      ));
    });
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407

    testWidgets('button with elevation and shape', (WidgetTester tester) async {
      final GlobalKey materialKey = new GlobalKey();
      await tester.pumpWidget(
        new Material(
          key: materialKey,
          type: MaterialType.button,
          child: const SizedBox(width: 100.0, height: 100.0),
          color: const Color(0xFF0000FF),
          shape: const StadiumBorder(),
          elevation: 4.0,
        )
      );

      expect(find.byKey(materialKey), rendersOnPhysicalShape(
          shape: const StadiumBorder(),
          elevation: 4.0,
      ));
    });
408
  });
409 410 411 412 413 414 415 416 417 418 419

  group('Border painting', () {
    testWidgets('border is painted on physical layers', (WidgetTester tester) async {
      final GlobalKey materialKey = new GlobalKey();
      await tester.pumpWidget(
        new Material(
          key: materialKey,
          type: MaterialType.button,
          child: const SizedBox(width: 100.0, height: 100.0),
          color: const Color(0xFF0000FF),
          shape: const CircleBorder(
420
            side: BorderSide(
421
              width: 2.0,
422
              color: Color(0xFF0000FF),
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
            )
          ),
        )
      );

      final RenderBox box = tester.renderObject(find.byKey(materialKey));
      expect(box, paints..circle());
    });

    testWidgets('border is painted for transparent material', (WidgetTester tester) async {
      final GlobalKey materialKey = new GlobalKey();
      await tester.pumpWidget(
        new Material(
          key: materialKey,
          type: MaterialType.transparency,
          child: const SizedBox(width: 100.0, height: 100.0),
          shape: const CircleBorder(
440
            side: BorderSide(
441
              width: 2.0,
442
              color: Color(0xFF0000FF),
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
            )
          ),
        )
      );

      final RenderBox box = tester.renderObject(find.byKey(materialKey));
      expect(box, paints..circle());
    });

    testWidgets('border is not painted for when border side is none', (WidgetTester tester) async {
      final GlobalKey materialKey = new GlobalKey();
      await tester.pumpWidget(
        new Material(
          key: materialKey,
          type: MaterialType.transparency,
          child: const SizedBox(width: 100.0, height: 100.0),
          shape: const CircleBorder(),
        )
      );

      final RenderBox box = tester.renderObject(find.byKey(materialKey));
      expect(box, isNot(paints..circle()));
    });
  });
467
}