diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 75441c75d..5966c8475 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -67,4 +67,4 @@ jobs: working-directory: ./android steps: - uses: actions/checkout@v2 - - run: ./gradlew clean build -b capacitor/build.gradle -Pandroid.useAndroidX=true -Pandroid.enableJetifier=true + - run: ./gradlew clean test build -b capacitor/build.gradle -Pandroid.useAndroidX=true -Pandroid.enableJetifier=true diff --git a/android/capacitor/build.gradle b/android/capacitor/build.gradle index 7c22386cb..34262e4f9 100644 --- a/android/capacitor/build.gradle +++ b/android/capacitor/build.gradle @@ -70,4 +70,5 @@ dependencies { androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion" androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion" implementation "org.apache.cordova:framework:$cordovaAndroidVersion" + testImplementation 'org.json:json:20140107' } \ No newline at end of file diff --git a/android/capacitor/src/main/java/com/getcapacitor/JSObject.java b/android/capacitor/src/main/java/com/getcapacitor/JSObject.java index 1a765f667..376b2b7b3 100644 --- a/android/capacitor/src/main/java/com/getcapacitor/JSObject.java +++ b/android/capacitor/src/main/java/com/getcapacitor/JSObject.java @@ -54,6 +54,10 @@ public String getString(String key, String defaultValue) { return defaultValue; } + public Integer getInteger(String key) { + return getInteger(key, null); + } + public Integer getInteger(String key, Integer defaultValue) { try { return super.getInt(key); @@ -62,10 +66,6 @@ public Integer getInteger(String key, Integer defaultValue) { return defaultValue; } - public Integer getInteger(String key) { - return getInteger(key, null); - } - public Boolean getBoolean(String key, Boolean defaultValue) { try { return super.getBoolean(key); diff --git a/android/capacitor/src/test/java/com/getcapacitor/JSObjectTest.java b/android/capacitor/src/test/java/com/getcapacitor/JSObjectTest.java new file mode 100644 index 000000000..95a9881db --- /dev/null +++ b/android/capacitor/src/test/java/com/getcapacitor/JSObjectTest.java @@ -0,0 +1,209 @@ +package com.getcapacitor; + +import org.json.JSONException; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class JSObjectTest { + + @Test + public void getStringReturnsNull_WhenJSObject_IsConstructed_WithNoInitialJSONObject() { + JSObject jsObject = new JSObject(); + + String actualValue = jsObject.getString("should be null"); + + assertNull(actualValue); + } + + @Test + public void getStringReturnsExpectedValue_WhenJSObject_IsConstructed_WithAValidJSONObject() throws JSONException { + JSObject jsObject = new JSObject("{\"thisKeyExists\": \"this is the key value\"}"); + + String expectedValue = jsObject.getString("thisKeyExists"); + String actualValue = "this is the key value"; + + assertEquals(expectedValue, actualValue); + } + + @Test + public void getStringReturnsDefaultValue_WhenJSObject_IsConstructed_WithNoInitialJSONObject() throws JSONException { + JSObject jsObject = new JSObject(); + + String expectedValue = jsObject.getString("thisKeyDoesNotExist", "default value"); + String actualValue = "default value"; + + assertEquals(expectedValue, actualValue); + } + + @Test + public void getStringReturnsDefaultValue_WhenJSObject_IsConstructed_WithAValueAsInteger() throws JSONException { + JSObject jsObject = new JSObject("{\"thisKeyExists\": 1}"); + + String expectedValue = jsObject.getString("thisKeyExists", "default value"); + String actualValue = "default value"; + + assertEquals(expectedValue, actualValue); + } + + @Test + public void getIntegerReturnsNull_WhenJSObject_IsConstructed_WithNoInitialJSONObject() { + JSObject jsObject = new JSObject(); + + Integer actualValue = jsObject.getInteger("should be null"); + + assertNull(actualValue); + } + + @Test + public void getIntegerReturnsExpectedValue_WhenJSObject_IsConstructed_WithAValidJSONObject() throws JSONException { + JSObject jsObject = new JSObject("{\"thisKeyExists\": 1}"); + + Integer expectedValue = jsObject.getInteger("thisKeyExists"); + Integer actualValue = 1; + + assertEquals(expectedValue, actualValue); + } + + @Test + public void getIntegerReturnsDefaultValue_WhenJSObject_IsConstructed_WithNoInitialJSONObject() throws JSONException { + JSObject jsObject = new JSObject(); + + Integer expectedValue = jsObject.getInteger("thisKeyDoesNotExist", 1); + Integer actualValue = 1; + + assertEquals(expectedValue, actualValue); + } + + @Test + public void getStringReturnsDefaultValue_WhenJSObject_IsConstructed_WithAValueAsString() throws JSONException { + JSObject jsObject = new JSObject("{\"thisKeyExists\": \"not an integer\"}"); + + Integer expectedValue = jsObject.getInteger("thisKeyExists", 1); + Integer actualValue = 1; + + assertEquals(expectedValue, actualValue); + } + + @Test + public void getBoolReturnsNull_WhenJSObject_IsConstructed_WithNoInitialJSONObject() { + JSObject jsObject = new JSObject(); + + Boolean actualValue = jsObject.getBool("should be null"); + + assertNull(actualValue); + } + + @Test + public void getBoolReturnsExpectedValue_WhenJSObject_IsConstructed_WithAValidJSONObject() throws JSONException { + JSObject jsObject = new JSObject("{\"thisKeyExists\": true}"); + + Boolean expectedValue = jsObject.getBool("thisKeyExists"); + Boolean actualValue = true; + + assertEquals(expectedValue, actualValue); + } + + @Test + public void getBooleanReturnsDefaultValue_WhenJSObject_IsConstructed_WithNoInitialJSONObject() throws JSONException { + JSObject jsObject = new JSObject(); + + Boolean expectedValue = jsObject.getBoolean("thisKeyDoesNotExist", true); + Boolean actualValue = true; + + assertEquals(expectedValue, actualValue); + } + + @Test + public void getBooleanReturnsDefaultValue_WhenJSObject_IsConstructed_WithAValueAsString() throws JSONException { + JSObject jsObject = new JSObject("{\"thisKeyExists\": \"not an integer\"}"); + + Boolean expectedValue = jsObject.getBoolean("thisKeyExists", true); + Boolean actualValue = true; + + assertEquals(expectedValue, actualValue); + } + + @Test + public void getJSObjectReturnsNull_WhenJSObject_IsConstructed_WithNoInitialJSONObject() { + JSObject jsObject = new JSObject(); + + JSObject actualValue = jsObject.getJSObject("should be null"); + + assertNull(actualValue); + } + + @Test + public void getJsObjectReturnsExpectedValue_WhenJSObject_IsConstructed_WithAValidJSONObject() throws JSONException { + JSObject jsObject = new JSObject("{\"thisKeyExists\": { \"innerObjectKey\": \"innerObjectValue\" }}"); + + String actualValue = jsObject.getJSObject("thisKeyExists").getString("innerObjectKey"); + String expectedValue = "innerObjectValue"; + + assertEquals(expectedValue, actualValue); + } + + @Test + public void getJSObjectReturnsDefaultValue_WhenJSObject_IsConstructed_WithNoInitialJSONObject() throws JSONException { + JSObject jsObject = new JSObject(); + + String actualValue = jsObject.getJSObject("thisKeyExists", new JSObject("{\"thisKeyExists\": \"default string\"}")).getString("thisKeyExists"); + String expectedValue = "default string"; + + assertEquals(expectedValue, actualValue); + } + + @Test + public void putBoolean_AddsValueToJSObject_UnderCorrectKey() { + JSObject jsObject = new JSObject(); + jsObject.put("bool", true); + + Boolean actualValue = jsObject.getBool("bool"); + + assertTrue(actualValue); + } + + @Test + public void putInteger_AddsValueToJSObject_UnderCorrectKey() { + JSObject jsObject = new JSObject(); + jsObject.put("integer", 1); + + Integer expectedValue = 1; + Integer actualValue = jsObject.getInteger("integer"); + + assertEquals(actualValue, expectedValue); + } + + @Test + public void putLong_AddsValueToJSObject_UnderCorrectKey() throws JSONException { + JSObject jsObject = new JSObject(); + jsObject.put("long", 1l); + + Long expectedValue = 1l; + Long actualValue = jsObject.getLong("long"); + + assertEquals(actualValue, expectedValue); + } + + @Test + public void putDouble_AddsValueToJSObject_UnderCorrectKey() throws JSONException { + JSObject jsObject = new JSObject(); + jsObject.put("double", 1d); + + Double expectedValue = 1d; + Double actualValue = jsObject.getDouble("double"); + + assertEquals(actualValue, expectedValue); + } + + @Test + public void putString_AddsValueToJSObject_UnderCorrectKey() throws JSONException { + JSObject jsObject = new JSObject(); + jsObject.put("string", "test"); + + String expectedValue = "test"; + String actualValue = jsObject.getString("string"); + + assertEquals(actualValue, expectedValue); + } +}