Skip to content

Commit

Permalink
"Clear This History.." for today burns windows (#2745)
Browse files Browse the repository at this point in the history
Task/Issue URL:
https://app.asana.com/0/1177771139624306/1207195823454499/f

**Description**:
When user selects "Clear This History" from the history menu to clear
history for today, we have to burn windows too.
  • Loading branch information
tomasstrba authored May 8, 2024
1 parent 1c65660 commit 17047bd
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 6 deletions.
12 changes: 11 additions & 1 deletion DuckDuckGo/Fire/Model/Fire.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ final class Fire {
@MainActor
func burnVisits(of visits: [Visit],
except fireproofDomains: FireproofDomains,
isToday: Bool,
completion: (() -> Void)? = nil) {

// Get domains to burn
Expand All @@ -254,7 +255,16 @@ final class Fire {
domains = domains.convertedToETLDPlus1(tld: tld)

historyCoordinating.burnVisits(visits) {
self.burnEntity(entity: .none(selectedDomains: domains),
let entity: BurningEntity

// Burn all windows in case we are burning visits for today
if isToday {
entity = .allWindows(mainWindowControllers: self.windowControllerManager.mainWindowControllers, selectedDomains: domains)
} else {
entity = .none(selectedDomains: domains)
}

self.burnEntity(entity: entity,
includingHistory: false,
completion: completion)
}
Expand Down
41 changes: 38 additions & 3 deletions DuckDuckGo/Menus/CleanThisHistoryMenuItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,48 @@ import History

final class ClearThisHistoryMenuItem: NSMenuItem {

enum HistoryTimeWindow {
case today
case other(dateString: String)

var isToday: Bool {
switch self {
case .today:
return true
case .other:
return false
}
}

var dateString: String? {
switch self {
case .today:
return nil
case .other(let dateString):
return dateString
}
}

init(dateString: String?) {
if let dateString {
self = .other(dateString: dateString)
} else {
self = .today
}
}
}

// Keep the dateString for alerts so we don't need to use the formatter again
func setDateString(_ dateString: String?) {
representedObject = dateString
func setRepresentingObject(historyTimeWindow: HistoryTimeWindow) {
representedObject = historyTimeWindow
}

var dateString: String? {
representedObject as? String
(representedObject as? HistoryTimeWindow)?.dateString
}

var isToday: Bool {
(representedObject as? HistoryTimeWindow)?.isToday ?? false
}

// Getting visits for the whole menu section in order to perform burning
Expand Down
3 changes: 2 additions & 1 deletion DuckDuckGo/Menus/HistoryMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ final class HistoryMenu: NSMenu {
let headerItem = ClearThisHistoryMenuItem(title: UserText.clearThisHistoryMenuItem,
action: #selector(AppDelegate.clearThisHistory(_:)),
keyEquivalent: "")
headerItem.setDateString(dateString)
let historyTimeWindow = ClearThisHistoryMenuItem.HistoryTimeWindow(dateString: dateString)
headerItem.setRepresentingObject(historyTimeWindow: historyTimeWindow)
return [
headerItem,
.separator()
Expand Down
5 changes: 4 additions & 1 deletion DuckDuckGo/Menus/MainMenuActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -482,13 +482,16 @@ extension MainViewController {
}

let dateString = sender.dateString
let isToday = sender.isToday
let visits = sender.getVisits()
let alert = NSAlert.clearHistoryAndDataAlert(dateString: dateString)
alert.beginSheetModal(for: window, completionHandler: { response in
guard case .alertFirstButtonReturn = response else {
return
}
FireCoordinator.fireViewModel.fire.burnVisits(of: visits, except: FireproofDomains.shared)
FireCoordinator.fireViewModel.fire.burnVisits(of: visits,
except: FireproofDomains.shared,
isToday: isToday)
})
}

Expand Down
73 changes: 73 additions & 0 deletions UnitTests/Fire/Model/FireTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,79 @@ final class FireTests: XCTestCase {
XCTAssertFalse(appStateRestorationManager.canRestoreLastSessionState)
}

func testWhenBurnVisitIsCalledForTodayThenAllExistingTabsAreCleared() {
let manager = WebCacheManagerMock()
let historyCoordinator = HistoryCoordinatingMock()
let permissionManager = PermissionManagerMock()
let faviconManager = FaviconManagerMock()
let recentlyClosedCoordinator = RecentlyClosedCoordinatorMock()

let fire = Fire(cacheManager: manager,
historyCoordinating: historyCoordinator,
permissionManager: permissionManager,
windowControllerManager: WindowControllersManager.shared,
faviconManagement: faviconManager,
recentlyClosedCoordinator: recentlyClosedCoordinator,
tld: ContentBlocking.shared.tld)
let tabCollectionViewModel = TabCollectionViewModel.makeTabCollectionViewModel()
_ = WindowsManager.openNewWindow(with: tabCollectionViewModel, lazyLoadTabs: true)
XCTAssertNotEqual(tabCollectionViewModel.allTabsCount, 0)

let finishedBurningExpectation = expectation(description: "Finished burning")
fire.burnVisits(of: [],
except: FireproofDomains.shared,
isToday: true,
completion: {
finishedBurningExpectation.fulfill()
})

waitForExpectations(timeout: 5)
XCTAssertEqual(tabCollectionViewModel.allTabsCount, 0)
XCTAssert(manager.clearCalled)
XCTAssert(historyCoordinator.burnVisitsCalled)
XCTAssertFalse(historyCoordinator.burnAllCalled)
XCTAssert(permissionManager.burnPermissionsOfDomainsCalled)
XCTAssertFalse(permissionManager.burnPermissionsCalled)
XCTAssert(recentlyClosedCoordinator.burnCacheCalled)
}

func testWhenBurnVisitIsCalledForOtherDayThenExistingTabsRemainOpen() {
let manager = WebCacheManagerMock()
let historyCoordinator = HistoryCoordinatingMock()
let permissionManager = PermissionManagerMock()
let faviconManager = FaviconManagerMock()
let recentlyClosedCoordinator = RecentlyClosedCoordinatorMock()

let fire = Fire(cacheManager: manager,
historyCoordinating: historyCoordinator,
permissionManager: permissionManager,
windowControllerManager: WindowControllersManager.shared,
faviconManagement: faviconManager,
recentlyClosedCoordinator: recentlyClosedCoordinator,
tld: ContentBlocking.shared.tld)
let tabCollectionViewModel = TabCollectionViewModel.makeTabCollectionViewModel()
_ = WindowsManager.openNewWindow(with: tabCollectionViewModel, lazyLoadTabs: true)
XCTAssertNotEqual(tabCollectionViewModel.allTabsCount, 0)
let numberOfTabs = tabCollectionViewModel.allTabsCount

let finishedBurningExpectation = expectation(description: "Finished burning")
fire.burnVisits(of: [],
except: FireproofDomains.shared,
isToday: false,
completion: {
finishedBurningExpectation.fulfill()
})

waitForExpectations(timeout: 5)
XCTAssertEqual(tabCollectionViewModel.allTabsCount, numberOfTabs)
XCTAssert(manager.clearCalled)
XCTAssert(historyCoordinator.burnVisitsCalled)
XCTAssertFalse(historyCoordinator.burnAllCalled)
XCTAssert(permissionManager.burnPermissionsOfDomainsCalled)
XCTAssertFalse(permissionManager.burnPermissionsCalled)
XCTAssert(recentlyClosedCoordinator.burnCacheCalled)
}

func preparePersistedState(withFileName fileName: String) -> FileStore {
let fileStore = FileStoreMock()
let state = SavedStateMock()
Expand Down

0 comments on commit 17047bd

Please sign in to comment.