blob: 4bb19d1303538b8342721491438611e1ac514b1a [file] [log] [blame] [edit]
/*
Copyright 2018-present the Material Components for iOS authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import <XCTest/XCTest.h>
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wprivate-header"
#import "MDCFlexibleHeaderMinMaxHeight.h"
#import "MDCFlexibleHeaderTopSafeArea.h"
#pragma clang diagnostic pop
#import "FlexibleHeaderTopSafeAreaTestsFakeTopSafeAreaDelegate.h"
NS_ASSUME_NONNULL_BEGIN
@interface FlexibleHeaderMinMaxHeightIncludesSafeAreaTests : XCTestCase
@end
@implementation FlexibleHeaderMinMaxHeightIncludesSafeAreaTests {
MDCFlexibleHeaderMinMaxHeight *_minMaxHeight;
MDCFlexibleHeaderTopSafeArea *_topSafeArea;
FlexibleHeaderTopSafeAreaTestsFakeTopSafeAreaDelegate *_delegate;
}
- (void)setUp {
[super setUp];
_topSafeArea = [[MDCFlexibleHeaderTopSafeArea alloc] init];
_delegate = [[FlexibleHeaderTopSafeAreaTestsFakeTopSafeAreaDelegate alloc] init];
_topSafeArea.topSafeAreaDelegate = _delegate;
}
- (void)tearDown {
_topSafeArea = nil;
_delegate = nil;
_minMaxHeight = nil;
[super tearDown];
}
#pragma mark - Defaults
- (void)testDefaults {
// Given
_delegate.deviceTopSafeAreaInset = 123;
// When
_minMaxHeight = [[MDCFlexibleHeaderMinMaxHeight alloc] initWithTopSafeArea:_topSafeArea];
_minMaxHeight.minMaxHeightIncludesSafeArea = YES;
// Then
XCTAssertEqualWithAccuracy(_minMaxHeight.minimumHeight, 56 + _delegate.deviceTopSafeAreaInset,
0.0001);
XCTAssertEqualWithAccuracy(_minMaxHeight.maximumHeight, 56 + _delegate.deviceTopSafeAreaInset,
0.0001);
XCTAssertEqualWithAccuracy(_minMaxHeight.minimumHeightWithTopSafeArea,
56 + _delegate.deviceTopSafeAreaInset, 0.0001);
XCTAssertEqualWithAccuracy(_minMaxHeight.maximumHeightWithTopSafeArea,
56 + _delegate.deviceTopSafeAreaInset, 0.0001);
XCTAssertEqualWithAccuracy(_minMaxHeight.maximumHeightWithoutTopSafeArea, 56, 0.0001);
}
#pragma mark - recalculateMinMaxHeight
- (void)testRecalculateMinMaxHeight {
// Given
_delegate.deviceTopSafeAreaInset = 123;
_minMaxHeight = [[MDCFlexibleHeaderMinMaxHeight alloc] initWithTopSafeArea:_topSafeArea];
_minMaxHeight.minMaxHeightIncludesSafeArea = YES;
// When
_delegate.deviceTopSafeAreaInset = 50;
[_minMaxHeight recalculateMinMaxHeight];
// Then
XCTAssertEqualWithAccuracy(_minMaxHeight.minimumHeight, 56 + _delegate.deviceTopSafeAreaInset,
0.0001);
XCTAssertEqualWithAccuracy(_minMaxHeight.maximumHeight, 56 + _delegate.deviceTopSafeAreaInset,
0.0001);
XCTAssertEqualWithAccuracy(_minMaxHeight.minimumHeightWithTopSafeArea,
56 + _delegate.deviceTopSafeAreaInset, 0.0001);
XCTAssertEqualWithAccuracy(_minMaxHeight.maximumHeightWithTopSafeArea,
56 + _delegate.deviceTopSafeAreaInset, 0.0001);
XCTAssertEqualWithAccuracy(_minMaxHeight.maximumHeightWithoutTopSafeArea, 56, 0.0001);
}
#pragma mark - Setting min/max stops automatic safe area adjustment
- (void)testSettingMinStopsAutomaticSafeAreaAdjustmentForMin {
// Given
_delegate.deviceTopSafeAreaInset = 123;
_minMaxHeight = [[MDCFlexibleHeaderMinMaxHeight alloc] initWithTopSafeArea:_topSafeArea];
_minMaxHeight.minMaxHeightIncludesSafeArea = YES;
// When
_minMaxHeight.minimumHeight = 30;
// Then
XCTAssertEqualWithAccuracy(_minMaxHeight.minimumHeight, 30, 0.0001);
XCTAssertEqualWithAccuracy(_minMaxHeight.maximumHeight, 56 + _delegate.deviceTopSafeAreaInset,
0.0001);
// MDCFlexibleHeaderMinMaxHeight is now assuming that min/max height include the safe area insets
// because minMaxHeightIncludesSafeArea is YES.
XCTAssertEqualWithAccuracy(_minMaxHeight.minimumHeightWithTopSafeArea, 30, 0.0001);
XCTAssertEqualWithAccuracy(_minMaxHeight.maximumHeightWithTopSafeArea,
56 + _delegate.deviceTopSafeAreaInset, 0.0001);
XCTAssertEqualWithAccuracy(_minMaxHeight.maximumHeightWithoutTopSafeArea, 56, 0.0001);
}
- (void)testSettingMaxStopsAutomaticSafeAreaAdjustmentForMax {
// Given
_delegate.deviceTopSafeAreaInset = 123;
_minMaxHeight = [[MDCFlexibleHeaderMinMaxHeight alloc] initWithTopSafeArea:_topSafeArea];
_minMaxHeight.minMaxHeightIncludesSafeArea = YES;
// When
_minMaxHeight.maximumHeight = 200;
// Then
XCTAssertEqualWithAccuracy(_minMaxHeight.minimumHeight, 56 + _delegate.deviceTopSafeAreaInset,
0.0001);
XCTAssertEqualWithAccuracy(_minMaxHeight.maximumHeight, 200, 0.0001);
// MDCFlexibleHeaderMinMaxHeight is now assuming that min/max height include the safe area insets
// because minMaxHeightIncludesSafeArea is YES.
XCTAssertEqualWithAccuracy(_minMaxHeight.minimumHeightWithTopSafeArea,
56 + _delegate.deviceTopSafeAreaInset, 0.0001);
XCTAssertEqualWithAccuracy(_minMaxHeight.maximumHeightWithTopSafeArea, 200, 0.0001);
XCTAssertEqualWithAccuracy(_minMaxHeight.maximumHeightWithoutTopSafeArea,
200 - _delegate.deviceTopSafeAreaInset, 0.0001);
}
#pragma mark - Side effects of min/max setters
- (void)testSettingMaxLessThanMinAdjustsMin {
// Given
_delegate.deviceTopSafeAreaInset = 123;
_minMaxHeight = [[MDCFlexibleHeaderMinMaxHeight alloc] initWithTopSafeArea:_topSafeArea];
_minMaxHeight.minMaxHeightIncludesSafeArea = YES;
// When
_minMaxHeight.minimumHeight = 50;
_minMaxHeight.maximumHeight = 30;
// Then
XCTAssertEqualWithAccuracy(_minMaxHeight.minimumHeight, 30, 0.0001);
XCTAssertEqualWithAccuracy(_minMaxHeight.maximumHeight, 30, 0.0001);
// MDCFlexibleHeaderMinMaxHeight is now assuming that min/max height include the safe area insets
// because minMaxHeightIncludesSafeArea is YES.
XCTAssertEqualWithAccuracy(_minMaxHeight.minimumHeightWithTopSafeArea, 30, 0.0001);
XCTAssertEqualWithAccuracy(_minMaxHeight.maximumHeightWithTopSafeArea, 30, 0.0001);
XCTAssertEqualWithAccuracy(_minMaxHeight.maximumHeightWithoutTopSafeArea,
30 - _delegate.deviceTopSafeAreaInset, 0.0001);
}
- (void)testSettingMinGreaterThanMaxAdjustsMax {
// Given
_delegate.deviceTopSafeAreaInset = 123;
_minMaxHeight = [[MDCFlexibleHeaderMinMaxHeight alloc] initWithTopSafeArea:_topSafeArea];
_minMaxHeight.minMaxHeightIncludesSafeArea = YES;
// When
_minMaxHeight.maximumHeight = 100;
_minMaxHeight.minimumHeight = 200;
// Then
XCTAssertEqualWithAccuracy(_minMaxHeight.minimumHeight, 200, 0.0001);
XCTAssertEqualWithAccuracy(_minMaxHeight.maximumHeight, 200, 0.0001);
// MDCFlexibleHeaderMinMaxHeight is now assuming that min/max height include the safe area insets
// because minMaxHeightIncludesSafeArea is YES.
XCTAssertEqualWithAccuracy(_minMaxHeight.minimumHeightWithTopSafeArea, 200, 0.0001);
XCTAssertEqualWithAccuracy(_minMaxHeight.maximumHeightWithTopSafeArea, 200, 0.0001);
XCTAssertEqualWithAccuracy(_minMaxHeight.maximumHeightWithoutTopSafeArea,
200 - _delegate.deviceTopSafeAreaInset, 0.0001);
}
@end
NS_ASSUME_NONNULL_END