Merge "[Regional preferences] Add numbering system page."

This commit is contained in:
Tom Hsu
2023-01-09 17:25:10 +00:00
committed by Android (Google) Code Review
22 changed files with 813 additions and 215 deletions

View File

@@ -98,8 +98,10 @@ public class CalendarTypeControllerTest {
private static String getDisplayKeywordValue(String value) {
String languageTag = new Locale.Builder()
.setUnicodeLocaleKeyword(
RegionalPreferencesFragment.TYPE_CALENDAR, value).build().toLanguageTag();
return ULocale.getDisplayKeywordValue(languageTag, "calendar",
ExtensionTypes.CALENDAR, value).build().toLanguageTag();
return ULocale.getDisplayKeywordValue(
languageTag,
RegionalPreferencesDataUtils.DISPLAY_KEYWORD_OF_CALENDAR,
ULocale.forLocale(Locale.getDefault(Locale.Category.FORMAT)));
}
}

View File

@@ -0,0 +1,106 @@
/*
* Copyright (C) 2023 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.content.Context;
import android.os.Bundle;
import android.os.Looper;
import androidx.test.annotation.UiThreadTest;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.testutils.ResourcesUtils;
import org.junit.Before;
import org.junit.Test;
import java.util.Locale;
public class NumberingPreferencesFragmentTest {
private Context mApplicationContext;
private NumberingPreferencesFragment mFragment;
@Before
@UiThreadTest
public void setUp() throws Exception {
if (Looper.myLooper() == null) {
Looper.prepare();
}
mApplicationContext = ApplicationProvider.getApplicationContext();
mFragment = new NumberingPreferencesFragment();
}
@Test
@UiThreadTest
public void initTitle_optionIsLanguageSelection_titleIsNumbers() {
Bundle bundle = new Bundle();
bundle.putString(RegionalPreferencesEntriesFragment.ARG_KEY_REGIONAL_PREFERENCE,
NumberingSystemItemController.ARG_VALUE_LANGUAGE_SELECT);
mFragment.setArguments(bundle);
String result = mFragment.initTitle();
assertEquals(ResourcesUtils.getResourcesString(
mApplicationContext, "numbers_preferences_title"), result);
}
@Test
@UiThreadTest
public void initTitle_optionIsNumberingSystemSelection_titleIsLocaleDisplayName() {
Locale expectedLocale = Locale.forLanguageTag("en-US");
Bundle bundle = new Bundle();
bundle.putString(RegionalPreferencesEntriesFragment.ARG_KEY_REGIONAL_PREFERENCE,
NumberingSystemItemController.ARG_VALUE_NUMBERING_SYSTEM_SELECT);
bundle.putString(
NumberingSystemItemController.KEY_SELECTED_LANGUAGE,
expectedLocale.toLanguageTag());
mFragment.setArguments(bundle);
String result = mFragment.initTitle();
assertEquals(expectedLocale.getDisplayName(expectedLocale), result);
}
@Test
@UiThreadTest
public void getMetricsCategory_optionIsLanguageSelection_resultIsLanguageSelection() {
Bundle extras = new Bundle();
extras.putString(RegionalPreferencesEntriesFragment.ARG_KEY_REGIONAL_PREFERENCE,
NumberingSystemItemController.ARG_VALUE_LANGUAGE_SELECT);
mFragment.setArguments(extras);
int result = mFragment.getMetricsCategory();
assertEquals(SettingsEnums.NUMBERING_SYSTEM_LANGUAGE_SELECTION_PREFERENCE, result);
}
@Test
@UiThreadTest
public void getMetricsCategory_optionIsNumberSelection_resultIsNumberSelection() {
Bundle extras = new Bundle();
extras.putString(RegionalPreferencesEntriesFragment.ARG_KEY_REGIONAL_PREFERENCE,
NumberingSystemItemController.ARG_VALUE_NUMBERING_SYSTEM_SELECT);
mFragment.setArguments(extras);
int result = mFragment.getMetricsCategory();
assertEquals(SettingsEnums.NUMBERING_SYSTEM_NUMBER_FORMAT_SELECTION_PREFERENCE, result);
}
}

View File

@@ -0,0 +1,83 @@
/*
* Copyright (C) 2023 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.os.LocaleList;
import androidx.test.core.app.ApplicationProvider;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.Locale;
public class NumberingSystemControllerTest {
private Context mApplicationContext;
private NumberingSystemController mController;
private LocaleList mCacheLocales;
@Before
public void setUp() throws Exception {
mApplicationContext = ApplicationProvider.getApplicationContext();
mController = new NumberingSystemController(mApplicationContext, "key");
mCacheLocales = LocaleList.getDefault();
}
@After
public void tearDown() throws Exception {
LocaleList.setDefault(mCacheLocales);
}
@Test
public void getSummary_has1Locale_showEnUs() {
LocaleList.setDefault(LocaleList.forLanguageTags("en-US"));
String summary = mController.getSummary().toString();
String expectedResult =
Locale.forLanguageTag("en-us").getDisplayName();
assertEquals(expectedResult, summary);
}
@Test
public void getSummary_has2Locales_showEnUsAndZhTw() {
LocaleList.setDefault(LocaleList.forLanguageTags("en-US,zh-TW"));
String summary = mController.getSummary().toString();
Locale locale1 = Locale.forLanguageTag("en-US");
Locale locale2 = Locale.forLanguageTag("zh-TW");
String expectedResult =
locale1.getDisplayName(locale1) + ", " + locale2.getDisplayName(locale2);
assertEquals(expectedResult, summary);
}
@Test
public void getSummary_localeHasExtensionTag_showEnUsWithoutTag() {
LocaleList.setDefault(LocaleList.forLanguageTags("en-US-u-ca-chinese"));
String summary = mController.getSummary().toString();
String expectedResult = Locale.forLanguageTag("en-US").getDisplayName();
assertEquals(expectedResult, summary);
}
}

View File

@@ -0,0 +1,146 @@
/*
* Copyright (C) 2023 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 static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import android.content.Context;
import android.os.Bundle;
import android.os.LocaleList;
import android.os.Looper;
import android.util.AndroidRuntimeException;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
import androidx.test.annotation.UiThreadTest;
import androidx.test.core.app.ApplicationProvider;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.Locale;
public class NumberingSystemItemControllerTest {
private Context mApplicationContext;
private NumberingSystemItemController mController;
private NumberingPreferencesFragment mFragment;
private PreferenceScreen mPreferenceScreen;
private LocaleList mCacheLocale;
@Before
@UiThreadTest
public void setUp() throws Exception {
if (Looper.myLooper() == null) {
Looper.prepare();
}
mApplicationContext = ApplicationProvider.getApplicationContext();
mFragment = spy(new NumberingPreferencesFragment());
PreferenceManager preferenceManager = new PreferenceManager(mApplicationContext);
mPreferenceScreen = preferenceManager.createPreferenceScreen(mApplicationContext);
mCacheLocale = LocaleList.getDefault();
}
@After
public void tearDown() {
LocaleList.setDefault(mCacheLocale);
}
@Test
@UiThreadTest
public void handlePreferenceTreeClick_languageSelect_launchFragment() {
Bundle bundle = new Bundle();
bundle.putString(RegionalPreferencesEntriesFragment.ARG_KEY_REGIONAL_PREFERENCE,
NumberingSystemItemController.ARG_VALUE_LANGUAGE_SELECT);
bundle.putString(
NumberingSystemItemController.KEY_SELECTED_LANGUAGE, Locale.US.toLanguageTag());
TickButtonPreference preference = new TickButtonPreference(mApplicationContext);
preference.setKey("I_am_the_key");
mPreferenceScreen.addPreference(preference);
mController = new NumberingSystemItemController(mApplicationContext, bundle);
mController.setParentFragment(mFragment);
mController.displayPreference(mPreferenceScreen);
boolean isCallingStartActivity = false;
try {
mController.handlePreferenceTreeClick(preference);
} catch (AndroidRuntimeException exception) {
isCallingStartActivity = true;
}
assertTrue(isCallingStartActivity);
}
@Test
@UiThreadTest
public void handlePreferenceTreeClick_numbersSelect_preferenceHasTick() {
Bundle bundle = new Bundle();
bundle.putString(RegionalPreferencesEntriesFragment.ARG_KEY_REGIONAL_PREFERENCE,
NumberingSystemItemController.ARG_VALUE_NUMBERING_SYSTEM_SELECT);
bundle.putString(
NumberingSystemItemController.KEY_SELECTED_LANGUAGE, Locale.US.toLanguageTag());
TickButtonPreference preference = new TickButtonPreference(mApplicationContext);
preference.setKey("test_key");
mPreferenceScreen.addPreference(preference);
mController = new NumberingSystemItemController(mApplicationContext, bundle);
mController.setParentFragment(mFragment);
mController.displayPreference(mPreferenceScreen);
mController.handlePreferenceTreeClick(preference);
verify(mFragment).setArguments(any());
}
@Test
@UiThreadTest
public void displayPreference_languageOptAndHas2Locale_show2Options() {
LocaleList.setDefault(LocaleList.forLanguageTags("en-US, zh-TW"));
Bundle bundle = new Bundle();
bundle.putString(RegionalPreferencesEntriesFragment.ARG_KEY_REGIONAL_PREFERENCE,
NumberingSystemItemController.ARG_VALUE_LANGUAGE_SELECT);
bundle.putString(
NumberingSystemItemController.KEY_SELECTED_LANGUAGE, Locale.US.toLanguageTag());
mController = new NumberingSystemItemController(mApplicationContext, bundle);
mController.setParentFragment(mFragment);
mController.displayPreference(mPreferenceScreen);
assertEquals(LocaleList.getDefault().size(), mPreferenceScreen.getPreferenceCount());
}
@Test
@UiThreadTest
public void displayPreference_enUsNumbersOpt_show1Option() {
LocaleList.setDefault(LocaleList.forLanguageTags("en-US, zh-TW"));
Bundle bundle = new Bundle();
bundle.putString(RegionalPreferencesEntriesFragment.ARG_KEY_REGIONAL_PREFERENCE,
NumberingSystemItemController.ARG_VALUE_NUMBERING_SYSTEM_SELECT);
bundle.putString(
NumberingSystemItemController.KEY_SELECTED_LANGUAGE, Locale.US.toLanguageTag());
mController = new NumberingSystemItemController(mApplicationContext, bundle);
mController.setParentFragment(mFragment);
mController.displayPreference(mPreferenceScreen);
// en-US only has 1 numbering system.
assertEquals(1, mPreferenceScreen.getPreferenceCount());
}
}

View File

@@ -62,7 +62,7 @@ public class RegionalPreferencesDataUtilsTest {
mApplicationContext, "und-u-mu-celsius");
String unit = RegionalPreferencesDataUtils.getDefaultUnicodeExtensionData(
mApplicationContext, RegionalPreferencesFragment.TYPE_TEMPERATURE);
mApplicationContext, ExtensionTypes.TEMPERATURE_UNIT);
assertEquals(LocalePreferences.TemperatureUnit.CELSIUS, unit);
}
@@ -74,7 +74,7 @@ public class RegionalPreferencesDataUtilsTest {
Locale.setDefault(Locale.forLanguageTag("en-US-u-mu-celsius"));
String unit = RegionalPreferencesDataUtils.getDefaultUnicodeExtensionData(
mApplicationContext, RegionalPreferencesFragment.TYPE_TEMPERATURE);
mApplicationContext, ExtensionTypes.TEMPERATURE_UNIT);
assertEquals(LocalePreferences.TemperatureUnit.CELSIUS, unit);
}
@@ -86,16 +86,16 @@ public class RegionalPreferencesDataUtilsTest {
Locale.setDefault(Locale.forLanguageTag("en-US"));
String unit = RegionalPreferencesDataUtils.getDefaultUnicodeExtensionData(
mApplicationContext, RegionalPreferencesFragment.TYPE_TEMPERATURE);
mApplicationContext, ExtensionTypes.TEMPERATURE_UNIT);
assertEquals(RegionalPreferencesFragment.TYPE_DEFAULT, unit);
assertEquals(RegionalPreferencesDataUtils.DEFAULT_VALUE, unit);
}
@Test
public void savePreference_saveCalendarIsDangi_success() {
RegionalPreferencesDataUtils.savePreference(
mApplicationContext,
RegionalPreferencesFragment.TYPE_CALENDAR,
ExtensionTypes.CALENDAR,
LocalePreferences.CalendarType.DANGI
);
String providerContent = Settings.System.getString(
@@ -103,12 +103,12 @@ public class RegionalPreferencesDataUtilsTest {
Locale locale = Locale.forLanguageTag(providerContent);
String result1 = locale.getUnicodeLocaleType(RegionalPreferencesFragment.TYPE_CALENDAR);
String result1 = locale.getUnicodeLocaleType(ExtensionTypes.CALENDAR);
assertEquals(LocalePreferences.CalendarType.DANGI, result1);
String result2 = Locale.getDefault(Locale.Category.FORMAT)
.getUnicodeLocaleType(RegionalPreferencesFragment.TYPE_CALENDAR);
.getUnicodeLocaleType(ExtensionTypes.CALENDAR);
assertEquals(LocalePreferences.CalendarType.DANGI, result2);
@@ -126,7 +126,7 @@ public class RegionalPreferencesDataUtilsTest {
@Test
public void temperatureUnitsConverter_inputDefault_resultIsDefaultString() {
String result = RegionalPreferencesDataUtils.temperatureUnitsConverter(mApplicationContext,
RegionalPreferencesFragment.TYPE_DEFAULT);
RegionalPreferencesDataUtils.DEFAULT_VALUE);
assertEquals(ResourcesUtils.getResourcesString(
mApplicationContext, "default_string_of_regional_preference"), result);
@@ -144,7 +144,7 @@ public class RegionalPreferencesDataUtilsTest {
@Test
public void dayConverter_inputDefault_resultIsDefaultString() {
String result = RegionalPreferencesDataUtils.dayConverter(mApplicationContext,
RegionalPreferencesFragment.TYPE_DEFAULT);
RegionalPreferencesDataUtils.DEFAULT_VALUE);
assertEquals(ResourcesUtils.getResourcesString(
mApplicationContext, "default_string_of_regional_preference"), result);
@@ -153,7 +153,7 @@ public class RegionalPreferencesDataUtilsTest {
@Test
public void calendarConverter_inputDefault_resultIsDefaultString() {
String result = RegionalPreferencesDataUtils.dayConverter(mApplicationContext,
RegionalPreferencesFragment.TYPE_DEFAULT);
RegionalPreferencesDataUtils.DEFAULT_VALUE);
assertEquals(ResourcesUtils.getResourcesString(
mApplicationContext, "default_string_of_regional_preference"), result);

View File

@@ -41,7 +41,7 @@ public class RegionalPreferencesFragmentTest {
@Test
@UiThreadTest
public void getMetricsCategory_typeIsCa_resultIsCalendarPreference() {
mFragment.mType = RegionalPreferencesFragment.TYPE_CALENDAR;
mFragment.mType = ExtensionTypes.CALENDAR;
int result = mFragment.getMetricsCategory();
@@ -51,7 +51,7 @@ public class RegionalPreferencesFragmentTest {
@Test
@UiThreadTest
public void getMetricsCategory_typeIsFw_resultIsFirstDayOfWeekPreference() {
mFragment.mType = RegionalPreferencesFragment.TYPE_FIRST_DAY_OF_WEEK;
mFragment.mType = ExtensionTypes.FIRST_DAY_OF_WEEK;
int result = mFragment.getMetricsCategory();
@@ -61,7 +61,7 @@ public class RegionalPreferencesFragmentTest {
@Test
@UiThreadTest
public void getMetricsCategory_typeIsMu_resultIsTemperaturePreference() {
mFragment.mType = RegionalPreferencesFragment.TYPE_TEMPERATURE;
mFragment.mType = ExtensionTypes.TEMPERATURE_UNIT;
int result = mFragment.getMetricsCategory();