switch_test.dart 15.5 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 6

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

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 29 30 31 32 33 34 35 36 37 38 39 40
                  key: switchKey,
                  value: value,
                  onChanged: (bool newValue) {
                    setState(() {
                      value = newValue;
                    });
                  },
                ),
              ),
            );
          },
        ),
      ),
41 42 43 44 45 46
    );

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

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

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

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

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

88
  testWidgets('Switch can drag (LTR)', (WidgetTester tester) async {
89 90 91
    bool value = false;

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

    expect(value, isFalse);

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

    expect(value, isFalse);

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

    expect(value, isTrue);

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

    expect(value, isTrue);

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

    expect(value, isFalse);
  });
133 134 135 136 137

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

    await tester.pumpWidget(
138
      Directionality(
139
        textDirection: TextDirection.rtl,
140
        child: StatefulBuilder(
141
          builder: (BuildContext context, StateSetter setState) {
142 143 144
            return Material(
              child: Center(
                child: Switch(
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
                  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);
  });
177

178 179 180
  testWidgets('Switch has default colors when enabled', (WidgetTester tester) async {
    bool value = false;
    await tester.pumpWidget(
181
      Directionality(
182
        textDirection: TextDirection.rtl,
183
        child: StatefulBuilder(
184
          builder: (BuildContext context, StateSetter setState) {
185 186 187
            return Material(
              child: Center(
                child: Switch(
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
                  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
207
              rrect: RRect.fromLTRBR(
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
                  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),
223
              rrect: RRect.fromLTRBR(
224 225 226 227 228 229 230 231 232 233 234
                  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(
235
      Directionality(
236
        textDirection: TextDirection.rtl,
237
        child: StatefulBuilder(
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
          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,
257
            rrect: RRect.fromLTRBR(
258 259 260 261 262 263 264 265 266
                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(
267
      Directionality(
268
        textDirection: TextDirection.rtl,
269
        child: StatefulBuilder(
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
          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,
289
            rrect: RRect.fromLTRBR(
290 291 292 293 294 295 296 297 298
                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',
    );
  });

299 300 301
  testWidgets('Switch can be set color', (WidgetTester tester) async {
    bool value = false;
    await tester.pumpWidget(
302
      Directionality(
303
        textDirection: TextDirection.rtl,
304
        child: StatefulBuilder(
305
          builder: (BuildContext context, StateSetter setState) {
306 307 308
            return Material(
              child: Center(
                child: Switch(
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
                  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(
328 329 330 331
      Material.of(tester.element(find.byType(Switch))),
      paints
        ..rrect(
            color: Colors.blue[500],
332
            rrect: RRect.fromLTRBR(
333 334 335 336 337 338
                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.yellow[500])
    );
339 340 341 342
    await tester.drag(find.byType(Switch), const Offset(-30.0, 0.0));
    await tester.pump();

    expect(
343 344 345 346
      Material.of(tester.element(find.byType(Switch))),
      paints
        ..rrect(
            color: Colors.green[500],
347
            rrect: RRect.fromLTRBR(
348 349 350 351 352 353
                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.red[500])
    );
354
  });
355 356 357 358 359 360

  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(
361
      Directionality(
362
        textDirection: TextDirection.ltr,
363
        child: StatefulBuilder(
364
          builder: (BuildContext context, StateSetter setState) {
365 366 367
            return Material(
              child: Center(
                child: Switch(
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
                  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();
387
    await gesture.moveBy(Offset(switchRect.width, 0.0));
388 389 390 391 392 393 394 395
    await tester.pump();
    await gesture.up();
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 200));

    expect(value, isTrue);
    expect(tester.hasRunningAnimations, false);
  });
396 397 398 399

  testWidgets('switch has semantic events', (WidgetTester tester) async {
    dynamic semanticEvent;
    bool value = false;
400
    SystemChannels.accessibility.setMockMessageHandler((dynamic message) async {
401 402
      semanticEvent = message;
    });
403
    final SemanticsTester semanticsTester = SemanticsTester(tester);
404 405

    await tester.pumpWidget(
406
      Directionality(
407
        textDirection: TextDirection.ltr,
408
        child: StatefulBuilder(
409
          builder: (BuildContext context, StateSetter setState) {
410 411 412
            return Material(
              child: Center(
                child: Switch(
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
                  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);
  });
440 441 442 443

  testWidgets('switch sends semantic events from parent if fully merged', (WidgetTester tester) async {
    dynamic semanticEvent;
    bool value = false;
444
    SystemChannels.accessibility.setMockMessageHandler((dynamic message) async {
445 446
      semanticEvent = message;
    });
447
    final SemanticsTester semanticsTester = SemanticsTester(tester);
448 449

    await tester.pumpWidget(
450 451
      MaterialApp(
        home: StatefulBuilder(
452 453 454 455 456 457
          builder: (BuildContext context, StateSetter setState) {
            void onChanged(bool newValue) {
              setState(() {
                value = newValue;
              });
            }
458 459 460
            return Material(
              child: MergeSemantics(
                child: ListTile(
461 462 463 464
                  leading: const Text('test'),
                  onTap: () {
                    onChanged(!value);
                  },
465
                  trailing: Switch(
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
                    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);
  });
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 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

  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);

  });

532
}