Migrate LocaleNotification to main trunk
Bug: 248514263 Test: make RunSettingsRoboTests -j128 ROBOTEST_FILTER=AppLocalePickerActivityTest LocaleListEditorTest LocaleNotificationDataManagerTest NotificationCancelReceiverTest NotificationControllerTest Change-Id: Iac7ffd493485be8ebb10ae63e5ca4ea7a57c8c78
This commit is contained in:
@@ -32,11 +32,14 @@ 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.SystemProperties;
|
||||
import android.os.UserHandle;
|
||||
import android.telephony.TelephonyManager;
|
||||
|
||||
@@ -67,8 +70,10 @@ 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(
|
||||
@@ -79,6 +84,12 @@ import java.util.Locale;
|
||||
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 LocaleNotificationDataManager mDataManager;
|
||||
private AppLocalePickerActivity mActivity;
|
||||
|
||||
@Mock
|
||||
LocaleStore.LocaleInfo mLocaleInfo;
|
||||
@@ -99,10 +110,11 @@ public class AppLocalePickerActivityTest {
|
||||
when(mLocaleConfig.getStatus()).thenReturn(LocaleConfig.STATUS_SUCCESS);
|
||||
when(mLocaleConfig.getSupportedLocales()).thenReturn(LocaleList.forLanguageTags("en-US"));
|
||||
ReflectionHelpers.setStaticField(AppLocaleUtil.class, "sLocaleConfig", mLocaleConfig);
|
||||
sUid = Process.myUid();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
public void tearDown() throws Exception {
|
||||
mPackageManager.removePackage(TEST_PACKAGE_NAME);
|
||||
ReflectionHelpers.setStaticField(AppLocaleUtil.class, "sLocaleConfig", null);
|
||||
ShadowResources.setDisAllowPackage(false);
|
||||
@@ -210,13 +222,266 @@ public class AppLocalePickerActivityTest {
|
||||
assertThat(controller.get().isFinishing()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
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);
|
||||
|
||||
SystemProperties.set(AppLocalePickerActivity.PROP_SYSTEM_LOCALE_SUGGESTION, "false");
|
||||
mDataManager.clearLocaleNotificationMap();
|
||||
}
|
||||
|
||||
@Test
|
||||
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);
|
||||
|
||||
SystemProperties.set(AppLocalePickerActivity.PROP_SYSTEM_LOCALE_SUGGESTION, "false");
|
||||
mDataManager.clearLocaleNotificationMap();
|
||||
}
|
||||
|
||||
@Test
|
||||
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);
|
||||
|
||||
SystemProperties.set(AppLocalePickerActivity.PROP_SYSTEM_LOCALE_SUGGESTION, "false");
|
||||
mDataManager.clearLocaleNotificationMap();
|
||||
}
|
||||
|
||||
@Test
|
||||
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)).isTrue();
|
||||
assertThat(info.getNotificationCount()).isEqualTo(1);
|
||||
assertThat(info.getDismissCount()).isEqualTo(0);
|
||||
assertThat(info.getLastNotificationTimeMs()).isNotEqualTo(0);
|
||||
assertThat(info.getNotificationId()).isEqualTo(notificationId);
|
||||
|
||||
SystemProperties.set(AppLocalePickerActivity.PROP_SYSTEM_LOCALE_SUGGESTION, "false");
|
||||
mDataManager.clearLocaleNotificationMap();
|
||||
}
|
||||
|
||||
@Test
|
||||
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);
|
||||
|
||||
SystemProperties.set(AppLocalePickerActivity.PROP_SYSTEM_LOCALE_SUGGESTION, "false");
|
||||
mDataManager.clearLocaleNotificationMap();
|
||||
}
|
||||
|
||||
@Test
|
||||
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);
|
||||
|
||||
SystemProperties.set(AppLocalePickerActivity.PROP_SYSTEM_LOCALE_SUGGESTION, "false");
|
||||
mDataManager.clearLocaleNotificationMap();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEvaluateLocaleNotification_localeUpdateReachThreshold_uidAddedNoNotification()
|
||||
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);
|
||||
assertThat(info.getNotificationCount()).isEqualTo(2);
|
||||
assertThat(info.getDismissCount()).isEqualTo(0);
|
||||
assertThat(info.getLastNotificationTimeMs()).isEqualTo(lastNotificationTime);
|
||||
|
||||
SystemProperties.set(AppLocalePickerActivity.PROP_SYSTEM_LOCALE_SUGGESTION, "false");
|
||||
mDataManager.clearLocaleNotificationMap();
|
||||
}
|
||||
|
||||
@Test
|
||||
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);
|
||||
|
||||
SystemProperties.set(AppLocalePickerActivity.PROP_SYSTEM_LOCALE_SUGGESTION, "false");
|
||||
mDataManager.clearLocaleNotificationMap();
|
||||
}
|
||||
|
||||
private void initLocaleNotificationEnvironment() throws Exception {
|
||||
LocaleList.setDefault(LocaleList.forLanguageTags(EN_CA));
|
||||
SystemProperties.set(AppLocalePickerActivity.PROP_SYSTEM_LOCALE_SUGGESTION, "true");
|
||||
|
||||
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, Process.myUid());
|
||||
data.putExtra(AppInfoBase.ARG_PACKAGE_UID, sUid);
|
||||
ActivityController<TestAppLocalePickerActivity> activityController =
|
||||
Robolectric.buildActivity(TestAppLocalePickerActivity.class, data);
|
||||
Activity activity = activityController.get();
|
||||
@@ -259,6 +524,19 @@ public class AppLocalePickerActivityTest {
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user