From e099febfc93fb37cca2cecdb64b93fb63c1956cd Mon Sep 17 00:00:00 2001 From: Brandon T Date: Thu, 13 Jul 2023 12:48:39 -0400 Subject: [PATCH] Fix unit tests Rebase & Fixed bug in alert --- App/iOS/Delegates/AppDelegate.swift | 2 +- .../Toolbars/UrlBar/TabLocationView.swift | 2 +- Tests/ClientTests/TabManagerTests.swift | 112 +++++++++++++++++- 3 files changed, 111 insertions(+), 5 deletions(-) diff --git a/App/iOS/Delegates/AppDelegate.swift b/App/iOS/Delegates/AppDelegate.swift index ca3126b527b..fcece9db00d 100644 --- a/App/iOS/Delegates/AppDelegate.swift +++ b/App/iOS/Delegates/AppDelegate.swift @@ -178,7 +178,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { Preferences.PrivacyReports.ntpOnboardingCompleted.value = false Preferences.Privacy.shouldShowPersistentPrivateBrowsingAlert.value = false } else { - Preferences.Privacy.shouldShowPersistentPrivateBrowsingAlert.value = false + Preferences.Privacy.shouldShowPersistentPrivateBrowsingAlert.value = true } Preferences.General.isFirstLaunch.value = false diff --git a/Sources/Brave/Frontend/Browser/Toolbars/UrlBar/TabLocationView.swift b/Sources/Brave/Frontend/Browser/Toolbars/UrlBar/TabLocationView.swift index 89ef061fa58..1fb90bcba3d 100644 --- a/Sources/Brave/Frontend/Browser/Toolbars/UrlBar/TabLocationView.swift +++ b/Sources/Brave/Frontend/Browser/Toolbars/UrlBar/TabLocationView.swift @@ -251,7 +251,7 @@ class TabLocationView: UIView { private var isVoiceSearchAvailable: Bool - init(voiceSearchSupported: Bool, privateBrowsingManager: privateBrowsingManager) { + init(voiceSearchSupported: Bool, privateBrowsingManager: PrivateBrowsingManager) { isVoiceSearchAvailable = voiceSearchSupported super.init(frame: .zero) diff --git a/Tests/ClientTests/TabManagerTests.swift b/Tests/ClientTests/TabManagerTests.swift index 7b2c34f4727..08b2df90587 100644 --- a/Tests/ClientTests/TabManagerTests.swift +++ b/Tests/ClientTests/TabManagerTests.swift @@ -5,6 +5,7 @@ @testable import Brave @testable import Data import Shared +import Preferences import Storage import UIKit import WebKit @@ -122,6 +123,14 @@ open class MockTabManagerDelegate: TabManagerDelegate { privateBrowsingManager.isPrivateBrowsing = false super.tearDown() } + + private func setPersistentPrivateMode(_ isPersistent: Bool) { + Preferences.Privacy.persistentPrivateBrowsing.value = isPersistent + + if isPersistent { + Preferences.Privacy.privateBrowsingOnly.value = false + } + } // BRAVE TODO: We no longer "store tabs", happens async from CoreData, so this test has to reflect CD instead /* @@ -211,7 +220,39 @@ open class MockTabManagerDelegate: TabManagerDelegate { delegate.verify("Not all delegate methods were called") } - func testDeletePrivateTabsOnExit() { + func testDeletePrivateTabsPersistenceOnExit() { + setPersistentPrivateMode(true) + + // create one private and one normal tab + let tab = manager.addTab(isPrivate: false) + manager.selectTab(tab) + manager.selectTab(manager.addTab(isPrivate: true)) + + XCTAssertEqual(TabType.of(manager.selectedTab).isPrivate, true, "The selected tab should be the private tab") + XCTAssertEqual(manager.tabs(withType: .private).count, 1, "There should only be one private tab") + + manager.selectTab(tab) + XCTAssertEqual(manager.tabs(withType: .private).count, 1, "If the normal tab is selected the private tab should NOT be deleted") + XCTAssertEqual(manager.tabs(withType: .regular).count, 1, "The regular tab should stil be around") + + manager.selectTab(manager.addTab(isPrivate: true)) + XCTAssertEqual(manager.tabs(withType: .private).count, 2, "There should be two private tabs") + manager.willSwitchTabMode(leavingPBM: true) + XCTAssertEqual(manager.tabs(withType: .private).count, 2, "After willSwitchTabMode there should be 2 private tabs") + + manager.selectTab(manager.addTab(isPrivate: true)) + manager.selectTab(manager.addTab(isPrivate: true)) + XCTAssertEqual(manager.tabs(withType: .private).count, 4, "Private tabs should not be deleted when another one is added") + manager.selectTab(manager.addTab(isPrivate: false)) + XCTAssertEqual(manager.tabs(withType: .private).count, 4, "But once we add a normal tab we've switched out of private mode. Private tabs should be be persistent") + XCTAssertEqual(manager.tabs(withType: .regular).count, 2, "The original normal tab and the new one should both still exist") + + setPersistentPrivateMode(false) + } + + func testDeletePrivateTabsOnNonPersistenceExit() { + setPersistentPrivateMode(false) + // create one private and one normal tab let tab = manager.addTab(isPrivate: false) manager.selectTab(tab) @@ -237,7 +278,27 @@ open class MockTabManagerDelegate: TabManagerDelegate { XCTAssertEqual(manager.tabs(withType: .regular).count, 2, "The original normal tab and the new one should both still exist") } - func testTogglePBMDelete() { + func testTogglePBMDeletePersistent() { + setPersistentPrivateMode(true) + + let tab = manager.addTab(isPrivate: false) + manager.selectTab(tab) + manager.selectTab(manager.addTab(isPrivate: false)) + manager.selectTab(manager.addTab(isPrivate: true)) + + manager.willSwitchTabMode(leavingPBM: false) + XCTAssertEqual(manager.tabs(withType: .private).count, 1, "There should be 1 private tab") + manager.willSwitchTabMode(leavingPBM: true) + XCTAssertEqual(manager.tabs(withType: .private).count, 1, "There should be 1 private tab") + manager.removeTab(tab) + XCTAssertEqual(manager.tabs(withType: .regular).count, 1, "There should be 1 normal tab") + + setPersistentPrivateMode(false) + } + + func testTogglePBMDeleteNonPersistent() { + setPersistentPrivateMode(false) + let tab = manager.addTab(isPrivate: false) manager.selectTab(tab) manager.selectTab(manager.addTab(isPrivate: false)) @@ -328,7 +389,52 @@ open class MockTabManagerDelegate: TabManagerDelegate { delegate.verify("Not all delegate methods were called") } - func testDelegatesCalledWhenRemovingPrivateTabs() { + func testDelegatesCalledWhenRemovingPrivateTabsPersistence() { + setPersistentPrivateMode(true) + + //setup + let delegate = MockTabManagerDelegate() + + // create one private and one normal tab + let tab = manager.addTab(isPrivate: false) + let newTab = manager.addTab(isPrivate: false) + manager.selectTab(tab) + manager.selectTab(manager.addTab(isPrivate: true)) + manager.addDelegate(delegate) + + // Double check a few things + XCTAssertEqual(TabType.of(manager.selectedTab).isPrivate, true, "The selected tab should be the private tab") + XCTAssertEqual(manager.tabs(withType: .private).count, 1, "There should only be one private tab") + + // switch to normal mode. Which should not delete the private tabs + manager.willSwitchTabMode(leavingPBM: true) + + //make sure tabs are NOT cleared properly and indexes are reset + XCTAssertEqual(manager.tabs(withType: .private).count, 1, "Private tab should not have been deleted") + XCTAssertNotEqual(manager.selectedIndex, -1, "The selected index should have been reset") + + // didSelect should still be called when switching between a tab + let didSelect = MethodSpy(functionName: "tabManager(_:didSelectedTabChange:previous:)") { tabs in + XCTAssertNotNil(tabs[1], "there should be a previous tab") + let next = tabs[0]! + XCTAssertFalse(next.isPrivate) + } + + // make sure delegate method is actually called + delegate.expect([didSelect]) + + // select the new tab to trigger the delegate methods + manager.selectTab(newTab) + + // check + delegate.verify("Not all delegate methods were called") + + setPersistentPrivateMode(false) + } + + func testDelegatesCalledWhenRemovingPrivateTabsNonPersistence() { + setPersistentPrivateMode(false) + //setup let delegate = MockTabManagerDelegate()