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.util.Log
8+ import android.view.View
9+ import android.view.View.VISIBLE
610import androidx.test.espresso.Espresso.onView
11+ import androidx.test.espresso.UiController
12+ import androidx.test.espresso.ViewAction
713import androidx.test.espresso.action.ViewActions.swipeLeft
814import androidx.test.espresso.action.ViewActions.swipeRight
15+ import androidx.test.espresso.assertion.ViewAssertions.matches
16+ import androidx.test.espresso.matcher.ViewMatchers
17+ import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
18+ import androidx.test.espresso.matcher.ViewMatchers.withEffectiveVisibility
919import androidx.test.espresso.matcher.ViewMatchers.withId
1020import androidx.test.ext.junit.runners.AndroidJUnit4
1121import androidx.test.platform.app.InstrumentationRegistry
1222import androidx.test.rule.ActivityTestRule
1323import androidx.test.rule.GrantPermissionRule
24+ import androidx.test.uiautomator.UiDevice
1425import com.amaze.filemanager.R
1526import com.amaze.filemanager.test.StoragePermissionHelper
1627import com.amaze.filemanager.ui.activities.MainActivity
28+ import org.hamcrest.Matcher
29+ import org.junit.Assert
1730import org.junit.Before
1831import org.junit.Rule
1932import org.junit.Test
2033import org.junit.runner.RunWith
34+ import java.util.Optional
2135
2236/* *
2337 * Tests for [TabFragment] functionality, mainly for
@@ -81,7 +95,7 @@ class TabFragmentTest {
8195 @Test
8296 fun testFragmentStateSavingDuringConfigChange () {
8397 // First perform the swipe action
84- onView (withId(R .id.pager)).perform(swipeLeft( ))
98+ swipeLeftCompat (withId(R .id.pager))
8599
86100 // Force a configuration change by rotating the screen
87101 activityRule.activity.requestedOrientation = ActivityInfo .SCREEN_ORIENTATION_LANDSCAPE
@@ -96,9 +110,9 @@ class TabFragmentTest {
96110 fun testRapidTabSwitchingAndStateSaving () {
97111 // Perform rapid tab switches
98112 repeat(10 ) {
99- onView (withId(R .id.pager)).perform(swipeLeft( ))
113+ swipeLeftCompat (withId(R .id.pager))
100114 Thread .sleep(100 ) // Small delay to ensure swipe completes
101- onView (withId(R .id.pager)).perform(swipeRight( ))
115+ swipeRightCompat (withId(R .id.pager))
102116 Thread .sleep(100 ) // Small delay to ensure swipe completes
103117 }
104118
@@ -112,7 +126,7 @@ class TabFragmentTest {
112126 @Test
113127 fun testFragmentDetachmentAndStateSaving () {
114128 // First switch to a different tab
115- onView (withId(R .id.pager)).perform(swipeLeft( ))
129+ swipeLeftCompat (withId(R .id.pager))
116130
117131 // Get the TabFragment
118132 InstrumentationRegistry .getInstrumentation().runOnMainSync {
@@ -131,4 +145,52 @@ class TabFragmentTest {
131145 // Force state save through configuration change
132146 activityRule.activity.requestedOrientation = ActivityInfo .SCREEN_ORIENTATION_LANDSCAPE
133147 }
148+
149+ private fun swipeHack (interpolatorX : Float , interpolatorY : Float ,
150+ viewMatcher : Matcher <View >) {
151+ // HACK https://stackoverflow.com/a/74361805/3124150
152+
153+ onView(viewMatcher).perform(object : ViewAction {
154+ override fun getConstraints (): Matcher <View > = isDisplayed()
155+
156+ override fun getDescription (): String = " Swipe without checking rect availability"
157+
158+ override fun perform (
159+ uiController : UiController ,
160+ view : View
161+ ) {
162+ val device = UiDevice .getInstance(InstrumentationRegistry .getInstrumentation())
163+ val visibleRect = Rect ()
164+ view.getGlobalVisibleRect(visibleRect)
165+
166+ val endX = visibleRect.left + (visibleRect.right * interpolatorX).toInt()
167+ val endY = visibleRect.top + (visibleRect.bottom * interpolatorY).toInt()
168+
169+ // Swipe up from the center, at 5ms per step
170+ device.swipe(
171+ visibleRect.centerX(), visibleRect.centerY(),
172+ endX, endY, 10
173+ )
174+ }
175+
176+ })
177+ }
178+
179+ private fun swipeLeftCompat (viewMatcher : Matcher <View >) {
180+ val device = UiDevice .getInstance(InstrumentationRegistry .getInstrumentation())
181+ if (device.displayHeight <= 1280 ) {
182+ swipeHack(0.95f , 0.5f , viewMatcher)
183+ } else {
184+ onView(viewMatcher).perform(swipeLeft())
185+ }
186+ }
187+
188+ private fun swipeRightCompat (viewMatcher : Matcher <View >) {
189+ val device = UiDevice .getInstance(InstrumentationRegistry .getInstrumentation())
190+ if (device.displayHeight <= 1280 ) {
191+ swipeHack(0.05f , 0.5f , viewMatcher)
192+ } else {
193+ onView(viewMatcher).perform(swipeRight())
194+ }
195+ }
134196}
0 commit comments