custom_paint_test.dart 3.09 KB
Newer Older
1 2 3 4 5
// Copyright 2015 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_test/flutter_test.dart';
6
import 'package:flutter/rendering.dart';
7 8 9 10 11
import 'package:flutter/widgets.dart';

class TestCustomPainter extends CustomPainter {
  TestCustomPainter({ this.log, this.name });

12 13
  final List<String> log;
  final String name;
14

15
  @override
16 17 18 19
  void paint(Canvas canvas, Size size) {
    log.add(name);
  }

20
  @override
21 22 23 24
  bool shouldRepaint(TestCustomPainter oldPainter) => true;
}

void main() {
25
  testWidgets('Control test for custom painting', (WidgetTester tester) async {
26
    final List<String> log = <String>[];
27
    await tester.pumpWidget(new CustomPaint(
28 29 30 31 32 33 34 35 36
      painter: new TestCustomPainter(
        log: log,
        name: 'background'
      ),
      foregroundPainter: new TestCustomPainter(
        log: log,
        name: 'foreground'
      ),
      child: new CustomPaint(
37 38
        painter: new TestCustomPainter(
          log: log,
39
          name: 'child'
40
        )
41 42
      )
    ));
43

44
    expect(log, equals(<String>['background', 'child', 'foreground']));
45
  });
Ian Hickson's avatar
Ian Hickson committed
46 47

  testWidgets('CustomPaint sizing', (WidgetTester tester) async {
48
    final GlobalKey target = new GlobalKey();
Ian Hickson's avatar
Ian Hickson committed
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 79 80

    await tester.pumpWidget(new Center(
      child: new CustomPaint(key: target)
    ));
    expect(target.currentContext.size, Size.zero);

    await tester.pumpWidget(new Center(
      child: new CustomPaint(key: target, child: new Container())
    ));
    expect(target.currentContext.size, const Size(800.0, 600.0));

    await tester.pumpWidget(new Center(
      child: new CustomPaint(key: target, size: const Size(20.0, 20.0))
    ));
    expect(target.currentContext.size, const Size(20.0, 20.0));

    await tester.pumpWidget(new Center(
      child: new CustomPaint(key: target, size: const Size(2000.0, 100.0))
    ));
    expect(target.currentContext.size, const Size(800.0, 100.0));

    await tester.pumpWidget(new Center(
      child: new CustomPaint(key: target, size: Size.zero, child: new Container())
    ));
    expect(target.currentContext.size, const Size(800.0, 600.0));

    await tester.pumpWidget(new Center(
      child: new CustomPaint(key: target, child: new Container(height: 0.0, width: 0.0))
    ));
    expect(target.currentContext.size, Size.zero);

  });
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

  testWidgets('Raster cache hints', (WidgetTester tester) async {
    final GlobalKey target = new GlobalKey();

    final List<String> log = <String>[];
    await tester.pumpWidget(new CustomPaint(
      key: target,
      isComplex: true,
      painter: new TestCustomPainter(log: log),
    ));
    RenderCustomPaint renderCustom = target.currentContext.findRenderObject();
    expect(renderCustom.isComplex, true);
    expect(renderCustom.willChange, false);

    await tester.pumpWidget(new CustomPaint(
      key: target,
      willChange: true,
      foregroundPainter: new TestCustomPainter(log: log),
    ));
    renderCustom = target.currentContext.findRenderObject();
    expect(renderCustom.isComplex, false);
    expect(renderCustom.willChange, true);
  });
104
}