Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Web] Add a method to expose WebViewClient and WebChromeClient #1097

Merged
merged 4 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
package com.google.accompanist.sample.webview

import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.os.Bundle
import android.util.Log
import android.webkit.WebView
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
Expand All @@ -44,6 +47,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.unit.dp
import com.google.accompanist.sample.AccompanistSampleTheme
import com.google.accompanist.web.AccompanistWebViewClient
import com.google.accompanist.web.LoadingState
import com.google.accompanist.web.WebContent
import com.google.accompanist.web.WebView
Expand Down Expand Up @@ -115,13 +119,28 @@ class BasicWebViewSample : ComponentActivity() {
)
}

// A custom WebViewClient and WebChromeClient can be provided via subclassing
val webClient = remember {
object : AccompanistWebViewClient() {
override fun onPageStarted(
view: WebView?,
url: String?,
favicon: Bitmap?
) {
super.onPageStarted(view, url, favicon)
Log.d("Accompanist WebView", "Page started loading for $url")
}
}
}

WebView(
state = state,
modifier = Modifier.weight(1f),
navigator = navigator,
onCreated = { webView ->
webView.settings.javaScriptEnabled = true
}
},
client = webClient
)
}
}
Expand Down
19 changes: 18 additions & 1 deletion web/api/current.api
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
// Signature format: 4.0
package com.google.accompanist.web {

public class AccompanistWebChromeClient extends android.webkit.WebChromeClient {
ctor public AccompanistWebChromeClient();
method public com.google.accompanist.web.WebViewState getState();
property public com.google.accompanist.web.WebViewState state;
field public com.google.accompanist.web.WebViewState state;
}

public class AccompanistWebViewClient extends android.webkit.WebViewClient {
ctor public AccompanistWebViewClient();
method public com.google.accompanist.web.WebViewNavigator getNavigator();
method public com.google.accompanist.web.WebViewState getState();
property public com.google.accompanist.web.WebViewNavigator navigator;
property public com.google.accompanist.web.WebViewState state;
field public com.google.accompanist.web.WebViewNavigator navigator;
field public com.google.accompanist.web.WebViewState state;
}

public abstract sealed class LoadingState {
}

Expand Down Expand Up @@ -51,7 +68,7 @@ package com.google.accompanist.web {
}

public final class WebViewKt {
method @androidx.compose.runtime.Composable public static void WebView(com.google.accompanist.web.WebViewState state, optional androidx.compose.ui.Modifier modifier, optional boolean captureBackPresses, optional com.google.accompanist.web.WebViewNavigator navigator, optional kotlin.jvm.functions.Function1<? super android.webkit.WebView,kotlin.Unit> onCreated, optional kotlin.jvm.functions.Function2<? super android.webkit.WebResourceRequest,? super android.webkit.WebResourceError,kotlin.Unit> onError);
method @androidx.compose.runtime.Composable public static void WebView(com.google.accompanist.web.WebViewState state, optional androidx.compose.ui.Modifier modifier, optional boolean captureBackPresses, optional com.google.accompanist.web.WebViewNavigator navigator, optional kotlin.jvm.functions.Function1<? super android.webkit.WebView,kotlin.Unit> onCreated, optional com.google.accompanist.web.AccompanistWebViewClient client, optional com.google.accompanist.web.AccompanistWebChromeClient chromeClient);
method @androidx.compose.runtime.Composable public static com.google.accompanist.web.WebViewNavigator rememberWebViewNavigator(optional kotlinx.coroutines.CoroutineScope coroutineScope);
method @androidx.compose.runtime.Composable public static com.google.accompanist.web.WebViewState rememberWebViewState(String url);
method @androidx.compose.runtime.Composable public static com.google.accompanist.web.WebViewState rememberWebViewStateWithHTMLData(String data, optional String? baseUrl);
Expand Down
69 changes: 68 additions & 1 deletion web/src/androidTest/kotlin/com/google/accompanist/web/WebTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

package com.google.accompanist.web

import android.graphics.Bitmap
import android.webkit.WebView
import androidx.compose.material.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.testTag
Expand Down Expand Up @@ -175,6 +178,66 @@ class WebTest {
.check(webMatches(getCurrentUrl(), containsString(LINK_URL)))
}

@Test
fun testCustomClientIsAssigned() {
lateinit var state: WebViewState

var pageStartCalled = false

rule.setContent {
state = rememberWebViewStateWithHTMLData(data = TEST_DATA)
WebTestContent(
state,
idleResource,
client = object : AccompanistWebViewClient() {
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
super.onPageStarted(view, url, favicon)
pageStartCalled = true
}
}
)
}

// Ensure the data is loaded first
onWebView()
.check(webMatches(getCurrentUrl(), containsString("about:blank")))

// Wait for the webview to load and then perform the check
rule.waitForIdle()

assertThat(pageStartCalled).isTrue()
}

@Test
fun testChromeClientIsAssigned() {
lateinit var state: WebViewState

var titleReceived: String? = null

rule.setContent {
state = rememberWebViewStateWithHTMLData(data = TEST_TITLE_DATA)
WebTestContent(
state,
idleResource,
chromeClient = object : AccompanistWebChromeClient() {
override fun onReceivedTitle(view: WebView?, title: String?) {
super.onReceivedTitle(view, title)
titleReceived = title
}
}
)
}

// Wait for new title
onWebView()
.check(webMatches(getTitle(), equalTo(TITLE_TEXT)))

// Wait for the webview to load and then perform the check
rule.waitForIdle()

assertThat(titleReceived).isEqualTo(TITLE_TEXT)
}

// SDKs less than 29 do not call onPageStarted when loading about:blank.
// This breaks the idling resource counter.
// This test was testing an edge case that no longer exists and could potentially
Expand Down Expand Up @@ -467,6 +530,8 @@ private fun WebTestContent(
webViewState: WebViewState,
idlingResource: WebViewIdlingResource,
navigator: WebViewNavigator = rememberWebViewNavigator(),
client: AccompanistWebViewClient = remember { AccompanistWebViewClient() },
chromeClient: AccompanistWebChromeClient = remember { AccompanistWebChromeClient() }
) {
idlingResource.webviewLoading = webViewState.loadingState !is LoadingState.Finished

Expand All @@ -475,7 +540,9 @@ private fun WebTestContent(
state = webViewState,
modifier = Modifier.testTag(WebViewTag),
navigator = navigator,
onCreated = { it.settings.javaScriptEnabled = true }
onCreated = { it.settings.javaScriptEnabled = true },
client = client,
chromeClient = chromeClient
)
}
}
Expand Down