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

docs: how to prevent closing challenge upon back press #132

Merged
merged 2 commits into from
Oct 24, 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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,19 @@ No: the SDK depends on WebView, which is a UI component and cannot be instantiat

However, the SDK provides a completely silent (invisible to the end-user) mechanism with `hideDialog=true` config + "passive" site key (this is an Enterprise feature). But note that the token request still has to be called from the UI thread.

> How can I prevent the hCaptcha verification from being canceled when the back button is pressed?

It is possible by specifying `HCaptchaConfig.retryPredicate` as shown in the following code snippet:

```java
final HCaptchaConfig config = HCaptchaConfig.builder()
.siteKey("YOUR_API_SITE_KEY")
.retryPredicate((config, hCaptchaException) -> {
return hCaptchaException.getHCaptchaError() == HCaptchaError.CHALLENGE_CLOSED;
})
.build();
```

## For maintainers

If you plan to contribute to the repo, please see [MAINTAINERS.md](./MAINTAINERS.md) for detailed build, test, and release instructions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static com.hcaptcha.sdk.HCaptchaDialogFragment.KEY_LISTENER;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
Expand All @@ -32,6 +33,7 @@
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import androidx.fragment.app.testing.FragmentScenario;
import androidx.lifecycle.Lifecycle;
import androidx.test.core.app.ActivityScenario;
Expand Down Expand Up @@ -394,8 +396,10 @@ public void testBackShouldNotCloseCaptchaWithoutDefaultLoadingIndicator() {
scenario.onFragment(fragment -> {
final Dialog dialog = fragment.getDialog();
assertNotNull(dialog);
final View rootView = dialog.getWindow().getDecorView().getRootView().findFocus();
assertNotNull(rootView);
final KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK);
assertTrue(dialog.dispatchKeyEvent(keyEvent));
assertTrue(rootView.dispatchKeyEvent(keyEvent));
});
}
}
Expand Down Expand Up @@ -426,4 +430,45 @@ public void testTouchShouldNotCloseCaptchaWithoutDefaultLoadingIndicator() {
});
}
}

@Test
public void testBackShouldNotCloseCaptchaWithCustomRetry() {
try (FragmentScenario<HCaptchaDialogFragment> scenario = launch(
config.toBuilder()
.loading(true)
.retryPredicate((c, e) -> e.getHCaptchaError() == HCaptchaError.CHALLENGE_CLOSED)
.build(),
internalConfig.toBuilder()
.htmlProvider(new HCaptchaTestHtml(false))
.build(),
new HCaptchaStateTestAdapter())) {

scenario.onFragment(fragment -> {
final Dialog dialog = fragment.getDialog();
assertNotNull(dialog);
final View rootView = dialog.getWindow().getDecorView().getRootView().findFocus();
assertNotNull(rootView);
final KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK);
assertTrue(rootView.dispatchKeyEvent(keyEvent));
});
}
}

@Test
public void testNonBackShouldNotCloseCaptcha() throws InterruptedException {
try (FragmentScenario<HCaptchaDialogFragment> scenario = launch(
config,
internalConfig.toBuilder()
.htmlProvider(new HCaptchaTestHtml(false))
.build(),
new HCaptchaStateTestAdapter())) {

scenario.onFragment(fragment -> {
final Dialog dialog = fragment.getDialog();
assertNotNull(dialog);
final KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER);
assertFalse(dialog.dispatchKeyEvent(keyEvent));
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public View onCreateView(@Nullable LayoutInflater inflater,
requireContext(), config, internalConfig, this, listener, webView);
readyForInteraction = false;
return rootView;
} catch (BadParcelableException | InflateException | ClassCastException e) {
} catch (AssertionError | BadParcelableException | InflateException | ClassCastException e) {
HCaptchaLog.w("Cannot create view. Dismissing dialog...");
// Happens when fragment tries to reconstruct because the activity was killed
// And thus there is no way of communicating back
Expand Down