Skip to content

Commit a7b5a77

Browse files
MohitMaliDeveloperkelson42
authored andcommitted
Fixed test failure on API level 33.
* In the `ZimHostFragment`, there were occasional test failures due to specific conditions. When reattempting the test, it failed to detect the 'WiFi connection detected' dialog because the server was already running. To resolve this issue, we have improved our test case. Now, we first check if the server is already running. If it is, we close the server before running the test case. * In previous test failures within the `ZimHostFragment`, there were instances where the zim file was unselected, causing our test case to fail to locate the required views. To mitigate this, we now check whether the zim file is selected. If it's not selected, we first select the zim file before running the test case. * n the `LocalLibraryFragment` test, there were cases where it was unable to locate the 'file_management_no_files' view due to variations in the order of test cases. This occurred because a zim file was sometimes present in the `LocalLibrary`. To address this, we now check for the presence of any zim files in the `LocalLibrary` and delete them before running our test case.
1 parent 96b1b19 commit a7b5a77

File tree

4 files changed

+123
-7
lines changed

4 files changed

+123
-7
lines changed

app/src/androidTest/java/org/kiwix/kiwixmobile/nav/destination/library/LibraryRobot.kt

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,25 @@
1919
package org.kiwix.kiwixmobile.nav.destination.library
2020

2121
import android.util.Log
22+
import androidx.recyclerview.widget.RecyclerView.ViewHolder
2223
import androidx.test.espresso.Espresso.onView
2324
import androidx.test.espresso.action.ViewActions.click
25+
import androidx.test.espresso.action.ViewActions.longClick
2426
import androidx.test.espresso.assertion.ViewAssertions
25-
import androidx.test.espresso.matcher.ViewMatchers.withText
27+
import androidx.test.espresso.contrib.RecyclerViewActions.actionOnItemAtPosition
28+
import androidx.test.espresso.contrib.RecyclerViewActions.scrollToPosition
29+
import androidx.test.espresso.matcher.ViewMatchers.withId
2630
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
31+
import androidx.test.espresso.matcher.ViewMatchers.withText
2732
import applyWithViewHierarchyPrinting
2833
import com.adevinta.android.barista.interaction.BaristaSleepInteractions
2934
import org.kiwix.kiwixmobile.BaseRobot
30-
import org.kiwix.kiwixmobile.Findable.Text
3135
import org.kiwix.kiwixmobile.Findable.ViewId
3236
import org.kiwix.kiwixmobile.R
3337
import org.kiwix.kiwixmobile.localFileTransfer.LocalFileTransferRobot
3438
import org.kiwix.kiwixmobile.localFileTransfer.localFileTransfer
3539
import org.kiwix.kiwixmobile.testutils.TestUtils
40+
import org.kiwix.kiwixmobile.utils.RecyclerViewItemCount
3641

3742
fun library(func: LibraryRobot.() -> Unit) = LibraryRobot().applyWithViewHierarchyPrinting(func)
3843

