Skip to content

Commit

Permalink
[Slider] Changed clamping behaviour of the min setter to mutate the m…
Browse files Browse the repository at this point in the history
…ax instead of clamping to its value.

Summary: Same change applied to the max setter.

Reviewers: iangordon, O1 Material components iOS

Reviewed By: iangordon, O1 Material components iOS

Subscribers: ajsecord, iangordon

Tags: #material_components_ios

Differential Revision: http://codereview.cc/D1572
  • Loading branch information
randallli committed Sep 12, 2016
1 parent 1c60a2c commit f328856
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
12 changes: 8 additions & 4 deletions components/Slider/src/MDCSlider.h
Expand Up @@ -93,8 +93,10 @@ IB_DESIGNABLE
/**
The minimum value of the slider.
If you change the value of this property and the current value of the receiver is below the new
minimum, the current value will be adjusted to match the new minimum value.
If you change the value of this property and the @c value of the receiver is below the new minimum,
the current value will be adjusted to match the new minimum value.
If you change the value of this property and @c maximumValue of the receiver is below the new
minimum, the @c maximumValue will also be set to this new minimum value.
The default value of this property is 0.0.
*/
Expand All @@ -103,8 +105,10 @@ IB_DESIGNABLE
/**
The maximum value of the slider.
If you change the value of this property and the current value of the receiver is above the new
maximum, the current value will be adjusted to match the new maximum value.
If you change the value of this property and the @c value of the receiver is above the new maximum,
the current value will be adjusted to match the new maximum value.
If you change the value of this property and @c minimumValue of the receiver is above the new
maximum, the @c minimumValue will also be set to this new maximum value.
The default value of this property is 1.0.
*/
Expand Down
30 changes: 26 additions & 4 deletions components/Slider/tests/unit/SliderTests.m
Expand Up @@ -136,23 +136,45 @@ - (void)testSetMinimumToHigherThanValue {
- (void)testSetMaximumToLowerThanMinimum {
// Given
MDCSlider *slider = [[MDCSlider alloc] init];
CGFloat newMax = slider.minimumValue - [self randomNumber];

// When
slider.maximumValue = slider.minimumValue - [self randomNumber];
slider.maximumValue = newMax;

// Then
XCTAssertEqualWithAccuracy(slider.maximumValue, slider.minimumValue, kEpsilonAccuracy);
XCTAssertEqualWithAccuracy(slider.maximumValue, slider.minimumValue, kEpsilonAccuracy,
@"setting the slider's max to lower than the max must equal the min");
XCTAssertEqualWithAccuracy(
newMax, slider.minimumValue, kEpsilonAccuracy,
@"setting the slider's max must change the min when smaller than the min");
XCTAssertEqualWithAccuracy(
newMax, slider.maximumValue, kEpsilonAccuracy,
@"setting the slider's max must equal the value gotten even when smaller than the minimum");
XCTAssertEqualWithAccuracy(
newMax, slider.value, kEpsilonAccuracy,
@"setting the slider's min to lower than the value must change the value also");
}

- (void)testSetMinimumToLowerThanMaximum {
// Given
MDCSlider *slider = [[MDCSlider alloc] init];
CGFloat newMin = slider.maximumValue + [self randomNumber];

// When
slider.minimumValue = slider.maximumValue + [self randomNumber];
slider.minimumValue = newMin;

// Then
XCTAssertEqualWithAccuracy(slider.minimumValue, slider.maximumValue, kEpsilonAccuracy);
XCTAssertEqualWithAccuracy(slider.minimumValue, slider.maximumValue, kEpsilonAccuracy,
@"setting the slider's min to higher than the max must equal the max");
XCTAssertEqualWithAccuracy(
newMin, slider.minimumValue, kEpsilonAccuracy,
@"setting the slider's min must equal the value gotten even when larger than the maximum");
XCTAssertEqualWithAccuracy(
newMin, slider.maximumValue, kEpsilonAccuracy,
@"setting the slider's min to larger than the max must change the max also");
XCTAssertEqualWithAccuracy(
newMin, slider.value, kEpsilonAccuracy,
@"setting the slider's min to higher than the value must change the value also");
}

- (void)testDiscreteValues2 {
Expand Down
10 changes: 8 additions & 2 deletions components/private/ThumbTrack/src/MDCThumbTrack.m
Expand Up @@ -325,23 +325,29 @@ - (void)setShouldDisplayDiscreteValueLabel:(BOOL)shouldDisplayDiscreteValueLabel
}

- (void)setMinimumValue:(CGFloat)minimumValue {
_minimumValue = MIN(_maximumValue, minimumValue);
_minimumValue = minimumValue;
CGFloat previousValue = _value;
if (_value < _minimumValue) {
_value = _minimumValue;
}
if (_maximumValue < _minimumValue) {
_maximumValue = _minimumValue;
}
[self updateThumbTrackAnimated:NO
animateThumbAfterMove:NO
previousValue:previousValue
completion:NULL];
}

- (void)setMaximumValue:(CGFloat)maximumValue {
_maximumValue = MAX(_minimumValue, maximumValue);
_maximumValue = maximumValue;
CGFloat previousValue = _value;
if (_value > _maximumValue) {
_value = _maximumValue;
}
if (_minimumValue > _maximumValue) {
_minimumValue = _maximumValue;
}
[self updateThumbTrackAnimated:NO
animateThumbAfterMove:NO
previousValue:previousValue
Expand Down

0 comments on commit f328856

Please sign in to comment.