Skip to content

Commit

Permalink
[Tests] - Bump up the coverage numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
PravinPK committed Jun 24, 2021
1 parent 4ce875a commit 7bb24df
Show file tree
Hide file tree
Showing 13 changed files with 446 additions and 7 deletions.
8 changes: 8 additions & 0 deletions AEPAssurance.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
B6407206262C90FA00C43183 /* Assurance+PublicAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6407205262C90FA00C43183 /* Assurance+PublicAPI.swift */; };
B6407223262CEBC600C43183 /* URL+Parser.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6407222262CEBC600C43183 /* URL+Parser.swift */; };
B640722B262CF06100C43183 /* AssuranceEnvironment.swift in Sources */ = {isa = PBXBuildFile; fileRef = B640722A262CF06100C43183 /* AssuranceEnvironment.swift */; };
B64212B326849021007343B6 /* MockPinPad.swift in Sources */ = {isa = PBXBuildFile; fileRef = B64212B226849021007343B6 /* MockPinPad.swift */; };
B64212B52684A655007343B6 /* AssuranceConnectionError.swift in Sources */ = {isa = PBXBuildFile; fileRef = B64212B42684A655007343B6 /* AssuranceConnectionError.swift */; };
B64FDF6E264F33EF00D33192 /* AssurancePlugin.swift in Sources */ = {isa = PBXBuildFile; fileRef = B64FDF6D264F33EF00D33192 /* AssurancePlugin.swift */; };
B64FDF70264F341600D33192 /* PluginFakeEvent.swift in Sources */ = {isa = PBXBuildFile; fileRef = B64FDF6F264F341600D33192 /* PluginFakeEvent.swift */; };
B64FDF73264F384700D33192 /* PluginFakeEventTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = B64FDF71264F384400D33192 /* PluginFakeEventTests.swift */; };
Expand Down Expand Up @@ -219,6 +221,8 @@
B6407205262C90FA00C43183 /* Assurance+PublicAPI.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Assurance+PublicAPI.swift"; sourceTree = "<group>"; };
B6407222262CEBC600C43183 /* URL+Parser.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "URL+Parser.swift"; sourceTree = "<group>"; };
B640722A262CF06100C43183 /* AssuranceEnvironment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AssuranceEnvironment.swift; sourceTree = "<group>"; };
B64212B226849021007343B6 /* MockPinPad.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockPinPad.swift; sourceTree = "<group>"; };
B64212B42684A655007343B6 /* AssuranceConnectionError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AssuranceConnectionError.swift; sourceTree = "<group>"; };
B64FDF6D264F33EF00D33192 /* AssurancePlugin.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AssurancePlugin.swift; sourceTree = "<group>"; };
B64FDF6F264F341600D33192 /* PluginFakeEvent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PluginFakeEvent.swift; sourceTree = "<group>"; };
B64FDF71264F384400D33192 /* PluginFakeEventTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PluginFakeEventTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -453,6 +457,7 @@
B6D6A016265D8863005042BE /* AssuranceClientInfoTests.swift */,
B6D6A01F265F086A005042BE /* AssuranceBlobTests.swift */,
B63ADA8D266F718900A1281A /* ErrorViewTests.swift */,
B64212B42684A655007343B6 /* AssuranceConnectionError.swift */,
B63EB79C2668D524005BE2C5 /* AssuranceClientLogMessageTests.swift */,
B64071A9262C313E00C43183 /* Info.plist */,
);
Expand Down Expand Up @@ -598,6 +603,7 @@
B6D6A028265FA0EE005042BE /* MockAssuranceUIUtil.swift */,
B63ADABB26731A0400A1281A /* MockSocket.swift */,
B6DBD81F2678FB19009EB47E /* MockStatusUI.swift */,
B64212B226849021007343B6 /* MockPinPad.swift */,
);
path = Mocks;
sourceTree = "<group>";
Expand Down Expand Up @@ -1137,6 +1143,7 @@
B6CECC682655A94F000D517F /* ThreadSafeQueueTests.swift in Sources */,
B6AFD58E2634041E00C63608 /* AssuranceEventTests.swift in Sources */,
B6D6A01D265F05F8005042BE /* MockNetworkService.swift in Sources */,
B64212B326849021007343B6 /* MockPinPad.swift in Sources */,
B6DBD8202678FB19009EB47E /* MockStatusUI.swift in Sources */,
B6AB2DF326337BE700FE8AEA /* AssuranceStateTests.swift in Sources */,
B6D2A91026322D6D0062AC5E /* AssuranceEnvironmentTests.swift in Sources */,
Expand All @@ -1149,6 +1156,7 @@
B63EB79D2668D524005BE2C5 /* AssuranceClientLogMessageTests.swift in Sources */,
B6D6A02A265FAAA6005042BE /* MockUIService.swift in Sources */,
B6CECC5E26545834000D517F /* MockAssurance.swift in Sources */,
B64212B52684A655007343B6 /* AssuranceConnectionError.swift in Sources */,
B6AB2DEE26337BB700FE8AEA /* MockDataStore.swift in Sources */,
B6CECC6026545857000D517F /* PluginHubTests.swift in Sources */,
B6D6A017265D8863005042BE /* AssuranceClientInfoTests.swift in Sources */,
Expand Down
1 change: 1 addition & 0 deletions AEPAssurance/Source/AssuranceSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ class AssuranceSession {
// since we don't give retry option for these errors and UI will be dismissed anyway, hence notify plugins for onSessionTerminated
if !error.info.shouldRetry {
clearSessionData()
statusUI.remove()
pluginHub.notifyPluginsOnSessionTerminated()
}
}
Expand Down
2 changes: 1 addition & 1 deletion AEPAssurance/Source/Socket/WebView/WebViewSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import WebKit

