-
-
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.
Implemented Instrumentation test for NotificationDatastore.kt
NotificationDatastoreTest tests all the possible edge cases which might cause problem in working of new notification system.
- Loading branch information
1 parent
abc3f30
commit 391acf2
Showing
2 changed files
with
88 additions
and
5 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
AnkiDroid/src/androidTest/java/com/ichi2/anki/NotificationDatastoreTest.kt
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 |
---|---|---|
@@ -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() | ||
} | ||
} | ||
} |
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