Skip to content

Commit

Permalink
Refactor FXIOS-10292 [Glean unit tests] PrivateBrowsingTelemetry to u…
Browse files Browse the repository at this point in the history
…se GleanWrapper (#24061)
  • Loading branch information
cyndichin authored Jan 9, 2025
1 parent 548c1af commit 907817e
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 33 deletions.
4 changes: 4 additions & 0 deletions firefox-ios/Client.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,7 @@
8A94418A2CE3E190007FF4E5 /* MockSearchEngineManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A9441892CE3E190007FF4E5 /* MockSearchEngineManager.swift */; };
8A95FF642B1E969E00AC303D /* TelemetryContextualIdentifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A95FF632B1E969E00AC303D /* TelemetryContextualIdentifier.swift */; };
8A95FF672B1E97A800AC303D /* TelemetryContextualIdentifierTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A95FF652B1E977E00AC303D /* TelemetryContextualIdentifierTests.swift */; };
8A967E422D30419100B1017D /* TelemetryDebugMessage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A967E412D30418A00B1017D /* TelemetryDebugMessage.swift */; };
8A96C4BB28F9E7B300B75884 /* XCTestCaseRootViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A96C4BA28F9E7B300B75884 /* XCTestCaseRootViewController.swift */; };
8A97E6EA2C58487900F94793 /* AddressLocaleFeatureValidator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A97E6E92C58487900F94793 /* AddressLocaleFeatureValidator.swift */; };
8A97E6EF2C584C4900F94793 /* AddressLocaleFeatureValidatorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8A97E6ED2C584AC300F94793 /* AddressLocaleFeatureValidatorTests.swift */; };
Expand Down Expand Up @@ -7743,6 +7744,7 @@
8A9441892CE3E190007FF4E5 /* MockSearchEngineManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockSearchEngineManager.swift; sourceTree = "<group>"; };
8A95FF632B1E969E00AC303D /* TelemetryContextualIdentifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TelemetryContextualIdentifier.swift; sourceTree = "<group>"; };
8A95FF652B1E977E00AC303D /* TelemetryContextualIdentifierTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TelemetryContextualIdentifierTests.swift; sourceTree = "<group>"; };
8A967E412D30418A00B1017D /* TelemetryDebugMessage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TelemetryDebugMessage.swift; sourceTree = "<group>"; };
8A96C4B828F9DD8700B75884 /* ThemableTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThemableTests.swift; sourceTree = "<group>"; };
8A96C4BA28F9E7B300B75884 /* XCTestCaseRootViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XCTestCaseRootViewController.swift; sourceTree = "<group>"; };
8A97E6E92C58487900F94793 /* AddressLocaleFeatureValidator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddressLocaleFeatureValidator.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -12098,6 +12100,7 @@
8ACA8F722919870B00D3075D /* Helpers */ = {
isa = PBXGroup;
children = (
8A967E412D30418A00B1017D /* TelemetryDebugMessage.swift */,
8ACA8F73291987AE00D3075D /* AccountSyncHandlerTests.swift */,
2165B2BF2860BB41004C0786 /* AdjustTelemetryHelperTests.swift */,
8A7A26E629D4C0D800EA76F1 /* IntroScreenManagerTests.swift */,
Expand Down Expand Up @@ -17394,6 +17397,7 @@
8A36AC2C2886F27F00CDC0AD /* MockTabManager.swift in Sources */,
5A3A7DD62889CF3D0065F81A /* BookmarksDataAdaptorTests.swift in Sources */,
8AD08D1727E91AC800B8E907 /* TabsTelemetryTests.swift in Sources */,
8A967E422D30419100B1017D /* TelemetryDebugMessage.swift in Sources */,
C8C3FE9D29F907B30038E3BA /* MockSearchHandlerRouteCoordinator.swift in Sources */,
ED55DC8C2CC2D7DA00E3FE3A /* SearchEngineSelectionCoordinatorTests.swift in Sources */,
C8C3FEA129F973C40038E3BA /* MockBrowserViewController.swift in Sources */,
Expand Down
8 changes: 7 additions & 1 deletion firefox-ios/Client/Telemetry/PrivateBrowsingTelemetry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ import Foundation
import Glean

struct PrivateBrowsingTelemetry {
private let gleanWrapper: GleanWrapper

init(gleanWrapper: GleanWrapper = DefaultGleanWrapper()) {
self.gleanWrapper = gleanWrapper
}

func sendDataClearanceTappedTelemetry(didConfirm: Bool) {
let didConfirmExtra = GleanMetrics.PrivateBrowsing.DataClearanceIconTappedExtra(didConfirm: didConfirm)
GleanMetrics.PrivateBrowsing.dataClearanceIconTapped.record(didConfirmExtra)
gleanWrapper.recordEvent(for: GleanMetrics.PrivateBrowsing.dataClearanceIconTapped, extras: didConfirmExtra)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 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/

struct TelemetryDebugMessage {
let firstText = "Expected savedMetric to be of type "
let lastText = ", but got "
let text: String

init<Metatype>(expectedMetric: Metatype, resultMetric: Metatype) {
text = "\(firstText)\(expectedMetric)\(lastText)\(resultMetric)"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class MockGleanWrapper: GleanWrapper {
var cancelTimingCalled = 0
var stopAndAccumulateCalled = 0
var savedEvent: Any?
var savedExtras: Any?

var savedHandleDeeplinkUrl: URL?
var savedSetUploadIsEnabled: Bool?
Expand All @@ -45,6 +46,7 @@ class MockGleanWrapper: GleanWrapper {
func recordEvent<ExtraObject>(for metric: EventMetricType<ExtraObject>,
extras: EventExtras) where ExtraObject: EventExtras {
savedEvent = metric
savedExtras = extras
recordEventCalled += 1
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,50 @@ import XCTest
@testable import Client

final class PrivateBrowsingTelemetryTests: XCTestCase {
var gleanWrapper: MockGleanWrapper!

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 puth them in a state in which they can collect data.
Glean.shared.registerPings(GleanMetrics.Pings.shared)
Glean.shared.resetGlean(clearStores: true)
gleanWrapper = MockGleanWrapper()
}

func testDataClearanceConfirmed() throws {
let subject = PrivateBrowsingTelemetry()
let subject = PrivateBrowsingTelemetry(gleanWrapper: gleanWrapper)

subject.sendDataClearanceTappedTelemetry(didConfirm: true)

testEventMetricRecordingSuccess(metric: GleanMetrics.PrivateBrowsing.dataClearanceIconTapped)

let resultValue = try XCTUnwrap(GleanMetrics.PrivateBrowsing.dataClearanceIconTapped.testGetValue())
XCTAssertEqual(resultValue[0].extra?["did_confirm"], "true")
let savedEvent = try XCTUnwrap(
gleanWrapper.savedEvent as? EventMetricType<GleanMetrics.PrivateBrowsing.DataClearanceIconTappedExtra>
)
let savedExtras = try XCTUnwrap(
gleanWrapper.savedExtras as? GleanMetrics.PrivateBrowsing.DataClearanceIconTappedExtra
)
let expectedMetricType = type(of: GleanMetrics.PrivateBrowsing.dataClearanceIconTapped)
let resultMetricType = type(of: savedEvent)
let message = TelemetryDebugMessage(expectedMetric: expectedMetricType, resultMetric: resultMetricType)

XCTAssert(resultMetricType == expectedMetricType, message.text)
XCTAssertEqual(gleanWrapper.recordEventCalled, 1)
XCTAssertEqual(savedExtras.didConfirm, true)
}

func testDataClearanceCancelled() throws {
let subject = PrivateBrowsingTelemetry()
let subject = PrivateBrowsingTelemetry(gleanWrapper: gleanWrapper)

subject.sendDataClearanceTappedTelemetry(didConfirm: false)

testEventMetricRecordingSuccess(metric: GleanMetrics.PrivateBrowsing.dataClearanceIconTapped)

let resultValue = try XCTUnwrap(GleanMetrics.PrivateBrowsing.dataClearanceIconTapped.testGetValue())
XCTAssertEqual(resultValue[0].extra?["did_confirm"], "false")
let savedEvent = try XCTUnwrap(
gleanWrapper.savedEvent as? EventMetricType<GleanMetrics.PrivateBrowsing.DataClearanceIconTappedExtra>
)
let savedExtras = try XCTUnwrap(
gleanWrapper.savedExtras as? GleanMetrics.PrivateBrowsing.DataClearanceIconTappedExtra
)
let expectedMetricType = type(of: GleanMetrics.PrivateBrowsing.dataClearanceIconTapped)
let resultMetricType = type(of: savedEvent)
let message = TelemetryDebugMessage(expectedMetric: expectedMetricType, resultMetric: resultMetricType)

XCTAssert(resultMetricType == expectedMetricType, message.text)
XCTAssertEqual(gleanWrapper.recordEventCalled, 1)
XCTAssertEqual(savedExtras.didConfirm, false)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class InactiveTabsTelemetryTests: XCTestCase {
let savedMetric = try XCTUnwrap(gleanWrapper.savedEvent as? CounterMetricType)
let expectedMetricType = type(of: GleanMetrics.InactiveTabsTray.inactiveTabShown)
let resultMetricType = type(of: savedMetric)
let debugMessage = DebugMessage(expectedMetric: expectedMetricType, resultMetric: resultMetricType)
let debugMessage = TelemetryDebugMessage(expectedMetric: expectedMetricType, resultMetric: resultMetricType)
XCTAssert(resultMetricType == expectedMetricType, debugMessage.text)
XCTAssertEqual(gleanWrapper.incrementCounterCalled, 1)
}
Expand All @@ -40,7 +40,7 @@ final class InactiveTabsTelemetryTests: XCTestCase {
let savedMetric = try XCTUnwrap(gleanWrapper.savedEvent as? CounterMetricType)
let expectedMetricType = type(of: GleanMetrics.InactiveTabsTray.inactiveTabsCloseAllBtn)
let resultMetricType = type(of: savedMetric)
let debugMessage = DebugMessage(expectedMetric: expectedMetricType, resultMetric: resultMetricType)
let debugMessage = TelemetryDebugMessage(expectedMetric: expectedMetricType, resultMetric: resultMetricType)
XCTAssert(resultMetricType == expectedMetricType, debugMessage.text)
XCTAssertEqual(gleanWrapper.incrementCounterCalled, 1)
}
Expand All @@ -51,7 +51,7 @@ final class InactiveTabsTelemetryTests: XCTestCase {
let savedMetric = try XCTUnwrap(gleanWrapper.savedEvent as? CounterMetricType)
let expectedMetricType = type(of: GleanMetrics.InactiveTabsTray.openInactiveTab)
let resultMetricType = type(of: savedMetric)
let debugMessage = DebugMessage(expectedMetric: expectedMetricType, resultMetric: resultMetricType)
let debugMessage = TelemetryDebugMessage(expectedMetric: expectedMetricType, resultMetric: resultMetricType)
XCTAssert(resultMetricType == expectedMetricType, debugMessage.text)
XCTAssertEqual(gleanWrapper.incrementCounterCalled, 1)
}
Expand All @@ -62,7 +62,7 @@ final class InactiveTabsTelemetryTests: XCTestCase {
let savedMetric = try XCTUnwrap(gleanWrapper.savedEvent as? CounterMetricType)
let expectedMetricType = type(of: GleanMetrics.InactiveTabsTray.inactiveTabSwipeClose)
let resultMetricType = type(of: savedMetric)
let debugMessage = DebugMessage(expectedMetric: expectedMetricType, resultMetric: resultMetricType)
let debugMessage = TelemetryDebugMessage(expectedMetric: expectedMetricType, resultMetric: resultMetricType)
XCTAssert(resultMetricType == expectedMetricType, debugMessage.text)
XCTAssertEqual(gleanWrapper.incrementCounterCalled, 1)
}
Expand All @@ -75,20 +75,8 @@ final class InactiveTabsTelemetryTests: XCTestCase {
)
let expectedMetricType = type(of: GleanMetrics.InactiveTabsTray.toggleInactiveTabTray)
let resultMetricType = type(of: savedMetric)
let debugMessage = DebugMessage(expectedMetric: expectedMetricType, resultMetric: resultMetricType)
let debugMessage = TelemetryDebugMessage(expectedMetric: expectedMetricType, resultMetric: resultMetricType)
XCTAssert(resultMetricType == expectedMetricType, debugMessage.text)
XCTAssertEqual(gleanWrapper.recordEventCalled, 1)
}

// MARK: Helpers

private struct DebugMessage {
let firstText = "Expected savedMetric to be of type "
let lastText = ", but got "
let text: String

init<Metatype>(expectedMetric: Metatype, resultMetric: Metatype) {
text = "\(firstText)\(expectedMetric)\(lastText)\(resultMetric)"
}
}
}

0 comments on commit 907817e

Please sign in to comment.