77package mozilla.lockbox.store
88
99import android.content.Context
10+ import android.content.res.Resources
1011import android.content.SharedPreferences
12+ import android.content.res.Configuration
1113import android.os.Build
14+ import android.os.LocaleList
1215import android.preference.PreferenceManager
1316import android.view.autofill.AutofillManager
1417import com.f2prateek.rx.preferences2.Preference
@@ -41,6 +44,7 @@ import org.powermock.api.mockito.PowerMockito
4144import org.powermock.core.classloader.annotations.PrepareForTest
4245import org.powermock.modules.junit4.PowerMockRunner
4346import org.robolectric.util.ReflectionHelpers
47+ import java.util.Locale
4448import 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