Skip to content

Commit 730dc4f

Browse files
committed
update LocaleUtils and refactor language preference tests
1 parent 4758f5d commit 730dc4f

6 files changed

Lines changed: 52 additions & 36 deletions

File tree

app/src/main/kotlin/com/vrem/util/LocaleUtils.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ val baseSupportedLocales: List<Locale> =
7272
).toList()
7373

7474
fun findByCountryCode(countryCode: String): Locale =
75-
availableLocales.firstOrNull { countryCode.toCapitalize(Locale.getDefault()) == it.country }
75+
availableLocales.firstOrNull { countryCode.uppercase(Locale.ROOT) == it.country }
7676
?: currentLocale
7777

7878
fun allCountries(): List<Locale> = countriesLocales.values.toList()
@@ -83,10 +83,14 @@ fun supportedLanguages(): List<Locale> =
8383
fun supportedLanguageTags(): List<String> =
8484
listOf("") + baseSupportedLocales.map { it.toLanguageTag() }
8585

86+
private fun normalizeLanguageTag(languageTag: String): String =
87+
languageTag.replace('_', '-').trim()
88+
8689
fun findByLanguageTag(languageTag: String): Locale {
87-
if (languageTag.isEmpty()) return currentLocale
90+
val normalizedLanguageTag = normalizeLanguageTag(languageTag)
91+
if (normalizedLanguageTag.isEmpty()) return currentLocale
8892

89-
val target = Locale.forLanguageTag(languageTag)
93+
val target = Locale.forLanguageTag(normalizedLanguageTag)
9094
if (target.language.isEmpty()) return currentLocale
9195

9296
return baseSupportedLocales.find { it == target }

app/src/main/kotlin/com/vrem/wifianalyzer/vendor/model/VendorService.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ class VendorService(
3434
fun findVendorName(address: String = String.EMPTY): String = vendorData.macs[address.clean()].orEmpty()
3535

3636
fun findMacAddresses(vendorName: String = String.EMPTY): List<String> =
37-
vendorData.vendors[vendorName.uppercase(Locale.getDefault())].orEmpty()
37+
vendorData.vendors[vendorName.uppercase(Locale.ROOT)].orEmpty()
3838

3939
fun findVendors(vendorName: String = String.EMPTY): List<String> {
40-
val name = vendorName.uppercase(Locale.getDefault())
40+
val name = vendorName.uppercase(Locale.ROOT)
4141
return vendorData.vendors
4242
.filterKeys { filter(it, name) }
4343
.keys

app/src/test/kotlin/com/vrem/util/LocaleUtilsTest.kt

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ class LocaleUtilsTest {
6969

7070
@Test
7171
fun toLanguageTagWithKnownCode() {
72-
assertThat(toLanguageTag(Locale.US)).isEqualTo(Locale.US.language + "_" + Locale.US.country)
73-
assertThat(toLanguageTag(Locale.ENGLISH)).isEqualTo(Locale.ENGLISH.language + "_")
72+
assertThat(toLanguageTag(Locale.US)).isEqualTo("en-US")
73+
assertThat(toLanguageTag(ENGLISH)).isEqualTo("en")
74+
assertThat(toLanguageTag(CHINESE_SIMPLIFIED)).isEqualTo("zh-Hans")
75+
assertThat(toLanguageTag(CHINESE_TRADITIONAL)).isEqualTo("zh-Hant")
7476
}
7577

7678
@Test
@@ -83,9 +85,16 @@ class LocaleUtilsTest {
8385

8486
@Test
8587
fun findByLanguageTagWithKnownTag() {
86-
assertThat(findByLanguageTag(toLanguageTag(Locale.SIMPLIFIED_CHINESE))).isEqualTo(Locale.SIMPLIFIED_CHINESE)
87-
assertThat(findByLanguageTag(toLanguageTag(Locale.TRADITIONAL_CHINESE))).isEqualTo(Locale.TRADITIONAL_CHINESE)
88-
assertThat(findByLanguageTag(toLanguageTag(Locale.ENGLISH))).isEqualTo(Locale.ENGLISH)
88+
// BCP-47 format (new)
89+
assertThat(findByLanguageTag("zh-Hans")).isEqualTo(CHINESE_SIMPLIFIED)
90+
assertThat(findByLanguageTag("zh-Hant")).isEqualTo(CHINESE_TRADITIONAL)
91+
assertThat(findByLanguageTag("en")).isEqualTo(ENGLISH)
92+
assertThat(findByLanguageTag("en-US")).isEqualTo(ENGLISH)
93+
94+
// Backward compatibility: underscore format (old)
95+
assertThat(findByLanguageTag("zh_Hans")).isEqualTo(CHINESE_SIMPLIFIED)
96+
assertThat(findByLanguageTag("zh_Hant")).isEqualTo(CHINESE_TRADITIONAL)
97+
assertThat(findByLanguageTag("en_US")).isEqualTo(ENGLISH)
8998
}
9099

91100
@Test

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

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

2020
import android.os.Build
2121
import androidx.test.ext.junit.runners.AndroidJUnit4
22-
import com.vrem.util.supportedLanguages
23-
import com.vrem.util.toCapitalize
24-
import com.vrem.util.toLanguageTag
22+
import com.vrem.util.supportedLanguageTags
23+
import com.vrem.util.titlecaseFirst
2524
import com.vrem.wifianalyzer.R
2625
import com.vrem.wifianalyzer.RobolectricUtil
2726
import org.assertj.core.api.Assertions.assertThat
@@ -35,7 +34,7 @@ import java.util.Locale
3534
@Config(sdk = [Build.VERSION_CODES.BAKLAVA])
3635
class LanguagePreferenceTest {
3736
private val mainActivity = RobolectricUtil.INSTANCE.activity
38-
private val languages = supportedLanguages()
37+
private val languageTags = supportedLanguageTags()
3938
private val attributeSet = Robolectric.getAttributeSetFromXml(R.xml.test_attrs)
4039
private val fixture = LanguagePreference(mainActivity, attributeSet)
4140

@@ -44,9 +43,16 @@ class LanguagePreferenceTest {
4443
// execute
4544
val actual: Array<CharSequence> = fixture.entries
4645
// validate
47-
assertThat(actual).hasSize(languages.size)
48-
languages.forEach {
49-
assertThat(actual).contains(it.getDisplayName(it).toCapitalize(Locale.getDefault()))
46+
assertThat(actual).hasSize(languageTags.size)
47+
48+
// Check system default entry
49+
assertThat(actual[0]).isEqualTo(mainActivity.getString(R.string.system_default))
50+
51+
// Check language entries
52+
languageTags.drop(1).forEachIndexed { index, tag ->
53+
val locale = Locale.forLanguageTag(tag)
54+
val displayName = locale.getDisplayName(locale).titlecaseFirst(locale)
55+
assertThat(actual[index + 1]).isEqualTo(displayName)
5056
}
5157
}
5258

@@ -55,9 +61,7 @@ class LanguagePreferenceTest {
5561
// execute
5662
val actual: Array<CharSequence> = fixture.entryValues
5763
// validate
58-
assertThat(actual).hasSize(languages.size)
59-
languages.forEach {
60-
assertThat(actual).contains(toLanguageTag(it))
61-
}
64+
assertThat(actual).hasSize(languageTags.size)
65+
assertThat(actual).containsExactlyElementsOf(languageTags)
6266
}
6367
}

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ import android.content.SharedPreferences.OnSharedPreferenceChangeListener
2121
import android.os.Build
2222
import androidx.test.ext.junit.runners.AndroidJUnit4
2323
import com.vrem.util.currentCountryCode
24-
import com.vrem.util.currentLanguageTag
2524
import com.vrem.util.ordinals
26-
import com.vrem.util.toLanguageTag
2725
import com.vrem.wifianalyzer.R
2826
import com.vrem.wifianalyzer.navigation.NavigationMenu
2927
import com.vrem.wifianalyzer.wifi.accesspoint.AccessPointViewType
@@ -46,7 +44,6 @@ import org.mockito.kotlin.verify
4644
import org.mockito.kotlin.verifyNoMoreInteractions
4745
import org.mockito.kotlin.whenever
4846
import org.robolectric.annotation.Config
49-
import java.util.Locale
5047

5148
@RunWith(AndroidJUnit4::class)
5249
@Config(sdk = [Build.VERSION_CODES.BAKLAVA])
@@ -364,16 +361,14 @@ class SettingsTest {
364361
}
365362

366363
@Test
367-
fun languageLocale() {
368-
// setup
369-
val defaultValue = currentLanguageTag()
370-
val expected = Locale.FRENCH
371-
doReturn(toLanguageTag(expected)).whenever(repository).string(R.string.language_key, defaultValue)
364+
fun appLocale() {
365+
// Note: appLocale() uses AppCompatDelegate.getApplicationLocales() which is system API
366+
// and returns the current application locale (device default in test environment)
372367
// execute
373368
val actual = fixture.appLocale()
374-
// validate
375-
assertThat(actual).isEqualTo(expected)
376-
verify(repository).string(R.string.language_key, defaultValue)
369+
// validate: should return a valid Locale (default in test)
370+
assertThat(actual).isNotNull
371+
assertThat(actual.language).isNotEmpty()
377372
}
378373

379374
@Test

app/src/test/kotlin/com/vrem/wifianalyzer/wifi/band/WiFiChannelCountryTest.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@ class WiFiChannelCountryTest {
3030

3131
@Test
3232
fun find() {
33-
val expected = Locale.US
34-
val actual = WiFiChannelCountry.find(expected.country)
35-
assertThat(actual.countryCode).isEqualTo(expected.country)
36-
assertThat(actual.countryName(expected)).isEqualTo(expected.displayCountry)
33+
val countryLocale = Locale.US
34+
val displayLocale = Locale.getDefault()
35+
36+
val actual = WiFiChannelCountry.find(countryLocale.country)
37+
38+
assertThat(actual.countryCode).isEqualTo(countryLocale.country)
39+
assertThat(actual.countryName(displayLocale))
40+
.isEqualTo(countryLocale.getDisplayCountry(displayLocale))
3741
}
3842

3943
@Test

0 commit comments

Comments
 (0)