| 1 |
/* |
| 2 |
* Copyright (C) 2020 The Android Open Source Project |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package com.android.android.internal.app |
| 18 |
|
| 19 |
import android.os.SystemProperties |
| 20 |
import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 21 |
import androidx.test.platform.app.InstrumentationRegistry |
| 22 |
import com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn |
| 23 |
import com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession |
| 24 |
import com.android.internal.R |
| 25 |
import com.android.internal.app.LocalePicker |
| 26 |
import com.google.common.truth.Truth.assertThat |
| 27 |
import org.junit.Before |
| 28 |
import org.junit.After |
| 29 |
import org.junit.Test |
| 30 |
import org.junit.runner.RunWith |
| 31 |
import org.mockito.ArgumentMatchers.eq |
| 32 |
import org.mockito.MockitoSession |
| 33 |
|
| 34 |
@RunWith(AndroidJUnit4::class) |
| 35 |
class LocalizationTest { |
| 36 |
private val mContext = InstrumentationRegistry.getInstrumentation().context |
| 37 |
private val mUnfilteredLocales = |
| 38 |
mContext.getResources().getStringArray(R.array.supported_locales) |
| 39 |
|
| 40 |
private lateinit var mMockitoSession: MockitoSession |
| 41 |
|
| 42 |
@Before |
| 43 |
fun setUp() { |
| 44 |
mMockitoSession = mockitoSession() |
| 45 |
.initMocks(this) |
| 46 |
.spyStatic(SystemProperties::class.java) |
| 47 |
.startMocking() |
| 48 |
} |
| 49 |
|
| 50 |
@After |
| 51 |
fun tearDown() { |
| 52 |
mMockitoSession.finishMocking() |
| 53 |
} |
| 54 |
|
| 55 |
@Test |
| 56 |
fun testGetSupportedLocales_noFilter() { |
| 57 |
// Filter not set. |
| 58 |
setTestLocaleFilter(null) |
| 59 |
|
| 60 |
val locales1 = LocalePicker.getSupportedLocales(mContext) |
| 61 |
|
| 62 |
assertThat(locales1).isEqualTo(mUnfilteredLocales) |
| 63 |
|
| 64 |
// Empty filter. |
| 65 |
setTestLocaleFilter("") |
| 66 |
|
| 67 |
val locales2 = LocalePicker.getSupportedLocales(mContext) |
| 68 |
|
| 69 |
assertThat(locales2).isEqualTo(mUnfilteredLocales) |
| 70 |
} |
| 71 |
|
| 72 |
@Test |
| 73 |
fun testGetSupportedLocales_invalidFilter() { |
| 74 |
setTestLocaleFilter("**") |
| 75 |
|
| 76 |
val locales = LocalePicker.getSupportedLocales(mContext) |
| 77 |
|
| 78 |
assertThat(locales).isEqualTo(mUnfilteredLocales) |
| 79 |
} |
| 80 |
|
| 81 |
@Test |
| 82 |
fun testGetSupportedLocales_inclusiveFilter() { |
| 83 |
setTestLocaleFilter("^(de-AT|de-DE|en|ru).*") |
| 84 |
|
| 85 |
val locales = LocalePicker.getSupportedLocales(mContext) |
| 86 |
|
| 87 |
assertThat(locales).isEqualTo( |
| 88 |
mUnfilteredLocales |
| 89 |
.filter { it.startsWithAnyOf("de-AT", "de-DE", "en", "ru") } |
| 90 |
.toTypedArray() |
| 91 |
) |
| 92 |
} |
| 93 |
|
| 94 |
@Test |
| 95 |
fun testGetSupportedLocales_exclusiveFilter() { |
| 96 |
setTestLocaleFilter("^(?!de-IT|es|fr).*") |
| 97 |
|
| 98 |
val locales = LocalePicker.getSupportedLocales(mContext) |
| 99 |
|
| 100 |
assertThat(locales).isEqualTo( |
| 101 |
mUnfilteredLocales |
| 102 |
.filter { !it.startsWithAnyOf("de-IT", "es", "fr") } |
| 103 |
.toTypedArray() |
| 104 |
) |
| 105 |
} |
| 106 |
|
| 107 |
private fun setTestLocaleFilter(localeFilter: String?) { |
| 108 |
doReturn(localeFilter).`when` { SystemProperties.get(eq("ro.localization.locale_filter")) } |
| 109 |
} |
| 110 |
|
| 111 |
private fun String.startsWithAnyOf(vararg prefixes: String): Boolean { |
| 112 |
prefixes.forEach { |
| 113 |
if (startsWith(it)) return true |
| 114 |
} |
| 115 |
|
| 116 |
return false |
| 117 |
} |
| 118 |
} |