switch_test.dart 18.7 KB
Newer Older
1 2 3 4
// 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.

5
import 'package:flutter/cupertino.dart';
6
import 'package:flutter/material.dart';
7
import 'package:flutter/rendering.dart';
8
import 'package:flutter/services.dart';
9
import 'package:flutter_test/flutter_test.dart';
10
import 'package:flutter/gestures.dart';
11

12
import '../rendering/mock_canvas.dart';
13
import '../widgets/semantics_tester.dart';
14

15 16
void main() {
  testWidgets('Switch can toggle on tap', (WidgetTester tester) async {
17
    final Key switchKey = UniqueKey();
18 19 20
    bool value = false;

    await tester.pumpWidget(
21
      Directionality(
22
        textDirection: TextDirection.ltr,
23
        child: StatefulBuilder(
24
          builder: (BuildContext context, StateSetter setState) {
25 26 27
            return Material(
              child: Center(
                child: Switch(
28
                  dragStartBehavior: DragStartBehavior.down,
29 30 31 32 33 34 35 36 37 38 39 40 41
                  key: switchKey,
                  value: value,
                  onChanged: (bool newValue) {
                    setState(() {
                      value = newValue;
                    });
                  },
                ),
              ),
            );
          },
        ),
      ),
42 43 44 45 46 47
    );

    expect(value, isFalse);
    await tester.tap(find.byKey(switchKey));
    expect(value, isTrue);
  });
48

49 50
  testWidgets('Switch size is configurable by ThemeData.materialTapTargetSize', (WidgetTester tester) async {
    await tester.pumpWidget(
51 52 53
      Theme(
        data: ThemeData(materialTapTargetSize: MaterialTapTargetSize.padded),
        child: Directionality(
54
          textDirection: TextDirection.ltr,
55 56 57
          child: Material(
            child: Center(
              child: Switch(
58
                dragStartBehavior: DragStartBehavior.down,
59
                value: true,
60
                onChanged: (bool newValue) { },
61 62 63 64 65 66 67 68 69 70
              ),
            ),
          ),
        ),
      ),
    );

    expect(tester.getSize(find.byType(Switch)), const Size(59.0, 48.0));

    await tester.pumpWidget(
71 72 73
      Theme(
        data: ThemeData(materialTapTargetSize: MaterialTapTargetSize.shrinkWrap),
        child: Directionality(
74
          textDirection: TextDirection.ltr,
75 76 77
          child: Material(
            child: Center(
              child: Switch(
78
                dragStartBehavior: DragStartBehavior.down,
79
                value: true,
80
                onChanged: (bool newValue) { },
81 82 83 84 85 86 87 88 89 90
              ),
            ),
          ),
        ),
      ),
    );

    expect(tester.getSize(find.byType(Switch)), const Size(59.0, 40.0));
  });

91
  testWidgets('Switch can drag (LTR)', (WidgetTester tester) async {
92 93 94
    bool value = false;

    await tester.pumpWidget(
95
      Directionality(
96
        textDirection: TextDirection.ltr,
97
        child: StatefulBuilder(
98
          builder: (BuildContext context, StateSetter setState) {
99 100 101
            return Material(
              child: Center(
                child: Switch(
102
                  dragStartBehavior: DragStartBehavior.down,
103 104 105 106 107 108 109 110 111 112 113 114
                  value: value,
                  onChanged: (bool newValue) {
                    setState(() {
                      value = newValue;
                    });
                  },
                ),
              ),
            );
          },
        ),
      ),
115 116 117 118
    );

    expect(value, isFalse);

119
    await tester.drag(find.byType(Switch), const Offset(-30.0, 0.0));
120 121 122

    expect(value, isFalse);

123
    await tester.drag(find.byType(Switch), const Offset(30.0, 0.0));
124 125 126 127

    expect(value, isTrue);

    await tester.pump();
128
    await tester.drag(find.byType(Switch), const Offset(30.0, 0.0));
129 130 131 132

    expect(value, isTrue);

    await tester.pump();
133
    await tester.drag(find.byType(Switch), const Offset(-30.0, 0.0));
134 135 136

    expect(value, isFalse);
  });
137

138 139 140 141 142 143 144 145 146 147 148
  testWidgets('Switch can drag with dragStartBehavior', (WidgetTester tester) async {
    bool value = false;

    await tester.pumpWidget(
      Directionality(
        textDirection: TextDirection.ltr,
        child: StatefulBuilder(
          builder: (BuildContext context, StateSetter setState) {
            return Material(
              child: Center(
                child: Switch(
149 150 151 152 153 154
                  dragStartBehavior: DragStartBehavior.down,
                  value: value,
                  onChanged: (bool newValue) {
                    setState(() {
                      value = newValue;
                    });
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
                ),
              ),
            );
          },
        ),
      ),
    );

    expect(value, isFalse);
    await tester.drag(find.byType(Switch), const Offset(-30.0, 0.0));
    expect(value, isFalse);

    await tester.drag(find.byType(Switch), const Offset(30.0, 0.0));
    expect(value, isTrue);
    await tester.pump();
    await tester.drag(find.byType(Switch), const Offset(30.0, 0.0));
    expect(value, isTrue);
    await tester.pump();
    await tester.drag(find.byType(Switch), const Offset(-30.0, 0.0));
    expect(value, isFalse);

    await tester.pumpWidget(
      Directionality(
        textDirection: TextDirection.ltr,
        child: StatefulBuilder(
          builder: (BuildContext context, StateSetter setState) {
            return Material(
              child: Center(
                child: Switch(
                    dragStartBehavior: DragStartBehavior.start,
                    value: value,
                    onChanged: (bool newValue) {
                      setState(() {
                        value = newValue;
                      });
191
                    },
192 193 194 195 196 197 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
                ),
              ),
            );
          },
        ),
      ),
    );
    await tester.pumpAndSettle();
    final Rect switchRect = tester.getRect(find.byType(Switch));

    TestGesture gesture = await tester.startGesture(switchRect.center);
    // We have to execute the drag in two frames because the first update will
    // just set the start position.
    await gesture.moveBy(const Offset(20.0, 0.0));
    await gesture.moveBy(const Offset(20.0, 0.0));
    expect(value, isTrue);
    await gesture.up();
    await tester.pump();

    gesture = await tester.startGesture(switchRect.center);
    await gesture.moveBy(const Offset(20.0, 0.0));
    await gesture.moveBy(const Offset(20.0, 0.0));
    expect(value, isTrue);
    await gesture.up();
    await tester.pump();

    gesture = await tester.startGesture(switchRect.center);
    await gesture.moveBy(const Offset(-20.0, 0.0));
    await gesture.moveBy(const Offset(-20.0, 0.0));
    expect(value, isFalse);
  });

224 225 226 227
  testWidgets('Switch can drag (RTL)', (WidgetTester tester) async {
    bool value = false;

    await tester.pumpWidget(
228
      Directionality(
229
        textDirection: TextDirection.rtl,
230
        child: StatefulBuilder(
231
          builder: (BuildContext context, StateSetter setState) {
232 233 234
            return Material(
              child: Center(
                child: Switch(
235
                  dragStartBehavior: DragStartBehavior.down,
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
                  value: value,
                  onChanged: (bool newValue) {
                    setState(() {
                      value = newValue;
                    });
                  },
                ),
              ),
            );
          },
        ),
      ),
    );

    await tester.drag(find.byType(Switch), const Offset(30.0, 0.0));

    expect(value, isFalse);

    await tester.drag(find.byType(Switch), const Offset(-30.0, 0.0));

    expect(value, isTrue);

    await tester.pump();
    await tester.drag(find.byType(Switch), const Offset(-30.0, 0.0));

    expect(value, isTrue);

    await tester.pump();
    await tester.drag(find.byType(Switch), const Offset(30.0, 0.0));

    expect(value, isFalse);
  });
268

269 270 271
  testWidgets('Switch has default colors when enabled', (WidgetTester tester) async {
    bool value = false;
    await tester.pumpWidget(
272
      Directionality(
273
        textDirection: TextDirection.rtl,
274
        child: StatefulBuilder(
275
          builder: (BuildContext context, StateSetter setState) {
276 277 278
            return Material(
              child: Center(
                child: Switch(
279
                  dragStartBehavior: DragStartBehavior.down,
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
                  value: value,
                  onChanged: (bool newValue) {
                    setState(() {
                      value = newValue;
                    });
                  },
                ),
              ),
            );
          },
        ),
      ),
    );

    expect(
        Material.of(tester.element(find.byType(Switch))),
        paints
          ..rrect(
              color: const Color(0x52000000), // Black with 32% opacity
299
              rrect: RRect.fromLTRBR(
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
                  383.5, 293.0, 416.5, 307.0, const Radius.circular(7.0)))
          ..circle(color: const Color(0x33000000))
          ..circle(color: const Color(0x24000000))
          ..circle(color: const Color(0x1f000000))
          ..circle(color: Colors.grey.shade50),
      reason: 'Inactive enabled switch should match these colors',
    );
    await tester.drag(find.byType(Switch), const Offset(-30.0, 0.0));
    await tester.pump();

    expect(
        Material.of(tester.element(find.byType(Switch))),
        paints
          ..rrect(
              color: Colors.blue[600].withAlpha(0x80),
315
              rrect: RRect.fromLTRBR(
316 317 318 319 320 321 322 323 324 325 326
                  383.5, 293.0, 416.5, 307.0, const Radius.circular(7.0)))
          ..circle(color: const Color(0x33000000))
          ..circle(color: const Color(0x24000000))
          ..circle(color: const Color(0x1f000000))
          ..circle(color: Colors.blue[600]),
      reason: 'Active enabled switch should match these colors',
    );
  });

  testWidgets('Switch has default colors when disabled', (WidgetTester tester) async {
    await tester.pumpWidget(
327
      Directionality(
328
        textDirection: TextDirection.rtl,
329
        child: StatefulBuilder(
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
          builder: (BuildContext context, StateSetter setState) {
            return const Material(
              child: Center(
                child: Switch(
                  value: false,
                  onChanged: null,
                ),
              ),
            );
          },
        ),
      ),
    );

    expect(
      Material.of(tester.element(find.byType(Switch))),
      paints
        ..rrect(
            color: Colors.black12,
349
            rrect: RRect.fromLTRBR(
350 351 352 353 354 355 356 357 358
                383.5, 293.0, 416.5, 307.0, const Radius.circular(7.0)))
        ..circle(color: const Color(0x33000000))
        ..circle(color: const Color(0x24000000))
        ..circle(color: const Color(0x1f000000))
        ..circle(color: Colors.grey.shade400),
      reason: 'Inactive disabled switch should match these colors',
    );

    await tester.pumpWidget(
359
      Directionality(
360
        textDirection: TextDirection.rtl,
361
        child: StatefulBuilder(
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
          builder: (BuildContext context, StateSetter setState) {
            return const Material(
              child: Center(
                child: Switch(
                  value: true,
                  onChanged: null,
                ),
              ),
            );
          },
        ),
      ),
    );

    expect(
      Material.of(tester.element(find.byType(Switch))),
      paints
        ..rrect(
            color: Colors.black12,
381
            rrect: RRect.fromLTRBR(
382 383 384 385 386 387 388 389 390
                383.5, 293.0, 416.5, 307.0, const Radius.circular(7.0)))
        ..circle(color: const Color(0x33000000))
        ..circle(color: const Color(0x24000000))
        ..circle(color: const Color(0x1f000000))
        ..circle(color: Colors.grey.shade400),
      reason: 'Active disabled switch should match these colors',
    );
  });

391 392 393
  testWidgets('Switch can be set color', (WidgetTester tester) async {
    bool value = false;
    await tester.pumpWidget(
394
      Directionality(
395
        textDirection: TextDirection.rtl,
396
        child: StatefulBuilder(
397
          builder: (BuildContext context, StateSetter setState) {
398 399 400
            return Material(
              child: Center(
                child: Switch(
401
                  dragStartBehavior: DragStartBehavior.down,
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
                  value: value,
                  onChanged: (bool newValue) {
                    setState(() {
                      value = newValue;
                    });
                  },
                  activeColor: Colors.red[500],
                  activeTrackColor: Colors.green[500],
                  inactiveThumbColor: Colors.yellow[500],
                  inactiveTrackColor: Colors.blue[500],
                ),
              ),
            );
          },
        ),
      ),
    );

    expect(
421 422 423 424
      Material.of(tester.element(find.byType(Switch))),
      paints
        ..rrect(
            color: Colors.blue[500],
425
            rrect: RRect.fromLTRBR(
426 427 428 429
                383.5, 293.0, 416.5, 307.0, const Radius.circular(7.0)))
        ..circle(color: const Color(0x33000000))
        ..circle(color: const Color(0x24000000))
        ..circle(color: const Color(0x1f000000))
430
        ..circle(color: Colors.yellow[500]),
431
    );
432 433 434 435
    await tester.drag(find.byType(Switch), const Offset(-30.0, 0.0));
    await tester.pump();

    expect(
436 437 438 439
      Material.of(tester.element(find.byType(Switch))),
      paints
        ..rrect(
            color: Colors.green[500],
440
            rrect: RRect.fromLTRBR(
441 442 443 444
                383.5, 293.0, 416.5, 307.0, const Radius.circular(7.0)))
        ..circle(color: const Color(0x33000000))
        ..circle(color: const Color(0x24000000))
        ..circle(color: const Color(0x1f000000))
445
        ..circle(color: Colors.red[500]),
446
    );
447
  });
448 449 450 451 452 453

  testWidgets('Drag ends after animation completes', (WidgetTester tester) async {
    // Regression test for https://github.com/flutter/flutter/issues/17773

    bool value = false;
    await tester.pumpWidget(
454
      Directionality(
455
        textDirection: TextDirection.ltr,
456
        child: StatefulBuilder(
457
          builder: (BuildContext context, StateSetter setState) {
458 459 460
            return Material(
              child: Center(
                child: Switch(
461
                  dragStartBehavior: DragStartBehavior.down,
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
                  value: value,
                  onChanged: (bool newValue) {
                    setState(() {
                      value = newValue;
                    });
                  },
                ),
              ),
            );
          },
        ),
      ),
    );

    expect(value, isFalse);

    final Rect switchRect = tester.getRect(find.byType(Switch));
    final TestGesture gesture = await tester.startGesture(switchRect.centerLeft);
    await tester.pump();
481
    await gesture.moveBy(Offset(switchRect.width, 0.0));
482 483 484 485 486 487 488 489
    await tester.pump();
    await gesture.up();
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 200));

    expect(value, isTrue);
    expect(tester.hasRunningAnimations, false);
  });
490 491 492 493

  testWidgets('switch has semantic events', (WidgetTester tester) async {
    dynamic semanticEvent;
    bool value = false;
494
    SystemChannels.accessibility.setMockMessageHandler((dynamic message) async {
495 496
      semanticEvent = message;
    });
497
    final SemanticsTester semanticsTester = SemanticsTester(tester);
498 499

    await tester.pumpWidget(
500
      Directionality(
501
        textDirection: TextDirection.ltr,
502
        child: StatefulBuilder(
503
          builder: (BuildContext context, StateSetter setState) {
504 505 506
            return Material(
              child: Center(
                child: Switch(
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
                  value: value,
                  onChanged: (bool newValue) {
                    setState(() {
                      value = newValue;
                    });
                  },
                ),
              ),
            );
          },
        ),
      ),
    );
    await tester.tap(find.byType(Switch));
    final RenderObject object = tester.firstRenderObject(find.byType(Switch));

    expect(value, true);
    expect(semanticEvent, <String, dynamic>{
      'type': 'tap',
      'nodeId': object.debugSemantics.id,
      'data': <String, dynamic>{},
    });
    expect(object.debugSemantics.getSemanticsData().hasAction(SemanticsAction.tap), true);

    semanticsTester.dispose();
    SystemChannels.accessibility.setMockMessageHandler(null);
  });
