fix: 引入Settings的Module
This commit is contained in:
@@ -0,0 +1,606 @@
|
||||
/*
|
||||
* 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.localepicker;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.ApplicationPackageManager;
|
||||
import android.app.LocaleConfig;
|
||||
import android.app.settings.SettingsEnums;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.InstallSourceInfo;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageManager.NameNotFoundException;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.content.res.Resources;
|
||||
import android.net.Uri;
|
||||
import android.os.LocaleList;
|
||||
import android.os.Process;
|
||||
import android.os.SystemClock;
|
||||
import android.os.UserHandle;
|
||||
import android.platform.test.annotations.RequiresFlagsEnabled;
|
||||
import android.platform.test.flag.junit.CheckFlagsRule;
|
||||
import android.platform.test.flag.junit.DeviceFlagsValueProvider;
|
||||
import android.telephony.TelephonyManager;
|
||||
|
||||
import androidx.annotation.ArrayRes;
|
||||
|
||||
import com.android.internal.app.LocaleStore;
|
||||
import com.android.settings.applications.AppInfoBase;
|
||||
import com.android.settings.applications.AppLocaleUtil;
|
||||
import com.android.settings.flags.Flags;
|
||||
import com.android.settings.testutils.FakeFeatureFactory;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnit;
|
||||
import org.mockito.junit.MockitoRule;
|
||||
import org.robolectric.Robolectric;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.Shadows;
|
||||
import org.robolectric.android.controller.ActivityController;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.annotation.Implementation;
|
||||
import org.robolectric.annotation.Implements;
|
||||
import org.robolectric.shadows.ShadowPackageManager;
|
||||
import org.robolectric.shadows.ShadowTelephonyManager;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(
|
||||
shadows = {
|
||||
AppLocalePickerActivityTest.ShadowApplicationPackageManager.class,
|
||||
AppLocalePickerActivityTest.ShadowResources.class,
|
||||
})
|
||||
public class AppLocalePickerActivityTest {
|
||||
private static final String TEST_PACKAGE_NAME = "com.android.settings";
|
||||
private static final Uri TEST_PACKAGE_URI = Uri.parse("package:" + TEST_PACKAGE_NAME);
|
||||
private static final String EN_CA = "en-CA";
|
||||
private static final String EN_US = "en-US";
|
||||
private static int sUid;
|
||||
|
||||
private FakeFeatureFactory mFeatureFactory;
|
||||
private LocaleNotificationDataManager mDataManager;
|
||||
private AppLocalePickerActivity mActivity;
|
||||
|
||||
@Mock
|
||||
LocaleStore.LocaleInfo mLocaleInfo;
|
||||
@Mock
|
||||
private LocaleConfig mLocaleConfig;
|
||||
|
||||
@Rule
|
||||
public MockitoRule rule = MockitoJUnit.rule();
|
||||
@Rule
|
||||
public final CheckFlagsRule mCheckFlagsRule =
|
||||
DeviceFlagsValueProvider.createCheckFlagsRule();
|
||||
|
||||
private Context mContext;
|
||||
private ShadowPackageManager mPackageManager;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mContext = spy(RuntimeEnvironment.application);
|
||||
mPackageManager = Shadows.shadowOf(mContext.getPackageManager());
|
||||
mLocaleConfig = mock(LocaleConfig.class);
|
||||
when(mLocaleConfig.getStatus()).thenReturn(LocaleConfig.STATUS_SUCCESS);
|
||||
when(mLocaleConfig.getSupportedLocales()).thenReturn(LocaleList.forLanguageTags("en-US"));
|
||||
ReflectionHelpers.setStaticField(AppLocaleUtil.class, "sLocaleConfig", mLocaleConfig);
|
||||
sUid = Process.myUid();
|
||||
mFeatureFactory = FakeFeatureFactory.setupForTest();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
mPackageManager.removePackage(TEST_PACKAGE_NAME);
|
||||
ReflectionHelpers.setStaticField(AppLocaleUtil.class, "sLocaleConfig", null);
|
||||
ShadowResources.setDisAllowPackage(false);
|
||||
ShadowApplicationPackageManager.setNoLaunchEntry(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void launchAppLocalePickerActivity_hasPackageName_success() {
|
||||
ActivityController<TestAppLocalePickerActivity> controller =
|
||||
initActivityController(true);
|
||||
controller.create();
|
||||
|
||||
assertThat(controller.get().isFinishing()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void launchAppLocalePickerActivity_appNoLocaleConfig_failed() {
|
||||
when(mLocaleConfig.getStatus()).thenReturn(LocaleConfig.STATUS_NOT_SPECIFIED);
|
||||
|
||||
ActivityController<TestAppLocalePickerActivity> controller =
|
||||
initActivityController(true);
|
||||
controller.create();
|
||||
|
||||
assertThat(controller.get().isFinishing()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void launchAppLocalePickerActivity_appSignPlatformKey_failed() {
|
||||
final ApplicationInfo applicationInfo = new ApplicationInfo();
|
||||
applicationInfo.privateFlags |= ApplicationInfo.PRIVATE_FLAG_SIGNED_WITH_PLATFORM_KEY;
|
||||
applicationInfo.packageName = TEST_PACKAGE_NAME;
|
||||
|
||||
final PackageInfo packageInfo = new PackageInfo();
|
||||
packageInfo.packageName = TEST_PACKAGE_NAME;
|
||||
packageInfo.applicationInfo = applicationInfo;
|
||||
mPackageManager.installPackage(packageInfo);
|
||||
|
||||
ActivityController<TestAppLocalePickerActivity> controller =
|
||||
initActivityController(true);
|
||||
controller.create();
|
||||
|
||||
assertThat(controller.get().isFinishing()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void launchAppLocalePickerActivity_appMatchDisallowedPackage_failed() {
|
||||
ShadowResources.setDisAllowPackage(true);
|
||||
|
||||
ActivityController<TestAppLocalePickerActivity> controller =
|
||||
initActivityController(true);
|
||||
controller.create();
|
||||
|
||||
assertThat(controller.get().isFinishing()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void launchAppLocalePickerActivity_appNoLaunchEntry_failed() {
|
||||
ShadowApplicationPackageManager.setNoLaunchEntry(true);
|
||||
|
||||
ActivityController<TestAppLocalePickerActivity> controller =
|
||||
initActivityController(true);
|
||||
controller.create();
|
||||
|
||||
assertThat(controller.get().isFinishing()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void launchAppLocalePickerActivity_intentWithoutPackageName_failed() {
|
||||
ActivityController<TestAppLocalePickerActivity> controller =
|
||||
initActivityController(false);
|
||||
controller.create();
|
||||
|
||||
assertThat(controller.get().isFinishing()).isTrue();
|
||||
}
|
||||
|
||||
@Ignore("b/313604701")
|
||||
@Test
|
||||
public void onLocaleSelected_getLocaleNotNull_getLanguageTag() {
|
||||
ActivityController<TestAppLocalePickerActivity> controller =
|
||||
initActivityController(true);
|
||||
Locale locale = new Locale("en", "US");
|
||||
when(mLocaleInfo.getLocale()).thenReturn(locale);
|
||||
when(mLocaleInfo.isSystemLocale()).thenReturn(false);
|
||||
|
||||
controller.create();
|
||||
AppLocalePickerActivity mActivity = controller.get();
|
||||
mActivity.onLocaleSelected(mLocaleInfo);
|
||||
|
||||
verify(mLocaleInfo, times(2)).getLocale();
|
||||
assertThat(mLocaleInfo.getLocale().toLanguageTag()).isEqualTo("en-US");
|
||||
assertThat(controller.get().isFinishing()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onLocaleSelected_getLocaleNull_getEmptyLanguageTag() {
|
||||
ActivityController<TestAppLocalePickerActivity> controller =
|
||||
initActivityController(true);
|
||||
when(mLocaleInfo.getLocale()).thenReturn(null);
|
||||
when(mLocaleInfo.isSystemLocale()).thenReturn(false);
|
||||
|
||||
controller.create();
|
||||
AppLocalePickerActivity mActivity = controller.get();
|
||||
mActivity.onLocaleSelected(mLocaleInfo);
|
||||
|
||||
verify(mLocaleInfo, times(1)).getLocale();
|
||||
assertThat(controller.get().isFinishing()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onLocaleSelected_logLocaleSource() {
|
||||
ActivityController<TestAppLocalePickerActivity> controller =
|
||||
initActivityController(true);
|
||||
LocaleList.setDefault(LocaleList.forLanguageTags("ja-JP,en-CA,en-US"));
|
||||
Locale locale = new Locale("en", "US");
|
||||
when(mLocaleInfo.getLocale()).thenReturn(locale);
|
||||
when(mLocaleInfo.isSystemLocale()).thenReturn(false);
|
||||
when(mLocaleInfo.isSuggested()).thenReturn(true);
|
||||
when(mLocaleInfo.isSuggestionOfType(LocaleStore.LocaleInfo.SUGGESTION_TYPE_SIM)).thenReturn(
|
||||
true);
|
||||
when(mLocaleInfo.isSuggestionOfType(
|
||||
LocaleStore.LocaleInfo.SUGGESTION_TYPE_SYSTEM_AVAILABLE_LANGUAGE)).thenReturn(
|
||||
true);
|
||||
when(mLocaleInfo.isSuggestionOfType(
|
||||
LocaleStore.LocaleInfo.SUGGESTION_TYPE_OTHER_APP_LANGUAGE)).thenReturn(
|
||||
true);
|
||||
when(mLocaleInfo.isSuggestionOfType(
|
||||
LocaleStore.LocaleInfo.SUGGESTION_TYPE_IME_LANGUAGE)).thenReturn(
|
||||
true);
|
||||
|
||||
controller.create();
|
||||
AppLocalePickerActivity mActivity = controller.get();
|
||||
mActivity.onLocaleSelected(mLocaleInfo);
|
||||
|
||||
int localeSource = 15; // SIM_LOCALE | SYSTEM_LOCALE |IME_LOCALE|APP_LOCALE
|
||||
verify(mFeatureFactory.metricsFeatureProvider).action(
|
||||
any(), eq(SettingsEnums.ACTION_CHANGE_APP_LANGUAGE_FROM_SUGGESTED),
|
||||
eq(localeSource));
|
||||
}
|
||||
|
||||
@Test
|
||||
@RequiresFlagsEnabled(Flags.FLAG_LOCALE_NOTIFICATION_ENABLED)
|
||||
public void onLocaleSelected_evaluateNotification_simpleLocaleUpdate_localeCreatedWithUid()
|
||||
throws Exception {
|
||||
sUid = 100;
|
||||
initLocaleNotificationEnvironment();
|
||||
ActivityController<TestAppLocalePickerActivity> controller = initActivityController(true);
|
||||
controller.create();
|
||||
AppLocalePickerActivity mActivity = controller.get();
|
||||
LocaleNotificationDataManager dataManager =
|
||||
NotificationController.getInstance(mActivity).getDataManager();
|
||||
|
||||
mActivity.onLocaleSelected(mLocaleInfo);
|
||||
|
||||
// Notification is not triggered.
|
||||
// In the sharedpreference, en-US's uid list contains uid1 and the notificationCount
|
||||
// equals 0.
|
||||
NotificationInfo info = dataManager.getNotificationInfo(EN_US);
|
||||
assertThat(info.getUidCollection().contains(sUid)).isTrue();
|
||||
assertThat(info.getNotificationCount()).isEqualTo(0);
|
||||
assertThat(info.getDismissCount()).isEqualTo(0);
|
||||
assertThat(info.getLastNotificationTimeMs()).isEqualTo(0);
|
||||
|
||||
mDataManager.clearLocaleNotificationMap();
|
||||
}
|
||||
|
||||
@Test
|
||||
@RequiresFlagsEnabled(Flags.FLAG_LOCALE_NOTIFICATION_ENABLED)
|
||||
public void onLocaleSelected_evaluateNotification_twoLocaleUpdate_triggerNotification()
|
||||
throws Exception {
|
||||
// App with uid 101 changed its locale from System to en-US.
|
||||
sUid = 101;
|
||||
initLocaleNotificationEnvironment();
|
||||
// Initialize the proto to contain en-US locale. Its uid list includes 100.
|
||||
Set<Integer> uidSet = Set.of(100);
|
||||
initSharedPreference(EN_US, uidSet, 0, 0, 0, 0);
|
||||
|
||||
mActivity.onLocaleSelected(mLocaleInfo);
|
||||
|
||||
// Notification is triggered.
|
||||
// In the proto file, en-US's uid list contains 101, the notificationCount equals 1, and
|
||||
// LastNotificationTime > 0.
|
||||
NotificationInfo info = mDataManager.getNotificationInfo(EN_US);
|
||||
assertThat(info.getUidCollection()).contains(sUid);
|
||||
assertThat(info.getNotificationCount()).isEqualTo(1);
|
||||
assertThat(info.getDismissCount()).isEqualTo(0);
|
||||
assertThat(info.getLastNotificationTimeMs()).isNotEqualTo(0);
|
||||
verify(mFeatureFactory.metricsFeatureProvider).action(
|
||||
any(), eq(SettingsEnums.ACTION_NOTIFICATION_FOR_SYSTEM_LOCALE));
|
||||
|
||||
mDataManager.clearLocaleNotificationMap();
|
||||
}
|
||||
|
||||
@Test
|
||||
@RequiresFlagsEnabled(Flags.FLAG_LOCALE_NOTIFICATION_ENABLED)
|
||||
public void onLocaleSelected_evaluateNotification_oddLocaleUpdate_uidAddedWithoutNotification()
|
||||
throws Exception {
|
||||
// App with uid 102 changed its locale from System to en-US.
|
||||
sUid = 102;
|
||||
initLocaleNotificationEnvironment();
|
||||
// Initialize the proto to include en-US locale. Its uid list includes 100,101 and
|
||||
// the notification count equals 1.
|
||||
int notificationId = (int) SystemClock.uptimeMillis();
|
||||
Set<Integer> uidSet = Set.of(100, 101);
|
||||
initSharedPreference(EN_US, uidSet, 0, 1,
|
||||
Calendar.getInstance().getTimeInMillis(), notificationId);
|
||||
|
||||
mActivity.onLocaleSelected(mLocaleInfo);
|
||||
|
||||
// Notification is not triggered because count % 2 != 0.
|
||||
// In the proto file, en-US's uid list contains 102, the notificationCount equals 1, and
|
||||
// LastNotificationTime > 0.
|
||||
NotificationInfo info = mDataManager.getNotificationInfo(EN_US);
|
||||
assertThat(info.getUidCollection()).contains(sUid);
|
||||
assertThat(info.getNotificationCount()).isEqualTo(1);
|
||||
assertThat(info.getDismissCount()).isEqualTo(0);
|
||||
assertThat(info.getLastNotificationTimeMs()).isNotEqualTo(0);
|
||||
assertThat(info.getNotificationId()).isEqualTo(notificationId);
|
||||
|
||||
mDataManager.clearLocaleNotificationMap();
|
||||
}
|
||||
|
||||
@Test
|
||||
@RequiresFlagsEnabled(Flags.FLAG_LOCALE_NOTIFICATION_ENABLED)
|
||||
public void onLocaleSelected_evaluateNotification_frequentLocaleUpdate_uidAddedNoNotification()
|
||||
throws Exception {
|
||||
// App with uid 103 changed its locale from System to en-US.
|
||||
sUid = 103;
|
||||
initLocaleNotificationEnvironment();
|
||||
// Initialize the proto to include en-US locale. Its uid list includes 100,101,102 and
|
||||
// the notification count equals 1.
|
||||
int notificationId = (int) SystemClock.uptimeMillis();
|
||||
Set<Integer> uidSet = Set.of(100, 101, 102);
|
||||
initSharedPreference(EN_US, uidSet, 0, 1,
|
||||
Calendar.getInstance().getTimeInMillis(), notificationId);
|
||||
|
||||
mActivity.onLocaleSelected(mLocaleInfo);
|
||||
|
||||
// Notification is not triggered because the duration is less than the threshold.
|
||||
// In the proto file, en-US's uid list contains 103, the notificationCount equals 1, and
|
||||
// LastNotificationTime > 0.
|
||||
NotificationInfo info = mDataManager.getNotificationInfo(EN_US);
|
||||
assertThat(info.getUidCollection()).contains(sUid);
|
||||
assertThat(info.getNotificationCount()).isEqualTo(1);
|
||||
assertThat(info.getDismissCount()).isEqualTo(0);
|
||||
assertThat(info.getLastNotificationTimeMs()).isNotEqualTo(0);
|
||||
assertThat(info.getNotificationId()).isEqualTo(notificationId);
|
||||
|
||||
mDataManager.clearLocaleNotificationMap();
|
||||
}
|
||||
|
||||
@Test
|
||||
@RequiresFlagsEnabled(Flags.FLAG_LOCALE_NOTIFICATION_ENABLED)
|
||||
public void onLocaleSelected_evaluateNotification_2ndOddLocaleUpdate_uidAddedNoNotification()
|
||||
throws Exception {
|
||||
// App with uid 104 changed its locale from System to en-US.
|
||||
sUid = 104;
|
||||
initLocaleNotificationEnvironment();
|
||||
|
||||
// Initialize the proto to include en-US locale. Its uid list includes 100,101,102,103 and
|
||||
// the notification count equals 1.
|
||||
int notificationId = (int) SystemClock.uptimeMillis();
|
||||
Set<Integer> uidSet = Set.of(100, 101, 102, 103);
|
||||
initSharedPreference(EN_US, uidSet, 0, 1, Calendar.getInstance().getTimeInMillis(),
|
||||
notificationId);
|
||||
|
||||
mActivity.onLocaleSelected(mLocaleInfo);
|
||||
|
||||
// Notification is not triggered because uid count % 2 != 0
|
||||
// In the proto file, en-US's uid list contains uid4, the notificationCount equals 1, and
|
||||
// LastNotificationTime > 0.
|
||||
NotificationInfo info = mDataManager.getNotificationInfo(EN_US);
|
||||
assertThat(info.getUidCollection()).contains(sUid);
|
||||
assertThat(info.getNotificationCount()).isEqualTo(1);
|
||||
assertThat(info.getDismissCount()).isEqualTo(0);
|
||||
assertThat(info.getLastNotificationTimeMs()).isNotEqualTo(0);
|
||||
|
||||
mDataManager.clearLocaleNotificationMap();
|
||||
}
|
||||
|
||||
@Test
|
||||
@RequiresFlagsEnabled(Flags.FLAG_LOCALE_NOTIFICATION_ENABLED)
|
||||
public void testEvaluateLocaleNotification_evenLocaleUpdate_trigger2ndNotification()
|
||||
throws Exception {
|
||||
sUid = 105;
|
||||
initLocaleNotificationEnvironment();
|
||||
|
||||
// Initialize the proto to include en-US locale. Its uid list includes 100,101,102,103,104
|
||||
// and the notification count equals 1.
|
||||
// Eight days later, App with uid 105 changed its locale from System to en-US
|
||||
int notificationId = (int) SystemClock.uptimeMillis();
|
||||
Set<Integer> uidSet = Set.of(100, 101, 102, 103, 104);
|
||||
Calendar now = Calendar.getInstance();
|
||||
now.add(Calendar.DAY_OF_MONTH, -8); // Set the lastNotificationTime to eight days ago.
|
||||
long lastNotificationTime = now.getTimeInMillis();
|
||||
initSharedPreference(EN_US, uidSet, 0, 1, lastNotificationTime, notificationId);
|
||||
|
||||
mActivity.onLocaleSelected(mLocaleInfo);
|
||||
|
||||
// Notification is triggered.
|
||||
// In the proto file, en-US's uid list contains 105, the notificationCount equals 2, and
|
||||
// LastNotificationTime is updated.
|
||||
NotificationInfo info = mDataManager.getNotificationInfo(EN_US);
|
||||
assertThat(info.getUidCollection()).contains(sUid);
|
||||
assertThat(info.getNotificationCount()).isEqualTo(2);
|
||||
assertThat(info.getDismissCount()).isEqualTo(0);
|
||||
assertThat(info.getLastNotificationTimeMs()).isGreaterThan(lastNotificationTime);
|
||||
|
||||
mDataManager.clearLocaleNotificationMap();
|
||||
}
|
||||
|
||||
@Test
|
||||
@RequiresFlagsEnabled(Flags.FLAG_LOCALE_NOTIFICATION_ENABLED)
|
||||
public void testEvaluateLocaleNotification_localeUpdateReachThreshold_noUidNorNotification()
|
||||
throws Exception {
|
||||
// App with uid 106 changed its locale from System to en-US.
|
||||
sUid = 106;
|
||||
initLocaleNotificationEnvironment();
|
||||
// Initialize the proto to include en-US locale. Its uid list includes
|
||||
// 100,101,102,103,104,105 and the notification count equals 2.
|
||||
int notificationId = (int) SystemClock.uptimeMillis();
|
||||
Set<Integer> uidSet = Set.of(100, 101, 102, 103, 104, 105);
|
||||
Calendar now = Calendar.getInstance();
|
||||
now.add(Calendar.DAY_OF_MONTH, -8);
|
||||
long lastNotificationTime = now.getTimeInMillis();
|
||||
initSharedPreference(EN_US, uidSet, 0, 2, lastNotificationTime, notificationId);
|
||||
|
||||
mActivity.onLocaleSelected(mLocaleInfo);
|
||||
|
||||
// Notification is not triggered because the notification count threshold, 2, is reached.
|
||||
// In the proto file, en-US's uid list contains 106, the notificationCount equals 2, and
|
||||
// LastNotificationTime > 0.
|
||||
NotificationInfo info = mDataManager.getNotificationInfo(EN_US);
|
||||
assertThat(info.getUidCollection().contains(sUid)).isFalse();
|
||||
assertThat(info.getNotificationCount()).isEqualTo(2);
|
||||
assertThat(info.getDismissCount()).isEqualTo(0);
|
||||
assertThat(info.getLastNotificationTimeMs()).isEqualTo(lastNotificationTime);
|
||||
|
||||
mDataManager.clearLocaleNotificationMap();
|
||||
}
|
||||
|
||||
@Test
|
||||
@RequiresFlagsEnabled(Flags.FLAG_LOCALE_NOTIFICATION_ENABLED)
|
||||
public void testEvaluateLocaleNotification_appChangedLocales_newLocaleCreated()
|
||||
throws Exception {
|
||||
sUid = 100;
|
||||
initLocaleNotificationEnvironment();
|
||||
// App with uid 100 changed its locale from en-US to ja-JP.
|
||||
Locale locale = Locale.forLanguageTag("ja-JP");
|
||||
when(mLocaleInfo.getLocale()).thenReturn(locale);
|
||||
// Initialize the proto to include en-US locale. Its uid list includes
|
||||
// 100,101,102,103,104,105,106 and the notification count equals 2.
|
||||
int notificationId = (int) SystemClock.uptimeMillis();
|
||||
Set<Integer> uidSet = Set.of(100, 101, 102, 103, 104, 105, 106);
|
||||
Calendar now = Calendar.getInstance();
|
||||
now.add(Calendar.DAY_OF_MONTH, -8);
|
||||
initSharedPreference(EN_US, uidSet, 0, 2, now.getTimeInMillis(),
|
||||
notificationId);
|
||||
|
||||
mActivity.onLocaleSelected(mLocaleInfo);
|
||||
|
||||
// Notification is not triggered
|
||||
// In the proto file, a map for ja-JP is created. Its uid list contains uid1.
|
||||
NotificationInfo info = mDataManager.getNotificationInfo("ja-JP");
|
||||
assertThat(info.getUidCollection()).contains(sUid);
|
||||
assertThat(info.getNotificationCount()).isEqualTo(0);
|
||||
assertThat(info.getDismissCount()).isEqualTo(0);
|
||||
assertThat(info.getLastNotificationTimeMs()).isEqualTo(0);
|
||||
|
||||
mDataManager.clearLocaleNotificationMap();
|
||||
}
|
||||
|
||||
private void initLocaleNotificationEnvironment() throws Exception {
|
||||
LocaleList.setDefault(LocaleList.forLanguageTags(EN_CA));
|
||||
|
||||
Locale locale = Locale.forLanguageTag("en-US");
|
||||
when(mLocaleInfo.getLocale()).thenReturn(locale);
|
||||
when(mLocaleInfo.isSystemLocale()).thenReturn(false);
|
||||
when(mLocaleInfo.isAppCurrentLocale()).thenReturn(false);
|
||||
|
||||
ActivityController<TestAppLocalePickerActivity> controller = initActivityController(true);
|
||||
controller.create();
|
||||
mActivity = controller.get();
|
||||
mDataManager = NotificationController.getInstance(mActivity).getDataManager();
|
||||
}
|
||||
|
||||
private void initSharedPreference(String locale, Set<Integer> uidSet, int dismissCount,
|
||||
int notificationCount, long lastNotificationTime, int notificationId)
|
||||
throws Exception {
|
||||
NotificationInfo info = new NotificationInfo(uidSet, notificationCount, dismissCount,
|
||||
lastNotificationTime, notificationId);
|
||||
mDataManager.putNotificationInfo(locale, info);
|
||||
}
|
||||
|
||||
private ActivityController<TestAppLocalePickerActivity> initActivityController(
|
||||
boolean hasPackageName) {
|
||||
Intent data = new Intent();
|
||||
if (hasPackageName) {
|
||||
data.setData(TEST_PACKAGE_URI);
|
||||
}
|
||||
data.putExtra(AppInfoBase.ARG_PACKAGE_UID, sUid);
|
||||
ActivityController<TestAppLocalePickerActivity> activityController =
|
||||
Robolectric.buildActivity(TestAppLocalePickerActivity.class, data);
|
||||
Activity activity = activityController.get();
|
||||
|
||||
ShadowTelephonyManager shadowTelephonyManager = Shadows.shadowOf(
|
||||
activity.getSystemService(TelephonyManager.class));
|
||||
shadowTelephonyManager.setSimCountryIso("US");
|
||||
shadowTelephonyManager.setNetworkCountryIso("US");
|
||||
|
||||
return activityController;
|
||||
}
|
||||
|
||||
private static class TestAppLocalePickerActivity extends AppLocalePickerActivity {
|
||||
@Override
|
||||
public Context createContextAsUser(UserHandle user, int flags) {
|
||||
// return the current context as a work profile
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@Implements(ApplicationPackageManager.class)
|
||||
public static class ShadowApplicationPackageManager extends
|
||||
org.robolectric.shadows.ShadowApplicationPackageManager {
|
||||
private static boolean sNoLaunchEntry = false;
|
||||
|
||||
@Implementation
|
||||
protected Object getInstallSourceInfo(String packageName) {
|
||||
return new InstallSourceInfo("", null, null, "");
|
||||
}
|
||||
|
||||
@Implementation
|
||||
protected List<ResolveInfo> queryIntentActivities(Intent intent, int flags) {
|
||||
if (sNoLaunchEntry) {
|
||||
return new ArrayList();
|
||||
} else {
|
||||
return super.queryIntentActivities(intent, flags);
|
||||
}
|
||||
}
|
||||
|
||||
private static void setNoLaunchEntry(boolean noLaunchEntry) {
|
||||
sNoLaunchEntry = noLaunchEntry;
|
||||
}
|
||||
|
||||
@Implementation
|
||||
protected ApplicationInfo getApplicationInfo(String packageName, int flags)
|
||||
throws NameNotFoundException {
|
||||
if (packageName.equals(TEST_PACKAGE_NAME)) {
|
||||
ApplicationInfo applicationInfo = new ApplicationInfo();
|
||||
applicationInfo.packageName = TEST_PACKAGE_NAME;
|
||||
applicationInfo.uid = sUid;
|
||||
return applicationInfo;
|
||||
} else {
|
||||
return super.getApplicationInfo(packageName, flags);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Implements(Resources.class)
|
||||
public static class ShadowResources extends
|
||||
org.robolectric.shadows.ShadowResources {
|
||||
private static boolean sDisAllowPackage = false;
|
||||
|
||||
@Implementation
|
||||
public String[] getStringArray(@ArrayRes int id) {
|
||||
if (sDisAllowPackage) {
|
||||
return new String[]{TEST_PACKAGE_NAME};
|
||||
} else {
|
||||
return new String[0];
|
||||
}
|
||||
}
|
||||
|
||||
private static void setDisAllowPackage(boolean disAllowPackage) {
|
||||
sDisAllowPackage = disAllowPackage;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
/*
|
||||
* 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.localepicker;
|
||||
|
||||
import static com.android.settings.localepicker.LocaleDialogFragment.ARG_DIALOG_TYPE;
|
||||
import static com.android.settings.localepicker.LocaleDialogFragment.ARG_TARGET_LOCALE;
|
||||
import static com.android.settings.localepicker.LocaleDialogFragment.DIALOG_CONFIRM_SYSTEM_DEFAULT;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.window.OnBackInvokedDispatcher;
|
||||
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
import androidx.fragment.app.FragmentTransaction;
|
||||
|
||||
import com.android.internal.app.LocaleStore;
|
||||
import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
|
||||
|
||||
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.annotation.Config;
|
||||
import org.robolectric.annotation.LooperMode;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(shadows = {ShadowAlertDialogCompat.class})
|
||||
@LooperMode(LooperMode.Mode.LEGACY)
|
||||
public class LocaleDialogFragmentTest {
|
||||
|
||||
@Mock
|
||||
private OnBackInvokedDispatcher mOnBackInvokedDispatcher;
|
||||
|
||||
private FragmentActivity mActivity;
|
||||
private LocaleDialogFragment mDialogFragment;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
mActivity = Robolectric.setupActivity(FragmentActivity.class);
|
||||
mDialogFragment = LocaleDialogFragment.newInstance();
|
||||
LocaleStore.LocaleInfo localeInfo = LocaleStore.getLocaleInfo(Locale.ENGLISH);
|
||||
Bundle args = new Bundle();
|
||||
args.putInt(ARG_DIALOG_TYPE, DIALOG_CONFIRM_SYSTEM_DEFAULT);
|
||||
args.putSerializable(ARG_TARGET_LOCALE, localeInfo);
|
||||
mDialogFragment.setArguments(args);
|
||||
FragmentManager fragmentManager = mActivity.getSupportFragmentManager();
|
||||
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
|
||||
fragmentTransaction.add(mDialogFragment, null);
|
||||
fragmentTransaction.commit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onCreateDialog_onBackInvokedCallbackIsRegistered() {
|
||||
mDialogFragment.setBackDispatcher(mOnBackInvokedDispatcher);
|
||||
mDialogFragment.onCreateDialog(null);
|
||||
|
||||
verify(mOnBackInvokedDispatcher).registerOnBackInvokedCallback(
|
||||
eq(OnBackInvokedDispatcher.PRIORITY_DEFAULT), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onBackInvoked_dialogIsStillDisplaying() {
|
||||
mDialogFragment.setBackDispatcher(mOnBackInvokedDispatcher);
|
||||
AlertDialog alertDialog = (AlertDialog) mDialogFragment.onCreateDialog(null);
|
||||
alertDialog.show();
|
||||
assertThat(alertDialog).isNotNull();
|
||||
assertThat(alertDialog.isShowing()).isTrue();
|
||||
|
||||
mOnBackInvokedDispatcher.registerOnBackInvokedCallback(
|
||||
eq(OnBackInvokedDispatcher.PRIORITY_DEFAULT), any());
|
||||
|
||||
mDialogFragment.getBackInvokedCallback().onBackInvoked();
|
||||
|
||||
assertThat(alertDialog.isShowing()).isTrue();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,493 @@
|
||||
/*
|
||||
* Copyright (C) 2017 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.android.settings.localepicker.AppLocalePickerActivity.EXTRA_APP_LOCALE;
|
||||
import static com.android.settings.localepicker.LocaleDialogFragment.DIALOG_ADD_SYSTEM_LOCALE;
|
||||
import static com.android.settings.localepicker.LocaleListEditor.EXTRA_SYSTEM_LOCALE_DIALOG_TYPE;
|
||||
import static com.android.settings.localepicker.LocaleListEditor.LOCALE_SUGGESTION;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.anyBoolean;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
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.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.res.Configuration;
|
||||
import android.content.res.Resources;
|
||||
import android.os.Bundle;
|
||||
import android.os.LocaleList;
|
||||
import android.platform.test.annotations.RequiresFlagsEnabled;
|
||||
import android.platform.test.flag.junit.CheckFlagsRule;
|
||||
import android.platform.test.flag.junit.DeviceFlagsValueProvider;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
import androidx.fragment.app.FragmentManager;
|
||||
import androidx.fragment.app.FragmentTransaction;
|
||||
|
||||
import com.android.internal.app.LocaleStore;
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.flags.Flags;
|
||||
import com.android.settings.testutils.FakeFeatureFactory;
|
||||
import com.android.settings.testutils.shadow.ShadowActivityManager;
|
||||
import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
|
||||
import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
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.annotation.Config;
|
||||
import org.robolectric.annotation.LooperMode;
|
||||
import org.robolectric.util.ReflectionHelpers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(shadows = {
|
||||
ShadowAlertDialogCompat.class,
|
||||
ShadowActivityManager.class,
|
||||
com.android.settings.testutils.shadow.ShadowFragment.class,
|
||||
})
|
||||
@LooperMode(LooperMode.Mode.LEGACY)
|
||||
public class LocaleListEditorTest {
|
||||
|
||||
private static final String ARG_DIALOG_TYPE = "arg_dialog_type";
|
||||
private static final String TAG_DIALOG_CONFIRM_SYSTEM_DEFAULT = "dialog_confirm_system_default";
|
||||
private static final String TAG_DIALOG_NOT_AVAILABLE = "dialog_not_available_locale";
|
||||
private static final String TAG_DIALOG_ADD_SYSTEM_LOCALE = "dialog_add_system_locale";
|
||||
private static final int DIALOG_CONFIRM_SYSTEM_DEFAULT = 1;
|
||||
private static final int REQUEST_CONFIRM_SYSTEM_DEFAULT = 1;
|
||||
|
||||
private LocaleListEditor mLocaleListEditor;
|
||||
private Context mContext;
|
||||
private FragmentActivity mActivity;
|
||||
private List<LocaleStore.LocaleInfo> mLocaleList;
|
||||
private Intent mIntent = new Intent();
|
||||
private LocaleDragCell mLocaleDragCell;
|
||||
private LocaleDragAndDropAdapter.CustomViewHolder mCustomViewHolder;
|
||||
|
||||
@Mock
|
||||
private LocaleDragAndDropAdapter mAdapter;
|
||||
@Mock
|
||||
private View mAddLanguage;
|
||||
@Mock
|
||||
private Resources mResources;
|
||||
@Mock
|
||||
private LocaleStore.LocaleInfo mLocaleInfo;
|
||||
@Mock
|
||||
private FragmentManager mFragmentManager;
|
||||
@Mock
|
||||
private FragmentTransaction mFragmentTransaction;
|
||||
@Mock
|
||||
private View mView;
|
||||
@Mock
|
||||
private IActivityManager mActivityService;
|
||||
@Mock
|
||||
private MetricsFeatureProvider mMetricsFeatureProvider;
|
||||
@Mock
|
||||
private TextView mLabel;
|
||||
@Mock
|
||||
private CheckBox mCheckbox;
|
||||
@Mock
|
||||
private TextView mMiniLabel;
|
||||
@Mock
|
||||
private TextView mLocalized;
|
||||
@Mock
|
||||
private TextView mCurrentDefault;
|
||||
@Mock
|
||||
private ImageView mDragHandle;
|
||||
@Mock
|
||||
private NotificationController mNotificationController;
|
||||
|
||||
@Rule
|
||||
public final CheckFlagsRule mCheckFlagsRule =
|
||||
DeviceFlagsValueProvider.createCheckFlagsRule();
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = spy(RuntimeEnvironment.application);
|
||||
mLocaleListEditor = spy(new LocaleListEditor());
|
||||
when(mLocaleListEditor.getContext()).thenReturn(mContext);
|
||||
mActivity = spy(Robolectric.buildActivity(FragmentActivity.class).get());
|
||||
when(mLocaleListEditor.getActivity()).thenReturn(mActivity);
|
||||
when(mLocaleListEditor.getNotificationController()).thenReturn(
|
||||
mNotificationController);
|
||||
ReflectionHelpers.setField(mLocaleListEditor, "mEmptyTextView",
|
||||
new TextView(RuntimeEnvironment.application));
|
||||
ReflectionHelpers.setField(mLocaleListEditor, "mRestrictionsManager",
|
||||
RuntimeEnvironment.application.getSystemService(Context.RESTRICTIONS_SERVICE));
|
||||
ReflectionHelpers.setField(mLocaleListEditor, "mUserManager",
|
||||
RuntimeEnvironment.application.getSystemService(Context.USER_SERVICE));
|
||||
ReflectionHelpers.setField(mLocaleListEditor, "mAdapter", mAdapter);
|
||||
ReflectionHelpers.setField(mLocaleListEditor, "mAddLanguage", mAddLanguage);
|
||||
ReflectionHelpers.setField(mLocaleListEditor, "mFragmentManager", mFragmentManager);
|
||||
ReflectionHelpers.setField(mLocaleListEditor, "mMetricsFeatureProvider",
|
||||
mMetricsFeatureProvider);
|
||||
when(mFragmentManager.beginTransaction()).thenReturn(mFragmentTransaction);
|
||||
FakeFeatureFactory.setupForTest();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
ReflectionHelpers.setField(mLocaleListEditor, "mRemoveMode", false);
|
||||
ReflectionHelpers.setField(mLocaleListEditor, "mShowingRemoveDialog", false);
|
||||
ReflectionHelpers.setField(mLocaleListEditor, "mLocaleAdditionMode", false);
|
||||
ShadowAlertDialogCompat.reset();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDisallowConfigLocale_unrestrict() {
|
||||
ReflectionHelpers.setField(mLocaleListEditor, "mIsUiRestricted", true);
|
||||
mLocaleListEditor.onAttach(mContext);
|
||||
mLocaleListEditor.onResume();
|
||||
assertThat(mLocaleListEditor.getEmptyTextView().getVisibility()).isEqualTo(View.GONE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDisallowConfigLocale_restrict() {
|
||||
ReflectionHelpers.setField(mLocaleListEditor, "mIsUiRestricted", false);
|
||||
mLocaleListEditor.onAttach(mContext);
|
||||
mLocaleListEditor.onResume();
|
||||
assertThat(mLocaleListEditor.getEmptyTextView().getVisibility()).isEqualTo(View.VISIBLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void showRemoveLocaleWarningDialog_allLocaleSelected_shouldShowErrorDialog() {
|
||||
//pre-condition
|
||||
when(mAdapter.getCheckedCount()).thenReturn(1);
|
||||
when(mAdapter.getItemCount()).thenReturn(1);
|
||||
when(mAdapter.isFirstLocaleChecked()).thenReturn(true);
|
||||
ReflectionHelpers.setField(mLocaleListEditor, "mRemoveMode", true);
|
||||
ReflectionHelpers.setField(mLocaleListEditor, "mShowingRemoveDialog", true);
|
||||
|
||||
//launch dialog
|
||||
mLocaleListEditor.showRemoveLocaleWarningDialog();
|
||||
|
||||
final AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
|
||||
|
||||
assertThat(dialog).isNotNull();
|
||||
|
||||
final ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
|
||||
|
||||
assertThat(shadowDialog.getTitle()).isEqualTo(
|
||||
mContext.getString(R.string.dlg_remove_locales_error_title));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void showRemoveLocaleWarningDialog_mainLocaleSelected_shouldShowLocaleChangeDialog() {
|
||||
//pre-condition
|
||||
when(mAdapter.getCheckedCount()).thenReturn(1);
|
||||
when(mAdapter.getItemCount()).thenReturn(2);
|
||||
when(mAdapter.isFirstLocaleChecked()).thenReturn(true);
|
||||
ReflectionHelpers.setField(mLocaleListEditor, "mRemoveMode", true);
|
||||
ReflectionHelpers.setField(mLocaleListEditor, "mShowingRemoveDialog", true);
|
||||
|
||||
//launch dialog
|
||||
mLocaleListEditor.showRemoveLocaleWarningDialog();
|
||||
|
||||
final AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
|
||||
|
||||
assertThat(dialog).isNotNull();
|
||||
|
||||
final ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
|
||||
|
||||
assertThat(shadowDialog.getMessage()).isEqualTo(
|
||||
mContext.getString(R.string.dlg_remove_locales_message));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void showRemoveLocaleWarningDialog_mainLocaleNotSelected_shouldShowConfirmDialog() {
|
||||
//pre-condition
|
||||
when(mAdapter.getCheckedCount()).thenReturn(1);
|
||||
when(mAdapter.getItemCount()).thenReturn(2);
|
||||
when(mAdapter.isFirstLocaleChecked()).thenReturn(false);
|
||||
ReflectionHelpers.setField(mLocaleListEditor, "mRemoveMode", true);
|
||||
ReflectionHelpers.setField(mLocaleListEditor, "mShowingRemoveDialog", true);
|
||||
|
||||
//launch dialog
|
||||
mLocaleListEditor.showRemoveLocaleWarningDialog();
|
||||
|
||||
final AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
|
||||
|
||||
assertThat(dialog).isNotNull();
|
||||
|
||||
final ShadowAlertDialogCompat shadowDialog = ShadowAlertDialogCompat.shadowOf(dialog);
|
||||
|
||||
assertThat(shadowDialog.getMessage()).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void showConfirmDialog_systemLocaleSelected_shouldShowLocaleChangeDialog()
|
||||
throws Exception {
|
||||
//pre-condition
|
||||
setUpLocaleConditions();
|
||||
final Configuration config = new Configuration();
|
||||
config.setLocales((LocaleList.forLanguageTags("zh-TW,en-US")));
|
||||
when(mActivityService.getConfiguration()).thenReturn(config);
|
||||
when(mAdapter.getFeedItemList()).thenReturn(mLocaleList);
|
||||
when(mAdapter.getCheckedCount()).thenReturn(1);
|
||||
when(mAdapter.getItemCount()).thenReturn(2);
|
||||
when(mAdapter.isFirstLocaleChecked()).thenReturn(true);
|
||||
ReflectionHelpers.setField(mLocaleListEditor, "mRemoveMode", true);
|
||||
ReflectionHelpers.setField(mLocaleListEditor, "mShowingRemoveDialog", true);
|
||||
|
||||
//launch the first dialog
|
||||
mLocaleListEditor.showRemoveLocaleWarningDialog();
|
||||
|
||||
final AlertDialog dialog = ShadowAlertDialogCompat.getLatestAlertDialog();
|
||||
|
||||
assertThat(dialog).isNotNull();
|
||||
|
||||
// click the remove button
|
||||
dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick();
|
||||
|
||||
assertThat(dialog.isShowing()).isFalse();
|
||||
|
||||
// check the second dialog is showing
|
||||
verify(mFragmentTransaction).add(any(LocaleDialogFragment.class),
|
||||
eq(TAG_DIALOG_CONFIRM_SYSTEM_DEFAULT));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mayAppendUnicodeTags_appendUnicodeTags_success() {
|
||||
LocaleStore.LocaleInfo localeInfo = LocaleStore.fromLocale(Locale.forLanguageTag("en-US"));
|
||||
|
||||
LocaleStore.LocaleInfo result =
|
||||
LocaleListEditor.mayAppendUnicodeTags(localeInfo, "und-u-fw-wed-mu-celsius");
|
||||
|
||||
assertThat(result.getLocale().getUnicodeLocaleType("fw")).isEqualTo("wed");
|
||||
assertThat(result.getLocale().getUnicodeLocaleType("mu")).isEqualTo("celsius");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onActivityResult_ResultCodeIsOk_showNotAvailableDialog() {
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putInt(ARG_DIALOG_TYPE, DIALOG_CONFIRM_SYSTEM_DEFAULT);
|
||||
mIntent.putExtras(bundle);
|
||||
setUpLocaleConditions();
|
||||
mLocaleListEditor.onActivityResult(REQUEST_CONFIRM_SYSTEM_DEFAULT, Activity.RESULT_OK,
|
||||
mIntent);
|
||||
|
||||
verify(mFragmentTransaction).add(any(LocaleDialogFragment.class),
|
||||
eq(TAG_DIALOG_NOT_AVAILABLE));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onActivityResult_ResultCodeIsCancel_notifyAdapterListChanged() {
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putInt(ARG_DIALOG_TYPE, DIALOG_CONFIRM_SYSTEM_DEFAULT);
|
||||
mIntent.putExtras(bundle);
|
||||
setUpLocaleConditions();
|
||||
mLocaleListEditor.onActivityResult(REQUEST_CONFIRM_SYSTEM_DEFAULT, Activity.RESULT_CANCELED,
|
||||
mIntent);
|
||||
|
||||
verify(mAdapter).notifyListChanged(mLocaleInfo);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onTouch_dragDifferentLocaleToTop_showConfirmDialog() throws Exception {
|
||||
MotionEvent event = MotionEvent.obtain(0L, 0L, MotionEvent.ACTION_UP, 0.0f, 0.0f, 0);
|
||||
setUpLocaleConditions();
|
||||
final Configuration config = new Configuration();
|
||||
config.setLocales((LocaleList.forLanguageTags("zh-TW,en-US")));
|
||||
when(mActivityService.getConfiguration()).thenReturn(config);
|
||||
when(mAdapter.getFeedItemList()).thenReturn(mLocaleList);
|
||||
mLocaleListEditor.onTouch(mView, event);
|
||||
|
||||
verify(mFragmentTransaction).add(any(LocaleDialogFragment.class),
|
||||
eq(TAG_DIALOG_CONFIRM_SYSTEM_DEFAULT));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onTouch_dragSameLocaleToTop_updateAdapter() throws Exception {
|
||||
MotionEvent event = MotionEvent.obtain(0L, 0L, MotionEvent.ACTION_UP, 0.0f, 0.0f, 0);
|
||||
setUpLocaleConditions();
|
||||
final Configuration config = new Configuration();
|
||||
config.setLocales((LocaleList.forLanguageTags("en-US,zh-TW")));
|
||||
when(mActivityService.getConfiguration()).thenReturn(config);
|
||||
when(mAdapter.getFeedItemList()).thenReturn(mLocaleList);
|
||||
mLocaleListEditor.onTouch(mView, event);
|
||||
|
||||
verify(mAdapter).doTheUpdate();
|
||||
}
|
||||
|
||||
@Test
|
||||
@RequiresFlagsEnabled(Flags.FLAG_LOCALE_NOTIFICATION_ENABLED)
|
||||
public void showDiallogForAddedLocale_showConfirmDialog() {
|
||||
initIntentAndResourceForLocaleDialog();
|
||||
mLocaleListEditor.onViewStateRestored(null);
|
||||
|
||||
verify(mFragmentTransaction).add(any(LocaleDialogFragment.class),
|
||||
eq(TAG_DIALOG_ADD_SYSTEM_LOCALE));
|
||||
}
|
||||
|
||||
@Test
|
||||
@RequiresFlagsEnabled(Flags.FLAG_LOCALE_NOTIFICATION_ENABLED)
|
||||
public void showDiallogForAddedLocale_clickAdd() {
|
||||
initIntentAndResourceForLocaleDialog();
|
||||
mLocaleListEditor.onViewStateRestored(null);
|
||||
LocaleStore.LocaleInfo info = LocaleStore.fromLocale(Locale.forLanguageTag("en-US"));
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putInt(ARG_DIALOG_TYPE, DIALOG_ADD_SYSTEM_LOCALE);
|
||||
bundle.putSerializable(LocaleDialogFragment.ARG_TARGET_LOCALE, info);
|
||||
Intent intent = new Intent().putExtras(bundle);
|
||||
|
||||
mLocaleListEditor.onActivityResult(DIALOG_ADD_SYSTEM_LOCALE, Activity.RESULT_OK, intent);
|
||||
|
||||
verify(mAdapter).addLocale(any(LocaleStore.LocaleInfo.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@RequiresFlagsEnabled(Flags.FLAG_LOCALE_NOTIFICATION_ENABLED)
|
||||
public void showDiallogForAddedLocale_clickCancel() {
|
||||
initIntentAndResourceForLocaleDialog();
|
||||
mLocaleListEditor.onViewStateRestored(null);
|
||||
LocaleStore.LocaleInfo info = LocaleStore.fromLocale(Locale.forLanguageTag("en-US"));
|
||||
Bundle bundle = new Bundle();
|
||||
bundle.putInt(ARG_DIALOG_TYPE, DIALOG_ADD_SYSTEM_LOCALE);
|
||||
bundle.putSerializable(LocaleDialogFragment.ARG_TARGET_LOCALE, info);
|
||||
Intent intent = new Intent().putExtras(bundle);
|
||||
|
||||
mLocaleListEditor.onActivityResult(DIALOG_ADD_SYSTEM_LOCALE, Activity.RESULT_CANCELED,
|
||||
intent);
|
||||
|
||||
verify(mAdapter, never()).addLocale(any(LocaleStore.LocaleInfo.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@RequiresFlagsEnabled(Flags.FLAG_LOCALE_NOTIFICATION_ENABLED)
|
||||
public void showDiallogForAddedLocale_invalidLocale_noDialog() {
|
||||
Intent intent = new Intent("ACTION")
|
||||
.putExtra(EXTRA_APP_LOCALE, "ab-CD") // invalid locale
|
||||
.putExtra(EXTRA_SYSTEM_LOCALE_DIALOG_TYPE, LOCALE_SUGGESTION);
|
||||
mActivity.setIntent(intent);
|
||||
|
||||
mLocaleListEditor.onViewStateRestored(null);
|
||||
|
||||
verify(mFragmentTransaction, never()).add(any(LocaleDialogFragment.class),
|
||||
eq(TAG_DIALOG_ADD_SYSTEM_LOCALE));
|
||||
}
|
||||
|
||||
@Test
|
||||
@RequiresFlagsEnabled(Flags.FLAG_LOCALE_NOTIFICATION_ENABLED)
|
||||
public void showDiallogForAddedLocale_noDialogType_noDialog() {
|
||||
Intent intent = new Intent("ACTION")
|
||||
.putExtra(EXTRA_APP_LOCALE, "ja-JP");
|
||||
// no EXTRA_SYSTEM_LOCALE_DIALOG_TYPE in the extra
|
||||
mActivity.setIntent(intent);
|
||||
|
||||
mLocaleListEditor.onViewStateRestored(null);
|
||||
|
||||
verify(mFragmentTransaction, never()).add(any(LocaleDialogFragment.class),
|
||||
eq(TAG_DIALOG_ADD_SYSTEM_LOCALE));
|
||||
}
|
||||
|
||||
@Test
|
||||
@RequiresFlagsEnabled(Flags.FLAG_LOCALE_NOTIFICATION_ENABLED)
|
||||
public void showDiallogForAddedLocale_inSystemLocale_noDialog() {
|
||||
LocaleList.setDefault(LocaleList.forLanguageTags("en-US,ar-AE-u-nu-arab"));
|
||||
Intent intent = new Intent("ACTION")
|
||||
.putExtra(EXTRA_APP_LOCALE, "ar-AE")
|
||||
.putExtra(EXTRA_SYSTEM_LOCALE_DIALOG_TYPE, LOCALE_SUGGESTION);
|
||||
mActivity.setIntent(intent);
|
||||
|
||||
mLocaleListEditor.onViewStateRestored(null);
|
||||
|
||||
verify(mFragmentTransaction, never()).add(any(LocaleDialogFragment.class),
|
||||
eq(TAG_DIALOG_ADD_SYSTEM_LOCALE));
|
||||
}
|
||||
|
||||
private void initIntentAndResourceForLocaleDialog() {
|
||||
Intent intent = new Intent("ACTION")
|
||||
.putExtra(EXTRA_APP_LOCALE, "ja-JP")
|
||||
.putExtra(EXTRA_SYSTEM_LOCALE_DIALOG_TYPE, LOCALE_SUGGESTION);
|
||||
|
||||
mActivity.setIntent(intent);
|
||||
String[] supportedLocales = new String[]{"en-US", "ja-JP"};
|
||||
View contentView = LayoutInflater.from(mActivity).inflate(R.layout.locale_dialog, null);
|
||||
doReturn(contentView).when(mLocaleListEditor).getLocaleDialogView();
|
||||
when(mLocaleListEditor.getSupportedLocales()).thenReturn(supportedLocales);
|
||||
when(mContext.getPackageName()).thenReturn("com.android.settings");
|
||||
when(mActivity.getCallingPackage()).thenReturn("com.android.settings");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onBindViewHolder_shouldSetCheckedBoxText() {
|
||||
ReflectionHelpers.setField(mLocaleListEditor, "mRemoveMode", true);
|
||||
mLocaleList = new ArrayList<>();
|
||||
mLocaleList.add(mLocaleInfo);
|
||||
when(mLocaleInfo.getFullNameNative()).thenReturn("English");
|
||||
when(mLocaleInfo.getLocale()).thenReturn(LocaleList.forLanguageTags("en-US").get(0));
|
||||
|
||||
mAdapter = spy(new LocaleDragAndDropAdapter(mLocaleListEditor, mLocaleList));
|
||||
ReflectionHelpers.setField(mAdapter, "mFeedItemList", mLocaleList);
|
||||
ReflectionHelpers.setField(mAdapter, "mCacheItemList", new ArrayList<>(mLocaleList));
|
||||
ReflectionHelpers.setField(mAdapter, "mContext", mContext);
|
||||
ViewGroup view = new FrameLayout(mContext);
|
||||
mCustomViewHolder = mAdapter.onCreateViewHolder(view, 0);
|
||||
mLocaleDragCell = new LocaleDragCell(mContext, null);
|
||||
ReflectionHelpers.setField(mCustomViewHolder, "mLocaleDragCell", mLocaleDragCell);
|
||||
ReflectionHelpers.setField(mLocaleDragCell, "mLabel", mLabel);
|
||||
ReflectionHelpers.setField(mLocaleDragCell, "mLocalized", mLocalized);
|
||||
ReflectionHelpers.setField(mLocaleDragCell, "mCurrentDefault", mCurrentDefault);
|
||||
ReflectionHelpers.setField(mLocaleDragCell, "mMiniLabel", mMiniLabel);
|
||||
ReflectionHelpers.setField(mLocaleDragCell, "mDragHandle", mDragHandle);
|
||||
ReflectionHelpers.setField(mLocaleDragCell, "mCheckbox", mCheckbox);
|
||||
|
||||
mAdapter.onBindViewHolder(mCustomViewHolder, 0);
|
||||
|
||||
verify(mAdapter).setCheckBoxDescription(any(LocaleDragCell.class), any(), anyBoolean());
|
||||
}
|
||||
|
||||
private void setUpLocaleConditions() {
|
||||
ShadowActivityManager.setService(mActivityService);
|
||||
mLocaleList = new ArrayList<>();
|
||||
mLocaleList.add(mLocaleInfo);
|
||||
when(mLocaleInfo.getFullNameNative()).thenReturn("English");
|
||||
when(mLocaleInfo.getLocale()).thenReturn(LocaleList.forLanguageTags("en-US").get(0));
|
||||
when(mAdapter.getFeedItemList()).thenReturn(mLocaleList);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* 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.localepicker;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class LocaleNotificationDataManagerTest {
|
||||
private Context mContext;
|
||||
private LocaleNotificationDataManager mDataManager;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = spy(RuntimeEnvironment.application);
|
||||
mDataManager = new LocaleNotificationDataManager(mContext);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
mDataManager.clearLocaleNotificationMap();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPutGetNotificationInfo() {
|
||||
String locale = "en-US";
|
||||
Set<Integer> uidSet = Set.of(101);
|
||||
NotificationInfo info = new NotificationInfo(uidSet, 1, 1, 100L, 1000);
|
||||
|
||||
mDataManager.putNotificationInfo(locale, info);
|
||||
NotificationInfo expected = mDataManager.getNotificationInfo(locale);
|
||||
|
||||
assertThat(info.equals(expected)).isTrue();
|
||||
assertThat(expected.getNotificationId()).isEqualTo(info.getNotificationId());
|
||||
assertThat(expected.getDismissCount()).isEqualTo(info.getDismissCount());
|
||||
assertThat(expected.getNotificationCount()).isEqualTo(info.getNotificationCount());
|
||||
assertThat(expected.getUidCollection()).isEqualTo(info.getUidCollection());
|
||||
assertThat(expected.getLastNotificationTimeMs()).isEqualTo(
|
||||
info.getLastNotificationTimeMs());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveNotificationInfo() {
|
||||
String locale = "en-US";
|
||||
Set<Integer> uidSet = Set.of(101);
|
||||
NotificationInfo info = new NotificationInfo(uidSet, 1, 1, 100L, 1000);
|
||||
|
||||
mDataManager.putNotificationInfo(locale, info);
|
||||
assertThat(mDataManager.getNotificationInfo(locale)).isEqualTo(info);
|
||||
mDataManager.removeNotificationInfo(locale);
|
||||
assertThat(mDataManager.getNotificationInfo(locale)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNotificationMap() {
|
||||
String enUS = "en-US";
|
||||
Set<Integer> uidSet1 = Set.of(101, 102);
|
||||
NotificationInfo info1 = new NotificationInfo(uidSet1, 1, 1, 1000L, 1234);
|
||||
String jaJP = "ja-JP";
|
||||
Set<Integer> uidSet2 = Set.of(103, 104);
|
||||
NotificationInfo info2 = new NotificationInfo(uidSet2, 1, 0, 2000L, 5678);
|
||||
mDataManager.putNotificationInfo(enUS, info1);
|
||||
mDataManager.putNotificationInfo(jaJP, info2);
|
||||
|
||||
Map<String, NotificationInfo> map = mDataManager.getLocaleNotificationInfoMap();
|
||||
|
||||
assertThat(map.size()).isEqualTo(2);
|
||||
assertThat(mDataManager.getNotificationInfo(enUS).equals(map.get(enUS))).isTrue();
|
||||
assertThat(mDataManager.getNotificationInfo(jaJP).equals(map.get(jaJP))).isTrue();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.android.settings.localepicker;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
import android.app.Activity;
|
||||
|
||||
import com.android.internal.app.LocaleStore;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.Robolectric;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.Shadows;
|
||||
import org.robolectric.android.controller.ActivityController;
|
||||
import org.robolectric.shadows.ShadowActivity;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class LocalePickerWithRegionActivityTest {
|
||||
|
||||
private LocalePickerWithRegionActivity mActivity;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
final ActivityController<LocalePickerWithRegionActivity> mActivityController =
|
||||
Robolectric.buildActivity(LocalePickerWithRegionActivity.class);
|
||||
mActivity = spy(mActivityController.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onLocaleSelected_resultShouldBeOK() {
|
||||
final ShadowActivity shadowActivity = Shadows.shadowOf(mActivity);
|
||||
mActivity.onLocaleSelected(mock(LocaleStore.LocaleInfo.class));
|
||||
|
||||
assertEquals(Activity.RESULT_OK, shadowActivity.getResultCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void onLocaleSelected_localeInfoShouldBeSentBack() {
|
||||
final ShadowActivity shadowActivity = Shadows.shadowOf(mActivity);
|
||||
mActivity.onLocaleSelected(mock(LocaleStore.LocaleInfo.class));
|
||||
|
||||
assertNotNull(shadowActivity.getResultIntent().getSerializableExtra(
|
||||
LocaleListEditor.INTENT_LOCALE_KEY));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* 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.localepicker;
|
||||
|
||||
import static com.android.settings.localepicker.AppLocalePickerActivity.EXTRA_APP_LOCALE;
|
||||
import static com.android.settings.localepicker.AppLocalePickerActivity.EXTRA_NOTIFICATION_ID;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import android.app.settings.SettingsEnums;
|
||||
import android.content.Intent;
|
||||
|
||||
import androidx.activity.result.ActivityResultLauncher;
|
||||
|
||||
import com.android.settings.testutils.FakeFeatureFactory;
|
||||
|
||||
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.android.controller.ActivityController;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class NotificationActionActivityTest {
|
||||
private NotificationActionActivity mNotificationActivity;
|
||||
private ActivityController<NotificationActionActivity> mActivityController;
|
||||
private FakeFeatureFactory mFeatureFactory;
|
||||
@Mock
|
||||
private NotificationController mNotificationController;
|
||||
@Mock
|
||||
private ActivityResultLauncher<Intent> mLauncher;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mFeatureFactory = FakeFeatureFactory.setupForTest();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnCreate_launchSystemLanguageSettings() throws Exception {
|
||||
String targetLocale = "ja-JP";
|
||||
int notificationId = 123;
|
||||
Intent intent = new Intent()
|
||||
.putExtra(EXTRA_APP_LOCALE, targetLocale)
|
||||
.putExtra(EXTRA_NOTIFICATION_ID, notificationId);
|
||||
|
||||
mActivityController = Robolectric.buildActivity(NotificationActionActivity.class, intent);
|
||||
mNotificationActivity = spy(mActivityController.get());
|
||||
doReturn(mNotificationController).when(mNotificationActivity).getNotificationController(
|
||||
any());
|
||||
doReturn(notificationId).when(mNotificationController).getNotificationId(eq(targetLocale));
|
||||
doReturn(mLauncher).when(mNotificationActivity).getLauncher();
|
||||
|
||||
mNotificationActivity.onCreate(null);
|
||||
|
||||
verify(mLauncher).launch(any(Intent.class));
|
||||
verify(mFeatureFactory.metricsFeatureProvider).action(
|
||||
any(), eq(SettingsEnums.ACTION_NOTIFICATION_CLICK_FOR_SYSTEM_LOCALE));
|
||||
verify(mNotificationActivity).finish();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* 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.localepicker;
|
||||
|
||||
import static com.android.settings.localepicker.AppLocalePickerActivity.EXTRA_APP_LOCALE;
|
||||
import static com.android.settings.localepicker.AppLocalePickerActivity.EXTRA_NOTIFICATION_ID;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.app.settings.SettingsEnums;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
||||
import com.android.settings.testutils.FakeFeatureFactory;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class NotificationCancelReceiverTest {
|
||||
private Context mContext;
|
||||
private NotificationCancelReceiver mReceiver;
|
||||
@Mock
|
||||
private NotificationController mNotificationController;
|
||||
private FakeFeatureFactory mFeatureFactory;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mReceiver = spy(new NotificationCancelReceiver());
|
||||
mFeatureFactory = FakeFeatureFactory.setupForTest();
|
||||
doReturn(mNotificationController).when(mReceiver).getNotificationController(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnReceive_incrementDismissCount() {
|
||||
String locale = "en-US";
|
||||
int notificationId = 100;
|
||||
Intent intent = new Intent()
|
||||
.putExtra(EXTRA_APP_LOCALE, locale)
|
||||
.putExtra(EXTRA_NOTIFICATION_ID, notificationId);
|
||||
when(mNotificationController.getNotificationId(locale)).thenReturn(notificationId);
|
||||
|
||||
mReceiver.onReceive(mContext, intent);
|
||||
|
||||
verify(mNotificationController).incrementDismissCount(eq(locale));
|
||||
verify(mFeatureFactory.metricsFeatureProvider).action(
|
||||
any(), eq(SettingsEnums.ACTION_NOTIFICATION_SWIPE_FOR_SYSTEM_LOCALE));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
/*
|
||||
* 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.localepicker;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.LocaleList;
|
||||
import android.os.SystemClock;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Set;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class NotificationControllerTest {
|
||||
private Context mContext;
|
||||
private LocaleNotificationDataManager mDataManager;
|
||||
private NotificationController mNotificationController;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mNotificationController = NotificationController.getInstance(mContext);
|
||||
mDataManager = mNotificationController.getDataManager();
|
||||
LocaleList.setDefault(LocaleList.forLanguageTags("en-CA"));
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
mDataManager.clearLocaleNotificationMap();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void incrementDismissCount_addOne() throws Exception {
|
||||
String enUS = "en-US";
|
||||
Set<Integer> uidSet = Set.of(100, 101);
|
||||
long lastNotificationTime = Calendar.getInstance().getTimeInMillis();
|
||||
int id = (int) SystemClock.uptimeMillis();
|
||||
initSharedPreference(enUS, uidSet, 0, 1, lastNotificationTime, id);
|
||||
|
||||
mNotificationController.incrementDismissCount(enUS);
|
||||
NotificationInfo result = mDataManager.getNotificationInfo(enUS);
|
||||
|
||||
assertThat(result.getDismissCount()).isEqualTo(1); // dismissCount increments
|
||||
assertThat(result.getUidCollection()).isEqualTo(uidSet);
|
||||
assertThat(result.getNotificationCount()).isEqualTo(1);
|
||||
assertThat(result.getLastNotificationTimeMs()).isEqualTo(lastNotificationTime);
|
||||
assertThat(result.getNotificationId()).isEqualTo(id);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRemoveNotificationInfo_removed() throws Exception {
|
||||
String enUS = "en-US";
|
||||
Set<Integer> uidSet = Set.of(100, 101);
|
||||
long lastNotificationTime = Calendar.getInstance().getTimeInMillis();
|
||||
int id = (int) SystemClock.uptimeMillis();
|
||||
initSharedPreference(enUS, uidSet, 0, 1, lastNotificationTime, id);
|
||||
|
||||
mNotificationController.removeNotificationInfo(enUS);
|
||||
|
||||
assertThat(mDataManager.getNotificationInfo(enUS)).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShouldTriggerNotification_inSystemLocale_returnFalse() throws Exception {
|
||||
int uid = 102;
|
||||
// As checking whether app's locales exist in system locales, both app locales and system
|
||||
// locales have to remove the u extension first when doing the comparison. The following
|
||||
// three locales are all in the system locale after removing the u extension so it's
|
||||
// unnecessary to trigger a notification for the suggestion.
|
||||
String locale1 = "en-CA";
|
||||
String locale2 = "ar-JO-u-nu-latn";
|
||||
String locale3 = "ar-JO";
|
||||
|
||||
LocaleList.setDefault(
|
||||
LocaleList.forLanguageTags("en-CA-u-mu-fahrenhe,ar-JO-u-mu-fahrenhe-nu-latn"));
|
||||
|
||||
assertThat(mNotificationController.shouldTriggerNotification(uid, locale1)).isFalse();
|
||||
assertThat(mNotificationController.shouldTriggerNotification(uid, locale2)).isFalse();
|
||||
assertThat(mNotificationController.shouldTriggerNotification(uid, locale3)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShouldTriggerNotification_noNotification_returnFalse() throws Exception {
|
||||
int uid = 100;
|
||||
String locale = "en-US";
|
||||
|
||||
boolean triggered = mNotificationController.shouldTriggerNotification(uid, locale);
|
||||
|
||||
assertThat(triggered).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShouldTriggerNotification_return1stTrue() throws Exception {
|
||||
// Initialze proto with en-US locale. Its uid contains 100.
|
||||
Set<Integer> uidSet = Set.of(100);
|
||||
String locale = "en-US";
|
||||
long lastNotificationTime = 0L;
|
||||
int notificationId = 0;
|
||||
initSharedPreference(locale, uidSet, 0, 1, lastNotificationTime, notificationId);
|
||||
|
||||
// When the second app is configured to "en-US", the notification is triggered.
|
||||
int uid = 101;
|
||||
boolean triggered = mNotificationController.shouldTriggerNotification(uid, locale);
|
||||
|
||||
assertThat(triggered).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShouldTriggerNotification_returnFalse_dueToOddCount() throws Exception {
|
||||
// Initialze proto with en-US locale. Its uid contains 100,101.
|
||||
Set<Integer> uidSet = Set.of(100, 101);
|
||||
String locale = "en-US";
|
||||
long lastNotificationTime = Calendar.getInstance().getTimeInMillis();
|
||||
int id = (int) SystemClock.uptimeMillis();
|
||||
initSharedPreference(locale, uidSet, 0, 1, lastNotificationTime, id);
|
||||
|
||||
// When the other app is configured to "en-US", the notification is not triggered because
|
||||
// the app count is odd.
|
||||
int uid = 102;
|
||||
boolean triggered = mNotificationController.shouldTriggerNotification(uid, locale);
|
||||
|
||||
assertThat(triggered).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShouldTriggerNotification_returnFalse_dueToFrequency() throws Exception {
|
||||
// Initialze proto with en-US locale. Its uid contains 100,101,102.
|
||||
Set<Integer> uidSet = Set.of(100, 101, 102);
|
||||
String locale = "en-US";
|
||||
long lastNotificationTime = Calendar.getInstance().getTimeInMillis();
|
||||
int id = (int) SystemClock.uptimeMillis();
|
||||
initSharedPreference(locale, uidSet, 0, 1, lastNotificationTime, id);
|
||||
|
||||
// When the other app is configured to "en-US", the notification is not triggered because it
|
||||
// is too frequent.
|
||||
int uid = 103;
|
||||
boolean triggered = mNotificationController.shouldTriggerNotification(uid, locale);
|
||||
|
||||
assertThat(triggered).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShouldTriggerNotification_return2ndTrue() throws Exception {
|
||||
// Initialze proto with en-US locale. Its uid contains 100,101,102,103,104.
|
||||
Set<Integer> uidSet = Set.of(100, 101, 102, 103, 104);
|
||||
String locale = "en-US";
|
||||
int id = (int) SystemClock.uptimeMillis();
|
||||
Calendar time = Calendar.getInstance();
|
||||
time.add(Calendar.MINUTE, 86400 * 8 * (-1));
|
||||
long lastNotificationTime = time.getTimeInMillis();
|
||||
initSharedPreference(locale, uidSet, 0, 1, lastNotificationTime, id);
|
||||
|
||||
// When the other app is configured to "en-US", the notification is triggered.
|
||||
int uid = 105;
|
||||
boolean triggered = mNotificationController.shouldTriggerNotification(uid, locale);
|
||||
|
||||
assertThat(triggered).isTrue();
|
||||
}
|
||||
|
||||
private void initSharedPreference(String locale, Set<Integer> uidCollection, int dismissCount,
|
||||
int notificationCount, long lastNotificationTime, int notificationId)
|
||||
throws Exception {
|
||||
NotificationInfo info = new NotificationInfo(uidCollection, notificationCount, dismissCount,
|
||||
lastNotificationTime, notificationId);
|
||||
mDataManager.putNotificationInfo(locale, info);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user