Commit 041abb84 authored by Arturo Mejia's avatar Arturo Mejia
Browse files

Moving captureThumbnail from EngineSession to EngineView.

parent 4b63e100
......@@ -5,7 +5,6 @@
package mozilla.components.browser.engine.gecko
import android.annotation.SuppressLint
import android.graphics.Bitmap
import kotlin.coroutines.CoroutineContext
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineScope
......@@ -559,12 +558,6 @@ class GeckoEngineSession(
}
}
override fun captureThumbnail(): Bitmap? {
// TODO Waiting for the Gecko team to create an API for this
// See https://bugzilla.mozilla.org/show_bug.cgi?id=1462018
return null
}
private fun createGeckoSession() {
this.geckoSession = geckoSessionProvider()
......
......@@ -5,6 +5,7 @@
package mozilla.components.browser.engine.gecko
import android.content.Context
import android.graphics.Bitmap
import android.support.v4.view.ViewCompat
import android.util.AttributeSet
import android.widget.FrameLayout
......@@ -58,4 +59,6 @@ class GeckoEngineView @JvmOverloads constructor(
}
override fun canScrollVerticallyDown() = true // waiting for this issue https://bugzilla.mozilla.org/show_bug.cgi?id=1507569
override fun captureThumbnail(onFinish: (Bitmap?) -> Unit) = Unit
}
......@@ -5,7 +5,6 @@
package mozilla.components.browser.engine.gecko
import android.annotation.SuppressLint
import android.graphics.Bitmap
import kotlin.coroutines.CoroutineContext
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineScope
......@@ -559,12 +558,6 @@ class GeckoEngineSession(
}
}
override fun captureThumbnail(): Bitmap? {
// TODO Waiting for the Gecko team to create an API for this
// See https://bugzilla.mozilla.org/show_bug.cgi?id=1462018
return null
}
private fun createGeckoSession() {
this.geckoSession = geckoSessionProvider()
......
......@@ -5,12 +5,15 @@
package mozilla.components.browser.engine.gecko
import android.content.Context
import android.graphics.Bitmap
import android.support.v4.view.ViewCompat
import android.util.AttributeSet
import android.widget.FrameLayout
import mozilla.components.concept.engine.EngineSession
import mozilla.components.concept.engine.EngineView
import org.mozilla.geckoview.GeckoResult
/**
* Gecko-based EngineView implementation.
*/
......@@ -58,4 +61,15 @@ class GeckoEngineView @JvmOverloads constructor(
}
override fun canScrollVerticallyDown() = true // waiting for this issue https://bugzilla.mozilla.org/show_bug.cgi?id=1507569
override fun captureThumbnail(onFinish: (Bitmap?) -> Unit) {
val geckoResult = currentGeckoView.capturePixels()
geckoResult.then({ bitmap ->
onFinish(bitmap)
GeckoResult<Void>()
}, {
onFinish(null)
GeckoResult<Void>()
})
}
}
......@@ -4,22 +4,31 @@
package mozilla.components.browser.engine.gecko
import android.content.Context
import android.graphics.Bitmap
import androidx.test.core.app.ApplicationProvider
import mozilla.components.support.test.mock
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.`when`
import org.mockito.Mockito.mock
import org.mockito.Mockito.times
import org.mockito.Mockito.verify
import org.mozilla.geckoview.GeckoResult
import org.mozilla.geckoview.GeckoSession
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
@RunWith(RobolectricTestRunner::class)
class GeckoEngineViewTest {
private val context: Context
get() = ApplicationProvider.getApplicationContext()
@Test
fun render() {
val engineView = GeckoEngineView(RuntimeEnvironment.application)
val engineView = GeckoEngineView(context)
val engineSession = mock(GeckoEngineSession::class.java)
val geckoSession = mock(GeckoSession::class.java)
val geckoView = mock(NestedGeckoView::class.java)
......@@ -34,4 +43,32 @@ class GeckoEngineViewTest {
engineView.render(engineSession)
verify(geckoView, times(1)).setSession(geckoSession)
}
@Test
fun captureThumbnail() {
val engineView = GeckoEngineView(context)
val mockGeckoView = mock(NestedGeckoView::class.java)
var thumbnail: Bitmap? = null
var geckoResult = GeckoResult<Bitmap>()
`when`(mockGeckoView.capturePixels()).thenReturn(geckoResult)
engineView.currentGeckoView = mockGeckoView
engineView.captureThumbnail {
thumbnail = it
}
geckoResult.complete(mock())
assertNotNull(thumbnail)
geckoResult = GeckoResult()
`when`(mockGeckoView.capturePixels()).thenReturn(geckoResult)
engineView.captureThumbnail {
thumbnail = it
}
geckoResult.completeExceptionally(mock())
assertNull(thumbnail)
}
}
......@@ -5,7 +5,6 @@
package mozilla.components.browser.engine.gecko
import android.annotation.SuppressLint
import android.graphics.Bitmap
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
......@@ -553,12 +552,6 @@ class GeckoEngineSession(
}
}
override fun captureThumbnail(): Bitmap? {
// TODO Waiting for the Gecko team to create an API for this
// See https://bugzilla.mozilla.org/show_bug.cgi?id=1462018
return null
}
private fun initGeckoSession() {
this.geckoSession = geckoSessionProvider()
defaultSettings?.trackingProtectionPolicy?.let { enableTrackingProtection(it) }
......
......@@ -5,6 +5,7 @@
package mozilla.components.browser.engine.gecko
import android.content.Context
import android.graphics.Bitmap
import android.support.v4.view.ViewCompat
import android.util.AttributeSet
import android.widget.FrameLayout
......@@ -58,4 +59,6 @@ class GeckoEngineView @JvmOverloads constructor(
}
override fun canScrollVerticallyDown() = true // waiting for this issue https://bugzilla.mozilla.org/show_bug.cgi?id=1507569
override fun captureThumbnail(onFinish: (Bitmap?) -> Unit) = Unit
}
......@@ -4,7 +4,6 @@
package mozilla.components.browser.engine.servo
import android.graphics.Bitmap
import android.net.Uri
import mozilla.components.concept.engine.DefaultSettings
import mozilla.components.concept.engine.EngineSession
......@@ -143,9 +142,4 @@ class ServoEngineSession(
override fun exitFullScreenMode() {
// not implemented yet
}
override fun captureThumbnail(): Bitmap? {
// not implemented yet
return null
}
}
......@@ -5,6 +5,7 @@
package mozilla.components.browser.engine.servo
import android.content.Context
import android.graphics.Bitmap
import android.util.AttributeSet
import android.widget.FrameLayout
import mozilla.components.concept.engine.EngineSession
......@@ -43,4 +44,6 @@ class ServoEngineView @JvmOverloads constructor(
servoView.onPause()
}
override fun captureThumbnail(onFinish: (Bitmap?) -> Unit) = Unit
}
......@@ -5,7 +5,6 @@
package mozilla.components.browser.engine.system
import android.content.Context
import android.graphics.Bitmap
import android.os.Bundle
import android.webkit.CookieManager
import android.webkit.WebChromeClient
......@@ -332,13 +331,6 @@ class SystemEngineSession(
fullScreenCallback?.onCustomViewHidden()
}
override fun captureThumbnail(): Bitmap? {
webView.buildDrawingCache()
val outBitmap = webView.drawingCache?.let { cache -> Bitmap.createBitmap(cache) }
webView.destroyDrawingCache()
return outBitmap
}
internal fun toggleDesktopUA(userAgent: String, requestDesktop: Boolean): String {
return if (requestDesktop) {
userAgent.replace("Mobile", "eliboM").replace("Android", "diordnA")
......
......@@ -48,7 +48,6 @@ import mozilla.components.concept.engine.EngineView
import mozilla.components.concept.engine.HitResult
import mozilla.components.concept.engine.prompt.PromptRequest
import mozilla.components.concept.engine.request.RequestInterceptor.InterceptionResponse
import mozilla.components.support.ktx.android.content.isOSOnLowMemory
import mozilla.components.support.utils.DownloadUtils
import java.util.Date
......@@ -157,13 +156,6 @@ class SystemEngineView @JvmOverloads constructor(
secure = cert != null,
host = cert?.let { Uri.parse(url).host },
issuer = cert?.issuedBy?.oName)
if (!isLowOnMemory()) {
val thumbnail = session?.captureThumbnail()
if (thumbnail != null) {
onThumbnailChange(thumbnail)
}
}
}
}
}
......@@ -266,11 +258,6 @@ class SystemEngineView @JvmOverloads constructor(
}
}
@VisibleForTesting
internal var testLowMemory = false
private fun isLowOnMemory() = testLowMemory || (context?.isOSOnLowMemory() == true)
@Suppress("ComplexMethod")
private fun createWebChromeClient() = object : WebChromeClient() {
override fun getVisitedHistory(callback: ValueCallback<Array<String>>) {
......@@ -593,6 +580,20 @@ class SystemEngineView @JvmOverloads constructor(
override fun canScrollVerticallyDown() = session?.webView?.canScrollVertically(1) ?: false
override fun captureThumbnail(onFinish: (Bitmap?) -> Unit) {
val webView = session?.webView
val thumbnails = if (webView == null) {
null
} else {
webView.buildDrawingCache()
val outBitmap = webView.drawingCache?.let { cache -> Bitmap.createBitmap(cache) }
webView.destroyDrawingCache()
outBitmap
}
onFinish(thumbnails)
}
private fun resetJSAlertAbuseState() {
jsAlertCount = 0
shouldShowMoreDialogs = true
......
package mozilla.components.browser.engine.system
import android.content.Context
import android.graphics.Bitmap
import android.net.Uri
import android.os.Bundle
import android.os.Looper
......@@ -671,17 +670,6 @@ class SystemEngineSessionTest {
verify(webViewDatabase).clearHttpAuthUsernamePassword()
}
@Test
fun captureThumbnail() {
val engineSession = spy(SystemEngineSession(getApplicationContext()))
val webView = mock(WebView::class.java)
engineSession.webView = webView
assertNull(engineSession.captureThumbnail())
`when`(webView.drawingCache).thenReturn(Bitmap.createBitmap(10, 10, Bitmap.Config.RGB_565))
assertNotNull(engineSession.captureThumbnail())
}
@Test
fun testExitFullscreenModeWithWebViewAndCallBack() {
val engineSession = SystemEngineSession(getApplicationContext())
......
......@@ -54,7 +54,6 @@ import org.mockito.Mockito.doReturn
import org.mockito.Mockito.mock
import org.mockito.Mockito.never
import org.mockito.Mockito.reset
import org.mockito.Mockito.spy
import org.mockito.Mockito.times
import org.mockito.Mockito.verify
import org.mockito.Mockito.verifyZeroInteractions
......@@ -829,46 +828,6 @@ class SystemEngineViewTest {
assertNull(engineSession.fullScreenCallback)
}
@Test
fun `when a page is loaded a thumbnail should be captured`() {
val engineSession = spy(SystemEngineSession(getApplicationContext()))
doReturn(
Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888)
).`when`(engineSession).captureThumbnail()
val engineView = SystemEngineView(getApplicationContext())
engineView.render(engineSession)
var thumbnailChanged = false
engineSession.register(object : EngineSession.Observer {
override fun onThumbnailChange(bitmap: Bitmap?) {
thumbnailChanged = bitmap != null
}
})
engineSession.webView.webViewClient.onPageFinished(null, "http://mozilla.org")
assertTrue(thumbnailChanged)
}
@Test
fun `when a page is loaded and the os is in low memory condition none thumbnail should be captured`() {
val engineSession = SystemEngineSession(getApplicationContext())
val engineView = SystemEngineView(getApplicationContext())
engineView.render(engineSession)
engineView.testLowMemory = true
var thumbnailChanged = false
engineSession.register(object : EngineSession.Observer {
override fun onThumbnailChange(bitmap: Bitmap?) {
thumbnailChanged = bitmap != null
}
})
engineSession.webView.webViewClient.onPageFinished(null, "http://mozilla.org")
assertFalse(thumbnailChanged)
}
@Test
fun `onPageFinished handles invalid URL`() {
val engineSession = SystemEngineSession(getApplicationContext())
......@@ -1396,6 +1355,32 @@ class SystemEngineViewTest {
assertTrue((request as PromptRequest.Confirm).title.contains("mozilla.org"))
}
@Test
fun captureThumbnail() {
val engineView = SystemEngineView(getApplicationContext())
engineView.session = mock()
`when`(engineView.session!!.webView).thenReturn(mock())
`when`(engineView.session!!.webView.drawingCache)
.thenReturn(Bitmap.createBitmap(10, 10, Bitmap.Config.RGB_565))
var thumbnail: Bitmap? = null
engineView.captureThumbnail {
thumbnail = it
}
assertNotNull(thumbnail)
engineView.session = null
engineView.captureThumbnail {
thumbnail = it
}
assertNull(thumbnail)
}
private fun Date.add(timeUnit: Int, amountOfTime: Int): Date {
val calendar = Calendar.getInstance()
calendar.time = this
......
......@@ -51,7 +51,6 @@ class EngineObserverTest {
override fun findNext(forward: Boolean) {}
override fun clearFindMatches() {}
override fun exitFullScreenMode() {}
override fun captureThumbnail(): Bitmap? = null
override fun saveState(): EngineSessionState = mock()
override fun loadData(data: String, mimeType: String, encoding: String) {
......@@ -100,7 +99,6 @@ class EngineObserverTest {
override fun findNext(forward: Boolean) {}
override fun clearFindMatches() {}
override fun exitFullScreenMode() {}
override fun captureThumbnail(): Bitmap? = null
override fun saveState(): EngineSessionState = mock()
override fun loadData(data: String, mimeType: String, encoding: String) {}
override fun loadUrl(url: String) {
......@@ -140,7 +138,6 @@ class EngineObserverTest {
}
override fun toggleDesktopMode(enable: Boolean, reload: Boolean) {}
override fun captureThumbnail(): Bitmap? = null
override fun saveState(): EngineSessionState = mock()
override fun loadUrl(url: String) {}
override fun loadData(data: String, mimeType: String, encoding: String) {}
......
......@@ -239,11 +239,6 @@ abstract class EngineSession(
*/
abstract fun exitFullScreenMode()
/**
* Takes a screenshot of the actual tab
*/
abstract fun captureThumbnail(): Bitmap?
/**
* Close the session. This may free underlying objects. Call this when you are finished using
* this session.
......
......@@ -7,6 +7,7 @@ package mozilla.components.concept.engine
import android.arch.lifecycle.Lifecycle
import android.arch.lifecycle.LifecycleObserver
import android.arch.lifecycle.OnLifecycleEvent
import android.graphics.Bitmap
import android.view.View
/**
......@@ -65,6 +66,12 @@ interface EngineView {
* true if can and false otherwise.
*/
fun canScrollVerticallyDown(): Boolean = true
/**
* Take a screenshot of the actual session.
* @param onFinish A callback to inform that process of capturing a thumbnail has finished.
*/
fun captureThumbnail(onFinish: (Bitmap?) -> Unit)
}
/**
......
......@@ -625,6 +625,4 @@ open class DummyEngineSession : EngineSession() {
fun notifyInternalObservers(block: Observer.() -> Unit) {
notifyObservers(block)
}
override fun captureThumbnail(): Bitmap? = null
}
\ No newline at end of file
......@@ -5,6 +5,7 @@
package mozilla.components.concept.engine
import android.content.Context
import android.graphics.Bitmap
import android.widget.FrameLayout
import org.junit.Assert.assertTrue
import org.junit.Test
......@@ -59,11 +60,13 @@ class EngineViewTest {
private fun createDummyEngineView(context: Context): EngineView = DummyEngineView(context)
open class DummyEngineView(context: Context?) : FrameLayout(context), EngineView {
override fun captureThumbnail(onFinish: (Bitmap?) -> Unit) = Unit
override fun render(session: EngineSession) {}
}
// Class it not actually a View!
open class BrokenEngineView : EngineView {
override fun captureThumbnail(onFinish: (Bitmap?) -> Unit) = Unit
override fun render(session: EngineSession) {}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment