stepper_test.dart 10.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
// Copyright 2016 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 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  testWidgets('Stepper tap callback test', (WidgetTester tester) async {
    int index = 0;

    await tester.pumpWidget(
13 14
      new MaterialApp(
        home: new Material(
15
          child: new Stepper(
16 17 18
            onStepTapped: (int i) {
              index = i;
            },
19
            steps: const <Step>[
20
              const Step(
21
                title: const Text('Step 1'),
22
                content: const SizedBox(
23
                  width: 100.0,
24 25
                  height: 100.0,
                ),
26
              ),
27
              const Step(
28
                title: const Text('Step 2'),
29
                content: const SizedBox(
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
                  width: 100.0,
                  height: 100.0,
                ),
              ),
            ],
          ),
        ),
      ),
    );
    await tester.tap(find.text('Step 2'));
    expect(index, 1);
  });

  testWidgets('Stepper expansion test', (WidgetTester tester) async {
    await tester.pumpWidget(
      new MaterialApp(
        home: new Center(
          child: new Material(
            child: new Stepper(
49
              steps: const <Step>[
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
                const Step(
                  title: const Text('Step 1'),
                  content: const SizedBox(
                    width: 100.0,
                    height: 100.0,
                  ),
                ),
                const Step(
                  title: const Text('Step 2'),
                  content: const SizedBox(
                    width: 200.0,
                    height: 200.0,
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
69 70 71 72 73 74
    );

    RenderBox box = tester.renderObject(find.byType(Stepper));
    expect(box.size.height, 332.0);

    await tester.pumpWidget(
75 76 77 78 79
      new MaterialApp(
        home: new Center(
          child: new Material(
            child: new Stepper(
              currentStep: 1,
80
              steps: const <Step>[
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
                const Step(
                  title: const Text('Step 1'),
                  content: const SizedBox(
                    width: 100.0,
                    height: 100.0,
                  ),
                ),
                const Step(
                  title: const Text('Step 2'),
                  content: const SizedBox(
                    width: 200.0,
                    height: 200.0,
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
100 101 102 103 104 105 106 107 108 109 110 111
    );

    await tester.pump(const Duration(milliseconds: 100));
    box = tester.renderObject(find.byType(Stepper));
    expect(box.size.height, greaterThan(332.0));
    await tester.pump(const Duration(milliseconds: 100));
    box = tester.renderObject(find.byType(Stepper));
    expect(box.size.height, 432.0);
  });

  testWidgets('Stepper horizontal size test', (WidgetTester tester) async {
    await tester.pumpWidget(
112 113 114 115 116
      new MaterialApp(
        home: new Center(
          child: new Material(
            child: new Stepper(
              type: StepperType.horizontal,
117
              steps: const <Step>[
118 119 120 121 122 123 124 125 126 127 128 129
                const Step(
                  title: const Text('Step 1'),
                  content: const SizedBox(
                    width: 100.0,
                    height: 100.0,
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
130 131
    );

132
    final RenderBox box = tester.renderObject(find.byType(Stepper));
133 134 135 136 137
    expect(box.size.height, 600.0);
  });

  testWidgets('Stepper visibility test', (WidgetTester tester) async {
    await tester.pumpWidget(
138 139 140 141
      new MaterialApp(
        home: new Material(
          child: new Stepper(
            type: StepperType.horizontal,
142
            steps: const <Step>[
143 144 145 146 147 148 149 150 151 152 153 154
              const Step(
                title: const Text('Step 1'),
                content: const Text('A'),
              ),
              const Step(
                title: const Text('Step 2'),
                content: const Text('B'),
              ),
            ],
          ),
        ),
      ),
155 156 157 158 159 160
    );

    expect(find.text('A'), findsOneWidget);
    expect(find.text('B'), findsNothing);

    await tester.pumpWidget(
161 162 163 164 165
      new MaterialApp(
        home: new Material(
          child: new Stepper(
            currentStep: 1,
            type: StepperType.horizontal,
166
            steps: const <Step>[
167 168 169 170 171 172 173 174 175 176 177 178
              const Step(
                title: const Text('Step 1'),
                content: const Text('A'),
              ),
              const Step(
                title: const Text('Step 2'),
                content: const Text('B'),
              ),
            ],
          ),
        ),
      ),
179 180 181 182 183 184 185 186 187 188 189
    );

    expect(find.text('A'), findsNothing);
    expect(find.text('B'), findsOneWidget);
  });

  testWidgets('Stepper button test', (WidgetTester tester) async {
    bool continuePressed = false;
    bool cancelPressed = false;

    await tester.pumpWidget(
190 191 192 193 194 195 196 197 198 199
      new MaterialApp(
        home: new Material(
          child: new Stepper(
            type: StepperType.horizontal,
            onStepContinue: () {
              continuePressed = true;
            },
            onStepCancel: () {
              cancelPressed = true;
            },
200
            steps: const <Step>[
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
              const Step(
                title: const Text('Step 1'),
                content: const SizedBox(
                  width: 100.0,
                  height: 100.0,
                ),
              ),
              const Step(
                title: const Text('Step 2'),
                content: const SizedBox(
                  width: 200.0,
                  height: 200.0,
                ),
              ),
            ],
          ),
        ),
      ),
219 220 221 222 223 224 225 226 227 228 229 230 231
    );

    await tester.tap(find.text('CONTINUE'));
    await tester.tap(find.text('CANCEL'));

    expect(continuePressed, isTrue);
    expect(cancelPressed, isTrue);
  });

  testWidgets('Stepper disabled step test', (WidgetTester tester) async {
    int index = 0;

    await tester.pumpWidget(
232 233 234 235 236 237
      new MaterialApp(
        home: new Material(
          child: new Stepper(
            onStepTapped: (int i) {
              index = i;
            },
238
            steps: const <Step>[
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
              const Step(
                title: const Text('Step 1'),
                content: const SizedBox(
                  width: 100.0,
                  height: 100.0,
                ),
              ),
              const Step(
                title: const Text('Step 2'),
                state: StepState.disabled,
                content: const SizedBox(
                  width: 100.0,
                  height: 100.0,
                ),
              ),
            ],
          ),
        ),
      ),
258 259 260 261 262 263 264 265
    );

    await tester.tap(find.text('Step 2'));
    expect(index, 0);
  });

  testWidgets('Stepper scroll test', (WidgetTester tester) async {
    await tester.pumpWidget(
266 267 268
      new MaterialApp(
        home: new Material(
          child: new Stepper(
269
            steps: const <Step>[
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
              const Step(
                title: const Text('Step 1'),
                content: const SizedBox(
                  width: 100.0,
                  height: 300.0,
                ),
              ),
              const Step(
                title: const Text('Step 2'),
                content: const SizedBox(
                  width: 100.0,
                  height: 300.0,
                ),
              ),
              const Step(
                title: const Text('Step 3'),
                content: const SizedBox(
                  width: 100.0,
                  height: 100.0,
                ),
              ),
            ],
          ),
        ),
      ),
295 296
    );

297
    final ScrollableState scrollableState = tester.firstState(find.byType(Scrollable));
298
    expect(scrollableState.position.pixels, 0.0);
299 300 301

    await tester.tap(find.text('Step 3'));
    await tester.pumpWidget(
302 303
      new MaterialApp(
        home: new Material(
304
          child: new Stepper(
305
            currentStep: 2,
306
            steps: const <Step>[
307
              const Step(
308
                title: const Text('Step 1'),
309
                content: const SizedBox(
310
                  width: 100.0,
311 312
                  height: 300.0,
                ),
313
              ),
314
              const Step(
315
                title: const Text('Step 2'),
316
                content: const SizedBox(
317
                  width: 100.0,
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
                  height: 300.0,
                ),
              ),
              const Step(
                title: const Text('Step 3'),
                content: const SizedBox(
                  width: 100.0,
                  height: 100.0,
                ),
              ),
            ],
          ),
        ),
      ),
    );

    await tester.pump(const Duration(milliseconds: 100));
    expect(scrollableState.position.pixels, greaterThan(0.0));
  });

  testWidgets('Stepper index test', (WidgetTester tester) async {
    await tester.pumpWidget(
      new MaterialApp(
        home: new Center(
          child: new Material(
            child: new Stepper(
344
              steps: const <Step>[
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
                const Step(
                  title: const Text('A'),
                  state: StepState.complete,
                  content: const SizedBox(
                    width: 100.0,
                    height: 100.0,
                  ),
                ),
                const Step(
                  title: const Text('B'),
                  content: const SizedBox(
                    width: 100.0,
                    height: 100.0,
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
365 366 367 368 369 370 371 372
    );

    expect(find.text('1'), findsNothing);
    expect(find.text('2'), findsOneWidget);
  });

  testWidgets('Stepper error test', (WidgetTester tester) async {
    await tester.pumpWidget(
373 374 375 376
      new MaterialApp(
        home: new Center(
          child: new Material(
            child: new Stepper(
377
              steps: const <Step>[
378 379 380 381 382 383 384 385 386 387 388 389 390
                const Step(
                  title: const Text('A'),
                  state: StepState.error,
                  content: const SizedBox(
                    width: 100.0,
                    height: 100.0,
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
391 392 393 394 395
    );

    expect(find.text('!'), findsOneWidget);
  });
}