Skip to content

Commit a7af87c

Browse files
bsneedBrandon Sneed
andauthored
Fix context issue and macOS vendor system miss. (#44)
* Fix missing macOS vendor system return. * Move context out of lifecycle event check. * Added test that context is present along with the top level keys * Improved test reporting. * Added exclusions for linux & watchOS in context test. Co-authored-by: Brandon Sneed <[email protected]>
1 parent 14a3264 commit a7af87c

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

Sources/Segment/Plugins/Platforms/Vendors/VendorSystem.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ internal class VendorSystem {
6868
static var current: VendorSystem {
6969
#if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst)
7070
return iOSVendorSystem()
71+
#elseif os(macOS)
72+
return MacOSVendorSystem()
7173
#elseif os(watchOS)
7274
return watchOSVendorSystem()
7375
#elseif os(Linux)

Sources/Segment/Startup.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ extension Analytics: Subscriber {
3737
internal func platformPlugins() -> [PlatformPlugin]? {
3838
var plugins = [PlatformPlugin]()
3939

40+
// add context plugin as well as it's platform specific internally.
41+
// this must come first.
42+
plugins.append(Context())
43+
4044
// setup lifecycle if desired
4145
if configuration.values.trackApplicationLifecycleEvents {
42-
// add context plugin as well as it's platform specific internally.
43-
// this must come first.
44-
plugins.append(Context())
45-
4646
#if os(iOS) || os(tvOS)
4747
plugins += [iOSLifecycleMonitor(), iOSLifecycleEvents(), DeviceToken()]
4848
#endif

Tests/Segment-Tests/Analytics_Tests.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,39 @@ final class Analytics_Tests: XCTestCase {
111111
XCTAssertTrue(anonId.count == 36) // it's a UUID y0.
112112
}
113113

114+
func testContext() {
115+
let analytics = Analytics(configuration: Configuration(writeKey: "test"))
116+
let outputReader = OutputReaderPlugin()
117+
analytics.add(plugin: outputReader)
118+
119+
waitUntilStarted(analytics: analytics)
120+
121+
analytics.track(name: "token check")
122+
123+
let trackEvent: TrackEvent? = outputReader.lastEvent as? TrackEvent
124+
let context = trackEvent?.context?.dictionaryValue
125+
// Verify that context isn't empty here.
126+
// We need to verify the values but will do that in separate platform specific tests.
127+
XCTAssertNotNil(context)
128+
XCTAssertNotNil(context?["screen"], "screen missing!")
129+
XCTAssertNotNil(context?["network"], "network missing!")
130+
XCTAssertNotNil(context?["os"], "os missing!")
131+
XCTAssertNotNil(context?["timezone"], "timezone missing!")
132+
XCTAssertNotNil(context?["library"], "library missing!")
133+
XCTAssertNotNil(context?["device"], "device missing!")
134+
135+
// this key not present on watchOS (doesn't have webkit)
136+
#if !os(watchOS)
137+
XCTAssertNotNil(context?["userAgent"], "userAgent missing!")
138+
#endif
139+
140+
// these keys not present on linux
141+
#if !os(Linux)
142+
XCTAssertNotNil(context?["app"], "app missing!")
143+
XCTAssertNotNil(context?["locale"], "locale missing!")
144+
#endif
145+
}
146+
114147
func testDeviceToken() {
115148
let analytics = Analytics(configuration: Configuration(writeKey: "test"))
116149
let outputReader = OutputReaderPlugin()

0 commit comments

Comments
 (0)