JobScheduler CTS: Correctly run the tests depending on "charging" event.

The previous code used BatteryController.ACTION_CHARGING_STABLE which is an
intent deleted already at Android M. Instead, the new implementation uses
the shell command to fake the battery status and let BatteryController itself
trigger the equivalent broadcase.

Bug: 128656618
Test: CtsJobSchedulerTestCases on Chromebase(w/o batetry), and Pixel
Change-Id: I8d818a050705b0ddafd43bfa48551f7fddab2030
Merged-In: I5758ea3c30653986f4ccccbc4d0d39746b753b63
diff --git a/tests/JobScheduler/src/android/jobscheduler/cts/ConnectivityConstraintTest.java b/tests/JobScheduler/src/android/jobscheduler/cts/ConnectivityConstraintTest.java
index 4486c15..e092a0d 100644
--- a/tests/JobScheduler/src/android/jobscheduler/cts/ConnectivityConstraintTest.java
+++ b/tests/JobScheduler/src/android/jobscheduler/cts/ConnectivityConstraintTest.java
@@ -114,6 +114,8 @@
                 mWifiManager.isWifiEnabled() == mInitialWiFiState);
 
         mContext.unregisterReceiver(receiver);
+
+        super.tearDown();
     }
 
     // --------------------------------------------------------------------------------------------
diff --git a/tests/JobScheduler/src/android/jobscheduler/cts/ConstraintTest.java b/tests/JobScheduler/src/android/jobscheduler/cts/ConstraintTest.java
index 7b8bf4b..bed0d07 100644
--- a/tests/JobScheduler/src/android/jobscheduler/cts/ConstraintTest.java
+++ b/tests/JobScheduler/src/android/jobscheduler/cts/ConstraintTest.java
@@ -41,10 +41,6 @@
  */
 @TargetApi(21)
 public abstract class ConstraintTest extends InstrumentationTestCase {
-    /** Force the scheduler to consider the device to be on stable charging. */
-    private static final Intent EXPEDITE_STABLE_CHARGING =
-            new Intent("com.android.server.task.controllers.BatteryController.ACTION_CHARGING_STABLE");
-
     /** Environment that notifies of JobScheduler callbacks. */
     static MockJobService.TestEnvironment kTestEnvironment =
             MockJobService.TestEnvironment.getTestEnvironment();
@@ -112,6 +108,7 @@
 
     @Override
     public void tearDown() throws Exception {
+        SystemUtil.runShellCommand(getInstrumentation(), "cmd battery reset");
         if (mStorageStateChanged) {
             // Put storage service back in to normal operation.
             SystemUtil.runShellCommand(getInstrumentation(), "cmd devicestoragemonitor reset");
@@ -124,8 +121,11 @@
      * considered to be on stable power - that is, plugged in for a period of 2 minutes.
      * Rather than wait for this to happen, we cheat and send this broadcast instead.
      */
-    protected void sendExpediteStableChargingBroadcast() {
-        getContext().sendBroadcast(EXPEDITE_STABLE_CHARGING);
+    protected void sendExpediteStableChargingBroadcast() throws Exception {
+        // Faking the device to be 90% charging and then to be 91%, so that it triggers
+        // BatteryManager.ACTION_CHARGING in the upward change-level transition logic.
+        SystemUtil.runShellCommand(getInstrumentation(), "cmd battery set level 90");
+        SystemUtil.runShellCommand(getInstrumentation(), "cmd battery set level 91");
     }
 
     public void assertHasUriPermission(Uri uri, int grantFlags) {