clip_test.dart 6.85 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1 2 3 4 5 6
// 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/material.dart';
7 8 9
import 'package:flutter/rendering.dart';

import '../rendering/mock_canvas.dart';
Ian Hickson's avatar
Ian Hickson committed
10 11 12 13 14 15 16 17 18 19 20

final List<String> log = <String>[];

class PathClipper extends CustomClipper<Path> {
  @override
  Path getClip(Size size) {
    log.add('getClip');
    return new Path()
      ..addRect(new Rect.fromLTWH(50.0, 50.0, 100.0, 100.0));
  }
  @override
21
  bool shouldReclip(PathClipper oldClipper) => false;
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
}

class ValueClipper<T> extends CustomClipper<T> {
  ValueClipper(this.message, this.value);

  final String message;
  final T value;

  @override
  T getClip(Size size) {
    log.add(message);
    return value;
  }

  @override
37
  bool shouldReclip(ValueClipper<T> oldClipper) {
38 39
    return oldClipper.message != message || oldClipper.value != value;
  }
Ian Hickson's avatar
Ian Hickson committed
40 41 42
}

void main() {
43 44
  testWidgets('ClipPath', (WidgetTester tester) async {
    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
45 46 47 48 49 50 51 52
      new ClipPath(
        clipper: new PathClipper(),
        child: new GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: () { log.add('tap'); },
        )
      )
    );
53
    expect(log, equals(<String>['getClip']));
Ian Hickson's avatar
Ian Hickson committed
54

55
    await tester.tapAt(const Offset(10.0, 10.0));
56
    expect(log, equals(<String>['getClip']));
Ian Hickson's avatar
Ian Hickson committed
57 58
    log.clear();

59
    await tester.tapAt(const Offset(100.0, 100.0));
60
    expect(log, equals(<String>['tap']));
Ian Hickson's avatar
Ian Hickson committed
61 62 63
    log.clear();
  });

64 65
  testWidgets('ClipOval', (WidgetTester tester) async {
    await tester.pumpWidget(
Ian Hickson's avatar
Ian Hickson committed
66 67 68 69 70 71 72
      new ClipOval(
        child: new GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: () { log.add('tap'); },
        )
      )
    );
73
    expect(log, equals(<String>[]));
Ian Hickson's avatar
Ian Hickson committed
74

75
    await tester.tapAt(const Offset(10.0, 10.0));
76
    expect(log, equals(<String>[]));
Ian Hickson's avatar
Ian Hickson committed
77 78
    log.clear();

79
    await tester.tapAt(const Offset(400.0, 300.0));
80
    expect(log, equals(<String>['tap']));
Ian Hickson's avatar
Ian Hickson committed
81 82
    log.clear();
  });
83

84 85 86 87 88 89 90 91 92 93 94 95 96 97
  testWidgets('Transparent ClipOval hit test', (WidgetTester tester) async {
    await tester.pumpWidget(
      new Opacity(
        opacity: 0.0,
        child: new ClipOval(
          child: new GestureDetector(
            behavior: HitTestBehavior.opaque,
            onTap: () { log.add('tap'); },
          )
        )
      )
    );
    expect(log, equals(<String>[]));

98
    await tester.tapAt(const Offset(10.0, 10.0));
99 100 101
    expect(log, equals(<String>[]));
    log.clear();

102
    await tester.tapAt(const Offset(400.0, 300.0));
103 104 105 106
    expect(log, equals(<String>['tap']));
    log.clear();
  });

