Close FAB menu on back pressed

This commit is contained in:
Simon Tenbeitel 2017-02-28 17:34:34 +01:00 committed by ligi
parent bc8cc0283b
commit 1f4ac8bd7b
3 changed files with 57 additions and 0 deletions

View file

@ -2,6 +2,7 @@ package org.ligi.passandroid
import android.annotation.TargetApi import android.annotation.TargetApi
import android.support.test.espresso.Espresso.onView import android.support.test.espresso.Espresso.onView
import android.support.test.espresso.Espresso.pressBack
import android.support.test.espresso.action.ViewActions.click import android.support.test.espresso.action.ViewActions.click
import android.support.test.espresso.assertion.ViewAssertions.matches import android.support.test.espresso.assertion.ViewAssertions.matches
import android.support.test.espresso.matcher.ViewMatchers.isDisplayed import android.support.test.espresso.matcher.ViewMatchers.isDisplayed
@ -10,6 +11,8 @@ import org.junit.Rule
import org.junit.Test import org.junit.Test
import org.ligi.passandroid.R.id.pass_recyclerview import org.ligi.passandroid.R.id.pass_recyclerview
import org.ligi.passandroid.functions.checkThatHelpIsThere import org.ligi.passandroid.functions.checkThatHelpIsThere
import org.ligi.passandroid.functions.expand
import org.ligi.passandroid.functions.isCollapsed
import org.ligi.passandroid.ui.PassListActivity import org.ligi.passandroid.ui.PassListActivity
import org.ligi.trulesk.TruleskActivityRule import org.ligi.trulesk.TruleskActivityRule
@ -35,5 +38,15 @@ class ThePassListActivity {
checkThatHelpIsThere() checkThatHelpIsThere()
} }
@Test
fun testCloseFabOnBackPressed() {
onView(withId(R.id.fam)).perform(expand())
pressBack()
onView(withId(R.id.fam))
.check(matches(isDisplayed()))
.check(matches(isCollapsed()))
}
} }

View file

@ -0,0 +1,39 @@
package org.ligi.passandroid.functions
import android.support.test.espresso.UiController
import android.support.test.espresso.ViewAction
import android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom
import android.view.View
import net.i2p.android.ext.floatingactionbutton.FloatingActionsMenu
import org.hamcrest.Description
import org.hamcrest.Matcher
import org.hamcrest.TypeSafeMatcher
fun expand(): ViewAction? = ExpandFabAction()
fun isCollapsed(): Matcher<in View>? = CollapsedCheck() as Matcher<in View>
class ExpandFabAction : ViewAction {
override fun getConstraints(): Matcher<View> = isAssignableFrom(FloatingActionsMenu::class.java)
override fun getDescription() = "expands the floating action menu"
override fun perform(uiController: UiController?, view: View?) {
val fam = view as FloatingActionsMenu
fam.expand()
}
}
class CollapsedCheck : TypeSafeMatcher<FloatingActionsMenu>(FloatingActionsMenu::class.java) {
override fun describeTo(description: Description?) {
description?.appendText("is in collapsed state")
}
override fun matchesSafely(fam: FloatingActionsMenu?): Boolean {
return !fam?.isExpanded!!
}
}

View file

@ -302,4 +302,9 @@ class PassListActivity : PassAndroidActivity() {
} }
override fun onBackPressed() {
if (fam.isExpanded) fam.collapse()
else super.onBackPressed()
}
} }