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

override onRenderProcessGone #9

Merged
merged 4 commits into from
Nov 23, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ internal class CheckoutDialog(

setOnCancelListener {
checkoutEventProcessor.onCheckoutCanceled()
(checkoutWebView.parent as ViewGroup).removeView(checkoutWebView)
checkoutWebView.parent?.let {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parent may now be removed in onRenderProcessGone, so we should do a null check here

(checkoutWebView.parent as ViewGroup).removeView(checkoutWebView)
}
}

header.setOnMenuItemClickListener {
Expand Down
16 changes: 16 additions & 0 deletions lib/src/main/java/com/shopify/checkoutkit/CheckoutWebView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color.TRANSPARENT
import android.net.Uri
import android.os.Build
import android.os.Handler
import android.os.Looper
import android.util.AttributeSet
import android.view.KeyEvent
import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import android.webkit.RenderProcessGoneDetail
import android.webkit.WebResourceError
import android.webkit.WebResourceRequest
import android.webkit.WebResourceResponse
Expand Down Expand Up @@ -116,6 +119,19 @@ internal class CheckoutWebView(context: Context, attributeSet: AttributeSet? = n
checkoutBridge.getEventProcessor().onCheckoutViewLoadComplete()
}

override fun onRenderProcessGone(view: WebView, detail: RenderProcessGoneDetail): Boolean {
kiftio marked this conversation as resolved.
Show resolved Hide resolved
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !detail.didCrash()) {
// Renderer was killed because system ran out of memory.

// Removing the view from `CheckoutWebViewContainer will trigger a cache clear
// and call webView.destroy()
(view.parent as ViewGroup).removeView(view)
true
} else {
false
}
}

override fun shouldOverrideUrlLoading(
view: WebView?,
request: WebResourceRequest?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package com.shopify.checkoutkit

import android.net.Uri
import android.webkit.RenderProcessGoneDetail
import android.webkit.WebResourceError
import android.webkit.WebResourceRequest
import android.webkit.WebResourceResponse
Expand All @@ -39,6 +40,7 @@ import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import org.robolectric.Robolectric
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config

@RunWith(RobolectricTestRunner::class)
class CheckoutWebViewClientTest {
Expand Down Expand Up @@ -147,6 +149,47 @@ class CheckoutWebViewClientTest {
verify(checkoutWebViewEventProcessor).onCheckoutViewLoadComplete()
}

@Test
fun `onRenderProcessGone should return false if sdk version is too low to check detail#didCrash()`() {
val view = viewWithProcessor(activity).withParent()
val webViewClient = view.CheckoutWebViewClient()
val detail = mock<RenderProcessGoneDetail>()
whenever(detail.didCrash()).thenReturn(false)

val result = webViewClient.onRenderProcessGone(view, detail)

assertThat(result).isFalse
assertThat(view.parent).isNotNull
}

@Config(sdk = [26])
@Test
fun `onRenderProcessGone should do nothing if the renderer crashed`() {
val view = viewWithProcessor(activity).withParent()
val webViewClient = view.CheckoutWebViewClient()
val detail = mock<RenderProcessGoneDetail>()
whenever(detail.didCrash()).thenReturn(true)

val result = webViewClient.onRenderProcessGone(view, detail)

assertThat(result).isFalse
assertThat(view.parent).isNotNull
}

@Config(sdk = [26])
@Test
fun `onRenderProcessGone should remove the view from its parent if the render process crashed due to low memory`() {
val view = viewWithProcessor(activity).withParent()
val webViewClient = view.CheckoutWebViewClient()
val detail = mock<RenderProcessGoneDetail>()
whenever(detail.didCrash()).thenReturn(false)

val result = webViewClient.onRenderProcessGone(view, detail)

assertThat(result).isTrue
assertThat(view.parent).isNull()
}

private fun mockWebRequest(uri: Uri, forMainFrame: Boolean = false): WebResourceRequest {
val mockRequest = mock<WebResourceRequest>()
whenever(mockRequest.url).thenReturn(uri)
Expand Down Expand Up @@ -181,4 +224,10 @@ class CheckoutWebViewClientTest {
whenever(mock.reasonPhrase).thenReturn(description)
return mock
}

private fun CheckoutWebView.withParent(): CheckoutWebView {
val container = CheckoutWebViewContainer(activity)
container.addView(this)
return this
}
}