Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 8 additions & 0 deletions firefox-ios/Client.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1398,6 +1398,8 @@
AA24AD322E7C3A8F008659A3 /* TrendingSearchClientTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA24AD312E7C3A85008659A3 /* TrendingSearchClientTests.swift */; };
AA24AD342E7C3AD2008659A3 /* MockTrendingSearchEngine.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA24AD332E7C3ACC008659A3 /* MockTrendingSearchEngine.swift */; };
AA4D99432E857AF300BB039D /* MockRecentSearchProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA4D99422E857AEE00BB039D /* MockRecentSearchProvider.swift */; };
AA742C7A2E90358400CF55B3 /* MockMicrosurveyTelemetry.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA742C792E90357E00CF55B3 /* MockMicrosurveyTelemetry.swift */; };
AA742C7C2E904B9B00CF55B3 /* MicrosurveyTelemetryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA742C7B2E904B9500CF55B3 /* MicrosurveyTelemetryTests.swift */; };
AAB4321B2E8187190075E47F /* RecentSearchProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = AAB4321A2E8187130075E47F /* RecentSearchProvider.swift */; };
AAB4321D2E8189390075E47F /* RecentSearchProviderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = AAB4321C2E8189310075E47F /* RecentSearchProviderTests.swift */; };
AAB434062E82F0600075E47F /* MockTrendingSearchProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = AAB434052E82F05C0075E47F /* MockTrendingSearchProvider.swift */; };
Expand Down Expand Up @@ -9433,6 +9435,8 @@
AA24AD312E7C3A85008659A3 /* TrendingSearchClientTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TrendingSearchClientTests.swift; sourceTree = "<group>"; };
AA24AD332E7C3ACC008659A3 /* MockTrendingSearchEngine.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockTrendingSearchEngine.swift; sourceTree = "<group>"; };
AA4D99422E857AEE00BB039D /* MockRecentSearchProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockRecentSearchProvider.swift; sourceTree = "<group>"; };
AA742C792E90357E00CF55B3 /* MockMicrosurveyTelemetry.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockMicrosurveyTelemetry.swift; sourceTree = "<group>"; };
AA742C7B2E904B9500CF55B3 /* MicrosurveyTelemetryTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MicrosurveyTelemetryTests.swift; sourceTree = "<group>"; };
AA80494199BED7BAA77241A9 /* zh-CN */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "zh-CN"; path = "zh-CN.lproj/Storage.strings"; sourceTree = "<group>"; };
AAAB41FC97F6C8F8A1506603 /* ro */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = ro; path = ro.lproj/AuthenticationManager.strings; sourceTree = "<group>"; };
AAB4321A2E8187130075E47F /* RecentSearchProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecentSearchProvider.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -13324,6 +13328,7 @@
8A827E332C20C8C0008D5E3C /* Mock */ = {
isa = PBXGroup;
children = (
AA742C792E90357E00CF55B3 /* MockMicrosurveyTelemetry.swift */,
8A827E312C20C8AE008D5E3C /* MockMicrosurveySurfaceManager.swift */,
8A4EA0D72C01125100E4E4F1 /* MicrosurveyMockModel.swift */,
);
Expand All @@ -13344,6 +13349,7 @@
children = (
8A827E332C20C8C0008D5E3C /* Mock */,
8A4EA0D22C010BF800E4E4F1 /* MicrosurveySurfaceManagerTests.swift */,
AA742C7B2E904B9500CF55B3 /* MicrosurveyTelemetryTests.swift */,
8AAEB9FD2BF50718000C02B5 /* MicrosurveyViewControllerTests.swift */,
8A8482EE2BE15FFE00F9007B /* MicrosurveyPromptStateTests.swift */,
8AAEBA092BF53AB5000C02B5 /* MicrosurveyStateTests.swift */,
Expand Down Expand Up @@ -19202,6 +19208,7 @@
2197DF8A287624BF00215624 /* LibraryViewModelTests.swift in Sources */,
5A3A7DDC2889EC5D0065F81A /* BookmarksHandlerMock.swift in Sources */,
8015C8B52D0A31FE0093AED0 /* RecordedNimbusContextTests.swift in Sources */,
AA742C7A2E90358400CF55B3 /* MockMicrosurveyTelemetry.swift in Sources */,
2F44FA1B1A9D426A00FD20CC /* TestHashExtensions.swift in Sources */,
8A04136B2825ABEA00D20B10 /* SponsoredTileTelemetryTests.swift in Sources */,
21D8EA932ABE04F7003FF16E /* TabTrayViewControllerTests.swift in Sources */,
Expand Down Expand Up @@ -19407,6 +19414,7 @@
C83B7DD629BBB49D005565C2 /* SurveySurfaceManagerTests.swift in Sources */,
E1E6F8CE29D4B7E700068D8D /* GleanPlumbContextProviderTests.swift in Sources */,
8AC884212D5262070033ABF5 /* CrashTrackerTests.swift in Sources */,
AA742C7C2E904B9B00CF55B3 /* MicrosurveyTelemetryTests.swift in Sources */,
C8DC90D02A067C5B0008832B /* MarkupParseUtilityTests.swift in Sources */,
21EEAA1B2D3AE3B300595119 /* BookmarksTelemetryTests.swift in Sources */,
8AF3B15C2AF99C77009BB262 /* ReadingListPanelTests.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@
import Foundation
import Glean

struct MicrosurveyTelemetry {
protocol MicrosurveyTelemetryProtocol {
func surveyViewed(surveyId: String)
func privacyNoticeTapped(surveyId: String)
func dismissButtonTapped(surveyId: String)
func userResponseSubmitted(surveyId: String, userSelection: String)
func confirmationShown(surveyId: String)
}

struct MicrosurveyTelemetry: MicrosurveyTelemetryProtocol {
private let gleanWrapper: GleanWrapper

init(gleanWrapper: GleanWrapper = DefaultGleanWrapper()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import Redux
import Common

final class MicrosurveyMiddleware {
private let microsurveyTelemetry = MicrosurveyTelemetry()
private let microsurveyTelemetry: MicrosurveyTelemetryProtocol

init(microsurveyTelemetry: MicrosurveyTelemetryProtocol = MicrosurveyTelemetry()) {
self.microsurveyTelemetry = microsurveyTelemetry
}

lazy var microsurveyProvider: Middleware<AppState> = { state, action in
let windowUUID = action.windowUUID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,17 @@ import XCTest

final class MicrosurveyMiddlewareIntegrationTests: XCTestCase, StoreTestUtility {
var mockStore: MockStoreForMiddleware<AppState>!
var mockMicrosurveyTelemetry: MockMicrosurveyTelemetry!

override func setUp() {
super.setUp()
// Due to changes allow certain custom pings to implement their own opt-out
// independent of Glean, custom pings may need to be registered manually in
// tests in order to put them in a state in which they can collect data.
Glean.shared.registerPings(GleanMetrics.Pings.shared)
Glean.shared.resetGlean(clearStores: true)
mockMicrosurveyTelemetry = MockMicrosurveyTelemetry()
DependencyHelperMock().bootstrapDependencies()
setupStore()
}

override func tearDown() {
mockMicrosurveyTelemetry = nil
DependencyHelperMock().reset()
resetStore()
super.tearDown()
Expand All @@ -34,9 +32,7 @@ final class MicrosurveyMiddlewareIntegrationTests: XCTestCase, StoreTestUtility

subject.microsurveyProvider(AppState(), action)

testEventMetricRecordingSuccess(metric: GleanMetrics.Microsurvey.dismissButtonTapped)
let resultValue = try XCTUnwrap(GleanMetrics.Microsurvey.dismissButtonTapped.testGetValue())
XCTAssertEqual(resultValue[0].extra?["survey_id"], "microsurvey-id")
XCTAssertEqual(mockMicrosurveyTelemetry.dismissButtonTappedCalledCount, 1)

let actionCalled = try XCTUnwrap(mockStore.dispatchedActions.first as? MicrosurveyPromptAction)
let actionType = try XCTUnwrap(actionCalled.actionType as? MicrosurveyPromptActionType)
Expand All @@ -51,10 +47,7 @@ final class MicrosurveyMiddlewareIntegrationTests: XCTestCase, StoreTestUtility

subject.microsurveyProvider(AppState(), action)

testEventMetricRecordingSuccess(metric: GleanMetrics.Microsurvey.privacyNoticeTapped)
let resultValue = try XCTUnwrap(GleanMetrics.Microsurvey.privacyNoticeTapped.testGetValue())
XCTAssertEqual(resultValue[0].extra?["survey_id"], "microsurvey-id")

XCTAssertEqual(mockMicrosurveyTelemetry.privacyNoticeTappedCalledCount, 1)
XCTAssertEqual(mockStore.dispatchedActions.count, 0)
}

Expand All @@ -69,10 +62,7 @@ final class MicrosurveyMiddlewareIntegrationTests: XCTestCase, StoreTestUtility

subject.microsurveyProvider(AppState(), action)

testEventMetricRecordingSuccess(metric: GleanMetrics.Microsurvey.submitButtonTapped)
let resultValue = try XCTUnwrap(GleanMetrics.Microsurvey.submitButtonTapped.testGetValue())
XCTAssertEqual(resultValue[0].extra?["survey_id"], "microsurvey-id")
XCTAssertEqual(resultValue[0].extra?["user_selection"], "Neutral")
XCTAssertEqual(mockMicrosurveyTelemetry.userResponseSubmittedCalledCount, 1)

let actionCalled = try XCTUnwrap(mockStore.dispatchedActions.first as? MicrosurveyPromptAction)
let actionType = try XCTUnwrap(actionCalled.actionType as? MicrosurveyPromptActionType)
Expand All @@ -87,9 +77,7 @@ final class MicrosurveyMiddlewareIntegrationTests: XCTestCase, StoreTestUtility

subject.microsurveyProvider(AppState(), action)

testEventMetricRecordingSuccess(metric: GleanMetrics.Microsurvey.shown)
let resultValue = try XCTUnwrap(GleanMetrics.Microsurvey.shown.testGetValue())
XCTAssertEqual(resultValue[0].extra?["survey_id"], "microsurvey-id")
XCTAssertEqual(mockMicrosurveyTelemetry.surveyViewedCalledCount, 1)
}

func testConfirmationViewedAction() throws {
Expand All @@ -98,9 +86,7 @@ final class MicrosurveyMiddlewareIntegrationTests: XCTestCase, StoreTestUtility

subject.microsurveyProvider(AppState(), action)

testEventMetricRecordingSuccess(metric: GleanMetrics.Microsurvey.confirmationShown)
let resultValue = try XCTUnwrap(GleanMetrics.Microsurvey.confirmationShown.testGetValue())
XCTAssertEqual(resultValue[0].extra?["survey_id"], "microsurvey-id")
XCTAssertEqual(mockMicrosurveyTelemetry.confirmationShownCalledCount, 1)
}

private func getAction(for actionType: MicrosurveyActionType) -> MicrosurveyAction {
Expand All @@ -113,7 +99,7 @@ final class MicrosurveyMiddlewareIntegrationTests: XCTestCase, StoreTestUtility

// MARK: - Helpers
private func createSubject() -> MicrosurveyMiddleware {
return MicrosurveyMiddleware()
return MicrosurveyMiddleware(microsurveyTelemetry: mockMicrosurveyTelemetry)
}

// MARK: StoreTestUtility
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// 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
// file, You can obtain one at http://mozilla.org/MPL/2.0/

import Glean
import XCTest
@testable import Client

final class MicrosurveyTelemetryTests: XCTestCase {
var mockGleanWrapper: MockGleanWrapper!

override func setUp() {
super.setUp()
mockGleanWrapper = MockGleanWrapper()
}

override func tearDown() {
mockGleanWrapper = nil
super.tearDown()
}

func testRecordEvent_WhenSurveyViewed_ThenGleanIsCalled() throws {
let subject = createSubject()
let event = GleanMetrics.Microsurvey.shown
typealias EventExtrasType = GleanMetrics.Microsurvey.ShownExtra
let expectedSurveyId = "microsurvey-id"

subject.surveyViewed(surveyId: expectedSurveyId)

let savedExtras = try XCTUnwrap(mockGleanWrapper.savedExtras.first as? EventExtrasType)
let savedMetric = try XCTUnwrap(mockGleanWrapper.savedEvents.first as? EventMetricType<EventExtrasType>)

XCTAssertEqual(mockGleanWrapper.recordEventCalled, 1)
XCTAssertEqual(savedExtras.surveyId, "microsurvey-id")
XCTAssert(savedMetric === event, "Received \(savedMetric) instead of \(event)")
}

func testRecordEvent_WhenPrivacyNoticeTapped_ThenGleanIsCalled() throws {
let subject = createSubject()
let event = GleanMetrics.Microsurvey.privacyNoticeTapped
typealias EventExtrasType = GleanMetrics.Microsurvey.PrivacyNoticeTappedExtra
let expectedSurveyId = "microsurvey-id"

subject.privacyNoticeTapped(surveyId: expectedSurveyId)

let savedExtras = try XCTUnwrap(mockGleanWrapper.savedExtras.first as? EventExtrasType)
let savedMetric = try XCTUnwrap(mockGleanWrapper.savedEvents.first as? EventMetricType<EventExtrasType>)

XCTAssertEqual(mockGleanWrapper.recordEventCalled, 1)
XCTAssertEqual(savedExtras.surveyId, expectedSurveyId)
XCTAssert(savedMetric === event, "Received \(savedMetric) instead of \(event)")
}

func testRecordEvent_WhenDismissButtonTapped_ThenGleanIsCalled() throws {
let subject = createSubject()
let event = GleanMetrics.Microsurvey.dismissButtonTapped
typealias EventExtrasType = GleanMetrics.Microsurvey.DismissButtonTappedExtra
let expectedSurveyId = "microsurvey-id"

subject.dismissButtonTapped(surveyId: expectedSurveyId)

let savedExtras = try XCTUnwrap(mockGleanWrapper.savedExtras.first as? EventExtrasType)
let savedMetric = try XCTUnwrap(mockGleanWrapper.savedEvents.first as? EventMetricType<EventExtrasType>)

XCTAssertEqual(mockGleanWrapper.recordEventCalled, 1)
XCTAssertEqual(savedExtras.surveyId, expectedSurveyId)
XCTAssert(savedMetric === event, "Received \(savedMetric) instead of \(event)")
}

func testRecordEvent_WhenUserResponseSubmitted_ThenGleanIsCalled() throws {
let subject = createSubject()
let event = GleanMetrics.Microsurvey.submitButtonTapped
typealias EventExtrasType = GleanMetrics.Microsurvey.SubmitButtonTappedExtra
let expectedSurveyId = "microsurvey-id"
let expectedSelection = "Neutral"

subject.userResponseSubmitted(surveyId: expectedSurveyId, userSelection: expectedSelection)

let savedExtras = try XCTUnwrap(mockGleanWrapper.savedExtras.first as? EventExtrasType)
let savedMetric = try XCTUnwrap(mockGleanWrapper.savedEvents.first as? EventMetricType<EventExtrasType>)

XCTAssertEqual(mockGleanWrapper.recordEventCalled, 1)
XCTAssertEqual(savedExtras.surveyId, expectedSurveyId)
XCTAssertEqual(savedExtras.userSelection, expectedSelection)
XCTAssert(savedMetric === event, "Received \(savedMetric) instead of \(event)")
}

func testRecordEvent_WhenConfirmationShown_ThenGleanIsCalled() throws {
let subject = createSubject()
let event = GleanMetrics.Microsurvey.confirmationShown
typealias EventExtrasType = GleanMetrics.Microsurvey.ConfirmationShownExtra
let expectedSurveyId = "microsurvey-id"

subject.confirmationShown(surveyId: expectedSurveyId)

let savedExtras = try XCTUnwrap(mockGleanWrapper.savedExtras.first as? EventExtrasType)
let savedMetric = try XCTUnwrap(mockGleanWrapper.savedEvents.first as? EventMetricType<EventExtrasType>)

XCTAssertEqual(mockGleanWrapper.recordEventCalled, 1)
XCTAssertEqual(savedExtras.surveyId, expectedSurveyId)
XCTAssert(savedMetric === event, "Received \(savedMetric) instead of \(event)")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change here from testing other telemetry structs with the mock is I removed the type comparison as well as the previous debug message.

}

private func createSubject() -> MicrosurveyTelemetry {
return MicrosurveyTelemetry(gleanWrapper: mockGleanWrapper)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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
// file, You can obtain one at http://mozilla.org/MPL/2.0/

import Foundation
@testable import Client

final class MockMicrosurveyTelemetry: MicrosurveyTelemetryProtocol {
var surveyViewedCalledCount = 0
var privacyNoticeTappedCalledCount = 0
var dismissButtonTappedCalledCount = 0
var userResponseSubmittedCalledCount = 0
var confirmationShownCalledCount = 0

func surveyViewed(surveyId: String) {
surveyViewedCalledCount += 1
}

func privacyNoticeTapped(surveyId: String) {
privacyNoticeTappedCalledCount += 1
}

func dismissButtonTapped(surveyId: String) {
dismissButtonTappedCalledCount += 1
}

func userResponseSubmitted(surveyId: String, userSelection: String) {
userResponseSubmittedCalledCount += 1
}

func confirmationShown(surveyId: String) {
confirmationShownCalledCount += 1
}
}
Loading