107 108 109
  testWidgets('ClipRect', (WidgetTester tester) async {
    await tester.pumpWidget(
      new Align(
110
        alignment: Alignment.topLeft,
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
        child: new SizedBox(
          width: 100.0,
          height: 100.0,
          child: new ClipRect(
            clipper: new ValueClipper<Rect>('a', new Rect.fromLTWH(5.0, 5.0, 10.0, 10.0)),
            child: new GestureDetector(
              behavior: HitTestBehavior.opaque,
              onTap: () { log.add('tap'); },
            )
          )
        )
      )
    );
    expect(log, equals(<String>['a']));

126
    await tester.tapAt(const Offset(10.0, 10.0));
127 128
    expect(log, equals(<String>['a', 'tap']));

129
    await tester.tapAt(const Offset(100.0, 100.0));
130 131 132 133
    expect(log, equals(<String>['a', 'tap']));

    await tester.pumpWidget(
      new Align(
134
        alignment: Alignment.topLeft,
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
        child: new SizedBox(
          width: 100.0,
          height: 100.0,
          child: new ClipRect(
            clipper: new ValueClipper<Rect>('a', new Rect.fromLTWH(5.0, 5.0, 10.0, 10.0)),
            child: new GestureDetector(
              behavior: HitTestBehavior.opaque,
              onTap: () { log.add('tap'); },
            )
          )
        )
      )
    );
    expect(log, equals(<String>['a', 'tap']));

    await tester.pumpWidget(
      new Align(
152
        alignment: Alignment.topLeft,
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
        child: new SizedBox(
          width: 200.0,
          height: 200.0,
          child: new ClipRect(
            clipper: new ValueClipper<Rect>('a', new Rect.fromLTWH(5.0, 5.0, 10.0, 10.0)),
            child: new GestureDetector(
              behavior: HitTestBehavior.opaque,
              onTap: () { log.add('tap'); },
            )
          )
        )
      )
    );
    expect(log, equals(<String>['a', 'tap', 'a']));

    await tester.pumpWidget(
      new Align(
170
        alignment: Alignment.topLeft,
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
        child: new SizedBox(
          width: 200.0,
          height: 200.0,
          child: new ClipRect(
            clipper: new ValueClipper<Rect>('a', new Rect.fromLTWH(5.0, 5.0, 10.0, 10.0)),
            child: new GestureDetector(
              behavior: HitTestBehavior.opaque,
              onTap: () { log.add('tap'); },
            )
          )
        )
      )
    );
    expect(log, equals(<String>['a', 'tap', 'a']));

    await tester.pumpWidget(
      new Align(
188
        alignment: Alignment.topLeft,
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
        child: new SizedBox(
          width: 200.0,
          height: 200.0,
          child: new ClipRect(
            clipper: new ValueClipper<Rect>('b', new Rect.fromLTWH(5.0, 5.0, 10.0, 10.0)),
            child: new GestureDetector(
              behavior: HitTestBehavior.opaque,
              onTap: () { log.add('tap'); },
            )
          )
        )
      )
    );
    expect(log, equals(<String>['a', 'tap', 'a', 'b']));

    await tester.pumpWidget(
      new Align(
206
        alignment: Alignment.topLeft,
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
        child: new SizedBox(
          width: 200.0,
          height: 200.0,
          child: new ClipRect(
            clipper: new ValueClipper<Rect>('c', new Rect.fromLTWH(25.0, 25.0, 10.0, 10.0)),
            child: new GestureDetector(
              behavior: HitTestBehavior.opaque,
              onTap: () { log.add('tap'); },
            )
          )
        )
      )
    );
    expect(log, equals(<String>['a', 'tap', 'a', 'b', 'c']));

222
    await tester.tapAt(const Offset(30.0, 30.0));
223 224
    expect(log, equals(<String>['a', 'tap', 'a', 'b', 'c', 'tap']));

225
    await tester.tapAt(const Offset(100.0, 100.0));
226 227 228
    expect(log, equals(<String>['a', 'tap', 'a', 'b', 'c', 'tap']));
  });

229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
  testWidgets('debugPaintSizeEnabled', (WidgetTester tester) async {
    await tester.pumpWidget(
      const ClipRect(
        child: const Placeholder(),
      ),
    );
    expect(tester.renderObject(find.byType(ClipRect)).paint, paints
      ..save()
      ..clipRect(rect: new Rect.fromLTRB(0.0, 0.0, 800.0, 600.0))
      ..save()
      ..path() // Placeholder
      ..restore()
      ..restore()
    );
    debugPaintSizeEnabled = true;
    expect(tester.renderObject(find.byType(ClipRect)).debugPaint, paints // ignore: INVALID_USE_OF_PROTECTED_MEMBER
      ..rect(rect: new Rect.fromLTRB(0.0, 0.0, 800.0, 600.0))
      ..paragraph()
    );
    debugPaintSizeEnabled = false;
  });
Ian Hickson's avatar
Ian Hickson committed
250
}