forked from ankidroid/Anki-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnkiDroidJsAPITest.kt
More file actions
497 lines (432 loc) · 17.6 KB
/
AnkiDroidJsAPITest.kt
File metadata and controls
497 lines (432 loc) · 17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/****************************************************************************************
* Copyright (c) 2020 Mani infinyte01@gmail.com *
* *
* *
* This program is free software; you can redistribute it and/or modify it under *
* the terms of the GNU General Public License as published by the Free Software *
* Foundation; either version 3 of the License, or (at your option) any later *
* version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT ANY *
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
* PARTICULAR PURPOSE. See the GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License along with *
* this program. If not, see <http://www.gnu.org/licenses/>. *
* *
* *************************************************************************************/
package com.ichi2.anki
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.ichi2.anki.AnkiDroidJsAPI.Companion.SUCCESS_KEY
import com.ichi2.anki.AnkiDroidJsAPI.Companion.VALUE_KEY
import com.ichi2.anki.utils.ext.setFlag
import com.ichi2.libanki.CardType
import com.ichi2.libanki.utils.TimeManager
import com.ichi2.utils.BASIC_MODEL_NAME
import net.ankiweb.rsdroid.withoutUnicodeIsolation
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
import org.json.JSONArray
import org.json.JSONObject
import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith
import kotlin.test.assertEquals
@RunWith(AndroidJUnit4::class)
class AnkiDroidJsAPITest : RobolectricTest() {
@Test
fun ankiGetNextTimeTest() =
runTest {
val models = col.notetypes
val didA = addDeck("Test", setAsSelected = true)
val basic = models.byName(BASIC_MODEL_NAME)
basic!!.put("did", didA)
addBasicNote("foo", "bar")
val reviewer: Reviewer = startReviewer()
val jsapi = reviewer.jsApi
reviewer.displayCardAnswer()
waitForAsyncTasksToComplete()
assertThat(
getDataFromRequest("nextTime1", jsapi).withoutUnicodeIsolation(),
equalTo(formatApiResult("<1m")),
)
assertThat(
getDataFromRequest("nextTime2", jsapi).withoutUnicodeIsolation(),
equalTo(formatApiResult("<6m")),
)
assertThat(
getDataFromRequest("nextTime3", jsapi).withoutUnicodeIsolation(),
equalTo(formatApiResult("<10m")),
)
assertThat(
getDataFromRequest("nextTime4", jsapi).withoutUnicodeIsolation(),
equalTo(formatApiResult("4d")),
)
}
@Test
fun ankiTestCurrentCard() =
runTest {
val models = col.notetypes
val didA = addDeck("Test", setAsSelected = true)
val basic = models.byName(BASIC_MODEL_NAME)
basic!!.put("did", didA)
addBasicNote("foo", "bar")
val reviewer: Reviewer = startReviewer()
val jsapi = reviewer.jsApi
reviewer.displayCardAnswer()
waitForAsyncTasksToComplete()
val currentCard = reviewer.currentCard!!
// Card Did
assertThat(
getDataFromRequest("cardDid", jsapi),
equalTo(formatApiResult(currentCard.did)),
)
// Card Id
assertThat(
getDataFromRequest("cardId", jsapi),
equalTo(formatApiResult(currentCard.id)),
)
// Card Nid
assertThat(
getDataFromRequest("cardNid", jsapi),
equalTo(formatApiResult(currentCard.nid)),
)
// Card ODid
assertThat(
getDataFromRequest("cardODid", jsapi),
equalTo(formatApiResult(currentCard.oDid)),
)
// Card Type
assertThat(
getDataFromRequest("cardType", jsapi),
equalTo(formatApiResult(currentCard.type.code)),
)
// Card ODue
assertThat(
getDataFromRequest("cardODue", jsapi),
equalTo(formatApiResult(currentCard.oDue)),
)
// Card Due
assertThat(
getDataFromRequest("cardDue", jsapi),
equalTo(formatApiResult(currentCard.due)),
)
// Card Factor
assertThat(
getDataFromRequest("cardFactor", jsapi),
equalTo(formatApiResult(currentCard.factor)),
)
// Card Lapses
assertThat(
getDataFromRequest("cardLapses", jsapi),
equalTo(formatApiResult(currentCard.lapses)),
)
// Card Ivl
assertThat(
getDataFromRequest("cardInterval", jsapi),
equalTo(formatApiResult(currentCard.ivl)),
)
// Card mod
assertThat(
getDataFromRequest("cardMod", jsapi),
equalTo(formatApiResult(currentCard.mod)),
)
// Card Queue
assertThat(
getDataFromRequest("cardQueue", jsapi),
equalTo(formatApiResult(currentCard.queue.code)),
)
// Card Reps
assertThat(
getDataFromRequest("cardReps", jsapi),
equalTo(formatApiResult(currentCard.reps)),
)
// Card left
assertThat(
getDataFromRequest("cardLeft", jsapi),
equalTo(formatApiResult(currentCard.left)),
)
// Card Flag
assertThat(
getDataFromRequest("cardFlag", jsapi),
equalTo(formatApiResult(0)),
)
reviewer.currentCard!!.setFlag(Flag.RED.ordinal)
assertThat(
getDataFromRequest("cardFlag", jsapi),
equalTo(formatApiResult(1)),
)
// Card Mark
assertThat(
getDataFromRequest("cardMark", jsapi),
equalTo(formatApiResult(false)),
)
reviewer.currentCard!!.note().addTag("marked")
assertThat(
getDataFromRequest("cardMark", jsapi),
equalTo(formatApiResult(true)),
)
}
@Test
fun ankiJsUiTest() =
runTest {
val models = col.notetypes
val didA = addDeck("Test", setAsSelected = true)
val basic = models.byName(BASIC_MODEL_NAME)
basic!!.put("did", didA)
addBasicNote("foo", "bar")
val reviewer: Reviewer = startReviewer()
val jsapi = reviewer.jsApi
waitForAsyncTasksToComplete()
// Displaying question
assertThat(
getDataFromRequest("isDisplayingAnswer", jsapi),
equalTo(formatApiResult(reviewer.isDisplayingAnswer)),
)
reviewer.displayCardAnswer()
assertThat(
getDataFromRequest("isDisplayingAnswer", jsapi),
equalTo(formatApiResult(reviewer.isDisplayingAnswer)),
)
// Full Screen
assertThat(
getDataFromRequest("isInFullscreen", jsapi),
equalTo(formatApiResult(reviewer.isFullscreen)),
)
// Top bar
assertThat(
getDataFromRequest("isTopbarShown", jsapi),
equalTo(formatApiResult(reviewer.prefShowTopbar)),
)
// Night Mode
assertThat(
getDataFromRequest("isInNightMode", jsapi),
equalTo(formatApiResult(reviewer.isInNightMode)),
)
}
@Test
fun ankiMarkAndFlagCardTest() =
runTest {
// js api test for marking and flagging card
val models = col.notetypes
val didA = addDeck("Test", setAsSelected = true)
val basic = models.byName(BASIC_MODEL_NAME)
basic!!.put("did", didA)
addBasicNote("foo", "bar")
val reviewer: Reviewer = startReviewer()
val jsapi = reviewer.jsApi
waitForAsyncTasksToComplete()
// ---------------
// Card mark test
// ---------------
// Before marking card
assertThat(
getDataFromRequest("cardMark", jsapi),
equalTo(formatApiResult(false)),
)
// Mark card
assertThat(
getDataFromRequest("markCard", jsapi, "true"),
equalTo(formatApiResult(true)),
)
// After marking card
assertThat(
getDataFromRequest("cardMark", jsapi),
equalTo(formatApiResult(true)),
)
// ---------------
// Card flag test
// ---------------
// before toggling flag
assertThat(
getDataFromRequest("cardFlag", jsapi),
equalTo(formatApiResult(0)),
)
// call javascript function to toggle flag
assertThat(
getDataFromRequest("toggleFlag", jsapi, "red"),
equalTo(formatApiResult(true)),
)
// after toggling flag
assertThat(
getDataFromRequest("cardFlag", jsapi),
equalTo(formatApiResult(1)),
)
}
@Ignore("the test need to be updated")
fun ankiBurySuspendTest() =
runTest {
// js api test for bury and suspend notes and cards
// add five notes, four will be buried and suspended
// count number of notes, if buried or suspended then
// in scheduling the count will be less than previous scheduling
val models = col.notetypes
val didA = addDeck("Test", setAsSelected = true)
val basic = models.byName(BASIC_MODEL_NAME)
basic!!.put("did", didA)
addBasicNote("foo", "bar")
addBasicNote("baz", "bak")
addBasicNote("Anki", "Droid")
addBasicNote("Test Card", "Bury and Suspend Card")
addBasicNote("Test Note", "Bury and Suspend Note")
val reviewer: Reviewer = startReviewer()
val jsapi = reviewer.jsApi
// ----------
// Bury Card
// ----------
// call script to bury current card
assertThat(
getDataFromRequest("buryCard", jsapi),
equalTo(formatApiResult(true)),
)
// count number of notes
val sched = reviewer.getColUnsafe
assertThat(sched.cardCount(), equalTo(4))
// ----------
// Bury Note
// ----------
// call script to bury current note
assertThat(
getDataFromRequest("buryNote", jsapi),
equalTo(formatApiResult(true)),
)
// count number of notes
assertThat(sched.cardCount(), equalTo(3))
// -------------
// Suspend Card
// -------------
// call script to suspend current card
assertThat(
getDataFromRequest("suspendCard", jsapi),
equalTo(formatApiResult(true)),
)
// count number of notes
assertThat(sched.cardCount(), equalTo(2))
// -------------
// Suspend Note
// -------------
// call script to suspend current note
assertThat(
getDataFromRequest("suspendNote", jsapi),
equalTo(formatApiResult(true)),
)
// count number of notes
assertThat(sched.cardCount(), equalTo(1))
}
private fun startReviewer(): Reviewer = ReviewerTest.startReviewer(this)
@Test
fun ankiSetCardDueTest() =
runTest {
TimeManager.reset()
val models = col.notetypes
val didA = addDeck("Test", setAsSelected = true)
val basic = models.byName(BASIC_MODEL_NAME)
basic!!.put("did", didA)
addBasicNote("foo", "bar")
addBasicNote("baz", "bak")
val reviewer: Reviewer = startReviewer()
waitForAsyncTasksToComplete()
val jsapi = reviewer.jsApi
// get card id for testing due
val cardIdRes = getDataFromRequest("cardId", jsapi)
val jsonObject = JSONObject(cardIdRes)
val cardId = jsonObject.get(VALUE_KEY).toString().toLong()
// test that card rescheduled for 15 days interval and returned true
assertThat(getDataFromRequest("setCardDue", jsapi, "15"), equalTo(formatApiResult(true)))
waitForAsyncTasksToComplete()
// verify that it did get rescheduled
// --------------------------------
val cardToBeReschedule = col.getCard(cardId)
assertEquals(15 + col.sched.today, cardToBeReschedule.due, "Card is rescheduled")
}
@Test
fun ankiResetProgressTest() =
runTest {
val n = addBasicNote("Front", "Back")
val c = n.firstCard()
// Make card review with 28L due and 280% ease
c.type = CardType.Rev
c.due = 28
c.factor = 2800
c.ivl = 8
// before reset
assertEquals(28, c.due, "Card due before reset")
assertEquals(8, c.ivl, "Card interval before reset")
assertEquals(2800, c.factor, "Card ease before reset")
assertEquals(CardType.Rev, c.type, "Card type before reset")
val reviewer: Reviewer = startReviewer()
waitForAsyncTasksToComplete()
val jsapi = reviewer.jsApi
// test that card reset
assertThat(getDataFromRequest("resetProgress", jsapi), equalTo(formatApiResult(true)))
waitForAsyncTasksToComplete()
// verify that card progress reset
// --------------------------------
val cardAfterReset = col.getCard(reviewer.currentCard!!.id)
assertEquals(2, cardAfterReset.due, "Card due after reset")
assertEquals(0, cardAfterReset.ivl, "Card interval after reset")
assertEquals(CardType.New, cardAfterReset.type, "Card type after reset")
}
@Test
fun ankiGetNoteTagsTest() =
runTest {
val n =
addBasicNote("Front", "Back").update {
tags = mutableListOf("tag1", "tag2", "tag3")
}
val reviewer: Reviewer = startReviewer()
waitForAsyncTasksToComplete()
val jsapi = reviewer.jsApi
// test get tags for note
val expectedTags = n.tags
val response = getDataFromRequest("getNoteTags", jsapi, jsonObjectOf("noteId" to n.id))
val jsonResponse = JSONObject(response)
val actualTags = JSONArray(jsonResponse.getString("value"))
assertEquals(expectedTags.size, actualTags.length())
for (i in 0 until actualTags.length()) {
assertEquals(expectedTags[i], actualTags.getString(i))
}
}
companion object {
fun jsApiContract(data: String = ""): ByteArray =
JSONObject()
.apply {
put("version", "0.0.3")
put("developer", "test@example.com")
put("data", data)
}.toString()
.toByteArray()
private inline fun formatSuccessfulApiResult(block: JSONObject.() -> Unit) =
JSONObject()
.apply {
put(SUCCESS_KEY, true)
block(this)
}.toString()
fun formatApiResult(res: Boolean) = formatSuccessfulApiResult { put(VALUE_KEY, res) }
fun formatApiResult(res: Int) = formatSuccessfulApiResult { put(VALUE_KEY, res) }
fun formatApiResult(res: Long) = formatSuccessfulApiResult { put(VALUE_KEY, res) }
fun formatApiResult(res: String) = formatSuccessfulApiResult { put(VALUE_KEY, res) }
suspend fun getDataFromRequest(
methodName: String,
jsAPI: AnkiDroidJsAPI,
apiData: String = "",
): String =
jsAPI
.handleJsApiRequest(methodName, jsApiContract(apiData), false)
.decodeToString()
suspend fun getDataFromRequest(
methodName: String,
jsAPI: AnkiDroidJsAPI,
apiData: JSONObject,
): String =
jsAPI
.handleJsApiRequest(methodName, jsApiContract(apiData.toString()), false)
.decodeToString()
}
}
private fun jsonObjectOf(vararg pairs: Pair<String, Any>): JSONObject =
JSONObject().apply {
for ((key, value) in pairs) {
put(key, value)
}
}