Merge "[Regional Preference] Add each option entry"

This commit is contained in:
Tom Hsu
2022-12-30 03:20:13 +00:00
committed by Android (Google) Code Review
16 changed files with 1500 additions and 0 deletions

View File

@@ -0,0 +1,96 @@
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.regionalpreferences;
import static org.junit.Assert.assertEquals;
import android.content.Context;
import android.provider.Settings;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.testutils.ResourcesUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.Locale;
public class CalendarTypeControllerTest {
private Context mApplicationContext;
private CalendarTypeController mController;
private String mCacheProviderContent = "";
private Locale mCacheLocale;
@Before
public void setUp() throws Exception {
mApplicationContext = ApplicationProvider.getApplicationContext();
mController = new CalendarTypeController(mApplicationContext, "key");
mCacheProviderContent = Settings.System.getString(
mApplicationContext.getContentResolver(), Settings.System.LOCALE_PREFERENCES);
mCacheLocale = Locale.getDefault(Locale.Category.FORMAT);
}
@After
public void tearDown() throws Exception {
RegionalPreferenceUtils.setSettingsProviderContent(
mApplicationContext, mCacheProviderContent);
Locale.setDefault(mCacheLocale);
}
@Test
public void getSummary_hasProviderValue_resultIsChinese() {
RegionalPreferenceUtils.setSettingsProviderContent(
mApplicationContext, "und-u-ca-chinese");
CharSequence type = mController.getSummary();
assertEquals(LocalePreferences.CalendarType.CHINESE, type.toString());
}
@Test
public void getSummary_hasProviderValue_resultIsDangi() {
RegionalPreferenceUtils.setSettingsProviderContent(
mApplicationContext, "und-u-ca-dangi");
CharSequence type = mController.getSummary();
assertEquals(LocalePreferences.CalendarType.DANGI, type.toString());
}
@Test
public void getSummary_noProviderValueButHasDefaultLocaleWithSubtag_resultIsSat() {
RegionalPreferenceUtils.setSettingsProviderContent(mApplicationContext, "");
Locale.setDefault(Locale.forLanguageTag("en-US-u-ca-chinese"));
CharSequence type = mController.getSummary();
assertEquals(LocalePreferences.CalendarType.CHINESE, type.toString());
}
@Test
public void getSummary_noProviderValueAndDefaultLocaleWithoutSubtag_resultIsEmpty() {
RegionalPreferenceUtils.setSettingsProviderContent(mApplicationContext, "");
Locale.setDefault(Locale.forLanguageTag("en-US"));
CharSequence type = mController.getSummary();
assertEquals(ResourcesUtils.getResourcesString(
mApplicationContext, "default_string_of_regional_preference"), type.toString());
}
}

View File

@@ -0,0 +1,94 @@
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.regionalpreferences;
import static org.junit.Assert.assertEquals;
import android.content.Context;
import android.provider.Settings;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.testutils.ResourcesUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.Locale;
public class FirstDayOfWeekControllerTest {
private Context mApplicationContext;
private FirstDayOfWeekController mController;
private String mCacheProviderContent = "";
private Locale mCacheLocale;
@Before
public void setUp() throws Exception {
mApplicationContext = ApplicationProvider.getApplicationContext();
mController = new FirstDayOfWeekController(mApplicationContext, "key");
mCacheProviderContent = Settings.System.getString(
mApplicationContext.getContentResolver(), Settings.System.LOCALE_PREFERENCES);
mCacheLocale = Locale.getDefault(Locale.Category.FORMAT);
}
@After
public void tearDown() throws Exception {
RegionalPreferenceUtils.setSettingsProviderContent(
mApplicationContext, mCacheProviderContent);
Locale.setDefault(mCacheLocale);
}
@Test
public void getSummary_hasProviderValue_resultIsWed() {
RegionalPreferenceUtils.setSettingsProviderContent(mApplicationContext, "und-u-fw-wed");
CharSequence day = mController.getSummary();
assertEquals(LocalePreferences.FirstDayOfWeek.WEDNESDAY, day.toString());
}
@Test
public void getSummary_hasProviderValue_resultIsSat() {
RegionalPreferenceUtils.setSettingsProviderContent(mApplicationContext, "und-u-fw-sat");
CharSequence day = mController.getSummary();
assertEquals(LocalePreferences.FirstDayOfWeek.SATURDAY, day.toString());
}
@Test
public void getSummary_noProviderValueButHasDefaultLocaleWithSubtag_resultIsSat() {
RegionalPreferenceUtils.setSettingsProviderContent(mApplicationContext, "");
Locale.setDefault(Locale.forLanguageTag("en-US-u-fw-sat"));
CharSequence day = mController.getSummary();
assertEquals(LocalePreferences.FirstDayOfWeek.SATURDAY, day.toString());
}
@Test
public void getSummary_noProviderValueAndDefaultLocaleWithoutSubtag_resultIsEmpty() {
RegionalPreferenceUtils.setSettingsProviderContent(mApplicationContext, "");
Locale.setDefault(Locale.forLanguageTag("en-US"));
CharSequence day = mController.getSummary();
assertEquals(ResourcesUtils.getResourcesString(
mApplicationContext, "default_string_of_regional_preference"), day.toString());
}
}

View File

