From f37cc5f6cf2c883dc48f6218c9b3328f9e05e86e Mon Sep 17 00:00:00 2001 From: Diego Rey Mendez Date: Thu, 18 Apr 2024 10:33:03 +0200 Subject: [PATCH] Fixes the unit tests for NetworkProtectionIPCTunnelController and adds more tests --- ...NetworkProtectionIPCTunnelController.swift | 2 +- .../PixelKitMock.swift | 5 +- ...rkProtectionIPCTunnelControllerTests.swift | 52 ++++++++++++++++--- .../NetworkProtectionTestingSupport.swift | 17 +++--- 4 files changed, 61 insertions(+), 15 deletions(-) diff --git a/DuckDuckGo/NetworkProtection/AppTargets/DeveloperIDTarget/NetworkProtectionIPCTunnelController.swift b/DuckDuckGo/NetworkProtection/AppTargets/DeveloperIDTarget/NetworkProtectionIPCTunnelController.swift index 09843e888c..7e05352066 100644 --- a/DuckDuckGo/NetworkProtection/AppTargets/DeveloperIDTarget/NetworkProtectionIPCTunnelController.swift +++ b/DuckDuckGo/NetworkProtection/AppTargets/DeveloperIDTarget/NetworkProtectionIPCTunnelController.swift @@ -112,7 +112,7 @@ extension NetworkProtectionIPCTunnelController: TunnelController { func handleFailure(_ error: Error) { log(error) - pixelKit?.fire(StartAttempt.failure(error), frequency: .dailyAndCount) + pixelKit?.fire(StopAttempt.failure(error), frequency: .dailyAndCount) } do { diff --git a/LocalPackages/PixelKit/Sources/PixelKitTestingUtilities/PixelKitMock.swift b/LocalPackages/PixelKit/Sources/PixelKitTestingUtilities/PixelKitMock.swift index d2e5cdc617..dd9bf5390f 100644 --- a/LocalPackages/PixelKit/Sources/PixelKitTestingUtilities/PixelKitMock.swift +++ b/LocalPackages/PixelKit/Sources/PixelKitTestingUtilities/PixelKitMock.swift @@ -18,6 +18,7 @@ import Foundation import PixelKit +import XCTest public final class PixelKitMock: PixelFiring { @@ -42,8 +43,8 @@ public final class PixelKitMock: PixelFiring { actualFireCalls.append(fireCall) } - public var expectationsMet: Bool { - expectedFireCalls == actualFireCalls + public func verifyExpectations(file: StaticString, line: UInt) { + XCTAssertEqual(expectedFireCalls, actualFireCalls, file: file, line: line) } } diff --git a/UnitTests/NetworkProtection/NetworkProtectionIPCTunnelControllerTests.swift b/UnitTests/NetworkProtection/NetworkProtectionIPCTunnelControllerTests.swift index 178ba61b3c..b2376ee397 100644 --- a/UnitTests/NetworkProtection/NetworkProtectionIPCTunnelControllerTests.swift +++ b/UnitTests/NetworkProtection/NetworkProtectionIPCTunnelControllerTests.swift @@ -41,15 +41,18 @@ final class NetworkProtectionIPCTunnelControllerTests: XCTestCase { await controller.start() - XCTAssertTrue(pixelKit.expectationsMet) + pixelKit.verifyExpectations(file: #file, line: #line) } func testStartTunnelLoginItemFailure() async { let error = NSError(domain: "test", code: 1) + let expectedError = NetworkProtectionIPCTunnelController.RequestError.internalLoginItemError(error) + let pixelKit = PixelKitMock(expecting: [ .init(pixel: NetworkProtectionIPCTunnelController.StartAttempt.begin, frequency: .standard), - .init(pixel: NetworkProtectionIPCTunnelController.StartAttempt.failure(error), frequency: .dailyAndCount) + .init(pixel: NetworkProtectionIPCTunnelController.StartAttempt.failure(expectedError), frequency: .dailyAndCount) ]) + let controller = NetworkProtectionIPCTunnelController( featureVisibility: MockFeatureVisibility(), loginItemsManager: MockLoginItemsManager(mockResult: .failure(error)), @@ -58,7 +61,24 @@ final class NetworkProtectionIPCTunnelControllerTests: XCTestCase { await controller.start() - XCTAssertTrue(pixelKit.expectationsMet) + pixelKit.verifyExpectations(file: #file, line: #line) + } + + func testStartTunnelIPCFailure() async { + let error = NSError(domain: "test", code: 1) + let pixelKit = PixelKitMock(expecting: [ + .init(pixel: NetworkProtectionIPCTunnelController.StartAttempt.begin, frequency: .standard), + .init(pixel: NetworkProtectionIPCTunnelController.StartAttempt.failure(error), frequency: .dailyAndCount) + ]) + let controller = NetworkProtectionIPCTunnelController( + featureVisibility: MockFeatureVisibility(), + loginItemsManager: MockLoginItemsManager(mockResult: .success), + ipcClient: MockIPCClient(error: error), + pixelKit: pixelKit) + + await controller.start() + + pixelKit.verifyExpectations(file: #file, line: #line) } // MARK: - Tunnel Stop Tests @@ -76,15 +96,18 @@ final class NetworkProtectionIPCTunnelControllerTests: XCTestCase { await controller.stop() - XCTAssertTrue(pixelKit.expectationsMet) + pixelKit.verifyExpectations(file: #file, line: #line) } func testStopTunnelLoginItemFailure() async { let error = NSError(domain: "test", code: 1) + let expectedError = NetworkProtectionIPCTunnelController.RequestError.internalLoginItemError(error) + let pixelKit = PixelKitMock(expecting: [ .init(pixel: NetworkProtectionIPCTunnelController.StopAttempt.begin, frequency: .standard), - .init(pixel: NetworkProtectionIPCTunnelController.StopAttempt.failure(error), frequency: .dailyAndCount) + .init(pixel: NetworkProtectionIPCTunnelController.StopAttempt.failure(expectedError), frequency: .dailyAndCount) ]) + let controller = NetworkProtectionIPCTunnelController( featureVisibility: MockFeatureVisibility(), loginItemsManager: MockLoginItemsManager(mockResult: .failure(error)), @@ -93,6 +116,23 @@ final class NetworkProtectionIPCTunnelControllerTests: XCTestCase { await controller.stop() - XCTAssertTrue(pixelKit.expectationsMet) + pixelKit.verifyExpectations(file: #file, line: #line) + } + + func testStopTunnelIPCFailure() async { + let error = NSError(domain: "test", code: 1) + let pixelKit = PixelKitMock(expecting: [ + .init(pixel: NetworkProtectionIPCTunnelController.StopAttempt.begin, frequency: .standard), + .init(pixel: NetworkProtectionIPCTunnelController.StopAttempt.failure(error), frequency: .dailyAndCount) + ]) + let controller = NetworkProtectionIPCTunnelController( + featureVisibility: MockFeatureVisibility(), + loginItemsManager: MockLoginItemsManager(mockResult: .success), + ipcClient: MockIPCClient(error: error), + pixelKit: pixelKit) + + await controller.stop() + + pixelKit.verifyExpectations(file: #file, line: #line) } } diff --git a/UnitTests/NetworkProtection/Support/NetworkProtectionTestingSupport.swift b/UnitTests/NetworkProtection/Support/NetworkProtectionTestingSupport.swift index 8aeec1c4ff..e298ad553d 100644 --- a/UnitTests/NetworkProtection/Support/NetworkProtectionTestingSupport.swift +++ b/UnitTests/NetworkProtection/Support/NetworkProtectionTestingSupport.swift @@ -102,18 +102,23 @@ struct MockConnectionErrorObserver: ConnectionErrorObserver { } struct MockIPCClient: NetworkProtectionIPCClient { - var ipcStatusObserver: NetworkProtection.ConnectionStatusObserver = MockConnectionStatusObserver() - var ipcServerInfoObserver: NetworkProtection.ConnectionServerInfoObserver = MockServerInfoObserver() + private let error: Error? + var ipcStatusObserver: NetworkProtection.ConnectionStatusObserver = MockConnectionStatusObserver() + var ipcServerInfoObserver: NetworkProtection.ConnectionServerInfoObserver = MockServerInfoObserver() var ipcConnectionErrorObserver: NetworkProtection.ConnectionErrorObserver = MockConnectionErrorObserver() - func start() { - // Intentional no-op + init(error: Error? = nil) { + self.error = error } - func stop() { - // Intentional no-op + func start(completion: @escaping (Error?) -> Void) { + completion(error) + } + + func stop(completion: @escaping (Error?) -> Void) { + completion(error) } }