Introduce timeout for capturing thumbnail in BrowserAnimator

This commit is contained in:
Christian Sadilek 2021-12-01 15:23:01 -05:00 committed by mergify[bot]
parent ae0dba29b1
commit 229c410437
3 changed files with 23 additions and 8 deletions

View File

@ -12,6 +12,7 @@ import androidx.core.graphics.drawable.toDrawable
import androidx.fragment.app.Fragment
import androidx.lifecycle.LifecycleCoroutineScope
import androidx.navigation.NavOptions
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import mozilla.components.concept.engine.EngineView
import org.mozilla.fenix.R
@ -50,16 +51,25 @@ class BrowserAnimator(
* make transitions from the browser fragment to the home fragment
* smoother. So, in addition, we are currently also using it as a
* workaround to prevent flashes during those navigations.
*
* @param timeoutInMs timeout in milliseconds after which the operation
* should be cancelled.
* @param onComplete callback invoked when operation is completed or
* cancelled, see [timeoutInMs]. The boolean passed to the lambda
* indicates whether or not the operation was successful i.e., true,
* if the bitmap was successfully set as a background, otherwise false.
*/
fun captureEngineViewAndDrawStatically(onComplete: () -> Unit) {
fun captureEngineViewAndDrawStatically(timeoutInMs: Long = 250L, onComplete: (Boolean) -> Unit) {
unwrappedEngineView?.asView()?.context.let {
viewLifecycleScope.get()?.launch {
var cancelled = false
var completed = false
// isAdded check is necessary because of a bug in viewLifecycleOwner. See AC#3828
if (!fragment.isAdded()) {
return@launch
}
unwrappedEngineView?.captureThumbnail { bitmap ->
if (!fragment.isAdded()) {
if (!fragment.isAdded() || cancelled) {
return@captureThumbnail
}
@ -70,8 +80,13 @@ class BrowserAnimator(
}
unwrappedEngineView?.asView()?.visibility = View.GONE
onComplete()
completed = true
onComplete(bitmap != null)
}
delay(timeoutInMs)
if (!completed) {
cancelled = true
onComplete(false)
}
}
}

View File

@ -103,9 +103,9 @@ class DefaultBrowserToolbarControllerTest {
}
every {
browserAnimator.captureEngineViewAndDrawStatically(any())
browserAnimator.captureEngineViewAndDrawStatically(any(), any())
} answers {
firstArg<() -> Unit>()()
secondArg<(Boolean) -> Unit>()(true)
}
tabCounterClicked = false

View File

@ -118,8 +118,8 @@ class DefaultBrowserToolbarMenuControllerTest {
}
every { settings.topSitesMaxLimit } returns 16
val onComplete = slot<() -> Unit>()
every { browserAnimator.captureEngineViewAndDrawStatically(capture(onComplete)) } answers { onComplete.captured.invoke() }
val onComplete = slot<(Boolean) -> Unit>()
every { browserAnimator.captureEngineViewAndDrawStatically(any(), capture(onComplete)) } answers { onComplete.captured.invoke(true) }
selectedTab = createTab("https://www.mozilla.org", id = "1")
browserStore = BrowserStore(