11package com.amaze.filemanager.ui.fragments
22
33import android.content.pm.ActivityInfo
4+ import android.graphics.Rect
45import android.os.Build.VERSION.SDK_INT
56import android.os.Build.VERSION_CODES.TIRAMISU
7+ import android.view.View
68import androidx.test.espresso.Espresso.onView
9+ import androidx.test.espresso.UiController
10+ import androidx.test.espresso.ViewAction
711import androidx.test.espresso.action.ViewActions.swipeLeft
812import androidx.test.espresso.action.ViewActions.swipeRight
13+ import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
914import androidx.test.espresso.matcher.ViewMatchers.withId
1015import androidx.test.ext.junit.runners.AndroidJUnit4
1116import androidx.test.platform.app.InstrumentationRegistry
1217import androidx.test.rule.ActivityTestRule
1318import androidx.test.rule.GrantPermissionRule
19+ import androidx.test.uiautomator.UiDevice
1420import com.amaze.filemanager.R
1521import com.amaze.filemanager.test.StoragePermissionHelper
1622import com.amaze.filemanager.ui.activities.MainActivity
23+ import org.hamcrest.Matcher
1724import org.junit.Before
1825import org.junit.Rule
1926import org.junit.Test
@@ -81,7 +88,7 @@ class TabFragmentTest {
8188 @Test
8289 fun testFragmentStateSavingDuringConfigChange () {
8390 // First perform the swipe action
84- onView (withId(R .id.pager)).perform(swipeLeft( ))
91+ swipeLeftCompat (withId(R .id.pager))
8592
8693 // Force a configuration change by rotating the screen
8794 activityRule.activity.requestedOrientation = ActivityInfo .SCREEN_ORIENTATION_LANDSCAPE
@@ -96,9 +103,9 @@ class TabFragmentTest {
96103 fun testRapidTabSwitchingAndStateSaving () {
97104 // Perform rapid tab switches
98105 repeat(10 ) {
99- onView (withId(R .id.pager)).perform(swipeLeft( ))
106+ swipeLeftCompat (withId(R .id.pager))
100107 Thread .sleep(100 ) // Small delay to ensure swipe completes
101- onView (withId(R .id.pager)).perform(swipeRight( ))
108+ swipeRightCompat (withId(R .id.pager))
102109 Thread .sleep(100 ) // Small delay to ensure swipe completes
103110 }
104111
@@ -112,7 +119,7 @@ class TabFragmentTest {
112119 @Test
113120 fun testFragmentDetachmentAndStateSaving () {
114121 // First switch to a different tab
115- onView (withId(R .id.pager)).perform(swipeLeft( ))
122+ swipeLeftCompat (withId(R .id.pager))
116123
117124 // Get the TabFragment
118125 InstrumentationRegistry .getInstrumentation().runOnMainSync {
@@ -131,4 +138,67 @@ class TabFragmentTest {
131138 // Force state save through configuration change
132139 activityRule.activity.requestedOrientation = ActivityInfo .SCREEN_ORIENTATION_LANDSCAPE
133140 }
141+
142+ /* *
143+ * Hack that works like swipeLeft or swipeRight on smaller screens
144+ */
145+ private fun swipeHack (
146+ interpolatorX : Float ,
147+ interpolatorY : Float ,
148+ viewMatcher : Matcher <View >,
149+ ) {
150+ /* HACK
151+ If the View items are contained inside a ScrollView, and the screen's height is not
152+ enough to show 90% of the ScrollView, an error is thrown. This is a problem for smaller
153+ screens, to fix this we simply run the swipe "manually".
154+ See https://stackoverflow.com/a/74361805/3124150
155+ */
156+
157+ onView(viewMatcher).perform(
158+ object : ViewAction {
159+ override fun getConstraints (): Matcher <View > = isDisplayed()
160+
161+ override fun getDescription (): String = " Swipe without checking rect availability"
162+
163+ override fun perform (
164+ uiController : UiController ,
165+ view : View ,
166+ ) {
167+ val device = UiDevice .getInstance(InstrumentationRegistry .getInstrumentation())
168+ val visibleRect = Rect ()
169+ view.getGlobalVisibleRect(visibleRect)
170+
171+ val endX = visibleRect.left + (visibleRect.right * interpolatorX).toInt()
172+ val endY = visibleRect.top + (visibleRect.bottom * interpolatorY).toInt()
173+
174+ // Swipe up from the center, at 5ms per step
175+ device.swipe(
176+ visibleRect.centerX(),
177+ visibleRect.centerY(),
178+ endX,
179+ endY,
180+ 10 ,
181+ )
182+ }
183+ },
184+ )
185+ }
186+
187+ private fun swipeLeftCompat (viewMatcher : Matcher <View >) {
188+ val device = UiDevice .getInstance(InstrumentationRegistry .getInstrumentation())
189+ if (device.displayHeight <= 1280 ) {
190+ swipeHack(0.95f , 0.5f , viewMatcher)
191+ } else {
192+ onView(viewMatcher).perform(swipeLeft())
193+ }
194+ }
195+
196+ private fun swipeRightCompat (viewMatcher : Matcher <View >) {
197+ val device = UiDevice .getInstance(InstrumentationRegistry .getInstrumentation())
198+ if (device.displayHeight <= 1280 ) {
199+ swipeHack(0.05f , 0.5f , viewMatcher)
200+ } else {
201+ onView(viewMatcher).perform(swipeRight())
202+ }
203+ }
134204}
0 commit comments