|
| 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 | +} |
0 commit comments