-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
135 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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\" : \"[email protected]\"};\n" | ||
markCardJs += "var jsApi = {\"version\" : \"0.0.2\", \"developer\" : \"[email protected]\"};\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\" : \"[email protected]\"};\n" | ||
flagCardJs += "var jsApi = {\"version\" : \"0.0.2\", \"developer\" : \"[email protected]\"};\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\" : \"[email protected]\"};\n" | ||
script += "var jsApi = {\"version\" : \"0.0.2\", \"developer\" : \"[email protected]\"};\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", "[email protected]") | ||
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters