Skip to content

Commit 90b5684

Browse files
committed
update SettingsTest with appLocale and syncLanguage tests
1 parent 903780a commit 90b5684

1 file changed

Lines changed: 81 additions & 7 deletions

File tree

app/src/test/kotlin/com/vrem/wifianalyzer/settings/SettingsTest.kt

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package com.vrem.wifianalyzer.settings
1919

2020
import android.content.SharedPreferences.OnSharedPreferenceChangeListener
2121
import android.os.Build
22+
import androidx.appcompat.app.AppCompatDelegate
23+
import androidx.core.os.LocaleListCompat
2224
import androidx.test.ext.junit.runners.AndroidJUnit4
2325
import com.vrem.util.currentCountryCode
2426
import com.vrem.util.ordinals
@@ -36,9 +38,13 @@ import org.assertj.core.api.Assertions.assertThat
3638
import org.junit.After
3739
import org.junit.Test
3840
import org.junit.runner.RunWith
41+
import org.mockito.kotlin.any
3942
import org.mockito.kotlin.doNothing
4043
import org.mockito.kotlin.doReturn
44+
import org.mockito.kotlin.eq
4145
import org.mockito.kotlin.mock
46+
import org.mockito.kotlin.mockStatic
47+
import org.mockito.kotlin.never
4248
import org.mockito.kotlin.times
4349
import org.mockito.kotlin.verify
4450
import org.mockito.kotlin.verifyNoMoreInteractions
@@ -362,13 +368,81 @@ class SettingsTest {
362368

363369
@Test
364370
fun appLocale() {
365-
// Note: appLocale() uses AppCompatDelegate.getApplicationLocales() which is system API
366-
// and returns the current application locale (device default in test environment)
367-
// execute
368-
val actual = fixture.appLocale()
369-
// validate: should return a valid Locale (default in test)
370-
assertThat(actual).isNotNull
371-
assertThat(actual.language).isNotEmpty()
371+
// setup
372+
val localeTag = "de-DE"
373+
mockStatic<AppCompatDelegate>().use { mockedDelegate ->
374+
mockedDelegate
375+
.`when`<LocaleListCompat> {
376+
AppCompatDelegate.getApplicationLocales()
377+
}.thenReturn(LocaleListCompat.forLanguageTags(localeTag))
378+
// execute
379+
val actual = fixture.appLocale()
380+
// validate
381+
assertThat(actual.language).isEqualTo("de")
382+
}
383+
}
384+
385+
@Test
386+
fun syncLanguageWhenLocaleChanges() {
387+
// setup
388+
val newLocaleTag = "zh"
389+
val currentStoredTag = "en"
390+
391+
mockStatic<AppCompatDelegate>().use { mockedDelegate ->
392+
mockedDelegate
393+
.`when`<LocaleListCompat> {
394+
AppCompatDelegate.getApplicationLocales()
395+
}.thenReturn(LocaleListCompat.forLanguageTags(newLocaleTag))
396+
397+
whenever(repository.string(R.string.language_key, "")).thenReturn(currentStoredTag)
398+
// execute
399+
fixture.syncLanguage()
400+
// validate
401+
verify(repository).string(R.string.language_key, "")
402+
verify(repository).save(R.string.language_key, newLocaleTag)
403+
}
404+
}
405+
406+
@Test
407+
fun syncLanguageWhenLocaleIsSame() {
408+
// setup
409+
val sameTag = "en"
410+
411+
mockStatic<AppCompatDelegate>().use { mockedDelegate ->
412+
mockedDelegate
413+
.`when`<LocaleListCompat> {
414+
AppCompatDelegate.getApplicationLocales()
415+
}.thenReturn(LocaleListCompat.forLanguageTags(sameTag))
416+
417+
whenever(repository.string(R.string.language_key, "")).thenReturn(sameTag)
418+
// execute
419+
fixture.syncLanguage()
420+
// validate
421+
verify(repository).string(R.string.language_key, "")
422+
verify(repository, never()).save(eq(R.string.language_key), any<String>())
423+
}
424+
}
425+
426+
@Test
427+
fun syncLanguageWithNormalization() {
428+
// setup
429+
val systemLocaleTag = "zh-CN"
430+
val expectedNormalizedTag = "zh-Hans"
431+
val currentStoredTag = "en"
432+
433+
mockStatic<AppCompatDelegate>().use { mockedDelegate ->
434+
mockedDelegate
435+
.`when`<LocaleListCompat> {
436+
AppCompatDelegate.getApplicationLocales()
437+
}.thenReturn(LocaleListCompat.forLanguageTags(systemLocaleTag))
438+
439+
whenever(repository.string(R.string.language_key, "")).thenReturn(currentStoredTag)
440+
// execute
441+
fixture.syncLanguage()
442+
// validate
443+
verify(repository).string(R.string.language_key, "")
444+
verify(repository).save(R.string.language_key, expectedNormalizedTag)
445+
}
372446
}
373447

374448
@Test

0 commit comments

Comments
 (0)