Skip to content
This repository has been archived by the owner on Dec 14, 2021. It is now read-only.

Commit

Permalink
Unit test for switch sync service
Browse files Browse the repository at this point in the history
  • Loading branch information
rxumoz committed Nov 21, 2019
1 parent 76ce685 commit b678b15
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package mozilla.lockbox.presenter
import io.reactivex.observers.TestObserver
import io.reactivex.subjects.PublishSubject
import io.reactivex.subjects.Subject
import kotlinx.coroutines.ExperimentalCoroutinesApi
import mozilla.lockbox.R
import mozilla.lockbox.action.AppWebPageAction
import mozilla.lockbox.action.DialogAction
Expand All @@ -25,6 +26,7 @@ import mozilla.lockbox.adapter.ToggleSettingConfiguration
import mozilla.lockbox.extensions.assertLastValue
import mozilla.lockbox.flux.Action
import mozilla.lockbox.flux.Dispatcher
import mozilla.lockbox.store.AccountStore
import mozilla.lockbox.store.FingerprintStore
import mozilla.lockbox.store.SettingStore
import org.junit.Assert
Expand All @@ -40,6 +42,7 @@ import org.robolectric.RobolectricTestRunner
import org.mockito.Mockito.`when` as whenCalled

@RunWith(RobolectricTestRunner::class)
@ExperimentalCoroutinesApi
class SettingPresenterTest {
class SettingViewFake : SettingView {
var settingItem: List<SettingCellConfiguration>? = null
Expand All @@ -56,13 +59,15 @@ class SettingPresenterTest {

private val unlockWithFingerprintStub = PublishSubject.create<Boolean>()
private val sendUsageDataStub = PublishSubject.create<Boolean>()
private val useLocalServiceStub = PublishSubject.create<Boolean>()
private val itemListSortOrderStub = PublishSubject.create<Setting.ItemListSort>()
private val unlockWithFingerprintPendingAuthStub = PublishSubject.create<Boolean>()
private val autoLockTimeStub = PublishSubject.create<Setting.AutoLockTime>()
private val onEnablingFingerprintStub = PublishSubject.create<FingerprintAuthAction>()

@Mock
val settingStore = PowerMockito.mock(SettingStore::class.java)
val accountStore = PowerMockito.mock(AccountStore::class.java)

private lateinit var testHelper: ListAdapterTestHelper
private val view = SettingViewFake()
Expand All @@ -78,6 +83,7 @@ class SettingPresenterTest {
whenCalled(settingStore.unlockWithFingerprint).thenReturn(unlockWithFingerprintStub)
whenCalled(settingStore.unlockWithFingerprintPendingAuth).thenReturn(unlockWithFingerprintPendingAuthStub)
whenCalled(settingStore.sendUsageData).thenReturn(sendUsageDataStub)
whenCalled(settingStore.useLocalService).thenReturn(useLocalServiceStub)
whenCalled(settingStore.itemListSortOrder).thenReturn(itemListSortOrderStub)
whenCalled(settingStore.onEnablingFingerprint).thenReturn(onEnablingFingerprintStub)

Expand All @@ -87,7 +93,9 @@ class SettingPresenterTest {
testHelper = ListAdapterTestHelper()
subject = SettingPresenter(
view,
false,
dispatcher,
accountStore,
settingStore,
fingerprintStore
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import org.mockito.Mockito
class WelcomePresenterTest {
class FakeWelcomeView : WelcomeView {
var existingAccount: Boolean? = null
var chinaBuild: Boolean? = null

override fun showExistingAccount(email: String) {
existingAccount = true
Expand All @@ -34,6 +35,10 @@ class WelcomePresenterTest {
existingAccount = false
}

override fun hideSwitchService() {
chinaBuild = false
}

val learnMoreStub = PublishSubject.create<Unit>()
override val learnMoreClicks: Observable<Unit> = learnMoreStub

Expand All @@ -44,6 +49,10 @@ class WelcomePresenterTest {
val getStartedExistingAcccountStub: PublishSubject<Unit> = PublishSubject.create<Unit>()
override val getStartedAutomaticallyClicks: Observable<Unit>
get() = getStartedExistingAcccountStub

val switchServiceStub: PublishSubject<Unit> = PublishSubject.create<Unit>()
override val switchServiceClicks: Observable<Unit>
get() = switchServiceStub
}

@Mock
Expand All @@ -58,7 +67,7 @@ class WelcomePresenterTest {
val dispatcher = Dispatcher()
val dispatcherObserver = TestObserver.create<Action>()

val subject = WelcomePresenter(view, dispatcher,
val subject = WelcomePresenter(view, false, dispatcher,
accountStore = accountStore,
fingerprintStore = fingerprintStore
)
Expand Down
43 changes: 43 additions & 0 deletions app/src/test/java/mozilla/lockbox/store/SettingStoreTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
package mozilla.lockbox.store

import android.content.Context
import android.content.res.Resources
import android.content.SharedPreferences
import android.content.res.Configuration
import android.os.Build
import android.os.LocaleList
import android.preference.PreferenceManager
import android.view.autofill.AutofillManager
import com.f2prateek.rx.preferences2.Preference
Expand Down Expand Up @@ -41,6 +44,7 @@ import org.powermock.api.mockito.PowerMockito
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.PowerMockRunner
import org.robolectric.util.ReflectionHelpers
import java.util.Locale
import org.mockito.Mockito.`when` as whenCalled

@RunWith(PowerMockRunner::class)
Expand All @@ -49,6 +53,18 @@ class SettingStoreTest : DisposingTest() {
@Mock
val context: Context = Mockito.mock(Context::class.java)

@Mock
val resources: Resources = Mockito.mock(Resources::class.java)

@Mock
val configuration: Configuration = Mockito.mock(Configuration::class.java)

@Mock
val locales: LocaleList = Mockito.mock(LocaleList::class.java)

@Mock
val locale: Locale = Mockito.mock(Locale::class.java)

@Mock
val sharedPreferences: SharedPreferences = Mockito.mock(SharedPreferences::class.java)

Expand Down Expand Up @@ -139,6 +155,21 @@ class SettingStoreTest : DisposingTest() {
override fun delete() { TODO("not implemented") }
}

private val useLocalServiceSetting = PublishSubject.create<Boolean>()
private val useLocalserviceStub = object : Preference<Boolean> {
override fun asObservable(): Observable<Boolean> {
return useLocalServiceSetting
}

override fun isSet(): Boolean { TODO("not implemented") }
override fun key(): String { TODO("not implemented") }
override fun asConsumer(): Consumer<in Boolean> { TODO("not implemented") }
override fun defaultValue(): Boolean { TODO("not implemented") }
override fun get(): Boolean { TODO("not implemented") }
override fun set(value: Boolean) { TODO("not implemented") }
override fun delete() { TODO("not implemented") }
}

private val itemListSortSetting = PublishSubject.create<String>()
private val itemListSortStub = object : Preference<String> {
override fun asObservable(): Observable<String> {
Expand All @@ -157,6 +188,7 @@ class SettingStoreTest : DisposingTest() {
private val dispatcher = Dispatcher()
var subject = SettingStore(dispatcher, fingerprintStore)
private val sendUsageDataObserver = TestObserver<Boolean>()
private val useLocalServiceObserver = TestObserver<Boolean>()
private val itemListSortOrder = TestObserver<Setting.ItemListSort>()
private val unlockWithFingerprint = TestObserver<Boolean>()
private val autoLockTime = TestObserver<Setting.AutoLockTime>()
Expand All @@ -168,6 +200,11 @@ class SettingStoreTest : DisposingTest() {
fun setUp() {
ReflectionHelpers.setStaticField(Build.VERSION::class.java, "SDK_INT", 28)

whenCalled(context.resources).thenReturn(resources)
whenCalled(resources.configuration).thenReturn(configuration)
whenCalled(configuration.locales).thenReturn(locales)
whenCalled(locales.get(0)).thenReturn(locale)

whenCalled(editor.putBoolean(Mockito.anyString(), Mockito.anyBoolean())).thenReturn(editor)
whenCalled(editor.putString(Mockito.anyString(), Mockito.anyString())).thenReturn(editor)
whenCalled(sharedPreferences.edit()).thenReturn(editor)
Expand All @@ -178,6 +215,7 @@ class SettingStoreTest : DisposingTest() {
whenCalled(rxSharedPreferences.getString(eq(SettingStore.Keys.AUTO_LOCK_TIME), anyString())).thenReturn(autoLockStub)
whenCalled(rxSharedPreferences.getBoolean(eq(SettingStore.Keys.DEVICE_SECURITY_PRESENT), anyBoolean())).thenReturn(deviceSecureStub)
whenCalled(rxSharedPreferences.getBoolean(eq(SettingStore.Keys.SEND_USAGE_DATA), anyBoolean())).thenReturn(sendUsageDataStub)
whenCalled(rxSharedPreferences.getBoolean(eq(SettingStore.Keys.USE_LOCAL_SERVICE), anyBoolean())).thenReturn(useLocalserviceStub)
whenCalled(rxSharedPreferences.getBoolean(eq(SettingStore.Keys.UNLOCK_WITH_FINGERPRINT), anyBoolean())).thenReturn(unlockWithFingerprintStub)
whenCalled(rxSharedPreferences.getString(eq(SettingStore.Keys.ITEM_LIST_SORT_ORDER), anyString())).thenReturn(itemListSortStub)
whenCalled(rxSharedPreferences.getBoolean(SettingStore.Keys.UNLOCK_WITH_FINGERPRINT_PENDING_AUTH)).thenReturn(unlockWithFingerprintPendingAuthStub)
Expand All @@ -193,6 +231,7 @@ class SettingStoreTest : DisposingTest() {
subject.sendUsageData.subscribe(sendUsageDataObserver)
subject.unlockWithFingerprint.subscribe(unlockWithFingerprint)
subject.itemListSortOrder.subscribe(itemListSortOrder)
subject.useLocalService.subscribe(useLocalServiceObserver)

clearInvocations(editor)
}
Expand Down Expand Up @@ -384,6 +423,7 @@ class SettingStoreTest : DisposingTest() {

verify(editor).putString(SettingStore.Keys.ITEM_LIST_SORT_ORDER, Constant.SettingDefault.itemListSort.name)
verify(editor).putBoolean(SettingStore.Keys.SEND_USAGE_DATA, Constant.SettingDefault.sendUsageData)
verify(editor).putBoolean(SettingStore.Keys.USE_LOCAL_SERVICE, Constant.SettingDefault.useLocalServiceFalse)
verify(editor).putString(SettingStore.Keys.AUTO_LOCK_TIME, Constant.SettingDefault.autoLockTime.name)
verify(editor).apply()
verify(autofillManager).disableAutofillServices()
Expand All @@ -396,6 +436,7 @@ class SettingStoreTest : DisposingTest() {

verify(editor).putString(SettingStore.Keys.ITEM_LIST_SORT_ORDER, Constant.SettingDefault.itemListSort.name)
verify(editor).putBoolean(SettingStore.Keys.SEND_USAGE_DATA, Constant.SettingDefault.sendUsageData)
verify(editor).putBoolean(SettingStore.Keys.USE_LOCAL_SERVICE, Constant.SettingDefault.useLocalServiceFalse)
verify(editor).putString(SettingStore.Keys.AUTO_LOCK_TIME, Constant.SettingDefault.autoLockTime.name)
verify(editor).apply()
verify(autofillManager).disableAutofillServices()
Expand All @@ -408,6 +449,7 @@ class SettingStoreTest : DisposingTest() {

verify(editor).putString(SettingStore.Keys.ITEM_LIST_SORT_ORDER, Constant.SettingDefault.itemListSort.name)
verify(editor).putBoolean(SettingStore.Keys.SEND_USAGE_DATA, Constant.SettingDefault.sendUsageData)
verify(editor).putBoolean(SettingStore.Keys.USE_LOCAL_SERVICE, Constant.SettingDefault.useLocalServiceFalse)
verify(editor).putString(SettingStore.Keys.AUTO_LOCK_TIME, Constant.SettingDefault.autoLockTime.name)
verify(editor).apply()
verify(autofillManager, never()).disableAutofillServices()
Expand All @@ -420,6 +462,7 @@ class SettingStoreTest : DisposingTest() {

verify(editor).putString(SettingStore.Keys.ITEM_LIST_SORT_ORDER, Constant.SettingDefault.itemListSort.name)
verify(editor).putBoolean(SettingStore.Keys.SEND_USAGE_DATA, Constant.SettingDefault.sendUsageData)
verify(editor).putBoolean(SettingStore.Keys.USE_LOCAL_SERVICE, Constant.SettingDefault.useLocalServiceFalse)
verify(editor).putString(SettingStore.Keys.AUTO_LOCK_TIME, Constant.SettingDefault.autoLockTime.name)
verify(editor).apply()
verify(autofillManager, never()).disableAutofillServices()
Expand Down

0 comments on commit b678b15

Please sign in to comment.