Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Issue #605: Include base64 support + review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jonalmeida authored and csadilek committed Aug 17, 2018
1 parent 32e2064 commit 7a00a81
Show file tree
Hide file tree
Showing 16 changed files with 210 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ class GeckoEngineSession(
/**
* See [EngineSession.loadData]
*/
override fun loadData(data: String, mimeType: String) {
geckoSession.loadString(data, mimeType)
override fun loadData(data: String, mimeType: String, encoding: String) {
when (encoding) {
"base64" -> geckoSession.loadData(data.toByteArray(), mimeType)
else -> geckoSession.loadString(data, mimeType)
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.any
import org.mockito.ArgumentMatchers.anyString
import org.mockito.ArgumentMatchers.eq
import org.mockito.Mockito.`when`
import org.mockito.Mockito.mock
import org.mockito.Mockito.verify
import org.mozilla.gecko.util.BundleEventListener
import org.mozilla.gecko.util.GeckoBundle
import org.mozilla.gecko.util.ThreadUtils
Expand Down Expand Up @@ -121,6 +124,7 @@ class GeckoEngineSessionTest {
@Test
fun testLoadData() {
val engineSession = GeckoEngineSession(mock(GeckoRuntime::class.java))

var loadUriReceived = false
engineSession.geckoSession.eventDispatcher.registerUiThreadListener(
BundleEventListener { _, _, _ -> loadUriReceived = true },
Expand All @@ -131,8 +135,33 @@ class GeckoEngineSessionTest {
assertTrue(loadUriReceived)

loadUriReceived = false
engineSession.loadData("Hello!", "text/plain")
engineSession.loadData("Hello!", "text/plain", "UTF-8")
assertTrue(loadUriReceived)

loadUriReceived = false
engineSession.loadData("ahr0cdovl21vemlsbgeub3jn==", "text/plain", "base64")
assertTrue(loadUriReceived)

loadUriReceived = false
engineSession.loadData("ahr0cdovl21vemlsbgeub3jn==", encoding = "base64")
assertTrue(loadUriReceived)
}

@Test
fun testLoadDataBase64() {
val engineSession = GeckoEngineSession(mock(GeckoRuntime::class.java))
val geckoSession = mock(GeckoSession::class.java)

engineSession.geckoSession = geckoSession

engineSession.loadData("Hello!", "text/plain", "UTF-8")
verify(geckoSession).loadString(eq("Hello!"), anyString())

engineSession.loadData("ahr0cdovl21vemlsbgeub3jn==", "text/plain", "base64")
verify(geckoSession).loadData(eq("ahr0cdovl21vemlsbgeub3jn==".toByteArray()), eq("text/plain"))

engineSession.loadData("ahr0cdovl21vemlsbgeub3jn==", encoding = "base64")
verify(geckoSession).loadData(eq("ahr0cdovl21vemlsbgeub3jn==".toByteArray()), eq("text/plain"))
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ class GeckoEngineSession(
/**
* See [EngineSession.loadData]
*/
override fun loadData(data: String, mimeType: String) {
geckoSession.loadString(data, mimeType)
override fun loadData(data: String, mimeType: String, encoding: String) {
when (encoding) {
"base64" -> geckoSession.loadData(data.toByteArray(), mimeType)
else -> geckoSession.loadString(data, mimeType)
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.any
import org.mockito.ArgumentMatchers.anyString
import org.mockito.ArgumentMatchers.eq
import org.mockito.Mockito.`when`
import org.mockito.Mockito.mock
import org.mockito.Mockito.verify
Expand Down Expand Up @@ -144,6 +146,7 @@ class GeckoEngineSessionTest {
@Test
fun testLoadData() {
val engineSession = GeckoEngineSession(mock(GeckoRuntime::class.java))

var loadUriReceived = false
engineSession.geckoSession.eventDispatcher.registerUiThreadListener(
BundleEventListener { _, _, _ -> loadUriReceived = true },
Expand All @@ -154,8 +157,33 @@ class GeckoEngineSessionTest {
assertTrue(loadUriReceived)

loadUriReceived = false
engineSession.loadData("Hello!", "text/plain")
engineSession.loadData("Hello!", "text/plain", "UTF-8")
assertTrue(loadUriReceived)

loadUriReceived = false
engineSession.loadData("ahr0cdovl21vemlsbgeub3jn==", "text/plain", "base64")
assertTrue(loadUriReceived)

loadUriReceived = false
engineSession.loadData("ahr0cdovl21vemlsbgeub3jn==", encoding = "base64")
assertTrue(loadUriReceived)
}

@Test
fun testLoadDataBase64() {
val engineSession = GeckoEngineSession(mock(GeckoRuntime::class.java))
val geckoSession = mock(GeckoSession::class.java)

engineSession.geckoSession = geckoSession

engineSession.loadData("Hello!", "text/plain", "UTF-8")
verify(geckoSession).loadString(eq("Hello!"), anyString())

engineSession.loadData("ahr0cdovl21vemlsbgeub3jn==", "text/plain", "base64")
verify(geckoSession).loadData(eq("ahr0cdovl21vemlsbgeub3jn==".toByteArray()), eq("text/plain"))

engineSession.loadData("ahr0cdovl21vemlsbgeub3jn==", encoding = "base64")
verify(geckoSession).loadData(eq("ahr0cdovl21vemlsbgeub3jn==".toByteArray()), eq("text/plain"))
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ class GeckoEngineSession(
/**
* See [EngineSession.loadData]
*/
override fun loadData(data: String, mimeType: String) {
geckoSession.loadString(data, mimeType)
override fun loadData(data: String, mimeType: String, encoding: String) {
when (encoding) {
"base64" -> geckoSession.loadData(data.toByteArray(), mimeType)
else -> geckoSession.loadString(data, mimeType)
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ import org.junit.Assert.fail
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.any
import org.mockito.ArgumentMatchers.anyString
import org.mockito.ArgumentMatchers.eq
import org.mockito.Mockito.`when`
import org.mockito.Mockito.mock
import org.mockito.Mockito.verify
import org.mozilla.gecko.util.BundleEventListener
import org.mozilla.gecko.util.GeckoBundle
import org.mozilla.geckoview.GeckoResponse
Expand Down Expand Up @@ -120,6 +123,7 @@ class GeckoEngineSessionTest {
@Test
fun testLoadData() {
val engineSession = GeckoEngineSession(mock(GeckoRuntime::class.java))

var loadUriReceived = false
engineSession.geckoSession.eventDispatcher.registerUiThreadListener(
BundleEventListener { _, _, _ -> loadUriReceived = true },
Expand All @@ -130,8 +134,33 @@ class GeckoEngineSessionTest {
assertTrue(loadUriReceived)

loadUriReceived = false
engineSession.loadData("Hello!", "text/plain")
engineSession.loadData("Hello!", "text/plain", "UTF-8")
assertTrue(loadUriReceived)

loadUriReceived = false
engineSession.loadData("ahr0cdovl21vemlsbgeub3jn==", "text/plain", "base64")
assertTrue(loadUriReceived)

loadUriReceived = false
engineSession.loadData("ahr0cdovl21vemlsbgeub3jn==", encoding = "base64")
assertTrue(loadUriReceived)
}

@Test
fun testLoadDataBase64() {
val engineSession = GeckoEngineSession(mock(GeckoRuntime::class.java))
val geckoSession = mock(GeckoSession::class.java)

engineSession.geckoSession = geckoSession

engineSession.loadData("Hello!", "text/plain", "UTF-8")
verify(geckoSession).loadString(eq("Hello!"), anyString())

engineSession.loadData("ahr0cdovl21vemlsbgeub3jn==", "text/plain", "base64")
verify(geckoSession).loadData(eq("ahr0cdovl21vemlsbgeub3jn==".toByteArray()), eq("text/plain"))

engineSession.loadData("ahr0cdovl21vemlsbgeub3jn==", encoding = "base64")
verify(geckoSession).loadData(eq("ahr0cdovl21vemlsbgeub3jn==".toByteArray()), eq("text/plain"))
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package mozilla.components.browser.engine.system

/**
* Used for when we need to schedule loading a url or data with a mime type, if there isn't
* a session already created.
*/
data class ScheduledLoad constructor(
val url: String?,
val data: String? = null,
val mimeType: String? = null
) {
constructor(data: String, mimeType: String) : this(null, data, mimeType)
constructor() : this(null, null, null)
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ import java.lang.ref.WeakReference
* WebView-based EngineSession implementation.
*/
class SystemEngineSession : EngineSession() {
internal var urlToLoad: String? = null
internal var mimeTypeToLoad: String? = null
internal var view: WeakReference<SystemEngineView>? = null
internal var scheduledLoad = ScheduledLoad(null)

/**
* See [EngineSession.loadUrl]
Expand All @@ -27,7 +26,7 @@ class SystemEngineSession : EngineSession() {
if (internalView == null) {
// We can't load a URL without a WebView. So let's just remember the URL here until
// this session gets linked to a WebView. See: EngineView.render(session).
urlToLoad = url
scheduledLoad = ScheduledLoad(url)
} else {
internalView.loadUrl(url)
}
Expand All @@ -36,16 +35,15 @@ class SystemEngineSession : EngineSession() {
/**
* See [EngineSession.loadData]
*/
override fun loadData(data: String, mimeType: String) {
override fun loadData(data: String, mimeType: String, encoding: String) {
val internalView = currentView()

if (internalView == null) {
// We remember the data that we want to load and when then session gets linked
// to a WebView we check for a mimeType and call loadData then.
urlToLoad = data
mimeTypeToLoad = mimeType
// to a WebView we call loadData then.
scheduledLoad = ScheduledLoad(data, mimeType)
} else {
internalView.loadData(data, mimeType, "UTF-8")
internalView.loadData(data, mimeType, encoding)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,14 @@ class SystemEngineView @JvmOverloads constructor(

internalSession.view = WeakReference(this)

internalSession.mimeTypeToLoad?.let {
currentWebView.loadData(internalSession.urlToLoad, internalSession.mimeTypeToLoad, "UTF-8")
internalSession.urlToLoad = null
internalSession.mimeTypeToLoad = null
internalSession.scheduledLoad.data?.let {
currentWebView.loadData(it, internalSession.scheduledLoad.mimeType, "UTF-8")
internalSession.scheduledLoad = ScheduledLoad()
}

internalSession.urlToLoad?.let {
currentWebView.loadUrl(internalSession.urlToLoad)
internalSession.urlToLoad = null
internalSession.scheduledLoad.url?.let {
currentWebView.loadUrl(it)
internalSession.scheduledLoad = ScheduledLoad()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package mozilla.components.browser.engine.system

import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Test

class ScheduledLoadTest {
@Test
fun testConstructorNullability() {
var scheduledLoad = ScheduledLoad()
assertNull(scheduledLoad.url)
assertNull(scheduledLoad.data)
assertNull(scheduledLoad.mimeType)

scheduledLoad = ScheduledLoad("https://mozilla.org")
assertNotNull(scheduledLoad.url)
assertNull(scheduledLoad.data)
assertNull(scheduledLoad.mimeType)

scheduledLoad = ScheduledLoad("<html><body></body></html>", "text/html")
assertNull(scheduledLoad.url)
assertNotNull(scheduledLoad.data)
assertNotNull(scheduledLoad.mimeType)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,13 @@ class SystemEngineSessionTest {
`when`(engineSession.currentView()).thenReturn(webView)

engineSession.loadData("<html><body>Hello!</body></html>")
verify(webView).loadData(anyString(), eq("text/html"), eq("UTF-8"))
verify(webView).loadData(eq("<html><body>Hello!</body></html>"), eq("text/html"), eq("UTF-8"))

engineSession.loadData("Hello!", "text/plain")
verify(webView).loadData(anyString(), eq("text/plain"), eq("UTF-8"))
engineSession.loadData("Hello!", "text/plain", "UTF-8")
verify(webView).loadData(eq("Hello!"), eq("text/plain"), eq("UTF-8"))

engineSession.loadData("ahr0cdovl21vemlsbgeub3jn==", "text/plain", "base64")
verify(webView).loadData(eq("ahr0cdovl21vemlsbgeub3jn=="), eq("text/plain"), eq("base64"))
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class EngineObserverTest {
return emptyMap()
}

override fun loadData(data: String, mimeType: String) {
override fun loadData(data: String, mimeType: String, encoding: String) {
notifyObservers { onLocationChange(data) }
notifyObservers { onProgress(100) }
notifyObservers { onLoadingStateChange(true) }
Expand Down Expand Up @@ -68,10 +68,7 @@ class EngineObserverTest {
return emptyMap()
}

override fun loadData(data: String, mimeType: String) {
// TODO: What do we do here?
}

override fun loadData(data: String, mimeType: String, encoding: String) {}
override fun loadUrl(url: String) {
if (url.startsWith("https://")) {
notifyObservers { onSecurityChange(true, "host", "issuer") }
Expand Down Expand Up @@ -109,7 +106,7 @@ class EngineObserverTest {
}

override fun loadUrl(url: String) {}
override fun loadData(data: String, mimeType: String) {}
override fun loadData(data: String, mimeType: String, encoding: String) {}
}
val observer = EngineObserver(session)
engineSession.register(observer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,22 @@ abstract class EngineSession(

/**
* Loads the data with the given mimeType.
* Example:
* ```
* engineSession.loadData("<html><body>Example HTML content here</body></html>", "text/html")
* ```
*
* If the data is base64 encoded, you can override the default encoding (UTF-8) with 'base64'.
* Example:
* ```
* engineSession.loadData("ahr0cdovl21vemlsbgeub3jn==", "text/plain", "base64")
* ```
*
* @param data The data that should be rendering.
* @param mimeType the data type needed by the engine to know how to render it.
* @param encoding specifies whether the data is base64 encoded; use 'base64' else defaults to "UTF-8".
*/
abstract fun loadData(data: String, mimeType: String = "text/html")
abstract fun loadData(data: String, mimeType: String = "text/html", encoding: String = "UTF-8")

/**
* Stops loading the current session.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ open class DummyEngineSession : EngineSession() {

override fun loadUrl(url: String) {}

override fun loadData(data: String, mimeType: String) {}
override fun loadData(data: String, mimeType: String, encoding: String) {}

override fun stopLoading() {}

Expand Down
Loading

0 comments on commit 7a00a81

Please sign in to comment.