зеркало из https://github.com/mozilla/glean.git
Update the docs and examples to the GleanTestRule
This commit is contained in:
Родитель
89d8e3d333
Коммит
7f276c7146
|
@ -26,7 +26,6 @@ There are test APIs available too:
|
|||
```Kotlin
|
||||
import org.mozilla.yourApplication.GleanMetrics.SearchDefault
|
||||
|
||||
Glean.enableTestingMode()
|
||||
// Was the experiment annotated in Glean pings?
|
||||
assertTrue(Glean.testIsExperimentActive("blue-button-effective"))
|
||||
// Was the correct branch reported?
|
||||
|
|
|
@ -28,7 +28,6 @@ There are test APIs available too:
|
|||
|
||||
```Kotlin
|
||||
import org.mozilla.yourApplication.GleanMetrics.Flags
|
||||
Glean.enableTestingMode()
|
||||
|
||||
// Was anything recorded?
|
||||
assertTrue(Flags.a11yEnabled.testHasValue())
|
||||
|
|
|
@ -28,7 +28,6 @@ There are test APIs available too:
|
|||
|
||||
```Kotlin
|
||||
import org.mozilla.yourApplication.GleanMetrics.Controls
|
||||
Glean.enableTestingMode()
|
||||
|
||||
// Was anything recorded?
|
||||
assertTrue(Controls.refreshPressed.testHasValue())
|
||||
|
|
|
@ -46,7 +46,6 @@ There are test APIs available too.
|
|||
|
||||
```Kotlin
|
||||
import org.mozilla.yourApplication.GleanMetrics.Install
|
||||
Glean.enableTestingMode()
|
||||
|
||||
// Was anything recorded?
|
||||
assertTrue(Install.firstRun.testHasValue())
|
||||
|
|
|
@ -33,7 +33,6 @@ There are test APIs available too, for example:
|
|||
|
||||
```Kotlin
|
||||
import org.mozilla.yourApplication.GleanMetrics.Views
|
||||
Glean.enableTestingMode()
|
||||
|
||||
// Was any event recorded?
|
||||
assertTrue(Views.loginOpened.testHasValue())
|
||||
|
|
|
@ -32,7 +32,6 @@ There are test APIs available too:
|
|||
|
||||
```Kotlin
|
||||
import org.mozilla.yourApplication.GleanMetrics.Stability
|
||||
Glean.enableTestingMode()
|
||||
// Was anything recorded?
|
||||
assertTrue(Stability.crashCount["uncaught_exception"].testHasValue())
|
||||
assertTrue(Stability.crashCount["native_code_crash"].testHasValue())
|
||||
|
|
|
@ -31,7 +31,6 @@ There are test APIs available too:
|
|||
|
||||
```Kotlin
|
||||
import org.mozilla.yourApplication.GleanMetrics.Login
|
||||
Glean.enableTestingMode()
|
||||
|
||||
// Was anything recorded?
|
||||
assertTrue(Login.errorsByStage["server_auth"].testHasValue())
|
||||
|
|
|
@ -26,7 +26,7 @@ search.default:
|
|||
...
|
||||
```
|
||||
|
||||
## API
|
||||
## API
|
||||
|
||||
```Kotlin
|
||||
import org.mozilla.yourApplication.GleanMetrics.SearchDefault
|
||||
|
@ -42,7 +42,6 @@ There are test APIs available too:
|
|||
```Kotlin
|
||||
import org.mozilla.yourApplication.GleanMetrics.SearchDefault
|
||||
|
||||
Glean.enableTestingMode()
|
||||
// Was anything recorded?
|
||||
assertTrue(SearchDefault.name.testHasValue())
|
||||
// Does the string metric have the expected value?
|
||||
|
|
|
@ -40,7 +40,6 @@ There are test APIs available too:
|
|||
|
||||
```Kotlin
|
||||
import org.mozilla.yourApplication.GleanMetrics.Search
|
||||
Glean.enableTestingMode()
|
||||
|
||||
// Was anything recorded?
|
||||
assertTrue(Search.engines.testHasValue())
|
||||
|
|
|
@ -59,7 +59,6 @@ There are test APIs available too:
|
|||
|
||||
```Kotlin
|
||||
import org.mozilla.yourApplication.GleanMetrics.Auth
|
||||
Glean.enableTestingMode()
|
||||
|
||||
// Was anything recorded?
|
||||
assertTrue(Auth.loginTime.testHasValue())
|
||||
|
|
|
@ -58,7 +58,6 @@ Continuing the `pageLoad` example above, at this point the metric should have a
|
|||
|
||||
```Kotlin
|
||||
import org.mozilla.yourApplication.GleanMetrics.Pages
|
||||
Glean.enableTestingMode()
|
||||
|
||||
// Was anything recorded?
|
||||
assertTrue(pages.pageLoad.testHasValue())
|
||||
|
@ -76,7 +75,7 @@ assertEquals(2L, snapshot.count())
|
|||
## Limits
|
||||
|
||||
* Which range of values is recorded in detail depends on the `time_unit`, e.g. for milliseconds, all values greater 60000 are recorded as overflow values.
|
||||
|
||||
|
||||
## Examples
|
||||
|
||||
* How long does it take a page to load?
|
||||
|
|
|
@ -31,11 +31,10 @@ There are test APIs available too.
|
|||
|
||||
```Kotlin
|
||||
import org.mozilla.yourApplication.GleanMetrics.User
|
||||
Glean.enableTestingMode()
|
||||
|
||||
// Was anything recorded?
|
||||
assertTrue(User.clientId.testHasValue())
|
||||
// Was it the expected value?
|
||||
// Was it the expected value?
|
||||
assertEquals(uuid, User.clientId.testGetValue())
|
||||
```
|
||||
|
||||
|
@ -50,7 +49,7 @@ assertEquals(uuid, User.clientId.testGetValue())
|
|||
## Recorded errors
|
||||
|
||||
* None.
|
||||
|
||||
|
||||
## Reference
|
||||
|
||||
* See [Kotlin API docs](../../../javadoc/glean/mozilla.telemetry.glean.private/-uuid-metric-type/index.html).
|
||||
|
|
|
@ -101,40 +101,30 @@ import my.component.GleanMetrics.CustomPingData
|
|||
import org.mockito.Mockito.spy
|
||||
import org.mockito.Mockito.`when`
|
||||
|
||||
/**
|
||||
* This is an helper function used to enable testing mode for Glean.
|
||||
* Should only be called once before the tests, but nothing breaks if it's
|
||||
* called more than once!
|
||||
*/
|
||||
fun setupGleanOnce() {
|
||||
// Enable testing mode
|
||||
// (Perhaps called from a @Before method so it precedes every test in the suite.)
|
||||
Glean.enableTestingMode()
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class MyCustomPingSchedulerTest {
|
||||
// Apply the GleanTestRule
|
||||
@get:Rule
|
||||
val gleanRule = GleanTestRule(ApplicationProvider.getApplicationContext())
|
||||
|
||||
// We're using the WorkManager in a bunch of places, and Glean will crash
|
||||
// in tests without this line. Let's simply put it here.
|
||||
WorkManagerTestInitHelper.initializeTestWorkManager(context)
|
||||
@Test
|
||||
fun `verify custom ping metrics`() {
|
||||
setupGleanOnce()
|
||||
|
||||
Glean.initialize(context)
|
||||
}
|
||||
val scheduler = spy(MyCustomPingScheduler())
|
||||
doAnswer {
|
||||
// Here we validate the content that goes into the ping.
|
||||
assertTrue(CustomPingData.sampleString.testHasValue())
|
||||
assertEquals("test-data", CustomPingData.sampleString.testGetValue())
|
||||
|
||||
@Test
|
||||
fun `verify custom ping metrics`() {
|
||||
setupGleanOnce()
|
||||
// We want to intercept this call, but we also want to make sure the
|
||||
// real Glean API is called in order to clear the ping store and to provide
|
||||
// consistent behaviour with respect to the application.
|
||||
it.callRealMethod()
|
||||
}.`when`(scheduler).sendPing()
|
||||
|
||||
val scheduler = spy(MyCustomPingScheduler())
|
||||
doAnswer {
|
||||
// Here we validate the content that goes into the ping.
|
||||
assertTrue(CustomPingData.sampleString.testHasValue())
|
||||
assertEquals("test-data", CustomPingData.sampleString.testGetValue())
|
||||
|
||||
// We want to intercept this call, but we also want to make sure the
|
||||
// real Glean API is called in order to clear the ping store and to provide
|
||||
// consistent behaviour with respect to the application.
|
||||
it.callRealMethod()
|
||||
}.`when`(scheduler).sendPing()
|
||||
|
||||
scheduler.addSomeData()
|
||||
scheduler.schedulePing()
|
||||
scheduler.addSomeData()
|
||||
scheduler.schedulePing()
|
||||
}
|
||||
}
|
||||
```
|
||||
|
|
|
@ -6,7 +6,21 @@ These functions expose a way to inspect and validate recorded metric values with
|
|||
|
||||
## General test API method semantics
|
||||
|
||||
In order to prevent issues with async calls when unit testing Glean, it is important to put the Glean SDK into testing mode by calling `Glean.enableTestingMode()`.
|
||||
In order to prevent issues with async calls when unit testing Glean, it is important to put the Glean SDK into testing mode by applying the JUnit `GleanTestRule` to your test class, as shown below:
|
||||
|
||||
```kotlin
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
class ActivityCollectingDataTest {
|
||||
// Apply the GleanTestRule
|
||||
@get:Rule
|
||||
val gleanRule = GleanTestRule(ApplicationProvider.getApplicationContext())
|
||||
|
||||
fun checkCollectedData() {
|
||||
// The Glean SDK testing API can be called here.
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
This will ensure that metrics are done recording when the other test functions are used.
|
||||
|
||||
To check if a value exists (i.e. it has been recorded), there is a `testHasValue()` function on each of the metric instances:
|
||||
|
@ -43,10 +57,6 @@ GleanMetrics.Foo.UriCount.testGetValue("customPing")
|
|||
|
||||
Here is a longer example to better illustrate the intended use of the test API:
|
||||
```kotlin
|
||||
// Enable testing mode
|
||||
// (Perhaps called from a @Before method so it precedes every test in the suite.)
|
||||
Glean.enableTestingMode()
|
||||
|
||||
// Record a metric value with extra to validate against
|
||||
GleanMetrics.BrowserEngagement.click.record(
|
||||
mapOf(
|
||||
|
|
Загрузка…
Ссылка в новой задаче