Add Test Butler and restore offline login test

This commit is contained in:
Matthieu 2021-05-08 21:58:14 +02:00
parent 0ebabf3fa7
commit 8a925c1e2f
3 changed files with 32 additions and 14 deletions

View File

@ -27,7 +27,7 @@ android {
versionCode 11 versionCode 11
versionName "1.0.beta1" versionName "1.0.beta1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "org.pixeldroid.app.testUtility.TestRunner"
testInstrumentationRunnerArguments clearPackageData: 'true' testInstrumentationRunnerArguments clearPackageData: 'true'
} }
lintOptions{ lintOptions{
@ -188,6 +188,9 @@ dependencies {
* Not in release, so not mentioned in licenses list * Not in release, so not mentioned in licenses list
*/ */
androidTestImplementation 'com.linkedin.testbutler:test-butler-library:2.2.1'
androidTestUtil 'com.linkedin.testbutler:test-butler-app:2.2.1'
// debugImplementation required vs testImplementation: https://issuetracker.google.com/issues/128612536 // debugImplementation required vs testImplementation: https://issuetracker.google.com/issues/128612536
//noinspection FragmentGradleConfiguration //noinspection FragmentGradleConfiguration
stagingImplementation("androidx.fragment:fragment-testing:1.3.3") { stagingImplementation("androidx.fragment:fragment-testing:1.3.3") {

View File

@ -1,5 +1,5 @@
package org.pixeldroid.app package org.pixeldroid.app
/*
import android.content.Context import android.content.Context
import androidx.test.core.app.ActivityScenario import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ApplicationProvider import androidx.test.core.app.ApplicationProvider
@ -12,6 +12,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
import androidx.test.uiautomator.UiDevice import androidx.test.uiautomator.UiDevice
import androidx.test.uiautomator.UiSelector import androidx.test.uiautomator.UiSelector
import com.linkedin.android.testbutler.TestButler
import org.pixeldroid.app.testUtility.clearData import org.pixeldroid.app.testUtility.clearData
import org.pixeldroid.app.testUtility.initDB import org.pixeldroid.app.testUtility.initDB
import org.pixeldroid.app.utils.db.AppDatabase import org.pixeldroid.app.utils.db.AppDatabase
@ -21,21 +22,13 @@ import org.junit.Rule
import org.junit.Test import org.junit.Test
import org.junit.rules.Timeout import org.junit.rules.Timeout
import org.junit.runner.RunWith import org.junit.runner.RunWith
import org.pixeldroid.app.testUtility.waitForView
@RunWith(AndroidJUnit4::class) @RunWith(AndroidJUnit4::class)
class LoginActivityOfflineTest { class LoginActivityOfflineTest {
private lateinit var context: Context private lateinit var context: Context
companion object {
fun switchAirplaneMode() {
val device = UiDevice.getInstance(getInstrumentation())
device.openQuickSettings()
device.findObject(UiSelector().textContains("Airplane")).click()
device.pressHome()
}
}
private lateinit var db: AppDatabase private lateinit var db: AppDatabase
@get:Rule @get:Rule
@ -43,7 +36,8 @@ class LoginActivityOfflineTest {
@Before @Before
fun before() { fun before() {
switchAirplaneMode() TestButler.setWifiState(false)
TestButler.setGsmState(false)
context = ApplicationProvider.getApplicationContext<Context>() context = ApplicationProvider.getApplicationContext<Context>()
db = initDB(context) db = initDB(context)
db.clearAllTables() db.clearAllTables()
@ -52,19 +46,22 @@ class LoginActivityOfflineTest {
@Test @Test
fun emptyDBandOfflineModeDisplayCorrectMessage() { fun emptyDBandOfflineModeDisplayCorrectMessage() {
waitForView(R.id.login_activity_connection_required)
onView(withId(R.id.login_activity_connection_required)).check(matches(isDisplayed())) onView(withId(R.id.login_activity_connection_required)).check(matches(isDisplayed()))
} }
@Test @Test
fun retryButtonReloadsLoginActivity() { fun retryButtonReloadsLoginActivity() {
waitForView(R.id.login_activity_connection_required_button)
onView(withId(R.id.login_activity_connection_required_button)).perform(click()) onView(withId(R.id.login_activity_connection_required_button)).perform(click())
onView(withId(R.id.login_activity_connection_required)).check(matches(isDisplayed())) onView(withId(R.id.login_activity_connection_required)).check(matches(isDisplayed()))
} }
@After @After
fun after() { fun after() {
switchAirplaneMode() TestButler.setWifiState(true)
TestButler.setGsmState(true)
db.close() db.close()
clearData() clearData()
} }
}*/ }

View File

@ -0,0 +1,18 @@
package org.pixeldroid.app.testUtility
import android.os.Bundle
import androidx.test.runner.AndroidJUnitRunner
import com.linkedin.android.testbutler.TestButler
class TestRunner: AndroidJUnitRunner() {
override fun onStart() {
TestButler.setup(targetContext)
super.onStart()
}
override fun finish(resultCode: Int, results: Bundle) {
TestButler.teardown(targetContext)
super.finish(resultCode, results)
}
}