-
Notifications
You must be signed in to change notification settings - Fork 944
/
MDCTabBarViewTests.m
147 lines (112 loc) · 4.4 KB
/
MDCTabBarViewTests.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2019-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>
#import "MDCTabBarView.h"
@interface MDCTabBarViewTests : XCTestCase
@property(nonatomic, strong) MDCTabBarView *tabBarView;
@property(nonatomic, strong) UITabBarItem *itemA;
@property(nonatomic, strong) UITabBarItem *itemB;
@property(nonatomic, strong) UITabBarItem *itemC;
@end
@implementation MDCTabBarViewTests
- (void)setUp {
[super setUp];
self.tabBarView = [[MDCTabBarView alloc] init];
self.itemA = [[UITabBarItem alloc] initWithTitle:@"A" image:nil tag:0];
self.itemB = [[UITabBarItem alloc] initWithTitle:@"B" image:nil tag:0];
self.itemC = [[UITabBarItem alloc] initWithTitle:@"C" image:nil tag:0];
}
- (void)tearDown {
self.itemA = nil;
self.itemB = nil;
self.itemC = nil;
self.tabBarView = nil;
[super tearDown];
}
- (void)testInitCreatesObject {
// When
MDCTabBarView *tabBarView = [[MDCTabBarView alloc] init];
// Then
XCTAssertNotNil(tabBarView);
XCTAssertNotNil(tabBarView.items);
}
/// Tab bars should by default select nil in their items array. The behavior should also be
/// consistent with the UIKit
- (void)testSelectsNilByDefault {
// Given
self.tabBarView.items = @[ self.itemA, self.itemB, self.itemC ];
// Then
XCTAssertNil(self.tabBarView.selectedItem);
}
/// Tab bars should preserve their selection if possible when changing items.
- (void)testPreservesSelectedItem {
// Given items {A, B} which selected item A
self.tabBarView.items = @[ self.itemA, self.itemB ];
self.tabBarView.selectedItem = self.itemA;
XCTAssertEqual(self.tabBarView.selectedItem, self.itemA);
// When
self.tabBarView.items = @[ self.itemC, self.itemA ];
// Then should preserve the selection of A.
XCTAssertEqual(self.tabBarView.selectedItem, self.itemA);
}
/// Tab bars should select nil if the old selection is no longer present.
- (void)testSelectsNilWhenSelectedItemMissing {
// Given items {A, B} which selected item A.
self.tabBarView.items = @[ self.itemA, self.itemB ];
self.tabBarView.selectedItem = self.itemA;
XCTAssertEqual(self.tabBarView.selectedItem, self.itemA);
// When
self.tabBarView.items = @[ self.itemB, self.itemC ];
// Then set items not including A, which should select nil.
XCTAssertNil(self.tabBarView.selectedItem);
}
/// Tab bars should safely accept having their items set to the empty array.
- (void)testSafelyHandlesEmptyItems {
// Given
self.tabBarView.items = @[];
XCTAssertNil(self.tabBarView.selectedItem);
// When
self.tabBarView.items = @[ self.itemA ];
self.tabBarView.items = @[];
// Then
XCTAssertNil(self.tabBarView.selectedItem);
}
// Tab bar should throw error when select the item that doesn't belongs to items.
- (void)testSafelyHandlesNonExistItem {
// Given
self.tabBarView.items = @[];
XCTAssertNil(self.tabBarView.selectedItem);
// When set selected item to nil.
self.tabBarView.selectedItem = nil;
// Then should make no difference to the selection.
XCTAssertNil(self.tabBarView.selectedItem);
// Given items {A, B} which selected item A.
self.tabBarView.items = @[ self.itemA, self.itemB ];
self.tabBarView.selectedItem = self.itemA;
XCTAssertEqual(self.tabBarView.selectedItem, self.itemA);
// When set selected item to C, then should raise exception.
XCTAssertThrows(self.tabBarView.selectedItem = self.itemC);
}
// Setting items to the same set of items should change nothing.
- (void)testItemsUpdateIsIdempotent {
// Given items {A, B} which selected item A.
self.tabBarView.items = @[ self.itemA, self.itemB ];
self.tabBarView.selectedItem = self.itemA;
XCTAssertEqual(self.tabBarView.selectedItem, self.itemA);
// When set same set of items.
self.tabBarView.items = @[ self.itemA, self.itemB ];
// Then should make no difference to the selection.
XCTAssertEqual(self.tabBarView.selectedItem, self.itemA);
}
@end