@@ -0,0 +1,29 @@
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.regionalpreferences;
import android.content.Context;
import android.provider.Settings;
/** Utils for each regional preference unit test. */
public final class RegionalPreferenceUtils {
/** Set language tag to Settings Provider */
public static void setSettingsProviderContent(Context context, String languageTag) {
Settings.System.putString(context.getContentResolver(),
Settings.System.LOCALE_PREFERENCES, languageTag);
}
}

View File

@@ -0,0 +1,84 @@
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.regionalpreferences;
import static com.android.settings.core.BasePreferenceController.AVAILABLE;
import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;
import static org.junit.Assert.assertEquals;
import android.app.UiAutomation;
import android.content.Context;
import android.os.SystemProperties;
import androidx.test.InstrumentationRegistry;
import androidx.test.core.app.ApplicationProvider;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class RegionalPreferencesControllerTest {
private boolean mCacheProperty = false;
private Context mApplicationContext;
private RegionalPreferencesController mController;
@Before
public void setUp() throws Exception {
mApplicationContext = ApplicationProvider.getApplicationContext();
mCacheProperty =
SystemProperties.getBoolean(RegionalPreferencesController.FEATURE_PROPERTY, false);
mController = new RegionalPreferencesController(mApplicationContext, "key");
}
@After
public void tearDown() throws Exception {
setProp(mCacheProperty);
}
@Test
public void getAvailabilityStatus_systemPropertyIstrue_available() throws Exception {
setProp(true);
int result = mController.getAvailabilityStatus();
assertEquals(AVAILABLE, result);
}
@Test
public void getAvailabilityStatus_systemPropertyIstrue_unavailable() throws Exception {
setProp(false);
int result = mController.getAvailabilityStatus();
assertEquals(CONDITIONALLY_UNAVAILABLE, result);
}
private static void setProp(boolean isEnabled) throws Exception {
UiAutomation uiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
uiAutomation.executeShellCommand(
"setprop " + RegionalPreferencesController.FEATURE_PROPERTY + " " + isEnabled);
for (int i = 0; i < 3; i++) {
Thread.sleep(500);
if (SystemProperties.getBoolean(
RegionalPreferencesController.FEATURE_PROPERTY, false) == isEnabled) {
break;
}
}
}
}

View File

@@ -0,0 +1,46 @@
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.regionalpreferences;
import static org.junit.Assert.assertEquals;
import android.app.settings.SettingsEnums;
import android.os.Looper;
import androidx.test.annotation.UiThreadTest;
import org.junit.Before;
import org.junit.Test;
public class RegionalPreferencesEntriesFragmentTest {
private RegionalPreferencesEntriesFragment mFragment;
@Before
@UiThreadTest
public void setUp() throws Exception {
if (Looper.myLooper() == null) {
Looper.prepare();
}
mFragment = new RegionalPreferencesEntriesFragment();
}
@Test
@UiThreadTest
public void getMetricsCategory_returnRegionalPreference() {
assertEquals(SettingsEnums.REGIONAL_PREFERENCE, mFragment.getMetricsCategory());
}
}

View File

@@ -0,0 +1,96 @@
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.regionalpreferences;
import static org.junit.Assert.assertEquals;
import android.content.Context;
import android.provider.Settings;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.testutils.ResourcesUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.Locale;
public class TemperatureUnitControllerTest {
private Context mApplicationContext;
private TemperatureUnitController mController;
private String mCacheProviderContent = "";
private Locale mCacheLocale;
@Before
public void setUp() throws Exception {
mApplicationContext = ApplicationProvider.getApplicationContext();
mController = new TemperatureUnitController(mApplicationContext, "key");
mCacheProviderContent = Settings.System.getString(
mApplicationContext.getContentResolver(), Settings.System.LOCALE_PREFERENCES);
mCacheLocale = Locale.getDefault(Locale.Category.FORMAT);
}
@After
public void tearDown() throws Exception {
RegionalPreferenceUtils.setSettingsProviderContent(
mApplicationContext, mCacheProviderContent);
Locale.setDefault(mCacheLocale);
}
@Test
public void getSummary_hasProviderValue_resultIsCelsius() {
RegionalPreferenceUtils.setSettingsProviderContent(
mApplicationContext, "und-u-mu-celsius");
CharSequence unit = mController.getSummary();
assertEquals(LocalePreferences.TemperatureUnit.CELSIUS, unit.toString());
}
@Test
public void getSummary_hasProviderValue_resultIsFahrenheit() {
RegionalPreferenceUtils.setSettingsProviderContent(
mApplicationContext, "und-u-mu-fahrenhe");
CharSequence unit = mController.getSummary();
assertEquals(LocalePreferences.TemperatureUnit.FAHRENHEIT, unit.toString());
}
@Test
public void getSummary_noProviderValueButHasDefaultLocaleWithSubtag_resultIsFahrenheit() {
RegionalPreferenceUtils.setSettingsProviderContent(mApplicationContext, "");
Locale.setDefault(Locale.forLanguageTag("en-US-u-mu-fahrenhe"));
CharSequence unit = mController.getSummary();
assertEquals(LocalePreferences.TemperatureUnit.FAHRENHEIT, unit.toString());
}
@Test
public void getSummary_noProviderValueAndDefaultLocaleWithoutSubtag_resultIsEmpty() {
RegionalPreferenceUtils.setSettingsProviderContent(mApplicationContext, "");
Locale.setDefault(Locale.forLanguageTag("en-US"));
CharSequence unit = mController.getSummary();
assertEquals(ResourcesUtils.getResourcesString(
mApplicationContext, "default_string_of_regional_preference"), unit.toString());
}
}