Skip to content

Commit 7d9fc19

Browse files
mebrianlinmobile-devx-github-bot
authored andcommitted
Move the tearDown notification to tearDownWithCompletionHandler:.
This is the tearDown variant that is invoked last, so having it here ensures all tearDown logic has already happened. PiperOrigin-RevId: 734605838
1 parent 05e97f5 commit 7d9fc19

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

TestLib/XCTestCase/XCTestCase+GREYTest.m

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,16 @@ - (void)grey_invokeTest {
186186
andReplaceWithInstanceMethod:@selector(tearDown)];
187187
GREYFatalAssertWithMessage(swizzleSuccess, @"Cannot swizzle %@ tearDown",
188188
NSStringFromClass(selfClass));
189+
190+
IMP tearDownWithCompletionHandlerIMP =
191+
[self methodForSelector:@selector(grey_tearDownWithCompletionHandler:)];
192+
swizzleSuccess = [swizzler swizzleClass:selfClass
193+
addInstanceMethod:@selector(grey_tearDownWithCompletionHandler:)
194+
withImplementation:tearDownWithCompletionHandlerIMP
195+
andReplaceWithInstanceMethod:@selector(tearDownWithCompletionHandler:)];
196+
GREYFatalAssertWithMessage(
197+
swizzleSuccess,
198+
@"Cannot swizzle %@ tearDownWithCompletionHandler:", NSStringFromClass(selfClass));
189199
[self grey_markSwizzled];
190200
}
191201

@@ -275,7 +285,24 @@ - (void)grey_tearDown {
275285
return YES;
276286
});
277287
INVOKE_ORIGINAL_IMP(void, @selector(grey_tearDown));
278-
[self grey_sendNotification:kGREYXCTestCaseInstanceDidTearDown];
288+
}
289+
290+
/**
291+
* A swizzled implementation for XCTestCase::tearDownWithCompletionHandler:.
292+
*
293+
* @remark These methods need to be added to each instance of XCTestCase because we don't expect
294+
* tests to invoke <tt> [super tearDownWithCompletionHandler:] </tt>.
295+
*/
296+
- (void)grey_tearDownWithCompletionHandler:(void (^)(NSError *error))completion {
297+
__weak __typeof__(self) weakSelf = self;
298+
INVOKE_ORIGINAL_IMP1(void, @selector(grey_tearDownWithCompletionHandler:), ^(NSError *error) {
299+
__typeof__(self) strongSelf = weakSelf;
300+
GREYFatalAssertWithMessage(strongSelf, @"The test case should not have been deallocated.");
301+
302+
completion(error);
303+
304+
[strongSelf grey_sendNotification:kGREYXCTestCaseInstanceDidTearDown];
305+
});
279306
}
280307

281308
/**

0 commit comments

Comments
 (0)