Enhancement in tests for reducing CI failure

This commit is contained in:
MohitMali 2022-11-10 18:24:24 +05:30
parent ef545094a2
commit 5198225972
17 changed files with 105 additions and 33 deletions

View File

@ -61,4 +61,6 @@ abstract class BaseActivityTest {
protected fun testComponent(): TestComponent = DaggerTestComponent.builder() protected fun testComponent(): TestComponent = DaggerTestComponent.builder()
.context(context) .context(context)
.build() .build()
abstract fun waitForIdle()
} }

View File

@ -18,12 +18,20 @@
package org.kiwix.kiwixmobile.help package org.kiwix.kiwixmobile.help
import androidx.test.internal.runner.junit4.statement.UiThreadStatement.runOnUiThread import androidx.test.internal.runner.junit4.statement.UiThreadStatement.runOnUiThread
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice
import org.junit.Before
import org.junit.Test import org.junit.Test
import org.kiwix.kiwixmobile.BaseActivityTest import org.kiwix.kiwixmobile.BaseActivityTest
import org.kiwix.kiwixmobile.R import org.kiwix.kiwixmobile.R
class HelpFragmentTest : BaseActivityTest() { class HelpFragmentTest : BaseActivityTest() {
@Before
override fun waitForIdle() {
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).waitForIdle()
}
@Test @Test
fun verifyHelpActivity() { fun verifyHelpActivity() {
runOnUiThread { activityRule.activity.navigate(R.id.helpFragment) } runOnUiThread { activityRule.activity.navigate(R.id.helpFragment) }

View File

@ -47,7 +47,7 @@ class InitialDownloadTest : BaseActivityTest() {
} }
@Before @Before
fun waitForIdle() { override fun waitForIdle() {
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).waitForIdle() UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).waitForIdle()
} }

View File

@ -18,6 +18,9 @@
package org.kiwix.kiwixmobile.intro package org.kiwix.kiwixmobile.intro
import androidx.test.internal.runner.junit4.statement.UiThreadStatement.runOnUiThread import androidx.test.internal.runner.junit4.statement.UiThreadStatement.runOnUiThread
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice
import org.junit.Before
import org.junit.Test import org.junit.Test
import org.kiwix.kiwixmobile.BaseActivityTest import org.kiwix.kiwixmobile.BaseActivityTest
import org.kiwix.kiwixmobile.R import org.kiwix.kiwixmobile.R
@ -27,6 +30,11 @@ class IntroFragmentTest : BaseActivityTest() {
@Test @Test
fun viewIsSwipeableAndNavigatesToMain() { fun viewIsSwipeableAndNavigatesToMain() {
runOnUiThread { activityRule.activity.navigate(R.id.introFragment) } runOnUiThread { activityRule.activity.navigate(R.id.introFragment) }
intro(IntroRobot::swipeLeft) clickGetStarted { } intro(IntroRobot::swipeLeft) clickGetStarted {}
}
@Before
override fun waitForIdle() {
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).waitForIdle()
} }
} }

View File

