Skip to content

Commit

Permalink
update junit test
Browse files Browse the repository at this point in the history
  • Loading branch information
krmanik committed Oct 22, 2023
1 parent 68cba38 commit 979fbef
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 52 deletions.
3 changes: 3 additions & 0 deletions AnkiDroid/src/main/assets/scripts/js-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
168 changes: 124 additions & 44 deletions AnkiDroid/src/test/java/com/ichi2/anki/AnkiDroidJsAPITest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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", "[email protected]")
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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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\" : \"[email protected]\"};\n"
markCardJs += "var jsApi = {\"version\" : \"0.0.2\", \"developer\" : \"[email protected]\"};\n"

// init JS API
markCardJs += "AnkiDroidJS.init(JSON.stringify(jsApi));\n"
Expand All @@ -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\" : \"[email protected]\"};\n"
flagCardJs += "var jsApi = {\"version\" : \"0.0.2\", \"developer\" : \"[email protected]\"};\n"

// init JS API
flagCardJs += "AnkiDroidJS.init(JSON.stringify(jsApi));\n"
Expand Down Expand Up @@ -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\" : \"[email protected]\"};\n"
script += "var jsApi = {\"version\" : \"0.0.2\", \"developer\" : \"[email protected]\"};\n"

// init JS API
script += "AnkiDroidJS.init(JSON.stringify(jsApi));\n"
Expand Down Expand Up @@ -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
Expand All @@ -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", "[email protected]")
return data.toString()
}
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions AnkiDroid/src/test/java/com/ichi2/anki/ReviewerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions AnkiDroid/src/test/resources/test-js-addon.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 979fbef

Please sign in to comment.