Merge "[Settings] Add test cases for App LocalePicker page" into main
This commit is contained in:
@@ -31,6 +31,7 @@ import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.core.app.NotificationCompat;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceCategory;
|
||||
@@ -62,6 +63,7 @@ public class AppLocaleAllListPreferenceController extends
|
||||
private static final String KEY_PREFERENCE_CATEGORY_ADD_LANGUAGE_ALL_SUPPORTED =
|
||||
"system_language_all_supported_category";
|
||||
|
||||
private Activity mActivity;
|
||||
@SuppressWarnings("NullAway")
|
||||
private PreferenceCategory mPreferenceCategory;
|
||||
private Set<LocaleStore.LocaleInfo> mLocaleList;
|
||||
@@ -84,12 +86,15 @@ public class AppLocaleAllListPreferenceController extends
|
||||
@SuppressWarnings("NullAway")
|
||||
public AppLocaleAllListPreferenceController(@NonNull Context context,
|
||||
@NonNull String preferenceKey, @Nullable String packageName,
|
||||
boolean isNumberingSystemMode, @NonNull LocaleStore.LocaleInfo parentLocale) {
|
||||
boolean isNumberingSystemMode, @NonNull LocaleStore.LocaleInfo parentLocale,
|
||||
@NonNull Activity activity, @NonNull AppLocaleCollector appLocaleCollector) {
|
||||
super(context, preferenceKey);
|
||||
mPackageName = packageName;
|
||||
mIsNumberingSystemMode = isNumberingSystemMode;
|
||||
mParentLocale = parentLocale;
|
||||
mIsCountryMode = mParentLocale != null;
|
||||
mActivity = activity;
|
||||
mAppLocaleCollector = appLocaleCollector;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -137,7 +142,8 @@ public class AppLocaleAllListPreferenceController extends
|
||||
setupSupportedPreference(newList, existingSupportedPreferences);
|
||||
}
|
||||
|
||||
private void setupSupportedPreference(List<LocaleStore.LocaleInfo> localeInfoList,
|
||||
@VisibleForTesting
|
||||
void setupSupportedPreference(List<LocaleStore.LocaleInfo> localeInfoList,
|
||||
Map<String, Preference> existingSupportedPreferences) {
|
||||
if (mIsNumberingSystemMode) {
|
||||
mPreferenceCategory.setTitle("");
|
||||
@@ -163,6 +169,7 @@ public class AppLocaleAllListPreferenceController extends
|
||||
// TODO: b/388199937 - Switch to correct fragment.
|
||||
Log.d(TAG, "setupPreference: mIsCountryMode = " + mIsCountryMode);
|
||||
switchFragment(mContext, locale, shouldShowAppLanguage(locale));
|
||||
mActivity.finish();
|
||||
return true;
|
||||
});
|
||||
mSupportedPreferences.put(locale.getId(), pref);
|
||||
@@ -173,7 +180,8 @@ public class AppLocaleAllListPreferenceController extends
|
||||
return AVAILABLE;
|
||||
}
|
||||
|
||||
private void switchFragment(Context context, LocaleStore.LocaleInfo localeInfo,
|
||||
@VisibleForTesting
|
||||
void switchFragment(Context context, LocaleStore.LocaleInfo localeInfo,
|
||||
boolean shouldShowAppLanguage) {
|
||||
if (shouldShowAppLanguage) {
|
||||
LocaleUtils.onLocaleSelected(mContext, localeInfo, mPackageName);
|
||||
@@ -190,10 +198,10 @@ public class AppLocaleAllListPreferenceController extends
|
||||
.setArguments(extra)
|
||||
.launch();
|
||||
}
|
||||
((Activity) mContext).finish();
|
||||
}
|
||||
|
||||
private boolean shouldShowAppLanguage(LocaleStore.LocaleInfo localeInfo) {
|
||||
@VisibleForTesting
|
||||
boolean shouldShowAppLanguage(LocaleStore.LocaleInfo localeInfo) {
|
||||
boolean isSystemLocale = localeInfo.isSystemLocale();
|
||||
boolean isRegionLocale = localeInfo.getParent() != null;
|
||||
boolean mayHaveDifferentNumberingSystem = localeInfo.hasNumberingSystems();
|
||||
|
@@ -400,13 +400,14 @@ public class AppLocalePickerFragment extends DashboardFragment implements
|
||||
mIsNumberingMode = args.getBoolean(
|
||||
RegionAndNumberingSystemPickerFragment.EXTRA_IS_NUMBERING_SYSTEM);
|
||||
|
||||
AppLocaleCollector appLocaleCollector = new AppLocaleCollector(context, mPackageName);
|
||||
mSuggestedListPreferenceController =
|
||||
new AppLocaleSuggestedListPreferenceController(context,
|
||||
KEY_PREFERENCE_APP_LOCALE_SUGGESTED_LIST, mPackageName, mIsNumberingMode,
|
||||
mLocaleInfo);
|
||||
mLocaleInfo, getActivity(), appLocaleCollector);
|
||||
mAppLocaleAllListPreferenceController = new AppLocaleAllListPreferenceController(
|
||||
context, KEY_PREFERENCE_APP_LOCALE_LIST, mPackageName, mIsNumberingMode,
|
||||
mLocaleInfo);
|
||||
mLocaleInfo, getActivity(), appLocaleCollector);
|
||||
final List<AbstractPreferenceController> controllers = new ArrayList<>();
|
||||
controllers.add(mSuggestedListPreferenceController);
|
||||
controllers.add(mAppLocaleAllListPreferenceController);
|
||||
|
@@ -26,6 +26,7 @@ import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.core.app.NotificationCompat;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceCategory;
|
||||
@@ -55,6 +56,7 @@ public class AppLocaleSuggestedListPreferenceController extends
|
||||
private static final String KEY_PREFERENCE_CATEGORY_ADD_A_LANGUAGE_SUGGESTED =
|
||||
"system_language_suggested_category";
|
||||
|
||||
private Activity mActivity;
|
||||
@SuppressWarnings("NullAway")
|
||||
private PreferenceCategory mPreferenceCategory;
|
||||
private Set<LocaleStore.LocaleInfo> mLocaleList;
|
||||
@@ -76,12 +78,15 @@ public class AppLocaleSuggestedListPreferenceController extends
|
||||
@SuppressWarnings("NullAway")
|
||||
public AppLocaleSuggestedListPreferenceController(@NonNull Context context,
|
||||
@NonNull String preferenceKey, @Nullable String packageName,
|
||||
boolean isNumberingSystemMode, @NonNull LocaleStore.LocaleInfo parentLocale) {
|
||||
boolean isNumberingSystemMode, @NonNull LocaleStore.LocaleInfo parentLocale,
|
||||
@NonNull Activity activity, @NonNull AppLocaleCollector appLocaleCollector) {
|
||||
super(context, preferenceKey);
|
||||
mPackageName = packageName;
|
||||
mIsNumberingSystemMode = isNumberingSystemMode;
|
||||
mParentLocale = parentLocale;
|
||||
mIsCountryMode = mParentLocale != null;
|
||||
mActivity = activity;
|
||||
mAppLocaleCollector = appLocaleCollector;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -92,7 +97,6 @@ public class AppLocaleSuggestedListPreferenceController extends
|
||||
? KEY_PREFERENCE_CATEGORY_ADD_A_LANGUAGE_SUGGESTED
|
||||
: KEY_PREFERENCE_CATEGORY_APP_LANGUAGE_SUGGESTED);
|
||||
|
||||
mAppLocaleCollector = new AppLocaleCollector(mContext, mPackageName);
|
||||
mSuggestedPreferences = new ArrayMap<>();
|
||||
mLocaleOptions = new ArrayList<>();
|
||||
updatePreferences();
|
||||
@@ -129,7 +133,8 @@ public class AppLocaleSuggestedListPreferenceController extends
|
||||
setupSuggestedPreference(newList, existingSuggestedPreferences);
|
||||
}
|
||||
|
||||
private void setupSuggestedPreference(List<LocaleStore.LocaleInfo> localeInfoList,
|
||||
@VisibleForTesting
|
||||
void setupSuggestedPreference(List<LocaleStore.LocaleInfo> localeInfoList,
|
||||
Map<String, Preference> existingSuggestedPreferences) {
|
||||
for (LocaleStore.LocaleInfo locale : localeInfoList) {
|
||||
if (mIsNumberingSystemMode || mIsCountryMode) {
|
||||
@@ -150,7 +155,7 @@ public class AppLocaleSuggestedListPreferenceController extends
|
||||
}
|
||||
}
|
||||
}
|
||||
Log.d(TAG, "setupSuggestedPreference, mPreferenceCategory setVisible"
|
||||
Log.d(TAG, "setupSuggestedPreference, mPreferenceCategory setVisible = "
|
||||
+ (mPreferenceCategory.getPreferenceCount() > 0));
|
||||
mPreferenceCategory.setVisible(mPreferenceCategory.getPreferenceCount() > 0);
|
||||
}
|
||||
@@ -167,7 +172,7 @@ public class AppLocaleSuggestedListPreferenceController extends
|
||||
pref.setKey(locale.toString());
|
||||
pref.setOnPreferenceClickListener(clickedPref -> {
|
||||
LocaleUtils.onLocaleSelected(mContext, locale, mPackageName);
|
||||
((Activity) mContext).finish();
|
||||
mActivity.finish();
|
||||
return true;
|
||||
});
|
||||
mSuggestedPreferences.put(locale.getId(), pref);
|
||||
|
@@ -37,6 +37,7 @@ import androidx.preference.PreferenceCategory;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.android.internal.app.AppLocaleCollector;
|
||||
import com.android.internal.app.LocaleHelper;
|
||||
import com.android.internal.app.LocaleStore;
|
||||
import com.android.settings.R;
|
||||
@@ -329,13 +330,14 @@ public class RegionAndNumberingSystemPickerFragment extends DashboardFragment im
|
||||
controllers.add(mSuggestedListPreferenceController);
|
||||
controllers.add(mSystemLocaleAllListPreferenceController);
|
||||
} else {
|
||||
AppLocaleCollector appLocaleCollector = new AppLocaleCollector(context, mPackageName);
|
||||
mAppLocaleSuggestedListPreferenceController =
|
||||
new AppLocaleSuggestedListPreferenceController(context,
|
||||
KEY_PREFERENCE_APP_LOCALE_SUGGESTED_LIST, mPackageName,
|
||||
mIsNumberingMode, mLocaleInfo);
|
||||
mIsNumberingMode, mLocaleInfo, getActivity(), appLocaleCollector);
|
||||
mAppLocaleAllListPreferenceController = new AppLocaleAllListPreferenceController(
|
||||
context, KEY_PREFERENCE_APP_LOCALE_LIST, mPackageName, mIsNumberingMode,
|
||||
mLocaleInfo);
|
||||
mLocaleInfo, getActivity(), appLocaleCollector);
|
||||
controllers.add(mAppLocaleSuggestedListPreferenceController);
|
||||
controllers.add(mAppLocaleAllListPreferenceController);
|
||||
}
|
||||
|
@@ -0,0 +1,190 @@
|
||||
/**
|
||||
* Copyright (C) 2025 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.localepicker;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.isA;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.IActivityManager;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.res.Configuration;
|
||||
import android.os.LocaleList;
|
||||
import android.os.Looper;
|
||||
import android.telephony.TelephonyManager;
|
||||
import android.util.ArrayMap;
|
||||
import android.util.Log;
|
||||
|
||||
import com.android.internal.app.AppLocaleCollector;
|
||||
import com.android.internal.app.LocaleStore;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.backup.BackupSettingsFragment;
|
||||
import com.android.settings.testutils.shadow.ShadowActivityManager;
|
||||
import com.android.settings.testutils.shadow.ShadowFragment;
|
||||
import com.android.settingslib.widget.SelectorWithWidgetPreference;
|
||||
|
||||
import androidx.fragment.app.FragmentTransaction;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceCategory;
|
||||
import androidx.preference.PreferenceFragmentCompat;
|
||||
import androidx.preference.PreferenceManager;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
import androidx.test.InstrumentationRegistry;
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.Robolectric;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.Shadows;
|
||||
import org.robolectric.shadows.ShadowTelephonyManager;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(shadows = {
|
||||
ShadowFragment.class,
|
||||
ShadowActivityManager.class,
|
||||
})
|
||||
public class AppLocaleAllListPreferenceControllerTest {
|
||||
private static final String KEY_CATEGORY_APP_SUUPPORTED_LIST =
|
||||
"app_language_all_supported_category";
|
||||
private static final String KEY_ALL_LOCALE = "app_locale_list";
|
||||
private static final String TEST_PACKAGE_NAME = "com.android.settings";
|
||||
|
||||
private Activity mActivity;
|
||||
private Context mContext;
|
||||
private PreferenceCategory mPreferenceCategory;
|
||||
private AppLocaleAllListPreferenceController mController;
|
||||
private Set<LocaleStore.LocaleInfo> mSupportedLocale;
|
||||
@Mock
|
||||
private PreferenceManager mPreferenceManager;
|
||||
@Mock
|
||||
private PreferenceScreen mPreferenceScreen;
|
||||
@Mock
|
||||
private IActivityManager mActivityService;
|
||||
@Mock
|
||||
private LocaleStore.LocaleInfo mSupportedLocaleInfo_1;
|
||||
@Mock
|
||||
private FragmentTransaction mFragmentTransaction;
|
||||
@Mock
|
||||
private SharedPreferences mSharedPreferences;
|
||||
private AppLocaleCollector mAppLocaleCollector;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mActivity = Robolectric.setupActivity(Activity.class);
|
||||
mContext = spy(ApplicationProvider.getApplicationContext());
|
||||
if (Looper.myLooper() == null) {
|
||||
Looper.prepare();
|
||||
}
|
||||
|
||||
ShadowActivityManager.setService(mActivityService);
|
||||
final Configuration config = new Configuration();
|
||||
setUpLocaleConditions();
|
||||
config.setLocales(new LocaleList(mSupportedLocaleInfo_1.getLocale()));
|
||||
when(mActivityService.getConfiguration()).thenReturn(config);
|
||||
ShadowTelephonyManager shadowTelephonyManager =
|
||||
Shadows.shadowOf(mContext.getSystemService(TelephonyManager.class));
|
||||
shadowTelephonyManager.setSimCountryIso("us");
|
||||
shadowTelephonyManager.setNetworkCountryIso("us");
|
||||
mAppLocaleCollector = spy(
|
||||
new AppLocaleCollector(InstrumentationRegistry.getContext(), TEST_PACKAGE_NAME));
|
||||
mPreferenceCategory = spy(new PreferenceCategory(mContext, null));
|
||||
when(mPreferenceScreen.getPreferenceManager()).thenReturn(mPreferenceManager);
|
||||
when(mPreferenceCategory.getPreferenceManager()).thenReturn(mPreferenceManager);
|
||||
when(mPreferenceManager.getSharedPreferences()).thenReturn(mSharedPreferences);
|
||||
when(mPreferenceScreen.findPreference(KEY_CATEGORY_APP_SUUPPORTED_LIST)).thenReturn(
|
||||
mPreferenceCategory);
|
||||
mPreferenceCategory.setKey(KEY_CATEGORY_APP_SUUPPORTED_LIST);
|
||||
mPreferenceScreen.addPreference(mPreferenceCategory);
|
||||
mController = new AppLocaleAllListPreferenceController(mContext, KEY_ALL_LOCALE,
|
||||
TEST_PACKAGE_NAME, false, null, mActivity, mAppLocaleCollector);
|
||||
}
|
||||
|
||||
private void setUpLocaleConditions() {
|
||||
when(mSupportedLocaleInfo_1.getFullNameNative()).thenReturn("English (United States)");
|
||||
when(mSupportedLocaleInfo_1.getLocale()).thenReturn(
|
||||
LocaleList.forLanguageTags("en-US").get(0));
|
||||
when(mSupportedLocaleInfo_1.getId()).thenReturn("en-US");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void displayPreference_hasSupportedPreference_categoryIsVisible() {
|
||||
mController.displayPreference(mPreferenceScreen);
|
||||
mSupportedLocale = mAppLocaleCollector.getSupportedLocaleList(null, false, false);
|
||||
List<LocaleStore.LocaleInfo> localeList = new ArrayList<>();
|
||||
localeList.addAll(
|
||||
mSupportedLocale.stream().filter(localeInfo -> !localeInfo.isSuggested()).collect(
|
||||
Collectors.toList()));
|
||||
|
||||
assertTrue(mPreferenceCategory.isVisible());
|
||||
assertThat(mPreferenceCategory.getPreferenceCount()).isEqualTo(localeList.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onPreferenceClick_shouldShowAppLanguagePage() {
|
||||
Context activityContext = mock(Context.class);
|
||||
mController = new AppLocaleAllListPreferenceController(activityContext, KEY_ALL_LOCALE,
|
||||
TEST_PACKAGE_NAME, false, null, mActivity, mAppLocaleCollector);
|
||||
when(mSupportedLocaleInfo_1.isSuggested()).thenReturn(false);
|
||||
when(mSupportedLocaleInfo_1.isSystemLocale()).thenReturn(true);
|
||||
when(mSupportedLocaleInfo_1.getParent()).thenReturn(null);
|
||||
boolean shouldShowAppLanguage = mController.shouldShowAppLanguage(mSupportedLocaleInfo_1);
|
||||
mController.switchFragment(activityContext, mSupportedLocaleInfo_1, shouldShowAppLanguage);
|
||||
|
||||
verify(mFragmentTransaction, never()).add(any(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onPreferenceClick_shouldShowRegionalPage() {
|
||||
Context activityContext = mock(Context.class);
|
||||
mController = new AppLocaleAllListPreferenceController(activityContext, KEY_ALL_LOCALE,
|
||||
TEST_PACKAGE_NAME, false, null, mActivity, mAppLocaleCollector);
|
||||
when(mSupportedLocaleInfo_1.isSuggested()).thenReturn(false);
|
||||
when(mSupportedLocaleInfo_1.isSystemLocale()).thenReturn(false);
|
||||
when(mSupportedLocaleInfo_1.getParent()).thenReturn(null);
|
||||
boolean shouldShowAppLanguage = mController.shouldShowAppLanguage(mSupportedLocaleInfo_1);
|
||||
mController.switchFragment(activityContext, mSupportedLocaleInfo_1, shouldShowAppLanguage);
|
||||
|
||||
verify(mFragmentTransaction, never()).add(any(RegionAndNumberingSystemPickerFragment.class),
|
||||
anyString());
|
||||
}
|
||||
}
|
@@ -0,0 +1,181 @@
|
||||
/**
|
||||
* Copyright (C) 2025 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.localepicker;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.IActivityManager;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.res.Configuration;
|
||||
import android.os.LocaleList;
|
||||
import android.os.Looper;
|
||||
import android.telephony.TelephonyManager;
|
||||
import android.util.ArrayMap;
|
||||
|
||||
import com.android.internal.app.AppLocaleCollector;
|
||||
import com.android.internal.app.LocaleStore;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.testutils.shadow.ShadowActivityManager;
|
||||
import com.android.settings.testutils.shadow.ShadowFragment;
|
||||
import com.android.settingslib.widget.SelectorWithWidgetPreference;
|
||||
|
||||
import androidx.fragment.app.FragmentTransaction;
|
||||
import androidx.preference.Preference;
|
||||
import androidx.preference.PreferenceCategory;
|
||||
import androidx.preference.PreferenceFragmentCompat;
|
||||
import androidx.preference.PreferenceManager;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
import androidx.test.InstrumentationRegistry;
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.Robolectric;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.Shadows;
|
||||
import org.robolectric.shadows.ShadowTelephonyManager;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(shadows = {
|
||||
ShadowFragment.class,
|
||||
ShadowActivityManager.class,
|
||||
})
|
||||
public class AppLocaleSuggestedListPreferenceControllerTest {
|
||||
private static final String KEY_CATEGORY_APP_SUGGESTED_LIST =
|
||||
"app_language_suggested_category";
|
||||
private static final String KEY_SUGGESTED = "app_locale_suggested_list";
|
||||
private static final String TEST_PACKAGE_NAME = "com.android.settings";
|
||||
|
||||
private Activity mActivity;
|
||||
private Context mContext;
|
||||
private PreferenceCategory mPreferenceCategory;
|
||||
private AppLocaleSuggestedListPreferenceController mController;
|
||||
private List<LocaleStore.LocaleInfo> mLocaleList;
|
||||
private Set<LocaleStore.LocaleInfo> mSuggestedLocale;
|
||||
private Map<String, Preference> mPreferences = new ArrayMap<>();
|
||||
private AppLocaleCollector mAppLocaleCollector;
|
||||
@Mock
|
||||
private PreferenceManager mPreferenceManager;
|
||||
@Mock
|
||||
private PreferenceScreen mPreferenceScreen;
|
||||
@Mock
|
||||
private IActivityManager mActivityService;
|
||||
@Mock
|
||||
private LocaleStore.LocaleInfo mSuggestedLocaleInfo_1;
|
||||
@Mock
|
||||
private LocaleStore.LocaleInfo mSuggestedLocaleInfo_2;
|
||||
@Mock
|
||||
private FragmentTransaction mFragmentTransaction;
|
||||
@Mock
|
||||
private SharedPreferences mSharedPreferences;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mActivity = Robolectric.setupActivity(Activity.class);
|
||||
mContext = spy(ApplicationProvider.getApplicationContext());
|
||||
if (Looper.myLooper() == null) {
|
||||
Looper.prepare();
|
||||
}
|
||||
|
||||
ShadowActivityManager.setService(mActivityService);
|
||||
final Configuration config = new Configuration();
|
||||
setUpLocaleConditions();
|
||||
config.setLocales(new LocaleList(mSuggestedLocaleInfo_1.getLocale(),
|
||||
mSuggestedLocaleInfo_2.getLocale()));
|
||||
when(mActivityService.getConfiguration()).thenReturn(config);
|
||||
ShadowTelephonyManager shadowTelephonyManager =
|
||||
Shadows.shadowOf(mContext.getSystemService(TelephonyManager.class));
|
||||
shadowTelephonyManager.setSimCountryIso("us");
|
||||
shadowTelephonyManager.setNetworkCountryIso("us");
|
||||
mAppLocaleCollector = spy(
|
||||
new AppLocaleCollector(InstrumentationRegistry.getContext(), TEST_PACKAGE_NAME));
|
||||
mPreferenceCategory = spy(new PreferenceCategory(mContext, null));
|
||||
when(mPreferenceScreen.getPreferenceManager()).thenReturn(mPreferenceManager);
|
||||
when(mPreferenceCategory.getPreferenceManager()).thenReturn(mPreferenceManager);
|
||||
when(mPreferenceManager.getSharedPreferences()).thenReturn(mSharedPreferences);
|
||||
when(mPreferenceScreen.findPreference(KEY_CATEGORY_APP_SUGGESTED_LIST)).thenReturn(
|
||||
mPreferenceCategory);
|
||||
mPreferenceCategory.setKey(KEY_CATEGORY_APP_SUGGESTED_LIST);
|
||||
mPreferenceScreen.addPreference(mPreferenceCategory);
|
||||
mController = new AppLocaleSuggestedListPreferenceController(mContext, KEY_SUGGESTED,
|
||||
TEST_PACKAGE_NAME, false, null, mActivity, mAppLocaleCollector);
|
||||
}
|
||||
|
||||
private void setUpLocaleConditions() {
|
||||
mLocaleList = new ArrayList<>();
|
||||
when(mSuggestedLocaleInfo_1.getFullNameNative()).thenReturn("English (United States)");
|
||||
when(mSuggestedLocaleInfo_1.getLocale()).thenReturn(
|
||||
LocaleList.forLanguageTags("en-US").get(0));
|
||||
when(mSuggestedLocaleInfo_1.getId()).thenReturn("en-US");
|
||||
mLocaleList.add(mSuggestedLocaleInfo_1);
|
||||
when(mSuggestedLocaleInfo_2.getFullNameNative()).thenReturn("Español (Estados Unidos)");
|
||||
when(mSuggestedLocaleInfo_2.getLocale()).thenReturn(
|
||||
LocaleList.forLanguageTags("es-US").get(0));
|
||||
when(mSuggestedLocaleInfo_2.getId()).thenReturn("es-US");
|
||||
mLocaleList.add(mSuggestedLocaleInfo_2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void displayPreference_hasSupportedPreference_categoryIsVisible() {
|
||||
mController.displayPreference(mPreferenceScreen);
|
||||
mSuggestedLocale = mAppLocaleCollector.getSupportedLocaleList(null, false, false);
|
||||
List<LocaleStore.LocaleInfo> localeList = new ArrayList<>();
|
||||
localeList.addAll(
|
||||
mSuggestedLocale.stream().filter(localeInfo -> localeInfo.isSuggested()).collect(
|
||||
Collectors.toList()));
|
||||
|
||||
assertTrue(mPreferenceCategory.isVisible());
|
||||
assertThat(mPreferenceCategory.getPreferenceCount()).isEqualTo(localeList.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onPreferenceClick_shouldShowAppLanguagePage() {
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mController = new AppLocaleSuggestedListPreferenceController(mContext, KEY_SUGGESTED,
|
||||
TEST_PACKAGE_NAME, false, null, mActivity, mAppLocaleCollector);
|
||||
mController.displayPreference(mPreferenceScreen);
|
||||
mController.setupSuggestedPreference(mLocaleList, mPreferences);
|
||||
SelectorWithWidgetPreference preference = mPreferenceCategory.findPreference(
|
||||
mSuggestedLocaleInfo_1.toString());
|
||||
preference.performClick();
|
||||
|
||||
verify(mFragmentTransaction, never()).add(any(), anyString());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user