|
| 1 | +import Flutter |
| 2 | +import UIKit |
| 3 | +import XCTest |
| 4 | + |
| 5 | +@testable import Buzz |
| 6 | + |
| 7 | +final class ProfileSetupRecoveryTests: XCTestCase { |
| 8 | + @MainActor |
| 9 | + func testStartedEditorWithoutReadyRestoresProfileAndAllowsRetry() async throws { |
| 10 | + let messenger = ProfileSetupTestMessenger() |
| 11 | + let parent = UIViewController() |
| 12 | + let window = UIWindow(frame: UIScreen.main.bounds) |
| 13 | + window.windowScene = UIApplication.shared.connectedScenes.compactMap { $0 as? UIWindowScene }.first |
| 14 | + window.rootViewController = parent |
| 15 | + window.makeKeyAndVisible() |
| 16 | + defer { window.isHidden = true } |
| 17 | + var watchdogs: [() -> Void] = [] |
| 18 | + weak var engine: FlutterEngine? |
| 19 | + let coordinator = NativeProfileSetupCoordinator(messenger: messenger, |
| 20 | + scheduleEditorWatchdog: { watchdogs.append($0) }, |
| 21 | + startEditorEngine: { |
| 22 | + engine = $0 |
| 23 | + // Start the normal app entry point: it never sends avatar-editor ready. |
| 24 | + return $0.run() |
| 25 | + }, configureEditorEngine: { _ in }) |
| 26 | + messenger.invoke("present", arguments: ["session": 81]) |
| 27 | + let nav = try XCTUnwrap(parent.presentedViewController as? UINavigationController) |
| 28 | + if let transition = nav.transitionCoordinator { |
| 29 | + await withCheckedContinuation { continuation in |
| 30 | + transition.animate(alongsideTransition: nil) { _ in continuation.resume() } |
| 31 | + } |
| 32 | + } |
| 33 | + let profile = try XCTUnwrap(nav.viewControllers.first) |
| 34 | + let avatar = try XCTUnwrap(descendants(profile.view).compactMap { $0 as? UIButton } |
| 35 | + .first { $0.accessibilityLabel == "Add profile photo" }) |
| 36 | + let pan = UIPanGestureRecognizer() |
| 37 | + let disabledPan = UIPanGestureRecognizer() |
| 38 | + disabledPan.isEnabled = false |
| 39 | + nav.view.addGestureRecognizer(pan) |
| 40 | + nav.view.addGestureRecognizer(disabledPan) |
| 41 | + nav.interactivePopGestureRecognizer?.isEnabled = true |
| 42 | + for detent in [UISheetPresentationController.Detent.Identifier("profile"), .large] { |
| 43 | + autoreleasepool { |
| 44 | + nav.sheetPresentationController?.selectedDetentIdentifier = detent |
| 45 | + avatar.sendActions(for: .touchUpInside) |
| 46 | + XCTAssertNotNil(engine) |
| 47 | + XCTAssertTrue(nav.topViewController is FlutterViewController) |
| 48 | + XCTAssertFalse(pan.isEnabled) |
| 49 | + XCTAssertFalse(nav.interactivePopGestureRecognizer?.isEnabled ?? true) |
| 50 | + XCTAssertTrue(nav.isModalInPresentation) |
| 51 | + XCTAssertTrue(nav.isNavigationBarHidden) |
| 52 | + if watchdogs.count > 1 { |
| 53 | + // An old attempt's watchdog cannot tear down the successful retry. |
| 54 | + watchdogs[0]() |
| 55 | + XCTAssertTrue(nav.topViewController is FlutterViewController) |
| 56 | + XCTAssertNotNil(engine) |
| 57 | + } |
| 58 | + watchdogs.last?() |
| 59 | + XCTAssertTrue(nav.topViewController === profile) |
| 60 | + XCTAssertFalse(nav.isNavigationBarHidden) |
| 61 | + XCTAssertEqual(nav.sheetPresentationController?.detents.map(\.identifier), [.init("profile"), .large]) |
| 62 | + XCTAssertEqual(nav.sheetPresentationController?.selectedDetentIdentifier, detent) |
| 63 | + XCTAssertTrue(pan.isEnabled) |
| 64 | + XCTAssertFalse(disabledPan.isEnabled) |
| 65 | + XCTAssertTrue(nav.interactivePopGestureRecognizer?.isEnabled ?? false) |
| 66 | + XCTAssertFalse(nav.isModalInPresentation) |
| 67 | + XCTAssertTrue(avatar.isEnabled) |
| 68 | + XCTAssertFalse(avatar.isHidden) |
| 69 | + } |
| 70 | + // UIKit releases popped controllers at the end of its autorelease turn. |
| 71 | + let released = XCTNSPredicateExpectation(predicate: NSPredicate { _, _ in engine == nil }, object: nil) |
| 72 | + await fulfillment(of: [released], timeout: 2) |
| 73 | + } |
| 74 | + XCTAssertEqual(watchdogs.count, 2) |
| 75 | + withExtendedLifetime(coordinator) {} |
| 76 | + nav.dismiss(animated: false) |
| 77 | + } |
| 78 | + |
| 79 | + @MainActor |
| 80 | + private func descendants(_ view: UIView) -> [UIView] { |
| 81 | + view.subviews.flatMap { [$0] + descendants($0) } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +private final class ProfileSetupTestMessenger: NSObject, FlutterBinaryMessenger { |
| 86 | + private var handler: FlutterBinaryMessageHandler? |
| 87 | + func send(onChannel channel: String, message: Data?) {} |
| 88 | + func send(onChannel channel: String, message: Data?, binaryReply callback: FlutterBinaryReply?) { callback?(nil) } |
| 89 | + func setMessageHandlerOnChannel(_ channel: String, binaryMessageHandler handler: FlutterBinaryMessageHandler?) -> FlutterBinaryMessengerConnection { |
| 90 | + self.handler = handler |
| 91 | + return 1 |
| 92 | + } |
| 93 | + func cleanUpConnection(_ connection: FlutterBinaryMessengerConnection) { handler = nil } |
| 94 | + func invoke(_ method: String, arguments: [String: Any]) { |
| 95 | + let codec = FlutterStandardMethodCodec.sharedInstance() |
| 96 | + handler?(codec.encode(FlutterMethodCall(methodName: method, arguments: arguments))) { _ in } |
| 97 | + } |
| 98 | +} |
0 commit comments