[CTS] Make sure android.backup.app is never in stopped state
when running backup

Previously the test was flaky, because the app was in the stopped state
sometimes, which meant it wouldn't be backed up.
Now we explicitly ping the app for it to be moved out of stopped state
before running the backup operation.

Test: cts-tradefed run cts -m CtsBackupTestCases -t android.backup.cts.FullBackupQuotaTest#testQuotaReported
Bug: 65851529
Change-Id: Ifb2453e0fe004a10ca1a55605425b3a46abba48d
diff --git a/tests/backup/app/fullbackup/AndroidManifest.xml b/tests/backup/app/fullbackup/AndroidManifest.xml
index 849b13f..8161a95 100644
--- a/tests/backup/app/fullbackup/AndroidManifest.xml
+++ b/tests/backup/app/fullbackup/AndroidManifest.xml
@@ -23,6 +23,7 @@
         android:backupAgent="FullBackupBackupAgent"
         android:label="Android Backup CTS App"
         android:fullBackupOnly="true">
+
         <activity
             android:name=".MainActivity"
             android:label="Android Backup CTS App" >
@@ -31,5 +32,12 @@
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
         </activity>
+
+        <receiver android:name=".WakeUpReceiver">
+            <intent-filter>
+                <action android:name="android.backup.app.ACTION_WAKE_UP" />
+            </intent-filter>
+        </receiver>
+
     </application>
 </manifest>
diff --git a/tests/backup/app/src/android/backup/app/WakeUpReceiver.java b/tests/backup/app/src/android/backup/app/WakeUpReceiver.java
new file mode 100644
index 0000000..10a9360
--- /dev/null
+++ b/tests/backup/app/src/android/backup/app/WakeUpReceiver.java
@@ -0,0 +1,36 @@
+/*
+ * 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 android.backup.app;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.util.Log;
+
+/*
+ * Broadcast receiver pinged in order to make sure the app progressed from
+ * the stopped state after being installed, so that backup can be done.
+ */
+public class WakeUpReceiver extends BroadcastReceiver {
+
+    private static final String TAG = "WakeUpReceiver";
+
+    @Override
+    public void onReceive(Context context, Intent intent) {
+        Log.d(TAG, "android.backup.app should no longer be in the stopped state");
+    }
+}
diff --git a/tests/backup/src/android/backup/cts/FullBackupQuotaTest.java b/tests/backup/src/android/backup/cts/FullBackupQuotaTest.java
index 2343eb8..3924e87 100644
--- a/tests/backup/src/android/backup/cts/FullBackupQuotaTest.java
+++ b/tests/backup/src/android/backup/cts/FullBackupQuotaTest.java
@@ -50,6 +50,15 @@
         if (!isBackupSupported()) {
             return;
         }
+        // get the app out of (possibly) stopped state so that backup can be run
+        exec("cmd activity broadcast -a android.backup.app.ACTION_WAKE_UP " +
+                "-n android.backup.app/.WakeUpReceiver");
+
+        // give it 3s for the broadcast to be delivered
+        try {
+            Thread.sleep(3000);
+        } catch (InterruptedException e) {}
+
         String separator = clearLogcat();
         exec("bmgr backupnow " + BACKUP_APP_NAME);
         waitForLogcat(TIMEOUT_SECONDS,separator,