@ -32,14 +32,12 @@ fun intro(func: IntroRobot.() -> Unit) = IntroRobot().applyWithViewHierarchyPrin
class IntroRobot : BaseRobot() { class IntroRobot : BaseRobot() {
private val getStarted = ViewId(R.id.get_started) private val getStarted = ViewId(R.id.get_started)
private val viewPager = ViewId(R.id.view_pager)
fun swipeLeft() { fun swipeLeft() {
isVisible(getStarted) isVisible(getStarted)
isVisible(TextId(R.string.welcome_to_the_family)) isVisible(TextId(R.string.welcome_to_the_family))
isVisible(TextId(R.string.humankind_knowledge)) isVisible(TextId(R.string.humankind_knowledge))
attempt(10) { attempt(10) {
isVisible(viewPager).swipeLeft()
isVisible(TextId(R.string.save_books_offline)) isVisible(TextId(R.string.save_books_offline))
isVisible(TextId(R.string.download_books_message)) isVisible(TextId(R.string.download_books_message))
} }

View File

@ -18,13 +18,9 @@
package org.kiwix.kiwixmobile.language package org.kiwix.kiwixmobile.language
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import applyWithViewHierarchyPrinting import applyWithViewHierarchyPrinting
import org.kiwix.kiwixmobile.BaseRobot import org.kiwix.kiwixmobile.BaseRobot
import org.kiwix.kiwixmobile.Findable import org.kiwix.kiwixmobile.Findable.Text
import org.kiwix.kiwixmobile.Findable.ViewId import org.kiwix.kiwixmobile.Findable.ViewId
import org.kiwix.kiwixmobile.R import org.kiwix.kiwixmobile.R
@ -36,7 +32,7 @@ class LanguageRobot : BaseRobot() {
fun waitForDataToLoad() { fun waitForDataToLoad() {
try { try {
isVisible(Findable.Text("Off the Grid")) isVisible(Text("Off the Grid"))
} catch (e: RuntimeException) { } catch (e: RuntimeException) {
if (retryCountForDataToLoad > 0) { if (retryCountForDataToLoad > 0) {
retryCountForDataToLoad-- retryCountForDataToLoad--
@ -50,9 +46,9 @@ class LanguageRobot : BaseRobot() {
} }
fun searchAndSaveLanguage(searchLanguage: String, matchLanguage: String) { fun searchAndSaveLanguage(searchLanguage: String, matchLanguage: String) {
onView(withId(R.id.menu_language_search)).perform(click()) clickOn(ViewId(R.id.menu_language_search))
isVisible(ViewId(androidx.appcompat.R.id.search_src_text)).text = searchLanguage isVisible(ViewId(androidx.appcompat.R.id.search_src_text)).text = searchLanguage
onView(withText(matchLanguage)).perform(click()) clickOn(Text(matchLanguage))
onView(withId(R.id.menu_language_save)).perform(click()) clickOn(ViewId(R.id.menu_language_save))
} }
} }

View File

@ -21,6 +21,8 @@ import androidx.core.content.edit
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import androidx.test.core.app.ActivityScenario import androidx.test.core.app.ActivityScenario
import androidx.test.platform.app.InstrumentationRegistry import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice
import org.junit.After
import org.junit.Before import org.junit.Before
import org.junit.Test import org.junit.Test
import org.kiwix.kiwixmobile.BaseActivityTest import org.kiwix.kiwixmobile.BaseActivityTest
@ -33,12 +35,15 @@ import org.kiwix.kiwixmobile.webserver.ZimHostRobot
class TopLevelDestinationTest : BaseActivityTest() { class TopLevelDestinationTest : BaseActivityTest() {
@Before @Before
fun setUp() { override fun waitForIdle() {
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).waitForIdle()
PreferenceManager.getDefaultSharedPreferences( PreferenceManager.getDefaultSharedPreferences(
InstrumentationRegistry.getInstrumentation().targetContext.applicationContext InstrumentationRegistry.getInstrumentation().targetContext.applicationContext
).edit { ).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)
putBoolean(SharedPreferenceUtil.PREF_IS_TEST, true)
putBoolean(SharedPreferenceUtil.PREF_EXTERNAL_LINK_POPUP, true)
} }
} }
@ -72,4 +77,11 @@ class TopLevelDestinationTest : BaseActivityTest() {
pressBack() pressBack()
} }
} }
@After
fun setIsTestPreference() {
PreferenceManager.getDefaultSharedPreferences(context).edit {
putBoolean(SharedPreferenceUtil.PREF_IS_TEST, false)
}
}
} }

View File

@ -20,8 +20,11 @@ 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.rule.ActivityTestRule import androidx.test.rule.ActivityTestRule
import androidx.test.uiautomator.UiDevice
import org.junit.Assert import org.junit.Assert
import org.junit.Before
import org.junit.Test import org.junit.Test
import org.kiwix.kiwixlib.JNIKiwixReader import org.kiwix.kiwixlib.JNIKiwixReader
import org.kiwix.kiwixmobile.BaseActivityTest import org.kiwix.kiwixmobile.BaseActivityTest
@ -42,6 +45,11 @@ class MimeTypeTest : BaseActivityTest() {
} }
} }
@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

@ -18,18 +18,31 @@
package org.kiwix.kiwixmobile.note package org.kiwix.kiwixmobile.note
import android.os.Build
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.uiautomator.UiDevice
import org.junit.Before
import org.junit.Test import org.junit.Test
import org.kiwix.kiwixmobile.BaseActivityTest import org.kiwix.kiwixmobile.BaseActivityTest
import org.kiwix.kiwixmobile.R import org.kiwix.kiwixmobile.R
class NoteFragmentTest : BaseActivityTest() { class NoteFragmentTest : BaseActivityTest() {
@Before
override fun waitForIdle() {
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).waitForIdle()
}
@Test @Test
fun verifyNoteFragment() { fun verifyNoteFragment() {
UiThreadStatement.runOnUiThread { activityRule.activity.navigate(R.id.notesFragment) } if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
note { UiThreadStatement.runOnUiThread { activityRule.activity.navigate(R.id.notesFragment) }
assertNoteRecyclerViewExist() note {
assertSwitchWidgetExist() assertToolbarExist()
assertNoteRecyclerViewExist()
assertSwitchWidgetExist()
}
} }
} }
} }

