Update the docs and examples to the GleanTestRule

This commit is contained in:
Alessio Placitelli 2019-07-31 10:44:53 +02:00
Родитель 89d8e3d333
Коммит 7f276c7146
14 изменённых файлов: 40 добавлений и 52 удалений

Просмотреть файл

@ -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(