stepper_test.dart 9.21 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 49
                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'),
50
                content: const SizedBox(
51 52 53 54 55 56
                  width: 100.0,
                  height: 100.0
                )
              ),
              new Step(
                title: new Text('Step 2'),
57
                content: const SizedBox(
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
                  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'),
79
                content: const SizedBox(
80 81 82 83 84 85
                  width: 100.0,
                  height: 100.0
                )
              ),
              new Step(
                title: new Text('Step 2'),
86
                content: const SizedBox(
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 113
                  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'),
114
                content: const SizedBox(
115 116 117 118 119 120 121 122 123 124 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 190
                  width: 100.0,
                  height: 100.0
                )
              )
            ]
          )
        )
      )
    );

    RenderBox box = tester.renderObject(find.byType(Stepper));
    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'),
191
              content: const SizedBox(
192 193 194 195 196 197
                width: 100.0,
                height: 100.0
              )
            ),
            new Step(
              title: new Text('Step 2'),
198
              content: const SizedBox(
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 226
                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'),
227
              content: const SizedBox(
228 229 230 231 232 233 234
                width: 100.0,
                height: 100.0
              )
            ),
            new Step(
              title: new Text('Step 2'),
              state: StepState.disabled,
235
              content: const SizedBox(
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
                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'),
256
              content: const SizedBox(
257 258 259 260 261 262
                width: 100.0,
                height: 300.0
              )
            ),
            new Step(
              title: new Text('Step 2'),
263
              content: const SizedBox(
264 265 266 267 268 269
                width: 100.0,
                height: 300.0
              )
            ),
            new Step(
              title: new Text('Step 3'),
270
              content: const SizedBox(
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
                width: 100.0,
                height: 100.0
              )
            ),
          ]
        )
      )
    );

    ScrollableState scrollableState = tester.firstState(find.byType(Scrollable));
    expect(scrollableState.scrollOffset, 0.0);

    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'),
291
              content: const SizedBox(
292 293 294 295 296 297
                width: 100.0,
                height: 300.0
              )
            ),
            new Step(
              title: new Text('Step 2'),
298
              content: const SizedBox(
299 300 301 302 303 304
                width: 100.0,
                height: 300.0
              )
            ),
            new Step(
              title: new Text('Step 3'),
305
              content: const SizedBox(
306 307 308 309 310 311 312 313 314
                width: 100.0,
                height: 100.0
              )
            ),
          ]
        )
      )
    );

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

  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,
328
                content: const SizedBox(
329 330 331 332 333 334
                  width: 100.0,
                  height: 100.0
                )
              ),
              new Step(
                title: new Text('B'),
335
                content: const SizedBox(
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
                  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,
359
                content: const SizedBox(
360 361 362 363 364 365 366 367 368 369 370 371 372
                  width: 100.0,
                  height: 100.0
                )
              )
            ]
          )
        )
      )
    );

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