-
Notifications
You must be signed in to change notification settings - Fork 944
/
MDCBottomSheetPresentationControllerPreferredSheetHeightTests.m
131 lines (102 loc) · 4.46 KB
/
MDCBottomSheetPresentationControllerPreferredSheetHeightTests.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// 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 "MaterialBottomSheet.h"
#import <UIKit/UIKit.h>
#import <XCTest/XCTest.h>
#import "../../src/private/MDCSheetContainerView.h"
// Exposing internal methods for unit testing
@interface MDCBottomSheetPresentationController (Testing)
@property(nonatomic, strong) MDCSheetContainerView *sheetView;
- (void)setPreferredSheetHeight:(CGFloat)preferredSheetHeight;
@end
/**
A testing double for @c MDCSheetContainerView that allows setting an explicitly-nonstandardized
@c frame value.
@note Although it is possible to retrieve a non-standardized frame or bounds from a UIView object,
UIView will standardize the CGRect passed to @c setFrame:. To aid in testing, we turn
@c frame into a simple get/set property. We still call up to the super implementation of
@c setFrame: in case there are side-effects in UIView.
*/
@interface FakeSheetView : MDCSheetContainerView
@end
@implementation FakeSheetView {
CGRect _frame;
}
- (CGRect)frame {
return _frame;
}
- (void)setFrame:(CGRect)frame {
[super setFrame:frame];
_frame = frame;
}
@end
@interface MDCBottomSheetPresentationControllerPreferredSheetHeightTests : XCTestCase
@property(nonatomic, strong) FakeSheetView *sheetView;
@property(nonatomic, strong) MDCBottomSheetPresentationController *presentationController;
@end
@implementation MDCBottomSheetPresentationControllerPreferredSheetHeightTests
- (void)setUp {
[super setUp];
// The `sheetView` property is both an input and an output to `setPreferredSheetHeight:`. Its
// frame may be used to guess the preferredContentHeight of the sheet. Once calculated, it
// receives an updated value for `preferredSheetHeight`.
self.sheetView = [[FakeSheetView alloc] initWithFrame:CGRectZero
contentView:[[UIView alloc] init]
scrollView:[[UIScrollView alloc] init]];
// Only used as a required `-init` parameters for MDCBottomSheetPresentationController
UIViewController *stubPresentingViewController = [[UIViewController alloc] init];
UIViewController *stubPresentedViewController = [[UIViewController alloc] init];
self.presentationController = [[MDCBottomSheetPresentationController alloc]
initWithPresentedViewController:stubPresentedViewController
presentingViewController:stubPresentingViewController];
self.presentationController.sheetView = self.sheetView;
}
- (void)testSetPreferredSheetHeightZeroWhenSheetViewHasStandardizedFrame {
// Given
CGFloat sheetFrameHeight = 80;
self.sheetView.frame = CGRectMake(0, 0, 75, sheetFrameHeight);
// When
[self.presentationController setPreferredSheetHeight:0];
// Then
XCTAssertEqualWithAccuracy(self.sheetView.preferredSheetHeight, sheetFrameHeight / 2, 0.001);
}
- (void)testSetPreferredSheetHeightZeroWhenSheetViewHasUnstandardizedFrame {
// Given
CGFloat sheetFrameHeight = -80;
self.sheetView.frame = CGRectMake(75, 80, -75, sheetFrameHeight);
// When
[self.presentationController setPreferredSheetHeight:0];
// Then
XCTAssertEqualWithAccuracy(self.sheetView.preferredSheetHeight,
(CGFloat)fabs(sheetFrameHeight / 2), 0.001);
}
- (void)testSetPreferredSheetHeightPositiveValue {
// Given
CGFloat preferredSheetHeight = 120;
self.sheetView.frame = CGRectMake(0, 0, 75, 80);
// When
[self.presentationController setPreferredSheetHeight:preferredSheetHeight];
// Then
XCTAssertEqualWithAccuracy(self.sheetView.preferredSheetHeight, preferredSheetHeight, 0.001);
}
- (void)testSetPreferredSheetHeightNegativeValue {
// Given
CGFloat preferredSheetHeight = -120;
self.sheetView.frame = CGRectMake(0, 0, 75, -80);
// When
[self.presentationController setPreferredSheetHeight:preferredSheetHeight];
// Then
XCTAssertEqualWithAccuracy(self.sheetView.preferredSheetHeight, preferredSheetHeight, 0.001);
}
@end