// // UIButtonConfigurationTests.swift // suixinkanTests // import UIKit import XCTest @testable import suixinkan /// `UIButton` configuration 内容边距辅助方法测试。 @MainActor final class UIButtonConfigurationTests: XCTestCase { func testContentInsetsCreatePlainConfiguration() { let button = UIButton(type: .system) button.setConfigurationContentInsets( NSDirectionalEdgeInsets(top: 2, leading: 4, bottom: 6, trailing: 8) ) let contentInsets = button.configuration?.contentInsets XCTAssertEqual(contentInsets?.top, 2) XCTAssertEqual(contentInsets?.leading, 4) XCTAssertEqual(contentInsets?.bottom, 6) XCTAssertEqual(contentInsets?.trailing, 8) } func testContentInsetsPreserveExistingConfiguration() { let image = UIImage(systemName: "star.fill") var configuration = UIButton.Configuration.filled() configuration.title = "保留标题" configuration.image = image configuration.imagePlacement = .trailing configuration.imagePadding = 7 configuration.baseForegroundColor = .red let button = UIButton(configuration: configuration) button.setConfigurationContentInsets( NSDirectionalEdgeInsets(top: 1, leading: 3, bottom: 5, trailing: 9) ) XCTAssertEqual(button.configuration?.title, "保留标题") XCTAssertEqual(button.configuration?.image, image) XCTAssertEqual(button.configuration?.imagePlacement, .trailing) XCTAssertEqual(button.configuration?.imagePadding, 7) XCTAssertEqual(button.configuration?.baseForegroundColor, .red) XCTAssertEqual(button.configuration?.contentInsets.leading, 3) XCTAssertEqual(button.configuration?.contentInsets.trailing, 9) } func testContentInsetsPreserveLegacyButtonContentAndAllowUpdates() { let image = UIImage(systemName: "star") let button = UIButton(type: .system) button.setTitle("原始标题", for: .normal) button.setTitleColor(.blue, for: .normal) button.setImage(image, for: .normal) button.setConfigurationContentInsets( NSDirectionalEdgeInsets(top: 2, leading: 4, bottom: 2, trailing: 4) ) XCTAssertEqual(button.currentTitle, "原始标题") XCTAssertEqual(button.currentTitleColor, .blue) XCTAssertEqual(button.currentImage, image) button.setTitle("更新标题", for: .normal) button.setTitleColor(.red, for: .normal) XCTAssertEqual(button.currentTitle, "更新标题") XCTAssertEqual(button.currentTitleColor, .red) } }