Skip to content

Commit 5d6a6c8

Browse files
CGRectmaterial-automation
authored andcommitted
Rename SnackbarManagerTests to MDCSnackbarManagerTests with cleanup.
PiperOrigin-RevId: 684235249
1 parent 17067e8 commit 5d6a6c8

File tree

1 file changed

+272
-0
lines changed

1 file changed

+272
-0
lines changed
Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
// Copyright 2017-present the Material Components for iOS authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#import <XCTest/XCTest.h>
16+
17+
#pragma clang diagnostic push
18+
#pragma clang diagnostic ignored "-Wprivate-header"
19+
#import "MDCShadowElevations.h"
20+
#import "MDCSnackbarManager.h"
21+
#import "MDCSnackbarMessage.h"
22+
#import "MDCSnackbarMessageView.h"
23+
#import "MDCSnackbarManagerInternal.h"
24+
#import "MDCFakeMDCSnackbarManagerDelegate.h"
25+
26+
#pragma clang diagnostic push
27+
#pragma clang diagnostic ignored "-Wprivate-header"
28+
#pragma clang diagnostic pop
29+
30+
NS_ASSUME_NONNULL_BEGIN
31+
32+
@interface MDCSnackbarManagerInternal (MDCSnackbarManagerTesting)
33+
@property(nonatomic) MDCSnackbarMessageView *currentSnackbar;
34+
@end
35+
36+
@interface MDCSnackbarManager (MDCSnackbarManagerTesting)
37+
@property(nonnull, nonatomic, strong) MDCSnackbarManagerInternal *internalManager;
38+
@end
39+
40+
static NSString *const kTestFakeSnackbarCategory = @"Test Snackbar Category";
41+
static NSString *const kTestFakeSnackbarMessageText = @"Test Snackbar Message";
42+
static const CGFloat kTestFakeSnackbarMessageDuration = 10.0;
43+
static const CGFloat kTestFakeSnackbarMessageTimeoutDuration = 3.0;
44+
45+
@interface MDCSnackbarManagerTests : XCTestCase
46+
@property(nonatomic, strong, nullable) MDCSnackbarManager *managerUnderTest;
47+
@property(nonatomic, strong, nullable) FakeMDCSnackbarManagerDelegate *delegate;
48+
@property(nonatomic, strong, nullable) MDCSnackbarMessage *message;
49+
@end
50+
51+
@implementation MDCSnackbarManagerTests
52+
53+
- (void)setUp {
54+
[super setUp];
55+
56+
self.managerUnderTest = [[MDCSnackbarManager alloc] init];
57+
self.delegate = [[FakeMDCSnackbarManagerDelegate alloc] init];
58+
self.managerUnderTest.delegate = self.delegate;
59+
self.message = [MDCSnackbarMessage messageWithText:kTestFakeSnackbarMessageText];
60+
self.message.duration = kTestFakeSnackbarMessageDuration;
61+
}
62+
63+
- (void)tearDown {
64+
[self.managerUnderTest dismissAndCallCompletionBlocksWithCategory:nil];
65+
self.message = nil;
66+
self.managerUnderTest.delegate = nil;
67+
self.delegate = nil;
68+
self.managerUnderTest = nil;
69+
70+
[super tearDown];
71+
}
72+
73+
/** Tests that `hasMessagesShowingOrQueued` returns true after calling `showMessage`. */
74+
- (void)testHasMessagesShowingOrQueued {
75+
[self.managerUnderTest showMessage:self.message];
76+
XCTestExpectation *expectation = [self expectationWithDescription:@"has_shown_message"];
77+
78+
dispatch_async(dispatch_get_main_queue(), ^{
79+
XCTAssertTrue([self.managerUnderTest hasMessagesShowingOrQueued]);
80+
[expectation fulfill];
81+
});
82+
[self waitForExpectationsWithTimeout:kTestFakeSnackbarMessageTimeoutDuration handler:nil];
83+
}
84+
85+
- (void)testInstanceCreatedInBackgroundThread {
86+
XCTestExpectation *expect = [self expectationWithDescription:@""];
87+
88+
dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0), ^{
89+
MDCSnackbarManager *manager = [[MDCSnackbarManager alloc] init];
90+
(void)manager;
91+
[expect fulfill];
92+
});
93+
94+
[self waitForExpectations:@[ expect ] timeout:kTestFakeSnackbarMessageTimeoutDuration];
95+
}
96+
97+
/**
98+
* Tests that the default message elevation for a newly-initialized snackbar manager matches the
99+
* expected default value.
100+
*/
101+
- (void)testDefaultElevation {
102+
XCTAssertEqual([[MDCSnackbarManager alloc] init].messageElevation, MDCShadowElevationSnackbar);
103+
}
104+
105+
/** Tests that MDCSnackBarManager's elevation setter works as expected. */
106+
- (void)testCustomElevation {
107+
MDCSnackbarManager *manager = [[MDCSnackbarManager alloc] init];
108+
CGFloat fakeElevation = 10;
109+
110+
manager.messageElevation = fakeElevation;
111+
112+
XCTAssertEqual(manager.messageElevation, fakeElevation);
113+
}
114+
115+
/**
116+
* Tests that the trait collection passed into `traitCollectionDidChange` is updated when the
117+
* the trait collection for a snackbar message view changes.
118+
*/
119+
- (void)testTraitCollectionDidChangeCalledWhenTraitCollectionChanges {
120+
XCTestExpectation *expectation =
121+
[self expectationWithDescription:@"Called traitCollectionDidChange"];
122+
__block UITraitCollection *passedTraitCollection;
123+
__block MDCSnackbarMessageView *passedMessageView;
124+
self.managerUnderTest.traitCollectionDidChangeBlockForMessageView =
125+
^(MDCSnackbarMessageView *_Nonnull inMessageView,
126+
UITraitCollection *_Nullable previousTraitCollection) {
127+
passedMessageView = inMessageView;
128+
passedTraitCollection = previousTraitCollection;
129+
[expectation fulfill];
130+
};
131+
132+
[self.managerUnderTest showMessage:self.message];
133+
XCTestExpectation *mainQueueExpectation = [self expectationWithDescription:@"completed"];
134+
dispatch_async(dispatch_get_main_queue(), ^{
135+
[mainQueueExpectation fulfill];
136+
});
137+
[self waitForExpectations:@[ mainQueueExpectation ] timeout:1];
138+
UITraitCollection *testCollection = [UITraitCollection traitCollectionWithDisplayScale:77];
139+
MDCSnackbarMessageView *messageView = self.managerUnderTest.internalManager.currentSnackbar;
140+
[messageView traitCollectionDidChange:testCollection];
141+
142+
[self waitForExpectations:@[ expectation ] timeout:1];
143+
XCTAssertEqual(passedTraitCollection, testCollection);
144+
XCTAssertEqual(passedMessageView, messageView);
145+
}
146+
147+
/**
148+
* Tests that the custom message elevation value set on a snackbar manager is used by its message
149+
* view.
150+
*/
151+
- (void)testCurrentElevationMatchesElevationWhenElevationChanges {
152+
self.managerUnderTest.messageElevation = 4;
153+
154+
[self.managerUnderTest showMessage:self.message];
155+
XCTestExpectation *mainQueueExpectation = [self expectationWithDescription:@"completed"];
156+
dispatch_async(dispatch_get_main_queue(), ^{
157+
[mainQueueExpectation fulfill];
158+
});
159+
[self waitForExpectations:@[ mainQueueExpectation ] timeout:1];
160+
161+
MDCSnackbarMessageView *messageView = self.managerUnderTest.internalManager.currentSnackbar;
162+
XCTAssertEqualWithAccuracy(messageView.mdc_currentElevation, 4, 0.001);
163+
}
164+
165+
/**
166+
* Tests that overriding the base elevation for a snackbar manager configures its message view with
167+
* the new value.
168+
*/
169+
- (void)testSettingOverrideBaseElevationReturnsSetValue {
170+
CGFloat expectedBaseElevation = 99;
171+
172+
[self.managerUnderTest showMessage:self.message];
173+
XCTestExpectation *mainQueueExpectation = [self expectationWithDescription:@"completed"];
174+
dispatch_async(dispatch_get_main_queue(), ^{
175+
[mainQueueExpectation fulfill];
176+
});
177+
[self waitForExpectations:@[ mainQueueExpectation ] timeout:1];
178+
self.managerUnderTest.mdc_overrideBaseElevation = expectedBaseElevation;
179+
180+
MDCSnackbarMessageView *messageView = self.managerUnderTest.internalManager.currentSnackbar;
181+
XCTAssertEqualWithAccuracy(messageView.mdc_overrideBaseElevation, expectedBaseElevation, 0.001);
182+
}
183+
184+
/**
185+
* Tests that the elevationDidChange block is called when the snackbar manager's elevation property
186+
* is modified.
187+
*/
188+
- (void)testElevationDidChangeBlockCalledWhenElevationChangesValue {
189+
self.managerUnderTest.shouldApplyStyleChangesToVisibleSnackbars = YES;
190+
__block BOOL blockCalled = NO;
191+
192+
[self.managerUnderTest showMessage:self.message];
193+
XCTestExpectation *mainQueueExpectation = [self expectationWithDescription:@"completed"];
194+
dispatch_async(dispatch_get_main_queue(), ^{
195+
[mainQueueExpectation fulfill];
196+
});
197+
[self waitForExpectations:@[ mainQueueExpectation ] timeout:1];
198+
self.managerUnderTest.messageElevation = 5;
199+
self.managerUnderTest.mdc_elevationDidChangeBlockForMessageView =
200+
^(id<MDCElevatable> _, CGFloat elevation) {
201+
blockCalled = YES;
202+
};
203+
self.managerUnderTest.messageElevation = self.managerUnderTest.messageElevation + 1;
204+
205+
XCTAssertTrue(blockCalled);
206+
}
207+
208+
/**
209+
* Tests that the elevationDidChange block is not called when the snackbar manager's elevation
210+
* property is not modified.
211+
*/
212+
- (void)testElevationDidChangeBlockNotCalledWhenElevationIsSetWithoutChangingValue {
213+
self.managerUnderTest.shouldApplyStyleChangesToVisibleSnackbars = YES;
214+
__block BOOL blockCalled = NO;
215+
216+
[self.managerUnderTest showMessage:self.message];
217+
XCTestExpectation *mainQueueExpectation = [self expectationWithDescription:@"completed"];
218+
dispatch_async(dispatch_get_main_queue(), ^{
219+
[mainQueueExpectation fulfill];
220+
});
221+
[self waitForExpectations:@[ mainQueueExpectation ] timeout:1];
222+
self.managerUnderTest.messageElevation = 5;
223+
self.managerUnderTest.mdc_elevationDidChangeBlockForMessageView =
224+
^(id<MDCElevatable> _, CGFloat elevation) {
225+
blockCalled = YES;
226+
};
227+
self.managerUnderTest.messageElevation = self.managerUnderTest.messageElevation;
228+
229+
XCTAssertFalse(blockCalled);
230+
}
231+
232+
/**
233+
* Tests that the SnackbarManager returns true for `hasMessagesShowingOrQueued` after
234+
* `showMessage` is called while the manager is not in a suspended state.
235+
*/
236+
- (void)testShowMessage {
237+
__block BOOL result = NO;
238+
XCTestExpectation *expectation = [self expectationWithDescription:@"Snackbar state"];
239+
240+
[self.managerUnderTest showMessage:self.message];
241+
dispatch_async(dispatch_get_main_queue(), ^{
242+
result = [self.managerUnderTest hasMessagesShowingOrQueued];
243+
[expectation fulfill];
244+
});
245+
246+
[self waitForExpectations:@[ expectation ] timeout:kTestFakeSnackbarMessageTimeoutDuration];
247+
XCTAssertTrue(result);
248+
}
249+
250+
/**
251+
* Tests that a message with a category is dismissed and its completion handler is called after
252+
* calling `dismissAndCallCompletionBlocksWithCategory`.
253+
*/
254+
- (void)testSuspendAndResumeMessage {
255+
self.message.category = kTestFakeSnackbarCategory;
256+
XCTestExpectation *completionHandlerExpectation =
257+
[self expectationWithDescription:@"Completion Handler called"];
258+
self.message.completionHandler = ^(BOOL userInitiated) {
259+
[completionHandlerExpectation fulfill];
260+
};
261+
262+
[self.managerUnderTest showMessage:self.message];
263+
[self.managerUnderTest dismissAndCallCompletionBlocksWithCategory:kTestFakeSnackbarCategory];
264+
[self waitForExpectations:@[ completionHandlerExpectation ]
265+
timeout:kTestFakeSnackbarMessageTimeoutDuration];
266+
267+
XCTAssertFalse([self.managerUnderTest hasMessagesShowingOrQueued]);
268+
}
269+
270+
@end
271+
272+
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)