snack_bar_test.dart 17.5 KB
Newer Older
Hixie's avatar
Hixie committed
1 2 3 4
// 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.

Adam Barth's avatar
Adam Barth committed
5
import 'package:flutter_test/flutter_test.dart';
6
import 'package:flutter/material.dart';
7 8

void main() {
9
  testWidgets('SnackBar control test', (WidgetTester tester) async {
10
    String helloSnackBar = 'Hello SnackBar';
11
    Key tapTarget = const Key('tap-target');
12
    await tester.pumpWidget(new MaterialApp(
13 14 15 16 17 18 19
      home: new Scaffold(
        body: new Builder(
          builder: (BuildContext context) {
            return new GestureDetector(
              onTap: () {
                Scaffold.of(context).showSnackBar(new SnackBar(
                  content: new Text(helloSnackBar),
20
                  duration: const Duration(seconds: 2)
21 22 23 24 25 26 27 28 29 30
                ));
              },
              behavior: HitTestBehavior.opaque,
              child: new Container(
                height: 100.0,
                width: 100.0,
                key: tapTarget
              )
            );
          }
31
        )
32 33 34
      )
    ));
    expect(find.text(helloSnackBar), findsNothing);
35
    await tester.tap(find.byKey(tapTarget));
36
    expect(find.text(helloSnackBar), findsNothing);
37
    await tester.pump(); // schedule animation
38
    expect(find.text(helloSnackBar), findsOneWidget);
39
    await tester.pump(); // begin animation
40
    expect(find.text(helloSnackBar), findsOneWidget);
41
    await tester.pump(const Duration(milliseconds: 750)); // 0.75s // animation last frame; two second timer starts here
42
    expect(find.text(helloSnackBar), findsOneWidget);
43
    await tester.pump(const Duration(milliseconds: 750)); // 1.50s
44
    expect(find.text(helloSnackBar), findsOneWidget);
45
    await tester.pump(const Duration(milliseconds: 750)); // 2.25s
46
    expect(find.text(helloSnackBar), findsOneWidget);
47
    await tester.pump(const Duration(milliseconds: 750)); // 3.00s // timer triggers to dismiss snackbar, reverse animation is scheduled
48
    await tester.pump(); // begin animation
49
    expect(find.text(helloSnackBar), findsOneWidget); // frame 0 of dismiss animation
50
    await tester.pump(const Duration(milliseconds: 750)); // 3.75s // last frame of animation, snackbar removed from build
51
    expect(find.text(helloSnackBar), findsNothing);
Hixie's avatar
Hixie committed
52 53
  });

54
  testWidgets('SnackBar twice test', (WidgetTester tester) async {
55
    int snackBarCount = 0;
56
    Key tapTarget = const Key('tap-target');
57
    await tester.pumpWidget(new MaterialApp(
58 59 60 61 62 63 64 65
      home: new Scaffold(
        body: new Builder(
          builder: (BuildContext context) {
            return new GestureDetector(
              onTap: () {
                snackBarCount += 1;
                Scaffold.of(context).showSnackBar(new SnackBar(
                  content: new Text("bar$snackBarCount"),
66
                  duration: const Duration(seconds: 2)
67 68 69 70 71 72 73 74 75 76
                ));
              },
              behavior: HitTestBehavior.opaque,
              child: new Container(
                height: 100.0,
                width: 100.0,
                key: tapTarget
              )
            );
          }
77
        )
78 79 80 81
      )
    ));
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsNothing);
82 83
    await tester.tap(find.byKey(tapTarget)); // queue bar1
    await tester.tap(find.byKey(tapTarget)); // queue bar2
84 85
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsNothing);
86
    await tester.pump(); // schedule animation for bar1
87 88
    expect(find.text('bar1'), findsOneWidget);
    expect(find.text('bar2'), findsNothing);
89
    await tester.pump(); // begin animation
90 91
    expect(find.text('bar1'), findsOneWidget);
    expect(find.text('bar2'), findsNothing);
92
    await tester.pump(const Duration(milliseconds: 750)); // 0.75s // animation last frame; two second timer starts here
93 94
    expect(find.text('bar1'), findsOneWidget);
    expect(find.text('bar2'), findsNothing);
95
    await tester.pump(const Duration(milliseconds: 750)); // 1.50s
96 97
    expect(find.text('bar1'), findsOneWidget);
    expect(find.text('bar2'), findsNothing);
98
    await tester.pump(const Duration(milliseconds: 750)); // 2.25s
99 100
    expect(find.text('bar1'), findsOneWidget);
    expect(find.text('bar2'), findsNothing);
101
    await tester.pump(const Duration(milliseconds: 750)); // 3.00s // timer triggers to dismiss snackbar, reverse animation is scheduled
102
    await tester.pump(); // begin animation
103 104
    expect(find.text('bar1'), findsOneWidget);
    expect(find.text('bar2'), findsNothing);
105
    await tester.pump(const Duration(milliseconds: 750)); // 3.75s // last frame of animation, snackbar removed from build, new snack bar put in its place
106 107
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsOneWidget);
108
    await tester.pump(); // begin animation
