Paul Hadfield 8354946ce5 Fix onPackageChanged handling of disabled packages
TransportManager#onPackageChanged was not correctly handling the
circumstance where broadcast ACTION_PACKAGE_CHANGED was received
with EXTRA_CHANGED_COMPONENT_NAME_LIST containing only the
package name, instead of >=1 component names.  When that happens
it indicates that a package-wide change has occurred, such as
the package being enabled or disabled.

If the package in question contains backup transports, they
should be unregistered if the package is disabled, and re-
registered if the package is enabled.  But the current
TransportManager#onPackageChanged doesn't know enough to do so.

We can determine the enabled state of the package by calling
PackageManager#getApplicationEnabledSetting. This commit modifies
onPackageChanged to do that, and then (un/re)-register the
packaged transports as appropriate.

To test I extend the Roboelectric ShadowApplicationPackageManager
so that the {get,set}ApplicationEnabledState() methods are
exposed for use in the test setup.

Note, this is a retake of ag/15301455, with handling added for a
race condition which caused flakiness in CtsUtilTestCases: the
onPackageChanged method must handle a possible exception thrown
by the PackageManager if the package has just been removed.

Bug: 162725876
Fixes: 162725876
Test: 1. atest -v TransportManagerTest
      2. atest CtsBackupHostTestCases|CtsBackupTestCases|CtsUtilTestCases
      3. manual test on device:
           1 device:/ # bmgr list transports
           2 device:/ # pm disable com.google.android.gms
           3 device:/ # bmgr list transports
           4 device:/ # pm enable com.google.android.gms
           5 device:/ # bmgr list transports
         observe logcat showing BackupTransportManager MORE_DEBUG:
	   step 2: Package c.g.a.gms was disabled.
	   step 4: Package c.g.a.gms was enabled.
	           Transport c.g.a.gms/.b.c.D2dTransportService registered
Change-Id: I1ba20a50dfb9a6918605bbd3216a08f0047e2c10
(cherry picked from commit d68bb3786d6431411973ae1a54ac43de93552b90)
2022-03-07 11:51:24 +00:00
..

This folder is for Robolectric tests inside the platform.

To add a test class annotate it as follows:

@RunWith(FrameworkRobolectricTestRunner.class)
@Config(manifest = Config.NONE, sdk = 26)
@SystemLoaderClasses({ClassUnderTest.class, DependencyClasses.class})
@SystemLoaderPackages({"com.android.server.yourmodule"})

Robolectric loads some classes that it decides from versioned jars of the framework. Since we are
part of the framework some of our classes get loaded from these jars. This is NOT what we want, we
want to test against what we wrote in the tree. Because of this we use a custom test runner,
FrameworkRobolectricTestRunner, that bypasses these jars and loads certain classes from the system
class loader.

To specify which classes to load use either @SystemLoaderClasses or @SystemLoaderPackages. In
practice:
* You MUST put the class under test here.
* If you encounter any exceptions that might be caused by a different version of the class being
loaded, such as NoSuchMethodException, put the class involved in the exception in this annotation
and try again.

Check Android.mk file for more info.