Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
quanganhdo committed Oct 8, 2024
1 parent bdb228d commit 1a32611
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,10 @@ extension DataBrokerProtectionAgentManager: DataBrokerProtectionBackgroundActivi

func startScheduledOperations(completion: (() -> Void)?) {
fireMonitoringPixels()
queueManager.startScheduledOperationsIfPermitted(showWebView: false, operationDependencies: operationDependencies) { _ in
// no-op
} completion: {
completion?()
}
queueManager.startScheduledOperationsIfPermitted(showWebView: false,
operationDependencies: operationDependencies,
errorHandler: nil,
completion: completion)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ final class DataBrokerProtectionAgentManagerTests: XCTestCase {

let scanCalledExpectation = XCTestExpectation(description: "Scan called")
var startScheduledScansCalled = false
mockQueueManager.startScheduledOperationsIfPermittedCalledCompletion = { _ in
mockQueueManager.startScheduledOperationsIfPermittedCalledCompletion = {
startScheduledScansCalled = true
scanCalledExpectation.fulfill()
}
Expand Down Expand Up @@ -209,7 +209,7 @@ final class DataBrokerProtectionAgentManagerTests: XCTestCase {
mockDataManager.profileToReturn = mockProfile

var startScheduledScansCalled = false
mockQueueManager.startScheduledOperationsIfPermittedCalledCompletion = { _ in
mockQueueManager.startScheduledOperationsIfPermittedCalledCompletion = {
startScheduledScansCalled = true
}

Expand Down Expand Up @@ -237,7 +237,7 @@ final class DataBrokerProtectionAgentManagerTests: XCTestCase {
mockDataManager.profileToReturn = mockProfile

var startImmediateScansCalled = false
mockQueueManager.startImmediateOperationsIfPermittedCalledCompletion = { _ in
mockQueueManager.startImmediateOperationsIfPermittedCalledCompletion = {
startImmediateScansCalled = true
}

Expand Down Expand Up @@ -381,7 +381,7 @@ final class DataBrokerProtectionAgentManagerTests: XCTestCase {
privacyConfigurationManager: mockPrivacyConfigurationManager)

var startScheduledScansCalled = false
mockQueueManager.startScheduledOperationsIfPermittedCalledCompletion = { _ in
mockQueueManager.startScheduledOperationsIfPermittedCalledCompletion = {
startScheduledScansCalled = true
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ final class DataBrokerProtectionQueueManagerTests: XCTestCase {
userNotificationService: mockUserNotification)
}

func testWhenStartImmediateScan_andScanCompletesWithErrors_thenCompletionIsCalledWithErrors() async throws {
func testWhenStartImmediateScan_andScanCompletesWithErrors_thenErrorHandleIsCalledWithErrors_followedByCompletionBlock() async throws {
// Given
sut = DefaultDataBrokerProtectionQueueManager(operationQueue: mockQueue,
operationsCreator: mockOperationsCreator,
Expand All @@ -63,14 +63,18 @@ final class DataBrokerProtectionQueueManagerTests: XCTestCase {
let mockOperation = MockDataBrokerOperation(id: 1, operationType: .scan, errorDelegate: sut)
let mockOperationWithError = MockDataBrokerOperation(id: 2, operationType: .scan, errorDelegate: sut, shouldError: true)
mockOperationsCreator.operationCollections = [mockOperation, mockOperationWithError]
let expectation = expectation(description: "Expected errors to be returned in completion")
let expectation = expectation(description: "Expected completion to be called")
var errorCollection: DataBrokerProtectionAgentErrorCollection!
let expectedConcurrentOperations = DataBrokerExecutionConfig().concurrentOperationsFor(.scan)
var errorHandlerCalled = false

// When
sut.startImmediateOperationsIfPermitted(showWebView: false,
operationDependencies: mockDependencies) { errors in
errorCollection = errors
errorHandlerCalled = true
} completion: {
XCTAssertTrue(errorHandlerCalled)
expectation.fulfill()
}

Expand All @@ -83,7 +87,7 @@ final class DataBrokerProtectionQueueManagerTests: XCTestCase {
XCTAssertEqual(mockQueue.maxConcurrentOperationCount, expectedConcurrentOperations)
}

func testWhenStartScheduledScan_andScanCompletesWithErrors_thenCompletionIsCalledWithErrors() async throws {
func testWhenStartScheduledScan_andScanCompletesWithErrors_thenErrorHandlerIsCalledWithErrors_followedByCompletionBlock() async throws {
// Given
sut = DefaultDataBrokerProtectionQueueManager(operationQueue: mockQueue,
operationsCreator: mockOperationsCreator,
Expand All @@ -93,14 +97,18 @@ final class DataBrokerProtectionQueueManagerTests: XCTestCase {
let mockOperation = MockDataBrokerOperation(id: 1, operationType: .scan, errorDelegate: sut)
let mockOperationWithError = MockDataBrokerOperation(id: 2, operationType: .scan, errorDelegate: sut, shouldError: true)
mockOperationsCreator.operationCollections = [mockOperation, mockOperationWithError]
let expectation = expectation(description: "Expected errors to be returned in completion")
let expectation = expectation(description: "Expected completion to be called")
var errorCollection: DataBrokerProtectionAgentErrorCollection!
let expectedConcurrentOperations = DataBrokerExecutionConfig().concurrentOperationsFor(.all)
var errorHandlerCalled = false

// When
sut.startScheduledOperationsIfPermitted(showWebView: false,
operationDependencies: mockDependencies) { errors in
errorCollection = errors
errorHandlerCalled = true
} completion: {
XCTAssertTrue(errorHandlerCalled)
expectation.fulfill()
}

Expand Down Expand Up @@ -128,6 +136,8 @@ final class DataBrokerProtectionQueueManagerTests: XCTestCase {
// When
sut.startScheduledOperationsIfPermitted(showWebView: false, operationDependencies: mockDependencies) { errors in
errorCollection = errors
} completion: {
// no-op
}

mockQueue.completeOperationsUpTo(index: 2)
Expand All @@ -140,7 +150,10 @@ final class DataBrokerProtectionQueueManagerTests: XCTestCase {
mockOperationsCreator.operationCollections = mockOperations

// When
sut.startImmediateOperationsIfPermitted(showWebView: false, operationDependencies: mockDependencies) { _ in }
sut.startImmediateOperationsIfPermitted(showWebView: false, operationDependencies: mockDependencies) { _ in
} completion: {
// no-op
}

// Then
XCTAssert(errorCollection.operationErrors?.count == 2)
Expand All @@ -166,6 +179,8 @@ final class DataBrokerProtectionQueueManagerTests: XCTestCase {
// When
sut.startImmediateOperationsIfPermitted(showWebView: false, operationDependencies: mockDependencies) { errors in
errorCollection = errors
} completion: {
// no-op
}

mockQueue.completeOperationsUpTo(index: 2)
Expand All @@ -178,7 +193,10 @@ final class DataBrokerProtectionQueueManagerTests: XCTestCase {
mockOperationsCreator.operationCollections = mockOperations

// When
sut.startImmediateOperationsIfPermitted(showWebView: false, operationDependencies: mockDependencies) { _ in }
sut.startImmediateOperationsIfPermitted(showWebView: false, operationDependencies: mockDependencies) { _ in
} completion: {
// no-op
}

// Then
XCTAssert(errorCollection.operationErrors?.count == 2)
Expand All @@ -204,6 +222,8 @@ final class DataBrokerProtectionQueueManagerTests: XCTestCase {
// When
sut.startImmediateOperationsIfPermitted(showWebView: false, operationDependencies: mockDependencies) { errors in
errorCollectionFirst = errors
} completion: {
// no-op
}

mockQueue.completeOperationsUpTo(index: 2)
Expand All @@ -220,6 +240,8 @@ final class DataBrokerProtectionQueueManagerTests: XCTestCase {
// When
sut.startImmediateOperationsIfPermitted(showWebView: false, operationDependencies: mockDependencies) { errors in
errorCollectionSecond = errors
} completion: {
// no-op
}

mockQueue.completeAllOperations()
Expand All @@ -246,7 +268,10 @@ final class DataBrokerProtectionQueueManagerTests: XCTestCase {
var errorCollection: DataBrokerProtectionAgentErrorCollection!

// When
sut.startImmediateOperationsIfPermitted(showWebView: false, operationDependencies: mockDependencies) { _ in }
sut.startImmediateOperationsIfPermitted(showWebView: false, operationDependencies: mockDependencies) { _ in
} completion: {
// no-op
}

// Then
XCTAssert(mockQueue.operationCount == 10)
Expand All @@ -264,6 +289,7 @@ final class DataBrokerProtectionQueueManagerTests: XCTestCase {
// When
sut.startScheduledOperationsIfPermitted(showWebView: false, operationDependencies: mockDependencies) { errors in
errorCollection = errors
} completion: {
completionCalled.toggle()
}

Expand All @@ -290,6 +316,7 @@ final class DataBrokerProtectionQueueManagerTests: XCTestCase {
sut.startImmediateOperationsIfPermitted(showWebView: false,
operationDependencies: mockDependencies) { errors in
errorCollection = errors
} completion: {
expectation.fulfill()
}

Expand All @@ -311,6 +338,7 @@ final class DataBrokerProtectionQueueManagerTests: XCTestCase {
// When
sut.execute(.startOptOutOperations(showWebView: false,
operationDependencies: mockDependencies,
errorHandler: nil,
completion: nil))

// Then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class DataBrokerProtectionQueueModeTests: XCTestCase {
let sut = DataBrokerProtectionQueueMode.idle

// When
let result = sut.canBeInterruptedBy(newMode: .immediate(completion: nil))
let result = sut.canBeInterruptedBy(newMode: .immediate(errorHandler: nil, completion: nil))

// Then
XCTAssertTrue(result)
Expand All @@ -37,51 +37,51 @@ final class DataBrokerProtectionQueueModeTests: XCTestCase {
let sut = DataBrokerProtectionQueueMode.idle

// When
let result = sut.canBeInterruptedBy(newMode: .scheduled(completion: nil))
let result = sut.canBeInterruptedBy(newMode: .scheduled(errorHandler: nil, completion: nil))

// Then
XCTAssertTrue(result)
}

func testCurrentModeImmediate_andNewModeImmediate_thenInterruptionAllowed() throws {
// Given
let sut = DataBrokerProtectionQueueMode.immediate(completion: nil)
let sut = DataBrokerProtectionQueueMode.immediate(errorHandler: nil, completion: nil)

// When
let result = sut.canBeInterruptedBy(newMode: .immediate(completion: { _ in }))
let result = sut.canBeInterruptedBy(newMode: .immediate(errorHandler: { _ in }, completion: {}))

// Then
XCTAssertTrue(result)
}

func testCurrentModeImmediate_andNewModeScheduled_thenInterruptionNotAllowed() throws {
// Given
let sut = DataBrokerProtectionQueueMode.immediate(completion: nil)
let sut = DataBrokerProtectionQueueMode.immediate(errorHandler: nil, completion: nil)

// When
let result = sut.canBeInterruptedBy(newMode: .scheduled(completion: nil))
let result = sut.canBeInterruptedBy(newMode: .scheduled(errorHandler: nil, completion: nil))

// Then
XCTAssertFalse(result)
}

func testCurrentModeScheduled_andNewModeImmediate_thenInterruptionAllowed() throws {
// Given
let sut = DataBrokerProtectionQueueMode.scheduled(completion: nil)
let sut = DataBrokerProtectionQueueMode.scheduled(errorHandler: nil, completion: nil)

// When
let result = sut.canBeInterruptedBy(newMode: .immediate(completion: nil))
let result = sut.canBeInterruptedBy(newMode: .immediate(errorHandler: nil, completion: nil))

// Then
XCTAssertTrue(result)
}

func testCurrentModeScheduled_andNewModeScheduled_thenInterruptionNotAllowed() throws {
// Given
let sut = DataBrokerProtectionQueueMode.scheduled(completion: nil)
let sut = DataBrokerProtectionQueueMode.scheduled(errorHandler: nil, completion: nil)

// When
let result = sut.canBeInterruptedBy(newMode: .scheduled(completion: nil))
let result = sut.canBeInterruptedBy(newMode: .scheduled(errorHandler: nil, completion: nil))

// Then
XCTAssertFalse(result)
Expand All @@ -100,7 +100,7 @@ final class DataBrokerProtectionQueueModeTests: XCTestCase {

func testWhenModeIsImmediate_thenPriorityDateIsNil() throws {
// Given
let sut = DataBrokerProtectionQueueMode.immediate(completion: nil)
let sut = DataBrokerProtectionQueueMode.immediate(errorHandler: nil, completion: nil)

// When
let result = sut.priorityDate
Expand All @@ -111,7 +111,7 @@ final class DataBrokerProtectionQueueModeTests: XCTestCase {

func testWhenModeIsScheduled_thenPriorityDateIsNotNil() throws {
// Given
let sut = DataBrokerProtectionQueueMode.scheduled(completion: nil)
let sut = DataBrokerProtectionQueueMode.scheduled(errorHandler: nil, completion: nil)

// When
let result = sut.priorityDate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1208,21 +1208,21 @@ final class MockDataBrokerProtectionOperationQueueManager: DataBrokerProtectionQ
var startImmediateOperationsIfPermittedCompletionError: DataBrokerProtectionAgentErrorCollection?
var startScheduledOperationsIfPermittedCompletionError: DataBrokerProtectionAgentErrorCollection?

var startImmediateOperationsIfPermittedCalledCompletion: ((DataBrokerProtection.DataBrokerProtectionAgentErrorCollection?) -> Void)?
var startScheduledOperationsIfPermittedCalledCompletion: ((DataBrokerProtection.DataBrokerProtectionAgentErrorCollection?) -> Void)?
var startImmediateOperationsIfPermittedCalledCompletion: (() -> Void)?
var startScheduledOperationsIfPermittedCalledCompletion: (() -> Void)?

init(operationQueue: DataBrokerProtection.DataBrokerProtectionOperationQueue, operationsCreator: DataBrokerProtection.DataBrokerOperationsCreator, mismatchCalculator: DataBrokerProtection.MismatchCalculator, brokerUpdater: DataBrokerProtection.DataBrokerProtectionBrokerUpdater?, pixelHandler: Common.EventMapping<DataBrokerProtection.DataBrokerProtectionPixels>) {

}

func startImmediateOperationsIfPermitted(showWebView: Bool, operationDependencies: DataBrokerProtection.DataBrokerOperationDependencies, completion: ((DataBrokerProtection.DataBrokerProtectionAgentErrorCollection?) -> Void)?) {
completion?(startImmediateOperationsIfPermittedCompletionError)
startImmediateOperationsIfPermittedCalledCompletion?(startImmediateOperationsIfPermittedCompletionError)
func startImmediateOperationsIfPermitted(showWebView: Bool, operationDependencies: any DataBrokerProtection.DataBrokerOperationDependencies, errorHandler: ((DataBrokerProtection.DataBrokerProtectionAgentErrorCollection?) -> Void)?, completion: (() -> Void)?) {
errorHandler?(startImmediateOperationsIfPermittedCompletionError)
startImmediateOperationsIfPermittedCalledCompletion?()
}

func startScheduledOperationsIfPermitted(showWebView: Bool, operationDependencies: DataBrokerProtection.DataBrokerOperationDependencies, completion: ((DataBrokerProtection.DataBrokerProtectionAgentErrorCollection?) -> Void)?) {
completion?(startScheduledOperationsIfPermittedCompletionError)
startScheduledOperationsIfPermittedCalledCompletion?(startScheduledOperationsIfPermittedCompletionError)
func startScheduledOperationsIfPermitted(showWebView: Bool, operationDependencies: any DataBrokerProtection.DataBrokerOperationDependencies, errorHandler: ((DataBrokerProtection.DataBrokerProtectionAgentErrorCollection?) -> Void)?, completion: (() -> Void)?) {
errorHandler?(startScheduledOperationsIfPermittedCompletionError)
startScheduledOperationsIfPermittedCalledCompletion?()
}

func execute(_ command: DataBrokerProtection.DataBrokerProtectionQueueManagerDebugCommand) {
Expand Down

0 comments on commit 1a32611

Please sign in to comment.