This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 252
/
Copy pathFBSnapshotControllerTests.m
120 lines (100 loc) · 4.81 KB
/
FBSnapshotControllerTests.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
/*
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
#import <XCTest/XCTest.h>
#import "FBSnapshotTestController.h"
#import "FBSnapshotTestCasePlatform.h"
@interface FBSnapshotControllerTests : XCTestCase
@end
@implementation FBSnapshotControllerTests
#pragma mark - Tests
- (void)testCompareReferenceImageToImageShouldBeEqual
{
UIImage *referenceImage = [self _bundledImageNamed:@"square" type:@"png"];
XCTAssertNotNil(referenceImage);
UIImage *testImage = [self _bundledImageNamed:@"square-copy" type:@"png"];
XCTAssertNotNil(testImage);
FBSnapshotTestController *controller = [[FBSnapshotTestController alloc] initWithTestClass:nil];
NSError *error = nil;
XCTAssertTrue([controller compareReferenceImage:referenceImage toImage:testImage tolerance:0 error:&error]);
XCTAssertNil(error);
}
- (void)testCompareReferenceImageToImageShouldNotBeEqual
{
UIImage *referenceImage = [self _bundledImageNamed:@"square" type:@"png"];
XCTAssertNotNil(referenceImage);
UIImage *testImage = [self _bundledImageNamed:@"square_with_text" type:@"png"];
XCTAssertNotNil(testImage);
FBSnapshotTestController *controller = [[FBSnapshotTestController alloc] initWithTestClass:nil];
NSError *error = nil;
XCTAssertFalse([controller compareReferenceImage:referenceImage toImage:testImage tolerance:0 error:&error]);
XCTAssertNotNil(error);
XCTAssertEqual(error.code, FBSnapshotTestControllerErrorCodeImagesDifferent);
}
- (void)testCompareReferenceImageWithVeryLowToleranceShouldNotMatch
{
UIImage *referenceImage = [self _bundledImageNamed:@"square" type:@"png"];
XCTAssertNotNil(referenceImage);
UIImage *testImage = [self _bundledImageNamed:@"square_with_pixel" type:@"png"];
XCTAssertNotNil(testImage);
FBSnapshotTestController *controller = [[FBSnapshotTestController alloc] initWithTestClass:nil];
// With virtually no margin for error, this should fail to be equal
NSError *error = nil;
XCTAssertFalse([controller compareReferenceImage:referenceImage toImage:testImage tolerance:0.0001 error:&error]);
XCTAssertNotNil(error);
XCTAssertEqual(error.code, FBSnapshotTestControllerErrorCodeImagesDifferent);
}
- (void)testCompareReferenceImageWithVeryLowToleranceShouldMatch
{
UIImage *referenceImage = [self _bundledImageNamed:@"square" type:@"png"];
XCTAssertNotNil(referenceImage);
UIImage *testImage = [self _bundledImageNamed:@"square_with_pixel" type:@"png"];
XCTAssertNotNil(testImage);
FBSnapshotTestController *controller = [[FBSnapshotTestController alloc] initWithTestClass:nil];
// With some tolerance these should be considered the same
NSError *error = nil;
XCTAssertTrue([controller compareReferenceImage:referenceImage toImage:testImage tolerance:.001 error:&error]);
XCTAssertNil(error);
}
- (void)testCompareReferenceImageWithDifferentSizes
{
UIImage *referenceImage = [self _bundledImageNamed:@"square" type:@"png"];
XCTAssertNotNil(referenceImage);
UIImage *testImage = [self _bundledImageNamed:@"rect" type:@"png"];
XCTAssertNotNil(testImage);
FBSnapshotTestController *controller = [[FBSnapshotTestController alloc] initWithTestClass:nil];
// With some tolerance these should be considered the same
NSError *error = nil;
XCTAssertFalse([controller compareReferenceImage:referenceImage toImage:testImage tolerance:0 error:&error]);
XCTAssertNotNil(error);
XCTAssertEqual(error.code, FBSnapshotTestControllerErrorCodeImagesDifferentSizes);
}
- (void)testFailedImageWithDeviceAgnosticShouldHaveModelOnName
{
UIImage *referenceImage = [self _bundledImageNamed:@"square" type:@"png"];
XCTAssertNotNil(referenceImage);
UIImage *testImage = [self _bundledImageNamed:@"square_with_pixel" type:@"png"];
XCTAssertNotNil(testImage);
FBSnapshotTestController *controller = [[FBSnapshotTestController alloc] initWithTestClass:nil];
[controller setDeviceAgnostic:YES];
[controller setReferenceImagesDirectory:@"/dev/null/"];
NSError *error = nil;
SEL selector = @selector(isDeviceAgnostic);
[controller referenceImageForSelector:selector identifier:@"" error:&error];
XCTAssertNotNil(error);
NSString *deviceAgnosticReferencePath = FBDeviceAgnosticNormalizedFileName(NSStringFromSelector(selector));
XCTAssertTrue([(NSString *)[error.userInfo objectForKey:FBReferenceImageFilePathKey] containsString:deviceAgnosticReferencePath]);
}
#pragma mark - Private helper methods
- (UIImage *)_bundledImageNamed:(NSString *)name type:(NSString *)type
{
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *path = [bundle pathForResource:name ofType:type];
NSData *data = [[NSData alloc] initWithContentsOfFile:path];
return [[UIImage alloc] initWithData:data];
}
@end