Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor FXIOS-11023 [Glean] Use glean wrapper for DefaultShareTelemetry #24231

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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: 7 additions & 1 deletion firefox-ios/Client/Frontend/Share/ShareTelemetry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ protocol ShareTelemetry {
}

struct DefaultShareTelemetry: ShareTelemetry {
private let gleanWrapper: GleanWrapper

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

func sharedTo(
activityType: UIActivity.ActivityType?,
shareType: ShareType,
Expand All @@ -30,6 +36,6 @@ struct DefaultShareTelemetry: ShareTelemetry {
isOptedInSentFromFirefox: isOptedInSentFromFirefox,
shareType: shareType.typeName
)
GleanMetrics.ShareSheet.sharedTo.record(extra)
gleanWrapper.recordEvent(for: GleanMetrics.ShareSheet.sharedTo, extras: extra)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import XCTest

final class ShareTelemetryTests: XCTestCase {
private let testWebURL = URL(string: "https://mozilla.org")!
var gleanWrapper: MockGleanWrapper!

// For telemetry extras
let activityIdentifierKey = "activity_identifier"
Expand All @@ -19,11 +20,7 @@ final class ShareTelemetryTests: XCTestCase {

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)
Comment on lines -22 to -26
Copy link
Contributor

Choose a reason for hiding this comment

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

🔥

gleanWrapper = MockGleanWrapper()
}

func testSharedTo_withNoActivityType() throws {
Expand All @@ -42,14 +39,27 @@ final class ShareTelemetryTests: XCTestCase {
isOptedInSentFromFirefox: testIsOptedInSentFromFirefox
)

testEventMetricRecordingSuccess(metric: GleanMetrics.ShareSheet.sharedTo)
let savedEvent = try XCTUnwrap(
gleanWrapper.savedEvent as? EventMetricType<GleanMetrics.ShareSheet.SharedToExtra>
)

let savedExtras = try XCTUnwrap(
gleanWrapper.savedExtras as? GleanMetrics.ShareSheet.SharedToExtra
)

let resultValue = try XCTUnwrap(GleanMetrics.ShareSheet.sharedTo.testGetValue())
XCTAssertEqual(resultValue[0].extra?[activityIdentifierKey], "unknown")
XCTAssertEqual(resultValue[0].extra?[shareTypeKey], testShareType.typeName)
XCTAssertEqual(resultValue[0].extra?[hasShareMessageKey], String(testHasShareMessage))
XCTAssertEqual(resultValue[0].extra?[hasIsEnrolledInSentFromFirefoxKey], String(testIsEnrolledInSentFromFirefox))
XCTAssertEqual(resultValue[0].extra?[hasIsOptedInSentFromFirefoxKey], String(testIsOptedInSentFromFirefox))
let extraRecord = savedExtras.toExtraRecord()

let expectedMetricType = type(of: GleanMetrics.ShareSheet.sharedTo)
let resultMetricType = type(of: savedEvent)

let message = TelemetryDebugMessage(expectedMetric: expectedMetricType, resultMetric: resultMetricType)
XCTAssert(resultMetricType == expectedMetricType, message.text)
XCTAssertEqual(gleanWrapper.recordEventCalled, 1)
XCTAssertEqual(extraRecord[activityIdentifierKey], "unknown")
XCTAssertEqual(extraRecord[shareTypeKey], testShareType.typeName)
XCTAssertEqual(extraRecord[hasShareMessageKey], String(testHasShareMessage))
XCTAssertEqual(extraRecord[hasIsEnrolledInSentFromFirefoxKey], String(testIsEnrolledInSentFromFirefox))
XCTAssertEqual(extraRecord[hasIsOptedInSentFromFirefoxKey], String(testIsOptedInSentFromFirefox))
}

func testSharedTo_withActivityType() throws {
Expand All @@ -68,14 +78,25 @@ final class ShareTelemetryTests: XCTestCase {
isOptedInSentFromFirefox: testIsOptedInSentFromFirefox
)

testEventMetricRecordingSuccess(metric: GleanMetrics.ShareSheet.sharedTo)

let resultValue = try XCTUnwrap(GleanMetrics.ShareSheet.sharedTo.testGetValue())
XCTAssertEqual(resultValue[0].extra?[activityIdentifierKey], testActivityType.rawValue)
XCTAssertEqual(resultValue[0].extra?[shareTypeKey], testShareType.typeName)
XCTAssertEqual(resultValue[0].extra?[hasShareMessageKey], String(testHasShareMessage))
XCTAssertEqual(resultValue[0].extra?[hasIsEnrolledInSentFromFirefoxKey], String(testIsEnrolledInSentFromFirefox))
XCTAssertEqual(resultValue[0].extra?[hasIsOptedInSentFromFirefoxKey], String(testIsOptedInSentFromFirefox))
let savedEvent = try XCTUnwrap(
gleanWrapper.savedEvent as? EventMetricType<GleanMetrics.ShareSheet.SharedToExtra>
)
let savedExtras = try XCTUnwrap(
gleanWrapper.savedExtras as? GleanMetrics.ShareSheet.SharedToExtra
)
let extraRecord = savedExtras.toExtraRecord()

let expectedMetricType = type(of: GleanMetrics.ShareSheet.sharedTo)
let resultMetricType = type(of: savedEvent)

let message = TelemetryDebugMessage(expectedMetric: expectedMetricType, resultMetric: resultMetricType)
XCTAssert(resultMetricType == expectedMetricType, message.text)
XCTAssertEqual(gleanWrapper.recordEventCalled, 1)
XCTAssertEqual(extraRecord[activityIdentifierKey], testActivityType.rawValue)
XCTAssertEqual(extraRecord[shareTypeKey], testShareType.typeName)
XCTAssertEqual(extraRecord[hasShareMessageKey], String(testHasShareMessage))
XCTAssertEqual(extraRecord[hasIsEnrolledInSentFromFirefoxKey], String(testIsEnrolledInSentFromFirefox))
XCTAssertEqual(extraRecord[hasIsOptedInSentFromFirefoxKey], String(testIsOptedInSentFromFirefox))
}

func testSharedTo_enrolledAndOptedInSentFromFirefox() throws {
Expand All @@ -94,17 +115,29 @@ final class ShareTelemetryTests: XCTestCase {
isOptedInSentFromFirefox: testIsOptedInSentFromFirefox
)

testEventMetricRecordingSuccess(metric: GleanMetrics.ShareSheet.sharedTo)
let savedEvent = try XCTUnwrap(
gleanWrapper.savedEvent as? EventMetricType<GleanMetrics.ShareSheet.SharedToExtra>
)
let savedExtras = try XCTUnwrap(
gleanWrapper.savedExtras as? GleanMetrics.ShareSheet.SharedToExtra
)

let extraRecord = savedExtras.toExtraRecord()

let expectedMetricType = type(of: GleanMetrics.ShareSheet.sharedTo)
let resultMetricType = type(of: savedEvent)

let resultValue = try XCTUnwrap(GleanMetrics.ShareSheet.sharedTo.testGetValue())
XCTAssertEqual(resultValue[0].extra?[activityIdentifierKey], testActivityType.rawValue)
XCTAssertEqual(resultValue[0].extra?[shareTypeKey], testShareType.typeName)
XCTAssertEqual(resultValue[0].extra?[hasShareMessageKey], String(testHasShareMessage))
XCTAssertEqual(resultValue[0].extra?[hasIsEnrolledInSentFromFirefoxKey], String(testIsEnrolledInSentFromFirefox))
XCTAssertEqual(resultValue[0].extra?[hasIsOptedInSentFromFirefoxKey], String(testIsOptedInSentFromFirefox))
let message = TelemetryDebugMessage(expectedMetric: expectedMetricType, resultMetric: resultMetricType)
XCTAssert(resultMetricType == expectedMetricType, message.text)
XCTAssertEqual(gleanWrapper.recordEventCalled, 1)
XCTAssertEqual(extraRecord[activityIdentifierKey], testActivityType.rawValue)
XCTAssertEqual(extraRecord[shareTypeKey], testShareType.typeName)
XCTAssertEqual(extraRecord[hasShareMessageKey], String(testHasShareMessage))
XCTAssertEqual(extraRecord[hasIsEnrolledInSentFromFirefoxKey], String(testIsEnrolledInSentFromFirefox))
XCTAssertEqual(extraRecord[hasIsOptedInSentFromFirefoxKey], String(testIsOptedInSentFromFirefox))
}

func createSubject() -> ShareTelemetry {
return DefaultShareTelemetry()
return DefaultShareTelemetry(gleanWrapper: GleanWrapper)
Copy link
Collaborator

@DanielDervishi DanielDervishi Jan 20, 2025

Choose a reason for hiding this comment

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

Suggested change
return DefaultShareTelemetry(gleanWrapper: GleanWrapper)
return DefaultShareTelemetry(gleanWrapper: gleanWrapper)

Just noticed after the fact, we should be passing in the gleanWrapper variable rather than the protocol itself :)

Copy link
Contributor

Choose a reason for hiding this comment

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

+1 we will want to pass in the mock here

}
}