View File

@ -26,7 +26,7 @@ fun note(func: NoteRobot.() -> Unit) = NoteRobot().apply(func)
class NoteRobot : BaseRobot() { class NoteRobot : BaseRobot() {
init { fun assertToolbarExist() {
isVisible(Findable.ViewId(R.id.toolbar)) isVisible(Findable.ViewId(R.id.toolbar))
} }

View File

@ -24,6 +24,7 @@ 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.rule.ActivityTestRule
import androidx.test.uiautomator.UiDevice import androidx.test.uiautomator.UiDevice
import org.junit.After
import org.junit.Before import org.junit.Before
import org.junit.Test import org.junit.Test
import org.kiwix.kiwixmobile.BaseActivityTest import org.kiwix.kiwixmobile.BaseActivityTest
@ -40,15 +41,17 @@ class SearchFragmentTest : BaseActivityTest() {
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)
putBoolean(SharedPreferenceUtil.PREF_IS_TEST, true)
} }
} }
@Before @Before
fun waitForIdle() { override fun waitForIdle() {
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).waitForIdle() UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).waitForIdle()
} }
@Test fun searchFragmentSimple() { @Test
fun searchFragmentSimple() {
UiThreadStatement.runOnUiThread { activityRule.activity.navigate(R.id.libraryFragment) } UiThreadStatement.runOnUiThread { activityRule.activity.navigate(R.id.libraryFragment) }
val loadFileStream = val loadFileStream =
SearchFragmentTest::class.java.classLoader.getResourceAsStream("testzim.zim") SearchFragmentTest::class.java.classLoader.getResourceAsStream("testzim.zim")
@ -88,4 +91,11 @@ class SearchFragmentTest : BaseActivityTest() {
checkZimFileSearchSuccessful(R.id.readerFragment) checkZimFileSearchSuccessful(R.id.readerFragment)
} }
} }
@After
fun setIsTestPreference() {
PreferenceManager.getDefaultSharedPreferences(context).edit {
putBoolean(SharedPreferenceUtil.PREF_IS_TEST, false)
}
}
} }

View File

