Refactor tests to use the standard test runner

This commit is contained in:
Alessio Placitelli 2020-04-22 17:06:06 +02:00
Родитель 82d73f191f
Коммит 94371c5ddb
3 изменённых файлов: 28 добавлений и 10 удалений

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

@ -18,15 +18,16 @@ import androidx.test.uiautomator.UiDevice
import mozilla.telemetry.glean.testing.GleanTestLocalServer
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.mozilla.samples.gleancore.getPingServerPort
@RunWith(AndroidJUnit4::class)
class BaselinePingTest {
private val server = createMockWebServer()
@get:Rule
val activityRule: ActivityTestRule<MainActivity> = ActivityTestRule(MainActivity::class.java)
@get:Rule
val gleanRule = GleanTestLocalServer(context, getPingServerPort())
val gleanRule = GleanTestLocalServer(context, server.port)
private val context: Context
get() = ApplicationProvider.getApplicationContext()
@ -46,7 +47,7 @@ class BaselinePingTest {
device.pressHome()
// Validate the received data.
val baselinePing = waitForPingContent("baseline")!!
val baselinePing = waitForPingContent("baseline", server)!!
val metrics = baselinePing.getJSONObject("metrics")

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

@ -22,15 +22,16 @@ import org.mozilla.samples.gleancore.MainActivity
import androidx.test.uiautomator.UiDevice
import mozilla.telemetry.glean.testing.GleanTestLocalServer
import org.junit.Assert.assertNotEquals
import org.mozilla.samples.gleancore.getPingServerPort
@RunWith(AndroidJUnit4::class)
class DeletionRequestPingTest {
private val server = createMockWebServer()
@get:Rule
val activityRule: ActivityTestRule<MainActivity> = ActivityTestRule(MainActivity::class.java)
@get:Rule
val gleanRule = GleanTestLocalServer(context, getPingServerPort())
val gleanRule = GleanTestLocalServer(context, server.port)
private val context: Context
get() = ApplicationProvider.getApplicationContext()
@ -48,7 +49,7 @@ class DeletionRequestPingTest {
.perform(click())
// We might receive previous baseline or events ping, let's ignore that
val deletionPing = waitForPingContent("deletion-request")!!
val deletionPing = waitForPingContent("deletion-request", server)!!
// Validate the received data.
@ -63,7 +64,7 @@ class DeletionRequestPingTest {
device.pressHome()
// Validate the received data.
val baselinePing = waitForPingContent("baseline")!!
val baselinePing = waitForPingContent("baseline", server)!!
clientInfo = baselinePing.getJSONObject("client_info")

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

@ -4,10 +4,27 @@
package org.mozilla.samples.gleancore.pings
import okhttp3.mockwebserver.Dispatcher
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import okhttp3.mockwebserver.RecordedRequest
import org.json.JSONObject
import org.mozilla.samples.gleancore.getPingServer
import java.util.concurrent.TimeUnit
/**
* Create a mock webserver that accepts all requests and replies with "OK".
* @return a [MockWebServer] instance
*/
internal fun createMockWebServer(): MockWebServer {
val server = MockWebServer()
server.setDispatcher(object : Dispatcher() {
override fun dispatch(request: RecordedRequest): MockResponse {
return MockResponse().setBody("OK")
}
})
return server
}
/**
* Waits for ping with the given name to be received
* in the test ping server.
@ -17,11 +34,10 @@ import java.util.concurrent.TimeUnit
*/
fun waitForPingContent(
pingName: String,
server: MockWebServer,
maxAttempts: Int = 3
): JSONObject?
{
val server = getPingServer()
var attempts = 0
do {
attempts += 1