diff --git a/AnkiDroid/src/androidTest/java/com/ichi2/anki/NotificationDatastoreTest.kt b/AnkiDroid/src/androidTest/java/com/ichi2/anki/NotificationDatastoreTest.kt new file mode 100644 index 000000000000..721c89559465 --- /dev/null +++ b/AnkiDroid/src/androidTest/java/com/ichi2/anki/NotificationDatastoreTest.kt @@ -0,0 +1,88 @@ +package com.ichi2.anki + +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.test.platform.app.InstrumentationRegistry +import com.ichi2.anki.model.DeckNotification +import kotlinx.coroutines.runBlocking +import org.intellij.lang.annotations.Language +import org.junit.After +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import kotlin.random.Random +import kotlin.test.assertNotNull +import kotlin.test.assertNull +import kotlin.test.assertTrue + +@RunWith(AndroidJUnit4::class) +class NotificationDatastoreTest { + private lateinit var notificationDatastore: NotificationDatastore + + @Before + fun setup() { + val context = InstrumentationRegistry.getInstrumentation().targetContext + notificationDatastore = NotificationDatastore.getInstance(context) + } + + @Test + fun deckSchedDataReadWriteTest() { + val deckNotification = DeckNotification() + + runBlocking { + val isSuccess = notificationDatastore.setDeckSchedData( + deckNotification.did, + deckNotification + ) + assertTrue( + isSuccess, + "Unable to save the deck data in notification preference datastore." + ) + + val dataStored = notificationDatastore.getDeckSchedData(deckNotification.did) + assertNotNull( + dataStored, + "Unable to read the stored deck data from notification preference datastore." + ) + } + } + + @Test + fun unStoredDeckSchedDataTest() { + runBlocking { + val deckIdUnStored = Random.nextLong() + val dataUnStored = notificationDatastore.getDeckSchedData(deckIdUnStored) + assertNull(dataUnStored, "Expected null, But found data for deckId $deckIdUnStored") + } + } + + @Test(expected = Exception::class) + fun deckSchedDataDeSerializationTest() { + val deckId = 2L + + @Language("JSON") + val invalidDeckSchedJson = """ + { + "deckPreference":{ + "number":10, + "valueType":"CARDS" + }, + "did":1, + "enabled":false, + "includeSubdecks":true, + "notificationTime": 0 + } + """.trimIndent() + + runBlocking { + notificationDatastore.putStringAsync(deckId.toString(), invalidDeckSchedJson) + notificationDatastore.getDeckSchedData(deckId) + } + } + + @After + fun cleanUp() { + runBlocking { + notificationDatastore.clearDatastore() + } + } +} diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/NotificationDatastore.kt b/AnkiDroid/src/main/java/com/ichi2/anki/NotificationDatastore.kt index 1bbc3b36f494..f886d1317482 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/NotificationDatastore.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/NotificationDatastore.kt @@ -213,11 +213,6 @@ class NotificationDatastore private constructor(val context: Context) { * */ /* * We actually are not blocking the thread. This method throws an exception. It will not create problem for us. - * TODO: unit test that : - * * if there is no preference at all, we return null - * * if there is a preference without entry for this key we return null - * * if there is a preference whose entry for this key can't be cast to DeckNotification, throw - * * if there is a preference with entry for this key that can be cast, we get expected notification */ @Suppress("BlockingMethodInNonBlockingContext") suspend fun getDeckSchedData(did: DeckId): DeckNotification? {