534 535 536 537

  testWidgets('switch sends semantic events from parent if fully merged', (WidgetTester tester) async {
    dynamic semanticEvent;
    bool value = false;
538
    SystemChannels.accessibility.setMockMessageHandler((dynamic message) async {
539 540
      semanticEvent = message;
    });
541
    final SemanticsTester semanticsTester = SemanticsTester(tester);
542 543

    await tester.pumpWidget(
544 545
      MaterialApp(
        home: StatefulBuilder(
546 547 548 549 550 551
          builder: (BuildContext context, StateSetter setState) {
            void onChanged(bool newValue) {
              setState(() {
                value = newValue;
              });
            }
552 553 554
            return Material(
              child: MergeSemantics(
                child: ListTile(
555 556 557 558
                  leading: const Text('test'),
                  onTap: () {
                    onChanged(!value);
                  },
559
                  trailing: Switch(
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
                    value: value,
                    onChanged: onChanged,
                  ),
                ),
              ),
            );
          },
        ),
      ),
    );
    await tester.tap(find.byType(MergeSemantics));
    final RenderObject object = tester.firstRenderObject(find.byType(MergeSemantics));

    expect(value, true);
    expect(semanticEvent, <String, dynamic>{
      'type': 'tap',
      'nodeId': object.debugSemantics.id,
      'data': <String, dynamic>{},
    });
    expect(object.debugSemantics.getSemanticsData().hasAction(SemanticsAction.tap), true);

    semanticsTester.dispose();
    SystemChannels.accessibility.setMockMessageHandler(null);
  });
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625

  testWidgets('Switch.adaptive', (WidgetTester tester) async {
    bool value = false;

    Widget buildFrame(TargetPlatform platform) {
      return MaterialApp(
        theme: ThemeData(platform: platform),
        home: StatefulBuilder(
          builder: (BuildContext context, StateSetter setState) {
            return Material(
              child: Center(
                child: Switch.adaptive(
                  value: value,
                  onChanged: (bool newValue) {
                    setState(() {
                      value = newValue;
                    });
                  },
                ),
              ),
            );
          },
        ),
      );
    }

    await tester.pumpWidget(buildFrame(TargetPlatform.iOS));
    expect(find.byType(CupertinoSwitch), findsOneWidget);

    expect(value, isFalse);
    await tester.tap(find.byType(Switch));
    expect(value, isTrue);

    await tester.pumpWidget(buildFrame(TargetPlatform.android));
    await tester.pumpAndSettle(); // Finish the theme change animation.
    expect(find.byType(CupertinoSwitch), findsNothing);
    expect(value, isTrue);
    await tester.tap(find.byType(Switch));
    expect(value, isFalse);

  });

626
}