Skip to content

Commit

Permalink
Merge pull request #3986 from kiwix/Fixes#1999
Browse files Browse the repository at this point in the history
Added (unit test/UI test) cases for the `DarkModeViewPainter`.
  • Loading branch information
kelson42 authored Sep 9, 2024
2 parents f2534d3 + be33a59 commit 4fb63f0
Show file tree
Hide file tree
Showing 8 changed files with 506 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ fun downloadRobot(func: DownloadRobot.() -> Unit) =

class DownloadRobot : BaseRobot() {

private var retryCountForDataToLoad = 10
private var retryCountForCheckDownloadStart = 10

fun clickLibraryOnBottomNav() {
Expand All @@ -57,20 +56,30 @@ class DownloadRobot : BaseRobot() {
clickOn(ViewId(R.id.downloadsFragment))
}

fun waitForDataToLoad() {
fun waitForDataToLoad(retryCountForDataToLoad: Int = 10) {
try {
isVisible(TextId(string.your_languages))
} catch (e: RuntimeException) {
if (retryCountForDataToLoad > 0) {
retryCountForDataToLoad--
waitForDataToLoad()
// refresh the data if there is "Swipe Down for Library" visible on the screen.
refreshOnlineListIfSwipeDownForLibraryTextVisible()
waitForDataToLoad(retryCountForDataToLoad - 1)
return
}
// throw the exception when there is no more retry left.
throw RuntimeException("Couldn't load the online library list.\n Original exception = $e")
}
}

private fun refreshOnlineListIfSwipeDownForLibraryTextVisible() {
try {
onView(withText(string.swipe_down_for_library)).check(matches(isDisplayed()))
refreshOnlineList()
} catch (e: RuntimeException) {
// do nothing as the view is not visible
}
}

fun checkIfZimFileDownloaded() {
pauseForBetterTestPerformance()
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class InitialDownloadRobot : BaseRobot() {
isVisible(TextId(string.your_languages))
} catch (e: RuntimeException) {
if (retryCountForDataToLoad > 0) {
// refresh the data if there is "Swipe Down for Library" visible on the screen.
refreshOnlineListIfSwipeDownForLibraryTextVisible()
waitForDataToLoad(retryCountForDataToLoad - 1)
return
}
Expand All @@ -79,6 +81,15 @@ class InitialDownloadRobot : BaseRobot() {
}
}

private fun refreshOnlineListIfSwipeDownForLibraryTextVisible() {
try {
onView(withText(string.swipe_down_for_library)).check(matches(isDisplayed()))
refreshOnlineList()
} catch (e: RuntimeException) {
// do nothing as the view is not visible
}
}

fun downloadZimFile() {
pauseForBetterTestPerformance()
testFlakyView({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,19 @@ package org.kiwix.kiwixmobile.language
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.isChecked
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.isNotChecked
import androidx.test.espresso.matcher.ViewMatchers.withId
import applyWithViewHierarchyPrinting
import com.adevinta.android.barista.interaction.BaristaSleepInteractions
import com.adevinta.android.barista.interaction.BaristaSwipeRefreshInteractions.refresh
import junit.framework.AssertionFailedError
import org.kiwix.kiwixmobile.BaseRobot
import org.kiwix.kiwixmobile.Findable.StringId.TextId
import org.kiwix.kiwixmobile.Findable.Text
import org.kiwix.kiwixmobile.Findable.ViewId
import org.kiwix.kiwixmobile.Findable.StringId.TextId
import org.kiwix.kiwixmobile.R
import org.kiwix.kiwixmobile.core.R.string
import org.kiwix.kiwixmobile.testutils.TestUtils
Expand All @@ -50,6 +53,8 @@ class LanguageRobot : BaseRobot() {
isVisible(TextId(string.your_languages))
} catch (e: RuntimeException) {
if (retryCountForDataToLoad > 0) {
// refresh the data if there is "Swipe Down for Library" visible on the screen.
refreshOnlineListIfSwipeDownForLibraryTextVisible()
waitForDataToLoad(retryCountForDataToLoad - 1)
return
}
Expand All @@ -58,6 +63,19 @@ class LanguageRobot : BaseRobot() {
}
}

private fun refreshOnlineListIfSwipeDownForLibraryTextVisible() {
try {
onView(ViewMatchers.withText(string.swipe_down_for_library)).check(matches(isDisplayed()))
refreshOnlineList()
} catch (e: RuntimeException) {
// do nothing as the view is not visible
}
}

private fun refreshOnlineList() {
refresh(R.id.librarySwipeRefresh)
}

fun clickOnLanguageIcon() {
// Wait for a few seconds to properly saved selected language.
BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS.toLong())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Kiwix Android
* Copyright (c) 2024 Kiwix <android.kiwix.org>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

package org.kiwix.kiwixmobile.main

import android.view.View
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.matcher.ViewMatchers.withText
import applyWithViewHierarchyPrinting
import org.junit.Assert.assertEquals
import org.kiwix.kiwixmobile.BaseRobot
import org.kiwix.kiwixmobile.core.R
import org.kiwix.kiwixmobile.core.main.KiwixWebView
import org.kiwix.kiwixmobile.testutils.TestUtils.testFlakyView
import org.kiwix.kiwixmobile.utils.StandardActions.enterSettings
import org.kiwix.kiwixmobile.utils.StandardActions.openDrawer

fun darkModeViewPainter(func: DarkModeViewPainterRobot.() -> Unit) =
DarkModeViewPainterRobot().applyWithViewHierarchyPrinting(func)

class DarkModeViewPainterRobot : BaseRobot() {

fun openSettings() {
openDrawer()
enterSettings()
}

fun enableTheDarkMode() {
testFlakyView({
onView(withText(R.string.on)).perform(ViewActions.click())
})
}

fun enableTheLightMode() {
testFlakyView({
onView(withText(R.string.off)).perform(ViewActions.click())
})
}

fun assertNightModeEnabled(kiwixWebView: KiwixWebView) {
assertEquals(kiwixWebView.layerType, View.LAYER_TYPE_HARDWARE)
}

fun assertLightModeEnabled(kiwixWebView: KiwixWebView) {
assertEquals(kiwixWebView.layerType, View.LAYER_TYPE_NONE)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/*
* Kiwix Android
* Copyright (c) 2024 Kiwix <android.kiwix.org>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

package org.kiwix.kiwixmobile.main

import androidx.core.content.ContextCompat
import androidx.core.content.edit
import androidx.core.net.toUri
import androidx.lifecycle.Lifecycle
import androidx.navigation.fragment.NavHostFragment
import androidx.preference.PreferenceManager
import androidx.test.core.app.ActivityScenario
import androidx.test.espresso.accessibility.AccessibilityChecks
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.Rule
import org.junit.Test
import org.kiwix.kiwixmobile.BaseActivityTest
import org.kiwix.kiwixmobile.R
import org.kiwix.kiwixmobile.core.utils.LanguageUtils
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import org.kiwix.kiwixmobile.nav.destination.library.LocalLibraryFragmentDirections
import org.kiwix.kiwixmobile.nav.destination.reader.KiwixReaderFragment
import org.kiwix.kiwixmobile.settings.SettingsRobot
import org.kiwix.kiwixmobile.settings.settingsRobo
import org.kiwix.kiwixmobile.testutils.RetryRule
import org.kiwix.kiwixmobile.testutils.TestUtils
import java.io.File
import java.io.FileOutputStream
import java.io.OutputStream

class DarkModeViewPainterTest : BaseActivityTest() {
@Rule
@JvmField
var retryRule = RetryRule()
private lateinit var kiwixMainActivity: KiwixMainActivity

@Before
override fun waitForIdle() {
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).apply {
if (TestUtils.isSystemUINotRespondingDialogVisible(this)) {
TestUtils.closeSystemDialogs(context, this)
}
waitForIdle()
}
PreferenceManager.getDefaultSharedPreferences(
InstrumentationRegistry.getInstrumentation().targetContext.applicationContext
).edit {
putBoolean(SharedPreferenceUtil.PREF_SHOW_INTRO, false)
putBoolean(SharedPreferenceUtil.PREF_WIFI_ONLY, false)
putBoolean(SharedPreferenceUtil.PREF_IS_TEST, true)
putBoolean(SharedPreferenceUtil.PREF_EXTERNAL_LINK_POPUP, true)
putBoolean(SharedPreferenceUtil.PREF_SHOW_SHOWCASE, false)
putBoolean(SharedPreferenceUtil.PREF_PLAY_STORE_RESTRICTION, false)
putString(SharedPreferenceUtil.PREF_LANG, "en")
}
activityScenario = ActivityScenario.launch(KiwixMainActivity::class.java).apply {
moveToState(Lifecycle.State.RESUMED)
onActivity {
LanguageUtils.handleLocaleChange(
it,
"en",
SharedPreferenceUtil(context)
)
}
}
}

init {
AccessibilityChecks.enable().setRunChecksFromRootView(true)
}

@Test
fun testDarkMode() {
toggleDarkMode(true)
openZimFileInReader()
verifyDarkMode(true)
toggleDarkMode(false)
openZimFileInReader()
verifyDarkMode(false)
}

private fun openZimFileInReader() {
activityScenario.onActivity {
kiwixMainActivity = it
kiwixMainActivity.navigate(R.id.libraryFragment)
}
loadZimFileInReader()
}

private fun toggleDarkMode(enable: Boolean) {
darkModeViewPainter(DarkModeViewPainterRobot::openSettings)
settingsRobo(SettingsRobot::clickNightModePreference)
darkModeViewPainter {
if (enable) {
enableTheDarkMode()
} else {
enableTheLightMode()
}
pressBack()
}
}

private fun verifyDarkMode(isEnabled: Boolean) {
UiThreadStatement.runOnUiThread {
val navHostFragment: NavHostFragment =
kiwixMainActivity.supportFragmentManager
.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val kiwixReaderFragment =
navHostFragment.childFragmentManager.fragments[0] as KiwixReaderFragment
val currentWebView = kiwixReaderFragment.getCurrentWebView()
currentWebView?.let {
darkModeViewPainter {
if (isEnabled) {
assertNightModeEnabled(it)
} else {
assertLightModeEnabled(it)
}
}
} ?: run {
throw RuntimeException(
"Could not check the dark mode enable or not because zim file is not loaded in the reader"
)
}
}
}

private fun loadZimFileInReader() {
val loadFileStream =
DarkModeViewPainterTest::class.java.classLoader.getResourceAsStream("testzim.zim")
val zimFile = File(
ContextCompat.getExternalFilesDirs(context, null)[0],
"testzim.zim"
)
if (zimFile.exists()) zimFile.delete()
zimFile.createNewFile()
loadFileStream.use { inputStream ->
val outputStream: OutputStream = FileOutputStream(zimFile)
outputStream.use { it ->
val buffer = ByteArray(inputStream.available())
var length: Int
while (inputStream.read(buffer).also { length = it } > 0) {
it.write(buffer, 0, length)
}
}
}
UiThreadStatement.runOnUiThread {
kiwixMainActivity.navigate(
LocalLibraryFragmentDirections.actionNavigationLibraryToNavigationReader()
.apply { zimFileUri = zimFile.toUri().toString() }
)
}
}
}
2 changes: 1 addition & 1 deletion core/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
</PreferenceCategory>

<PreferenceCategory
android:key="pref_notes"
android:key="pref_bookmark"
android:title="@string/bookmarks"
app:iconSpaceReserved="false">

Expand Down
Loading

0 comments on commit 4fb63f0

Please sign in to comment.