// Copyright 2014 The Flutter 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/cupertino.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import '../rendering/mock_canvas.dart'; import '../widgets/semantics_tester.dart'; Widget wrap({ Widget child }) { return MediaQuery( data: const MediaQueryData(), child: Directionality( textDirection: TextDirection.ltr, child: Material(child: child), ), ); } void main() { testWidgets('SwitchListTile control test', (WidgetTester tester) async { final List<dynamic> log = <dynamic>[]; await tester.pumpWidget(wrap( child: SwitchListTile( value: true, onChanged: (bool value) { log.add(value); }, title: const Text('Hello'), ), )); await tester.tap(find.text('Hello')); log.add('-'); await tester.tap(find.byType(Switch)); expect(log, equals(<dynamic>[false, '-', false])); }); testWidgets('SwitchListTile control test', (WidgetTester tester) async { final SemanticsTester semantics = SemanticsTester(tester); await tester.pumpWidget(wrap( child: Column( children: <Widget>[ SwitchListTile( value: true, onChanged: (bool value) { }, title: const Text('AAA'), secondary: const Text('aaa'), ), CheckboxListTile( value: true, onChanged: (bool value) { }, title: const Text('BBB'), secondary: const Text('bbb'), ), RadioListTile<bool>( value: true, groupValue: false, onChanged: (bool value) { }, title: const Text('CCC'), secondary: const Text('ccc'), ), ], ), )); // This test verifies that the label and the control get merged. expect(semantics, hasSemantics(TestSemantics.root( children: <TestSemantics>[ TestSemantics.rootChild( id: 1, rect: const Rect.fromLTWH(0.0, 0.0, 800.0, 56.0), transform: null, flags: <SemanticsFlag>[ SemanticsFlag.hasEnabledState, SemanticsFlag.hasToggledState, SemanticsFlag.isEnabled, SemanticsFlag.isFocusable, SemanticsFlag.isToggled, ], actions: SemanticsAction.tap.index, label: 'aaa\nAAA', ), TestSemantics.rootChild( id: 3, rect: const Rect.fromLTWH(0.0, 0.0, 800.0, 56.0), transform: Matrix4.translationValues(0.0, 56.0, 0.0), flags: <SemanticsFlag>[ SemanticsFlag.hasCheckedState, SemanticsFlag.hasEnabledState, SemanticsFlag.isChecked, SemanticsFlag.isEnabled, SemanticsFlag.isFocusable, ], actions: SemanticsAction.tap.index, label: 'bbb\nBBB', ), TestSemantics.rootChild( id: 5, rect: const Rect.fromLTWH(0.0, 0.0, 800.0, 56.0), transform: Matrix4.translationValues(0.0, 112.0, 0.0), flags: <SemanticsFlag>[ SemanticsFlag.hasCheckedState, SemanticsFlag.hasEnabledState, SemanticsFlag.isEnabled, SemanticsFlag.isFocusable, SemanticsFlag.isInMutuallyExclusiveGroup, ], actions: SemanticsAction.tap.index, label: 'CCC\nccc', ), ], ))); semantics.dispose(); }); testWidgets('SwitchListTile has the right colors', (WidgetTester tester) async { bool value = false; await tester.pumpWidget( MediaQuery( data: const MediaQueryData(padding: EdgeInsets.all(8.0)), child: Directionality( textDirection: TextDirection.ltr, child: StatefulBuilder( builder: (BuildContext context, StateSetter setState) { return Material( child: SwitchListTile( 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( find.byType(Switch), paints ..rrect(color: Colors.blue[500]) ..circle(color: const Color(0x33000000)) ..circle(color: const Color(0x24000000)) ..circle(color: const Color(0x1f000000)) ..circle(color: Colors.yellow[500]), ); await tester.tap(find.byType(Switch)); await tester.pumpAndSettle(); expect( Material.of(tester.element(find.byType(Switch))), paints ..rrect(color: Colors.green[500]) ..circle(color: const Color(0x33000000)) ..circle(color: const Color(0x24000000)) ..circle(color: const Color(0x1f000000)) ..circle(color: Colors.red[500]), ); }); testWidgets('SwitchListTile.adaptive delegates to', (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: SwitchListTile.adaptive( value: value, onChanged: (bool newValue) { setState(() { value = newValue; }); }, ), ), ); }, ), ); } for (final TargetPlatform platform in <TargetPlatform>[ TargetPlatform.iOS, TargetPlatform.macOS ]) { value = false; await tester.pumpWidget(buildFrame(platform)); expect(find.byType(CupertinoSwitch), findsOneWidget); expect(value, isFalse, reason: 'on ${describeEnum(platform)}'); await tester.tap(find.byType(SwitchListTile)); expect(value, isTrue, reason: 'on ${describeEnum(platform)}'); } for (final TargetPlatform platform in <TargetPlatform>[ TargetPlatform.android, TargetPlatform.fuchsia, TargetPlatform.linux, TargetPlatform.windows ]) { value = false; await tester.pumpWidget(buildFrame(platform)); await tester.pumpAndSettle(); // Finish the theme change animation. expect(find.byType(CupertinoSwitch), findsNothing); expect(value, isFalse, reason: 'on ${describeEnum(platform)}'); await tester.tap(find.byType(SwitchListTile)); expect(value, isTrue, reason: 'on ${describeEnum(platform)}'); } }); testWidgets('SwitchListTile contentPadding', (WidgetTester tester) async { Widget buildFrame(TextDirection textDirection) { return MediaQuery( data: const MediaQueryData( padding: EdgeInsets.zero, textScaleFactor: 1.0, ), child: Directionality( textDirection: textDirection, child: Material( child: Container( alignment: Alignment.topLeft, child: SwitchListTile( contentPadding: const EdgeInsetsDirectional.only( start: 10.0, end: 20.0, top: 30.0, bottom: 40.0, ), secondary: const Text('L'), title: const Text('title'), value: true, onChanged: (bool selected) {}, ), ), ), ), ); } await tester.pumpWidget(buildFrame(TextDirection.ltr)); expect(tester.getTopLeft(find.text('L')).dx, 10.0); // contentPadding.start = 10 expect(tester.getTopRight(find.byType(Switch)).dx, 780.0); // 800 - contentPadding.end await tester.pumpWidget(buildFrame(TextDirection.rtl)); expect(tester.getTopLeft(find.byType(Switch)).dx, 20.0); // contentPadding.end = 20 expect(tester.getTopRight(find.text('L')).dx, 790.0); // 800 - contentPadding.start }); testWidgets('SwitchListTile can autofocus unless disabled.', (WidgetTester tester) async { final GlobalKey childKey = GlobalKey(); await tester.pumpWidget( MaterialApp( home: Material( child: ListView( children: <Widget>[ SwitchListTile( value: true, onChanged: (_) {}, title: Text('A', key: childKey), autofocus: true, ), ], ), ), ), ); await tester.pump(); expect(Focus.of(childKey.currentContext, nullOk: true).hasPrimaryFocus, isTrue); await tester.pumpWidget( MaterialApp( home: Material( child: ListView( children: <Widget>[ SwitchListTile( value: true, onChanged: null, title: Text('A', key: childKey), autofocus: true, ), ], ), ), ), ); await tester.pump(); expect(Focus.of(childKey.currentContext, nullOk: true).hasPrimaryFocus, isFalse); }); }