109 110
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsOneWidget);
111
    await tester.pump(const Duration(milliseconds: 750)); // 4.50s // animation last frame; two second timer starts here
112 113
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsOneWidget);
114
    await tester.pump(const Duration(milliseconds: 750)); // 5.25s
115 116
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsOneWidget);
117
    await tester.pump(const Duration(milliseconds: 750)); // 6.00s
118 119
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsOneWidget);
120
    await tester.pump(const Duration(milliseconds: 750)); // 6.75s // timer triggers to dismiss snackbar, reverse animation is scheduled
121
    await tester.pump(); // begin animation
122 123
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsOneWidget);
124
    await tester.pump(const Duration(milliseconds: 750)); // 7.50s // last frame of animation, snackbar removed from build, new snack bar put in its place
125 126
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsNothing);
127
  });
128

129
  testWidgets('SnackBar cancel test', (WidgetTester tester) async {
130
    int snackBarCount = 0;
131
    Key tapTarget = const Key('tap-target');
132
    int time;
133
    ScaffoldFeatureController<SnackBar, SnackBarClosedReason> lastController;
134
    await tester.pumpWidget(new MaterialApp(
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
      home: new Scaffold(
        body: new Builder(
          builder: (BuildContext context) {
            return new GestureDetector(
              onTap: () {
                snackBarCount += 1;
                lastController = Scaffold.of(context).showSnackBar(new SnackBar(
                  content: new Text("bar$snackBarCount"),
                  duration: new Duration(seconds: time)
                ));
              },
              behavior: HitTestBehavior.opaque,
              child: new Container(
                height: 100.0,
                width: 100.0,
                key: tapTarget
              )
            );
          }
154
        )
155 156 157 158 159
      )
    ));
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsNothing);
    time = 1000;
160
    await tester.tap(find.byKey(tapTarget)); // queue bar1
161
    ScaffoldFeatureController<SnackBar, SnackBarClosedReason> firstController = lastController;
162
    time = 2;
163
    await tester.tap(find.byKey(tapTarget)); // queue bar2
164 165
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsNothing);
166
    await tester.pump(); // schedule animation for bar1
167 168
    expect(find.text('bar1'), findsOneWidget);
    expect(find.text('bar2'), findsNothing);
169
    await tester.pump(); // begin animation
170 171
    expect(find.text('bar1'), findsOneWidget);
    expect(find.text('bar2'), findsNothing);
172
    await tester.pump(const Duration(milliseconds: 750)); // 0.75s // animation last frame; two second timer starts here
173 174
    expect(find.text('bar1'), findsOneWidget);
    expect(find.text('bar2'), findsNothing);
175
    await tester.pump(const Duration(milliseconds: 750)); // 1.50s
176 177
    expect(find.text('bar1'), findsOneWidget);
    expect(find.text('bar2'), findsNothing);
178
    await tester.pump(const Duration(milliseconds: 750)); // 2.25s
179 180
    expect(find.text('bar1'), findsOneWidget);
    expect(find.text('bar2'), findsNothing);
181
    await tester.pump(const Duration(milliseconds: 10000)); // 12.25s
182 183
    expect(find.text('bar1'), findsOneWidget);
    expect(find.text('bar2'), findsNothing);
184

185
    firstController.close(); // snackbar is manually dismissed
186

187
    await tester.pump(const Duration(milliseconds: 750)); // 13.00s // reverse animation is scheduled
188
    await tester.pump(); // begin animation
189 190
    expect(find.text('bar1'), findsOneWidget);
    expect(find.text('bar2'), findsNothing);
191
    await tester.pump(const Duration(milliseconds: 750)); // 13.75s // last frame of animation, snackbar removed from build, new snack bar put in its place
192 193
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsOneWidget);
194
    await tester.pump(); // begin animation
