forked from ParisiLabs/wire-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZMSnapshotTestCase.m
237 lines (201 loc) · 8.31 KB
/
ZMSnapshotTestCase.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//
// Wire
// Copyright (C) 2016 Wire Swiss GmbH
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see http://www.gnu.org/licenses/.
//
#import "ZMSnapshotTestCase.h"
#import <PureLayout/PureLayout.h>
#import <zmessaging/zmessaging.h>
#import "UIColor+WAZExtensions.h"
#import "ColorScheme.h"
#import "MagicConfig.h"
static CGSize const ZMDeviceSizeIPhone4 = (CGSize){ .width = 320, .height = 480 };
static CGSize const ZMDeviceSizeIPhone5 = (CGSize){ .width = 320, .height = 568 };
static CGSize const ZMDeviceSizeIPhone6 = (CGSize){ .width = 375, .height = 667 };
static CGSize const ZMDeviceSizeIPhone6Plus = (CGSize){ .width = 414, .height = 736 };
static CGSize const ZMDeviceSizeIPadPortrait = (CGSize){ .width = 768, .height = 1024 };
static CGSize const ZMDeviceSizeIPadLandscape = (CGSize){ .width = 1024, .height = 768 };
static NSArray *phoneSizes(void) {
return @[
[NSValue valueWithCGSize:ZMDeviceSizeIPhone4],
[NSValue valueWithCGSize:ZMDeviceSizeIPhone5],
[NSValue valueWithCGSize:ZMDeviceSizeIPhone6],
[NSValue valueWithCGSize:ZMDeviceSizeIPhone6Plus]
];
}
static NSArray *tabletSizes(void) {
return @[
[NSValue valueWithCGSize:ZMDeviceSizeIPadPortrait],
[NSValue valueWithCGSize:ZMDeviceSizeIPadLandscape]
];
}
static NSArray *deviceSizes(void) {
return [phoneSizes() arrayByAddingObjectsFromArray:tabletSizes()];
}
static NSSet *phoneWidths(void) {
return [phoneSizes() mapWithBlock:^NSValue *(NSValue *boxedSize) {
return @(boxedSize.CGSizeValue.width);
}].set;
}
@implementation ZMSnapshotTestCase
- (void)setUp
{
[super setUp];
[MagicConfig sharedConfig];
XCTAssertEqual(UIScreen.mainScreen.scale, 2, @"Snapshot tests need to be run on a device with a 2x scale");
[UIView setAnimationsEnabled:NO];
self.snapshotBackgroundColor = UIColor.clearColor;
// Enable when the design of the view has changed in order to update the reference snapshots
#ifdef RECORDING_SNAPSHOTS
self.recordMode = YES;
#endif
self.usesDrawViewHierarchyInRect = YES;
[NSManagedObjectContext setUseInMemoryStore:YES];
[NSManagedObjectContext resetUserInterfaceContext];
[NSManagedObjectContext resetSharedPersistentStoreCoordinator];
self.uiMOC = [NSManagedObjectContext createUserInterfaceContextWithStoreDirectory:[[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil]];
}
- (void)tearDown
{
[super tearDown];
[UIColor setAccentOverrideColor:ZMAccentColorUndefined];
[UIView setAnimationsEnabled:YES];
}
- (void)setAccentColor:(ZMAccentColor)accentColor
{
[UIColor setAccentOverrideColor:accentColor];
}
- (void)assertAmbigousLayout:(UIView *)view file:(const char[])file line:(NSUInteger)line
{
if (view.hasAmbiguousLayout) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
NSString *description = [NSString stringWithFormat:@"Ambigous layout in view: %@ trace: \n%@", view, [view performSelector:@selector(_autolayoutTrace)]];
#pragma clang diagnostic pop
NSString *filePath = [NSString stringWithFormat:@"%s", file];
[self recordFailureWithDescription:description inFile:filePath atLine:line expected:YES];
}
}
- (UIView *)containerViewWithView:(UIView *)view
{
UIView *container = [[UIView alloc] initWithFrame:view.bounds];
container.backgroundColor = self.snapshotBackgroundColor;
[container addSubview:view];
[view autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
return container;
}
- (void)verifyView:(UIView *)view file:(const char[])file line:(NSUInteger)line
{
[self verifyView:view tolerance:0 file:file line:line];
}
- (void)verifyView:(UIView *)view tolerance:(float)tolerance file:(const char[])file line:(NSUInteger)line
{
UIView *container = [self containerViewWithView:view];
if ([self assertEmptyFrame:container file:file line:line]) {
return;
}
FBSnapshotVerifyViewWithOptions(container, NSStringFromCGSize(view.bounds.size), FBSnapshotTestCaseDefaultSuffixes(), tolerance);
[self assertAmbigousLayout:container file:file line:line];
}
- (BOOL)assertEmptyFrame:(UIView *)view file:(const char[])file line:(NSUInteger)line
{
if (CGRectIsEmpty(view.frame)) {
NSString *description = @"View frame can not be empty";
NSString *filePath = [NSString stringWithFormat:@"%s", file];
[self recordFailureWithDescription:description inFile:filePath atLine:line expected:YES];
return YES;
}
return NO;
}
- (void)verifyViewInAllDeviceSizes:(UIView *)view file:(const char[])file line:(NSUInteger)line
{
[self verifyViewInAllDeviceSizes:view file:file line:line configurationBlock:nil];
}
- (void)verifyViewInAllPhoneWidths:(UIView *)view file:(const char[])file line:(NSUInteger)line
{
[self assertAmbigousLayout:view file:file line:line];
for (NSValue *value in phoneWidths()) {
[self verifyView:view width:value.CGSizeValue.width file:file line:line];
}
}
- (void)verifyViewInAllTabletWidths:(UIView *)view file:(const char[])file line:(NSUInteger)line
{
[self assertAmbigousLayout:view file:file line:line];
for (NSValue *value in tabletSizes()) {
[self verifyView:view width:value.CGSizeValue.width file:file line:line];
}
}
- (void)verifyView:(UIView *)view width:(CGFloat)width file:(const char[])file line:(NSUInteger)line
{
UIView *container = [self containerViewWithView:view];
[container autoSetDimension:ALDimensionWidth toSize:width];
[container setNeedsLayout];
[container layoutIfNeeded];
[container setNeedsLayout];
[container layoutIfNeeded];
if ([self assertEmptyFrame:container file:file line:line]) {
return;
}
FBSnapshotVerifyView(container, @(width).stringValue)
}
- (void)verifyViewInAllPhoneSizes:(UIView *)view
file:(const char[])file
line:(NSUInteger)line
configurationBlock:(void (^)(UIView * view))configuration;
{
[self verifyView:view inSizes:phoneSizes() file:file line:line configuration:^(UIView *view,__unused BOOL isPad) {
if (nil != configuration) {
configuration(view);
}
}];
}
- (void)verifyViewInAllDeviceSizes:(UIView *)view
file:(const char[])file
line:(NSUInteger)line
configurationBlock:(void (^)(UIView * view, BOOL isPad))configuration
{
[self verifyView:view inSizes:deviceSizes() file:file line:line configuration:configuration];
}
- (void)verifyView:(UIView *)view
inSizes:(NSArray <NSValue *>*)sizes
file:(const char[])file
line:(NSUInteger)line
configuration:(void (^)(UIView * view, BOOL isPad))configuration
{
for (NSValue *value in sizes) {
CGSize size = value.CGSizeValue;
view.frame = CGRectMake(0, 0, size.width, size.height);
if (nil != configuration) {
BOOL iPad = CGSizeEqualToSize(size, ZMDeviceSizeIPadLandscape) || CGSizeEqualToSize(size, ZMDeviceSizeIPadPortrait);
[UIView performWithoutAnimation:^{
configuration(view, iPad);
}];
}
[self verifyView:view file:file line:line];
}
}
#pragma mark - Helper
- (UIImage *)imageInTestBundleNamed:(NSString *)name
{
return [UIImage imageWithContentsOfFile:[self URLForResourceInTestBundleNamed:name].path];
}
- (NSURL *)URLForResourceInTestBundleNamed:(NSString *)name
{
NSBundle *bundle = [NSBundle bundleForClass:self.class];
NSURL *url = [bundle URLForResource:name.stringByDeletingPathExtension withExtension:name.pathExtension];
XCTAssertTrue(url.fileURL);
return url;
}
@end