1 /*
<lambda>null2  * Copyright 2019 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 androidx.testutils
18 
19 import android.content.Context
20 import android.content.res.Configuration
21 import android.view.View
22 import androidx.core.os.ConfigurationCompat
23 import androidx.test.core.app.ApplicationProvider
24 import androidx.test.ext.junit.runners.AndroidJUnit4
25 import androidx.test.filters.LargeTest
26 import java.util.Locale
27 import org.hamcrest.CoreMatchers
28 import org.hamcrest.MatcherAssert.assertThat
29 import org.junit.After
30 import org.junit.Before
31 import org.junit.Test
32 import org.junit.runner.RunWith
33 
34 // Fetch default locale as early as possible (e.g., before initializing LocaleTestUtilsTest class)
35 private val DEFAULT_LANGUAGE = Locale.getDefault().toString()
36 
37 @RunWith(AndroidJUnit4::class)
38 @LargeTest
39 class LocaleTestUtilsTest {
40     private val configuration: Configuration
41         get() = (ApplicationProvider.getApplicationContext() as Context).resources.configuration
42 
43     private val Configuration.language: String
44         get() = ConfigurationCompat.getLocales(this).get(0).toString()
45 
46     private lateinit var localeUtil: LocaleTestUtils
47     private var expectRtlInDefaultLanguage: Boolean = false
48 
49     @Before
50     fun setUp() {
51         localeUtil = LocaleTestUtils(ApplicationProvider.getApplicationContext() as Context)
52         determineDefaultLayoutDirection()
53     }
54 
55     @After
56     fun tearDown() {
57         localeUtil.resetLocale()
58     }
59 
60     @Test
61     fun test_defaultValues() {
62         assertDefaultValues()
63     }
64 
65     @Test
66     fun test_setAndResetLocale() {
67         assertDefaultValues()
68         localeUtil.setLocale(LocaleTestUtils.LTR_LANGUAGE)
69         assertLocaleIs(LocaleTestUtils.LTR_LANGUAGE, false)
70         localeUtil.resetLocale()
71         assertDefaultValues()
72         localeUtil.setLocale(LocaleTestUtils.RTL_LANGUAGE)
73         assertLocaleIs(LocaleTestUtils.RTL_LANGUAGE, true)
74         localeUtil.resetLocale()
75         assertDefaultValues()
76     }
77 
78     @Test
79     fun test_ltrRtlLanguagesExist() {
80         val availableLanguages = Locale.getAvailableLocales().map { it.toString() }
81         val getReason: (String, String) -> String = { name, code ->
82             "$name test language '$code' does not exist on test device"
83         }
84         assertThat(
85             getReason("Default", LocaleTestUtils.DEFAULT_TEST_LANGUAGE),
86             availableLanguages,
87             CoreMatchers.hasItem(LocaleTestUtils.DEFAULT_TEST_LANGUAGE)
88         )
89         assertThat(
90             getReason("LTR", LocaleTestUtils.LTR_LANGUAGE),
91             availableLanguages,
92             CoreMatchers.hasItem(LocaleTestUtils.LTR_LANGUAGE)
93         )
94         assertThat(
95             getReason("RTL", LocaleTestUtils.RTL_LANGUAGE),
96             availableLanguages,
97             CoreMatchers.hasItem(LocaleTestUtils.RTL_LANGUAGE)
98         )
99     }
100 
101     private fun assertDefaultValues() {
102         assertLocaleIs(DEFAULT_LANGUAGE, expectRtlInDefaultLanguage)
103     }
104 
105     private fun assertLocaleIs(lang: String, expectRtl: Boolean) {
106         assertThat("Locale should be $lang", configuration.language, CoreMatchers.equalTo(lang))
107         assertThat(
108             "Layout direction should be ${if (expectRtl) "RTL" else "LTR"}",
109             configuration.layoutDirection,
110             CoreMatchers.equalTo(
111                 if (expectRtl) View.LAYOUT_DIRECTION_RTL else View.LAYOUT_DIRECTION_LTR
112             )
113         )
114     }
115 
116     private fun determineDefaultLayoutDirection() {
117         assertThat(
118             "Locale must still be the default when determining the default layout direction",
119             configuration.language,
120             CoreMatchers.equalTo(DEFAULT_LANGUAGE)
121         )
122         expectRtlInDefaultLanguage = configuration.layoutDirection == View.LAYOUT_DIRECTION_RTL
123     }
124 }
125