fix: 引入Settings的Module

This commit is contained in:
2024-12-10 14:57:24 +08:00
parent ad8fc8731d
commit df105485bd
6934 changed files with 896168 additions and 2 deletions

View File

@@ -0,0 +1,97 @@
/*
* 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 {
RegionalPreferenceTestUtils.setSettingsProviderContent(
mApplicationContext, mCacheProviderContent);
Locale.setDefault(mCacheLocale);
}
@Test
public void getSummary_hasProviderValue_resultIsWed() {
RegionalPreferenceTestUtils.setSettingsProviderContent(mApplicationContext, "und-u-fw-wed");
String summary = mController.getSummary().toString();
assertEquals(ResourcesUtils.getResourcesString(
mApplicationContext, "wednesday_first_day_of_week"), summary);
}
@Test
public void getSummary_hasProviderValue_resultIsSat() {
RegionalPreferenceTestUtils.setSettingsProviderContent(mApplicationContext, "und-u-fw-sat");
String summary = mController.getSummary().toString();
assertEquals(ResourcesUtils.getResourcesString(
mApplicationContext, "saturday_first_day_of_week"), summary);
}
@Test
public void getSummary_noProviderValueButHasDefaultLocaleWithSubtag_resultIsSat() {
RegionalPreferenceTestUtils.setSettingsProviderContent(mApplicationContext, "");
Locale.setDefault(Locale.forLanguageTag("en-US-u-fw-sat"));
String summary = mController.getSummary().toString();
assertEquals(ResourcesUtils.getResourcesString(
mApplicationContext, "saturday_first_day_of_week"), summary);
}
@Test
public void getSummary_noProviderValueAndDefaultLocaleWithoutSubtag_resultIsdefault() {
RegionalPreferenceTestUtils.setSettingsProviderContent(mApplicationContext, "");
Locale.setDefault(Locale.forLanguageTag("en-US"));
String summary = mController.getSummary().toString();
assertEquals(ResourcesUtils.getResourcesString(
mApplicationContext, "default_string_of_regional_preference"), summary);
}
}

View File

@@ -0,0 +1,124 @@
/**
* 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 com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import android.content.Context;
import android.os.LocaleList;
import android.os.Looper;
import android.provider.Settings;
import com.android.internal.app.LocalePicker;
import com.android.settings.widget.TickButtonPreference;
import androidx.preference.PreferenceManager;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import java.util.Locale;
@RunWith(AndroidJUnit4.class)
public class FirstDayOfWeekItemListControllerTest {
private static final String KEY_PREFERENCE_CATEGORY_FIRST_DAY_OF_WEEK_ITEM =
"first_day_of_week_item_category";
private static final String KEY_PREFERENCE_FIRST_DAY_OF_WEEK_ITEM =
"first_day_of_week_item_list";
private Context mContext;
private PreferenceManager mPreferenceManager;
private PreferenceCategory mPreferenceCategory;
private PreferenceScreen mPreferenceScreen;
private FirstDayOfWeekItemListController mController;
private LocaleList mCacheLocaleList;
private Locale mCacheLocale;
private String mCacheProviderContent = "";
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
mContext = spy(ApplicationProvider.getApplicationContext());
if (Looper.myLooper() == null) {
Looper.prepare();
}
mPreferenceManager = new PreferenceManager(mContext);
mPreferenceScreen = mPreferenceManager.createPreferenceScreen(mContext);
mPreferenceCategory = new PreferenceCategory(mContext);
mPreferenceCategory.setKey(KEY_PREFERENCE_CATEGORY_FIRST_DAY_OF_WEEK_ITEM);
mPreferenceScreen.addPreference(mPreferenceCategory);
mController = new FirstDayOfWeekItemListController(mContext,
KEY_PREFERENCE_FIRST_DAY_OF_WEEK_ITEM);
mController.displayPreference(mPreferenceScreen);
mCacheProviderContent = Settings.System.getString(
mContext.getContentResolver(), Settings.System.LOCALE_PREFERENCES);
mCacheLocale = Locale.getDefault(Locale.Category.FORMAT);
mCacheLocaleList = LocaleList.getDefault();
}
@After
public void tearDown() throws Exception {
RegionalPreferenceTestUtils.setSettingsProviderContent(
mContext, mCacheProviderContent);
Locale.setDefault(mCacheLocale);
LocalePicker.updateLocales(mCacheLocaleList);
}
@Test
public void displayPreference_setSelectPreferredFirstDayOfWeekIsDefault() {
TickButtonPreference pref = (TickButtonPreference) mPreferenceCategory.getPreference(0);
pref.performClick();
String record = Settings.System.getString(
mContext.getContentResolver(), Settings.System.LOCALE_PREFERENCES);
assertThat(pref.getKey()).isEqualTo("default");
assertThat(record).contains("default");
}
@Test
public void displayPreference_setSelectPreferredFirstDayOfWeekIsSunday() {
TickButtonPreference pref = (TickButtonPreference) mPreferenceCategory.getPreference(1);
pref.performClick();
String record = Settings.System.getString(
mContext.getContentResolver(), Settings.System.LOCALE_PREFERENCES);
assertThat(pref.getKey()).isEqualTo("sun");
assertThat(record).contains("sun");
}
@Test
public void displayPreference_setSelectPreferredFirstDayOfWeekIsMonday() {
TickButtonPreference pref = (TickButtonPreference) mPreferenceCategory.getPreference(2);
pref.performClick();
String record = Settings.System.getString(
mContext.getContentResolver(), Settings.System.LOCALE_PREFERENCES);
assertThat(pref.getKey()).isEqualTo("mon");
assertThat(record).contains("mon");
}
}

View File

@@ -0,0 +1,67 @@
/*
* 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.os.Bundle;
import android.os.Looper;
import androidx.test.annotation.UiThreadTest;
import org.junit.Before;
import org.junit.Test;
public class NumberingPreferencesFragmentTest {
private NumberingPreferencesFragment mFragment;
@Before
@UiThreadTest
public void setUp() throws Exception {
if (Looper.myLooper() == null) {
Looper.prepare();
}
mFragment = new NumberingPreferencesFragment();
}
@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,61 @@
/*
* 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 com.android.settings.core.BasePreferenceController.AVAILABLE;
import static com.android.settings.core.BasePreferenceController.CONDITIONALLY_UNAVAILABLE;
import static org.junit.Assert.assertEquals;
import android.content.Context;
import android.os.LocaleList;
import androidx.test.core.app.ApplicationProvider;
import org.junit.Before;
import org.junit.Test;
public class NumberingSystemControllerTest {
private Context mApplicationContext;
private NumberingSystemController mController;
@Before
public void setUp() throws Exception {
mApplicationContext = ApplicationProvider.getApplicationContext();
}
@Test
public void getAvailabilityStatus_noLocale_unavailable() {
LocaleList.setDefault(LocaleList.forLanguageTags("en-US,zh-Hant-TW"));
mController = new NumberingSystemController(mApplicationContext, "key");
int result = mController.getAvailabilityStatus();
assertEquals(CONDITIONALLY_UNAVAILABLE, result);
}
@Test
public void getAvailabilityStatus_hasLocaleWithNumberingSystems_available() {
// ar-JO has different numbering system.
LocaleList.setDefault(LocaleList.forLanguageTags("en-US,zh-Hant-TW,ar-JO"));
mController = new NumberingSystemController(mApplicationContext, "key");
int result = mController.getAvailabilityStatus();
assertEquals(AVAILABLE, result);
}
}

View File

@@ -0,0 +1,204 @@
/*
* 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 com.google.common.truth.Truth.assertThat;
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.app.settings.SettingsEnums;
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 com.android.internal.app.LocalePicker;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.widget.TickButtonPreference;
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;
private FakeFeatureFactory mFeatureFactory;
@Before
@UiThreadTest
public void setUp() throws Exception {
if (Looper.myLooper() == null) {
Looper.prepare();
}
mApplicationContext = ApplicationProvider.getApplicationContext();
mFeatureFactory = FakeFeatureFactory.setupForTest();
mFragment = spy(new NumberingPreferencesFragment());
PreferenceManager preferenceManager = new PreferenceManager(mApplicationContext);
mPreferenceScreen = preferenceManager.createPreferenceScreen(mApplicationContext);
mCacheLocale = LocaleList.getDefault();
}
@After
public void tearDown() {
LocaleList.setDefault(mCacheLocale);
LocalePicker.updateLocales(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);
verify(mFeatureFactory.metricsFeatureProvider).action(
mApplicationContext,
SettingsEnums.ACTION_CHOOSE_LANGUAGE_FOR_NUMBERS_PREFERENCES);
}
@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());
verify(mFeatureFactory.metricsFeatureProvider).action(
mApplicationContext, SettingsEnums.ACTION_SET_NUMBERS_PREFERENCES);
}
@Test
@UiThreadTest
public void handlePreferenceTreeClick_numbersSelect_numberingSystemIsUpdated() {
LocalePicker.updateLocales(LocaleList.forLanguageTags("en-US,zh-TW,ar-BH"));
Bundle bundle = new Bundle();
bundle.putString(RegionalPreferencesEntriesFragment.ARG_KEY_REGIONAL_PREFERENCE,
NumberingSystemItemController.ARG_VALUE_NUMBERING_SYSTEM_SELECT);
bundle.putString(
NumberingSystemItemController.KEY_SELECTED_LANGUAGE, "ar-BH");
TickButtonPreference defaultPreference = new TickButtonPreference(mApplicationContext);
TickButtonPreference numberPreference = new TickButtonPreference(mApplicationContext);
defaultPreference.setKey("default");
numberPreference.setKey("latn");
mPreferenceScreen.addPreference(defaultPreference);
mPreferenceScreen.addPreference(numberPreference);
mController = new NumberingSystemItemController(mApplicationContext, bundle);
mController.setParentFragment(mFragment);
mController.displayPreference(mPreferenceScreen);
mController.handlePreferenceTreeClick(numberPreference);
assertThat(LocalePicker.getLocales().toLanguageTags()).contains(
"en-US,zh-TW,ar-BH-u-nu-latn");
}
@Test
@UiThreadTest
public void displayPreference_languageOptAndHas2LocaleWithSingleNu_showNothing() {
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(0, mPreferenceScreen.getPreferenceCount());
}
@Test
@UiThreadTest
public void displayPreference_languageOptAndHas2LocaleWithMultiNu_showLocaleWithMultiNuOnly() {
// ar-JO and dz-BT have multiple numbering systems.
LocaleList.setDefault(LocaleList.forLanguageTags("en-US,zh-TW,ar-JO,dz-BT"));
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(2, 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

@@ -0,0 +1,62 @@
/**
* 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.mockito.Mockito.anyString;
import static org.mockito.Mockito.verify;
import android.content.Context;
import android.os.Looper;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settingslib.widget.FooterPreference;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
@RunWith(AndroidJUnit4.class)
public class RegionalFooterPreferenceControllerTest {
private static String KEY_FOOTER_PREFERENCE = "regional_pref_footer";
private Context mContext;
private RegionalFooterPreferenceController mRegionalFooterPreferenceController;
@Mock
private FooterPreference mMockFooterPreference;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
if (Looper.myLooper() == null) {
Looper.prepare();
}
mContext = ApplicationProvider.getApplicationContext();
mRegionalFooterPreferenceController = new RegionalFooterPreferenceController(mContext,
KEY_FOOTER_PREFERENCE);
}
@Test
public void setupFooterPreference_shouldSetAsTextInLearnMore() {
mRegionalFooterPreferenceController.setupFooterPreference(mMockFooterPreference);
verify(mMockFooterPreference).setLearnMoreText(anyString());
}
}

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 RegionalPreferenceTestUtils {
/** 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,164 @@
/*
* 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 android.provider.Settings;
import androidx.core.text.util.LocalePreferences;
import androidx.test.core.app.ApplicationProvider;
import com.android.internal.app.LocalePicker;
import com.android.settings.testutils.ResourcesUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import java.util.Locale;
public class RegionalPreferencesDataUtilsTest {
private Context mApplicationContext;
private String mCacheProviderContent = "";
private Locale mCacheLocale;
private LocaleList mCacheLocaleList;
@Before
public void setUp() throws Exception {
mApplicationContext = ApplicationProvider.getApplicationContext();
mCacheProviderContent = Settings.System.getString(
mApplicationContext.getContentResolver(), Settings.System.LOCALE_PREFERENCES);
mCacheLocale = Locale.getDefault(Locale.Category.FORMAT);
mCacheLocaleList = LocaleList.getDefault();
}
@After
public void tearDown() throws Exception {
RegionalPreferenceTestUtils.setSettingsProviderContent(
mApplicationContext, mCacheProviderContent);
Locale.setDefault(mCacheLocale);
LocalePicker.updateLocales(mCacheLocaleList);
}
@Test
public void getDefaultUnicodeExtensionData_hasProviderValue_resultIsCelsius() {
RegionalPreferenceTestUtils.setSettingsProviderContent(
mApplicationContext, "und-u-mu-celsius");
String unit = RegionalPreferencesDataUtils.getDefaultUnicodeExtensionData(
mApplicationContext, ExtensionTypes.TEMPERATURE_UNIT);
assertEquals(LocalePreferences.TemperatureUnit.CELSIUS, unit);
}
@Test
public void getDefaultUnicodeExtensionData_hasDefaultLocaleSubtag_resultIsCelsius() {
RegionalPreferenceTestUtils.setSettingsProviderContent(
mApplicationContext, "und");
Locale.setDefault(Locale.forLanguageTag("en-US-u-mu-celsius"));
String unit = RegionalPreferencesDataUtils.getDefaultUnicodeExtensionData(
mApplicationContext, ExtensionTypes.TEMPERATURE_UNIT);
assertEquals(LocalePreferences.TemperatureUnit.CELSIUS, unit);
}
@Test
public void getDefaultUnicodeExtensionData_noSubtag_resultIsDefault() {
RegionalPreferenceTestUtils.setSettingsProviderContent(
mApplicationContext, "und");
Locale.setDefault(Locale.forLanguageTag("en-US"));
String unit = RegionalPreferencesDataUtils.getDefaultUnicodeExtensionData(
mApplicationContext, ExtensionTypes.TEMPERATURE_UNIT);
assertEquals(RegionalPreferencesDataUtils.DEFAULT_VALUE, unit);
}
@Test
@Ignore("b/265733270")
public void savePreference_saveCalendarIsDangi_success() {
RegionalPreferencesDataUtils.savePreference(
mApplicationContext,
ExtensionTypes.CALENDAR,
LocalePreferences.CalendarType.DANGI
);
String providerContent = Settings.System.getString(
mApplicationContext.getContentResolver(), Settings.System.LOCALE_PREFERENCES);
Locale locale = Locale.forLanguageTag(providerContent);
String result1 = locale.getUnicodeLocaleType(ExtensionTypes.CALENDAR);
assertEquals(LocalePreferences.CalendarType.DANGI, result1);
String result2 = Locale.getDefault(Locale.Category.FORMAT)
.getUnicodeLocaleType(ExtensionTypes.CALENDAR);
assertEquals(LocalePreferences.CalendarType.DANGI, result2);
}
@Test
public void temperatureUnitsConverter_inputFahrenheit_resultIsFahrenheitString() {
String result = RegionalPreferencesDataUtils.temperatureUnitsConverter(mApplicationContext,
LocalePreferences.TemperatureUnit.FAHRENHEIT);
assertEquals(ResourcesUtils.getResourcesString(
mApplicationContext, "fahrenheit_temperature_unit"), result);
}
@Test
public void temperatureUnitsConverter_inputDefault_resultIsDefaultString() {
String result = RegionalPreferencesDataUtils.temperatureUnitsConverter(mApplicationContext,
RegionalPreferencesDataUtils.DEFAULT_VALUE);
assertEquals(ResourcesUtils.getResourcesString(
mApplicationContext, "default_string_of_regional_preference"), result);
}
@Test
public void dayConverter_inputWed_resultIsWedString() {
String result = RegionalPreferencesDataUtils.dayConverter(mApplicationContext,
LocalePreferences.FirstDayOfWeek.WEDNESDAY);
assertEquals(ResourcesUtils.getResourcesString(
mApplicationContext, "wednesday_first_day_of_week"), result);
}
@Test
public void dayConverter_inputDefault_resultIsDefaultString() {
String result = RegionalPreferencesDataUtils.dayConverter(mApplicationContext,
RegionalPreferencesDataUtils.DEFAULT_VALUE);
assertEquals(ResourcesUtils.getResourcesString(
mApplicationContext, "default_string_of_regional_preference"), result);
}
@Test
public void calendarConverter_inputDefault_resultIsDefaultString() {
String result = RegionalPreferencesDataUtils.dayConverter(mApplicationContext,
RegionalPreferencesDataUtils.DEFAULT_VALUE);
assertEquals(ResourcesUtils.getResourcesString(
mApplicationContext, "default_string_of_regional_preference"), result);
}
}

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,99 @@
/*
* 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 {
RegionalPreferenceTestUtils.setSettingsProviderContent(
mApplicationContext, mCacheProviderContent);
Locale.setDefault(mCacheLocale);
}
@Test
public void getSummary_hasProviderValue_resultIsCelsius() {
RegionalPreferenceTestUtils.setSettingsProviderContent(
mApplicationContext, "und-u-mu-celsius");
String summary = mController.getSummary().toString();
assertEquals(ResourcesUtils.getResourcesString(
mApplicationContext, "celsius_temperature_unit"), summary);
}
@Test
public void getSummary_hasProviderValue_resultIsFahrenheit() {
RegionalPreferenceTestUtils.setSettingsProviderContent(
mApplicationContext, "und-u-mu-fahrenhe");
String summary = mController.getSummary().toString();
assertEquals(ResourcesUtils.getResourcesString(
mApplicationContext, "fahrenheit_temperature_unit"), summary);
}
@Test
public void getSummary_noProviderValueButHasDefaultLocaleWithSubtag_resultIsFahrenheit() {
RegionalPreferenceTestUtils.setSettingsProviderContent(mApplicationContext, "");
Locale.setDefault(Locale.forLanguageTag("en-US-u-mu-fahrenhe"));
String summary = mController.getSummary().toString();
assertEquals(ResourcesUtils.getResourcesString(
mApplicationContext, "fahrenheit_temperature_unit"), summary);
}
@Test
public void getSummary_noProviderValueAndDefaultLocaleWithoutSubtag_resultIsDefault() {
RegionalPreferenceTestUtils.setSettingsProviderContent(mApplicationContext, "");
Locale.setDefault(Locale.forLanguageTag("en-US"));
String summary = mController.getSummary().toString();
assertEquals(ResourcesUtils.getResourcesString(
mApplicationContext, "default_string_of_regional_preference"), summary);
}
}

View File

@@ -0,0 +1,121 @@
/**
* 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 com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import android.content.Context;
import android.os.LocaleList;
import android.os.Looper;
import android.provider.Settings;
import com.android.internal.app.LocalePicker;
import com.android.settings.widget.TickButtonPreference;
import androidx.preference.PreferenceManager;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import java.util.Locale;
@RunWith(AndroidJUnit4.class)
public class TemperatureUnitListControllerTest {
private static final String KEY_PREFERENCE_CATEGORY_TEMPERATURE_UNIT =
"temperature_unit_category";
private static final String KEY_PREFERENCE_TEMPERATURE_UNIT = "temperature_unit_list";
private Context mContext;
private PreferenceManager mPreferenceManager;
private PreferenceCategory mPreferenceCategory;
private PreferenceScreen mPreferenceScreen;
private TemperatureUnitListController mController;
private LocaleList mCacheLocaleList;
private Locale mCacheLocale;
private String mCacheProviderContent = "";
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
mContext = spy(ApplicationProvider.getApplicationContext());
if (Looper.myLooper() == null) {
Looper.prepare();
}
mPreferenceManager = new PreferenceManager(mContext);
mPreferenceScreen = mPreferenceManager.createPreferenceScreen(mContext);
mPreferenceCategory = new PreferenceCategory(mContext);
mPreferenceCategory.setKey(KEY_PREFERENCE_CATEGORY_TEMPERATURE_UNIT);
mPreferenceScreen.addPreference(mPreferenceCategory);
mController = new TemperatureUnitListController(mContext, KEY_PREFERENCE_TEMPERATURE_UNIT);
mController.displayPreference(mPreferenceScreen);
mCacheProviderContent = Settings.System.getString(
mContext.getContentResolver(), Settings.System.LOCALE_PREFERENCES);
mCacheLocale = Locale.getDefault(Locale.Category.FORMAT);
mCacheLocaleList = LocaleList.getDefault();
}
@After
public void tearDown() throws Exception {
RegionalPreferenceTestUtils.setSettingsProviderContent(
mContext, mCacheProviderContent);
Locale.setDefault(mCacheLocale);
LocalePicker.updateLocales(mCacheLocaleList);
}
@Test
public void displayPreference_setSelectPreferredTemperatureUnitIsDefault() {
TickButtonPreference pref = (TickButtonPreference) mPreferenceCategory.getPreference(0);
pref.performClick();
String record = Settings.System.getString(
mContext.getContentResolver(), Settings.System.LOCALE_PREFERENCES);
assertThat(pref.getKey()).isEqualTo("default");
assertThat(record).contains("default");
}
@Test
public void displayPreference_setSelectPreferredTemperatureUnitIsCelsius() {
TickButtonPreference pref = (TickButtonPreference) mPreferenceCategory.getPreference(1);
pref.performClick();
String record = Settings.System.getString(
mContext.getContentResolver(), Settings.System.LOCALE_PREFERENCES);
assertThat(pref.getKey()).isEqualTo("celsius");
assertThat(record).contains("celsius");
}
@Test
public void displayPreference_setSelectPreferredTemperatureUnitIsFahrenhe() {
TickButtonPreference pref = (TickButtonPreference) mPreferenceCategory.getPreference(2);
pref.performClick();
String record = Settings.System.getString(
mContext.getContentResolver(), Settings.System.LOCALE_PREFERENCES);
assertThat(pref.getKey()).isEqualTo("fahrenhe");
assertThat(record).contains("fahrenhe");
}
}