Skip to content

Commit

Permalink
fix: calling webView.loadUrl on destroyed WebView (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
CAMOBAP authored Dec 14, 2023
1 parent 9fb60fa commit a6026df
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import android.os.Handler;
import android.os.Looper;
import android.webkit.WebView;

import androidx.benchmark.BenchmarkState;
import androidx.benchmark.junit4.BenchmarkRule;
Expand Down Expand Up @@ -44,7 +43,7 @@ public void benchmarkWebViewLoad() {
final CountDownLatch latch = new CountDownLatch(1);
try {
rule.getScenario().onActivity(activity -> {
WebView webView = new WebView(activity);
HCaptchaWebView webView = new HCaptchaWebView(activity);
state.resumeTiming();
new HCaptchaWebViewHelper(
handler,
Expand Down
1 change: 0 additions & 1 deletion sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ android {
}

testOptions {
animationsDisabled = true
unitTests {
returnDefaultValues = true
includeAndroidResources = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ final class HCaptchaHeadlessWebView implements IHCaptchaVerifier {
HCaptchaLog.d("HeadlessWebView.init");
this.config = config;
this.listener = listener;
final WebView webView = new HCaptchaWebView(activity);
final HCaptchaWebView webView = new HCaptchaWebView(activity);
webView.setId(R.id.webView);
webView.setVisibility(View.GONE);
if (webView.getParent() == null) {
Expand Down
4 changes: 4 additions & 0 deletions sdk/src/main/java/com/hcaptcha/sdk/HCaptchaWebView.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,8 @@ public boolean onCheckIsTextEditor() {
public boolean performClick() {
return false;
}

public boolean isDestroyed() {
return getParent() == null;
}
}
10 changes: 7 additions & 3 deletions sdk/src/main/java/com/hcaptcha/sdk/HCaptchaWebViewHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final class HCaptchaWebViewHelper {

@Getter
@NonNull
private final WebView webView;
private final HCaptchaWebView webView;

@NonNull
private final IHCaptchaHtmlProvider htmlProvider;
Expand All @@ -50,7 +50,7 @@ final class HCaptchaWebViewHelper {
@NonNull final HCaptchaInternalConfig internalConfig,
@NonNull final IHCaptchaVerifier captchaVerifier,
@NonNull final HCaptchaStateListener listener,
@NonNull final WebView webView) {
@NonNull final HCaptchaWebView webView) {
this.context = context;
this.config = config;
this.captchaVerifier = captchaVerifier;
Expand Down Expand Up @@ -113,7 +113,11 @@ void resetAndExecute() {
}

void reset() {
webView.loadUrl("javascript:reset();");
if (webView.isDestroyed()) {
HCaptchaLog.w("WebView is destroyed already");
} else {
webView.loadUrl("javascript:reset();");
}
}

public boolean shouldRetry(HCaptchaException exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import android.view.ViewGroup;
import android.view.ViewParent;
import android.webkit.WebSettings;
import android.webkit.WebView;

import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -46,7 +45,7 @@ public class HCaptchaWebViewHelperTest {
HCaptchaStateListener stateListener;

@Mock
WebView webView;
HCaptchaWebView webView;

@Mock
WebSettings webSettings;
Expand All @@ -64,7 +63,7 @@ public void init() {
MockitoAnnotations.openMocks(this);
androidLogMock = mockStatic(Log.class);
stateListener = mock(HCaptchaStateListener.class);
webView = mock(WebView.class);
webView = mock(HCaptchaWebView.class);
webSettings = mock(WebSettings.class);
htmlProvider = mock(IHCaptchaHtmlProvider.class);
when(htmlProvider.getHtml()).thenReturn(MOCK_HTML);
Expand Down
3 changes: 3 additions & 0 deletions test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ android {
}

testBuildType = project.hasProperty("testingMinimizedBuild") ? "release" : "debug"
testOptions {
animationsDisabled = true
}
}

androidComponents {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import android.os.Handler;
import android.os.Looper;
import android.webkit.WebView;

import androidx.test.core.app.ActivityScenario;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
Expand Down Expand Up @@ -63,7 +62,7 @@ void onFailure(HCaptchaException e) {

final ActivityScenario<TestActivity> scenario = rule.getScenario();
scenario.onActivity(activity -> {
WebView webView = new WebView(activity);
HCaptchaWebView webView = new HCaptchaWebView(activity);
final HCaptchaWebViewHelper helper = new HCaptchaWebViewHelper(
handler, activity, config, internalConfig, verifier, listener, webView);
});
Expand Down

0 comments on commit a6026df

Please sign in to comment.