Skip to content

Commit

Permalink
fix flaky bookmark store tests (#3431)
Browse files Browse the repository at this point in the history
  • Loading branch information
mallexxx authored Oct 25, 2024
1 parent 3d3de16 commit 9758708
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 87 deletions.
6 changes: 6 additions & 0 deletions DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1854,6 +1854,8 @@
848648A22C76F4B20082282D /* BookmarksBarMenuViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 848648A02C76F4B20082282D /* BookmarksBarMenuViewController.swift */; };
84B49F0D2CB10F0900FF08BB /* OHHTTPStubs in Frameworks */ = {isa = PBXBuildFile; productRef = 84B49F0C2CB10F0900FF08BB /* OHHTTPStubs */; };
84B49F0F2CB10F0900FF08BB /* OHHTTPStubsSwift in Frameworks */ = {isa = PBXBuildFile; productRef = 84B49F0E2CB10F0900FF08BB /* OHHTTPStubsSwift */; };
84B479082CCA7A3E00F40329 /* Logger+UnitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84B479072CCA7A3900F40329 /* Logger+UnitTests.swift */; };
84B479092CCA7A3E00F40329 /* Logger+UnitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84B479072CCA7A3900F40329 /* Logger+UnitTests.swift */; };
84DC715A2C1C1E9000033B8C /* UserDefaultsWrapperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84DC71582C1C1E8A00033B8C /* UserDefaultsWrapperTests.swift */; };
84DC715B2C1C1E9000033B8C /* UserDefaultsWrapperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84DC71582C1C1E8A00033B8C /* UserDefaultsWrapperTests.swift */; };
84DDB90A2C92B66E008C997B /* WKVisitedLinkStoreWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84DDB9092C92B667008C997B /* WKVisitedLinkStoreWrapper.swift */; };
Expand Down Expand Up @@ -3957,6 +3959,7 @@
844D7DA32C9443E500BE61D4 /* NSPrintInfoExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NSPrintInfoExtension.swift; sourceTree = "<group>"; };
84537A022C998C24008723BC /* FireWindowSession.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FireWindowSession.swift; sourceTree = "<group>"; };
848648A02C76F4B20082282D /* BookmarksBarMenuViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BookmarksBarMenuViewController.swift; sourceTree = "<group>"; };
84B479072CCA7A3900F40329 /* Logger+UnitTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Logger+UnitTests.swift"; sourceTree = "<group>"; };
84DC71582C1C1E8A00033B8C /* UserDefaultsWrapperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserDefaultsWrapperTests.swift; sourceTree = "<group>"; };
84DDB9092C92B667008C997B /* WKVisitedLinkStoreWrapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WKVisitedLinkStoreWrapper.swift; sourceTree = "<group>"; };
84F1C8CE2C7705B500716446 /* BookmarksBarMenuPopover.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BookmarksBarMenuPopover.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -8565,6 +8568,7 @@
AAEC74B92642E66600C2EFBC /* Extensions */ = {
isa = PBXGroup;
children = (
84B479072CCA7A3900F40329 /* Logger+UnitTests.swift */,
4B4F72EB266B2ED300814C60 /* CollectionExtension.swift */,
1DFAB51F2A89830D00A0F7F6 /* SetExtensionTests.swift */,
B65349A9265CF45000DCC645 /* DispatchQueueExtensionsTests.swift */,
Expand Down Expand Up @@ -11749,6 +11753,7 @@
317295D32AF058D3002C3206 /* MockWaitlistTermsAndConditionsActionHandler.swift in Sources */,
9F0FFFB52BCCAE37007C87DD /* BookmarkAllTabsDialogCoordinatorViewModelTests.swift in Sources */,
3706FE34293F661700E42796 /* PermissionStoreTests.swift in Sources */,
84B479092CCA7A3E00F40329 /* Logger+UnitTests.swift in Sources */,
3706FE35293F661700E42796 /* ThirdPartyBrowserTests.swift in Sources */,
1DFAB5232A8983E100A0F7F6 /* SetExtensionTests.swift in Sources */,
56A054002C1AEFA1007D8FAB /* OnboardingManagerTests.swift in Sources */,
Expand Down Expand Up @@ -13156,6 +13161,7 @@
buildActionMask = 2147483647;
files = (
1DB9617A29F1D06D00CF5568 /* InternalUserDeciderMock.swift in Sources */,
84B479082CCA7A3E00F40329 /* Logger+UnitTests.swift in Sources */,
9833913327AAAEEE00DAF119 /* EmbeddedTrackerDataTests.swift in Sources */,
3776583127F8325B009A6B35 /* AutofillPreferencesTests.swift in Sources */,
373B2F852C387B830013A94B /* ActiveRemoteMessageModelTests.swift in Sources */,
Expand Down
6 changes: 5 additions & 1 deletion DuckDuckGo/Bookmarks/Services/BookmarkStoreMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import Bookmarks
import Foundation

final class BookmarkStoreMock: BookmarkStore {
final class BookmarkStoreMock: BookmarkStore, CustomDebugStringConvertible {

private let store: LocalBookmarkStore?

Expand Down Expand Up @@ -266,6 +266,10 @@ final class BookmarkStoreMock: BookmarkStore {
parent()
}
}

var debugDescription: String {
return "<\(type(of: self)) \(Unmanaged.passUnretained(self).toOpaque()): \(store.map { "\($0)" } ?? "<nil>")>"
}
}

#endif
3 changes: 1 addition & 2 deletions DuckDuckGo/Bookmarks/Services/LocalBookmarkStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ final class LocalBookmarkStore: BookmarkStore {
let fetchResults = (try? context.fetch(fetchRequest)) ?? []

if fetchResults.count != identifiers.count {
assertionFailure("\(#file): Fetched bookmark entities didn't match the number of provided UUIDs")
assertionFailure("\(self): Fetched bookmark entities \(fetchResults) didn't match the number of provided UUIDs \(identifiers)")
}

for object in fetchResults {
Expand Down Expand Up @@ -1107,7 +1107,6 @@ final class LocalBookmarkStore: BookmarkStore {
}

private func parent(for entity: BaseBookmarkEntity, in context: NSManagedObjectContext) throws -> BookmarkEntity {
let parentEntity: BookmarkEntity
if let parentId = entity.parentFolderUUID, parentId != PseudoFolder.bookmarks.id, parentId != "bookmarks_root",
let parentFetchRequestResult = try? context.fetch(BaseBookmarkEntity.singleEntity(with: parentId)).first {
return parentFetchRequestResult
Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGo/Menus/MainMenuActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ extension MainViewController {
eventMapping: EventMapping<AutofillPixelEvent> { _, _, _, _ in },
installDate: nil)
autofillPixelReporter.resetStoreDefaults()
var loginImportState = AutofillLoginImportState()
let loginImportState = AutofillLoginImportState()
loginImportState.hasImportedLogins = false
loginImportState.isCredentialsImportPromptPermanantlyDismissed = false
}
Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGo/YoutubePlayer/DuckPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ final class DuckPlayer {
let userValues = encodeUserValues()

/// If the user clicked on "Watch on Youtube" the next vide should open directly on youtube instead of displaying the overlay
var allowFirstVideo = shouldOpenNextVideoOnYoutube
let allowFirstVideo = shouldOpenNextVideoOnYoutube

/// Reset the flag for subsequent videos
shouldOpenNextVideoOnYoutube = false
Expand Down
15 changes: 9 additions & 6 deletions IntegrationTests/Tab/AddressBarTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,10 @@ class AddressBarTests: XCTestCase {
}
for (idx, tab) in viewModel.tabs.enumerated() {
viewModel.select(tab: tab)
try await Task.sleep(interval: 0.01)
for _ in 0..<10 {
guard addressBarValue != "tab-\(idx)" else { continue }
try await Task.sleep(interval: 0.01)
}
XCTAssertEqual(addressBarValue, "tab-\(idx)")
if tab.content == .newtab {
XCTAssertTrue(isAddressBarFirstResponder, "\(idx)")
Expand Down Expand Up @@ -872,7 +875,7 @@ class AddressBarTests: XCTestCase {
// GIVEN
let expectedImage = NSImage(named: "Shield")!
let evaluator = MockCertificateEvaluator()
let tab = Tab(content: .url(.duckDuckGo, credential: nil, source: .userEntered("")), certificateTrustEvaluator: evaluator)
let tab = Tab(content: .url(.duckDuckGo, credential: nil, source: .userEntered("")), webViewConfiguration: webViewConfiguration, certificateTrustEvaluator: evaluator)
let viewModel = TabCollectionViewModel(tabCollection: TabCollection(tabs: [tab]))
let tabLoadedPromise = tab.webViewDidFinishNavigationPublisher.timeout(5).first().promise()

Expand All @@ -891,7 +894,7 @@ class AddressBarTests: XCTestCase {
let expectedImage = NSImage(named: "Shield")!
let evaluator = MockCertificateEvaluator()
evaluator.isValidCertificate = true
let tab = Tab(content: .url(.duckDuckGo, credential: nil, source: .userEntered("")), certificateTrustEvaluator: evaluator)
let tab = Tab(content: .url(.duckDuckGo, credential: nil, source: .userEntered("")), webViewConfiguration: webViewConfiguration, certificateTrustEvaluator: evaluator)
let viewModel = TabCollectionViewModel(tabCollection: TabCollection(tabs: [tab]))
let tabLoadedPromise = tab.webViewDidFinishNavigationPublisher.timeout(5).first().promise()

Expand All @@ -910,7 +913,7 @@ class AddressBarTests: XCTestCase {
let expectedImage = NSImage(named: "ShieldDot")!
let evaluator = MockCertificateEvaluator()
evaluator.isValidCertificate = false
let tab = Tab(content: .url(.duckDuckGo, credential: nil, source: .userEntered("")), certificateTrustEvaluator: evaluator)
let tab = Tab(content: .url(.duckDuckGo, credential: nil, source: .userEntered("")), webViewConfiguration: webViewConfiguration, certificateTrustEvaluator: evaluator)
let viewModel = TabCollectionViewModel(tabCollection: TabCollection(tabs: [tab]))
let tabLoadedPromise = tab.webViewDidFinishNavigationPublisher.timeout(5).first().promise()

Expand All @@ -926,7 +929,7 @@ class AddressBarTests: XCTestCase {
@MainActor
func test_ZoomLevelNonDefault_ThenZoomButtonIsVisible() async throws {
// GIVEN
let tab = Tab(content: .url(.duckDuckGo, credential: nil, source: .userEntered("")))
let tab = Tab(content: .url(.duckDuckGo, credential: nil, source: .userEntered("")), webViewConfiguration: webViewConfiguration)
let viewModel = TabCollectionViewModel(tabCollection: TabCollection(tabs: [tab]))
viewModel.selectedTabViewModel?.zoomWasSet(to: .percent150)
let tabLoadedPromise = tab.webViewDidFinishNavigationPublisher.timeout(5).first().promise()
Expand All @@ -943,7 +946,7 @@ class AddressBarTests: XCTestCase {
@MainActor
func test_ZoomLevelDefault_ThenZoomButtonIsNotVisible() async throws {
// GIVEN
let tab = Tab(content: .url(.duckDuckGo, credential: nil, source: .userEntered("")))
let tab = Tab(content: .url(.duckDuckGo, credential: nil, source: .userEntered("")), webViewConfiguration: webViewConfiguration)
tab.webView.zoomLevel = AccessibilityPreferences.shared.defaultPageZoom
let viewModel = TabCollectionViewModel(tabCollection: TabCollection(tabs: [tab]))
viewModel.selectedTabViewModel?.zoomWasSet(to: .percent100)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,12 @@ final class AutofillCredentialsImportManagerTests: XCTestCase {
}

func testOnAutofillUserScriptShouldDisplayOverlay_SerializedInputContextWithCredentialsImportFalse_returnsTrue() {
let serializedInputContext = "{\"inputType\":\"credentials.username\",\"credentialsImport\":false}"
let result = manager.autofillUserScriptShouldDisplayOverlay("", for: "fill.dev")

XCTAssertTrue(result)
}

func testOnAutofillUserScriptShouldDisplayOverlay_SerializedInputContextWithCredentialsImportTrue_PromptHasNOTBeenPermanantlyDismissed_returnsTrue() {
let serializedInputContext = "{\"inputType\":\"credentials.username\",\"credentialsImport\":false}"
importState.isCredentialsImportPromptPermanantlyDismissed = false
let result = manager.autofillUserScriptShouldDisplayOverlay("", for: "fill.dev")

Expand Down
Loading

0 comments on commit 9758708

Please sign in to comment.