Fix segmented control golden tests (#18695)
This commit is contained in:
parent
def7634ba0
commit
5fbec7b812
@ -2,6 +2,8 @@
|
|||||||
// Use of this source code is governed by a BSD-style license that can be
|
// Use of this source code is governed by a BSD-style license that can be
|
||||||
// found in the LICENSE file.
|
// found in the LICENSE file.
|
||||||
|
|
||||||
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:flutter/widgets.dart';
|
import 'package:flutter/widgets.dart';
|
||||||
import 'package:flutter/cupertino.dart';
|
import 'package:flutter/cupertino.dart';
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
@ -94,7 +96,8 @@ void main() {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
fail('Should not be possible to create a segmented control with no children');
|
fail(
|
||||||
|
'Should not be possible to create a segmented control with no children');
|
||||||
} on AssertionError catch (e) {
|
} on AssertionError catch (e) {
|
||||||
expect(e.toString(), contains('children.length'));
|
expect(e.toString(), contains('children.length'));
|
||||||
}
|
}
|
||||||
@ -109,7 +112,8 @@ void main() {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
fail('Should not be possible to create a segmented control with just one child');
|
fail(
|
||||||
|
'Should not be possible to create a segmented control with just one child');
|
||||||
} on AssertionError catch (e) {
|
} on AssertionError catch (e) {
|
||||||
expect(e.toString(), contains('children.length'));
|
expect(e.toString(), contains('children.length'));
|
||||||
}
|
}
|
||||||
@ -138,7 +142,8 @@ void main() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
testWidgets('Children and onValueChanged can not be null', (WidgetTester tester) async {
|
testWidgets('Children and onValueChanged can not be null',
|
||||||
|
(WidgetTester tester) async {
|
||||||
try {
|
try {
|
||||||
await tester.pumpWidget(
|
await tester.pumpWidget(
|
||||||
boilerplate(
|
boilerplate(
|
||||||
@ -148,7 +153,8 @@ void main() {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
fail('Should not be possible to create segmented control with null children');
|
fail(
|
||||||
|
'Should not be possible to create segmented control with null children');
|
||||||
} on AssertionError catch (e) {
|
} on AssertionError catch (e) {
|
||||||
expect(e.toString(), contains('children'));
|
expect(e.toString(), contains('children'));
|
||||||
}
|
}
|
||||||
@ -166,13 +172,15 @@ void main() {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
fail('Should not be possible to create segmented control with null onValueChanged');
|
fail(
|
||||||
|
'Should not be possible to create segmented control with null onValueChanged');
|
||||||
} on AssertionError catch (e) {
|
} on AssertionError catch (e) {
|
||||||
expect(e.toString(), contains('onValueChanged'));
|
expect(e.toString(), contains('onValueChanged'));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
testWidgets('Widgets have correct default text/icon styles, change correctly on selection',
|
testWidgets(
|
||||||
|
'Widgets have correct default text/icon styles, change correctly on selection',
|
||||||
(WidgetTester tester) async {
|
(WidgetTester tester) async {
|
||||||
final Map<int, Widget> children = <int, Widget>{};
|
final Map<int, Widget> children = <int, Widget>{};
|
||||||
children[0] = const Text('Child 1');
|
children[0] = const Text('Child 1');
|
||||||
@ -198,8 +206,10 @@ void main() {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
DefaultTextStyle textStyle = tester.widget(find.widgetWithText(DefaultTextStyle, 'Child 1'));
|
DefaultTextStyle textStyle =
|
||||||
IconTheme iconTheme = tester.widget(find.widgetWithIcon(IconTheme, const IconData(1)));
|
tester.widget(find.widgetWithText(DefaultTextStyle, 'Child 1'));
|
||||||
|
IconTheme iconTheme =
|
||||||
|
tester.widget(find.widgetWithIcon(IconTheme, const IconData(1)));
|
||||||
|
|
||||||
expect(textStyle.style.color, CupertinoColors.white);
|
expect(textStyle.style.color, CupertinoColors.white);
|
||||||
expect(iconTheme.data.color, CupertinoColors.activeBlue);
|
expect(iconTheme.data.color, CupertinoColors.activeBlue);
|
||||||
@ -208,7 +218,8 @@ void main() {
|
|||||||
await tester.pump();
|
await tester.pump();
|
||||||
|
|
||||||
textStyle = tester.widget(find.widgetWithText(DefaultTextStyle, 'Child 1'));
|
textStyle = tester.widget(find.widgetWithText(DefaultTextStyle, 'Child 1'));
|
||||||
iconTheme = tester.widget(find.widgetWithIcon(IconTheme, const IconData(1)));
|
iconTheme =
|
||||||
|
tester.widget(find.widgetWithIcon(IconTheme, const IconData(1)));
|
||||||
|
|
||||||
expect(textStyle.style.color, CupertinoColors.activeBlue);
|
expect(textStyle.style.color, CupertinoColors.activeBlue);
|
||||||
expect(iconTheme.data.color, CupertinoColors.white);
|
expect(iconTheme.data.color, CupertinoColors.white);
|
||||||
@ -243,7 +254,8 @@ void main() {
|
|||||||
expect(value, isTrue);
|
expect(value, isTrue);
|
||||||
});
|
});
|
||||||
|
|
||||||
testWidgets('State does not change if onValueChanged does not call setState()',
|
testWidgets(
|
||||||
|
'State does not change if onValueChanged does not call setState()',
|
||||||
(WidgetTester tester) async {
|
(WidgetTester tester) async {
|
||||||
final Map<int, Widget> children = <int, Widget>{};
|
final Map<int, Widget> children = <int, Widget>{};
|
||||||
children[0] = const Text('Child 1');
|
children[0] = const Text('Child 1');
|
||||||
@ -265,7 +277,8 @@ void main() {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
final dynamic childList = getRenderSegmentedControl(tester).getChildrenAsList();
|
final dynamic childList =
|
||||||
|
getRenderSegmentedControl(tester).getChildrenAsList();
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
getRenderSegmentedControl(tester),
|
getRenderSegmentedControl(tester),
|
||||||
@ -314,7 +327,8 @@ void main() {
|
|||||||
'and should not change when tapped again', (WidgetTester tester) async {
|
'and should not change when tapped again', (WidgetTester tester) async {
|
||||||
await tester.pumpWidget(setupSimpleSegmentedControl());
|
await tester.pumpWidget(setupSimpleSegmentedControl());
|
||||||
|
|
||||||
final dynamic childList = getRenderSegmentedControl(tester).getChildrenAsList();
|
final dynamic childList =
|
||||||
|
getRenderSegmentedControl(tester).getChildrenAsList();
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
getRenderSegmentedControl(tester),
|
getRenderSegmentedControl(tester),
|
||||||
@ -389,12 +403,14 @@ void main() {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
testWidgets('Passed in value is child initially selected', (WidgetTester tester) async {
|
testWidgets('Passed in value is child initially selected',
|
||||||
|
(WidgetTester tester) async {
|
||||||
await tester.pumpWidget(setupSimpleSegmentedControl());
|
await tester.pumpWidget(setupSimpleSegmentedControl());
|
||||||
|
|
||||||
expect(getRenderSegmentedControl(tester).selectedIndex, 0);
|
expect(getRenderSegmentedControl(tester).selectedIndex, 0);
|
||||||
|
|
||||||
final dynamic childList = getRenderSegmentedControl(tester).getChildrenAsList();
|
final dynamic childList =
|
||||||
|
getRenderSegmentedControl(tester).getChildrenAsList();
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
getRenderSegmentedControl(tester),
|
getRenderSegmentedControl(tester),
|
||||||
@ -444,7 +460,8 @@ void main() {
|
|||||||
|
|
||||||
expect(getRenderSegmentedControl(tester).selectedIndex, null);
|
expect(getRenderSegmentedControl(tester).selectedIndex, null);
|
||||||
|
|
||||||
final dynamic childList = getRenderSegmentedControl(tester).getChildrenAsList();
|
final dynamic childList =
|
||||||
|
getRenderSegmentedControl(tester).getChildrenAsList();
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
getRenderSegmentedControl(tester),
|
getRenderSegmentedControl(tester),
|
||||||
@ -470,7 +487,8 @@ void main() {
|
|||||||
(WidgetTester tester) async {
|
(WidgetTester tester) async {
|
||||||
await tester.pumpWidget(setupSimpleSegmentedControl());
|
await tester.pumpWidget(setupSimpleSegmentedControl());
|
||||||
|
|
||||||
final dynamic childList = getRenderSegmentedControl(tester).getChildrenAsList();
|
final dynamic childList =
|
||||||
|
getRenderSegmentedControl(tester).getChildrenAsList();
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
getRenderSegmentedControl(tester),
|
getRenderSegmentedControl(tester),
|
||||||
@ -515,11 +533,13 @@ void main() {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
testWidgets('Long press does not change background color of currently-selected child',
|
testWidgets(
|
||||||
|
'Long press does not change background color of currently-selected child',
|
||||||
(WidgetTester tester) async {
|
(WidgetTester tester) async {
|
||||||
await tester.pumpWidget(setupSimpleSegmentedControl());
|
await tester.pumpWidget(setupSimpleSegmentedControl());
|
||||||
|
|
||||||
final dynamic childList = getRenderSegmentedControl(tester).getChildrenAsList();
|
final dynamic childList =
|
||||||
|
getRenderSegmentedControl(tester).getChildrenAsList();
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
getRenderSegmentedControl(tester),
|
getRenderSegmentedControl(tester),
|
||||||
@ -591,15 +611,16 @@ void main() {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
final RenderBox buttonBox = tester.renderObject(
|
final RenderBox buttonBox = tester
|
||||||
find.byKey(const ValueKey<String>('Segmented Control')));
|
.renderObject(find.byKey(const ValueKey<String>('Segmented Control')));
|
||||||
|
|
||||||
// Default height of Placeholder is 400.0px, which is greater than heights
|
// Default height of Placeholder is 400.0px, which is greater than heights
|
||||||
// of other child widgets.
|
// of other child widgets.
|
||||||
expect(buttonBox.size.height, 400.0);
|
expect(buttonBox.size.height, 400.0);
|
||||||
});
|
});
|
||||||
|
|
||||||
testWidgets('Width of each child widget is the same', (WidgetTester tester) async {
|
testWidgets('Width of each child widget is the same',
|
||||||
|
(WidgetTester tester) async {
|
||||||
final Map<int, Widget> children = <int, Widget>{};
|
final Map<int, Widget> children = <int, Widget>{};
|
||||||
children[0] = new Container();
|
children[0] = new Container();
|
||||||
children[1] = const Placeholder();
|
children[1] = const Placeholder();
|
||||||
@ -619,21 +640,23 @@ void main() {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
final RenderBox segmentedControl = tester.renderObject(
|
final RenderBox segmentedControl = tester
|
||||||
find.byKey(const ValueKey<String>('Segmented Control')));
|
.renderObject(find.byKey(const ValueKey<String>('Segmented Control')));
|
||||||
|
|
||||||
// Subtract the 16.0px from each side. Remaining width should be allocated
|
// Subtract the 16.0px from each side. Remaining width should be allocated
|
||||||
// to each child equally.
|
// to each child equally.
|
||||||
final double childWidth = (segmentedControl.size.width - 32.0) / 3;
|
final double childWidth = (segmentedControl.size.width - 32.0) / 3;
|
||||||
|
|
||||||
final dynamic childList = getRenderSegmentedControl(tester).getChildrenAsList();
|
final dynamic childList =
|
||||||
|
getRenderSegmentedControl(tester).getChildrenAsList();
|
||||||
|
|
||||||
for (dynamic child in childList) {
|
for (dynamic child in childList) {
|
||||||
expect(childWidth, child.parentData.surroundingRect.width);
|
expect(childWidth, child.parentData.surroundingRect.width);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
testWidgets('Width is finite in unbounded space', (WidgetTester tester) async {
|
testWidgets('Width is finite in unbounded space',
|
||||||
|
(WidgetTester tester) async {
|
||||||
final Map<int, Widget> children = <int, Widget>{};
|
final Map<int, Widget> children = <int, Widget>{};
|
||||||
children[0] = const Text('Child 1');
|
children[0] = const Text('Child 1');
|
||||||
children[1] = const Text('Child 2');
|
children[1] = const Text('Child 2');
|
||||||
@ -656,8 +679,8 @@ void main() {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
final RenderBox segmentedControl = tester.renderObject(
|
final RenderBox segmentedControl = tester
|
||||||
find.byKey(const ValueKey<String>('Segmented Control')));
|
.renderObject(find.byKey(const ValueKey<String>('Segmented Control')));
|
||||||
|
|
||||||
expect(segmentedControl.size.width.isFinite, isTrue);
|
expect(segmentedControl.size.width.isFinite, isTrue);
|
||||||
});
|
});
|
||||||
@ -680,8 +703,10 @@ void main() {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(tester.getTopRight(find.text('Child 1')).dx > tester.getTopRight(
|
expect(
|
||||||
find.text('Child 2')).dx, isTrue);
|
tester.getTopRight(find.text('Child 1')).dx >
|
||||||
|
tester.getTopRight(find.text('Child 2')).dx,
|
||||||
|
isTrue);
|
||||||
});
|
});
|
||||||
|
|
||||||
testWidgets('Correct initial selection and toggling behavior - RTL',
|
testWidgets('Correct initial selection and toggling behavior - RTL',
|
||||||
@ -713,7 +738,8 @@ void main() {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
final dynamic childList = getRenderSegmentedControl(tester).getChildrenAsList();
|
final dynamic childList =
|
||||||
|
getRenderSegmentedControl(tester).getChildrenAsList();
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
getRenderSegmentedControl(tester),
|
getRenderSegmentedControl(tester),
|
||||||
@ -869,6 +895,13 @@ void main() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
testWidgets('Golden Test Placeholder Widget', (WidgetTester tester) async {
|
testWidgets('Golden Test Placeholder Widget', (WidgetTester tester) async {
|
||||||
|
// Different machines render this content differently. Since the golden
|
||||||
|
// files are rendered on MacOS, this test should only be run on MacOS.
|
||||||
|
// If the golden files are regenerated on another OS, please change this
|
||||||
|
// test to only run on that OS.
|
||||||
|
if (Platform.isMacOS)
|
||||||
|
return;
|
||||||
|
|
||||||
final Map<int, Widget> children = <int, Widget>{};
|
final Map<int, Widget> children = <int, Widget>{};
|
||||||
children[0] = new Container();
|
children[0] = new Container();
|
||||||
children[1] = const Placeholder();
|
children[1] = const Placeholder();
|
||||||
@ -893,10 +926,20 @@ void main() {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
await expectLater(find.byType(RepaintBoundary), matchesGoldenFile('segmented_control_test.0.0.png'));
|
await expectLater(
|
||||||
|
find.byType(RepaintBoundary),
|
||||||
|
matchesGoldenFile('segmented_control_test.0.0.png'),
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
testWidgets('Golden Test Pressed State', (WidgetTester tester) async {
|
testWidgets('Golden Test Pressed State', (WidgetTester tester) async {
|
||||||
|
// Different machines render this content differently. Since the golden
|
||||||
|
// files are rendered on MacOS, this test should only be run on MacOS.
|
||||||
|
// If the golden files are regenerated on another OS, please change this
|
||||||
|
// test to only run on that OS.
|
||||||
|
if (!Platform.isMacOS)
|
||||||
|
return;
|
||||||
|
|
||||||
final Map<int, Widget> children = <int, Widget>{};
|
final Map<int, Widget> children = <int, Widget>{};
|
||||||
children[0] = const Text('A');
|
children[0] = const Text('A');
|
||||||
children[1] = const Text('B');
|
children[1] = const Text('B');
|
||||||
@ -924,7 +967,9 @@ void main() {
|
|||||||
final Offset center = tester.getCenter(find.text('B'));
|
final Offset center = tester.getCenter(find.text('B'));
|
||||||
await tester.startGesture(center);
|
await tester.startGesture(center);
|
||||||
await tester.pumpAndSettle();
|
await tester.pumpAndSettle();
|
||||||
|
await expectLater(
|
||||||
expect(find.byType(RepaintBoundary), matchesGoldenFile('segmented_control_test.1.0.png'));
|
find.byType(RepaintBoundary),
|
||||||
|
matchesGoldenFile('segmented_control_test.1.0.png'),
|
||||||
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user