195 196
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsOneWidget);
197
    await tester.pump(const Duration(milliseconds: 750)); // 14.50s // animation last frame; two second timer starts here
198 199
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsOneWidget);
200
    await tester.pump(const Duration(milliseconds: 750)); // 15.25s
201 202
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsOneWidget);
203
    await tester.pump(const Duration(milliseconds: 750)); // 16.00s
204 205
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsOneWidget);
206
    await tester.pump(const Duration(milliseconds: 750)); // 16.75s // timer triggers to dismiss snackbar, reverse animation is scheduled
207
    await tester.pump(); // begin animation
208 209
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsOneWidget);
210
    await tester.pump(const Duration(milliseconds: 750)); // 17.50s // last frame of animation, snackbar removed from build, new snack bar put in its place
211 212
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsNothing);
213
  });
214

215
  testWidgets('SnackBar dismiss test', (WidgetTester tester) async {
216
    int snackBarCount = 0;
217
    Key tapTarget = const Key('tap-target');
218
    await tester.pumpWidget(new MaterialApp(
219 220 221 222 223 224 225 226
      home: new Scaffold(
        body: new Builder(
          builder: (BuildContext context) {
            return new GestureDetector(
              onTap: () {
                snackBarCount += 1;
                Scaffold.of(context).showSnackBar(new SnackBar(
                  content: new Text("bar$snackBarCount"),
227
                  duration: const Duration(seconds: 2)
228 229 230 231 232 233 234 235 236 237
                ));
              },
              behavior: HitTestBehavior.opaque,
              child: new Container(
                height: 100.0,
                width: 100.0,
                key: tapTarget
              )
            );
          }
238
        )
239 240 241 242
      )
    ));
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsNothing);
243 244
    await tester.tap(find.byKey(tapTarget)); // queue bar1
    await tester.tap(find.byKey(tapTarget)); // queue bar2
245 246
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsNothing);
247
    await tester.pump(); // schedule animation for bar1
248 249
    expect(find.text('bar1'), findsOneWidget);
    expect(find.text('bar2'), findsNothing);
250
    await tester.pump(); // begin animation
251 252
    expect(find.text('bar1'), findsOneWidget);
    expect(find.text('bar2'), findsNothing);
253 254
    await tester.pump(const Duration(milliseconds: 750)); // 0.75s // animation last frame; two second timer starts here
    await tester.scroll(find.text('bar1'), const Offset(0.0, 50.0));
255
    await tester.pump(); // bar1 dismissed, bar2 begins animating
256 257
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsOneWidget);
258 259
  });

260
  testWidgets('SnackBar cannot be tapped twice', (WidgetTester tester) async {
261
    int tapCount = 0;
262
    await tester.pumpWidget(new MaterialApp(
263 264 265 266 267 268 269
      home: new Scaffold(
        body: new Builder(
          builder: (BuildContext context) {
            return new GestureDetector(
              onTap: () {
                Scaffold.of(context).showSnackBar(new SnackBar(
                  content: new Text('I am a snack bar.'),
270
                  duration: const Duration(seconds: 2),
271 272 273 274 275 276 277 278 279 280 281
                  action: new SnackBarAction(
                    label: 'ACTION',
                    onPressed: () {
                      ++tapCount;
                    }
                  )
                ));
              },
              child: new Text('X')
            );
          }
282
        )
283 284
      )
    ));
285 286 287
    await tester.tap(find.text('X'));
    await tester.pump(); // start animation
    await tester.pump(const Duration(milliseconds: 750));
288

289
    expect(tapCount, equals(0));
290
    await tester.tap(find.text('ACTION'));
291
    expect(tapCount, equals(1));
292
    await tester.tap(find.text('ACTION'));
293
    expect(tapCount, equals(1));
294 295
    await tester.pump();
    await tester.tap(find.text('ACTION'));
296
    expect(tapCount, equals(1));
297
  });
