banner_test.dart 10.4 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:math' as math;

7 8
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
9
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
10

11 12 13 14 15 16 17 18 19 20
class TestCanvas implements Canvas {
  final List<Invocation> invocations = <Invocation>[];

  @override
  void noSuchMethod(Invocation invocation) {
    invocations.add(invocation);
  }
}

void main() {
21 22 23 24
  // the textDirection values below are intentionally sometimes different and
  // sometimes the same as the layoutDirection, to make sure that they don't
  // affect the layout.

25
  test('A Banner with a location of topStart paints in the top left (LTR)', () {
26
    final BannerPainter bannerPainter = BannerPainter(
Ian Hickson's avatar
Ian Hickson committed
27
      message: 'foo',
28 29 30
      textDirection: TextDirection.rtl,
      location: BannerLocation.topStart,
      layoutDirection: TextDirection.ltr,
31 32
    );

33
    final TestCanvas canvas = TestCanvas();
34

35
    bannerPainter.paint(canvas, const Size(1000.0, 1000.0));
36

37
    final Invocation translateCommand = canvas.invocations.firstWhere((Invocation invocation) {
38 39 40 41 42 43 44
      return invocation.memberName == #translate;
    });

    expect(translateCommand, isNotNull);
    expect(translateCommand.positionalArguments[0], lessThan(100.0));
    expect(translateCommand.positionalArguments[1], lessThan(100.0));

45
    final Invocation rotateCommand = canvas.invocations.firstWhere((Invocation invocation) {
46 47 48 49
      return invocation.memberName == #rotate;
    });

    expect(rotateCommand, isNotNull);
50
    expect(rotateCommand.positionalArguments[0], equals(-math.pi / 4.0));
51 52
  });

53
  test('A Banner with a location of topStart paints in the top right (RTL)', () {
54
    final BannerPainter bannerPainter = BannerPainter(
55
      message: 'foo',
56
      textDirection: TextDirection.ltr,
57
      location: BannerLocation.topStart,
58
      layoutDirection: TextDirection.rtl,
59 60
    );

61
    final TestCanvas canvas = TestCanvas();
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

    bannerPainter.paint(canvas, const Size(1000.0, 1000.0));

    final Invocation translateCommand = canvas.invocations.firstWhere((Invocation invocation) {
      return invocation.memberName == #translate;
    });

    expect(translateCommand, isNotNull);
    expect(translateCommand.positionalArguments[0], greaterThan(900.0));
    expect(translateCommand.positionalArguments[1], lessThan(100.0));

    final Invocation rotateCommand = canvas.invocations.firstWhere((Invocation invocation) {
      return invocation.memberName == #rotate;
    });

    expect(rotateCommand, isNotNull);
78
    expect(rotateCommand.positionalArguments[0], equals(math.pi / 4.0));
79 80 81
  });

  test('A Banner with a location of topEnd paints in the top right (LTR)', () {
82
    final BannerPainter bannerPainter = BannerPainter(
Ian Hickson's avatar
Ian Hickson committed
83 84
      message: 'foo',
      textDirection: TextDirection.ltr,
85 86
      location: BannerLocation.topEnd,
      layoutDirection: TextDirection.ltr,
87 88
    );

89
    final TestCanvas canvas = TestCanvas();
90

91
    bannerPainter.paint(canvas, const Size(1000.0, 1000.0));
92

93
    final Invocation translateCommand = canvas.invocations.firstWhere((Invocation invocation) {
94 95 96 97 98 99 100
      return invocation.memberName == #translate;
    });

    expect(translateCommand, isNotNull);
    expect(translateCommand.positionalArguments[0], greaterThan(900.0));
    expect(translateCommand.positionalArguments[1], lessThan(100.0));

101
    final Invocation rotateCommand = canvas.invocations.firstWhere((Invocation invocation) {
102 103 104 105
      return invocation.memberName == #rotate;
    });

    expect(rotateCommand, isNotNull);
106
    expect(rotateCommand.positionalArguments[0], equals(math.pi / 4.0));
107 108
  });

109
  test('A Banner with a location of topEnd paints in the top left (RTL)', () {
110
    final BannerPainter bannerPainter = BannerPainter(
111 112 113
      message: 'foo',
      textDirection: TextDirection.rtl,
      location: BannerLocation.topEnd,
114
      layoutDirection: TextDirection.rtl,
115 116
    );

117
    final TestCanvas canvas = TestCanvas();
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

    bannerPainter.paint(canvas, const Size(1000.0, 1000.0));

    final Invocation translateCommand = canvas.invocations.firstWhere((Invocation invocation) {
      return invocation.memberName == #translate;
    });

    expect(translateCommand, isNotNull);
    expect(translateCommand.positionalArguments[0], lessThan(100.0));
    expect(translateCommand.positionalArguments[1], lessThan(100.0));

    final Invocation rotateCommand = canvas.invocations.firstWhere((Invocation invocation) {
      return invocation.memberName == #rotate;
    });

    expect(rotateCommand, isNotNull);
134
    expect(rotateCommand.positionalArguments[0], equals(-math.pi / 4.0));
135 136 137
  });

  test('A Banner with a location of bottomStart paints in the bottom left (LTR)', () {
138
    final BannerPainter bannerPainter = BannerPainter(
Ian Hickson's avatar
Ian Hickson committed
139 140
      message: 'foo',
      textDirection: TextDirection.ltr,
141 142
      location: BannerLocation.bottomStart,
      layoutDirection: TextDirection.ltr,
143 144
    );

145
    final TestCanvas canvas = TestCanvas();
146

147
    bannerPainter.paint(canvas, const Size(1000.0, 1000.0));
148

149
    final Invocation translateCommand = canvas.invocations.firstWhere((Invocation invocation) {
150 151 152 153 154 155 156
      return invocation.memberName == #translate;
    });

    expect(translateCommand, isNotNull);
    expect(translateCommand.positionalArguments[0], lessThan(100.0));
    expect(translateCommand.positionalArguments[1], greaterThan(900.0));

157
    final Invocation rotateCommand = canvas.invocations.firstWhere((Invocation invocation) {
158 159 160 161
      return invocation.memberName == #rotate;
    });

    expect(rotateCommand, isNotNull);
162
    expect(rotateCommand.positionalArguments[0], equals(math.pi / 4.0));
163 164
  });

165
  test('A Banner with a location of bottomStart paints in the bottom right (RTL)', () {
166
    final BannerPainter bannerPainter = BannerPainter(
167 168 169
      message: 'foo',
      textDirection: TextDirection.rtl,
      location: BannerLocation.bottomStart,
170
      layoutDirection: TextDirection.rtl,
171 172
    );

173
    final TestCanvas canvas = TestCanvas();
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189

    bannerPainter.paint(canvas, const Size(1000.0, 1000.0));

    final Invocation translateCommand = canvas.invocations.firstWhere((Invocation invocation) {
      return invocation.memberName == #translate;
    });

    expect(translateCommand, isNotNull);
    expect(translateCommand.positionalArguments[0], greaterThan(900.0));
    expect(translateCommand.positionalArguments[1], greaterThan(900.0));

    final Invocation rotateCommand = canvas.invocations.firstWhere((Invocation invocation) {
      return invocation.memberName == #rotate;
    });

    expect(rotateCommand, isNotNull);
190
    expect(rotateCommand.positionalArguments[0], equals(-math.pi / 4.0));
191 192 193
  });

  test('A Banner with a location of bottomEnd paints in the bottom right (LTR)', () {
194
    final BannerPainter bannerPainter = BannerPainter(
Ian Hickson's avatar
Ian Hickson committed
195
      message: 'foo',
196 197 198
      textDirection: TextDirection.rtl,
      location: BannerLocation.bottomEnd,
      layoutDirection: TextDirection.ltr,
199 200
    );

201
    final TestCanvas canvas = TestCanvas();
202

203
    bannerPainter.paint(canvas, const Size(1000.0, 1000.0));
204

205
    final Invocation translateCommand = canvas.invocations.firstWhere((Invocation invocation) {
206 207 208 209 210 211 212
      return invocation.memberName == #translate;
    });

    expect(translateCommand, isNotNull);
    expect(translateCommand.positionalArguments[0], greaterThan(900.0));
    expect(translateCommand.positionalArguments[1], greaterThan(900.0));

213
    final Invocation rotateCommand = canvas.invocations.firstWhere((Invocation invocation) {
214 215 216 217
      return invocation.memberName == #rotate;
    });

    expect(rotateCommand, isNotNull);
218
    expect(rotateCommand.positionalArguments[0], equals(-math.pi / 4.0));
219
  });
220 221

  test('A Banner with a location of bottomEnd paints in the bottom left (RTL)', () {
222
    final BannerPainter bannerPainter = BannerPainter(
223
      message: 'foo',
224
      textDirection: TextDirection.ltr,
225
      location: BannerLocation.bottomEnd,
226
      layoutDirection: TextDirection.rtl,
227 228
    );

229
    final TestCanvas canvas = TestCanvas();
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245

    bannerPainter.paint(canvas, const Size(1000.0, 1000.0));

    final Invocation translateCommand = canvas.invocations.firstWhere((Invocation invocation) {
      return invocation.memberName == #translate;
    });

    expect(translateCommand, isNotNull);
    expect(translateCommand.positionalArguments[0], lessThan(100.0));
    expect(translateCommand.positionalArguments[1], greaterThan(900.0));

    final Invocation rotateCommand = canvas.invocations.firstWhere((Invocation invocation) {
      return invocation.memberName == #rotate;
    });

    expect(rotateCommand, isNotNull);
246
    expect(rotateCommand.positionalArguments[0], equals(math.pi / 4.0));
247 248
  });

249
  testWidgetsWithLeakTracking('Banner widget', (WidgetTester tester) async {
250
    debugDisableShadows = false;
251 252 253
    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
254
        child: Banner(message: 'Hello', location: BannerLocation.topEnd),
255 256 257
      ),
    );
    expect(find.byType(CustomPaint), paints
258
      ..save()
259
      ..translate(x: 800.0, y: 0.0)
260
      ..rotate(angle: math.pi / 4.0)
Dan Field's avatar
Dan Field committed
261 262
      ..rect(rect: const Rect.fromLTRB(-40.0, 28.0, 40.0, 40.0), color: const Color(0x7f000000), hasMaskFilter: true)
      ..rect(rect: const Rect.fromLTRB(-40.0, 28.0, 40.0, 40.0), color: const Color(0xa0b71c1c), hasMaskFilter: false)
263
      ..paragraph(offset: const Offset(-40.0, 29.0))
264
      ..restore(),
265
    );
266
    debugDisableShadows = true;
267 268
  });

