From 979fbefb4b29994faa9d12be066948c46fefa41a Mon Sep 17 00:00:00 2001 From: Mani <12841290+krmanik@users.noreply.github.com> Date: Sat, 21 Oct 2023 12:45:51 +0800 Subject: [PATCH] update junit test --- AnkiDroid/src/main/assets/scripts/js-api.js | 3 + .../java/com/ichi2/anki/AnkiDroidJsAPITest.kt | 168 +++++++++++++----- .../test/java/com/ichi2/anki/ReviewerTest.kt | 8 +- .../com/ichi2/anki/jsaddons/AddonModelTest.kt | 2 +- .../src/test/resources/test-js-addon.json | 4 +- .../valid-ankidroid-js-addon-test.json | 2 +- 6 files changed, 135 insertions(+), 52 deletions(-) diff --git a/AnkiDroid/src/main/assets/scripts/js-api.js b/AnkiDroid/src/main/assets/scripts/js-api.js index 099dfcdf6c03..62c96e4be522 100644 --- a/AnkiDroid/src/main/assets/scripts/js-api.js +++ b/AnkiDroid/src/main/assets/scripts/js-api.js @@ -83,6 +83,9 @@ const AnkiDroidJS = { } const responseData = await response.text(); + if (endpoint.includes("nextTime") || endpoint.includes("deckName")) { + return responseData; + } return JSON.parse(responseData); } catch (error) { console.error("Request error:", error); diff --git a/AnkiDroid/src/test/java/com/ichi2/anki/AnkiDroidJsAPITest.kt b/AnkiDroid/src/test/java/com/ichi2/anki/AnkiDroidJsAPITest.kt index d4506dc4f32b..43691d5abc03 100644 --- a/AnkiDroid/src/test/java/com/ichi2/anki/AnkiDroidJsAPITest.kt +++ b/AnkiDroid/src/test/java/com/ichi2/anki/AnkiDroidJsAPITest.kt @@ -46,16 +46,15 @@ class AnkiDroidJsAPITest : RobolectricTest() { val reviewer: Reviewer = startReviewer() val javaScriptFunction = reviewer.javaScriptFunction() - val data = JSONObject() - data.put("version", "0.0.1") - data.put("developer", "dev@mail.com") + val data = initJsApiContract() // this will be changed when new api added // TODO - make this test to auto add api from list - val expected = "{\"setCardDue\":true,\"suspendNote\":true,\"markCard\":true,\"suspendCard\":true,\"buryCard\":true,\"toggleFlag\":true,\"buryNote\":true}" + val expected = + "{\"setCardDue\":true,\"suspendNote\":true,\"markCard\":true,\"suspendCard\":true,\"buryCard\":true,\"toggleFlag\":true,\"buryNote\":true}" waitForAsyncTasksToComplete() - assertThat(javaScriptFunction.init(data.toString()), equalTo(expected)) + assertThat(javaScriptFunction.init(data.toByteArray()).decodeToString(), equalTo(expected)) } @Test @@ -75,10 +74,22 @@ class AnkiDroidJsAPITest : RobolectricTest() { waitForAsyncTasksToComplete() - assertThat(javaScriptFunction.ankiGetNextTime1().withoutUnicodeIsolation(), equalTo("<1m")) - assertThat(javaScriptFunction.ankiGetNextTime2().withoutUnicodeIsolation(), equalTo("<6m")) - assertThat(javaScriptFunction.ankiGetNextTime3().withoutUnicodeIsolation(), equalTo("<10m")) - assertThat(javaScriptFunction.ankiGetNextTime4().withoutUnicodeIsolation(), equalTo("4d")) + assertThat( + javaScriptFunction.ankiGetNextTime1().decodeToString().withoutUnicodeIsolation(), + equalTo("<1m") + ) + assertThat( + javaScriptFunction.ankiGetNextTime2().decodeToString().withoutUnicodeIsolation(), + equalTo("<6m") + ) + assertThat( + javaScriptFunction.ankiGetNextTime3().decodeToString().withoutUnicodeIsolation(), + equalTo("<10m") + ) + assertThat( + javaScriptFunction.ankiGetNextTime4().decodeToString().withoutUnicodeIsolation(), + equalTo("4d") + ) } @Test @@ -100,43 +111,88 @@ class AnkiDroidJsAPITest : RobolectricTest() { val currentCard = reviewer.currentCard!! // Card Did - assertThat(javaScriptFunction.ankiGetCardDid(), equalTo(currentCard.did)) + assertThat( + javaScriptFunction.ankiGetCardDid().decodeToString().toLong(), + equalTo(currentCard.did) + ) // Card Id - assertThat(javaScriptFunction.ankiGetCardId(), equalTo(currentCard.id)) + assertThat( + javaScriptFunction.ankiGetCardId().decodeToString().toLong(), + equalTo(currentCard.id) + ) // Card Nid - assertThat(javaScriptFunction.ankiGetCardNid(), equalTo(currentCard.nid)) + assertThat( + javaScriptFunction.ankiGetCardNid().decodeToString().toLong(), + equalTo(currentCard.nid) + ) // Card ODid - assertThat(javaScriptFunction.ankiGetCardODid(), equalTo(currentCard.oDid)) + assertThat( + javaScriptFunction.ankiGetCardODid().decodeToString().toLong(), + equalTo(currentCard.oDid) + ) // Card Type - assertThat(javaScriptFunction.ankiGetCardType(), equalTo(currentCard.type)) + assertThat( + javaScriptFunction.ankiGetCardType().decodeToString().toInt(), + equalTo(currentCard.type) + ) // Card ODue - assertThat(javaScriptFunction.ankiGetCardODue(), equalTo(currentCard.oDue)) + assertThat( + javaScriptFunction.ankiGetCardODue().decodeToString().toLong(), + equalTo(currentCard.oDue) + ) // Card Due - assertThat(javaScriptFunction.ankiGetCardDue(), equalTo(currentCard.due)) + assertThat( + javaScriptFunction.ankiGetCardDue().decodeToString().toLong(), + equalTo(currentCard.due) + ) // Card Factor - assertThat(javaScriptFunction.ankiGetCardFactor(), equalTo(currentCard.factor)) + assertThat( + javaScriptFunction.ankiGetCardFactor().decodeToString().toInt(), + equalTo(currentCard.factor) + ) // Card Lapses - assertThat(javaScriptFunction.ankiGetCardLapses(), equalTo(currentCard.lapses)) + assertThat( + javaScriptFunction.ankiGetCardLapses().decodeToString().toInt(), + equalTo(currentCard.lapses) + ) // Card Ivl - assertThat(javaScriptFunction.ankiGetCardInterval(), equalTo(currentCard.ivl)) + assertThat( + javaScriptFunction.ankiGetCardInterval().decodeToString().toInt(), + equalTo(currentCard.ivl) + ) // Card mod - assertThat(javaScriptFunction.ankiGetCardMod(), equalTo(currentCard.mod)) + assertThat( + javaScriptFunction.ankiGetCardMod().decodeToString().toLong(), + equalTo(currentCard.mod) + ) // Card Queue - assertThat(javaScriptFunction.ankiGetCardQueue(), equalTo(currentCard.queue)) + assertThat( + javaScriptFunction.ankiGetCardQueue().decodeToString().toInt(), + equalTo(currentCard.queue) + ) // Card Reps - assertThat(javaScriptFunction.ankiGetCardReps(), equalTo(currentCard.reps)) + assertThat( + javaScriptFunction.ankiGetCardReps().decodeToString().toInt(), + equalTo(currentCard.reps) + ) // Card left - assertThat(javaScriptFunction.ankiGetCardLeft(), equalTo(currentCard.left)) + assertThat( + javaScriptFunction.ankiGetCardLeft().decodeToString().toInt(), + equalTo(currentCard.left) + ) // Card Flag - assertThat(javaScriptFunction.ankiGetCardFlag(), equalTo(0)) + assertThat(javaScriptFunction.ankiGetCardFlag().decodeToString().toInt(), equalTo(0)) reviewer.currentCard!!.setFlag(1) - assertThat(javaScriptFunction.ankiGetCardFlag(), equalTo(1)) + assertThat(javaScriptFunction.ankiGetCardFlag().decodeToString().toInt(), equalTo(1)) // Card Mark - assertThat(javaScriptFunction.ankiGetCardMark(), equalTo(false)) + assertThat( + javaScriptFunction.ankiGetCardMark().decodeToString().toBoolean(), + equalTo(false) + ) reviewer.currentCard!!.note().addTag("marked") - assertThat(javaScriptFunction.ankiGetCardMark(), equalTo(true)) + assertThat(javaScriptFunction.ankiGetCardMark().decodeToString().toBoolean(), equalTo(true)) } @Test @@ -155,16 +211,31 @@ class AnkiDroidJsAPITest : RobolectricTest() { waitForAsyncTasksToComplete() // Displaying question - assertThat(javaScriptFunction.ankiIsDisplayingAnswer(), equalTo(reviewer.isDisplayingAnswer)) + assertThat( + javaScriptFunction.ankiIsDisplayingAnswer().decodeToString().toBoolean(), + equalTo(reviewer.isDisplayingAnswer) + ) reviewer.displayCardAnswer() - assertThat(javaScriptFunction.ankiIsDisplayingAnswer(), equalTo(reviewer.isDisplayingAnswer)) + assertThat( + javaScriptFunction.ankiIsDisplayingAnswer().decodeToString().toBoolean(), + equalTo(reviewer.isDisplayingAnswer) + ) // Full Screen - assertThat(javaScriptFunction.ankiIsInFullscreen(), equalTo(reviewer.isFullscreen)) + assertThat( + javaScriptFunction.ankiIsInFullscreen().decodeToString().toBoolean(), + equalTo(reviewer.isFullscreen) + ) // Top bar - assertThat(javaScriptFunction.ankiIsTopbarShown(), equalTo(reviewer.prefShowTopbar)) + assertThat( + javaScriptFunction.ankiIsTopbarShown().decodeToString().toBoolean(), + equalTo(reviewer.prefShowTopbar) + ) // Night Mode - assertThat(javaScriptFunction.ankiIsInNightMode(), equalTo(reviewer.isInNightMode)) + assertThat( + javaScriptFunction.ankiIsInNightMode().decodeToString().toBoolean(), + equalTo(reviewer.isInNightMode) + ) } @Test @@ -187,13 +258,16 @@ class AnkiDroidJsAPITest : RobolectricTest() { // Card mark test // --------------- // Before marking card - assertThat(javaScriptFunction.ankiGetCardMark(), equalTo(false)) + assertThat( + javaScriptFunction.ankiGetCardMark().decodeToString().toBoolean(), + equalTo(false) + ) // call javascript function defined in card.js to mark card var markCardJs = "javascript:(function () {\n" // add js api developer contract - markCardJs += "var jsApi = {\"version\" : \"0.0.1\", \"developer\" : \"dev@mail.com\"};\n" + markCardJs += "var jsApi = {\"version\" : \"0.0.2\", \"developer\" : \"dev@mail.com\"};\n" // init JS API markCardJs += "AnkiDroidJS.init(JSON.stringify(jsApi));\n" @@ -211,13 +285,13 @@ class AnkiDroidJsAPITest : RobolectricTest() { // Card flag test // --------------- // before toggling flag - assertThat(javaScriptFunction.ankiGetCardFlag(), equalTo(0)) + assertThat(javaScriptFunction.ankiGetCardFlag().decodeToString().toInt(), equalTo(0)) // call javascript function defined in card.js to toggle flag var flagCardJs = "javascript:(function () {\n" // add js api developer contract - flagCardJs += "var jsApi = {\"version\" : \"0.0.1\", \"developer\" : \"test@example.com\"};\n" + flagCardJs += "var jsApi = {\"version\" : \"0.0.2\", \"developer\" : \"test@example.com\"};\n" // init JS API flagCardJs += "AnkiDroidJS.init(JSON.stringify(jsApi));\n" @@ -300,7 +374,7 @@ class AnkiDroidJsAPITest : RobolectricTest() { var script = "javascript:(function () {\n" // add js api developer contract - script += "var jsApi = {\"version\" : \"0.0.1\", \"developer\" : \"test@example.com\"};\n" + script += "var jsApi = {\"version\" : \"0.0.2\", \"developer\" : \"test@example.com\"};\n" // init JS API script += "AnkiDroidJS.init(JSON.stringify(jsApi));\n" @@ -332,12 +406,15 @@ class AnkiDroidJsAPITest : RobolectricTest() { val javaScriptFunction = reviewer.javaScriptFunction() // init js api - javaScriptFunction.init(initJsApiContract()) + javaScriptFunction.init(initJsApiContract().toByteArray()) // get card id for testing due - val cardId = javaScriptFunction.ankiGetCardId() + val cardId = javaScriptFunction.ankiGetCardId().decodeToString().toLong() // test that card rescheduled for 15 days interval and returned true - assertTrue("Card rescheduled, so returns true", javaScriptFunction.ankiSetCardDue(15)) + assertTrue( + "Card rescheduled, so returns true", + javaScriptFunction.ankiSetCardDue("15".toByteArray()).decodeToString().toBoolean() + ) waitForAsyncTasksToComplete() // verify that it did get rescheduled @@ -348,7 +425,7 @@ class AnkiDroidJsAPITest : RobolectricTest() { private fun initJsApiContract(): String { val data = JSONObject() - data.put("version", "0.0.1") + data.put("version", "0.0.2") data.put("developer", "test@example.com") return data.toString() } @@ -375,12 +452,15 @@ class AnkiDroidJsAPITest : RobolectricTest() { val javaScriptFunction = reviewer.javaScriptFunction() // init js api - javaScriptFunction.init(initJsApiContract()) + javaScriptFunction.init(initJsApiContract().toByteArray()) // get card id for testing due - val cardId = javaScriptFunction.ankiGetCardId() + val cardId = javaScriptFunction.ankiGetCardId().decodeToString().toLong() // test that card reset - assertTrue("Card progress reset", javaScriptFunction.ankiResetProgress()) + assertTrue( + "Card progress reset", + javaScriptFunction.ankiResetProgress().decodeToString().toBoolean() + ) waitForAsyncTasksToComplete() // verify that card progress reset diff --git a/AnkiDroid/src/test/java/com/ichi2/anki/ReviewerTest.kt b/AnkiDroid/src/test/java/com/ichi2/anki/ReviewerTest.kt index 46dabcf95eee..0eca7335c750 100644 --- a/AnkiDroid/src/test/java/com/ichi2/anki/ReviewerTest.kt +++ b/AnkiDroid/src/test/java/com/ichi2/anki/ReviewerTest.kt @@ -215,7 +215,7 @@ class ReviewerTest : RobolectricTest() { val javaScriptFunction = reviewer.javaScriptFunction() waitForAsyncTasksToComplete() - assertThat(javaScriptFunction.ankiGetDeckName(), equalTo("B")) + assertThat(javaScriptFunction.ankiGetDeckName().decodeToString(), equalTo("B")) } @Ignore("needs update for v3") @@ -283,9 +283,9 @@ class ReviewerTest : RobolectricTest() { private fun assertCounts(r: Reviewer, newCount: Int, stepCount: Int, revCount: Int) { val jsApi = r.javaScriptFunction() val countList = listOf( - jsApi.ankiGetNewCardCount(), - jsApi.ankiGetLrnCardCount(), - jsApi.ankiGetRevCardCount() + jsApi.ankiGetNewCardCount().decodeToString().toInt(), + jsApi.ankiGetLrnCardCount().decodeToString().toInt(), + jsApi.ankiGetRevCardCount().decodeToString().toInt() ) val expected = listOf( diff --git a/AnkiDroid/src/test/java/com/ichi2/anki/jsaddons/AddonModelTest.kt b/AnkiDroid/src/test/java/com/ichi2/anki/jsaddons/AddonModelTest.kt index 660c567b6ab1..744e2fbb324e 100644 --- a/AnkiDroid/src/test/java/com/ichi2/anki/jsaddons/AddonModelTest.kt +++ b/AnkiDroid/src/test/java/com/ichi2/anki/jsaddons/AddonModelTest.kt @@ -68,7 +68,7 @@ class AddonModelTest : RobolectricTest() { assertEquals(addon.name, "valid-ankidroid-js-addon-test") assertEquals(addon.addonTitle, "Valid AnkiDroid JS Addon") assertEquals(addon.version, "1.0.0") - assertEquals(addon.ankidroidJsApi, "0.0.1") + assertEquals(addon.ankidroidJsApi, "0.0.2") assertEquals(addon.addonType, "reviewer") assertEquals(addon.icon, "") // reviewer icon is empty diff --git a/AnkiDroid/src/test/resources/test-js-addon.json b/AnkiDroid/src/test/resources/test-js-addon.json index 271d3f56e10f..8aaa6376c777 100644 --- a/AnkiDroid/src/test/resources/test-js-addon.json +++ b/AnkiDroid/src/test/resources/test-js-addon.json @@ -5,7 +5,7 @@ "version": "1.1.1", "description": "Show progress bar in AnkiDroid, this package may not be used in node_modules. For using this addon view. https://github.com/ankidroid/Anki-Android/pull/9232", "main": "index.js", - "ankidroidJsApi": "0.0.1", + "ankidroidJsApi": "0.0.2", "addonType": "reviewer", "keywords": [ "ankidroid-js-addon" @@ -31,7 +31,7 @@ "version": "1.0.1", "description": "This addon will listed in Addon Browser. Also AddonInfo.isValidAnkiDroidAddon return true for this package. For more view. https://github.com/ankidroid/Anki-Android/pull/9232", "main": "index.js", - "ankidroidJsApi": "0.0.1", + "ankidroidJsApi": "0.0.2", "addonType": "reviewer", "keywords": [ "ankidroid-js-addon" diff --git a/AnkiDroid/src/test/resources/valid-ankidroid-js-addon-test.json b/AnkiDroid/src/test/resources/valid-ankidroid-js-addon-test.json index 91505cc75e8b..b1b315d8dceb 100644 --- a/AnkiDroid/src/test/resources/valid-ankidroid-js-addon-test.json +++ b/AnkiDroid/src/test/resources/valid-ankidroid-js-addon-test.json @@ -2,7 +2,7 @@ "name": "valid-ankidroid-js-addon-test", "addonTitle": "Valid AnkiDroid JS Addon", "version": "1.0.0", - "ankidroidJsApi": "0.0.1", + "ankidroidJsApi": "0.0.2", "addonType": "reviewer", "description": "This addon will listed in Addon Browser. Also AddonInfo.isValidAnkiDroidAddon return true for this package. For more view. https://github.com/ankidroid/Anki-Android/pull/9232", "main": "index.js",