banner_test.dart 9.89 KB
Newer Older
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 '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 = new 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 = new 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 51 52 53
      return invocation.memberName == #rotate;
    });

    expect(rotateCommand, isNotNull);
    expect(rotateCommand.positionalArguments[0], equals(-math.PI / 4.0));
  });

54 55 56
  test('A Banner with a location of topStart paints in the top right (RTL)', () {
    final BannerPainter bannerPainter = new BannerPainter(
      message: 'foo',
57
      textDirection: TextDirection.ltr,
58
      location: BannerLocation.topStart,
59
      layoutDirection: TextDirection.rtl,
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    );

    final TestCanvas canvas = new TestCanvas();

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

  test('A Banner with a location of topEnd paints in the top right (LTR)', () {
83
    final BannerPainter bannerPainter = new 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 = new 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 107 108 109
      return invocation.memberName == #rotate;
    });

    expect(rotateCommand, isNotNull);
    expect(rotateCommand.positionalArguments[0], equals(math.PI / 4.0));
  });

110 111 112 113 114
  test('A Banner with a location of topEnd paints in the top left (RTL)', () {
    final BannerPainter bannerPainter = new BannerPainter(
      message: 'foo',
      textDirection: TextDirection.rtl,
      location: BannerLocation.topEnd,
115
      layoutDirection: TextDirection.rtl,
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    );

    final TestCanvas canvas = new TestCanvas();

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

  test('A Banner with a location of bottomStart paints in the bottom left (LTR)', () {
139
    final BannerPainter bannerPainter = new 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 = new 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 163 164 165
      return invocation.memberName == #rotate;
    });

    expect(rotateCommand, isNotNull);
    expect(rotateCommand.positionalArguments[0], equals(math.PI / 4.0));
  });

166 167 168 169 170
  test('A Banner with a location of bottomStart paints in the bottom right (RTL)', () {
    final BannerPainter bannerPainter = new BannerPainter(
      message: 'foo',
      textDirection: TextDirection.rtl,
      location: BannerLocation.bottomStart,
171
      layoutDirection: TextDirection.rtl,
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
    );

    final TestCanvas canvas = new TestCanvas();

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

  test('A Banner with a location of bottomEnd paints in the bottom right (LTR)', () {
195
    final BannerPainter bannerPainter = new 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 = new 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 219 220
      return invocation.memberName == #rotate;
    });

    expect(rotateCommand, isNotNull);
    expect(rotateCommand.positionalArguments[0], equals(-math.PI / 4.0));
  });
221 222 223 224

  test('A Banner with a location of bottomEnd paints in the bottom left (RTL)', () {
    final BannerPainter bannerPainter = new BannerPainter(
      message: 'foo',
225
      textDirection: TextDirection.ltr,
226
      location: BannerLocation.bottomEnd,
227
      layoutDirection: TextDirection.rtl,
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
    );

    final TestCanvas canvas = new TestCanvas();

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

250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
  testWidgets('Banner widget', (WidgetTester tester) async {
    await tester.pumpWidget(
      const Directionality(
        textDirection: TextDirection.ltr,
        child: const Banner(message: 'Hello', location: BannerLocation.topEnd),
      ),
    );
    expect(find.byType(CustomPaint), paints
      ..save
      ..translate(x: 800.0, y: 0.0)
      ..rotate(angle: math.PI / 4.0)
      ..rect(rect: new Rect.fromLTRB(-40.0, 28.0, 40.0, 40.0), color: const Color(0x7f000000), hasMaskFilter: true)
      ..rect(rect: new Rect.fromLTRB(-40.0, 28.0, 40.0, 40.0), color: const Color(0xa0b71c1c), hasMaskFilter: false)
      ..paragraph(offset: const Offset(-40.0, 29.0))
      ..restore
    );
  });

  testWidgets('Banner widget in MaterialApp', (WidgetTester tester) async {
    await tester.pumpWidget(new MaterialApp(home: const Placeholder()));
    expect(find.byType(CheckedModeBanner), paints
      ..save
      ..translate(x: 800.0, y: 0.0)
      ..rotate(angle: math.PI / 4.0)
      ..rect(rect: new Rect.fromLTRB(-40.0, 28.0, 40.0, 40.0), color: const Color(0x7f000000), hasMaskFilter: true)
      ..rect(rect: new Rect.fromLTRB(-40.0, 28.0, 40.0, 40.0), color: const Color(0xa0b71c1c), hasMaskFilter: false)
      ..paragraph(offset: const Offset(-40.0, 24.0))
      ..restore
    );
  });
280
}