flow_test.dart 3.31 KB
Newer Older
Adam Barth's avatar
Adam Barth committed
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/widgets.dart';
7
import 'package:flutter/rendering.dart';
Adam Barth's avatar
Adam Barth committed
8 9

class TestFlowDelegate extends FlowDelegate {
10
  TestFlowDelegate({this.startOffset}) : super(repaint: startOffset);
Adam Barth's avatar
Adam Barth committed
11 12 13 14 15 16 17 18 19 20 21 22

  final Animation<double> startOffset;

  @override
  BoxConstraints getConstraintsForChild(int i, BoxConstraints constraints) {
    return constraints.loosen();
  }

  @override
  void paintChildren(FlowPaintingContext context) {
    double dy = startOffset.value;
    for (int i = 0; i < context.childCount; ++i) {
23
      context.paintChild(i, transform: Matrix4.translationValues(0.0, dy, 0.0));
Adam Barth's avatar
Adam Barth committed
24 25 26 27 28 29 30 31
      dy += 0.75 * context.getChildSize(i).height;
    }
  }

  @override
  bool shouldRepaint(TestFlowDelegate oldDelegate) => startOffset == oldDelegate.startOffset;
}

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
class OpacityFlowDelegate extends FlowDelegate {
  OpacityFlowDelegate(this.opacity);

  double opacity;

  @override
  void paintChildren(FlowPaintingContext context) {
    for (int i = 0; i < context.childCount; ++i) {
      context.paintChild(i, opacity: opacity);
    }
  }

  @override
  bool shouldRepaint(OpacityFlowDelegate oldDelegate) => opacity != oldDelegate.opacity;
}

Adam Barth's avatar
Adam Barth committed
48
void main() {
49
  testWidgets('Flow control test', (WidgetTester tester) async {
50
    final AnimationController startOffset = AnimationController.unbounded(
51 52
      vsync: tester,
    );
53
    final List<int> log = <int>[];
Adam Barth's avatar
Adam Barth committed
54

55
    Widget buildBox(int i) {
56
      return GestureDetector(
57 58 59
        onTap: () {
          log.add(i);
        },
60
        child: Container(
61 62
          width: 100.0,
          height: 100.0,
63
          color: const Color(0xFF0000FF),
64
          child: Text('$i', textDirection: TextDirection.ltr)
Adam Barth's avatar
Adam Barth committed
65 66
        )
      );
67 68
    }

69
    await tester.pumpWidget(
70 71
      Flow(
        delegate: TestFlowDelegate(startOffset: startOffset),
72 73 74 75 76 77 78 79 80 81 82
        children: <Widget>[
          buildBox(0),
          buildBox(1),
          buildBox(2),
          buildBox(3),
          buildBox(4),
          buildBox(5),
          buildBox(6),
        ]
      )
    );
Adam Barth's avatar
Adam Barth committed
83

84
    await tester.tap(find.text('0'));
85
    expect(log, equals(<int>[0]));
86
    await tester.tap(find.text('1'));
87
    expect(log, equals(<int>[0, 1]));
88
    await tester.tap(find.text('2'));
89
    expect(log, equals(<int>[0, 1, 2]));
Adam Barth's avatar
Adam Barth committed
90

91
    log.clear();
92
    await tester.tapAt(const Offset(20.0, 90.0));
93
    expect(log, equals(<int>[1]));
Adam Barth's avatar
Adam Barth committed
94

95
    startOffset.value = 50.0;
96
    await tester.pump();
Adam Barth's avatar
Adam Barth committed
97

98
    log.clear();
99
    await tester.tapAt(const Offset(20.0, 90.0));
100
    expect(log, equals(<int>[0]));
Adam Barth's avatar
Adam Barth committed
101
  });
102 103 104 105

  testWidgets('Flow opacity layer', (WidgetTester tester) async {
    const double opacity = 0.2;
    await tester.pumpWidget(
106 107
      Flow(
        delegate: OpacityFlowDelegate(opacity),
108
        children: <Widget>[
109
          Container(width: 100.0, height: 100.0),
110 111 112 113 114 115
        ]
      )
    );
    ContainerLayer layer = RendererBinding.instance.renderView.debugLayer;
    while (layer != null && !(layer is OpacityLayer))
      layer = layer.firstChild;
116
    expect(layer, isInstanceOf<OpacityLayer>());
117 118
    final OpacityLayer opacityLayer = layer;
    expect(opacityLayer.alpha, equals(opacity * 255));
119
    expect(layer.firstChild, isInstanceOf<TransformLayer>());
120
  });
Adam Barth's avatar
Adam Barth committed
121
}