class WebViewSocket: NSObject, SocketConnectable, WKNavigationDelegate, WKScriptMessageHandler {

var delegate: SocketDelegate
weak var delegate: SocketDelegate
var socketURL: URL?

/// variable tracking the current socket status
Expand Down
8 changes: 8 additions & 0 deletions AEPAssurance/UnitTests/AssuranceConnectionError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// AssuranceConnectionError.swift
// UnitTests
//
// Created by pprakash on 6/24/21.
//

import Foundation
205 changes: 201 additions & 4 deletions AEPAssurance/UnitTests/AssuranceSessionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ class AssuranceSessionTests: XCTestCase {

let runtime = TestableExtensionRuntime()
var session: AssuranceSession!
var assuranceExtension: Assurance!
var assuranceExtension: MockAssurance!
var mockSocket: MockSocket!
var mockStatusUI: MockStatusUI!
var mockPinPad: MockPinPad!
let mockUIService = MockUIService()
let mockMessagePresentable = MockFullscreenMessagePresentable()
let mockPlugin = PluginTaco()
Expand All @@ -35,11 +36,47 @@ class AssuranceSessionTests: XCTestCase {
mockSocket = MockSocket(withDelegate: session)
mockStatusUI = MockStatusUI(withSession: session)
session.socket = mockSocket
mockPinPad = MockPinPad(withExtension: assuranceExtension)
}

override func tearDown() {
}

func test_startSession() throws {
// setup
assuranceExtension.connectedWebSocketURL = nil

// test
session.startSession()

// verify
XCTAssertTrue(mockUIService.createFullscreenMessageCalled)
XCTAssertTrue(mockMessagePresentable.showCalled)
}

func test_startSession_whenAlreadyConnected() throws {
// setup
mockSocket.socketState = .open

// test
session.startSession()

// verify
XCTAssertFalse(mockUIService.createFullscreenMessageCalled)
}

func test_startSession_whenConnectionURLExist() throws {
// setup
assuranceExtension.connectedWebSocketURL = "wss://socket/connection"

// test
session.startSession()

// verify
XCTAssertTrue(mockSocket.connectCalled)
XCTAssertEqual("wss://socket/connection", mockSocket.connectURL?.absoluteString)
}

func test_session_RegistersPlugins() throws {
// verify that 3 internal plugins are registered
XCTAssertEqual(4, session.pluginHub.pluginCollection[AssuranceConstants.Vendor.MOBILE.hash]?.count)
Expand Down Expand Up @@ -139,6 +176,36 @@ class AssuranceSessionTests: XCTestCase {
XCTAssertEqual(0, session.inboundQueue.size())
}

func test_session_receives_startForwardingEvent() throws {
// setup
session.pluginHub.registerPlugin(mockPlugin, toSession: session)
session.statusUI = mockStatusUI
mockPlugin.expectation = XCTestExpectation(description: "Calls SessionConnected delegate method on plugin")

// test
session.webSocket(mockSocket, didReceiveEvent: startForwardingEvent)

// verify
wait(for: [mockPlugin.expectation!], timeout: 1.0)
XCTAssertTrue(session.canStartForwarding)
XCTAssertTrue(mockStatusUI.displayCalled)
XCTAssertTrue(mockStatusUI.updateForSocketConnectedCalled)
XCTAssertTrue(mockPlugin.isSessionConnectedCalled)
}

func test_session_receives_startForwardingEvent_AfterBootEventsAreCleared() throws {
// setup
session.didClearBootEvent = true
assuranceExtension.expectation = XCTestExpectation(description: "Calls extension to get the shared state events")

// test
session.webSocket(mockSocket, didReceiveEvent: startForwardingEvent)

// verify
wait(for: [assuranceExtension.expectation!], timeout: 2.0)
XCTAssertTrue(assuranceExtension.getAllExtensionStateDataCalled)
}

func test_session_addsClientLogs() throws {
// setup
session.statusUI = mockStatusUI
Expand Down Expand Up @@ -170,16 +237,146 @@ class AssuranceSessionTests: XCTestCase {
XCTAssertEqual(AssuranceConstants.DEFAULT_ENVIRONMENT, assuranceExtension.environment)
}

private func sampleAssuranceEvent() -> AssuranceEvent {
return AssuranceEvent(type: "sampleType", payload: nil)
func test_session_whenConnected_sendsClientInfoEvent() throws {
// test
session.webSocketDidConnect(mockSocket)

// verify
XCTAssertTrue(mockSocket.sendEventCalled)
XCTAssertEqual(AssuranceConstants.EventType.CLIENT, mockSocket.sentEvent?.type)
}

func test_session_whenWebSocketOnError() throws {
// test
XCTAssertNoThrow(session.webSocketOnError(mockSocket))
}

func test_session_whenSocketStateOpen_savesWebSocketURL() throws {
// setup
let sampleURL = URL(string: "https://adobe.com")
mockSocket.socketURL = sampleURL

// test
session.webSocket(mockSocket, didChangeState: .open)

// verify
XCTAssertEqual(sampleURL?.absoluteString, assuranceExtension.connectedWebSocketURL)
}

func test_session_whenSocketDisconnect_NormalClosure() throws {
// setup
session.pluginHub.registerPlugin(mockPlugin, toSession: session)
session.statusUI = mockStatusUI
session.pinCodeScreen = mockPinPad

// test
session.webSocketDidDisconnect(mockSocket, AssuranceConstants.SocketCloseCode.NORMAL_CLOSURE, "Normal Closure", true)

// verify
XCTAssertTrue(mockStatusUI.removeCalled)
XCTAssertTrue(mockPlugin.isSessionDisconnectCalled)
XCTAssertTrue(mockPinPad.connectionFinishedCalled)
}

private func startForwardingEvent() -> AssuranceEvent {
func test_session_whenSocketDisconnect_OrgMismatch() throws {
// setup
session.pluginHub.registerPlugin(mockPlugin, toSession: session)
session.statusUI = mockStatusUI
mockPinPad.isDisplayed = true
session.pinCodeScreen = mockPinPad
assuranceExtension.sessionId = "sampleSessionID"
assuranceExtension.connectedWebSocketURL = "url://with/sampleSessionID"

// test
session.webSocketDidDisconnect(mockSocket, AssuranceConstants.SocketCloseCode.ORG_MISMATCH, "", true)

// verify
XCTAssertTrue(mockStatusUI.removeCalled)
XCTAssertTrue(mockPinPad.connectionFailedWithErrorCalled)
XCTAssertEqual(AssuranceConnectionError.orgIDMismatch, mockPinPad.connectionFailedWithErrorValue)
XCTAssertFalse(session.canStartForwarding)
XCTAssertNil(assuranceExtension.sessionId)
XCTAssertNil(assuranceExtension.connectedWebSocketURL)
XCTAssertEqual(AssuranceConstants.DEFAULT_ENVIRONMENT, assuranceExtension.environment)
}

func test_session_whenSocketDisconnect_ConnectionLimit() throws {
// setup
session.statusUI = mockStatusUI
mockPinPad.isDisplayed = true
session.pinCodeScreen = mockPinPad

// test
session.webSocketDidDisconnect(mockSocket, AssuranceConstants.SocketCloseCode.CONNECTION_LIMIT, "", true)

// verify
XCTAssertTrue(mockStatusUI.removeCalled)
XCTAssertTrue(mockPinPad.connectionFailedWithErrorCalled)
XCTAssertEqual(AssuranceConnectionError.connectionLimit, mockPinPad.connectionFailedWithErrorValue)
}

func test_session_whenSocketDisconnect_EventLimit() throws {
// setup
session.statusUI = mockStatusUI
mockPinPad.isDisplayed = true
session.pinCodeScreen = mockPinPad

// test
session.webSocketDidDisconnect(mockSocket, AssuranceConstants.SocketCloseCode.EVENTS_LIMIT, "", true)

// verify
XCTAssertTrue(mockStatusUI.removeCalled)
XCTAssertTrue(mockPinPad.connectionFailedWithErrorCalled)
XCTAssertEqual(AssuranceConnectionError.eventLimit, mockPinPad.connectionFailedWithErrorValue)
}

func test_session_whenSocketDisconnect_ClientError() throws {
// setup
session.statusUI = mockStatusUI
mockPinPad.isDisplayed = true
session.pinCodeScreen = mockPinPad

// test
session.webSocketDidDisconnect(mockSocket, AssuranceConstants.SocketCloseCode.CLIENT_ERROR, "", true)

// verify
XCTAssertTrue(mockStatusUI.removeCalled)
XCTAssertTrue(mockPinPad.connectionFailedWithErrorCalled)
XCTAssertEqual(AssuranceConnectionError.clientError, mockPinPad.connectionFailedWithErrorValue)
}

func test_session_whenSocketDisconnect_AbnormalClosure() throws {
// setup
let sampleSocketURL = "wss://socketURL"
mockSocket.expectation = XCTestExpectation(description: "Attempts to reconnect")
session.statusUI = mockStatusUI
mockPinPad.isDisplayed = true
session.pinCodeScreen = mockPinPad
assuranceExtension.connectedWebSocketURL = sampleSocketURL

// test
session.webSocketDidDisconnect(mockSocket, AssuranceConstants.SocketCloseCode.ABNORMAL_CLOSURE, "", true)

// verify
wait(for: [mockSocket.expectation!], timeout: 2.0)
XCTAssertTrue(session.isAttemptingToReconnect)
XCTAssertFalse(session.canStartForwarding)
XCTAssertTrue(mockPinPad.connectionFailedWithErrorCalled)
XCTAssertTrue(mockSocket.connectCalled)
XCTAssertEqual(sampleSocketURL, mockSocket.connectURL?.absoluteString)

}

private func sampleAssuranceEvent() -> AssuranceEvent {
return AssuranceEvent(type: "sampleType", payload: nil)
}

private var tacoEvent: AssuranceEvent {
return AssuranceEvent(type: "control", payload: ["type": "Taco"], vendor: "Food")
}

private var startForwardingEvent: AssuranceEvent {
return AssuranceEvent(type: "control", payload: ["type": AnyCodable.init(AssuranceConstants.CommandType.START_EVENT_FORWARDING)], vendor: AssuranceConstants.Vendor.MOBILE)
}

}
Loading

0 comments on commit 7bb24df

Please sign in to comment.