Merge pull request #3205 from kiwix/Issue#3204

Fixed remove deprecated ActivityTestRule from test cases. Upgrade test dependencies to latest stable version.
This commit is contained in:
Kelson 2023-01-14 14:41:42 +01:00 committed by GitHub
commit 744eda8287
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 39 additions and 53 deletions

View File

@ -19,11 +19,10 @@
package org.kiwix.kiwixmobile package org.kiwix.kiwixmobile
import android.Manifest.permission import android.Manifest.permission
import android.app.Activity
import android.content.Context import android.content.Context
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
import androidx.test.rule.ActivityTestRule
import androidx.test.rule.GrantPermissionRule import androidx.test.rule.GrantPermissionRule
import org.junit.Rule import org.junit.Rule
import org.junit.runner.RunWith import org.junit.runner.RunWith
@ -34,7 +33,7 @@ import org.kiwix.kiwixmobile.main.KiwixMainActivity
@RunWith(AndroidJUnit4::class) @RunWith(AndroidJUnit4::class)
abstract class BaseActivityTest { abstract class BaseActivityTest {
@get:Rule @get:Rule
open var activityRule = ActivityTestRule(KiwixMainActivity::class.java) open var activityScenarioRule = ActivityScenarioRule(KiwixMainActivity::class.java)
@get:Rule @get:Rule
var readPermissionRule: GrantPermissionRule = var readPermissionRule: GrantPermissionRule =
@ -48,16 +47,6 @@ abstract class BaseActivityTest {
getInstrumentation().targetContext.applicationContext getInstrumentation().targetContext.applicationContext
} }
protected inline fun <reified T : Activity> activityTestRule(
noinline beforeActivityAction: (() -> Unit)? = null
) =
object : ActivityTestRule<T>(T::class.java) {
override fun beforeActivityLaunched() {
super.beforeActivityLaunched()
beforeActivityAction?.invoke()
}
}
protected fun testComponent(): TestComponent = DaggerTestComponent.builder() protected fun testComponent(): TestComponent = DaggerTestComponent.builder()
.context(context) .context(context)
.build() .build()

View File

@ -18,7 +18,6 @@
package org.kiwix.kiwixmobile.help package org.kiwix.kiwixmobile.help
import android.os.Build import android.os.Build
import androidx.test.internal.runner.junit4.statement.UiThreadStatement.runOnUiThread
import androidx.test.platform.app.InstrumentationRegistry import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice import androidx.test.uiautomator.UiDevice
import leakcanary.LeakAssertions import leakcanary.LeakAssertions
@ -43,7 +42,9 @@ class HelpFragmentTest : BaseActivityTest() {
@Test @Test
fun verifyHelpActivity() { fun verifyHelpActivity() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
runOnUiThread { activityRule.activity.navigate(R.id.helpFragment) } activityScenarioRule.scenario.onActivity {
it.navigate(R.id.helpFragment)
}
help { help {
clickOnWhatDoesKiwixDo() clickOnWhatDoesKiwixDo()
assertWhatDoesKiwixDoIsExpanded() assertWhatDoesKiwixDoIsExpanded()

View File

@ -25,7 +25,6 @@ import androidx.test.core.app.ActivityScenario
import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest import androidx.test.filters.LargeTest
import androidx.test.platform.app.InstrumentationRegistry import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.rule.ActivityTestRule
import androidx.test.uiautomator.UiDevice import androidx.test.uiautomator.UiDevice
import com.adevinta.android.barista.interaction.BaristaSleepInteractions import com.adevinta.android.barista.interaction.BaristaSleepInteractions
import leakcanary.LeakAssertions import leakcanary.LeakAssertions
@ -43,15 +42,6 @@ import org.kiwix.kiwixmobile.testutils.TestUtils
@LargeTest @LargeTest
@RunWith(AndroidJUnit4::class) @RunWith(AndroidJUnit4::class)
class InitialDownloadTest : BaseActivityTest() { class InitialDownloadTest : BaseActivityTest() {
override var activityRule: ActivityTestRule<KiwixMainActivity> = activityTestRule {
PreferenceManager.getDefaultSharedPreferences(context).edit {
putBoolean(SharedPreferenceUtil.PREF_SHOW_INTRO, false)
putBoolean(SharedPreferenceUtil.PREF_WIFI_ONLY, false)
putBoolean(SharedPreferenceUtil.PREF_SHOW_STORAGE_OPTION, true)
putBoolean(SharedPreferenceUtil.IS_PLAY_STORE_BUILD, true)
putBoolean(SharedPreferenceUtil.PREF_IS_TEST, true)
}
}
@Rule @Rule
@JvmField @JvmField
@ -60,6 +50,13 @@ class InitialDownloadTest : BaseActivityTest() {
@Before @Before
override fun waitForIdle() { override fun waitForIdle() {
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).waitForIdle() UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).waitForIdle()
PreferenceManager.getDefaultSharedPreferences(context).edit {
putBoolean(SharedPreferenceUtil.PREF_SHOW_INTRO, false)
putBoolean(SharedPreferenceUtil.PREF_WIFI_ONLY, false)
putBoolean(SharedPreferenceUtil.PREF_SHOW_STORAGE_OPTION, true)
putBoolean(SharedPreferenceUtil.IS_PLAY_STORE_BUILD, true)
putBoolean(SharedPreferenceUtil.PREF_IS_TEST, true)
}
} }
@Test @Test

View File

@ -20,7 +20,6 @@ package org.kiwix.kiwixmobile.intro
import android.os.Build import android.os.Build
import androidx.core.content.edit import androidx.core.content.edit
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import androidx.test.internal.runner.junit4.statement.UiThreadStatement.runOnUiThread
import androidx.test.platform.app.InstrumentationRegistry import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice import androidx.test.uiautomator.UiDevice
import leakcanary.LeakAssertions import leakcanary.LeakAssertions
@ -41,7 +40,9 @@ class IntroFragmentTest : BaseActivityTest() {
@Test @Test
fun viewIsSwipeableAndNavigatesToMain() { fun viewIsSwipeableAndNavigatesToMain() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
runOnUiThread { activityRule.activity.navigate(R.id.introFragment) } activityScenarioRule.scenario.onActivity {
it.navigate(R.id.introFragment)
}
intro(IntroRobot::swipeLeft) clickGetStarted {} intro(IntroRobot::swipeLeft) clickGetStarted {}
LeakAssertions.assertNoLeaks() LeakAssertions.assertNoLeaks()
} }

View File

@ -21,7 +21,6 @@ package org.kiwix.kiwixmobile.mimetype
import androidx.core.content.edit import androidx.core.content.edit
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import androidx.test.platform.app.InstrumentationRegistry import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.rule.ActivityTestRule
import androidx.test.uiautomator.UiDevice import androidx.test.uiautomator.UiDevice
import org.junit.Assert import org.junit.Assert
import org.junit.Before import org.junit.Before
@ -31,25 +30,21 @@ import org.kiwix.kiwixmobile.BaseActivityTest
import org.kiwix.kiwixmobile.core.NightModeConfig import org.kiwix.kiwixmobile.core.NightModeConfig
import org.kiwix.kiwixmobile.core.reader.ZimFileReader import org.kiwix.kiwixmobile.core.reader.ZimFileReader
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import org.kiwix.kiwixmobile.main.KiwixMainActivity
import java.io.File import java.io.File
import java.io.FileOutputStream import java.io.FileOutputStream
import java.io.OutputStream import java.io.OutputStream
class MimeTypeTest : BaseActivityTest() { class MimeTypeTest : BaseActivityTest() {
override var activityRule: ActivityTestRule<KiwixMainActivity> = activityTestRule { @Before
override fun waitForIdle() {
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).waitForIdle()
PreferenceManager.getDefaultSharedPreferences(context).edit { PreferenceManager.getDefaultSharedPreferences(context).edit {
putBoolean(SharedPreferenceUtil.PREF_SHOW_INTRO, false) putBoolean(SharedPreferenceUtil.PREF_SHOW_INTRO, false)
putBoolean(SharedPreferenceUtil.PREF_WIFI_ONLY, false) putBoolean(SharedPreferenceUtil.PREF_WIFI_ONLY, false)
} }
} }
@Before
override fun waitForIdle() {
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).waitForIdle()
}
@Test @Test
fun testMimeType() { fun testMimeType() {
val loadFileStream = MimeTypeTest::class.java.classLoader.getResourceAsStream("testzim.zim") val loadFileStream = MimeTypeTest::class.java.classLoader.getResourceAsStream("testzim.zim")

View File

@ -19,7 +19,6 @@
package org.kiwix.kiwixmobile.note package org.kiwix.kiwixmobile.note
import android.os.Build import android.os.Build
import androidx.test.internal.runner.junit4.statement.UiThreadStatement
import androidx.test.platform.app.InstrumentationRegistry import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice import androidx.test.uiautomator.UiDevice
import leakcanary.LeakAssertions import leakcanary.LeakAssertions
@ -44,7 +43,9 @@ class NoteFragmentTest : BaseActivityTest() {
@Test @Test
fun verifyNoteFragment() { fun verifyNoteFragment() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
UiThreadStatement.runOnUiThread { activityRule.activity.navigate(R.id.notesFragment) } activityScenarioRule.scenario.onActivity {
it.navigate(R.id.notesFragment)
}
note { note {
assertToolbarExist() assertToolbarExist()
assertNoteRecyclerViewExist() assertNoteRecyclerViewExist()

View File

@ -21,9 +21,9 @@ import android.os.Build
import androidx.core.content.edit import androidx.core.content.edit
import androidx.core.net.toUri import androidx.core.net.toUri
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import androidx.test.core.app.ActivityScenario
import androidx.test.internal.runner.junit4.statement.UiThreadStatement import androidx.test.internal.runner.junit4.statement.UiThreadStatement
import androidx.test.platform.app.InstrumentationRegistry import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.rule.ActivityTestRule
import androidx.test.uiautomator.UiDevice import androidx.test.uiautomator.UiDevice
import leakcanary.LeakAssertions import leakcanary.LeakAssertions
import org.junit.After import org.junit.After
@ -46,7 +46,11 @@ class SearchFragmentTest : BaseActivityTest() {
@JvmField @JvmField
var retryRule = RetryRule() var retryRule = RetryRule()
override var activityRule: ActivityTestRule<KiwixMainActivity> = activityTestRule { private lateinit var kiwixMainActivity: KiwixMainActivity
@Before
override fun waitForIdle() {
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).waitForIdle()
PreferenceManager.getDefaultSharedPreferences(context).edit { PreferenceManager.getDefaultSharedPreferences(context).edit {
putBoolean(SharedPreferenceUtil.PREF_SHOW_INTRO, false) putBoolean(SharedPreferenceUtil.PREF_SHOW_INTRO, false)
putBoolean(SharedPreferenceUtil.PREF_WIFI_ONLY, false) putBoolean(SharedPreferenceUtil.PREF_WIFI_ONLY, false)
@ -54,15 +58,13 @@ class SearchFragmentTest : BaseActivityTest() {
} }
} }
@Before
override fun waitForIdle() {
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).waitForIdle()
}
@Test @Test
fun searchFragmentSimple() { fun searchFragmentSimple() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
UiThreadStatement.runOnUiThread { activityRule.activity.navigate(R.id.libraryFragment) } ActivityScenario.launch(KiwixMainActivity::class.java).onActivity {
kiwixMainActivity = it
kiwixMainActivity.navigate(R.id.libraryFragment)
}
val loadFileStream = val loadFileStream =
SearchFragmentTest::class.java.classLoader.getResourceAsStream("testzim.zim") SearchFragmentTest::class.java.classLoader.getResourceAsStream("testzim.zim")
val zimFile = File(context.cacheDir, "testzim.zim") val zimFile = File(context.cacheDir, "testzim.zim")
@ -79,7 +81,7 @@ class SearchFragmentTest : BaseActivityTest() {
} }
} }
UiThreadStatement.runOnUiThread { UiThreadStatement.runOnUiThread {
activityRule.activity.navigate( kiwixMainActivity.navigate(
actionNavigationLibraryToNavigationReader() actionNavigationLibraryToNavigationReader()
.apply { zimFileUri = zimFile.toUri().toString() } .apply { zimFileUri = zimFile.toUri().toString() }
) )
@ -87,7 +89,7 @@ class SearchFragmentTest : BaseActivityTest() {
search { checkZimFileSearchSuccessful(R.id.readerFragment) } search { checkZimFileSearchSuccessful(R.id.readerFragment) }
UiThreadStatement.runOnUiThread { UiThreadStatement.runOnUiThread {
if (zimFile.canRead()) { if (zimFile.canRead()) {
activityRule.activity.openSearch(searchString = "Android") kiwixMainActivity.openSearch(searchString = "Android")
} else { } else {
throw RuntimeException( throw RuntimeException(
"File $zimFile is not readable." + "File $zimFile is not readable." +

View File

@ -197,7 +197,7 @@ object Libs {
/** /**
* https://developer.android.com/testing * https://developer.android.com/testing
*/ */
const val androidx_test_rules: String = "androidx.test:rules:" + Versions.androidx_test const val androidx_test_rules: String = "androidx.test:rules:" + Versions.androidx_test_core
/** /**
* https://developer.android.com/testing * https://developer.android.com/testing

View File

@ -16,7 +16,7 @@ object Versions {
const val org_jetbrains_kotlinx_kotlinx_coroutines: String = "1.4.1" const val org_jetbrains_kotlinx_kotlinx_coroutines: String = "1.4.1"
const val androidx_test_espresso: String = "3.4.0" const val androidx_test_espresso: String = "3.5.0"
const val androidx_test_espresso_contrib: String = "3.3.0" const val androidx_test_espresso_contrib: String = "3.3.0"
@ -36,9 +36,9 @@ object Versions {
const val com_jakewharton: String = "10.2.3" const val com_jakewharton: String = "10.2.3"
const val androidx_test: String = "1.4.0" const val androidx_test: String = "1.5.1"
const val androidx_test_core: String = "1.5.0-alpha02" const val androidx_test_core: String = "1.5.0"
const val androidx_test_orchestrator: String = "1.4.1" const val androidx_test_orchestrator: String = "1.4.1"
@ -66,7 +66,7 @@ object Versions {
const val preference_ktx: String = "1.1.1" const val preference_ktx: String = "1.1.1"
const val junit_jupiter: String = "5.7.0" const val junit_jupiter: String = "5.9.1"
const val assertj_core: String = "3.18.1" const val assertj_core: String = "3.18.1"
@ -112,7 +112,7 @@ object Versions {
const val aapt2: String = "4.1.1-6503028" const val aapt2: String = "4.1.1-6503028"
const val junit: String = "1.1.2" const val junit: String = "1.1.4"
/** /**
* Current version: "6.2" * Current version: "6.2"