Skip to content

Commit 9278c29

Browse files
author
John Foulkes
committed
Exposed setDeviceAgnostic globally
1 parent ff40e52 commit 9278c29

File tree

4 files changed

+85
-3
lines changed

4 files changed

+85
-3
lines changed

EXPMatchers+FBSnapshotTest.m

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ + (BOOL)compareSnapshotOfViewOrLayer:(id)viewOrLayer snapshot:(NSString *)snapsh
2525
snapshotController.recordMode = record;
2626
snapshotController.referenceImagesDirectory = referenceDirectory;
2727
snapshotController.usesDrawViewHierarchyInRect = [Expecta usesDrawViewHierarchyInRect];
28-
28+
snapshotController.deviceAgnostic = [Expecta isDeviceAgnostic];
29+
2930
if (! snapshotController.referenceImagesDirectory) {
3031
[NSException raise:@"Missing value for referenceImagesDirectory" format:@"Call [[EXPExpectFBSnapshotTest instance] setReferenceImagesDirectory"];
3132
}
@@ -91,6 +92,7 @@ - (NSString *)_getDefaultReferenceDirectory
9192
[NSException raise:@"Could not infer reference image folder" format:@"You should provide a reference dir using setGlobalReferenceImageDir(FB_REFERENCE_IMAGE_DIR);"];
9293
return nil;
9394
}
95+
9496
@end
9597

9698

@@ -160,7 +162,6 @@ - (NSString *)_getDefaultReferenceDirectory
160162

161163
match(^BOOL{
162164
NSString *referenceImageDir = [self _getDefaultReferenceDirectory];
163-
164165
// For view controllers do the viewWill/viewDid dance, then pass view through
165166
if ([actual isKindOfClass:UIViewController.class]) {
166167

@@ -244,7 +245,6 @@ - (NSString *)_getDefaultReferenceDirectory
244245

245246
match(^BOOL{
246247
NSString *referenceImageDir = [self _getDefaultReferenceDirectory];
247-
248248
// For view controllers do the viewWill/viewDid dance, then pass view through
249249
if ([actual isKindOfClass:UIViewController.class]) {
250250
[actual beginAppearanceTransition:YES animated:NO];

ExpectaObject+FBSnapshotTest.h

+4
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,8 @@
1414

1515
+ (BOOL)usesDrawViewHierarchyInRect;
1616

17+
+ (void)setDeviceAgnostic:(BOOL)deviceAgnostic;
18+
19+
+ (BOOL)isDeviceAgnostic;
20+
1721
@end

ExpectaObject+FBSnapshotTest.m

+11
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,15 @@ + (BOOL)usesDrawViewHierarchyInRect {
2222
return usesDrawViewHierarchyInRect.boolValue;
2323
}
2424

25+
+ (void)setDeviceAgnostic:(BOOL)deviceAgnostic
26+
{
27+
objc_setAssociatedObject(self, @selector(isDeviceAgnostic), @(deviceAgnostic), OBJC_ASSOCIATION_ASSIGN);
28+
}
29+
30+
+ (BOOL)isDeviceAgnostic
31+
{
32+
NSNumber *isDeviceAgnostic = objc_getAssociatedObject(self, @selector(isDeviceAgnostic));
33+
return isDeviceAgnostic.boolValue;
34+
}
35+
2536
@end

FBSnapshotTestCaseDemo/FBSnapshotTestCaseDemoTests/FBSnapshotTestCaseDemoSpecs.m

+67
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <Specta/Specta.h>
1111
#include <Expecta/Expecta.h>
1212
#include <Expecta+Snapshots/EXPMatchers+FBSnapshotTest.h>
13+
#import <FBSnapshotTestCase/FBSnapshotTestCasePlatform.h>
1314

1415
#include "FBExampleView.h"
1516
#import "FBViewController.h"
@@ -191,6 +192,72 @@
191192
}).to.raise(@"Nil was passed into haveValidSnapshot. snapshots_with_a_view_controller_Raises_when_a_nil_is_passed_in");
192193
});
193194
});
195+
196+
describe(@"device agnostic", ^{
197+
198+
__block FBViewController *controller;
199+
200+
before(^{
201+
controller = [[FBRedViewController alloc] init];
202+
controller.view.frame = frame;
203+
[Expecta setDeviceAgnostic:TRUE];
204+
});
205+
206+
after(^{
207+
[Expecta setDeviceAgnostic:FALSE];
208+
});
209+
210+
describe(@"recording", ^{
211+
212+
it(@"named", ^{
213+
expect(controller).toNot.recordSnapshotNamed(@"view controller 1");
214+
expect(controller.viewWillAppearCalled).to.beTruthy();
215+
expect(controller.viewDidAppearCalled).to.beTruthy();
216+
NSString *imageName = [[fileManager contentsOfDirectoryAtPath:imagesDirectory error:nil] firstObject];
217+
NSString *expectedImageName = FBDeviceAgnosticNormalizedFileName(@"view controller 1");
218+
expect(imageName).to.contain(expectedImageName);
219+
});
220+
221+
it(@"unnamed", ^{
222+
expect(controller).toNot.recordSnapshot();
223+
expect(controller.viewWillAppearCalled).to.beTruthy();
224+
expect(controller.viewDidAppearCalled).to.beTruthy();
225+
NSString *imageName = [[fileManager contentsOfDirectoryAtPath:imagesDirectory error:nil] firstObject];
226+
NSString *expectedImageName = FBDeviceAgnosticNormalizedFileName(@"snapshots_device_agnostic_recording_unnamed");
227+
expect(imageName).to.contain(expectedImageName);
228+
});
229+
230+
});
231+
232+
describe(@"unnamed", ^{
233+
234+
describe(@"named", ^{
235+
236+
it(@"matches view controller", ^{
237+
expect(controller).toNot.recordSnapshotNamed(@"view controller 2");
238+
239+
FBViewController *newVC = [[FBRedViewController alloc] init];
240+
newVC.view.frame = frame;
241+
expect(newVC).to.haveValidSnapshotNamed(@"view controller 2");
242+
expect(newVC.viewWillAppearCalled).to.beTruthy();
243+
expect(newVC.viewDidAppearCalled).to.beTruthy();
244+
});
245+
246+
});
247+
248+
it(@"matches view controller", ^{
249+
expect(controller).toNot.recordSnapshot();
250+
251+
FBViewController *newVC = [[FBRedViewController alloc] init];
252+
newVC.view.frame = frame;
253+
expect(newVC).to.haveValidSnapshot();
254+
expect(newVC.viewWillAppearCalled).to.beTruthy();
255+
expect(newVC.viewDidAppearCalled).to.beTruthy();
256+
});
257+
258+
});
259+
260+
});
194261
});
195262

196263
SpecEnd

0 commit comments

Comments
 (0)