|
| 1 | +import Testing |
| 2 | +import UIKit |
| 3 | +import WordPressShared |
| 4 | + |
| 5 | +@testable import WordPress |
| 6 | +@testable import WordPressData |
| 7 | + |
| 8 | +@MainActor |
| 9 | +@Suite(.serialized) |
| 10 | +struct WPTabBarControllerTests { |
| 11 | + @Test func nativeTabsKeepTheExistingDestinations() throws { |
| 12 | + try withController { controller in |
| 13 | + #expect(controller.tabs.map(\.identifier) == ["mysites", "reader", "notifications", "me"]) |
| 14 | + #expect(controller.tabs.allSatisfy { $0.preferredPlacement == .fixed }) |
| 15 | + #expect( |
| 16 | + controller.tabs.map(\.accessibilityIdentifier) == [ |
| 17 | + "tabbar_mysites", "tabbar_reader", "tabbar_notifications", "tabbar_me" |
| 18 | + ] |
| 19 | + ) |
| 20 | + #expect(controller.mode == .tabBar) |
| 21 | + |
| 22 | + controller.showReaderTab() |
| 23 | + #expect(controller.selectedTab?.viewController === controller.readerNavigationController) |
| 24 | + controller.showNotificationsTab() |
| 25 | + #expect(controller.selectedTab?.viewController === controller.notificationsNavigationController) |
| 26 | + controller.showMeTab() |
| 27 | + #expect(controller.selectedTab?.viewController === controller.meNavigationController) |
| 28 | + controller.showMySitesTab() |
| 29 | + #expect(controller.selectedTab?.viewController === controller.mySitesCoordinator.rootViewController) |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + @available(iOS 26, *) |
| 34 | + @Test func programmaticSelectionUpdatesMinimization() throws { |
| 35 | + try withController { controller in |
| 36 | + controller.showReaderTab() |
| 37 | + #expect(controller.tabBarMinimizeBehavior == .onScrollDown) |
| 38 | + controller.showMySitesTab() |
| 39 | + #expect(controller.tabBarMinimizeBehavior == .never) |
| 40 | + controller.selectedTab = try #require(controller.tab(forAppTab: .reader)) |
| 41 | + #expect(controller.tabBarMinimizeBehavior == .onScrollDown) |
| 42 | + controller.showMeTab() |
| 43 | + #expect(controller.tabBarMinimizeBehavior == .never) |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + @Test func replacingNotificationsPreservesOtherTabsAndTheirStacks() throws { |
| 48 | + try withController { controller in |
| 49 | + let originalTabs = controller.tabs |
| 50 | + let detail = UIViewController() |
| 51 | + controller.readerNavigationController.pushViewController(detail, animated: false) |
| 52 | + controller.showNotificationsTab() |
| 53 | + |
| 54 | + controller.shouldUseStaticNotifications = false |
| 55 | + |
| 56 | + #expect(controller.selectedAppTab == .notifications) |
| 57 | + #expect(controller.tabs[2] !== originalTabs[2]) |
| 58 | + for index in [0, 1, 3] { |
| 59 | + #expect(controller.tabs[index] === originalTabs[index]) |
| 60 | + } |
| 61 | + #expect(controller.readerNavigationController.topViewController === detail) |
| 62 | + #expect(controller.selectedTab?.viewController === controller.notificationsNavigationController) |
| 63 | + #expect(controller.notificationsViewController != nil) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @Test func signInRebuildsAccountTabsAndSelectsMySite() throws { |
| 68 | + try withController { controller in |
| 69 | + let originalTabs = controller.tabs |
| 70 | + controller.showMeTab() |
| 71 | + |
| 72 | + NotificationCenter.default.post( |
| 73 | + name: NSNotification.Name(rawValue: WordPressAuthenticationManager.WPSigninDidFinishNotification), |
| 74 | + object: nil |
| 75 | + ) |
| 76 | + |
| 77 | + #expect(controller.selectedAppTab == .mySites) |
| 78 | + #expect(controller.tabs[0] === originalTabs[0]) |
| 79 | + for index in [1, 2, 3] { |
| 80 | + #expect(controller.tabs[index] !== originalTabs[index]) |
| 81 | + } |
| 82 | + #expect(controller.selectedTab?.viewController === controller.mySitesCoordinator.rootViewController) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Test func nativeSelectionTracksAccessWithoutTrackingReselection() throws { |
| 87 | + try withController(staticReader: false) { controller in |
| 88 | + let window = show(controller) |
| 89 | + defer { window.isHidden = true } |
| 90 | + TestAnalyticsTracker.setup() |
| 91 | + defer { TestAnalyticsTracker.tearDown() } |
| 92 | + let reader = try #require(controller.tab(forAppTab: .reader)) |
| 93 | + let delegate = try #require(controller.delegate) |
| 94 | + |
| 95 | + #expect(delegate.tabBarController?(controller, shouldSelectTab: reader) == true) |
| 96 | + #expect(readerAccessCount == 1) |
| 97 | + |
| 98 | + controller.selectedTab = reader |
| 99 | + #expect(delegate.tabBarController?(controller, shouldSelectTab: reader) == true) |
| 100 | + #expect(readerAccessCount == 1) |
| 101 | + |
| 102 | + controller.trackTabAccessOnAppDidBecomeActive() |
| 103 | + #expect(readerAccessCount == 2) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + @Test func readerPosterDoesNotTrackReaderAccess() throws { |
| 108 | + try withController { controller in |
| 109 | + let window = show(controller) |
| 110 | + defer { window.isHidden = true } |
| 111 | + TestAnalyticsTracker.setup() |
| 112 | + defer { TestAnalyticsTracker.tearDown() } |
| 113 | + |
| 114 | + let reader = try #require(controller.tab(forAppTab: .reader)) |
| 115 | + #expect(controller.delegate?.tabBarController?(controller, shouldSelectTab: reader) == true) |
| 116 | + controller.selectedTab = reader |
| 117 | + controller.trackTabAccessOnAppDidBecomeActive() |
| 118 | + #expect(readerAccessCount == 0) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + @Test func notificationIndicatorUpdatesTheNativeTab() throws { |
| 123 | + try withController { controller in |
| 124 | + let notifications = try #require(controller.tab(forAppTab: .notifications)) |
| 125 | + notifications.image = UIImage(systemName: "pencil") |
| 126 | + notifications.accessibilityLabel = "Pending update" |
| 127 | + |
| 128 | + controller.updateNotificationBadgeVisibility() |
| 129 | + controller.viewDidAppear(false) |
| 130 | + |
| 131 | + #expect(notifications.image?.pngData() == UIImage(named: "tab-bar-notifications")?.pngData()) |
| 132 | + #expect(notifications.accessibilityLabel == "Notifications") |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + @Test func unreadActivityUpdatesTheNativeTabAfterAppearance() throws { |
| 137 | + let defaults = UserPersistentStoreFactory.userDefaultsInstance() |
| 138 | + let key = "welcomeNotificationSeen" |
| 139 | + let original = defaults.object(forKey: key) |
| 140 | + defaults.set(false, forKey: key) |
| 141 | + defer { defaults.set(original, forKey: key) } |
| 142 | + |
| 143 | + try withController { controller in |
| 144 | + controller.shouldUseStaticNotifications = false |
| 145 | + let notifications = try #require(controller.tab(forAppTab: .notifications)) |
| 146 | + controller.viewDidAppear(false) |
| 147 | + |
| 148 | + let unreadImage = try #require(UIImage(named: "tab-bar-notifications-unread")) |
| 149 | + #expect(notifications.image?.pngData() == unreadImage.pngData()) |
| 150 | + #expect(notifications.image?.renderingMode == .alwaysOriginal) |
| 151 | + #expect(notifications.accessibilityLabel == "Notifications Unread") |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + @Test func orientationFollowsTheSelectedNativeTab() throws { |
| 156 | + try withController { controller in |
| 157 | + let landscape = UITab(title: "Landscape", image: nil, identifier: "landscape") { _ in |
| 158 | + LandscapeViewController() |
| 159 | + } |
| 160 | + controller.tabs = [landscape] |
| 161 | + controller.selectedTab = landscape |
| 162 | + #expect(controller.supportedInterfaceOrientations == .landscape) |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + @Test func tabProvidersDoNotRetainTheTabBarController() throws { |
| 167 | + weak var releasedController: WPTabBarController? |
| 168 | + try autoreleasepool { |
| 169 | + try withController { controller in |
| 170 | + releasedController = controller |
| 171 | + for tab in controller.tabs { |
| 172 | + _ = tab.viewController |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + #expect(releasedController == nil) |
| 177 | + } |
| 178 | + |
| 179 | + private var readerAccessCount: Int { |
| 180 | + TestAnalyticsTracker.tracked.filter { $0.stat == .readerAccessed }.count |
| 181 | + } |
| 182 | + |
| 183 | + private func withController( |
| 184 | + staticReader: Bool = true, |
| 185 | + _ body: (WPTabBarController) throws -> Void |
| 186 | + ) throws { |
| 187 | + let original = ContextManager.overrideInstance |
| 188 | + let context = ContextManager.forTesting() |
| 189 | + ContextManager.overrideInstance = context |
| 190 | + defer { |
| 191 | + context.mainContext.reset() |
| 192 | + ContextManager.overrideInstance = original |
| 193 | + } |
| 194 | + try body(WPTabBarController(staticReader: staticReader, staticNotifications: true)) |
| 195 | + } |
| 196 | + |
| 197 | + private func show(_ controller: UIViewController) -> UIWindow { |
| 198 | + let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 390, height: 844)) |
| 199 | + window.rootViewController = controller |
| 200 | + window.isHidden = false |
| 201 | + window.layoutIfNeeded() |
| 202 | + return window |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | +private final class LandscapeViewController: UIViewController { |
| 207 | + override var supportedInterfaceOrientations: UIInterfaceOrientationMask { .landscape } |
| 208 | +} |
0 commit comments