This repository has been archived by the owner on Feb 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #18816: Disable TabsTray FAB on accessibility enabled
- Loading branch information
1 parent
3b3e4c0
commit 43d8d72
Showing
6 changed files
with
454 additions
and
9 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
app/src/main/java/org/mozilla/fenix/tabstray/AccessibleNewTabButtonBinding.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package org.mozilla.fenix.tabstray | ||
|
||
import android.view.View | ||
import android.widget.ImageButton | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.cancel | ||
import kotlinx.coroutines.flow.collect | ||
import kotlinx.coroutines.flow.map | ||
import mozilla.components.lib.state.ext.flowScoped | ||
import mozilla.components.support.base.feature.LifecycleAwareFeature | ||
import mozilla.components.support.ktx.kotlinx.coroutines.flow.ifAnyChanged | ||
import org.mozilla.fenix.R | ||
import org.mozilla.fenix.tabstray.browser.BrowserTrayInteractor | ||
import org.mozilla.fenix.tabstray.syncedtabs.SyncedTabsInteractor | ||
import org.mozilla.fenix.utils.Settings | ||
|
||
/** | ||
* Do not show accessible new tab button when accessibility service is disabled | ||
* | ||
* This binding is coupled with [FloatingActionButtonBinding]. | ||
* When [FloatingActionButtonBinding] is visible this should not be visible | ||
*/ | ||
class AccessibleNewTabButtonBinding( | ||
private val store: TabsTrayStore, | ||
private val settings: Settings, | ||
private val newTabButton: ImageButton, | ||
private val browserTrayInteractor: BrowserTrayInteractor, | ||
private val syncedTabsInteractor: SyncedTabsInteractor | ||
) : LifecycleAwareFeature { | ||
|
||
private var scope: CoroutineScope? = null | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
override fun start() { | ||
if (!settings.accessibilityServicesEnabled) { | ||
newTabButton.visibility = View.GONE | ||
return | ||
} | ||
|
||
scope = store.flowScoped { flow -> | ||
flow.map { it } | ||
.ifAnyChanged { state -> | ||
arrayOf( | ||
state.selectedPage, | ||
state.syncing | ||
) | ||
} | ||
.collect { state -> | ||
setAccessibleNewTabButton(state.selectedPage, state.syncing) | ||
} | ||
} | ||
} | ||
|
||
override fun stop() { | ||
scope?.cancel() | ||
} | ||
|
||
private fun setAccessibleNewTabButton(selectedPage: Page, syncing: Boolean) { | ||
when (selectedPage) { | ||
Page.NormalTabs -> { | ||
newTabButton.apply { | ||
visibility = View.VISIBLE | ||
setImageResource(R.drawable.ic_new) | ||
setOnClickListener { | ||
browserTrayInteractor.onFabClicked(false) | ||
} | ||
} | ||
} | ||
Page.PrivateTabs -> { | ||
newTabButton.apply { | ||
visibility = View.VISIBLE | ||
setImageResource(R.drawable.ic_new) | ||
setOnClickListener { | ||
browserTrayInteractor.onFabClicked(true) | ||
} | ||
} | ||
} | ||
Page.SyncedTabs -> { | ||
newTabButton.apply { | ||
visibility = | ||
when (syncing) { | ||
true -> View.GONE | ||
false -> View.VISIBLE | ||
} | ||
|
||
setImageResource(R.drawable.ic_fab_sync) | ||
setOnClickListener { | ||
// Notify the store observers (one of which is the SyncedTabsFeature), that | ||
// a sync was requested. | ||
if (!syncing) { | ||
store.dispatch(TabsTrayAction.SyncNow) | ||
syncedTabsInteractor.onRefresh() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
app/src/test/java/org/mozilla/fenix/tabstray/AccessibleNewTabButtonBindingTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package org.mozilla.fenix.tabstray | ||
|
||
import android.view.View | ||
import android.widget.ImageButton | ||
import androidx.appcompat.content.res.AppCompatResources | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import io.mockk.mockkStatic | ||
import io.mockk.verify | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.test.TestCoroutineDispatcher | ||
import mozilla.components.support.test.libstate.ext.waitUntilIdle | ||
import mozilla.components.support.test.rule.MainCoroutineRule | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.mozilla.fenix.R | ||
import org.mozilla.fenix.tabstray.browser.BrowserTrayInteractor | ||
import org.mozilla.fenix.tabstray.syncedtabs.SyncedTabsInteractor | ||
import org.mozilla.fenix.utils.Settings | ||
|
||
class AccessibleNewTabButtonBindingTest { | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
@get:Rule | ||
val coroutinesTestRule = MainCoroutineRule(TestCoroutineDispatcher()) | ||
|
||
private val settings: Settings = mockk(relaxed = true) | ||
private val newTabButton: ImageButton = mockk(relaxed = true) | ||
private val browserTrayInteractor: BrowserTrayInteractor = mockk(relaxed = true) | ||
private val syncedTabsInteractor: SyncedTabsInteractor = mockk(relaxed = true) | ||
|
||
@Before | ||
fun setup() { | ||
mockkStatic(AppCompatResources::class) | ||
every { AppCompatResources.getDrawable(any(), any()) } returns mockk(relaxed = true) | ||
} | ||
|
||
@Test | ||
fun `WHEN tab selected page is normal tab THEN new tab button is visible`() { | ||
val tabsTrayStore = TabsTrayStore(TabsTrayState(selectedPage = Page.NormalTabs)) | ||
val newTabButtonBinding = AccessibleNewTabButtonBinding( | ||
tabsTrayStore, settings, newTabButton, browserTrayInteractor, syncedTabsInteractor | ||
) | ||
every { settings.accessibilityServicesEnabled } returns true | ||
|
||
newTabButtonBinding.start() | ||
|
||
verify(exactly = 1) { newTabButton.visibility = View.VISIBLE } | ||
} | ||
|
||
@Test | ||
fun `WHEN tab selected page is private tab THEN new tab button is visible`() { | ||
val tabsTrayStore = TabsTrayStore(TabsTrayState(selectedPage = Page.PrivateTabs)) | ||
val newTabButtonBinding = AccessibleNewTabButtonBinding( | ||
tabsTrayStore, settings, newTabButton, browserTrayInteractor, syncedTabsInteractor | ||
) | ||
every { settings.accessibilityServicesEnabled } returns true | ||
|
||
newTabButtonBinding.start() | ||
|
||
verify(exactly = 1) { newTabButton.visibility = View.VISIBLE } | ||
} | ||
|
||
@Test | ||
fun `WHEN tab selected page is sync tab THEN new tab button is visible`() { | ||
val tabsTrayStore = TabsTrayStore(TabsTrayState(selectedPage = Page.SyncedTabs)) | ||
val newTabButtonBinding = AccessibleNewTabButtonBinding( | ||
tabsTrayStore, settings, newTabButton, browserTrayInteractor, syncedTabsInteractor | ||
) | ||
every { settings.accessibilityServicesEnabled } returns true | ||
|
||
newTabButtonBinding.start() | ||
|
||
verify(exactly = 1) { newTabButton.visibility = View.VISIBLE } | ||
} | ||
|
||
@Test | ||
fun `WHEN accessibility is disabled THEN new tab button is not visible`() { | ||
var tabsTrayStore = TabsTrayStore(TabsTrayState(selectedPage = Page.NormalTabs)) | ||
var newTabButtonBinding = AccessibleNewTabButtonBinding( | ||
tabsTrayStore, settings, newTabButton, browserTrayInteractor, syncedTabsInteractor | ||
) | ||
every { settings.accessibilityServicesEnabled } returns false | ||
|
||
newTabButtonBinding.start() | ||
|
||
verify(exactly = 1) { newTabButton.visibility = View.GONE } | ||
|
||
tabsTrayStore = TabsTrayStore(TabsTrayState(selectedPage = Page.PrivateTabs)) | ||
newTabButtonBinding = AccessibleNewTabButtonBinding( | ||
tabsTrayStore, settings, newTabButton, browserTrayInteractor, syncedTabsInteractor | ||
) | ||
|
||
newTabButtonBinding.start() | ||
|
||
verify(exactly = 2) { newTabButton.visibility = View.GONE } | ||
|
||
tabsTrayStore = TabsTrayStore(TabsTrayState(selectedPage = Page.SyncedTabs)) | ||
newTabButtonBinding = AccessibleNewTabButtonBinding( | ||
tabsTrayStore, settings, newTabButton, browserTrayInteractor, syncedTabsInteractor | ||
) | ||
|
||
newTabButtonBinding.start() | ||
|
||
verify(exactly = 3) { newTabButton.visibility = View.GONE } | ||
} | ||
|
||
@Test | ||
fun `WHEN selected page is updated THEN button is updated`() { | ||
val tabsTrayStore = TabsTrayStore(TabsTrayState(selectedPage = Page.NormalTabs)) | ||
val newTabButtonBinding = AccessibleNewTabButtonBinding( | ||
tabsTrayStore, settings, newTabButton, browserTrayInteractor, syncedTabsInteractor | ||
) | ||
every { settings.accessibilityServicesEnabled } returns true | ||
|
||
newTabButtonBinding.start() | ||
|
||
verify(exactly = 1) { newTabButton.setImageResource(R.drawable.ic_new) } | ||
|
||
tabsTrayStore.dispatch(TabsTrayAction.PageSelected(Page.positionToPage(Page.PrivateTabs.ordinal))) | ||
tabsTrayStore.waitUntilIdle() | ||
|
||
verify(exactly = 2) { newTabButton.setImageResource(R.drawable.ic_new) } | ||
|
||
tabsTrayStore.dispatch(TabsTrayAction.PageSelected(Page.positionToPage(Page.SyncedTabs.ordinal))) | ||
tabsTrayStore.waitUntilIdle() | ||
|
||
verify(exactly = 1) { newTabButton.setImageResource(R.drawable.ic_fab_sync) } | ||
|
||
tabsTrayStore.dispatch(TabsTrayAction.SyncNow) | ||
tabsTrayStore.waitUntilIdle() | ||
|
||
verify(exactly = 1) { newTabButton.visibility = View.GONE } | ||
} | ||
} |
Oops, something went wrong.