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

fix: calling webView.loadUrl on destroyed WebView #136

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
Loading