Skip to content

Commit

Permalink
Merge pull request #3497 from kiwix/Issue#1532
Browse files Browse the repository at this point in the history
Fixed Refactor LocalFileTransfer UI.
  • Loading branch information
kelson42 authored Oct 23, 2023
2 parents ce45eba + bfc0b4a commit 8cf8e1b
Show file tree
Hide file tree
Showing 17 changed files with 288 additions and 39 deletions.
1 change: 1 addition & 0 deletions app/detekt_baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<ID>MagicNumber:ShareFiles.kt$ShareFiles$24</ID>
<ID>MagicNumber:ZimManageViewModel.kt$ZimManageViewModel$5</ID>
<ID>MagicNumber:ZimManageViewModel.kt$ZimManageViewModel$500</ID>
<ID>MagicNumber:LocalFileTransferFragment.kt$LocalFileTransferFragment$500</ID>
<ID>NestedBlockDepth:LocalLibraryFragment.kt$LocalLibraryFragment$private fun checkPermissions()</ID>
<ID>NestedBlockDepth:PeerGroupHandshake.kt$PeerGroupHandshake$private fun readHandshakeAndExchangeMetaData(): InetAddress?</ID>
<ID>NestedBlockDepth:ReceiverHandShake.kt$ReceiverHandShake$override fun exchangeFileTransferMetadata(inputStream: InputStream, outputStream: OutputStream)</ID>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ class DownloadRobot : BaseRobot() {
}

private fun assertDeleteDialogDisplayed() {
pauseForBetterTestPerformance()
onView(withText("DELETE")).check(matches(isDisplayed()))
}

private fun clickOnDeleteZimFile() {
pauseForBetterTestPerformance()
onView(withText("DELETE")).perform(click())
}

Expand All @@ -73,9 +75,8 @@ class DownloadRobot : BaseRobot() {
longClickOnZimFile()
clickOnFileDeleteIcon()
assertDeleteDialogDisplayed()
BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS.toLong())
clickOnDeleteZimFile()
BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS.toLong())
pauseForBetterTestPerformance()
} catch (e: Exception) {
if (shouldDeleteZimFile) {
throw Exception(
Expand Down Expand Up @@ -122,12 +123,16 @@ class DownloadRobot : BaseRobot() {
}
}

private fun stopDownload() {
clickOn(ViewId(R.id.stop))
}

fun pauseDownload() {
clickOn(ViewId(R.id.pauseResume))
}

fun assertDownloadPaused() {
BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS.toLong())
pauseForBetterTestPerformance()
onView(withText(org.kiwix.kiwixmobile.core.R.string.paused_state)).check(matches(isDisplayed()))
}

Expand All @@ -136,7 +141,7 @@ class DownloadRobot : BaseRobot() {
}

fun assertDownloadResumed() {
BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS.toLong())
pauseForBetterTestPerformance()
onView(withText(org.kiwix.kiwixmobile.core.R.string.paused_state)).check(doesNotExist())
}

Expand All @@ -150,4 +155,34 @@ class DownloadRobot : BaseRobot() {
waitUntilDownloadComplete()
}
}

private fun pauseForBetterTestPerformance() {
BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS.toLong())
}

private fun assertStopDownloadDialogDisplayed() {
pauseForBetterTestPerformance()
isVisible(Text("Stop download?"))
}

private fun clickOnYesButton() {
onView(withText("YES")).perform(click())
}

