custom_paint_test.dart 2.27 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
// 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';
import 'package:flutter/widgets.dart';

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

  List<String> log;
  String name;

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

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

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

43
    expect(log, equals(<String>['background', 'child', 'foreground']));
44
  });
Ian Hickson's avatar
Ian Hickson committed
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 79

  testWidgets('CustomPaint sizing', (WidgetTester tester) async {
    GlobalKey target = new GlobalKey();

    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);

  });
80
}