Fix CTS failed test cases issue

Take the user's context to get PackageManager#getInstallSourceInfo API

Bug: 274199130
Test: atest LocaleConfigAppUpdateTest
      atest LocaleManagerOverrideLocaleConfigTest
      atest LocaleManagerTests
Change-Id: I2721991d033e4c3cd59a24ea353818abf2947c3f
This commit is contained in:
Josh Hou
2023-04-10 05:43:40 +00:00
parent 9b6e578333
commit 9dc1c23d35
4 changed files with 10 additions and 7 deletions

View File

@@ -323,7 +323,7 @@ public class LocaleManagerService extends SystemService {
*/
void notifyInstallerOfAppWhoseLocaleChanged(String appPackageName, int userId,
LocaleList locales) {
String installingPackageName = getInstallingPackageName(appPackageName);
String installingPackageName = getInstallingPackageName(appPackageName, userId);
if (installingPackageName != null) {
Intent intent = createBaseIntent(Intent.ACTION_APPLICATION_LOCALE_CHANGED,
appPackageName, locales);
@@ -464,7 +464,7 @@ public class LocaleManagerService extends SystemService {
* Checks if the calling app is the installer of the app whose locale changed.
*/
private boolean isCallerInstaller(String appPackageName, int userId) {
String installingPackageName = getInstallingPackageName(appPackageName);
String installingPackageName = getInstallingPackageName(appPackageName, userId);
if (installingPackageName != null) {
// Get the uid of installer-on-record to compare with the calling uid.
int installerUid = getPackageUid(installingPackageName, userId);
@@ -513,10 +513,11 @@ public class LocaleManagerService extends SystemService {
}
@Nullable
String getInstallingPackageName(String packageName) {
String getInstallingPackageName(String packageName, int userId) {
try {
return mContext.getPackageManager()
.getInstallSourceInfo(packageName).getInstallingPackageName();
return mContext.createContextAsUser(UserHandle.of(userId), /* flags= */
0).getPackageManager().getInstallSourceInfo(
packageName).getInstallingPackageName();
} catch (PackageManager.NameNotFoundException e) {
Slog.w(TAG, "Package not found " + packageName);
}

View File

@@ -152,9 +152,10 @@ public class SystemAppUpdateTracker {
void onPackageUpdateFinished(String packageName, int uid) {
try {
if ((!mUpdatedApps.contains(packageName)) && isUpdatedSystemApp(packageName)) {
int userId = UserHandle.getUserId(uid);
// If a system app is updated, verify that it has an installer-on-record.
String installingPackageName = mLocaleManagerService.getInstallingPackageName(
packageName);
packageName, userId);
if (installingPackageName == null) {
// We want to broadcast the locales info to the installer.
// If this app does not have an installer then do nothing.
@@ -162,7 +163,6 @@ public class SystemAppUpdateTracker {
}
try {
int userId = UserHandle.getUserId(uid);
// Fetch the app-specific locales.
// If non-empty then send the info to the installer.
LocaleList appLocales = mLocaleManagerService.getApplicationLocales(

View File

@@ -105,6 +105,7 @@ public class LocaleManagerServiceTest {
mMockPackageManager = mock(PackageManager.class);
mMockPackageMonitor = mock(PackageMonitor.class);
doReturn(mMockContext).when(mMockContext).createContextAsUser(any(), anyInt());
// For unit tests, set the default installer info
doReturn(DEFAULT_INSTALL_SOURCE_INFO).when(mMockPackageManager)
.getInstallSourceInfo(anyString());

View File

@@ -131,6 +131,7 @@ public class SystemAppUpdateTrackerTest {
doReturn(mMockPackageManager).when(mMockContext).getPackageManager();
doReturn(InstrumentationRegistry.getContext().getContentResolver())
.when(mMockContext).getContentResolver();
doReturn(mMockContext).when(mMockContext).createContextAsUser(any(), anyInt());
mStoragefile = new AtomicFile(new File(
Environment.getExternalStorageDirectory(), "systemUpdateUnitTests.xml"));