|
|
|
@ -18,6 +18,8 @@ package com.google.samples.apps.nowinandroid.core.datastore
|
|
|
|
|
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.datastore.test.testUserPreferencesDataStore
|
|
|
|
|
import kotlinx.coroutines.flow.first
|
|
|
|
|
import kotlinx.coroutines.test.TestScope
|
|
|
|
|
import kotlinx.coroutines.test.UnconfinedTestDispatcher
|
|
|
|
|
import kotlinx.coroutines.test.runTest
|
|
|
|
|
import org.junit.Before
|
|
|
|
|
import org.junit.Rule
|
|
|
|
@ -27,6 +29,9 @@ import kotlin.test.assertFalse
|
|
|
|
|
import kotlin.test.assertTrue
|
|
|
|
|
|
|
|
|
|
class NiaPreferencesDataSourceTest {
|
|
|
|
|
|
|
|
|
|
private val testScope = TestScope(UnconfinedTestDispatcher())
|
|
|
|
|
|
|
|
|
|
private lateinit var subject: NiaPreferencesDataSource
|
|
|
|
|
|
|
|
|
|
@get:Rule
|
|
|
|
@ -35,54 +40,56 @@ class NiaPreferencesDataSourceTest {
|
|
|
|
|
@Before
|
|
|
|
|
fun setup() {
|
|
|
|
|
subject = NiaPreferencesDataSource(
|
|
|
|
|
tmpFolder.testUserPreferencesDataStore(),
|
|
|
|
|
tmpFolder.testUserPreferencesDataStore(testScope),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
fun shouldHideOnboardingIsFalseByDefault() = runTest {
|
|
|
|
|
fun shouldHideOnboardingIsFalseByDefault() = testScope.runTest {
|
|
|
|
|
assertFalse(subject.userData.first().shouldHideOnboarding)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
fun userShouldHideOnboardingIsTrueWhenSet() = runTest {
|
|
|
|
|
fun userShouldHideOnboardingIsTrueWhenSet() = testScope.runTest {
|
|
|
|
|
subject.setShouldHideOnboarding(true)
|
|
|
|
|
assertTrue(subject.userData.first().shouldHideOnboarding)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
fun userShouldHideOnboarding_unfollowsLastTopic_shouldHideOnboardingIsFalse() = runTest {
|
|
|
|
|
// Given: user completes onboarding by selecting a single topic.
|
|
|
|
|
subject.toggleFollowedTopicId("1", true)
|
|
|
|
|
subject.setShouldHideOnboarding(true)
|
|
|
|
|
fun userShouldHideOnboarding_unfollowsLastTopic_shouldHideOnboardingIsFalse() =
|
|
|
|
|
testScope.runTest {
|
|
|
|
|
// Given: user completes onboarding by selecting a single topic.
|
|
|
|
|
subject.toggleFollowedTopicId("1", true)
|
|
|
|
|
subject.setShouldHideOnboarding(true)
|
|
|
|
|
|
|
|
|
|
// When: they unfollow that topic.
|
|
|
|
|
subject.toggleFollowedTopicId("1", false)
|
|
|
|
|
// When: they unfollow that topic.
|
|
|
|
|
subject.toggleFollowedTopicId("1", false)
|
|
|
|
|
|
|
|
|
|
// Then: onboarding should be shown again
|
|
|
|
|
assertFalse(subject.userData.first().shouldHideOnboarding)
|
|
|
|
|
}
|
|
|
|
|
// Then: onboarding should be shown again
|
|
|
|
|
assertFalse(subject.userData.first().shouldHideOnboarding)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
fun userShouldHideOnboarding_unfollowsAllTopics_shouldHideOnboardingIsFalse() = runTest {
|
|
|
|
|
// Given: user completes onboarding by selecting several topics.
|
|
|
|
|
subject.setFollowedTopicIds(setOf("1", "2"))
|
|
|
|
|
subject.setShouldHideOnboarding(true)
|
|
|
|
|
fun userShouldHideOnboarding_unfollowsAllTopics_shouldHideOnboardingIsFalse() =
|
|
|
|
|
testScope.runTest {
|
|
|
|
|
// Given: user completes onboarding by selecting several topics.
|
|
|
|
|
subject.setFollowedTopicIds(setOf("1", "2"))
|
|
|
|
|
subject.setShouldHideOnboarding(true)
|
|
|
|
|
|
|
|
|
|
// When: they unfollow those topics.
|
|
|
|
|
subject.setFollowedTopicIds(emptySet())
|
|
|
|
|
// When: they unfollow those topics.
|
|
|
|
|
subject.setFollowedTopicIds(emptySet())
|
|
|
|
|
|
|
|
|
|
// Then: onboarding should be shown again
|
|
|
|
|
assertFalse(subject.userData.first().shouldHideOnboarding)
|
|
|
|
|
}
|
|
|
|
|
// Then: onboarding should be shown again
|
|
|
|
|
assertFalse(subject.userData.first().shouldHideOnboarding)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
fun shouldUseDynamicColorFalseByDefault() = runTest {
|
|
|
|
|
fun shouldUseDynamicColorFalseByDefault() = testScope.runTest {
|
|
|
|
|
assertFalse(subject.userData.first().useDynamicColor)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
fun userShouldUseDynamicColorIsTrueWhenSet() = runTest {
|
|
|
|
|
fun userShouldUseDynamicColorIsTrueWhenSet() = testScope.runTest {
|
|
|
|
|
subject.setDynamicColorPreference(true)
|
|
|
|
|
assertTrue(subject.userData.first().useDynamicColor)
|
|
|
|
|
}
|
|
|
|
|