snack_bar_test.dart 12.7 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 11
    String helloSnackBar = 'Hello SnackBar';
    Key tapTarget = new Key('tap-target');
12
    await tester.pumpWidget(new MaterialApp(
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
      home: new Scaffold(
        body: new Builder(
          builder: (BuildContext context) {
            return new GestureDetector(
              onTap: () {
                Scaffold.of(context).showSnackBar(new SnackBar(
                  content: new Text(helloSnackBar),
                  duration: new Duration(seconds: 2)
                ));
              },
              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(new Duration(milliseconds: 750)); // 0.75s // animation last frame; two second timer starts here
42
    expect(find.text(helloSnackBar), findsOneWidget);
43
    await tester.pump(new Duration(milliseconds: 750)); // 1.50s
44
    expect(find.text(helloSnackBar), findsOneWidget);
45
    await tester.pump(new Duration(milliseconds: 750)); // 2.25s
46
    expect(find.text(helloSnackBar), findsOneWidget);
47 48
    await tester.pump(new Duration(milliseconds: 750)); // 3.00s // timer triggers to dismiss snackbar, reverse animation is scheduled
    await tester.pump(); // begin animation
49
    expect(find.text(helloSnackBar), findsOneWidget); // frame 0 of dismiss animation
50
    await tester.pump(new 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 56
    int snackBarCount = 0;
    Key tapTarget = new Key('tap-target');
57
    await tester.pumpWidget(new MaterialApp(
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
      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"),
                  duration: new Duration(seconds: 2)
                ));
              },
              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(new 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(new Duration(milliseconds: 750)); // 1.50s
96 97
    expect(find.text('bar1'), findsOneWidget);
    expect(find.text('bar2'), findsNothing);
98
    await tester.pump(new Duration(milliseconds: 750)); // 2.25s
99 100
    expect(find.text('bar1'), findsOneWidget);
    expect(find.text('bar2'), findsNothing);
101 102
    await tester.pump(new Duration(milliseconds: 750)); // 3.00s // timer triggers to dismiss snackbar, reverse animation is scheduled
    await tester.pump(); // begin animation
103 104
    expect(find.text('bar1'), findsOneWidget);
    expect(find.text('bar2'), findsNothing);
105
    await tester.pump(new 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(new 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(new Duration(milliseconds: 750)); // 5.25s
115 116
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsOneWidget);
117
    await tester.pump(new Duration(milliseconds: 750)); // 6.00s
118 119
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsOneWidget);
120 121
    await tester.pump(new Duration(milliseconds: 750)); // 6.75s // timer triggers to dismiss snackbar, reverse animation is scheduled
    await tester.pump(); // begin animation
122 123
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsOneWidget);
124
    await tester.pump(new 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 131 132 133
    int snackBarCount = 0;
    Key tapTarget = new Key('tap-target');
    int time;
    ScaffoldFeatureController<SnackBar, Null> 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 162
    ScaffoldFeatureController<SnackBar, Null> firstController = lastController;
    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(new 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(new Duration(milliseconds: 750)); // 1.50s
176 177
    expect(find.text('bar1'), findsOneWidget);
    expect(find.text('bar2'), findsNothing);
178
    await tester.pump(new Duration(milliseconds: 750)); // 2.25s
179 180
    expect(find.text('bar1'), findsOneWidget);
    expect(find.text('bar2'), findsNothing);
181
    await tester.pump(new 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 188
    await tester.pump(new Duration(milliseconds: 750)); // 13.00s // reverse animation is scheduled
    await tester.pump(); // begin animation
189 190
    expect(find.text('bar1'), findsOneWidget);
    expect(find.text('bar2'), findsNothing);
191
    await tester.pump(new 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(new 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(new Duration(milliseconds: 750)); // 15.25s
201 202
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsOneWidget);
203
    await tester.pump(new Duration(milliseconds: 750)); // 16.00s
204 205
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsOneWidget);
206 207
    await tester.pump(new Duration(milliseconds: 750)); // 16.75s // timer triggers to dismiss snackbar, reverse animation is scheduled
    await tester.pump(); // begin animation
208 209
    expect(find.text('bar1'), findsNothing);
    expect(find.text('bar2'), findsOneWidget);
210
    await tester.pump(new 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 217
    int snackBarCount = 0;
    Key tapTarget = new Key('tap-target');
218
    await tester.pumpWidget(new MaterialApp(
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
      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"),
                  duration: new Duration(seconds: 2)
                ));
              },
              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 255
    await tester.pump(new Duration(milliseconds: 750)); // 0.75s // animation last frame; two second timer starts here
    await tester.scroll(find.text('bar1'), new Offset(0.0, 50.0));
    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 270 271 272 273 274 275 276 277 278 279 280 281
      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.'),
                  duration: new Duration(seconds: 2),
                  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
}