Skip to content

Commit 5e3b8f6

Browse files
committed
Make discrete slider regression tests fail without the fix
The previous tests tapped exactly on tick centers, where the old and new coordinate mappings differ by only a couple of pixels, so they passed even with the fix reverted. Tap just either side of each visual midpoint instead, test the default (Material 3) theme and the gapped track, cover the drag delta normalizer with a full-track drag, and add a RangeSlider test.
1 parent d3733d2 commit 5e3b8f6

2 files changed

Lines changed: 249 additions & 267 deletions

File tree

‎packages/material_ui/test/range_slider_test.dart‎

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4316,6 +4316,91 @@ void main() {
43164316

43174317
await gesture.up();
43184318
});
4319+
4320+
// Regression test for https://github.com/flutter/flutter/issues/184391.
4321+
//
4322+
// Tick marks on a discrete rounded track are painted with half a track height of
4323+
// padding at each end, so the snap boundary between two ticks has to sit at their
4324+
// visual midpoint. Tapping on the exact tick center can't tell the difference, so
4325+
// tap just on either side of the midpoint.
4326+
testWidgets('Discrete RangeSlider snaps at the visual midpoint between ticks', (
4327+
WidgetTester tester,
4328+
) async {
4329+
const divisions = 10;
4330+
var values = const RangeValues(0, 10);
4331+
final tickCenters = <Offset>[];
4332+
4333+
await tester.pumpWidget(
4334+
MaterialApp(
4335+
home: Material(
4336+
child: SliderTheme(
4337+
data: SliderThemeData(
4338+
rangeTickMarkShape: _PositionRecordingRangeTickMarkShape(tickCenters),
4339+
),
4340+
child: StatefulBuilder(
4341+
builder: (BuildContext context, StateSetter setState) {
4342+
return Center(
4343+
child: RangeSlider(
4344+
values: values,
4345+
max: 10,
4346+
divisions: divisions,
4347+
onChanged: (RangeValues newValues) => setState(() => values = newValues),
4348+
),
4349+
);
4350+
},
4351+
),
4352+
),
4353+
),
4354+
),
4355+
);
4356+
// Later repaints append to the list, so keep only the first frame's ticks.
4357+
final List<Offset> ticks = tickCenters.take(divisions + 1).toList();
4358+
expect(ticks, hasLength(divisions + 1));
4359+
4360+
// A small offset keeps the taps well clear of the exact midpoint while
4361+
// staying smaller than the smallest drift of the old formula.
4362+
const midpointOffset = 0.05;
4363+
double midpointDx(int i) => (ticks[i].dx + ticks[i + 1].dx) / 2;
4364+
4365+
// Taps in the lower half move the start thumb.
4366+
for (var i = 0; i < 4; i++) {
4367+
await tester.tapAt(Offset(midpointDx(i) - midpointOffset, ticks[i].dy));
4368+
await tester.pump();
4369+
expect(
4370+
values.start,
4371+
i.toDouble(),
4372+
reason: 'Just left of the midpoint of ticks $i and ${i + 1}',
4373+
);
4374+
4375+
await tester.tapAt(Offset(midpointDx(i) + midpointOffset, ticks[i].dy));
4376+
await tester.pump();
4377+
expect(
4378+
values.start,
4379+
(i + 1).toDouble(),
4380+
reason: 'Just right of the midpoint of ticks $i and ${i + 1}',
4381+
);
4382+
}
4383+
4384+
// Taps closer to the end thumb than the start thumb (which is now at 4) move
4385+
// the end thumb.
4386+
for (int i = divisions - 3; i < divisions; i++) {
4387+
await tester.tapAt(Offset(midpointDx(i) - midpointOffset, ticks[i].dy));
4388+
await tester.pump();
4389+
expect(
4390+
values.end,
4391+
i.toDouble(),
4392+
reason: 'Just left of the midpoint of ticks $i and ${i + 1}',
4393+
);
4394+
4395+
await tester.tapAt(Offset(midpointDx(i) + midpointOffset, ticks[i].dy));
4396+
await tester.pump();
4397+
expect(
4398+
values.end,
4399+
(i + 1).toDouble(),
4400+
reason: 'Just right of the midpoint of ticks $i and ${i + 1}',
4401+
);
4402+
}
4403+
});
43194404
}
43204405

43214406
// A value indicator shape to log labelPainter text.
@@ -4356,3 +4441,31 @@ class LoggingRangeSliderValueIndicatorShape extends RangeSliderValueIndicatorSha
43564441
logPainter?.add(labelPainter);
43574442
}
43584443
}
4444+
4445+
/// Records the center of each tick mark as it is painted, so tests can tap
4446+
/// relative to the real tick marks without duplicating the track geometry math.
4447+
class _PositionRecordingRangeTickMarkShape extends RangeSliderTickMarkShape {
4448+
_PositionRecordingRangeTickMarkShape(this.positions);
4449+
4450+
final List<Offset> positions;
4451+
4452+
@override
4453+
Size getPreferredSize({required SliderThemeData sliderTheme, bool isEnabled = false}) {
4454+
return Size.zero;
4455+
}
4456+
4457+
@override
4458+
void paint(
4459+
PaintingContext context,
4460+
Offset center, {
4461+
required RenderBox parentBox,
4462+
required SliderThemeData sliderTheme,
4463+
required Animation<double> enableAnimation,
4464+
required Offset startThumbCenter,
4465+
required Offset endThumbCenter,
4466+
bool isEnabled = false,
4467+
required TextDirection textDirection,
4468+
}) {
4469+
positions.add(center);
4470+
}
4471+
}

0 commit comments

Comments
 (0)