Skip to content

Commit 6121b9d

Browse files
committed
Fix issue with smaller screen's menu overflow
1 parent 18c2074 commit 6121b9d

File tree

2 files changed

+112
-1
lines changed

2 files changed

+112
-1
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.amaze.filemanager.matcher
2+
3+
import android.content.Context
4+
import android.graphics.Bitmap
5+
import android.graphics.Canvas
6+
import android.graphics.drawable.Drawable
7+
import android.graphics.drawable.StateListDrawable
8+
import android.view.View
9+
import androidx.annotation.DrawableRes
10+
import androidx.appcompat.view.menu.ActionMenuItemView
11+
import androidx.test.espresso.matcher.BoundedMatcher
12+
import org.hamcrest.Description
13+
import org.hamcrest.Matcher
14+
15+
/**
16+
* This matcher allows to select by [ActionMenuItemView] icon
17+
*
18+
* From https://stackoverflow.com/a/70108466/3124150
19+
*/
20+
object ActionMenuIconMatcher {
21+
@JvmStatic
22+
fun withActionIconDrawable(
23+
@DrawableRes resourceId: Int,
24+
): Matcher<View?> {
25+
return object : BoundedMatcher<View?, ActionMenuItemView>(ActionMenuItemView::class.java) {
26+
override fun describeTo(description: Description) {
27+
description.appendText("has image drawable resource $resourceId")
28+
}
29+
30+
override fun matchesSafely(actionMenuItemView: ActionMenuItemView): Boolean {
31+
return sameBitmap(
32+
actionMenuItemView.context,
33+
actionMenuItemView.itemData.icon,
34+
resourceId,
35+
actionMenuItemView,
36+
)
37+
}
38+
}
39+
}
40+
41+
@JvmStatic
42+
private fun sameBitmap(
43+
context: Context,
44+
drawable: Drawable?,
45+
resourceId: Int,
46+
view: View,
47+
): Boolean {
48+
var drawable = drawable
49+
val otherDrawable: Drawable? = context.resources.getDrawable(resourceId)
50+
if (drawable == null || otherDrawable == null) {
51+
return false
52+
}
53+
54+
if (drawable is StateListDrawable) {
55+
val getStateDrawableIndex =
56+
StateListDrawable::class.java.getMethod(
57+
"getStateDrawableIndex",
58+
IntArray::class.java,
59+
)
60+
val getStateDrawable =
61+
StateListDrawable::class.java.getMethod(
62+
"getStateDrawable",
63+
Int::class.javaPrimitiveType,
64+
)
65+
val index = getStateDrawableIndex.invoke(drawable, view.drawableState)
66+
drawable = getStateDrawable.invoke(drawable, index) as Drawable
67+
}
68+
69+
val bitmap = getBitmapFromDrawable(context, drawable)
70+
val otherBitmap = getBitmapFromDrawable(context, otherDrawable)
71+
return bitmap.sameAs(otherBitmap)
72+
}
73+
74+
@JvmStatic
75+
private fun getBitmapFromDrawable(
76+
context: Context?,
77+
drawable: Drawable,
78+
): Bitmap {
79+
val bitmap: Bitmap =
80+
Bitmap.createBitmap(
81+
drawable.intrinsicWidth,
82+
drawable.intrinsicHeight,
83+
Bitmap.Config.ARGB_8888,
84+
)
85+
val canvas = Canvas(bitmap)
86+
drawable.setBounds(0, 0, canvas.width, canvas.height)
87+
drawable.draw(canvas)
88+
return bitmap
89+
}
90+
}

app/src/androidTest/java/com/amaze/filemanager/ui/fragments/BackupPrefsFragmentTest.kt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,19 @@ import androidx.preference.PreferenceManager
3232
import androidx.test.core.app.ActivityScenario
3333
import androidx.test.core.app.ApplicationProvider
3434
import androidx.test.espresso.Espresso.onView
35+
import androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu
36+
import androidx.test.espresso.NoMatchingViewException
3537
import androidx.test.espresso.action.ViewActions
38+
import androidx.test.espresso.assertion.ViewAssertions.matches
39+
import androidx.test.espresso.matcher.ViewMatchers
40+
import androidx.test.espresso.matcher.ViewMatchers.withEffectiveVisibility
3641
import androidx.test.espresso.matcher.ViewMatchers.withId
3742
import androidx.test.espresso.matcher.ViewMatchers.withText
3843
import androidx.test.ext.junit.runners.AndroidJUnit4
44+
import androidx.test.platform.app.InstrumentationRegistry
3945
import androidx.test.rule.GrantPermissionRule
4046
import com.amaze.filemanager.R
47+
import com.amaze.filemanager.matcher.ActionMenuIconMatcher.withActionIconDrawable
4148
import com.amaze.filemanager.test.StoragePermissionHelper
4249
import com.amaze.filemanager.ui.activities.PreferencesActivity
4350
import com.amaze.filemanager.ui.fragments.preferencefragments.BackupPrefsFragment
@@ -117,7 +124,21 @@ class BackupPrefsFragmentTest {
117124

118125
assertTrue(tempFile.exists())
119126

120-
onView(withId(R.id.home)).perform(ViewActions.click())
127+
try {
128+
// HACK to be able to open the overflow on smaller devices, but not on larger devices,
129+
// as the overflow menu would hide the home button on larger devices
130+
131+
onView(withId(R.id.home))
132+
.check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))
133+
// Use icon if visible
134+
onView(withActionIconDrawable(R.drawable.ic_home_white_24dp)).perform(ViewActions.click())
135+
} catch (_: NoMatchingViewException) {
136+
// Open the menu first, to be able to select the home button on smaller devices
137+
openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getInstrumentation().targetContext)
138+
// Use text if visible
139+
onView(withText(R.string.home)).perform(ViewActions.click())
140+
}
141+
121142
onView(withText(R.string.save)).perform(ViewActions.click())
122143

123144
assertTrue(exportFile.exists())

0 commit comments

Comments
 (0)