-
Notifications
You must be signed in to change notification settings - Fork 935
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3e772cd
commit 987b615
Showing
8 changed files
with
327 additions
and
2 deletions.
There are no files selected for viewing
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
110 changes: 110 additions & 0 deletions
110
...src/test/java/com/duckduckgo/savedsites/impl/sync/AppSavedSitesSyncFeatureListenerTest.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,110 @@ | ||
/* | ||
* Copyright (c) 2023 DuckDuckGo | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.duckduckgo.savedsites.impl.sync | ||
|
||
import android.content.Intent | ||
import androidx.core.app.NotificationManagerCompat | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import androidx.test.platform.app.InstrumentationRegistry | ||
import com.duckduckgo.common.test.CoroutineTestRule | ||
import com.duckduckgo.common.test.FileUtilities | ||
import com.duckduckgo.navigation.api.GlobalActivityStarter | ||
import com.duckduckgo.sync.api.SyncActivityWithEmptyParams | ||
import com.duckduckgo.sync.api.engine.FeatureSyncError | ||
import com.duckduckgo.sync.api.engine.SyncChangesResponse | ||
import com.duckduckgo.sync.api.engine.SyncableType.BOOKMARKS | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import org.junit.Assert.* | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.whenever | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
@RunWith(AndroidJUnit4::class) | ||
class AppSavedSitesSyncFeatureListenerTest { | ||
|
||
@get:Rule | ||
var coroutineRule = CoroutineTestRule() | ||
private val realContext = InstrumentationRegistry.getInstrumentation().targetContext | ||
private val notificationManager = NotificationManagerCompat.from(realContext) | ||
private val savedSitesSyncStore = RealSavedSitesSyncStore(realContext, coroutineRule.testScope, coroutineRule.testDispatcherProvider) | ||
private val globalActivityStarterMock = mock<GlobalActivityStarter>().apply { | ||
whenever(this.startIntent(realContext, SyncActivityWithEmptyParams)).thenReturn(Intent()) | ||
} | ||
|
||
private val testee = AppSavedSitesSyncFeatureListener( | ||
realContext, | ||
savedSitesSyncStore, | ||
notificationManager, | ||
AppSavedSitesSyncNotificationBuilder(globalActivityStarterMock), | ||
) | ||
|
||
@Test | ||
fun whenNoValuesThenIsSyncPausedIsFalse() { | ||
assertFalse(savedSitesSyncStore.isSyncPaused) | ||
} | ||
|
||
@Test | ||
fun whenSyncPausedAndOnSuccessWithChangesThenIsSyncPausedIsFalse() { | ||
savedSitesSyncStore.isSyncPaused = true | ||
val updatesJSON = FileUtilities.loadText(javaClass.classLoader!!, "json/merger_first_get.json") | ||
val validChanges = SyncChangesResponse(BOOKMARKS, updatesJSON) | ||
|
||
testee.onSuccess(validChanges) | ||
|
||
assertFalse(savedSitesSyncStore.isSyncPaused) | ||
} | ||
|
||
@Test | ||
fun whenSyncPausedAndOnSuccessWithoutChangesThenSyncPaused() { | ||
savedSitesSyncStore.isSyncPaused = true | ||
val validChanges = SyncChangesResponse.empty(BOOKMARKS) | ||
|
||
testee.onSuccess(validChanges) | ||
|
||
assertTrue(savedSitesSyncStore.isSyncPaused) | ||
} | ||
|
||
@Test | ||
fun whenSyncPausedAndOnErrorThenSyncPaused() { | ||
savedSitesSyncStore.isSyncPaused = true | ||
|
||
testee.onError(FeatureSyncError.COLLECTION_LIMIT_REACHED) | ||
|
||
assertTrue(savedSitesSyncStore.isSyncPaused) | ||
} | ||
|
||
@Test | ||
fun whenSyncActiveAndOnErrorThenSyncPaused() { | ||
savedSitesSyncStore.isSyncPaused = false | ||
|
||
testee.onError(FeatureSyncError.COLLECTION_LIMIT_REACHED) | ||
|
||
assertTrue(savedSitesSyncStore.isSyncPaused) | ||
} | ||
|
||
@Test | ||
fun whenOnSyncDisabledThenSyncPausedFalse() { | ||
savedSitesSyncStore.isSyncPaused = true | ||
|
||
testee.onSyncDisabled() | ||
|
||
assertFalse(savedSitesSyncStore.isSyncPaused) | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...tes-impl/src/test/java/com/duckduckgo/savedsites/impl/sync/RealSavedSitesSyncStoreTest.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,61 @@ | ||
/* | ||
* Copyright (c) 2023 DuckDuckGo | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.duckduckgo.savedsites.impl.sync | ||
|
||
import android.content.Context | ||
import app.cash.turbine.test | ||
import com.duckduckgo.common.test.CoroutineTestRule | ||
import com.duckduckgo.common.test.api.InMemorySharedPreferences | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Assert.* | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.mockito.ArgumentMatchers.anyInt | ||
import org.mockito.ArgumentMatchers.anyString | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.whenever | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
class RealSavedSitesSyncStoreTest { | ||
|
||
@get:Rule | ||
var coroutineRule = CoroutineTestRule() | ||
|
||
private val preferences = InMemorySharedPreferences() | ||
|
||
private val mockContext: Context = mock<Context>().apply { | ||
whenever(this.getSharedPreferences(anyString(), anyInt())).thenReturn(preferences) | ||
} | ||
|
||
val testee = RealSavedSitesSyncStore(mockContext, coroutineRule.testScope, coroutineRule.testDispatcherProvider) | ||
|
||
@Test | ||
fun whenNoValueIsSyncPausedThenReturnFalse() { | ||
assertFalse(testee.isSyncPaused) | ||
} | ||
|
||
@Test | ||
fun whenIsSyncPausedUpdatedThenEmitNewValue() = runTest { | ||
testee.isSyncPausedFlow().test { | ||
awaitItem() | ||
testee.isSyncPaused = true | ||
assertEquals(true, awaitItem()) | ||
cancelAndConsumeRemainingEvents() | ||
} | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...impl/src/test/java/com/duckduckgo/savedsites/impl/sync/SavedSiteRateLimitViewModelTest.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,62 @@ | ||
/* | ||
* Copyright (c) 2023 DuckDuckGo | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.duckduckgo.savedsites.impl.sync | ||
|
||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import androidx.test.platform.app.InstrumentationRegistry | ||
import app.cash.turbine.test | ||
import com.duckduckgo.common.test.CoroutineTestRule | ||
import com.duckduckgo.savedsites.impl.sync.SavedSiteRateLimitViewModel.Command.NavigateToBookmarks | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.test.runTest | ||
import org.junit.Assert.* | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
@RunWith(AndroidJUnit4::class) | ||
class SavedSiteRateLimitViewModelTest { | ||
@get:Rule | ||
var coroutineRule = CoroutineTestRule() | ||
|
||
private val realContext = InstrumentationRegistry.getInstrumentation().targetContext | ||
val savedSitesSyncStore = RealSavedSitesSyncStore(realContext, coroutineRule.testScope, coroutineRule.testDispatcherProvider) | ||
|
||
val testee = SavedSiteRateLimitViewModel( | ||
savedSitesSyncStore, | ||
coroutineRule.testDispatcherProvider, | ||
) | ||
|
||
@Test | ||
fun whenSyncPausedThenWarningVisible() = runTest { | ||
savedSitesSyncStore.isSyncPaused = true | ||
testee.viewState().test { | ||
assertTrue(awaitItem().warningVisible) | ||
cancelAndConsumeRemainingEvents() | ||
} | ||
} | ||
|
||
@Test | ||
fun whenUserClicksWarningActionThenNavigateToBookmarks() = runTest { | ||
testee.commands().test { | ||
testee.onWarningActionClicked() | ||
assertEquals(NavigateToBookmarks, awaitItem()) | ||
cancelAndConsumeRemainingEvents() | ||
} | ||
} | ||
} |
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
Oops, something went wrong.