fun stopDownloadIfAlreadyStarted() {
try {
pauseForBetterTestPerformance()
onView(withId(R.id.stop)).check(matches(isDisplayed()))
stopDownload()
assertStopDownloadDialogDisplayed()
clickOnYesButton()
pauseForBetterTestPerformance()
} catch (e: Exception) {
Log.i(
"DOWNLOAD_TEST",
"Failed to stop download with title [" + zimFileTitle + "]... " +
"Probably because it doesn't download the zim file"
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class DownloadTest : BaseActivityTest() {
deleteZimIfExists(false)
clickDownloadOnBottomNav()
waitForDataToLoad()
stopDownloadIfAlreadyStarted()
downloadZimFile()
assertDownloadStart()
pauseDownload()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@ class InitialDownloadRobot : BaseRobot() {
}

private fun assertDeleteDialogDisplayed() {
pauseForBetterTestPerformance()
onView(withText("DELETE")).check(matches(isDisplayed()))
}

private fun clickOnDeleteZimFile() {
pauseForBetterTestPerformance()
onView(withText("DELETE")).perform(click())
}

Expand All @@ -78,9 +80,8 @@ class InitialDownloadRobot : BaseRobot() {
longClickOnZimFile()
clickOnFileDeleteIcon()
assertDeleteDialogDisplayed()
BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS.toLong())
clickOnDeleteZimFile()
BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS.toLong())
pauseForBetterTestPerformance()
} catch (e: Exception) {
Log.i(
"TEST_DELETE_ZIM",
Expand Down Expand Up @@ -144,4 +145,25 @@ class InitialDownloadRobot : BaseRobot() {
assertDownloadStop()
}
}

private fun pauseForBetterTestPerformance() {
BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS.toLong())
}

fun stopDownloadIfAlreadyStarted() {
try {
pauseForBetterTestPerformance()
onView(withId(R.id.stop)).check(matches(isDisplayed()))
stopDownload()
assertStopDownloadDialogDisplayed()
clickOnYesToConfirm()
pauseForBetterTestPerformance()
} catch (e: Exception) {
Log.i(
"INITIAL_DOWNLOAD_TEST",
"Failed to stop download with title [" + zimFileTitle + "]... " +
"Probably because it doesn't download the zim file"
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class InitialDownloadTest : BaseActivityTest() {
assertLibraryListDisplayed()
refreshList()
waitForDataToLoad()
stopDownloadIfAlreadyStarted()
downloadZimFile()
assertStorageConfigureDialogDisplayed()
clickOnYesToConfirm()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.isChecked
import androidx.test.espresso.matcher.ViewMatchers.isNotChecked
import applyWithViewHierarchyPrinting
import com.adevinta.android.barista.interaction.BaristaSleepInteractions
import junit.framework.AssertionFailedError
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.R
import org.kiwix.kiwixmobile.testutils.TestUtils
import org.kiwix.kiwixmobile.utils.RecyclerViewMatcher

fun language(func: LanguageRobot.() -> Unit) = LanguageRobot().applyWithViewHierarchyPrinting(func)
Expand All @@ -54,6 +56,8 @@ class LanguageRobot : BaseRobot() {
}

fun clickOnLanguageIcon() {
// Wait for a few seconds to properly saved selected language.
BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS.toLong())
clickOn(ViewId(R.id.select_language))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@

package org.kiwix.kiwixmobile.localFileTransfer

import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.assertion.ViewAssertions.doesNotExist
import androidx.test.espresso.matcher.ViewMatchers.withText
import applyWithViewHierarchyPrinting
import com.adevinta.android.barista.interaction.BaristaSleepInteractions
import org.kiwix.kiwixmobile.BaseRobot
import org.kiwix.kiwixmobile.Findable.ViewId
import org.kiwix.kiwixmobile.Findable.StringId.TextId
import org.kiwix.kiwixmobile.Findable.ViewId
import org.kiwix.kiwixmobile.R
import org.kiwix.kiwixmobile.testutils.TestUtils

Expand Down Expand Up @@ -55,4 +58,38 @@ class LocalFileTransferRobot : BaseRobot() {
fun assertLocalLibraryVisible() {
isVisible(TextId(R.string.library))
}

fun assertClickNearbyDeviceMessageVisible() {
pauseForBetterTestPerformance()
isVisible(TextId(R.string.click_nearby_devices_message))
}

fun clickOnGotItButton() {
pauseForBetterTestPerformance()
clickOn(TextId(R.string.got_it))
}

fun assertDeviceNameMessageVisible() {
pauseForBetterTestPerformance()
isVisible(TextId(R.string.your_device_name_message))
}

fun assertNearbyDeviceListMessageVisible() {
pauseForBetterTestPerformance()
isVisible(TextId(R.string.nearby_devices_list_message))
}

fun assertTransferZimFilesListMessageVisible() {
pauseForBetterTestPerformance()
isVisible(TextId(R.string.transfer_zim_files_list_message))
}

fun assertClickNearbyDeviceMessageNotVisible() {
pauseForBetterTestPerformance()
onView(withText(R.string.click_nearby_devices_message)).check(doesNotExist())
}

private fun pauseForBetterTestPerformance() {
BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS.toLong())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,14 @@ class LocalFileTransferTest {
}
waitForIdle()
}
PreferenceManager.getDefaultSharedPreferences(context).edit {
putBoolean(SharedPreferenceUtil.PREF_SHOW_INTRO, false)
putBoolean(SharedPreferenceUtil.PREF_WIFI_ONLY, false)
putBoolean(SharedPreferenceUtil.PREF_IS_TEST, true)
}
activityScenario = ActivityScenario.launch(KiwixMainActivity::class.java).apply {
moveToState(Lifecycle.State.RESUMED)
}
}

@Test
fun localFileTransfer() {
shouldShowShowCaseFeatureToUser(false)
activityScenario = ActivityScenario.launch(KiwixMainActivity::class.java).apply {
moveToState(Lifecycle.State.RESUMED)
}
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {
activityScenario.onActivity {
it.navigate(R.id.libraryFragment)
Expand All @@ -111,10 +107,69 @@ class LocalFileTransferTest {
}
}

@Test
fun showCaseFeature() {
shouldShowShowCaseFeatureToUser(true, isResetShowCaseId = true)
activityScenario = ActivityScenario.launch(KiwixMainActivity::class.java).apply {
moveToState(Lifecycle.State.RESUMED)
onActivity {
it.navigate(R.id.libraryFragment)
}
}
library {
assertGetZimNearbyDeviceDisplayed()
clickFileTransferIcon {
assertClickNearbyDeviceMessageVisible()
clickOnGotItButton()
assertDeviceNameMessageVisible()
clickOnGotItButton()
assertNearbyDeviceListMessageVisible()
clickOnGotItButton()
assertTransferZimFilesListMessageVisible()
clickOnGotItButton()
pressBack()
assertGetZimNearbyDeviceDisplayed()
}
}
LeakAssertions.assertNoLeaks()
}

@Test
fun testShowCaseFeatureShowOnce() {
shouldShowShowCaseFeatureToUser(true)
activityScenario = ActivityScenario.launch(KiwixMainActivity::class.java).apply {
moveToState(Lifecycle.State.RESUMED)
onActivity {
it.navigate(R.id.libraryFragment)
}
}
library {
// test show case view show once.
clickFileTransferIcon(LocalFileTransferRobot::assertClickNearbyDeviceMessageNotVisible)
}
}

@After
fun setIsTestPreference() {
PreferenceManager.getDefaultSharedPreferences(context).edit {
putBoolean(SharedPreferenceUtil.PREF_IS_TEST, false)
putBoolean(SharedPreferenceUtil.PREF_SHOW_SHOWCASE, true)
}
}

private fun shouldShowShowCaseFeatureToUser(
shouldShowShowCase: Boolean,
isResetShowCaseId: Boolean = false
) {
PreferenceManager.getDefaultSharedPreferences(context).edit {
putBoolean(SharedPreferenceUtil.PREF_SHOW_INTRO, false)
putBoolean(SharedPreferenceUtil.PREF_WIFI_ONLY, false)
putBoolean(SharedPreferenceUtil.PREF_IS_TEST, true)
putBoolean(SharedPreferenceUtil.PREF_SHOW_SHOWCASE, shouldShowShowCase)
}
if (isResetShowCaseId) {
// To clear showCaseID to ensure the showcase view will show.
uk.co.deanwild.materialshowcaseview.PrefsManager.resetAll(context)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class TopLevelDestinationTest : BaseActivityTest() {
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)
}
activityScenario = ActivityScenario.launch(KiwixMainActivity::class.java).apply {
moveToState(Lifecycle.State.RESUMED)
Expand Down Expand Up @@ -100,6 +101,7 @@ class TopLevelDestinationTest : BaseActivityTest() {
fun setIsTestPreference() {
PreferenceManager.getDefaultSharedPreferences(context).edit {
putBoolean(SharedPreferenceUtil.PREF_IS_TEST, false)
putBoolean(SharedPreferenceUtil.PREF_SHOW_SHOWCASE, true)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class LibraryRobot : BaseRobot() {
}

fun assertNoFilesTextDisplayed() {
pauseForBetterTestPerformance()
isVisible(ViewId(R.id.file_management_no_files))
}

Expand All @@ -62,9 +63,8 @@ class LibraryRobot : BaseRobot() {
longClickOnZimFile()
clickOnFileDeleteIcon()
assertDeleteDialogDisplayed()
BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS.toLong())
clickOnDeleteZimFile()
BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS.toLong())
pauseForBetterTestPerformance()
} catch (e: Exception) {
Log.i(
"TEST_DELETE_ZIM",
Expand All @@ -79,6 +79,7 @@ class LibraryRobot : BaseRobot() {
}

private fun assertDeleteDialogDisplayed() {
pauseForBetterTestPerformance()
onView(withText("DELETE"))
.check(ViewAssertions.matches(isDisplayed()))
}
Expand All @@ -88,6 +89,11 @@ class LibraryRobot : BaseRobot() {
}

private fun clickOnDeleteZimFile() {
pauseForBetterTestPerformance()
onView(withText("DELETE")).perform(click())
}

private fun pauseForBetterTestPerformance() {
BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS.toLong())
}
}
Loading

0 comments on commit 8cf8e1b

Please sign in to comment.