Revision control
Copy as Markdown
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
import XCTest
@testable import WebEngine
final class AdsTelemetryContentScriptTests: XCTestCase {
private var adsTelemetryDelegate: MockAdsTelemetryDelegate!
override func setUp() {
super.setUp()
adsTelemetryDelegate = MockAdsTelemetryDelegate()
}
override func tearDown() {
super.tearDown()
adsTelemetryDelegate = nil
}
func testDidReceiveMessageGivenEmptyMessageThenNoDelegateCalled() {
let subject = AdsTelemetryContentScript(delegate: adsTelemetryDelegate)
subject.userContentController(didReceiveMessage: [])
XCTAssertEqual(adsTelemetryDelegate.trackAdsFoundOnPageCalled, 0)
XCTAssertEqual(adsTelemetryDelegate.trackAdsClickedOnPageCalled, 0)
}
func testDidReceiveMessageWithNoAdURLMatchThenNoDelegateCalled() {
let subject = AdsTelemetryContentScript(delegate: adsTelemetryDelegate)
subject.userContentController(didReceiveMessage: [
"cookies": [["name": "ABCDEFGH", "value": "cookie_val"]],
])
XCTAssertEqual(adsTelemetryDelegate.trackAdsFoundOnPageCalled, 0)
XCTAssertEqual(adsTelemetryDelegate.trackAdsClickedOnPageCalled, 0)
}
func testDidReceiveMessageWithBasicURLMatchButNoAdURLsThenNoDelegateCalled() {
let subject = AdsTelemetryContentScript(delegate: adsTelemetryDelegate)
subject.userContentController(didReceiveMessage: [
"cookies": [["name": "ABCDEFGH", "value": "cookie_val"]],
])
XCTAssertEqual(adsTelemetryDelegate.trackAdsFoundOnPageCalled, 0)
XCTAssertEqual(adsTelemetryDelegate.trackAdsClickedOnPageCalled, 0)
}
func testDidReceiveMessageWithBasicURLMatchAndMatchingAdURLsThenTrackOnPageCalled() {
let subject = AdsTelemetryContentScript(delegate: adsTelemetryDelegate)
subject.userContentController(didReceiveMessage: [
"cookies": [["name": "ABCDEFGH", "value": "cookie_val"]],
])
XCTAssertEqual(adsTelemetryDelegate.trackAdsFoundOnPageCalled, 1)
XCTAssertEqual(adsTelemetryDelegate.trackAdsClickedOnPageCalled, 0)
}
func testDidReceiveMessageWithBasicURLMatchAndMatchingAdURLsThenTrackOnPageCalledAndURLsSent() {
let subject = AdsTelemetryContentScript(delegate: adsTelemetryDelegate)
subject.userContentController(didReceiveMessage: [
"cookies": [["name": "ABCDEFGH", "value": "cookie_val"]],
])
XCTAssertEqual(adsTelemetryDelegate.trackAdsFoundOnPageCalled, 1)
XCTAssertEqual(adsTelemetryDelegate.savedTrackAdsOnPageURLs?.count ?? 0, 2)
XCTAssertEqual(adsTelemetryDelegate.savedTrackAdsOnPageProviderName, "mocksearch")
XCTAssertEqual(adsTelemetryDelegate.trackAdsClickedOnPageCalled, 0)
}
}