Skip to content

Commit

Permalink
mock the observations data
Browse files Browse the repository at this point in the history
  • Loading branch information
brindy committed Nov 26, 2024
1 parent 7ec9cde commit 5de2087
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//

Check failure on line 1 in Core/ObservationsDataClearing.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Header comments should be consistent with project patterns (file_header)
// WebCacheManager+ObservationsDataClearing.swift
// ObservationsDataCleaning.swift
// DuckDuckGo
//
// Copyright © 2024 DuckDuckGo. All rights reserved.
Expand All @@ -21,17 +21,25 @@ import Common
import GRDB
import os.log

extension WebCacheManager {
public protocol ObservationsDataCleaning {

func removeObservationsData() {
func removeObservationsData() async

}

/// Used by data clearing. Has no unit tests just now because getting the observation data is flakey, so we inject this for testing.
public class DefaultObservationsDataCleaner: ObservationsDataCleaning {

public init() { }

public func removeObservationsData() async {
if let pool = getValidDatabasePool() {
removeObservationsData(from: pool)
} else {
Logger.general.debug("Could not find valid pool to clear observations data")
}
}

/// Only has internal so that it can be accessed by test
func getValidDatabasePool() -> DatabasePool? {
let bundleID = Bundle.main.bundleIdentifier ?? ""

Expand Down Expand Up @@ -62,4 +70,5 @@ extension WebCacheManager {
Pixel.fire(pixel: .debugCannotClearObservationsDatabase, error: error)
}
}

}
7 changes: 5 additions & 2 deletions Core/WebCacheManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,18 @@ public class WebCacheManager: WebsiteDataManaging {
let fireproofing: Fireproofing
let dataStoreIdManager: DataStoreIdManaging
let dataStoreCleaner: WebsiteDataStoreCleaning
let observationsCleaner: ObservationsDataCleaning

public init(cookieStorage: MigratableCookieStorage,
fireproofing: Fireproofing,
dataStoreIdManager: DataStoreIdManaging,
dataStoreCleaner: WebsiteDataStoreCleaning = DefaultWebsiteDataStoreCleaner()) {
dataStoreCleaner: WebsiteDataStoreCleaning = DefaultWebsiteDataStoreCleaner(),
observationsCleaner: ObservationsDataCleaning = DefaultObservationsDataCleaner()) {
self.cookieStorage = cookieStorage
self.fireproofing = fireproofing
self.dataStoreIdManager = dataStoreIdManager
self.dataStoreCleaner = dataStoreCleaner
self.observationsCleaner = observationsCleaner
}

/// The previous version saved cookies externally to the data so we can move them between containers. We now use
Expand Down Expand Up @@ -174,7 +177,7 @@ extension WebCacheManager {
await clearDataForSafelyRemovableDataTypes(fromStore: dataStore)
await clearFireproofableDataForNonFireproofDomains(fromStore: dataStore, usingFireproofing: fireproofing)
await clearCookiesForNonFireproofedDomains(fromStore: dataStore, usingFireproofing: fireproofing)
self.removeObservationsData()
await observationsCleaner.removeObservationsData()

let totalTime = CACurrentMediaTime() - startTime
Pixel.fire(pixel: .clearDataInDefaultPersistence(.init(number: totalTime)))
Expand Down
12 changes: 6 additions & 6 deletions DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@
85C2970A247EB7AA0063A335 /* Text.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 85C29709247EB7AA0063A335 /* Text.xcassets */; };
85C2971A248162CA0063A335 /* DaxOnboardingPadViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 85C29719248162CA0063A335 /* DaxOnboardingPadViewController.swift */; };
85C503F92CEF78C10075DF6F /* DataStoreIdManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 85C503F82CEF78C00075DF6F /* DataStoreIdManager.swift */; };
85C503FB2CF0ADCE0075DF6F /* WebCacheManager+ObservationsDataClearing.swift in Sources */ = {isa = PBXBuildFile; fileRef = 85C503FA2CF0ADBD0075DF6F /* WebCacheManager+ObservationsDataClearing.swift */; };
85C503FB2CF0ADCE0075DF6F /* ObservationsDataClearing.swift in Sources */ = {isa = PBXBuildFile; fileRef = 85C503FA2CF0ADBD0075DF6F /* ObservationsDataClearing.swift */; };
85C503FD2CF0E7B10075DF6F /* MockFireproofing.swift in Sources */ = {isa = PBXBuildFile; fileRef = 85C503FC2CF0E7B10075DF6F /* MockFireproofing.swift */; };
85C503FF2CF0F4DC0075DF6F /* MockWebsiteDataManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 85C503FE2CF0F4D60075DF6F /* MockWebsiteDataManager.swift */; };
85C8E61D2B0E47380029A6BD /* BookmarksDatabaseSetup.swift in Sources */ = {isa = PBXBuildFile; fileRef = 85C8E61C2B0E47380029A6BD /* BookmarksDatabaseSetup.swift */; };
Expand Down Expand Up @@ -1887,7 +1887,7 @@
85C29709247EB7AA0063A335 /* Text.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Text.xcassets; sourceTree = "<group>"; };
85C29719248162CA0063A335 /* DaxOnboardingPadViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DaxOnboardingPadViewController.swift; sourceTree = "<group>"; };
85C503F82CEF78C00075DF6F /* DataStoreIdManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataStoreIdManager.swift; sourceTree = "<group>"; };
85C503FA2CF0ADBD0075DF6F /* WebCacheManager+ObservationsDataClearing.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "WebCacheManager+ObservationsDataClearing.swift"; sourceTree = "<group>"; };
85C503FA2CF0ADBD0075DF6F /* ObservationsDataClearing.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ObservationsDataClearing.swift; sourceTree = "<group>"; };
85C503FC2CF0E7B10075DF6F /* MockFireproofing.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockFireproofing.swift; sourceTree = "<group>"; };
85C503FE2CF0F4D60075DF6F /* MockWebsiteDataManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockWebsiteDataManager.swift; sourceTree = "<group>"; };
85C8E61C2B0E47380029A6BD /* BookmarksDatabaseSetup.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BookmarksDatabaseSetup.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -6031,19 +6031,19 @@
F143C3311E4A9A6A00CFDE3A /* Web */ = {
isa = PBXGroup;
children = (
85AB84C02CF5DDAF007E679F /* WebsiteDataStoreCleaning.swift */,
85C503FA2CF0ADBD0075DF6F /* WebCacheManager+ObservationsDataClearing.swift */,
85A1B3B120C6CD9900C18F15 /* MigratableCookieStorage.swift */,
85C503F82CEF78C00075DF6F /* DataStoreIdManager.swift */,
8596C30C2B7EB1800058EF90 /* DataStoreWarmup.swift */,
85BDC3132434D8F80053DB07 /* DebugUserScript.swift */,
850559CF23CF647C0055C0D5 /* Fireproofing.swift */,
4B60ACA0252EC0B100E8D219 /* FullScreenVideoUserScript.swift */,
85BDC3182436161C0053DB07 /* LoginFormDetectionUserScript.swift */,
85A1B3B120C6CD9900C18F15 /* MigratableCookieStorage.swift */,
85C503FA2CF0ADBD0075DF6F /* ObservationsDataClearing.swift */,
4B75EA9126A266CB00018634 /* PrintingUserScript.swift */,
988F3DCE237D5C0F00AEE34C /* SchemeHandler.swift */,
836A941C247F23C600BF8EF5 /* UserAgentManager.swift */,
F1A886771F29394E0096251E /* WebCacheManager.swift */,
85AB84C02CF5DDAF007E679F /* WebsiteDataStoreCleaning.swift */,
83004E7F2193BB8200DA013C /* WKNavigationExtension.swift */,
830381BF1F850AAF00863075 /* WKWebViewConfigurationExtension.swift */,
);
Expand Down Expand Up @@ -8420,7 +8420,7 @@
1D8F727F2BA86D8000E31493 /* PixelExperiment.swift in Sources */,
56D8556C2BEA91C4009F9698 /* SyncAlertsPresenting.swift in Sources */,
37FD780F2A29E28B00B36DB1 /* SyncErrorHandler.swift in Sources */,
85C503FB2CF0ADCE0075DF6F /* WebCacheManager+ObservationsDataClearing.swift in Sources */,
85C503FB2CF0ADCE0075DF6F /* ObservationsDataClearing.swift in Sources */,
85F21DC621145DD5002631A6 /* global.swift in Sources */,
372A0FF02B2389590033BF7F /* SyncMetricsEventsHandler.swift in Sources */,
F41C2DA326C1925700F9A760 /* BookmarksAndFolders.xcdatamodeld in Sources */,
Expand Down
25 changes: 17 additions & 8 deletions DuckDuckGoTests/WebCacheManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class WebCacheManagerTests: XCTestCase {
lazy var fireproofing = MockFireproofing()
lazy var dataStoreIdManager = DataStoreIdManager(store: dataStoreIdStore)
let dataStoreCleaner = MockDataStoreCleaner()
let observationsCleaner = MockObservationsCleaner()

override func setUp() {
super.setUp()
Expand Down Expand Up @@ -77,7 +78,6 @@ class WebCacheManagerTests: XCTestCase {
XCTAssertTrue(cookies.contains(where: { $0.domain == ".example.com" }))
}

@available(iOS 17, *)
func test_WhenClearingDataAfterUsingContainer_ThenCookiesAreMigratedAndOldContainersAreRemoved() async {
// Mock having a single container so we can validate cleaning it gets called
dataStoreCleaner.countContainersReturnValue = 1
Expand Down Expand Up @@ -110,7 +110,6 @@ class WebCacheManagerTests: XCTestCase {
XCTAssertEqual(1, dataStoreCleaner.removeAllContainersAfterDelayCalls[0])
}

@available(iOS 17, *)
func test_WhenClearingData_ThenOldContainersAreRemoved() async {
// Mock existence of 5 containers so we can validate that cleaning it is called even without migrations
dataStoreCleaner.countContainersReturnValue = 5
Expand All @@ -119,11 +118,10 @@ class WebCacheManagerTests: XCTestCase {
XCTAssertEqual(5, dataStoreCleaner.removeAllContainersAfterDelayCalls[0])
}

/// Temporarily disabled.
@MainActor
func x_testWhenAccessingObservationsDbThenValidDatabasePoolIsReturned() {
let pool = makeWebCacheManager().getValidDatabasePool()
XCTAssertNotNil(pool, "DatabasePool should not be nil")
func test_WhenClearingData_ThenObservationsDatabaseIsCleared() async {
XCTAssertEqual(0, observationsCleaner.removeObservationsDataCallCount)
await makeWebCacheManager().clear(dataStore: .default())
XCTAssertEqual(1, observationsCleaner.removeObservationsDataCallCount)
}

@MainActor
Expand All @@ -132,7 +130,8 @@ class WebCacheManagerTests: XCTestCase {
cookieStorage: cookieStorage,
fireproofing: fireproofing,
dataStoreIdManager: dataStoreIdManager,
dataStoreCleaner: dataStoreCleaner
dataStoreCleaner: dataStoreCleaner,
observationsCleaner: observationsCleaner
)
}
}
Expand All @@ -151,3 +150,13 @@ class MockDataStoreCleaner: WebsiteDataStoreCleaning {
}

}

class MockObservationsCleaner: ObservationsDataCleaning {

var removeObservationsDataCallCount = 0

func removeObservationsData() async {
removeObservationsDataCallCount += 1
}

}

0 comments on commit 5de2087

Please sign in to comment.