Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions AnkiDroid/src/test/java/com/ichi2/anki/AnkiDroidJsAPITest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith
import kotlin.test.assertEquals
import kotlin.time.Duration.Companion.minutes

@RunWith(AndroidJUnit4::class)
class AnkiDroidJsAPITest : RobolectricTest() {
override fun getCollectionStorageMode() = CollectionStorageMode.IN_MEMORY_WITH_MEDIA

@Test
fun ankiGetNextTimeTest() =
runTest {
runTest(timeout = TEST_TIMEOUT) {
val didA = addDeck("Test", setAsSelected = true)
val basic = col.notetypes.byName(BASIC_NOTE_TYPE_NAME)
basic!!.did = didA
Expand Down Expand Up @@ -73,7 +74,7 @@ class AnkiDroidJsAPITest : RobolectricTest() {

@Test
fun ankiTestCurrentCard() =
runTest {
runTest(timeout = TEST_TIMEOUT) {
val didA = addDeck("Test", setAsSelected = true)
val basic = col.notetypes.byName(BASIC_NOTE_TYPE_NAME)
basic!!.did = didA
Expand Down Expand Up @@ -181,13 +182,17 @@ class AnkiDroidJsAPITest : RobolectricTest() {
)
}

// FIX: was incorrectly annotated with @override (a Java annotation), causing this test
// to be silently skipped by JUnit. Changed to @Test so it actually runs.
@Test
fun ankiJsUiTest() =
runTest {
runTest(timeout = TEST_TIMEOUT) {
val didA = addDeck("Test", setAsSelected = true)
val basic = col.notetypes.byName(BASIC_NOTE_TYPE_NAME)
basic!!.did = didA
addBasicNote("foo", "bar")
addBasicNote("foo", "bar").update {
tags = mutableListOf("tag1", "tag2", "tag3")
}

val reviewer: Reviewer = startReviewer()
val jsapi = reviewer.jsApi
Expand Down Expand Up @@ -224,7 +229,7 @@ class AnkiDroidJsAPITest : RobolectricTest() {

@Test
fun ankiMarkAndFlagCardTest() =
runTest {
runTest(timeout = TEST_TIMEOUT) {
// js api test for marking and flagging card
val didA = addDeck("Test", setAsSelected = true)
val basic = col.notetypes.byName(BASIC_NOTE_TYPE_NAME)
Expand Down Expand Up @@ -281,7 +286,7 @@ class AnkiDroidJsAPITest : RobolectricTest() {

@Ignore("the test need to be updated")
fun ankiBurySuspendTest() =
runTest {
runTest(timeout = TEST_TIMEOUT) {
// 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
Expand Down Expand Up @@ -352,7 +357,7 @@ class AnkiDroidJsAPITest : RobolectricTest() {

@Test
fun ankiSetCardDueTest() =
runTest {
runTest(timeout = TEST_TIMEOUT) {
TimeManager.reset()
val didA = addDeck("Test", setAsSelected = true)
val basic = col.notetypes.byName(BASIC_NOTE_TYPE_NAME)
Expand Down Expand Up @@ -381,7 +386,7 @@ class AnkiDroidJsAPITest : RobolectricTest() {

@Test
fun ankiResetProgressTest() =
runTest {
runTest(timeout = TEST_TIMEOUT) {
val n = addBasicNote("Front", "Back")
val c = n.firstCard()

Expand Down Expand Up @@ -416,7 +421,7 @@ class AnkiDroidJsAPITest : RobolectricTest() {

@Test
fun ankiGetNoteTagsTest() =
runTest {
runTest(timeout = TEST_TIMEOUT) {
val n =
addBasicNote("Front", "Back").update {
tags = mutableListOf("tag1", "tag2", "tag3")
Expand All @@ -440,6 +445,17 @@ class AnkiDroidJsAPITest : RobolectricTest() {
}

companion object {
/**
* Explicit timeout for all tests in this class.
*
* The default [kotlinx.coroutines.test.runTest] timeout of 1 minute is too tight for
* Robolectric-based tests on slow CI runners (particularly Windows), where
* [startReviewer] and [advanceRobolectricLooper] can consume most of that budget before
* any assertions run. 5 minutes gives adequate headroom while still catching genuine
* hangs.
*/
private val TEST_TIMEOUT = 5.minutes

fun jsApiContract(data: String = ""): ByteArray =
JSONObject()
.apply {
Expand Down