Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fine tune unit tests to improve reliability #3577

Merged
merged 18 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions Core/DataStoreWarmup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ public class DataStoreWarmup {

}

private class BlockingNavigationDelegate: NSObject, WKNavigationDelegate {
public class BlockingNavigationDelegate: NSObject, WKNavigationDelegate {

var finished: PassthroughSubject? = PassthroughSubject<Void, Never>()

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction) async -> WKNavigationActionPolicy {
public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction) async -> WKNavigationActionPolicy {
return .allow
}

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
if let finished {
finished.send()
self.finished = nil
Expand All @@ -59,7 +59,7 @@ private class BlockingNavigationDelegate: NSObject, WKNavigationDelegate {
}
}

func webViewWebContentProcessDidTerminate(_ webView: WKWebView) {
public func webViewWebContentProcessDidTerminate(_ webView: WKWebView) {
Pixel.fire(pixel: .webKitDidTerminateDuringWarmup)

if let finished {
Expand All @@ -71,7 +71,7 @@ private class BlockingNavigationDelegate: NSObject, WKNavigationDelegate {
}

var cancellable: AnyCancellable?
func waitForLoad() async {
public func waitForLoad() async {
await withCheckedContinuation { continuation in
cancellable = finished?.sink { _ in
continuation.resume()
Expand All @@ -80,10 +80,16 @@ private class BlockingNavigationDelegate: NSObject, WKNavigationDelegate {
}

@MainActor
func loadInBackgroundWebView(url: URL) async {
public func prepareWebView() -> WKWebView {
let config = WKWebViewConfiguration.persistent()
let webView = WKWebView(frame: .zero, configuration: config)
webView.navigationDelegate = self
return webView
}

@MainActor
public func loadInBackgroundWebView(url: URL) async {
let webView = prepareWebView()
let request = URLRequest(url: url)
webView.load(request)
await waitForLoad()
Expand Down
318 changes: 281 additions & 37 deletions DuckDuckGo.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

20 changes: 17 additions & 3 deletions DuckDuckGo.xcodeproj/xcshareddata/xcschemes/DuckDuckGo.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
</CommandLineArguments>
<Testables>
<TestableReference
skipped = "NO">
skipped = "NO"
parallelizable = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "84E341A51E2F7EFB00BDBA6F"
Expand All @@ -77,7 +78,8 @@
</SkippedTests>
</TestableReference>
<TestableReference
skipped = "NO">
skipped = "NO"
parallelizable = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "85D33FCA25C97B6E002B91A6"
Expand All @@ -87,7 +89,8 @@
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO">
skipped = "NO"
parallelizable = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "WaitlistTests"
Expand All @@ -96,6 +99,17 @@
ReferencedContainer = "container:LocalPackages/Waitlist">
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO"
parallelizable = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "984249BD2CED4F430071C7DB"
BuildableName = "WebViewUnitTests.xctest"
BlueprintName = "WebViewUnitTests"
ReferencedContainer = "container:DuckDuckGo.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
Expand Down
17 changes: 15 additions & 2 deletions DuckDuckGo.xcodeproj/xcshareddata/xcschemes/UnitTests.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
codeCoverageEnabled = "YES">
<Testables>
<TestableReference
skipped = "NO">
skipped = "NO"
parallelizable = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "84E341A51E2F7EFB00BDBA6F"
Expand All @@ -34,7 +35,8 @@
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO">
skipped = "NO"
parallelizable = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "85D33FCA25C97B6E002B91A6"
Expand All @@ -43,6 +45,17 @@
ReferencedContainer = "container:DuckDuckGo.xcodeproj">
</BuildableReference>
</TestableReference>
<TestableReference
skipped = "NO"
parallelizable = "NO">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "984249BD2CED4F430071C7DB"
BuildableName = "WebViewUnitTests.xctest"
BlueprintName = "WebViewUnitTests"
ReferencedContainer = "container:DuckDuckGo.xcodeproj">
</BuildableReference>
</TestableReference>
</Testables>
</TestAction>
<LaunchAction
Expand Down
12 changes: 12 additions & 0 deletions DuckDuckGo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,19 @@ import os.log
_ = DefaultUserAgentManager.shared
Database.shared.loadStore { _, _ in }
_ = BookmarksDatabaseSetup().loadStoreAndMigrate(bookmarksDatabase: bookmarksDatabase)

window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = UIStoryboard.init(name: "LaunchScreen", bundle: nil).instantiateInitialViewController()

let blockingDelegate = BlockingNavigationDelegate()
let webView = blockingDelegate.prepareWebView()
window?.rootViewController?.view.addSubview(webView)
window?.rootViewController?.view.backgroundColor = .red
webView.frame = CGRect(x: 10, y: 10, width: 300, height: 300)

let request = URLRequest(url: URL(string: "about:blank")!)
webView.load(request)

return true
}

Expand Down
8 changes: 6 additions & 2 deletions DuckDuckGoTests/AppConfigurationFetchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ class AppConfigurationFetchTests: XCTestCase {

let testGroupName = "configurationFetchTestGroup"

override func setUp() {
super.setUp()
override func setUpWithError() throws {
#if !targetEnvironment(simulator)
throw XCTSkip("Ignore when ran on a device")
#endif

try super.setUpWithError()

UserDefaults(suiteName: testGroupName)?.removePersistentDomain(forName: testGroupName)
}

Expand Down
3 changes: 2 additions & 1 deletion DuckDuckGoTests/ContextualDaxDialogsFactoryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ final class ContextualDaxDialogsFactoryTests: XCTestCase {
private var window: UIWindow!

override func setUpWithError() throws {
throw XCTSkip("Potentially flaky")
try super.setUpWithError()
delegate = ContextualOnboardingDelegateMock()
settingsMock = ContextualOnboardingSettingsMock()
Expand All @@ -48,7 +49,7 @@ final class ContextualDaxDialogsFactoryTests: XCTestCase {
}

override func tearDownWithError() throws {
window.isHidden = true
window?.isHidden = true
window = nil
delegate = nil
settingsMock = nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ class ContextualOnboardingNewTabDialogFactoryTests: XCTestCase {
var onDismissCalled: Bool!
var window: UIWindow!

override func setUp() {
super.setUp()
override func setUpWithError() throws {
throw XCTSkip("Potentially flaky")
try super.setUpWithError()
mockDelegate = CapturingOnboardingNavigationDelegate()
contextualOnboardingLogicMock = ContextualOnboardingLogicMock()
onboardingManagerMock = OnboardingManagerMock()
Expand All @@ -51,7 +52,7 @@ class ContextualOnboardingNewTabDialogFactoryTests: XCTestCase {
}

override func tearDown() {
window.isHidden = true
window?.isHidden = true
window = nil
factory = nil
mockDelegate = nil
Expand Down
6 changes: 3 additions & 3 deletions DuckDuckGoTests/DebouncerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ final class DebouncerTests: XCTestCase {
expectation.fulfill()
}

wait(for: [expectation], timeout: 0.1)
wait(for: [expectation], timeout: 1.0)
}

func testWhenCancelThenCancelBlockExecution() {
Expand All @@ -58,7 +58,7 @@ final class DebouncerTests: XCTestCase {
// WHEN
sut.cancel()

wait(for: [expectation], timeout: 0.1)
wait(for: [expectation], timeout: 1.0)
}

func testWhenDebounceTwoBlocksThenCancelFirstTaskWhenSecondBlockIsScheduled() {
Expand All @@ -76,6 +76,6 @@ final class DebouncerTests: XCTestCase {
secondTaskExpectation.fulfill()
}

wait(for: [firstTaskExpectation, secondTaskExpectation], timeout: 0.1)
wait(for: [firstTaskExpectation, secondTaskExpectation], timeout: 1.0)
}
}
4 changes: 2 additions & 2 deletions DuckDuckGoTests/FavoritesListInteractingAdapterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ final class FavoritesListInteractingAdapterTests: XCTestCase {

NotificationCenter.default.post(name: AppUserDefaults.Notifications.favoritesDisplayModeChange, object: nil)

wait(for: [expectation], timeout: 0.1)
wait(for: [expectation], timeout: 1.0)
}

func testPublishesUpdateOnExternalListUpdate() {
Expand All @@ -69,7 +69,7 @@ final class FavoritesListInteractingAdapterTests: XCTestCase {

publisher.send()

wait(for: [expectation], timeout: 0.1)
wait(for: [expectation], timeout: 1.0)
}

private func createSUT() -> FavoritesListInteractingAdapter {
Expand Down
9 changes: 7 additions & 2 deletions DuckDuckGoTests/FireButtonReferenceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ final class FireButtonReferenceTests: XCTestCase {

@MainActor
func testClearDataUsingLegacyContainer() async throws {

// Using WKWebsiteDataStore(forIdentifier:) doesn't persist cookies in a testable way, so use the legacy container here.
let fireproofing = UserDefaultsFireproofing.shared
fireproofing.clearAll()
Expand All @@ -63,11 +64,15 @@ final class FireButtonReferenceTests: XCTestCase {

let cookieStorage = CookieStorage()

let warmup = WebViewWarmupHelper()

for test in referenceTests {
let cookie = try XCTUnwrap(cookie(for: test))

let warmup = DataStoreWarmup()
await warmup.ensureReady(applicationState: .unknown)
let warmupCompleted = XCTestExpectation(description: "Warmup Completed")
warmup.warmupWebView(expectation: warmupCompleted)
samsymons marked this conversation as resolved.
Show resolved Hide resolved

await fulfillment(of: [warmupCompleted], timeout: 5)

let cookieStore = WKWebsiteDataStore.default().httpCookieStore
await cookieStore.setCookie(cookie)
Expand Down
Loading
Loading