import UIKit import XCTest @testable import suixinkan /// 页面通用 Loading 必须跟随可见性,并与其它页面或请求的令牌相互独立。 @MainActor final class GlobalPageLoadingTests: XCTestCase { override func setUp() { super.setUp() GlobalLoadingManager.shared.hideAll() } override func tearDown() { GlobalLoadingManager.shared.hideAll() super.tearDown() } /// 页面首次读取可以提前标记,但只有完整出现后才使用通用遮罩。 func testLoadingWaitsForVisiblePageAndFinishesWithoutLeaking() { let controller = makeController() controller.setPageLoading(true) XCTAssertFalse(GlobalLoadingManager.shared.isShowing) controller.viewDidAppear(false) XCTAssertTrue(GlobalLoadingManager.shared.isShowing) controller.setPageLoading(false) XCTAssertFalse(GlobalLoadingManager.shared.isShowing) controller.viewWillDisappear(false) } /// 同步重复刷新展示不会重复持有令牌,成功或错误结束都只需一次释放。 func testRepeatedLoadingUpdatesOwnOnlyOneToken() { let controller = makeController() controller.viewDidAppear(false) controller.setPageLoading(true) controller.setPageLoading(true) controller.setPageLoading(true) controller.setPageLoading(false) XCTAssertFalse(GlobalLoadingManager.shared.isShowing) controller.viewWillDisappear(false) } /// 离页时释放本页;迟到的结束不能关闭另一请求,迟到开始也不能覆盖新页面。 func testLeavingAndLateUpdatesCannotHideOrCoverAnotherPage() { let controller = makeController() controller.viewDidAppear(false) controller.setPageLoading(true) let other = GlobalLoadingManager.shared.beginRequest() controller.viewWillDisappear(false) XCTAssertTrue(GlobalLoadingManager.shared.isShowing) controller.setPageLoading(false) XCTAssertTrue(GlobalLoadingManager.shared.isShowing) GlobalLoadingManager.shared.endRequest(other) controller.setPageLoading(true) XCTAssertFalse(GlobalLoadingManager.shared.isShowing) } /// 后台关闭通用遮罩,返回前台只恢复仍可见且读取未结束的页面。 func testBackgroundAndForegroundRespectPendingReadAndVisibility() { let controller = makeController() controller.viewDidAppear(false) controller.setPageLoading(true) NotificationCenter.default.post(name: UIApplication.willResignActiveNotification, object: nil) XCTAssertFalse(GlobalLoadingManager.shared.isShowing) GlobalLoadingManager.shared.hideAll() NotificationCenter.default.post(name: UIApplication.didBecomeActiveNotification, object: nil) XCTAssertTrue(GlobalLoadingManager.shared.isShowing) controller.viewWillDisappear(false) NotificationCenter.default.post(name: UIApplication.didBecomeActiveNotification, object: nil) XCTAssertFalse(GlobalLoadingManager.shared.isShowing) } /// 请求已在后台完成时,不因页面回到前台而重新展示过期遮罩。 func testReadFinishingInBackgroundDoesNotRestoreLoading() { let controller = makeController() controller.viewDidAppear(false) controller.setPageLoading(true) NotificationCenter.default.post(name: UIApplication.willResignActiveNotification, object: nil) controller.setPageLoading(false) NotificationCenter.default.post(name: UIApplication.didBecomeActiveNotification, object: nil) XCTAssertFalse(GlobalLoadingManager.shared.isShowing) controller.viewWillDisappear(false) } private func makeController() -> GlobalLoadingPageViewController { let controller = GlobalLoadingPageViewController() controller.loadViewIfNeeded() NotificationCenter.default.post(name: UIApplication.didBecomeActiveNotification, object: nil) return controller } }