From 2e4122cc618aaf5441bf7f3d0f5c673f9d6e2360 Mon Sep 17 00:00:00 2001 From: Qun Cheng <36861262+QuncCccccc@users.noreply.github.com> Date: Fri, 30 Jun 2023 02:05:48 -0700 Subject: [PATCH] Update `SwitchListTile` tests for M2/M3 (#129809) Updated and reorganized unit tests for `SwitchListTile` to have M2 and M3 versions. More info in https://github.com/flutter/flutter/issues/127064 --- .../test/material/switch_list_tile_test.dart | 380 ++++++++++++++---- 1 file changed, 308 insertions(+), 72 deletions(-) diff --git a/packages/flutter/test/material/switch_list_tile_test.dart b/packages/flutter/test/material/switch_list_tile_test.dart index 818c8b8d25..e1ffc36376 100644 --- a/packages/flutter/test/material/switch_list_tile_test.dart +++ b/packages/flutter/test/material/switch_list_tile_test.dart @@ -116,18 +116,70 @@ void main() { semantics.dispose(); }); - testWidgets('SwitchListTile has the right colors', (WidgetTester tester) async { + testWidgets('Material2 - SwitchListTile has the right colors', (WidgetTester tester) async { bool value = false; - final ThemeData theme = ThemeData(); - final bool material3 = theme.useMaterial3; await tester.pumpWidget( MediaQuery( data: const MediaQueryData(padding: EdgeInsets.all(8.0)), child: Theme( - data: theme, + data: ThemeData(useMaterial3: false), child: Directionality( textDirection: TextDirection.ltr, - child: + 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]) + ..rrect(color: const Color(0x33000000)) + ..rrect(color: const Color(0x24000000)) + ..rrect(color: const Color(0x1f000000)) + ..rrect(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]) + ..rrect(color: const Color(0x33000000)) + ..rrect(color: const Color(0x24000000)) + ..rrect(color: const Color(0x1f000000)) + ..rrect(color: Colors.red[500]), + ); + }); + + testWidgets('Material3 - SwitchListTile has the right colors', (WidgetTester tester) async { + bool value = false; + await tester.pumpWidget( + MediaQuery( + data: const MediaQueryData(padding: EdgeInsets.all(8.0)), + child: Theme( + data: ThemeData(useMaterial3: true), + child: Directionality( + textDirection: TextDirection.ltr, + child: StatefulBuilder( builder: (BuildContext context, StateSetter setState) { return Material( @@ -151,17 +203,10 @@ void main() { expect( find.byType(Switch), - material3 - ? (paints - ..rrect(color: Colors.blue[500]) - ..rrect() - ..rrect(color: Colors.yellow[500])) - : (paints - ..rrect(color: Colors.blue[500]) - ..rrect(color: const Color(0x33000000)) - ..rrect(color: const Color(0x24000000)) - ..rrect(color: const Color(0x1f000000)) - ..rrect(color: Colors.yellow[500])), + paints + ..rrect(color: Colors.blue[500]) + ..rrect() + ..rrect(color: Colors.yellow[500]) ); await tester.tap(find.byType(Switch)); @@ -169,17 +214,10 @@ void main() { expect( Material.of(tester.element(find.byType(Switch))), - material3 - ? (paints - ..rrect(color: Colors.green[500]) - ..rrect() - ..rrect(color: Colors.red[500])) - : (paints - ..rrect(color: Colors.green[500]) - ..rrect(color: const Color(0x33000000)) - ..rrect(color: const Color(0x24000000)) - ..rrect(color: const Color(0x1f000000)) - ..rrect(color: Colors.red[500])), + paints + ..rrect(color: Colors.green[500]) + ..rrect() + ..rrect(color: Colors.red[500]) ); }); @@ -633,13 +671,11 @@ void main() { ); }); - testWidgets('SwitchListTile respects thumbColor in active/enabled states', (WidgetTester tester) async { + testWidgets('Material2 - SwitchListTile respects thumbColor in active/enabled states', (WidgetTester tester) async { const Color activeEnabledThumbColor = Color(0xFF000001); const Color activeDisabledThumbColor = Color(0xFF000002); const Color inactiveEnabledThumbColor = Color(0xFF000003); const Color inactiveDisabledThumbColor = Color(0xFF000004); - final ThemeData theme = ThemeData(); - final bool material3 = theme.useMaterial3; Color getThumbColor(Set states) { if (states.contains(MaterialState.disabled)) { @@ -657,8 +693,10 @@ void main() { final MaterialStateProperty thumbColor = MaterialStateColor.resolveWith(getThumbColor); Widget buildSwitchListTile({required bool enabled, required bool selected}) { - return wrap( - child: StatefulBuilder( + return MaterialApp( + theme: ThemeData(useMaterial3: false), + home: Material( + child: StatefulBuilder( builder: (BuildContext context, StateSetter setState) { return SwitchListTile( value: selected, @@ -666,6 +704,7 @@ void main() { onChanged: enabled ? (_) { } : null, ); }), + ), ); } @@ -673,18 +712,14 @@ void main() { await tester.pumpAndSettle(); expect( Material.of(tester.element(find.byType(Switch))), - material3 - ? (paints..rrect()..rrect()..rrect(color: inactiveDisabledThumbColor)) - : (paints..rrect()..rrect()..rrect()..rrect()..rrect(color: inactiveDisabledThumbColor)), + paints..rrect()..rrect()..rrect()..rrect()..rrect(color: inactiveDisabledThumbColor) ); await tester.pumpWidget(buildSwitchListTile(enabled: false, selected: true)); await tester.pumpAndSettle(); expect( Material.of(tester.element(find.byType(Switch))), - material3 - ? (paints..rrect()..rrect()..rrect(color: activeDisabledThumbColor)) - : (paints..rrect()..rrect()..rrect()..rrect()..rrect(color: activeDisabledThumbColor)), + paints..rrect()..rrect()..rrect()..rrect()..rrect(color: activeDisabledThumbColor) ); await tester.pumpWidget(buildSwitchListTile(enabled: true, selected: false)); @@ -692,9 +727,7 @@ void main() { expect( Material.of(tester.element(find.byType(Switch))), - material3 - ? (paints..rrect()..rrect()..rrect(color: inactiveEnabledThumbColor)) - : (paints..rrect()..rrect()..rrect()..rrect()..rrect(color: inactiveEnabledThumbColor)), + paints..rrect()..rrect()..rrect()..rrect()..rrect(color: inactiveEnabledThumbColor) ); await tester.pumpWidget(buildSwitchListTile(enabled: true, selected: true)); @@ -702,18 +735,82 @@ void main() { expect( Material.of(tester.element(find.byType(Switch))), - material3 - ? (paints..rrect()..rrect()..rrect(color: activeEnabledThumbColor)) - : (paints..rrect()..rrect()..rrect()..rrect()..rrect(color: activeEnabledThumbColor)), + paints..rrect()..rrect()..rrect()..rrect()..rrect(color: activeEnabledThumbColor) ); }); - testWidgets('SwitchListTile respects thumbColor in hovered/pressed states', (WidgetTester tester) async { + testWidgets('Material3 - SwitchListTile respects thumbColor in active/enabled states', (WidgetTester tester) async { + const Color activeEnabledThumbColor = Color(0xFF000001); + const Color activeDisabledThumbColor = Color(0xFF000002); + const Color inactiveEnabledThumbColor = Color(0xFF000003); + const Color inactiveDisabledThumbColor = Color(0xFF000004); + + Color getThumbColor(Set states) { + if (states.contains(MaterialState.disabled)) { + if (states.contains(MaterialState.selected)) { + return activeDisabledThumbColor; + } + return inactiveDisabledThumbColor; + } + if (states.contains(MaterialState.selected)) { + return activeEnabledThumbColor; + } + return inactiveEnabledThumbColor; + } + + final MaterialStateProperty thumbColor = MaterialStateColor.resolveWith(getThumbColor); + + Widget buildSwitchListTile({required bool enabled, required bool selected}) { + return MaterialApp( + theme: ThemeData(useMaterial3: true), + home: Material( + child: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return SwitchListTile( + value: selected, + thumbColor: thumbColor, + onChanged: enabled ? (_) { } : null, + ); + }), + ), + ); + } + + await tester.pumpWidget(buildSwitchListTile(enabled: false, selected: false)); + await tester.pumpAndSettle(); + expect( + Material.of(tester.element(find.byType(Switch))), + paints..rrect()..rrect()..rrect(color: inactiveDisabledThumbColor), + ); + + await tester.pumpWidget(buildSwitchListTile(enabled: false, selected: true)); + await tester.pumpAndSettle(); + expect( + Material.of(tester.element(find.byType(Switch))), + paints..rrect()..rrect()..rrect(color: activeDisabledThumbColor), + ); + + await tester.pumpWidget(buildSwitchListTile(enabled: true, selected: false)); + await tester.pumpAndSettle(); + + expect( + Material.of(tester.element(find.byType(Switch))), + paints..rrect()..rrect()..rrect(color: inactiveEnabledThumbColor), + ); + + await tester.pumpWidget(buildSwitchListTile(enabled: true, selected: true)); + await tester.pumpAndSettle(); + + expect( + Material.of(tester.element(find.byType(Switch))), + paints..rrect()..rrect()..rrect(color: activeEnabledThumbColor), + ); + }); + + testWidgets('Material2 - SwitchListTile respects thumbColor in hovered/pressed states', (WidgetTester tester) async { tester.binding.focusManager.highlightStrategy = FocusHighlightStrategy.alwaysTraditional; const Color hoveredThumbColor = Color(0xFF4caf50); const Color pressedThumbColor = Color(0xFFF44336); - final ThemeData theme = ThemeData(); - final bool material3 = theme.useMaterial3; Color getThumbColor(Set states) { if (states.contains(MaterialState.pressed)) { @@ -729,8 +826,8 @@ void main() { Widget buildSwitchListTile() { return MaterialApp( - theme: theme, - home: wrap( + theme: ThemeData(useMaterial3: false), + home: Material( child: StatefulBuilder( builder: (BuildContext context, StateSetter setState) { return SwitchListTile( @@ -754,9 +851,7 @@ void main() { expect( Material.of(tester.element(find.byType(Switch))), - material3 - ? (paints..rrect()..rrect()..rrect(color: hoveredThumbColor)) - : (paints..rrect()..rrect()..rrect()..rrect()..rrect(color: hoveredThumbColor)), + paints..rrect()..rrect()..rrect()..rrect()..rrect(color: hoveredThumbColor), ); // On pressed state @@ -764,9 +859,63 @@ void main() { await tester.pumpAndSettle(); expect( Material.of(tester.element(find.byType(Switch))), - material3 - ? (paints..rrect()..rrect()..rrect(color: pressedThumbColor)) - : (paints..rrect()..rrect()..rrect()..rrect()..rrect(color: pressedThumbColor)), + paints..rrect()..rrect()..rrect()..rrect()..rrect(color: pressedThumbColor), + ); + }); + + testWidgets('Material3 - SwitchListTile respects thumbColor in hovered/pressed states', (WidgetTester tester) async { + tester.binding.focusManager.highlightStrategy = FocusHighlightStrategy.alwaysTraditional; + const Color hoveredThumbColor = Color(0xFF4caf50); + const Color pressedThumbColor = Color(0xFFF44336); + + Color getThumbColor(Set states) { + if (states.contains(MaterialState.pressed)) { + return pressedThumbColor; + } + if (states.contains(MaterialState.hovered)) { + return hoveredThumbColor; + } + return Colors.transparent; + } + + final MaterialStateProperty thumbColor = MaterialStateColor.resolveWith(getThumbColor); + + Widget buildSwitchListTile() { + return MaterialApp( + theme: ThemeData(useMaterial3: true), + home: Material( + child: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return SwitchListTile( + value: false, + thumbColor: thumbColor, + onChanged: (_) { }, + ); + }), + ), + ); + } + + await tester.pumpWidget(buildSwitchListTile()); + await tester.pumpAndSettle(); + + // Start hovering + final TestGesture gesture = await tester.createGesture(kind: PointerDeviceKind.mouse); + await gesture.moveTo(tester.getCenter(find.byType(Switch))); + + await tester.pumpAndSettle(); + + expect( + Material.of(tester.element(find.byType(Switch))), + paints..rrect()..rrect()..rrect(color: hoveredThumbColor), + ); + + // On pressed state + await tester.press(find.byType(Switch)); + await tester.pumpAndSettle(); + expect( + Material.of(tester.element(find.byType(Switch))), + paints..rrect()..rrect()..rrect(color: pressedThumbColor), ); }); @@ -961,12 +1110,10 @@ void main() { ); }); - testWidgets('SwitchListTile respects materialTapTargetSize', (WidgetTester tester) async { - final ThemeData theme = ThemeData(); - final bool material3 = theme.useMaterial3; + testWidgets('Material2 - SwitchListTile respects materialTapTargetSize', (WidgetTester tester) async { Widget buildSwitchListTile(MaterialTapTargetSize materialTapTargetSize) { return MaterialApp( - theme: theme, + theme: ThemeData(useMaterial3: false), home: Material( child: StatefulBuilder( builder: (BuildContext context, StateSetter setState) { @@ -983,20 +1130,47 @@ void main() { await tester.pumpWidget(buildSwitchListTile(MaterialTapTargetSize.padded)); final Switch switchWidget = tester.widget(find.byType(Switch)); expect(switchWidget.materialTapTargetSize, MaterialTapTargetSize.padded); - expect(tester.getSize(find.byType(Switch)), material3 ? const Size(60.0, 48.0) : const Size(59.0, 48.0)); + expect(tester.getSize(find.byType(Switch)), const Size(59.0, 48.0)); await tester.pumpWidget(buildSwitchListTile(MaterialTapTargetSize.shrinkWrap)); final Switch switchWidget1 = tester.widget(find.byType(Switch)); expect(switchWidget1.materialTapTargetSize, MaterialTapTargetSize.shrinkWrap); - expect(tester.getSize(find.byType(Switch)), material3 ? const Size(60.0, 40.0) : const Size(59.0, 40.0)); + expect(tester.getSize(find.byType(Switch)), const Size(59.0, 40.0)); }); - testWidgets('SwitchListTile.adaptive respects applyCupertinoTheme', (WidgetTester tester) async { - final ThemeData theme = ThemeData(); + testWidgets('Material3 - SwitchListTile respects materialTapTargetSize', (WidgetTester tester) async { + Widget buildSwitchListTile(MaterialTapTargetSize materialTapTargetSize) { + return MaterialApp( + theme: ThemeData(useMaterial3: true), + home: Material( + child: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return SwitchListTile( + materialTapTargetSize: materialTapTargetSize, + value: false, + onChanged: (_) {}, + ); + }), + ), + ); + } + + await tester.pumpWidget(buildSwitchListTile(MaterialTapTargetSize.padded)); + final Switch switchWidget = tester.widget(find.byType(Switch)); + expect(switchWidget.materialTapTargetSize, MaterialTapTargetSize.padded); + expect(tester.getSize(find.byType(Switch)), const Size(60.0, 48.0)); + + await tester.pumpWidget(buildSwitchListTile(MaterialTapTargetSize.shrinkWrap)); + final Switch switchWidget1 = tester.widget(find.byType(Switch)); + expect(switchWidget1.materialTapTargetSize, MaterialTapTargetSize.shrinkWrap); + expect(tester.getSize(find.byType(Switch)), const Size(60.0, 40.0)); + }); + + testWidgets('Material2 - SwitchListTile.adaptive respects applyCupertinoTheme', (WidgetTester tester) async { Widget buildSwitchListTile(bool applyCupertinoTheme, TargetPlatform platform) { return MaterialApp( - theme: theme.copyWith(platform: platform), - home: wrap( + theme: ThemeData(useMaterial3: false, platform: platform), + home: Material( child: StatefulBuilder( builder: (BuildContext context, StateSetter setState) { return SwitchListTile.adaptive( @@ -1015,7 +1189,7 @@ void main() { expect(find.byType(CupertinoSwitch), findsOneWidget); expect( Material.of(tester.element(find.byType(Switch))), - paints..rrect(color: theme.useMaterial3 ? const Color(0xFF6750A4) : const Color(0xFF2196F3)), + paints..rrect(color: const Color(0xFF2196F3)), ); await tester.pumpWidget(buildSwitchListTile(false, platform)); @@ -1028,12 +1202,46 @@ void main() { } }); - testWidgets('SwitchListTile respects materialTapTargetSize', (WidgetTester tester) async { - final ThemeData theme = ThemeData(); - final bool material3 = theme.useMaterial3; + testWidgets('Material3 - SwitchListTile.adaptive respects applyCupertinoTheme', (WidgetTester tester) async { + Widget buildSwitchListTile(bool applyCupertinoTheme, TargetPlatform platform) { + return MaterialApp( + theme: ThemeData(useMaterial3: true, platform: platform), + home: Material( + child: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return SwitchListTile.adaptive( + applyCupertinoTheme: applyCupertinoTheme, + value: true, + onChanged: (_) {}, + ); + }), + ), + ); + } + + for (final TargetPlatform platform in [ TargetPlatform.iOS, TargetPlatform.macOS ]) { + await tester.pumpWidget(buildSwitchListTile(true, platform)); + await tester.pumpAndSettle(); + expect(find.byType(CupertinoSwitch), findsOneWidget); + expect( + Material.of(tester.element(find.byType(Switch))), + paints..rrect(color: const Color(0xFF6750A4)), + ); + + await tester.pumpWidget(buildSwitchListTile(false, platform)); + await tester.pumpAndSettle(); + expect(find.byType(CupertinoSwitch), findsOneWidget); + expect( + Material.of(tester.element(find.byType(Switch))), + paints..rrect(color: const Color(0xFF34C759)), + ); + } + }); + + testWidgets('Material2 - SwitchListTile respects materialTapTargetSize', (WidgetTester tester) async { Widget buildSwitchListTile(MaterialTapTargetSize materialTapTargetSize) { return MaterialApp( - theme: theme, + theme: ThemeData(useMaterial3: false), home: Material( child: StatefulBuilder( builder: (BuildContext context, StateSetter setState) { @@ -1050,12 +1258,40 @@ void main() { await tester.pumpWidget(buildSwitchListTile(MaterialTapTargetSize.padded)); final Switch switchWidget = tester.widget(find.byType(Switch)); expect(switchWidget.materialTapTargetSize, MaterialTapTargetSize.padded); - expect(tester.getSize(find.byType(Switch)), material3 ? const Size(60.0, 48.0) : const Size(59.0, 48.0)); + expect(tester.getSize(find.byType(Switch)), const Size(59.0, 48.0)); await tester.pumpWidget(buildSwitchListTile(MaterialTapTargetSize.shrinkWrap)); final Switch switchWidget1 = tester.widget(find.byType(Switch)); expect(switchWidget1.materialTapTargetSize, MaterialTapTargetSize.shrinkWrap); - expect(tester.getSize(find.byType(Switch)), material3 ? const Size(60.0, 40.0) : const Size(59.0, 40.0)); + expect(tester.getSize(find.byType(Switch)), const Size(59.0, 40.0)); + }); + + testWidgets('Material3 - SwitchListTile respects materialTapTargetSize', (WidgetTester tester) async { + Widget buildSwitchListTile(MaterialTapTargetSize materialTapTargetSize) { + return MaterialApp( + theme: ThemeData(useMaterial3: true), + home: Material( + child: StatefulBuilder( + builder: (BuildContext context, StateSetter setState) { + return SwitchListTile( + materialTapTargetSize: materialTapTargetSize, + value: false, + onChanged: (_) {}, + ); + }), + ), + ); + } + + await tester.pumpWidget(buildSwitchListTile(MaterialTapTargetSize.padded)); + final Switch switchWidget = tester.widget(find.byType(Switch)); + expect(switchWidget.materialTapTargetSize, MaterialTapTargetSize.padded); + expect(tester.getSize(find.byType(Switch)), const Size(60.0, 48.0)); + + await tester.pumpWidget(buildSwitchListTile(MaterialTapTargetSize.shrinkWrap)); + final Switch switchWidget1 = tester.widget(find.byType(Switch)); + expect(switchWidget1.materialTapTargetSize, MaterialTapTargetSize.shrinkWrap); + expect(tester.getSize(find.byType(Switch)), const Size(60.0, 40.0)); }); testWidgets('SwitchListTile passes the value of dragStartBehavior to Switch', (WidgetTester tester) async {