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

Commit eefbabc

Browse files
committed
Unit test for switch sync service
1 parent 7f5adc1 commit eefbabc

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

app/src/test/java/mozilla/lockbox/presenter/SettingPresenterTest.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package mozilla.lockbox.presenter
99
import io.reactivex.observers.TestObserver
1010
import io.reactivex.subjects.PublishSubject
1111
import io.reactivex.subjects.Subject
12+
import kotlinx.coroutines.ExperimentalCoroutinesApi
1213
import mozilla.lockbox.R
1314
import mozilla.lockbox.action.AppWebPageAction
1415
import mozilla.lockbox.action.DialogAction
@@ -25,6 +26,7 @@ import mozilla.lockbox.adapter.ToggleSettingConfiguration
2526
import mozilla.lockbox.extensions.assertLastValue
2627
import mozilla.lockbox.flux.Action
2728
import mozilla.lockbox.flux.Dispatcher
29+
import mozilla.lockbox.store.AccountStore
2830
import mozilla.lockbox.store.FingerprintStore
2931
import mozilla.lockbox.store.SettingStore
3032
import org.junit.Assert
@@ -40,6 +42,7 @@ import org.robolectric.RobolectricTestRunner
4042
import org.mockito.Mockito.`when` as whenCalled
4143

4244
@RunWith(RobolectricTestRunner::class)
45+
@ExperimentalCoroutinesApi
4346
class SettingPresenterTest {
4447
class SettingViewFake : SettingView {
4548
var settingItem: List<SettingCellConfiguration>? = null
@@ -56,13 +59,15 @@ class SettingPresenterTest {
5659

5760
private val unlockWithFingerprintStub = PublishSubject.create<Boolean>()
5861
private val sendUsageDataStub = PublishSubject.create<Boolean>()
62+
private val useLocalServiceStub = PublishSubject.create<Boolean>()
5963
private val itemListSortOrderStub = PublishSubject.create<Setting.ItemListSort>()
6064
private val unlockWithFingerprintPendingAuthStub = PublishSubject.create<Boolean>()
6165
private val autoLockTimeStub = PublishSubject.create<Setting.AutoLockTime>()
6266
private val onEnablingFingerprintStub = PublishSubject.create<FingerprintAuthAction>()
6367

6468
@Mock
6569
val settingStore = PowerMockito.mock(SettingStore::class.java)
70+
val accountStore = PowerMockito.mock(AccountStore::class.java)
6671

6772
private lateinit var testHelper: ListAdapterTestHelper
6873
private val view = SettingViewFake()
@@ -78,6 +83,7 @@ class SettingPresenterTest {
7883
whenCalled(settingStore.unlockWithFingerprint).thenReturn(unlockWithFingerprintStub)
7984
whenCalled(settingStore.unlockWithFingerprintPendingAuth).thenReturn(unlockWithFingerprintPendingAuthStub)
8085
whenCalled(settingStore.sendUsageData).thenReturn(sendUsageDataStub)
86+
whenCalled(settingStore.useLocalService).thenReturn(useLocalServiceStub)
8187
whenCalled(settingStore.itemListSortOrder).thenReturn(itemListSortOrderStub)
8288
whenCalled(settingStore.onEnablingFingerprint).thenReturn(onEnablingFingerprintStub)
8389

@@ -87,7 +93,9 @@ class SettingPresenterTest {
8793
testHelper = ListAdapterTestHelper()
8894
subject = SettingPresenter(
8995
view,
96+
false,
9097
dispatcher,
98+
accountStore,
9199
settingStore,
92100
fingerprintStore
93101
)

app/src/test/java/mozilla/lockbox/presenter/WelcomePresenterTest.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import org.mockito.Mockito
2525
class WelcomePresenterTest {
2626
class FakeWelcomeView : WelcomeView {
2727
var existingAccount: Boolean? = null
28+
var chinaBuild: Boolean? = null
2829

2930
override fun showExistingAccount(email: String) {
3031
existingAccount = true
@@ -34,6 +35,10 @@ class WelcomePresenterTest {
3435
existingAccount = false
3536
}
3637

38+
override fun hideSwitchService() {
39+
chinaBuild = false
40+
}
41+
3742
val learnMoreStub = PublishSubject.create<Unit>()
3843
override val learnMoreClicks: Observable<Unit> = learnMoreStub
3944

@@ -44,6 +49,10 @@ class WelcomePresenterTest {
4449
val getStartedExistingAcccountStub: PublishSubject<Unit> = PublishSubject.create<Unit>()
4550
override val getStartedAutomaticallyClicks: Observable<Unit>
4651
get() = getStartedExistingAcccountStub
52+
53+
val switchServiceStub: PublishSubject<Unit> = PublishSubject.create<Unit>()
54+
override val switchServiceClicks: Observable<Unit>
55+
get() = switchServiceStub
4756
}
4857

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

61-
val subject = WelcomePresenter(view, dispatcher,
70+
val subject = WelcomePresenter(view, false, dispatcher,
6271
accountStore = accountStore,
6372
fingerprintStore = fingerprintStore
6473
)

app/src/test/java/mozilla/lockbox/store/SettingStoreTest.kt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
package mozilla.lockbox.store
88

99
import android.content.Context
10+
import android.content.res.Resources
1011
import android.content.SharedPreferences
12+
import android.content.res.Configuration
1113
import android.os.Build
14+
import android.os.LocaleList
1215
import android.preference.PreferenceManager
1316
import android.view.autofill.AutofillManager
1417
import com.f2prateek.rx.preferences2.Preference
@@ -41,6 +44,7 @@ import org.powermock.api.mockito.PowerMockito
4144
import org.powermock.core.classloader.annotations.PrepareForTest
4245
import org.powermock.modules.junit4.PowerMockRunner
4346
import org.robolectric.util.ReflectionHelpers
47+
import java.util.Locale
4448
import org.mockito.Mockito.`when` as whenCalled
4549

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

56+
@Mock
57+
val resources: Resources = Mockito.mock(Resources::class.java)
58+
59+
@Mock
60+
val configuration: Configuration = Mockito.mock(Configuration::class.java)
61+
62+
@Mock
63+
val locales: LocaleList = Mockito.mock(LocaleList::class.java)
64+
65+
@Mock
66+
val locale: Locale = Mockito.mock(Locale::class.java)
67+
5268
@Mock
5369
val sharedPreferences: SharedPreferences = Mockito.mock(SharedPreferences::class.java)
5470

@@ -139,6 +155,21 @@ class SettingStoreTest : DisposingTest() {
139155
override fun delete() { TODO("not implemented") }
140156
}
141157

158+
private val useLocalServiceSetting = PublishSubject.create<Boolean>()
159+
private val useLocalserviceStub = object : Preference<Boolean> {
160+
override fun asObservable(): Observable<Boolean> {
161+
return useLocalServiceSetting
162+
}
163+
164+
override fun isSet(): Boolean { TODO("not implemented") }
165+
override fun key(): String { TODO("not implemented") }
166+
override fun asConsumer(): Consumer<in Boolean> { TODO("not implemented") }
167+
override fun defaultValue(): Boolean { TODO("not implemented") }
168+
override fun get(): Boolean { TODO("not implemented") }
169+
override fun set(value: Boolean) { TODO("not implemented") }
170+
override fun delete() { TODO("not implemented") }
171+
}
172+
142173
private val itemListSortSetting = PublishSubject.create<String>()
143174
private val itemListSortStub = object : Preference<String> {
144175
override fun asObservable(): Observable<String> {
@@ -157,6 +188,7 @@ class SettingStoreTest : DisposingTest() {
157188
private val dispatcher = Dispatcher()
158189
var subject = SettingStore(dispatcher, fingerprintStore)
159190
private val sendUsageDataObserver = TestObserver<Boolean>()
191+
private val useLocalServiceObserver = TestObserver<Boolean>()
160192
private val itemListSortOrder = TestObserver<Setting.ItemListSort>()
161193
private val unlockWithFingerprint = TestObserver<Boolean>()
162194
private val autoLockTime = TestObserver<Setting.AutoLockTime>()
@@ -168,6 +200,11 @@ class SettingStoreTest : DisposingTest() {
168200
fun setUp() {
169201
ReflectionHelpers.setStaticField(Build.VERSION::class.java, "SDK_INT", 28)
170202

203+
whenCalled(context.resources).thenReturn(resources)
204+
whenCalled(resources.configuration).thenReturn(configuration)
205+
whenCalled(configuration.locales).thenReturn(locales)
206+
whenCalled(locales.get(0)).thenReturn(locale)
207+
171208
whenCalled(editor.putBoolean(Mockito.anyString(), Mockito.anyBoolean())).thenReturn(editor)
172209
whenCalled(editor.putString(Mockito.anyString(), Mockito.anyString())).thenReturn(editor)
173210
whenCalled(sharedPreferences.edit()).thenReturn(editor)
@@ -178,6 +215,7 @@ class SettingStoreTest : DisposingTest() {
178215
whenCalled(rxSharedPreferences.getString(eq(SettingStore.Keys.AUTO_LOCK_TIME), anyString())).thenReturn(autoLockStub)
179216
whenCalled(rxSharedPreferences.getBoolean(eq(SettingStore.Keys.DEVICE_SECURITY_PRESENT), anyBoolean())).thenReturn(deviceSecureStub)
180217
whenCalled(rxSharedPreferences.getBoolean(eq(SettingStore.Keys.SEND_USAGE_DATA), anyBoolean())).thenReturn(sendUsageDataStub)
218+
whenCalled(rxSharedPreferences.getBoolean(eq(SettingStore.Keys.USE_LOCAL_SERVICE), anyBoolean())).thenReturn(useLocalserviceStub)
181219
whenCalled(rxSharedPreferences.getBoolean(eq(SettingStore.Keys.UNLOCK_WITH_FINGERPRINT), anyBoolean())).thenReturn(unlockWithFingerprintStub)
182220
whenCalled(rxSharedPreferences.getString(eq(SettingStore.Keys.ITEM_LIST_SORT_ORDER), anyString())).thenReturn(itemListSortStub)
183221
whenCalled(rxSharedPreferences.getBoolean(SettingStore.Keys.UNLOCK_WITH_FINGERPRINT_PENDING_AUTH)).thenReturn(unlockWithFingerprintPendingAuthStub)
@@ -193,6 +231,7 @@ class SettingStoreTest : DisposingTest() {
193231
subject.sendUsageData.subscribe(sendUsageDataObserver)
194232
subject.unlockWithFingerprint.subscribe(unlockWithFingerprint)
195233
subject.itemListSortOrder.subscribe(itemListSortOrder)
234+
subject.useLocalService.subscribe(useLocalServiceObserver)
196235

197236
clearInvocations(editor)
198237
}
@@ -384,6 +423,7 @@ class SettingStoreTest : DisposingTest() {
384423

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

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

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

421463
verify(editor).putString(SettingStore.Keys.ITEM_LIST_SORT_ORDER, Constant.SettingDefault.itemListSort.name)
422464
verify(editor).putBoolean(SettingStore.Keys.SEND_USAGE_DATA, Constant.SettingDefault.sendUsageData)
465+
verify(editor).putBoolean(SettingStore.Keys.USE_LOCAL_SERVICE, Constant.SettingDefault.useLocalServiceFalse)
423466
verify(editor).putString(SettingStore.Keys.AUTO_LOCK_TIME, Constant.SettingDefault.autoLockTime.name)
424467
verify(editor).apply()
425468
verify(autofillManager, never()).disableAutofillServices()

0 commit comments

Comments
 (0)