@@ -165,6 +165,53 @@ final class Analytics_Tests: XCTestCase {
165165#endif
166166 }
167167
168+
169+ func testContextWithUserAgent( ) {
170+ let configuration = Configuration ( writeKey: " test " )
171+ configuration. userAgent ( " testing user agent " )
172+ let analytics = Analytics ( configuration: configuration)
173+ let outputReader = OutputReaderPlugin ( )
174+ analytics. add ( plugin: outputReader)
175+
176+ #if !os(watchOS) && !os(Linux)
177+ // prime the pump for userAgent, since it's retrieved async.
178+ let vendorSystem = VendorSystem . current
179+ while vendorSystem. userAgent == nil {
180+ RunLoop . main. run ( until: Date . distantPast)
181+ }
182+ #endif
183+
184+ waitUntilStarted ( analytics: analytics)
185+
186+ // add a referrer
187+ analytics. openURL ( URL ( string: " https://google.com " ) !)
188+
189+ analytics. track ( name: " token check " )
190+
191+ let trackEvent : TrackEvent ? = outputReader. lastEvent as? TrackEvent
192+ let context = trackEvent? . context? . dictionaryValue
193+ // Verify that context isn't empty here.
194+ // We need to verify the values but will do that in separate platform specific tests.
195+ XCTAssertNotNil ( context)
196+ XCTAssertNotNil ( context ? [ " screen " ] , " screen missing! " )
197+ XCTAssertNotNil ( context ? [ " network " ] , " network missing! " )
198+ XCTAssertNotNil ( context ? [ " os " ] , " os missing! " )
199+ XCTAssertNotNil ( context ? [ " timezone " ] , " timezone missing! " )
200+ XCTAssertNotNil ( context ? [ " library " ] , " library missing! " )
201+ XCTAssertNotNil ( context ? [ " device " ] , " device missing! " )
202+
203+ let referrer = context ? [ " referrer " ] as! [ String : Any ]
204+ XCTAssertEqual ( referrer [ " url " ] as! String , " https://google.com " )
205+
206+ XCTAssertEqual ( context ? [ " userAgent " ] as! String , " testing user agent " )
207+
208+ // these keys not present on linux
209+ #if !os(Linux)
210+ XCTAssertNotNil ( context ? [ " app " ] , " app missing! " )
211+ XCTAssertNotNil ( context ? [ " locale " ] , " locale missing! " )
212+ #endif
213+ }
214+
168215 func testDeviceToken( ) {
169216 let analytics = Analytics ( configuration: Configuration ( writeKey: " test " ) )
170217 let outputReader = OutputReaderPlugin ( )
0 commit comments