[fenix] Extract quick action sheet observer code (https://github.com/mozilla-mobile/fenix/pull/4368)
parent
80b2c4ee51
commit
84349bda63
@ -0,0 +1,68 @@
|
||||
/* 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.quickactionsheet
|
||||
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers.IO
|
||||
import kotlinx.coroutines.Dispatchers.Main
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import mozilla.components.browser.session.Session
|
||||
import org.mozilla.fenix.components.Components
|
||||
import org.mozilla.fenix.components.toolbar.QuickActionSheetAction
|
||||
import java.net.MalformedURLException
|
||||
import java.net.URL
|
||||
|
||||
class QuickActionSheetSessionObserver(
|
||||
private val parentScope: CoroutineScope,
|
||||
private val components: Components,
|
||||
private val dispatch: (QuickActionSheetAction) -> Unit
|
||||
) : Session.Observer {
|
||||
|
||||
private var findBookmarkJob: Job? = null
|
||||
|
||||
override fun onLoadingStateChanged(session: Session, loading: Boolean) {
|
||||
if (!loading) {
|
||||
updateBookmarkState(session)
|
||||
dispatch(QuickActionSheetAction.BounceNeededChange)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onUrlChanged(session: Session, url: String) {
|
||||
updateBookmarkState(session)
|
||||
updateAppLinksState(session)
|
||||
}
|
||||
|
||||
/**
|
||||
* Launches job to update the bookmark button on the quick action sheet.
|
||||
*/
|
||||
fun updateBookmarkState(session: Session) {
|
||||
findBookmarkJob?.cancel()
|
||||
findBookmarkJob = parentScope.launch(Main) {
|
||||
val found = findBookmarkedURL(session)
|
||||
dispatch(QuickActionSheetAction.BookmarkedStateChange(found))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the app link button on the quick action sheet.
|
||||
*/
|
||||
private fun updateAppLinksState(session: Session) {
|
||||
val url = session.url
|
||||
val appLinks = components.useCases.appLinksUseCases.appLinkRedirect
|
||||
dispatch(QuickActionSheetAction.AppLinkStateChange(appLinks(url).hasExternalApp()))
|
||||
}
|
||||
|
||||
private suspend fun findBookmarkedURL(session: Session): Boolean = withContext(IO) {
|
||||
try {
|
||||
val url = URL(session.url).toString()
|
||||
val list = components.core.bookmarksStorage.getBookmarksWithUrl(url)
|
||||
list.isNotEmpty() && list[0].url == url
|
||||
} catch (e: MalformedURLException) {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
/* 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.quickactionsheet
|
||||
|
||||
import io.mockk.Runs
|
||||
import io.mockk.every
|
||||
import io.mockk.just
|
||||
import io.mockk.mockk
|
||||
import io.mockk.spyk
|
||||
import io.mockk.verify
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.ObsoleteCoroutinesApi
|
||||
import mozilla.components.browser.session.Session
|
||||
import mozilla.components.feature.app.links.AppLinkRedirect
|
||||
import mozilla.components.feature.app.links.AppLinksUseCases
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.mozilla.fenix.components.Components
|
||||
import org.mozilla.fenix.components.toolbar.BrowserStore
|
||||
import org.mozilla.fenix.components.toolbar.QuickActionSheetAction
|
||||
|
||||
@ExperimentalCoroutinesApi
|
||||
@ObsoleteCoroutinesApi
|
||||
class QuickActionSheetSessionObserverTest {
|
||||
|
||||
private lateinit var components: Components
|
||||
private lateinit var appLinkRedirect: AppLinksUseCases.GetAppLinkRedirect
|
||||
private lateinit var store: BrowserStore
|
||||
private lateinit var dispatch: (QuickActionSheetAction) -> Unit
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
components = mockk(relaxed = true)
|
||||
appLinkRedirect = mockk(relaxed = true)
|
||||
store = mockk(relaxed = true)
|
||||
dispatch = { store.dispatch(it) }
|
||||
|
||||
every { components.useCases.appLinksUseCases.appLinkRedirect } returns appLinkRedirect
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `onLoadingStateChanged dispatches BounceNeededChange and updates bookmark button`() {
|
||||
val session: Session = mockk()
|
||||
val observer = spyk(QuickActionSheetSessionObserver(mockk(), components, dispatch))
|
||||
every { observer.updateBookmarkState(session) } just Runs
|
||||
|
||||
observer.onLoadingStateChanged(session, true)
|
||||
verify(exactly = 0) { store.dispatch(QuickActionSheetAction.BounceNeededChange) }
|
||||
|
||||
observer.onLoadingStateChanged(session, false)
|
||||
verify { observer.updateBookmarkState(session) }
|
||||
verify { store.dispatch(QuickActionSheetAction.BounceNeededChange) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `onUrlChanged updates bookmark and app link buttons`() {
|
||||
val url = "https://example.com"
|
||||
val session: Session = mockk()
|
||||
every { session.url } returns url
|
||||
|
||||
val observer = spyk(QuickActionSheetSessionObserver(mockk(), components, dispatch))
|
||||
every { observer.updateBookmarkState(session) } just Runs
|
||||
every { appLinkRedirect.invoke(url) } returns AppLinkRedirect(mockk(), "", false)
|
||||
|
||||
observer.onUrlChanged(session, "")
|
||||
verify { observer.updateBookmarkState(session) }
|
||||
verify { appLinkRedirect.invoke("https://example.com") }
|
||||
verify { store.dispatch(QuickActionSheetAction.AppLinkStateChange(true)) }
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue