From 76de30edc84816d5133c32edca5b5b6835bd064e Mon Sep 17 00:00:00 2001 From: Jose Alba Date: Wed, 10 Jun 2020 15:10:02 -0400 Subject: [PATCH] Updated Slider test (#58630) --- packages/flutter/lib/src/material/slider.dart | 16 +- .../test/material/range_slider_test.dart | 121 ++++++++------- .../flutter/test/material/slider_test.dart | 141 +++++++++--------- .../test/material/slider_theme_test.dart | 16 +- 4 files changed, 156 insertions(+), 138 deletions(-) diff --git a/packages/flutter/lib/src/material/slider.dart b/packages/flutter/lib/src/material/slider.dart index acb4d05d307..e93434bc38e 100644 --- a/packages/flutter/lib/src/material/slider.dart +++ b/packages/flutter/lib/src/material/slider.dart @@ -1290,7 +1290,9 @@ class _RenderSlider extends RenderBox with RelayoutWhenSystemFontsChangeMixin { } } - void _handleDragStart(DragStartDetails details) => _startInteraction(details.globalPosition); + void _handleDragStart(DragStartDetails details) { + _startInteraction(details.globalPosition); + } void _handleDragUpdate(DragUpdateDetails details) { if (!_state.mounted) { @@ -1311,11 +1313,17 @@ class _RenderSlider extends RenderBox with RelayoutWhenSystemFontsChangeMixin { } } - void _handleDragEnd(DragEndDetails details) => _endInteraction(); + void _handleDragEnd(DragEndDetails details) { + _endInteraction(); + } - void _handleTapDown(TapDownDetails details) => _startInteraction(details.globalPosition); + void _handleTapDown(TapDownDetails details) { + _startInteraction(details.globalPosition); + } - void _handleTapUp(TapUpDetails details) => _endInteraction(); + void _handleTapUp(TapUpDetails details) { + _endInteraction(); + } @override bool hitTestSelf(Offset position) => true; diff --git a/packages/flutter/test/material/range_slider_test.dart b/packages/flutter/test/material/range_slider_test.dart index a92b64ec228..938d758cc63 100644 --- a/packages/flutter/test/material/range_slider_test.dart +++ b/packages/flutter/test/material/range_slider_test.dart @@ -1319,7 +1319,7 @@ void main() { await tester.pumpWidget(buildApp(divisions: 3)); - final RenderBox valueIndicatorBox = tester.firstRenderObject(find.byType(Overlay)); + final RenderBox valueIndicatorBox = tester.renderObject(find.byType(Overlay)); final Offset topRight = tester.getTopRight(find.byType(RangeSlider)).translate(-24, 0); final TestGesture gesture = await tester.startGesture(topRight); @@ -1330,18 +1330,16 @@ void main() { valueIndicatorBox, paints ..path(color: sliderTheme.valueIndicatorColor) - ..path(color: sliderTheme.valueIndicatorColor), + ..paragraph() ); await gesture.up(); // Wait for value indicator animation to finish. await tester.pumpAndSettle(); - }); testWidgets('Range Slider removes value indicator from overlay if Slider gets disposed without value indicator animation completing.', (WidgetTester tester) async { - final ThemeData theme = _buildTheme(); - final SliderThemeData sliderTheme = theme.sliderTheme; RangeValues values = const RangeValues(0.5, 0.75); + const Color fillColor = Color(0xf55f5f5f); Widget buildApp({ Color activeColor, @@ -1353,42 +1351,41 @@ void main() { values = newValues; }; return MaterialApp( - home: Directionality( - textDirection: TextDirection.ltr, - child: Material( - child: Navigator(onGenerateRoute: (RouteSettings settings) { - return MaterialPageRoute(builder: (BuildContext context) { - return Column( - children: [ - Theme( - data: theme, - child: RangeSlider( - values: values, - labels: RangeLabels(values.start.toStringAsFixed(2), - values.end.toStringAsFixed(2)), - divisions: divisions, - onChanged: onChanged, - ), + home: Scaffold( + // The builder is used to pass the context from the MaterialApp widget + // to the [Navigator]. This context is required in order for the + // Navigator to work. + body: Builder( + builder: (BuildContext context) { + return Column( + children: [ + RangeSlider( + values: values, + labels: RangeLabels( + values.start.toStringAsFixed(2), + values.end.toStringAsFixed(2), ), - RaisedButton( - child: const Text('Next'), - onPressed: () { - Navigator.of(context).pushReplacement( - MaterialPageRoute( - builder: (BuildContext context) { - return RaisedButton( - child: const Text('Inner page'), - onPressed: () => Navigator.of(context).pop(), - ); - }, - ), - ); - }, - ), - ], - ); - }); - }), + divisions: divisions, + onChanged: onChanged, + ), + RaisedButton( + child: const Text('Next'), + onPressed: () { + Navigator.of(context).pushReplacement( + MaterialPageRoute( + builder: (BuildContext context) { + return RaisedButton( + child: const Text('Inner page'), + onPressed: () { Navigator.of(context).pop(); }, + ); + }, + ), + ); + }, + ), + ], + ); + }, ), ), ); @@ -1396,24 +1393,29 @@ void main() { await tester.pumpWidget(buildApp(divisions: 3)); - /// The value indicator is added to the overlay when it is clicked or dragged. - /// Because both of these gestures are occurring then it adds same value indicator - /// twice into the overlay. - final RenderBox valueIndicatorBox = tester.firstRenderObject(find.byType(Overlay)); + final RenderObject valueIndicatorBox = tester.renderObject(find.byType(Overlay)); final Offset topRight = tester.getTopRight(find.byType(RangeSlider)).translate(-24, 0); final TestGesture gesture = await tester.startGesture(topRight); // Wait for value indicator animation to finish. await tester.pumpAndSettle(); - expect(find.byType(RangeSlider), isNotNull); expect( valueIndicatorBox, paints - ..rrect(color: sliderTheme.inactiveTrackColor) - ..rect(color: sliderTheme.activeTrackColor) - ..rrect(color: sliderTheme.inactiveTrackColor), + // Represents the raised button wth next text. + ..path(color: Colors.black) + ..paragraph() + // Represents the range slider. + ..path(color: fillColor) + ..paragraph() + ..path(color: fillColor) + ..paragraph(), ); + // Represents the Raised Button and Range Slider. + expect(valueIndicatorBox, paintsExactlyCountTimes(#drawPath, 3)); + expect(valueIndicatorBox, paintsExactlyCountTimes(#drawParagraph, 3)); + await tester.tap(find.text('Next')); await tester.pumpAndSettle(); @@ -1421,13 +1423,18 @@ void main() { expect( valueIndicatorBox, isNot( - paints - ..rrect(color: sliderTheme.inactiveTrackColor) - ..rect(color: sliderTheme.activeTrackColor) - ..rrect(color: sliderTheme.inactiveTrackColor) + paints + ..path(color: fillColor) + ..paragraph() + ..path(color: fillColor) + ..paragraph(), ), ); + // Represents the raised button with inner page text. + expect(valueIndicatorBox, paintsExactlyCountTimes(#drawPath, 1)); + expect(valueIndicatorBox, paintsExactlyCountTimes(#drawParagraph, 1)); + // Don't stop holding the value indicator. await gesture.up(); await tester.pumpAndSettle(); @@ -1548,7 +1555,7 @@ void main() { ), ); - final RenderBox valueIndicatorBox = tester.firstRenderObject(find.byType(Overlay)); + final RenderBox valueIndicatorBox = tester.renderObject(find.byType(Overlay)); // Get the bounds of the track by finding the slider edges and translating // inwards by the overlay radius. @@ -1572,8 +1579,7 @@ void main() { valueIndicatorBox, paints ..path(color: sliderTheme.valueIndicatorColor) - ..path(color: sliderTheme.overlappingShapeStrokeColor) - ..path(color: sliderTheme.valueIndicatorColor), + ..paragraph() ); await gesture.up(); @@ -1624,7 +1630,7 @@ void main() { ), ); - final RenderBox valueIndicatorBox = tester.firstRenderObject(find.byType(Overlay)); + final RenderBox valueIndicatorBox = tester.renderObject(find.byType(Overlay)); // Get the bounds of the track by finding the slider edges and translating // inwards by the overlay radius. @@ -1648,8 +1654,7 @@ void main() { valueIndicatorBox, paints ..path(color: sliderTheme.valueIndicatorColor) - ..path(color: sliderTheme.overlappingShapeStrokeColor) - ..path(color: sliderTheme.valueIndicatorColor), + ..paragraph() ); await gesture.up(); diff --git a/packages/flutter/test/material/slider_test.dart b/packages/flutter/test/material/slider_test.dart index 285c815f11e..46fac5013da 100644 --- a/packages/flutter/test/material/slider_test.dart +++ b/packages/flutter/test/material/slider_test.dart @@ -653,7 +653,7 @@ void main() { await tester.pumpWidget(buildApp()); final MaterialInkController material = Material.of(tester.element(find.byType(Slider))); - final RenderBox valueIndicatorBox = tester.firstRenderObject(find.byType(Overlay)); + final RenderBox valueIndicatorBox = tester.renderObject(find.byType(Overlay)); // Check default theme for enabled widget. expect(material, paints..rrect(color: sliderTheme.activeTrackColor)..rrect(color: sliderTheme.inactiveTrackColor)); @@ -769,16 +769,8 @@ void main() { expect( valueIndicatorBox, paints - ..rrect(color: sliderTheme.activeTrackColor) - ..rrect(color: sliderTheme.inactiveTrackColor) - ..circle(color: sliderTheme.overlayColor) - ..circle(color: sliderTheme.activeTickMarkColor) - ..circle(color: sliderTheme.activeTickMarkColor) - ..circle(color: sliderTheme.inactiveTickMarkColor) - ..circle(color: sliderTheme.inactiveTickMarkColor) - ..shadow(color: Colors.black) - ..circle(color: sliderTheme.thumbColor) - ..path(color: sliderTheme.valueIndicatorColor), + ..path(color: sliderTheme.valueIndicatorColor) + ..paragraph(), ); await gesture.up(); // Wait for value indicator animation to finish. @@ -1037,7 +1029,7 @@ void main() { await tester.pumpAndSettle(); expect( - tester.firstRenderObject(find.byType(Overlay)), + tester.renderObject(find.byType(Overlay)), paints ..path( includes: const [ @@ -1047,7 +1039,8 @@ void main() { Offset(-216.0, -16.0), ], color: const Color(0xf55f5f5f), - ), + ) + ..paragraph(), ); await gesture.up(); @@ -1059,7 +1052,7 @@ void main() { await tester.pumpAndSettle(); expect( - tester.firstRenderObject(find.byType(Overlay)), + tester.renderObject(find.byType(Overlay)), paints ..path( includes: const [ @@ -1069,7 +1062,8 @@ void main() { Offset(-216.0, -16.0), ], color: const Color(0xf55f5f5f), - ), + ) + ..paragraph(), ); await gesture.up(); @@ -1085,7 +1079,7 @@ void main() { gesture = await tester.startGesture(center); await tester.pumpAndSettle(); - expect(tester.firstRenderObject(find.byType(Overlay)), + expect(tester.renderObject(find.byType(Overlay)), paints ..path( includes: const [ @@ -1095,7 +1089,8 @@ void main() { Offset(-216.0, -16.0), ], color: const Color(0xf55f5f5f), - ), + ) + ..paragraph(), ); await gesture.up(); @@ -1111,7 +1106,7 @@ void main() { await tester.pumpAndSettle(); expect( - tester.firstRenderObject(find.byType(Overlay)), + tester.renderObject(find.byType(Overlay)), paints ..path( includes: const [ @@ -1121,7 +1116,8 @@ void main() { Offset(-216.0, -16.0), ], color: const Color(0xf55f5f5f), - ), + ) + ..paragraph(), ); await gesture.up(); @@ -1906,12 +1902,12 @@ void main() { await tester.pumpAndSettle(); - final RenderBox valueIndicatorBox = tester.firstRenderObject(find.byType(Overlay)); + final RenderBox valueIndicatorBox = tester.renderObject(find.byType(Overlay)); expect( valueIndicatorBox, isVisible - ? (paints..path(color: theme.valueIndicatorColor)) - : isNot(paints..path(color: theme.valueIndicatorColor)), + ? (paints..path(color: theme.valueIndicatorColor)..paragraph()) + : isNot(paints..path(color: theme.valueIndicatorColor)..paragraph()), ); await gesture.up(); } @@ -1989,6 +1985,7 @@ void main() { testWidgets('Slider removes value indicator from overlay if Slider gets disposed without value indicator animation completing.', (WidgetTester tester) async { final Key sliderKey = UniqueKey(); + const Color fillColor = Color(0xf55f5f5f); double value = 0.0; Widget buildApp({ @@ -1998,43 +1995,43 @@ void main() { bool enabled = true, }) { return MaterialApp( - home: Directionality( - textDirection: TextDirection.ltr, - child: Material( - child: Navigator(onGenerateRoute: (RouteSettings settings) { - return MaterialPageRoute(builder: (BuildContext context) { - return Column( - children: [ - Slider( - key: sliderKey, - min: 0.0, - max: 100.0, - divisions: divisions, - label: '${value.round()}', - value: value, - onChanged: (double newValue) { - value = newValue; - }, - ), - RaisedButton( - child: const Text('Next'), - onPressed: () { - Navigator.of(context).pushReplacement( - MaterialPageRoute( - builder: (BuildContext context) { - return RaisedButton( - child: const Text('Inner page'), - onPressed: () => Navigator.of(context).pop(), - ); - }, - ), - ); - }, - ), - ], - ); - }); - }), + home: Scaffold( + body: Builder( + // The builder is used to pass the context from the MaterialApp widget + // to the [Navigator]. This context is required in order for the + // Navigator to work. + builder: (BuildContext context) { + return Column( + children: [ + Slider( + key: sliderKey, + min: 0.0, + max: 100.0, + divisions: divisions, + label: '${value.round()}', + value: value, + onChanged: (double newValue) { + value = newValue; + }, + ), + RaisedButton( + child: const Text('Next'), + onPressed: () { + Navigator.of(context).pushReplacement( + MaterialPageRoute( + builder: (BuildContext context) { + return RaisedButton( + child: const Text('Inner page'), + onPressed: () { Navigator.of(context).pop(); }, + ); + }, + ), + ); + }, + ), + ], + ); + }, ), ), ); @@ -2042,10 +2039,7 @@ void main() { await tester.pumpWidget(buildApp(divisions: 3)); - /// The value indicator is added to the overlay when it is clicked or dragged. - /// Because both of these gestures are occurring then it adds same value indicator - /// twice into the overlay. - final RenderBox valueIndicatorBox = tester.firstRenderObject(find.byType(Overlay)); + final RenderObject valueIndicatorBox = tester.renderObject(find.byType(Overlay)); final Offset topRight = tester.getTopRight(find.byType(Slider)).translate(-24, 0); final TestGesture gesture = await tester.startGesture(topRight); // Wait for value indicator animation to finish. @@ -2055,10 +2049,17 @@ void main() { expect( valueIndicatorBox, paints - ..rrect(color: const Color(0xff2196f3)) // Active track. - ..rrect(color: const Color(0x3d2196f3)), // Inactive track. + // Represents the raised button with text, next. + ..path(color: Colors.black) + ..paragraph() + // Represents the Slider. + ..path(color: fillColor) + ..paragraph() ); + expect(valueIndicatorBox, paintsExactlyCountTimes(#drawPath, 2)); + expect(valueIndicatorBox, paintsExactlyCountTimes(#drawParagraph, 2)); + await tester.tap(find.text('Next')); await tester.pumpAndSettle(); @@ -2066,12 +2067,16 @@ void main() { expect( valueIndicatorBox, isNot( - paints - ..rrect(color: const Color(0xff2196f3)) // Active track. - ..rrect(color: const Color(0x3d2196f3)) // Inactive track. + paints + ..path(color: fillColor) + ..paragraph(), ), ); + // Represents the RaisedButton with inner Text, inner page. + expect(valueIndicatorBox, paintsExactlyCountTimes(#drawPath, 1)); + expect(valueIndicatorBox, paintsExactlyCountTimes(#drawParagraph, 1)); + // Don't stop holding the value indicator. await gesture.up(); await tester.pumpAndSettle(); diff --git a/packages/flutter/test/material/slider_theme_test.dart b/packages/flutter/test/material/slider_theme_test.dart index 7a2e26f9b05..6a675624775 100644 --- a/packages/flutter/test/material/slider_theme_test.dart +++ b/packages/flutter/test/material/slider_theme_test.dart @@ -401,7 +401,7 @@ void main() { await tester.pumpWidget(buildApp('1')); - final RenderBox valueIndicatorBox = tester.firstRenderObject(find.byType(Overlay)); + final RenderBox valueIndicatorBox = tester.renderObject(find.byType(Overlay)); Offset center = tester.getCenter(find.byType(Slider)); TestGesture gesture = await tester.startGesture(center); @@ -578,7 +578,7 @@ void main() { await tester.pumpWidget(buildApp('1')); - final RenderBox valueIndicatorBox = tester.firstRenderObject(find.byType(Overlay)); + final RenderBox valueIndicatorBox = tester.renderObject(find.byType(Overlay)); Offset center = tester.getCenter(find.byType(Slider)); TestGesture gesture = await tester.startGesture(center); @@ -989,7 +989,7 @@ void main() { )); final MaterialInkController material = Material.of(tester.element(find.byType(Slider))); - final RenderBox valueIndicatorBox = tester.firstRenderObject(find.byType(Overlay)); + final RenderBox valueIndicatorBox = tester.renderObject(find.byType(Overlay)); // Tap the center of the track and wait for animations to finish. final Offset center = tester.getCenter(find.byType(Slider)); @@ -1020,7 +1020,7 @@ void main() { )); final MaterialInkController material = Material.of(tester.element(find.byType(Slider))); - final RenderBox valueIndicatorBox = tester.firstRenderObject(find.byType(Overlay)); + final RenderBox valueIndicatorBox = tester.renderObject(find.byType(Overlay)); // Tap the center of the track to kick off the animation of the value indicator. final Offset center = tester.getCenter(find.byType(Slider)); @@ -1053,7 +1053,7 @@ void main() { divisions: 4, )); - final RenderBox valueIndicatorBox = tester.firstRenderObject(find.byType(Overlay)); + final RenderBox valueIndicatorBox = tester.renderObject(find.byType(Overlay)); // Tap the center of the track to kick off the animation of the value indicator. final Offset center = tester.getCenter(find.byType(Slider)); @@ -1084,7 +1084,7 @@ void main() { await tester.pumpWidget(_buildRangeApp(sliderTheme)); - final RenderBox valueIndicatorBox = tester.firstRenderObject(find.byType(Overlay)); + final RenderBox valueIndicatorBox = tester.renderObject(find.byType(Overlay)); final Offset center = tester.getCenter(find.byType(RangeSlider)); final TestGesture gesture = await tester.startGesture(center); @@ -1131,7 +1131,7 @@ void main() { )); // final RenderBox sliderBox = tester.firstRenderObject(find.byType(RangeSlider)); - final RenderBox valueIndicatorBox = tester.firstRenderObject(find.byType(Overlay)); + final RenderBox valueIndicatorBox = tester.renderObject(find.byType(Overlay)); // Tap the center of the track to kick off the animation of the value indicator. final Offset center = tester.getCenter(find.byType(RangeSlider)); @@ -1162,7 +1162,7 @@ void main() { divisions: 4, )); - final RenderBox valueIndicatorBox = tester.firstRenderObject(find.byType(Overlay)); + final RenderBox valueIndicatorBox = tester.renderObject(find.byType(Overlay)); // Tap the center of the track to kick off the animation of the value indicator. final Offset center = tester.getCenter(find.byType(RangeSlider));