stepper_test.dart 9.23 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// 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(
      new Material(
        child: new Stepper(
          onStepTapped: (int i) {
            index = i;
          },
          steps: <Step>[
            new Step(
              title: new Text('Step 1'),
21
              content: const SizedBox(
22 23 24 25 26 27
                width: 100.0,
                height: 100.0
              )
            ),
            new Step(
              title: new Text('Step 2'),
28
              content: const SizedBox(
29 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 Center(
        child: new Material(
          child: new Stepper(
            steps: <Step>[
              new Step(
                title: new Text('Step 1'),
49
                content: const SizedBox(
50 51 52 53 54 55
                  width: 100.0,
                  height: 100.0
                )
              ),
              new Step(
                title: new Text('Step 2'),
56
                content: const SizedBox(
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
                  width: 200.0,
                  height: 200.0
                )
              )
            ]
          )
        )
      )
    );

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

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

    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(
      new Center(
        child: new Material(
          child: new Stepper(
            type: StepperType.horizontal,
            steps: <Step>[
              new Step(
                title: new Text('Step 1'),
113
                content: const SizedBox(
114 115 116 117 118 119 120 121 122 123
                  width: 100.0,
                  height: 100.0
                )
              )
            ]
          )
        )
      )
    );

124
    final RenderBox box = tester.renderObject(find.byType(Stepper));
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    expect(box.size.height, 600.0);
  });

  testWidgets('Stepper visibility test', (WidgetTester tester) async {
    await tester.pumpWidget(
      new Material(
        child: new Stepper(
          type: StepperType.horizontal,
          steps: <Step>[
            new Step(
              title: new Text('Step 1'),
              content: new Text('A')
            ),
            new Step(
              title: new Text('Step 2'),
              content: new Text('B')
            )
          ]
        )
      )
    );

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

    await tester.pumpWidget(
      new Material(
        child: new Stepper(
          currentStep: 1,
          type: StepperType.horizontal,
          steps: <Step>[
            new Step(
              title: new Text('Step 1'),
              content: new Text('A')
            ),
            new Step(
              title: new Text('Step 2'),
              content: new Text('B')
            )
          ]
        )
      )
    );

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

    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(
      new Material(
        child: new Stepper(
          onStepTapped: (int i) {
            index = i;
          },
          steps: <Step>[
            new Step(
              title: new Text('Step 1'),
226
              content: const SizedBox(
227 228 229 230 231 232 233
                width: 100.0,
                height: 100.0
              )
            ),
            new Step(
              title: new Text('Step 2'),
              state: StepState.disabled,
234
              content: const SizedBox(
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
                width: 100.0,
                height: 100.0
              )
            )
          ]
        )
      )
    );

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

  testWidgets('Stepper scroll test', (WidgetTester tester) async {
    await tester.pumpWidget(
      new Material(
        child: new Stepper(
          steps: <Step>[
            new Step(
              title: new Text('Step 1'),
255
              content: const SizedBox(
256 257 258 259 260 261
                width: 100.0,
                height: 300.0
              )
            ),
            new Step(
              title: new Text('Step 2'),
262
              content: const SizedBox(
263 264 265 266 267 268
                width: 100.0,
                height: 300.0
              )
            ),
            new Step(
              title: new Text('Step 3'),
269
              content: const SizedBox(
270 271 272 273 274 275 276 277 278
                width: 100.0,
                height: 100.0
              )
            ),
          ]
        )
      )
    );

279
    final ScrollableState scrollableState = tester.firstState(find.byType(Scrollable));
280
    expect(scrollableState.position.pixels, 0.0);
281 282 283 284 285 286 287 288 289

    await tester.tap(find.text('Step 3'));
    await tester.pumpWidget(
      new Material(
        child: new Stepper(
          currentStep: 2,
          steps: <Step>[
            new Step(
              title: new Text('Step 1'),
290
              content: const SizedBox(
291 292 293 294 295 296
                width: 100.0,
                height: 300.0
              )
            ),
            new Step(
              title: new Text('Step 2'),
297
              content: const SizedBox(
298 299 300 301 302 303
                width: 100.0,
                height: 300.0
              )
            ),
            new Step(
              title: new Text('Step 3'),
304
              content: const SizedBox(
305 306 307 308 309 310 311 312 313
                width: 100.0,
                height: 100.0
              )
            ),
          ]
        )
      )
    );

314
    await tester.pump(const Duration(milliseconds: 100));
315
    expect(scrollableState.position.pixels, greaterThan(0.0));
316
  });
317 318 319 320 321 322 323 324 325 326

  testWidgets('Stepper index test', (WidgetTester tester) async {
    await tester.pumpWidget(
      new Center(
        child: new Material(
          child: new Stepper(
            steps: <Step>[
              new Step(
                title: new Text('A'),
                state: StepState.complete,
327
                content: const SizedBox(
328 329 330 331 332 333
                  width: 100.0,
                  height: 100.0
                )
              ),
              new Step(
                title: new Text('B'),
334
                content: const SizedBox(
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
                  width: 100.0,
                  height: 100.0
                )
              )
            ]
          )
        )
      )
    );

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

  testWidgets('Stepper error test', (WidgetTester tester) async {
    await tester.pumpWidget(
      new Center(
        child: new Material(
          child: new Stepper(
            steps: <Step>[
              new Step(
                title: new Text('A'),
                state: StepState.error,
358
                content: const SizedBox(
359 360 361 362 363 364 365 366 367 368 369 370 371
                  width: 100.0,
                  height: 100.0
                )
              )
            ]
          )
        )
      )
    );

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