@@ -60,7 +65,17 @@ class LibraryRobot : BaseRobot() {
6065

6166
fun deleteZimIfExists() {
6267
try {
63-
longClickOnZimFile()
68+
val recyclerViewId: Int = R.id.zimfilelist
69+
val recyclerViewItemsCount = RecyclerViewItemCount(recyclerViewId).checkRecyclerViewCount()
70+
// Scroll to the end of the RecyclerView to ensure all items are visible
71+
onView(withId(recyclerViewId))
72+
.perform(scrollToPosition<ViewHolder>(recyclerViewItemsCount - 1))
73+
74+
for (position in 0 until recyclerViewItemsCount) {
75+
// Long-click the item to select it
76+
onView(withId(recyclerViewId))
77+
.perform(actionOnItemAtPosition<ViewHolder>(position, longClick()))
78+
}
6479
clickOnFileDeleteIcon()
6580
assertDeleteDialogDisplayed()
6681
clickOnDeleteZimFile()
@@ -75,6 +90,7 @@ class LibraryRobot : BaseRobot() {
7590
}
7691

7792
private fun clickOnFileDeleteIcon() {
93+
pauseForBetterTestPerformance()
7894
clickOn(ViewId(R.id.zim_file_delete_item))
7995
}
8096

@@ -84,10 +100,6 @@ class LibraryRobot : BaseRobot() {
84100
.check(ViewAssertions.matches(isDisplayed()))
85101
}
86102

87-
private fun longClickOnZimFile() {
88-
longClickOn(Text(zimFileTitle))
89-
}
90-
91103
private fun clickOnDeleteZimFile() {
92104
pauseForBetterTestPerformance()
93105
onView(withText("DELETE")).perform(click())
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Kiwix Android
3+
* Copyright (c) 2023 Kiwix <android.kiwix.org>
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*
17+
*/
18+
19+
package org.kiwix.kiwixmobile.utils
20+
21+
import android.view.View
22+
import androidx.recyclerview.widget.RecyclerView
23+
import androidx.test.espresso.Espresso.onView
24+
import androidx.test.espresso.NoMatchingViewException
25+
import androidx.test.espresso.matcher.ViewMatchers.withId
26+
27+
class RecyclerViewItemCount(private val recyclerViewId: Int) {
28+
fun checkRecyclerViewCount(): Int {
29+
var recyclerViewItemCount = 0
30+
onView(withId(recyclerViewId))
31+
.check { view: View, noViewFoundException: NoMatchingViewException? ->
32+
if (noViewFoundException != null) {
33+
throw noViewFoundException
34+
}
35+
val recyclerView = view as RecyclerView
36+
// Get the item count from the RecyclerView
37+
recyclerViewItemCount = recyclerView.adapter?.itemCount ?: 0
38+
}
39+
return recyclerViewItemCount
40+
}
41+
}

app/src/androidTest/java/org/kiwix/kiwixmobile/webserver/ZimHostFragmentTest.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ class ZimHostFragmentTest {
104104
refreshLibraryList()
105105
assertZimFilesLoaded()
106106
openZimHostFragment()
107+
108+
// Check if server is already started
109+
stopServerIfAlreadyStarted()
110+
111+
// Check if both zim file are selected or not to properly run our test case
112+
selectZimFileIfNotAlreadySelected()
113+
107114
clickOnTestZim()
108115

109116
// Start the server with one ZIM file

app/src/androidTest/java/org/kiwix/kiwixmobile/webserver/ZimHostRobot.kt

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,25 @@
1818

1919
package org.kiwix.kiwixmobile.webserver
2020

21+
import android.util.Log
22+
import androidx.test.espresso.Espresso.onView
23+
import androidx.test.espresso.action.ViewActions.click
24+
import androidx.test.espresso.assertion.ViewAssertions.matches
25+
import androidx.test.espresso.matcher.ViewMatchers
2126
import androidx.test.espresso.matcher.ViewMatchers.assertThat
2227
import applyWithViewHierarchyPrinting
2328
import com.adevinta.android.barista.interaction.BaristaSleepInteractions
2429
import com.adevinta.android.barista.interaction.BaristaSwipeRefreshInteractions.refresh
30+
import junit.framework.AssertionFailedError
2531
import org.hamcrest.CoreMatchers
2632
import org.kiwix.kiwixmobile.BaseRobot
2733
import org.kiwix.kiwixmobile.Findable.StringId.TextId
2834
import org.kiwix.kiwixmobile.Findable.Text
2935
import org.kiwix.kiwixmobile.Findable.ViewId
3036
import org.kiwix.kiwixmobile.R
3137
import org.kiwix.kiwixmobile.testutils.TestUtils
38+
import org.kiwix.kiwixmobile.utils.RecyclerViewItemCount
39+
import org.kiwix.kiwixmobile.utils.RecyclerViewMatcher
3240
import org.kiwix.kiwixmobile.utils.RecyclerViewSelectedCheckBoxCountAssertion
3341
import org.kiwix.kiwixmobile.utils.StandardActions.openDrawer
3442

@@ -71,6 +79,54 @@ class ZimHostRobot : BaseRobot() {
7179
isVisible(Text("STOP SERVER"))
7280
}
7381

82+
fun stopServerIfAlreadyStarted() {
83+
try {
84+
assertServerStarted()
85+
stopServer()
86+
} catch (exception: Exception) {
87+
Log.i(
88+
"ZIM_HOST_FRAGMENT",
89+
"Failed to stop the server, Probably because server is not running"
90+
)
91+
}
92+
}
93+
94+
fun selectZimFileIfNotAlreadySelected() {
95+
try {
96+
// check both files are selected.
97+
assertItemHostedOnServer(2)
98+
} catch (assertionFailedError: AssertionFailedError) {
99+
try {
100+
val recyclerViewItemsCount =
101+
RecyclerViewItemCount(R.id.recyclerViewZimHost).checkRecyclerViewCount()
102+
(0 until recyclerViewItemsCount)
103+
.asSequence()
104+
.filter { it != 0 }
105+
.forEach(::selectZimFile)
106+
} catch (assertionFailedError: AssertionFailedError) {
107+
Log.i("ZIM_HOST_FRAGMENT", "Failed to select the zim file, probably it is already selected")
108+
}
109+
}
110+
}
111+
112+
private fun selectZimFile(position: Int) {
113+
try {
114+
onView(
115+
RecyclerViewMatcher(R.id.recyclerViewZimHost).atPositionOnView(
116+
position,
117+
R.id.itemBookCheckbox
118+
)
119+
).check(matches(ViewMatchers.isChecked()))
120+
} catch (assertionError: AssertionFailedError) {
121+
onView(
122+
RecyclerViewMatcher(R.id.recyclerViewZimHost).atPositionOnView(
123+
position,
124+
R.id.itemBookCheckbox
125+
)
126+
).perform(click())
127+
}
128+
}
129+
74130
fun assertItemHostedOnServer(itemCount: Int) {
75131
val checkedCheckboxCount =
76132
RecyclerViewSelectedCheckBoxCountAssertion(

0 commit comments

Comments
 (0)