-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from theozgurr/cancel_scheduled_work_impl
Cancel scheduled work impl
- Loading branch information
Showing
21 changed files
with
541 additions
and
129 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
124 changes: 124 additions & 0 deletions
124
...manlib/src/androidTest/java/com/notificationman/library/datastore/AppDataStoreImplTest.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,124 @@ | ||
package com.notificationman.library.datastore | ||
|
||
import android.content.Context | ||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.PreferenceDataStoreFactory | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.core.edit | ||
import androidx.datastore.preferences.preferencesDataStoreFile | ||
import androidx.test.core.app.ApplicationProvider | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.notificationman.library.extensions.getRandomUUID | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.cancel | ||
import kotlinx.coroutines.test.* | ||
import org.hamcrest.MatcherAssert.assertThat | ||
import org.hamcrest.core.Is.`is` | ||
import org.junit.After | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@ExperimentalCoroutinesApi | ||
@RunWith(AndroidJUnit4::class) | ||
class AppDataStoreImplTest { | ||
|
||
companion object { | ||
private const val NOTIFICATION_MAN_FAKE_PREFERENCES = "notification_man_fake_preferences" | ||
} | ||
|
||
private val testContext: Context = ApplicationProvider.getApplicationContext() | ||
private val testCoroutineDispatcher: TestCoroutineDispatcher = TestCoroutineDispatcher() | ||
private val testCoroutineScope = TestCoroutineScope(testCoroutineDispatcher + Job()) | ||
private val testDataStore: DataStore<Preferences> = | ||
PreferenceDataStoreFactory.create( | ||
scope = testCoroutineScope, | ||
produceFile = { testContext.preferencesDataStoreFile(NOTIFICATION_MAN_FAKE_PREFERENCES) } | ||
) | ||
private lateinit var appDataStoreImpl: AppDataStoreImpl | ||
|
||
@Before | ||
fun setUp() { | ||
Dispatchers.setMain(testCoroutineDispatcher) | ||
appDataStoreImpl = AppDataStoreImpl(testDataStore) | ||
} | ||
|
||
@Test | ||
fun test_appDataStoreImpl_saveWorkerId() { | ||
testCoroutineScope.runBlockingTest { | ||
with(appDataStoreImpl) { | ||
val id = getRandomUUID() | ||
saveWorkerId(id) | ||
val actual = getFirstWorkerId() | ||
assertThat(actual, `is`(id)) | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun test_appDataStoreImpl_deleteWorkerId() { | ||
testCoroutineScope.runBlockingTest { | ||
with(appDataStoreImpl) { | ||
val id1 = getRandomUUID() | ||
val id2 = getRandomUUID() | ||
val id3 = getRandomUUID() | ||
saveWorkerId(id1) | ||
saveWorkerId(id2) | ||
saveWorkerId(id3) | ||
deleteWorkerId(id2) | ||
val actual = getFirstWorkerId() | ||
assertThat(actual, `is`(id1)) | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun test_appDataStoreImpl_deleteAllWorkerIds() { | ||
testCoroutineScope.runBlockingTest { | ||
with(appDataStoreImpl) { | ||
val id = getRandomUUID() | ||
saveWorkerId(id) | ||
deleteAllWorkerIds() | ||
val actual = getWorkerIds() | ||
assertThat(actual, `is`(emptyList())) | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun test_appDataStoreImpl_getWorkerIds() { | ||
testCoroutineScope.runBlockingTest { | ||
with(appDataStoreImpl) { | ||
val id = getRandomUUID() | ||
saveWorkerId(id) | ||
val actual = getWorkerIds().size | ||
assertThat(actual, `is`(1)) | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun test_appDataStoreImpl_getFirstWorkerId() { | ||
testCoroutineScope.runBlockingTest { | ||
with(appDataStoreImpl) { | ||
val id = getRandomUUID() | ||
saveWorkerId(id) | ||
val actual = getFirstWorkerId() | ||
assertThat(actual, `is`(id)) | ||
} | ||
} | ||
} | ||
|
||
|
||
@After | ||
fun cleanUp() { | ||
Dispatchers.resetMain() | ||
testCoroutineDispatcher.cleanupTestCoroutines() | ||
testCoroutineScope.runBlockingTest { | ||
testDataStore.edit { it.clear() } | ||
} | ||
testCoroutineScope.cancel() | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
notificationmanlib/src/androidTest/java/com/notificationman/library/extensions/UUIDExt.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,5 @@ | ||
package com.notificationman.library.extensions | ||
|
||
import java.util.* | ||
|
||
fun getRandomUUID(): UUID = UUID.randomUUID() |
Oops, something went wrong.