Skip to content

Commit

Permalink
Merge pull request #1344 from google/bugfix/web_override
Browse files Browse the repository at this point in the history
[Web] Fix webview blocking js reload
  • Loading branch information
bentrengrove committed Sep 27, 2022
2 parents f15c8bd + 53f3293 commit f29f547
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
49 changes: 47 additions & 2 deletions web/src/androidTest/kotlin/com/google/accompanist/web/WebTest.kt
Expand Up @@ -44,6 +44,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.FlakyTest
import androidx.test.filters.SdkSuppress
import com.google.common.truth.Truth.assertThat
import junit.framework.TestCase.assertEquals
import kotlinx.coroutines.flow.toCollection
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
Expand All @@ -60,8 +61,8 @@ import java.util.concurrent.TimeUnit
@RunWith(AndroidJUnit4::class)
// Emulator image doesn't have a WebView until API 26
// Google API emulator image seems to be really flaky before 28 so currently we will set these tests
// to mine 29
@SdkSuppress(minSdkVersion = 28)
// to min 29 and max 30. 31/32 image is also really flaky
@SdkSuppress(minSdkVersion = 28, maxSdkVersion = 30)
class WebTest {
@get:Rule
val rule = createComposeRule()
Expand Down Expand Up @@ -470,6 +471,7 @@ class WebTest {
.check(webMatches(getText(), equalTo(LINK_TEXT)))
}

@FlakyTest
@Test
fun testNavigatorForward() {
lateinit var state: WebViewState
Expand Down Expand Up @@ -502,6 +504,7 @@ class WebTest {

navigator.navigateForward()
rule.waitUntil { navigator.canGoBack }
rule.waitForIdle()

assertThat(state.content.getCurrentUrl()).isEqualTo(LINK_URL)
}
Expand Down Expand Up @@ -618,6 +621,48 @@ class WebTest {
assertThat(isOnDisposeCalled).isTrue()
}

@Test
fun testJSReloadTriggersRefresh() {
lateinit var state: WebViewState
var pageStartedCalled = 0
val client = object : AccompanistWebViewClient() {
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
super.onPageStarted(view, url, favicon)
pageStartedCalled++
}
}

rule.setContent {
state = rememberWebViewStateWithHTMLData(
data =
"""
<html><body>
<input id="button" type="button" value="Reload"
onclick="window.location.reload()" />
</body></html>
""".trimIndent()
)

WebTestContent(
webViewState = state,
idlingResource = idleResource,
client = client
)
}

rule.waitForIdle()

onWebView()
.withElement(findElement(Locator.ID, "button"))
.perform(webClick())

// Check the url remained about:blank
onWebView()
.check(webMatches(getCurrentUrl(), equalTo("about:blank")))

assertEquals("Page should be loaded twice", 2, pageStartedCalled)
}

private val webNode: SemanticsNodeInteraction
get() = rule.onNodeWithTag(WebViewTag)

Expand Down
6 changes: 6 additions & 0 deletions web/src/main/java/com/google/accompanist/web/WebView.kt
Expand Up @@ -207,6 +207,12 @@ open class AccompanistWebViewClient : WebViewClient() {
view: WebView?,
request: WebResourceRequest?
): Boolean {
// If the url hasn't changed, this is probably an internal event like
// a javascript reload. We should let it happen.
if (view?.url == request?.url.toString()) {
return false
}

// Override all url loads to make the single source of truth
// of the URL the state holder Url
request?.let {
Expand Down

0 comments on commit f29f547

Please sign in to comment.