banner_test.dart 9.96 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 9 10
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

import '../rendering/mock_canvas.dart';
11 12 13 14 15 16 17 18 19 20 21

class TestCanvas implements Canvas {
  final List<Invocation> invocations = <Invocation>[];

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

void main() {
22 23 24 25
  // 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.

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

34
    final TestCanvas canvas = TestCanvas();
35

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

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

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

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

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

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

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

    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);
79
    expect(rotateCommand.positionalArguments[0], equals(math.pi / 4.0));
80 81 82
  });

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

90
    final TestCanvas canvas = TestCanvas();
91

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

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

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

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

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

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

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

    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);
135
    expect(rotateCommand.positionalArguments[0], equals(-math.pi / 4.0));
136 137 138
  });

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

146
    final TestCanvas canvas = TestCanvas();
147

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

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

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

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

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

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

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

    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);
191
    expect(rotateCommand.positionalArguments[0], equals(-math.pi / 4.0));
192 193 194
  });

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

202
    final TestCanvas canvas = TestCanvas();
203

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

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

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

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

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

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

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

    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);
247
    expect(rotateCommand.positionalArguments[0], equals(math.pi / 4.0));
248 249
  });

250
  testWidgets('Banner widget', (WidgetTester tester) async {
251
    debugDisableShadows = false;
252 253 254
    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
255
        child: Banner(message: 'Hello', location: BannerLocation.topEnd),
256 257 258
      ),
    );
    expect(find.byType(CustomPaint), paints
259
      ..save()
260
      ..translate(x: 800.0, y: 0.0)
261
      ..rotate(angle: math.pi / 4.0)
Dan Field's avatar
Dan Field committed
262 263
      ..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)
264
      ..paragraph(offset: const Offset(-40.0, 29.0))
265
      ..restore(),
266
    );
267
    debugDisableShadows = true;
268 269 270
  });

  testWidgets('Banner widget in MaterialApp', (WidgetTester tester) async {
271
    debugDisableShadows = false;
272
    await tester.pumpWidget(const MaterialApp(home: Placeholder()));
273
    expect(find.byType(CheckedModeBanner), paints
274
      ..save()
275
      ..translate(x: 800.0, y: 0.0)
276
      ..rotate(angle: math.pi / 4.0)
Dan Field's avatar
Dan Field committed
277 278
      ..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)
279
      ..paragraph(offset: const Offset(-40.0, 29.0))
280
      ..restore(),
281
    );
282
    debugDisableShadows = true;
283
  });
284
}