269
  testWidgetsWithLeakTracking('Banner widget in MaterialApp', (WidgetTester tester) async {
270
    debugDisableShadows = false;
271
    await tester.pumpWidget(const MaterialApp(home: Placeholder()));
272
    expect(find.byType(CheckedModeBanner), paints
273
      ..save()
274
      ..translate(x: 800.0, y: 0.0)
275
      ..rotate(angle: math.pi / 4.0)
Dan Field's avatar
Dan Field committed
276 277
      ..rect(rect: const Rect.fromLTRB(-40.0, 28.0, 40.0, 40.0), color: const Color(0x7f000000), hasMaskFilter: true)
      ..rect(rect: const Rect.fromLTRB(-40.0, 28.0, 40.0, 40.0), color: const Color(0xa0b71c1c), hasMaskFilter: false)
278
      ..paragraph(offset: const Offset(-40.0, 29.0))
279
      ..restore(),
280
    );
281
    debugDisableShadows = true;
282
  });
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297

  test('BannerPainter dispatches memory events', () async {
    await expectLater(
      await memoryEvents(
        () => BannerPainter(
          message: 'foo',
          textDirection: TextDirection.rtl,
          location: BannerLocation.topStart,
          layoutDirection: TextDirection.ltr,
        ).dispose(),
        BannerPainter,
      ),
      areCreateAndDispose,
    );
  });
298
}