@ -33,17 +33,24 @@ import org.kiwix.kiwixmobile.testutils.TestUtils.TEST_PAUSE_MS
import org.kiwix.kiwixmobile.utils.StandardActions import org.kiwix.kiwixmobile.utils.StandardActions
class KiwixSettingsFragmentTest { class KiwixSettingsFragmentTest {
@Rule @JvmField var activityTestRule = ActivityTestRule( @Rule
@JvmField
var activityTestRule = ActivityTestRule(
KiwixMainActivity::class.java KiwixMainActivity::class.java
) )
@Rule @JvmField var readPermissionRule: GrantPermissionRule = @Rule
@JvmField
var readPermissionRule: GrantPermissionRule =
GrantPermissionRule.grant(Manifest.permission.READ_EXTERNAL_STORAGE) GrantPermissionRule.grant(Manifest.permission.READ_EXTERNAL_STORAGE)
@Rule @JvmField var writePermissionRule: GrantPermissionRule = @Rule
@JvmField
var writePermissionRule: GrantPermissionRule =
GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE) GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE)
@Before fun setup() { @Before
fun setup() {
// Go to IntroFragment // Go to IntroFragment
UiThreadStatement.runOnUiThread { activityTestRule.activity.navigate(R.id.introFragment) } UiThreadStatement.runOnUiThread { activityTestRule.activity.navigate(R.id.introFragment) }
intro(IntroRobot::swipeLeft) clickGetStarted { } intro(IntroRobot::swipeLeft) clickGetStarted { }

View File

@ -31,7 +31,7 @@ import com.adevinta.android.barista.assertion.BaristaVisibilityAssertions.assert
import org.hamcrest.Matchers import org.hamcrest.Matchers
import org.kiwix.kiwixmobile.BaseRobot import org.kiwix.kiwixmobile.BaseRobot
import org.kiwix.kiwixmobile.Findable.Text import org.kiwix.kiwixmobile.Findable.Text
import org.kiwix.kiwixmobile.R import org.kiwix.kiwixmobile.core.R
/** /**
* Authored by Ayush Shrivastava on 25/8/20 * Authored by Ayush Shrivastava on 25/8/20

View File

@ -245,8 +245,11 @@ class LocalLibraryFragment : BaseFragment() {
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
if (!sharedPreferenceUtil.isPlayStoreBuildWithAndroid11OrAbove()) if (!sharedPreferenceUtil.isPlayStoreBuildWithAndroid11OrAbove() &&
!sharedPreferenceUtil.prefIsTest
) {
checkPermissions() checkPermissions()
}
} }
override fun onDestroyView() { override fun onDestroyView() {

View File

@ -1,5 +1,3 @@
import kotlin.String
/** /**
* Generated by https://github.com/jmfayard/buildSrcVersions * Generated by https://github.com/jmfayard/buildSrcVersions
* *
@ -188,12 +186,13 @@ object Libs {
/** /**
* https://developer.android.com/testing * https://developer.android.com/testing
*/ */
const val androidx_test_core: String = "androidx.test:core:" + Versions.androidx_test const val androidx_test_core: String = "androidx.test:core:" + Versions.androidx_test_core
/** /**
* https://developer.android.com/testing * https://developer.android.com/testing
*/ */
const val orchestrator: String = "androidx.test:orchestrator:" + Versions.androidx_test const val orchestrator: String =
"androidx.test:orchestrator:" + Versions.androidx_test_orchestrator
/** /**
* https://developer.android.com/testing * https://developer.android.com/testing

View File

@ -36,7 +36,11 @@ object Versions {
const val com_jakewharton: String = "10.2.3" const val com_jakewharton: String = "10.2.3"
const val androidx_test: String = "1.3.0" const val androidx_test: String = "1.4.0"
const val androidx_test_core: String = "1.5.0-alpha02"
const val androidx_test_orchestrator: String = "1.4.1"
const val io_objectbox: String = "2.9.1" const val io_objectbox: String = "2.9.1"

View File

@ -59,6 +59,9 @@ class SharedPreferenceUtil @Inject constructor(val context: Context) {
val prefIsFirstRun: Boolean val prefIsFirstRun: Boolean
get() = sharedPreferences.getBoolean(PREF_IS_FIRST_RUN, true) get() = sharedPreferences.getBoolean(PREF_IS_FIRST_RUN, true)
val prefIsTest: Boolean
get() = sharedPreferences.getBoolean(PREF_IS_TEST, false)
val prefFullScreen: Boolean val prefFullScreen: Boolean
get() = sharedPreferences.getBoolean(PREF_FULLSCREEN, false) get() = sharedPreferences.getBoolean(PREF_FULLSCREEN, false)
@ -213,11 +216,12 @@ class SharedPreferenceUtil @Inject constructor(val context: Context) {
const val PREF_WIFI_ONLY = "pref_wifi_only" const val PREF_WIFI_ONLY = "pref_wifi_only"
const val PREF_KIWIX_MOBILE = "kiwix-mobile" const val PREF_KIWIX_MOBILE = "kiwix-mobile"
const val PREF_SHOW_INTRO = "showIntro" const val PREF_SHOW_INTRO = "showIntro"
const val PREF_IS_TEST = "is_test"
private const val PREF_BACK_TO_TOP = "pref_backtotop" private const val PREF_BACK_TO_TOP = "pref_backtotop"
private const val PREF_FULLSCREEN = "pref_fullscreen" private const val PREF_FULLSCREEN = "pref_fullscreen"
private const val PREF_NEW_TAB_BACKGROUND = "pref_newtab_background" private const val PREF_NEW_TAB_BACKGROUND = "pref_newtab_background"
private const val PREF_STORAGE_TITLE = "pref_selected_title" private const val PREF_STORAGE_TITLE = "pref_selected_title"
private const val PREF_EXTERNAL_LINK_POPUP = "pref_external_link_popup" const val PREF_EXTERNAL_LINK_POPUP = "pref_external_link_popup"
const val PREF_SHOW_STORAGE_OPTION = "show_storgae_option" const val PREF_SHOW_STORAGE_OPTION = "show_storgae_option"
private const val PREF_IS_FIRST_RUN = "isFirstRun" private const val PREF_IS_FIRST_RUN = "isFirstRun"
private const val PREF_SHOW_BOOKMARKS_ALL_BOOKS = "show_bookmarks_current_book" private const val PREF_SHOW_BOOKMARKS_ALL_BOOKS = "show_bookmarks_current_book"