Skip to content

Commit b78d4b9

Browse files
authored
Merge pull request #238 from line/coverage/login-button
Add LoginButton unit tests
2 parents 1e0662a + 5a5f372 commit b78d4b9

File tree

4 files changed

+206
-3
lines changed

4 files changed

+206
-3
lines changed

LineSDK/LineSDK.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
2B8D25342E39A2540029FB34 /* LoginFlowFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2B8D25322E39A2540029FB34 /* LoginFlowFactory.swift */; };
1515
2B8D25362E39A2B50029FB34 /* LoginProcessMocks.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2B8D25352E39A2B50029FB34 /* LoginProcessMocks.swift */; };
1616
2B8D25382E39A2D40029FB34 /* LoginProcessFlowTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2B8D25372E39A2D40029FB34 /* LoginProcessFlowTests.swift */; };
17+
2BDD19402E41D90000C6824C /* LoginButtonTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2BDD193F2E41D90000C6824C /* LoginButtonTests.swift */; };
1718
3F75522F2123D215004AC047 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 3F75522E2123D214004AC047 /* Assets.xcassets */; };
1819
3F75523121244502004AC047 /* LoginButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F75523021244502004AC047 /* LoginButton.swift */; };
1920
3F946A212126D13A009914ED /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 3F946A202126D13A009914ED /* [email protected] */; };
@@ -589,6 +590,7 @@
589590
2B8D25322E39A2540029FB34 /* LoginFlowFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginFlowFactory.swift; sourceTree = "<group>"; };
590591
2B8D25352E39A2B50029FB34 /* LoginProcessMocks.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginProcessMocks.swift; sourceTree = "<group>"; };
591592
2B8D25372E39A2D40029FB34 /* LoginProcessFlowTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginProcessFlowTests.swift; sourceTree = "<group>"; };
593+
2BDD193F2E41D90000C6824C /* LoginButtonTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginButtonTests.swift; sourceTree = "<group>"; };
592594
3F75522E2123D214004AC047 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
593595
3F75523021244502004AC047 /* LoginButton.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LoginButton.swift; sourceTree = "<group>"; };
594596
3F946A202126D13A009914ED /* [email protected] */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "[email protected]"; sourceTree = "<group>"; };
@@ -1280,6 +1282,7 @@
12801282
4BBAFC4F2101D31300E7BFF6 /* ConstantTests.swift */,
12811283
4B90588821006E5D004D717F /* Info.plist */,
12821284
4B6A702821096E3700D71C66 /* KeychainStoreTests.swift */,
1285+
2BDD193F2E41D90000C6824C /* LoginButtonTests.swift */,
12831286
4B08F9C12119863500B140DF /* LineSDKErrorTests.swift */,
12841287
);
12851288
path = LineSDKTests;
@@ -2405,6 +2408,7 @@
24052408
4B6CACC7239F44D100BD6C35 /* OpenChatControllerTests.swift in Sources */,
24062409
4BA8E44E210EE79B00355F03 /* PipelineTests.swift in Sources */,
24072410
4B8FF4932105C49200890AEF /* LoginFlowTests.swift in Sources */,
2411+
2BDD19402E41D90000C6824C /* LoginButtonTests.swift in Sources */,
24082412
4B6CACC9239F4A2B00BD6C35 /* AssertionHelpers.swift in Sources */,
24092413
4BB0E256239F5C8A008C7F3F /* FormEntryTests.swift in Sources */,
24102414
DB0AFF612247FB2E002729AD /* PageTabViewTests.swift in Sources */,
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
//
2+
// LoginButtonTests.swift
3+
//
4+
// Copyright (c) 2016-present, LY Corporation. All rights reserved.
5+
//
6+
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
7+
// copy and distribute this software in source code or binary form for use
8+
// in connection with the web services and APIs provided by LY Corporation.
9+
//
10+
// As with any software that integrates with the LY Corporation platform, your use of this software
11+
// is subject to the LINE Developers Agreement [http://terms2.line.me/LINE_Developers_Agreement].
12+
// This copyright notice shall be included in all copies or substantial portions of the software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17+
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
//
21+
22+
import XCTest
23+
@testable import LineSDK
24+
25+
@MainActor
26+
class LoginButtonTests: XCTestCase, ViewControllerCompatibleTest {
27+
28+
var window: UIWindow!
29+
var loginButton: LoginButton!
30+
31+
override func setUp() async throws {
32+
let url = URL(string: "https://example.com/auth")
33+
LoginManager.shared.setup(channelID: "123", universalLinkURL: url)
34+
loginButton = LoginButton()
35+
}
36+
37+
override func tearDown() async throws {
38+
LoginManager.shared.reset()
39+
resetViewController()
40+
loginButton = nil
41+
}
42+
43+
// MARK: - Initialization Tests
44+
45+
func testInitialization() {
46+
XCTAssertEqual(loginButton.permissions, [.profile])
47+
XCTAssertEqual(loginButton.buttonSize, .normal)
48+
XCTAssertEqual(loginButton.accessibilityLabel, "login.button")
49+
XCTAssertNotNil(loginButton.titleLabel?.font)
50+
XCTAssertEqual(loginButton.titleColor(for: .normal), .white)
51+
XCTAssertEqual(loginButton.titleLabel?.textAlignment, .center)
52+
}
53+
54+
// MARK: - Button Text Tests
55+
56+
func testButtonTextCustomization() {
57+
let customText = "Custom Login Text"
58+
loginButton.buttonText = customText
59+
60+
XCTAssertEqual(loginButton.title(for: .normal), customText)
61+
}
62+
63+
func testIntrinsicContentSize() {
64+
let initialSize = loginButton.intrinsicContentSize
65+
XCTAssertTrue(initialSize.width > 0)
66+
XCTAssertTrue(initialSize.height > 0)
67+
68+
loginButton.buttonSize = .small
69+
let smallSize = loginButton.intrinsicContentSize
70+
XCTAssertTrue(smallSize.height < initialSize.height)
71+
72+
loginButton.buttonText = "Very Long Custom Login Button Text"
73+
let longTextSize = loginButton.intrinsicContentSize
74+
XCTAssertTrue(longTextSize.width > smallSize.width)
75+
}
76+
77+
// MARK: - Permission Tests
78+
79+
func testPermissionsProperty() {
80+
let permissions: Set<LoginPermission> = [.profile, .openID, .email]
81+
loginButton.permissions = permissions
82+
83+
XCTAssertEqual(loginButton.permissions, permissions)
84+
}
85+
86+
// MARK: - Parameters Tests
87+
88+
func testParametersProperty() {
89+
var parameters = LoginManager.Parameters()
90+
parameters.onlyWebLogin = true
91+
parameters.botPromptStyle = .aggressive
92+
93+
loginButton.parameters = parameters
94+
95+
XCTAssertEqual(loginButton.parameters.onlyWebLogin, true)
96+
XCTAssertEqual(loginButton.parameters.botPromptStyle, .aggressive)
97+
}
98+
99+
// MARK: - Presenting View Controller Tests
100+
101+
func testPresentingViewController() {
102+
let viewController = UIViewController()
103+
loginButton.presentingViewController = viewController
104+
105+
XCTAssertEqual(loginButton.presentingViewController, viewController)
106+
}
107+
108+
// MARK: - Delegate Tests
109+
110+
func testDelegateAssignment() {
111+
let delegate = MockLoginButtonDelegate()
112+
loginButton.delegate = delegate
113+
114+
XCTAssertTrue(loginButton.delegate === delegate)
115+
}
116+
117+
// MARK: - Login Action Tests
118+
119+
func testLoginAction() {
120+
let viewController = setupViewController()
121+
loginButton.presentingViewController = viewController
122+
loginButton.permissions = [.profile, .openID]
123+
124+
XCTAssertFalse(LoginManager.shared.isAuthorizing)
125+
126+
loginButton.login()
127+
128+
XCTAssertTrue(LoginManager.shared.isAuthorizing)
129+
XCTAssertNotNil(LoginManager.shared.currentProcess)
130+
}
131+
132+
// MARK: - Style Update Tests
133+
134+
func testStyleUpdateOnButtonSizeChange() {
135+
let originalSize = loginButton.frame.size
136+
137+
loginButton.buttonSize = .small
138+
loginButton.updateButtonStyle()
139+
140+
// The frame size should be updated after style change
141+
let newSize = loginButton.frame.size
142+
XCTAssertNotEqual(originalSize, newSize)
143+
}
144+
145+
func testStyleUpdateOnButtonTextChange() {
146+
let originalSize = loginButton.frame.size
147+
148+
loginButton.buttonText = "Different Text"
149+
150+
// The frame size should be updated after text change
151+
let newSize = loginButton.frame.size
152+
XCTAssertNotEqual(originalSize, newSize)
153+
}
154+
155+
// MARK: - Accessibility Tests
156+
157+
func testAccessibilityConfiguration() {
158+
XCTAssertEqual(loginButton.accessibilityLabel, "login.button")
159+
160+
// Test custom accessibility label
161+
loginButton.accessibilityLabel = "Custom Login"
162+
XCTAssertEqual(loginButton.accessibilityLabel, "Custom Login")
163+
}
164+
165+
// MARK: - UI Configuration Tests
166+
167+
func testTitleEdgeInsets() {
168+
loginButton.buttonSize = .normal
169+
loginButton.updateButtonStyle()
170+
171+
let insets = loginButton.titleEdgeInsets
172+
XCTAssertTrue(insets.left > 0)
173+
XCTAssertTrue(insets.right > 0)
174+
XCTAssertTrue(insets.top > 0)
175+
XCTAssertTrue(insets.bottom > 0)
176+
}
177+
178+
func testBackgroundImages() {
179+
loginButton.buttonSize = .normal
180+
loginButton.updateButtonStyle()
181+
182+
XCTAssertNotNil(loginButton.backgroundImage(for: .normal))
183+
XCTAssertNotNil(loginButton.backgroundImage(for: .highlighted))
184+
XCTAssertNotNil(loginButton.backgroundImage(for: .disabled))
185+
}
186+
}
187+
188+
// MARK: - Mock Delegate
189+
190+
private class MockLoginButtonDelegate: LoginButtonDelegate {
191+
192+
func loginButtonDidStartLogin(_ button: LoginButton) { }
193+
194+
func loginButton(_ button: LoginButton, didSucceedLogin loginResult: LoginResult) { }
195+
196+
func loginButton(_ button: LoginButton, didFailLogin error: LineSDKError) { }
197+
}

LineSDK/LineSDKTests/Utils/APITests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ class APITests: XCTestCase {
3535
LoginManager.shared.setup(channelID: "123", universalLinkURL: nil)
3636
}
3737

38-
override func tearDown() {
39-
LoginManager.shared.reset()
40-
super.tearDown()
38+
override func tearDown() async throws {
39+
await LoginManager.shared.reset()
40+
try await super.tearDown()
4141
}
4242

4343
let config = LoginConfiguration(channelID: "123", universalLinkURL: nil)

LineSDK/LineSDKTests/Utils/LoginManagerExtension.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import Foundation
2323
@testable import LineSDK
2424

2525
extension LoginManager {
26+
@MainActor
2627
func reset() {
2728
setup = false
2829

@@ -32,5 +33,6 @@ extension LoginManager {
3233
AccessTokenStore._shared = nil
3334

3435
LoginConfiguration._shared = nil
36+
currentProcess?.stop()
3537
}
3638
}

0 commit comments

Comments
 (0)