Skip to content

Commit

Permalink
feat(config): orientation (#103)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexande B <[email protected]>
  • Loading branch information
DSergiu and CAMOBAP authored Apr 5, 2023
1 parent 60e65cc commit 1ace918
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

# 3.7.0

- Feat: new `HCaptchaConfig.orientation` to set either `portrait` or `landscape` challenge orientation.

# 3.6.0

- Feat: new `HCaptcha.removeAllListener` and `HCaptcha.removeOn[Success|Failure|Open]Listener(listener)` to remove all or specific listener.
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ The following list contains configuration properties to allows customization of
|-------------------|-------------------------|----------|----------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `siteKey` | String | **Yes** | - | This is your sitekey, this allows you to load challenges. If you need a sitekey, please visit [hCaptcha](https://www.hcaptcha.com), and sign up to get your sitekey. |
| `size` | Enum | No | INVISIBLE | This specifies the "size" of the checkbox component. By default, the checkbox is invisible and the challenge is shown automatically. |
| `orientation` | Enum | No | PORTRAIT | This specifies the "orientation" of the challenge. |
| `theme` | Enum | No | LIGHT | hCaptcha supports light, dark, and contrast themes. |
| `locale` | String (ISO 639-1 code) | No | AUTO | You can enforce a specific language or let hCaptcha auto-detect the local language based on user's device. |
| `resetOnTimeout` | Boolean | No | False | (DEPRECATED, use `retryPredicate`) Automatically reload to fetch new challenge if user does not submit challenge. (Matches iOS SDK behavior.) |
Expand Down
4 changes: 2 additions & 2 deletions sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ android {
// See https://developer.android.com/studio/publish/versioning
// versionCode must be integer and be incremented by one for every new update
// android system uses this to prevent downgrades
versionCode 30
versionCode 31

// version number visible to the user
// should follow semantic versioning (See https://semver.org)
versionName "3.6.0"
versionName "3.7.0"

buildConfigField 'String', 'VERSION_NAME', "\"${defaultConfig.versionName}_${defaultConfig.versionCode}\""

Expand Down
2 changes: 2 additions & 0 deletions sdk/src/main/html/hcaptcha.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
siteKey: '10000000-ffff-ffff-ffff-000000000001',
locale: 'ro',
size: 'compact',
orientation: 'portrait',
theme: 'dark',
sentry: true,
rqdata: null,
Expand Down Expand Up @@ -94,6 +95,7 @@
return {
sitekey: bridgeConfig.siteKey,
size: bridgeConfig.size,
orientation: bridgeConfig.orientation,
theme: getTheme(bridgeConfig),
callback: function callback(token) {
return BridgeObject.onPass(token);
Expand Down
6 changes: 6 additions & 0 deletions sdk/src/main/java/com/hcaptcha/sdk/HCaptchaConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ public class HCaptchaConfig implements Serializable {
@Builder.Default
private HCaptchaSize size = HCaptchaSize.INVISIBLE;

/**
* The orientation of the challenge. Default is {@link HCaptchaOrientation#PORTRAIT}.
*/
@Builder.Default
private HCaptchaOrientation orientation = HCaptchaOrientation.PORTRAIT;

/**
* The theme. Default is {@link HCaptchaTheme#LIGHT}.
*/
Expand Down
39 changes: 39 additions & 0 deletions sdk/src/main/java/com/hcaptcha/sdk/HCaptchaOrientation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.hcaptcha.sdk;

import androidx.annotation.NonNull;

import com.fasterxml.jackson.annotation.JsonValue;

import java.io.Serializable;


/**
* The hCaptcha challenge orientation
*/
public enum HCaptchaOrientation implements Serializable {

PORTRAIT("portrait"),

LANDSCAPE("landscape");

private final String orientation;

HCaptchaOrientation(final String orientation) {
this.orientation = orientation;
}

/**
* @return the hCaptcha api.js string encoding
*/
public String getOrientation() {
return this.orientation;
}

@JsonValue
@NonNull
@Override
public String toString() {
return orientation;
}

}
6 changes: 5 additions & 1 deletion sdk/src/test/java/com/hcaptcha/sdk/HCaptchaConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ public void custom_locale() {
}

@Test
public void default_confis() {
public void default_config() {
final HCaptchaConfig config = HCaptchaConfig.builder().siteKey(MOCK_SITE_KEY).build();
assertEquals(MOCK_SITE_KEY, config.getSiteKey());
assertEquals(true, config.getSentry());
assertEquals(HCaptchaSize.INVISIBLE, config.getSize());
assertEquals(HCaptchaOrientation.PORTRAIT, config.getOrientation());
assertEquals(HCaptchaTheme.LIGHT, config.getTheme());
assertEquals(Locale.getDefault().getLanguage(), config.getLocale());
assertEquals("https://js.hcaptcha.com/1/api.js", config.getJsSrc());
Expand All @@ -36,6 +37,7 @@ public void default_confis() {

@Test
public void custom_config() {
final HCaptchaOrientation hCaptchaOrientation = HCaptchaOrientation.LANDSCAPE;
final HCaptchaSize hCaptchaSize = HCaptchaSize.COMPACT;
final HCaptchaTheme hCaptchaTheme = HCaptchaTheme.DARK;
final String customRqdata = "custom rqdata value";
Expand All @@ -55,11 +57,13 @@ public void custom_config() {
.sentry(sentry)
.theme(hCaptchaTheme)
.size(hCaptchaSize)
.orientation(hCaptchaOrientation)
.customTheme(customTheme)
.build();
assertEquals(MOCK_SITE_KEY, config.getSiteKey());
assertEquals(sentry, config.getSentry());
assertEquals(hCaptchaSize, config.getSize());
assertEquals(hCaptchaOrientation, config.getOrientation());
assertEquals(hCaptchaTheme, config.getTheme());
assertEquals(customLocale, config.getLocale());
assertEquals(customRqdata, config.getRqdata());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public void init() {
public void full_config_serialization() throws JSONException {
final String siteKey = "0000-1111-2222-3333";
final String locale = "ro";
final HCaptchaOrientation orientation = HCaptchaOrientation.PORTRAIT;
final HCaptchaSize size = HCaptchaSize.NORMAL;
final String rqdata = "custom rqdata";
final String jsSrc = "127.0.0.1/api.js";
Expand All @@ -63,6 +64,7 @@ public void full_config_serialization() throws JSONException {
.siteKey(siteKey)
.locale(locale)
.size(size)
.orientation(orientation)
.theme(HCaptchaTheme.DARK)
.rqdata(rqdata)
.jsSrc(jsSrc)
Expand Down Expand Up @@ -90,6 +92,7 @@ public void full_config_serialization() throws JSONException {
expected.put("imghost", imghost);
expected.put("locale", locale);
expected.put("size", size.toString());
expected.put("orientation", orientation.toString());
expected.put("theme", HCaptchaTheme.DARK.toString());
expected.put("customTheme", JSONObject.NULL);
expected.put("host", host);
Expand All @@ -106,12 +109,14 @@ public void subset_config_serialization() throws JSONException {
final String siteKey = "0000-1111-2222-3333";
final String locale = "ro";
final HCaptchaSize size = HCaptchaSize.NORMAL;
final HCaptchaOrientation orientation = HCaptchaOrientation.LANDSCAPE;
final String rqdata = "custom rqdata";
final long defaultTimeout = 120;
final HCaptchaConfig config = HCaptchaConfig.builder()
.siteKey(siteKey)
.locale(locale)
.size(size)
.orientation(orientation)
.theme(HCaptchaTheme.DARK)
.rqdata(rqdata)
.build();
Expand All @@ -129,6 +134,7 @@ public void subset_config_serialization() throws JSONException {
expected.put("imghost", JSONObject.NULL);
expected.put("locale", locale);
expected.put("size", size.toString());
expected.put("orientation", orientation.toString());
expected.put("theme", HCaptchaTheme.DARK.toString());
expected.put("customTheme", JSONObject.NULL);
expected.put("host", JSONObject.NULL);
Expand Down

0 comments on commit 1ace918

Please sign in to comment.