Skip to content
This repository was archived by the owner on Feb 5, 2025. It is now read-only.

Commit 43d7011

Browse files
committed
Add MemoryManagement tests confirming view controllers don't have retain cycles
- Created verifyObjectsDeallocatedAfterTeardown method that can be used for confirming that any object is deallocated after it's lifecycle should end - Testing most of the view controllers confirming that there aren't easily reproducible memory leaks remaining
1 parent c06ffa1 commit 43d7011

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

WordPressAuthenticator.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
0193F7752A615521004D7C16 /* MemoryManagementTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0193F7742A615521004D7C16 /* MemoryManagementTests.swift */; };
1011
020BE74A23B0BD2E007FE54C /* WordPressAuthenticatorDisplayImages.swift in Sources */ = {isa = PBXBuildFile; fileRef = 020BE74923B0BD2E007FE54C /* WordPressAuthenticatorDisplayImages.swift */; };
1112
020DEF6428AA091100C85D51 /* MagicLinkRequester.swift in Sources */ = {isa = PBXBuildFile; fileRef = 020DEF6328AA091100C85D51 /* MagicLinkRequester.swift */; };
1213
02A526CA28A3499C00FD1812 /* MagicLinkRequestedViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02A526C828A3499C00FD1812 /* MagicLinkRequestedViewController.swift */; };
@@ -261,6 +262,7 @@
261262
/* End PBXCopyFilesBuildPhase section */
262263

263264
/* Begin PBXFileReference section */
265+
0193F7742A615521004D7C16 /* MemoryManagementTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemoryManagementTests.swift; sourceTree = "<group>"; };
264266
020BE74923B0BD2E007FE54C /* WordPressAuthenticatorDisplayImages.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WordPressAuthenticatorDisplayImages.swift; sourceTree = "<group>"; };
265267
020DEF6328AA091100C85D51 /* MagicLinkRequester.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MagicLinkRequester.swift; sourceTree = "<group>"; };
266268
02A526C828A3499C00FD1812 /* MagicLinkRequestedViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MagicLinkRequestedViewController.swift; sourceTree = "<group>"; };
@@ -956,6 +958,7 @@
956958
3F86A83F29D280DC005D20C0 /* SingIn */,
957959
F18DF0E32525009200D83AFE /* SupportingFiles */,
958960
3F338B6B289B87E60014ADC5 /* UnitTests.xctestplan */,
961+
0193F7742A615521004D7C16 /* MemoryManagementTests.swift */,
959962
);
960963
path = WordPressAuthenticatorTests;
961964
sourceTree = "<group>";
@@ -1552,6 +1555,7 @@
15521555
buildActionMask = 2147483647;
15531556
files = (
15541557
3F4E64782990BBD4000DB555 /* IDTokenTests.swift in Sources */,
1558+
0193F7752A615521004D7C16 /* MemoryManagementTests.swift in Sources */,
15551559
3F86A84E29D3B53D005D20C0 /* SocialServiceTests.swift in Sources */,
15561560
3F879FDB293A49AA005C2B48 /* NewGoogleAuthenticatorTests.swift in Sources */,
15571561
F18DF0E5252500A600D83AFE /* WordPressAuthenticatorTests-Bridging-Header.h in Sources */,
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
@testable import WordPressAuthenticator
2+
import XCTest
3+
4+
final class MemoryManagementTests: XCTestCase {
5+
override func setUp() {
6+
super.setUp()
7+
8+
WordPressAuthenticator.initialize(
9+
configuration: WordpressAuthenticatorProvider.wordPressAuthenticatorConfiguration(),
10+
style: WordpressAuthenticatorProvider.wordPressAuthenticatorStyle(.random),
11+
unifiedStyle: WordpressAuthenticatorProvider.wordPressAuthenticatorUnifiedStyle(.random)
12+
)
13+
}
14+
15+
func testViewControllersDeallocatedAfterDismissing() {
16+
let viewControllers: [UIViewController] = [
17+
Storyboard.login.instance.instantiateInitialViewController()!,
18+
LoginPrologueLoginMethodViewController.instantiate(from: .login)!,
19+
LoginPrologueSignupMethodViewController.instantiate(from: .login)!,
20+
Login2FAViewController.instantiate(from: .login)!,
21+
LoginEmailViewController.instantiate(from: .login)!,
22+
LoginSelfHostedViewController.instantiate(from: .login)!,
23+
LoginSiteAddressViewController.instantiate(from: .login)!,
24+
LoginUsernamePasswordViewController.instantiate(from: .login)!,
25+
LoginWPComViewController.instantiate(from: .login)!,
26+
SignupEmailViewController.instantiate(from: .signup)!,
27+
SignupGoogleViewController.instantiate(from: .signup)!,
28+
GetStartedViewController.instantiate(from: .getStarted)!,
29+
VerifyEmailViewController.instantiate(from: .verifyEmail)!,
30+
PasswordViewController.instantiate(from: .password)!,
31+
TwoFAViewController.instantiate(from: .twoFA)!,
32+
GoogleAuthViewController.instantiate(from: .googleAuth)!,
33+
SiteAddressViewController.instantiate(from: .siteAddress)!,
34+
SiteCredentialsViewController.instantiate(from: .siteAddress)!
35+
]
36+
37+
for viewController in viewControllers {
38+
viewController.loadViewIfNeeded()
39+
}
40+
41+
verifyObjectsDeallocatedAfterTeardown(viewControllers)
42+
}
43+
44+
// MARK: - Helpers
45+
46+
private func verifyObjectsDeallocatedAfterTeardown(_ objects: [AnyObject]) {
47+
/// Create the array of weak objects so we could assert them in the teardown block
48+
let weakObjects: [() -> AnyObject?] = objects.map { object in { [weak object] in
49+
return object
50+
}
51+
}
52+
53+
/// All the weak items should be deallocated in the teardown block unless there's a retain cycle holding them
54+
addTeardownBlock {
55+
for object in weakObjects {
56+
XCTAssertNil(object(), "\(object()!.self) is not deallocated after teardown")
57+
}
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)