298 299 300 301 302 303 304 305 306 307

  testWidgets('SnackBar button text alignment', (WidgetTester tester) async {
    await tester.pumpWidget(new MaterialApp(
      home: new Scaffold(
        body: new Builder(
          builder: (BuildContext context) {
            return new GestureDetector(
              onTap: () {
                Scaffold.of(context).showSnackBar(new SnackBar(
                  content: new Text('I am a snack bar.'),
308
                  duration: const Duration(seconds: 2),
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
                  action: new SnackBarAction(label: 'ACTION', onPressed: () {})
                ));
              },
              child: new Text('X')
            );
          }
        )
      )
    ));
    await tester.tap(find.text('X'));
    await tester.pump(); // start animation
    await tester.pump(const Duration(milliseconds: 750));

    RenderBox textBox = tester.firstRenderObject(find.text('I am a snack bar.'));
    RenderBox actionTextBox = tester.firstRenderObject(find.text('ACTION'));
    RenderBox snackBarBox = tester.firstRenderObject(find.byType(SnackBar));

    Point textBottomLeft = textBox.localToGlobal(textBox.size.bottomLeft(Point.origin));
    Point textBottomRight = textBox.localToGlobal(textBox.size.bottomRight(Point.origin));
    Point actionTextBottomLeft = actionTextBox.localToGlobal(actionTextBox.size.bottomLeft(Point.origin));
    Point actionTextBottomRight = actionTextBox.localToGlobal(actionTextBox.size.bottomRight(Point.origin));
    Point snackBarBottomLeft = snackBarBox.localToGlobal(snackBarBox.size.bottomLeft(Point.origin));
    Point snackBarBottomRight = snackBarBox.localToGlobal(snackBarBox.size.bottomRight(Point.origin));

    expect(textBottomLeft.x - snackBarBottomLeft.x, 24.0);
    expect(actionTextBottomLeft.x - textBottomRight.x, 24.0);
    expect(snackBarBottomRight.x - actionTextBottomRight.x, 24.0);
  });
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351

  testWidgets('SnackBarClosedReason', (WidgetTester tester) async {
    final GlobalKey<ScaffoldState> scaffoldKey = new GlobalKey<ScaffoldState>();
    bool actionPressed = false;
    SnackBarClosedReason closedReason;

    await tester.pumpWidget(new MaterialApp(
      home: new Scaffold(
        key: scaffoldKey,
        body: new Builder(
          builder: (BuildContext context) {
            return new GestureDetector(
              onTap: () {
                Scaffold.of(context).showSnackBar(new SnackBar(
                  content: new Text('snack'),
352
                  duration: const Duration(seconds: 2),
353 354 355 356 357 358
                  action: new SnackBarAction(
                    label: 'ACTION',
                    onPressed: () {
                      actionPressed = true;
                    }
                  ),
359
                )).closed.then<Null>((SnackBarClosedReason reason) {
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
                  closedReason = reason;
                });
              },
              child: new Text('X')
            );
          },
        )
      )
    ));

    // Pop up the snack bar and then press its action button.
    await tester.tap(find.text('X'));
    await tester.pump(); // start animation
    await tester.pump(const Duration(milliseconds: 750));
    expect(actionPressed, isFalse);
    await tester.tap(find.text('ACTION'));
    expect(actionPressed, isTrue);
    await tester.pump(const Duration(seconds: 1));
    expect(closedReason, equals(SnackBarClosedReason.action));

    // Pop up the snack bar and then swipe downwards to dismiss it.
    await tester.tap(find.text('X'));
    await tester.pump(const Duration(milliseconds: 750));
    await tester.pump(const Duration(milliseconds: 750));
384
    await tester.scroll(find.text('snack'), const Offset(0.0, 50.0));
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
    await tester.pump();
    expect(closedReason, equals(SnackBarClosedReason.swipe));

    // Pop up the snack bar and then remove it.
    await tester.tap(find.text('X'));
    await tester.pump(const Duration(milliseconds: 750));
    scaffoldKey.currentState.removeCurrentSnackBar();
    await tester.pump(const Duration(seconds: 1));
    expect(closedReason, equals(SnackBarClosedReason.remove));

    // Pop up the snack bar and then hide it.
    await tester.tap(find.text('X'));
    await tester.pump(const Duration(milliseconds: 750));
    scaffoldKey.currentState.hideCurrentSnackBar();
    await tester.pump(const Duration(seconds: 1));
    expect(closedReason, equals(SnackBarClosedReason.hide));

    // Pop up the snack bar and then let it time out.
    await tester.tap(find.text('X'));
404 405 406
    await tester.pump(const Duration(milliseconds: 750));
    await tester.pump(const Duration(milliseconds: 750));
    await tester.pump(const Duration(milliseconds: 1500));
407
    await tester.pump(); // begin animation
408
    await tester.pump(const Duration(milliseconds: 750));
409 410 411
    expect(closedReason, equals(SnackBarClosedReason.timeout));
  });

412
}