ShortcutManager CTS: Make sure foreground apps are not...

...throttled.

Bug 29612099

Change-Id: I84111aef6f267ee57795529389799e8ed52d1bc9
diff --git a/tests/tests/shortcutmanager/common/src/android.content.pm.cts.shortcutmanager.common/Constants.java b/tests/tests/shortcutmanager/common/src/android.content.pm.cts.shortcutmanager.common/Constants.java
index 1fddaf9..84f92d7 100644
--- a/tests/tests/shortcutmanager/common/src/android.content.pm.cts.shortcutmanager.common/Constants.java
+++ b/tests/tests/shortcutmanager/common/src/android.content.pm.cts.shortcutmanager.common/Constants.java
@@ -27,4 +27,8 @@
     public static final String TEST_SET_DYNAMIC_SHORTCUTS = "testSetDynamicShortcuts";
     public static final String TEST_ADD_DYNAMIC_SHORTCUTS = "testAddDynamicShortcuts";
     public static final String TEST_UPDATE_SHORTCUTS = "testUpdateShortcuts";
+
+    public static final String TEST_ACTIVITY_UNTHROTTLED = "testActivityUnthrottled";
+    public static final String TEST_FG_SERVICE_UNTHROTTLED = "testFgServiceUnthrottled";
+    public static final String TEST_BG_SERVICE_THROTTLED = "testBgServiceThrottled";
 }
diff --git a/tests/tests/shortcutmanager/src/android/content/pm/cts/shortcutmanager/ShortcutManagerThrottlingTest.java b/tests/tests/shortcutmanager/src/android/content/pm/cts/shortcutmanager/ShortcutManagerThrottlingTest.java
index 2f9d690..78a55e1 100644
--- a/tests/tests/shortcutmanager/src/android/content/pm/cts/shortcutmanager/ShortcutManagerThrottlingTest.java
+++ b/tests/tests/shortcutmanager/src/android/content/pm/cts/shortcutmanager/ShortcutManagerThrottlingTest.java
@@ -62,7 +62,8 @@
             i.putExtra(Constants.EXTRA_METHOD, method);
             i.putExtra(Constants.EXTRA_REPLY_ACTION, replyAction);
             i.setComponent(ComponentName.unflattenFromString(
-                    "android.content.pm.cts.shortcutmanager.throttling/.ShortcutManagerThrottlingTestReceiver"
+                    "android.content.pm.cts.shortcutmanager.throttling/"
+                            + ".ShortcutManagerThrottlingTestReceiver"
                     ));
             getTestContext().sendBroadcast(i);
 
@@ -92,5 +93,20 @@
         resetThrottling(getInstrumentation());
 
         callTest(Constants.TEST_UPDATE_SHORTCUTS);
+
+        // --------------------------------------
+        resetThrottling(getInstrumentation());
+
+        callTest(Constants.TEST_BG_SERVICE_THROTTLED);
+
+        // --------------------------------------
+        resetThrottling(getInstrumentation());
+
+        callTest(Constants.TEST_ACTIVITY_UNTHROTTLED);
+
+        // --------------------------------------
+        resetThrottling(getInstrumentation());
+
+        callTest(Constants.TEST_FG_SERVICE_UNTHROTTLED);
     }
 }
diff --git a/tests/tests/shortcutmanager/throttling/AndroidManifest.xml b/tests/tests/shortcutmanager/throttling/AndroidManifest.xml
index 2a16253..305f8e1 100644
--- a/tests/tests/shortcutmanager/throttling/AndroidManifest.xml
+++ b/tests/tests/shortcutmanager/throttling/AndroidManifest.xml
@@ -26,6 +26,12 @@
                 <action android:name="android.content.pm.cts.shortcutmanager.ACTION_THROTTLING_TEST" />
             </intent-filter>
         </receiver>
+
+        <activity android:name=".MyActivity" />
+
+        <service android:name=".BgService" />
+
+        <service android:name=".FgService" />
     </application>
 </manifest>
 
diff --git a/tests/tests/shortcutmanager/throttling/src/android/content/pm/cts/shortcutmanager/throttling/BgService.java b/tests/tests/shortcutmanager/throttling/src/android/content/pm/cts/shortcutmanager/throttling/BgService.java
new file mode 100644
index 0000000..fbf8079
--- /dev/null
+++ b/tests/tests/shortcutmanager/throttling/src/android/content/pm/cts/shortcutmanager/throttling/BgService.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright (C) 2016 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.content.pm.cts.shortcutmanager.throttling;
+
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.list;
+
+import android.app.Service;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.ShortcutManager;
+import android.content.pm.cts.shortcutmanager.common.Constants;
+import android.os.IBinder;
+import android.util.Log;
+
+/**
+ * Make sure that when only a bg service is running, shortcut manager calls are throttled.
+ */
+public class BgService extends Service {
+    public static void start(Context context, String replyAction) {
+        final Intent i =
+                new Intent().setComponent(new ComponentName(context, BgService.class))
+                        .putExtra(Constants.EXTRA_REPLY_ACTION, replyAction);
+        context.startService(i);
+    }
+
+    @Override
+    public int onStartCommand(Intent intent, int flags, int startId) {
+        final Context context = getApplicationContext();
+        final ShortcutManager manager = context.getSystemService(ShortcutManager.class);
+
+        final String replyAction = intent.getStringExtra(Constants.EXTRA_REPLY_ACTION);
+
+        Log.i(ThrottledTests.TAG, Constants.TEST_BG_SERVICE_THROTTLED);
+
+        ReplyUtil.runTestAndReply(context, replyAction, () -> {
+            ThrottledTests.assertThrottled(
+                    context, () -> manager.setDynamicShortcuts(list()));
+        });
+
+        stopSelf();
+
+        return Service.START_NOT_STICKY;
+    }
+
+    @Override
+    public IBinder onBind(Intent intent) {
+        return null;
+    }
+}
diff --git a/tests/tests/shortcutmanager/throttling/src/android/content/pm/cts/shortcutmanager/throttling/FgService.java b/tests/tests/shortcutmanager/throttling/src/android/content/pm/cts/shortcutmanager/throttling/FgService.java
new file mode 100644
index 0000000..86f9dc7
--- /dev/null
+++ b/tests/tests/shortcutmanager/throttling/src/android/content/pm/cts/shortcutmanager/throttling/FgService.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright (C) 2016 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.content.pm.cts.shortcutmanager.throttling;
+
+import android.app.Notification;
+import android.app.Service;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.cts.shortcutmanager.common.Constants;
+import android.os.IBinder;
+import android.util.Log;
+
+/**
+ * Make sure that when a fg service is running, shortcut manager calls are not throttled.
+ */
+public class FgService extends Service {
+    public static void start(Context context, String replyAction) {
+        final Intent i =
+                new Intent().setComponent(new ComponentName(context, FgService.class))
+                        .putExtra(Constants.EXTRA_REPLY_ACTION, replyAction);
+        context.startService(i);
+    }
+
+    @Override
+    public int onStartCommand(Intent intent, int flags, int startId) {
+
+        // Start as foreground.
+        Notification notification = new Notification.Builder(getApplicationContext())
+                .setContentTitle("FgService")
+                .setSmallIcon(android.R.drawable.ic_popup_sync)
+                .build();
+        startForeground(1, notification);
+
+        final String replyAction = intent.getStringExtra(Constants.EXTRA_REPLY_ACTION);
+
+        Log.i(ThrottledTests.TAG, Constants.TEST_FG_SERVICE_UNTHROTTLED);
+
+        // Actual test.
+        ReplyUtil.runTestAndReply(this, replyAction, () -> {
+            ThrottledTests.assertCallNotThrottled(this);
+        });
+
+        // Stop self.
+        stopForeground(true);
+        stopSelf();
+
+        return Service.START_NOT_STICKY;
+    }
+
+    @Override
+    public IBinder onBind(Intent intent) {
+        return null;
+    }
+}
diff --git a/tests/tests/shortcutmanager/throttling/src/android/content/pm/cts/shortcutmanager/throttling/MyActivity.java b/tests/tests/shortcutmanager/throttling/src/android/content/pm/cts/shortcutmanager/throttling/MyActivity.java
new file mode 100644
index 0000000..d04a890
--- /dev/null
+++ b/tests/tests/shortcutmanager/throttling/src/android/content/pm/cts/shortcutmanager/throttling/MyActivity.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2016 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.content.pm.cts.shortcutmanager.throttling;
+
+import android.app.Activity;
+import android.content.pm.cts.shortcutmanager.common.Constants;
+import android.os.Bundle;
+import android.util.Log;
+
+/**
+ * Make sure when it's running shortcut manger calls are not throttled.
+ */
+public class MyActivity extends Activity {
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        final String replyAction = getIntent().getStringExtra(Constants.EXTRA_REPLY_ACTION);
+
+        Log.i(ThrottledTests.TAG, Constants.TEST_ACTIVITY_UNTHROTTLED);
+
+        ReplyUtil.runTestAndReply(this, replyAction, () -> {
+            ThrottledTests.assertCallNotThrottled(this);
+        });
+
+        finish();
+    }
+}
diff --git a/tests/tests/shortcutmanager/throttling/src/android/content/pm/cts/shortcutmanager/throttling/ReplyUtil.java b/tests/tests/shortcutmanager/throttling/src/android/content/pm/cts/shortcutmanager/throttling/ReplyUtil.java
new file mode 100644
index 0000000..1b0471c
--- /dev/null
+++ b/tests/tests/shortcutmanager/throttling/src/android/content/pm/cts/shortcutmanager/throttling/ReplyUtil.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2016 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.content.pm.cts.shortcutmanager.throttling;
+
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+import android.util.Log;
+
+public class ReplyUtil {
+    private ReplyUtil() {
+    }
+
+    public static void runTestAndReply(Context context, String replyAction, Runnable test) {
+        try {
+            test.run();
+
+            sendReply(context, replyAction, null);
+        } catch (Throwable e) {
+            String error = "Test failed: " + e.getMessage() + "\n" + Log.getStackTraceString(e);
+            sendReply(context, replyAction, error);
+        }
+    }
+
+    public static void sendReply(Context context, String replyAction,
+            String failureMessageOrNullForSuccess) {
+        // Create the reply bundle.
+        final Bundle ret = new Bundle();
+        if (failureMessageOrNullForSuccess == null) {
+            ret.putBoolean("success", true);
+        } else {
+            ret.putString("error", failureMessageOrNullForSuccess);
+        }
+
+        // Send reply
+        final Intent reply = new Intent(replyAction);
+        reply.putExtras(ret);
+
+        context.sendBroadcast(reply);
+    }
+}
diff --git a/tests/tests/shortcutmanager/throttling/src/android/content/pm/cts/shortcutmanager/throttling/ShortcutManagerThrottlingTestReceiver.java b/tests/tests/shortcutmanager/throttling/src/android/content/pm/cts/shortcutmanager/throttling/ShortcutManagerThrottlingTestReceiver.java
index e5aa3f8..2cbaf20 100644
--- a/tests/tests/shortcutmanager/throttling/src/android/content/pm/cts/shortcutmanager/throttling/ShortcutManagerThrottlingTestReceiver.java
+++ b/tests/tests/shortcutmanager/throttling/src/android/content/pm/cts/shortcutmanager/throttling/ShortcutManagerThrottlingTestReceiver.java
@@ -17,20 +17,14 @@
 
 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.list;
 
-import static junit.framework.Assert.assertTrue;
-
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.fail;
-
 import android.content.BroadcastReceiver;
+import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.ShortcutManager;
 import android.content.pm.cts.shortcutmanager.common.Constants;
-import android.os.Bundle;
 import android.util.Log;
 
-import java.util.function.BooleanSupplier;
 
 /**
  * Throttling test case.
@@ -52,71 +46,93 @@
     @Override
     public void onReceive(Context context, Intent intent) {
         if (Constants.ACTION_THROTTLING_TEST.equals(intent.getAction())) {
-            boolean success = false;
-            String error = null;
-            try {
-                final String method = intent.getStringExtra(Constants.EXTRA_METHOD);
-                switch (method) {
-                    case Constants.TEST_SET_DYNAMIC_SHORTCUTS:
-                        testSetDynamicShortcuts(context);
-                        break;
-                    case Constants.TEST_ADD_DYNAMIC_SHORTCUTS:
-                        testAddDynamicShortcuts(context);
-                        break;
-                    case Constants.TEST_UPDATE_SHORTCUTS:
-                        testUpdateShortcuts(context);
-                        break;
-                    default:
-                        fail("Unknown test: " + method);
-                }
+            final String replyAction = intent.getStringExtra(Constants.EXTRA_REPLY_ACTION);
+            final String method = intent.getStringExtra(Constants.EXTRA_METHOD);
+            switch (method) {
+                case Constants.TEST_SET_DYNAMIC_SHORTCUTS:
+                    testSetDynamicShortcuts(context, replyAction);
+                    break;
+                case Constants.TEST_ADD_DYNAMIC_SHORTCUTS:
+                    testAddDynamicShortcuts(context, replyAction);
+                    break;
+                case Constants.TEST_UPDATE_SHORTCUTS:
+                    testUpdateShortcuts(context, replyAction);
+                    break;
 
-                success = true;
-            } catch (Throwable e) {
-                error = "Test failed: " + e.getMessage() + "\n" + Log.getStackTraceString(e);
-            }
+                case Constants.TEST_BG_SERVICE_THROTTLED:
+                    testBgServiceThrottled(context, replyAction);
+                    break;
 
-            // Create the reply bundle.
-            final Bundle ret = new Bundle();
-            if (success) {
-                ret.putBoolean("success", true);
-            } else {
-                ret.putString("error", error);
-            }
+                case Constants.TEST_ACTIVITY_UNTHROTTLED:
+                    testActivityUnthrottled(context, replyAction);
+                    break;
+                case Constants.TEST_FG_SERVICE_UNTHROTTLED:
+                    testFgServiceUnthrottled(context, replyAction);
+                    break;
 
-            // Send reply
-            final Intent reply = new Intent(intent.getStringExtra(Constants.EXTRA_REPLY_ACTION));
-            reply.putExtras(ret);
-
-            context.sendBroadcast(reply);
-        }
-    }
-
-    private void assertThrottled(Context context, BooleanSupplier apiCall) {
-        assertFalse("Throttling must be reset here", getManager(context).isRateLimitingActive());
-
-        assertTrue("First call should succeed", apiCall.getAsBoolean());
-
-        // App can make 10 API calls between the interval, but there's a chance that the throttling
-        // gets reset within this loop, so we make 20 calls.
-        boolean throttled = false;
-        for (int i = 0; i < 19; i++) {
-            if (!apiCall.getAsBoolean()) {
-                throttled = true;
-                break;
+                default:
+                    ReplyUtil.sendReply(context, replyAction, "Unknown test: " + method);
+                    break;
             }
         }
-        assertTrue("API call not throttled", throttled);
     }
 
-    public void testSetDynamicShortcuts(Context context) {
-        assertThrottled(context, () -> getManager(context).setDynamicShortcuts(list()));
+    public void testSetDynamicShortcuts(Context context, String replyAction) {
+        Log.i(ThrottledTests.TAG, Constants.TEST_SET_DYNAMIC_SHORTCUTS);
+
+        ReplyUtil.runTestAndReply(context, replyAction, () -> {
+            ThrottledTests.assertThrottled(
+                    context, () -> getManager(context).setDynamicShortcuts(list()));
+        });
     }
 
-    public void testAddDynamicShortcuts(Context context) {
-        assertThrottled(context, () -> getManager(context).addDynamicShortcuts(list()));
+    public void testAddDynamicShortcuts(Context context, String replyAction) {
+        Log.i(ThrottledTests.TAG, Constants.TEST_ADD_DYNAMIC_SHORTCUTS);
+
+        ReplyUtil.runTestAndReply(context, replyAction, () -> {
+            ThrottledTests.assertThrottled(
+                    context, () -> getManager(context).addDynamicShortcuts(list()));
+        });
     }
 
-    public void testUpdateShortcuts(Context context) {
-        assertThrottled(context, () -> getManager(context).updateShortcuts(list()));
+    public void testUpdateShortcuts(Context context, String replyAction) {
+        Log.i(ThrottledTests.TAG, Constants.TEST_UPDATE_SHORTCUTS);
+
+        ReplyUtil.runTestAndReply(context, replyAction, () -> {
+            ThrottledTests.assertThrottled(
+                    context, () -> getManager(context).updateShortcuts(list()));
+        });
+    }
+
+    public void testBgServiceThrottled(Context context, String replyAction) {
+        ReplyUtil.runTestAndReply(context, replyAction, () -> {
+
+            BgService.start(context, replyAction);
+        });
+    }
+
+    public void testActivityUnthrottled(Context context, String replyAction) {
+        ReplyUtil.runTestAndReply(context, replyAction, () -> {
+
+            // First make sure the self is throttled.
+            ThrottledTests.ensureThrottled(context);
+
+            // Run the activity that runs the actual test.
+            final Intent i =
+                    new Intent().setComponent(new ComponentName(context, MyActivity.class))
+                    .putExtra(Constants.EXTRA_REPLY_ACTION, replyAction)
+                    .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
+            context.startActivity(i);
+        });
+    }
+
+    public void testFgServiceUnthrottled(Context context, String replyAction) {
+        ReplyUtil.runTestAndReply(context, replyAction, () -> {
+
+            // First make sure the self is throttled.
+            ThrottledTests.ensureThrottled(context);
+
+            FgService.start(context, replyAction);
+        });
     }
 }
diff --git a/tests/tests/shortcutmanager/throttling/src/android/content/pm/cts/shortcutmanager/throttling/ThrottledTests.java b/tests/tests/shortcutmanager/throttling/src/android/content/pm/cts/shortcutmanager/throttling/ThrottledTests.java
new file mode 100644
index 0000000..5053e87
--- /dev/null
+++ b/tests/tests/shortcutmanager/throttling/src/android/content/pm/cts/shortcutmanager/throttling/ThrottledTests.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2016 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.content.pm.cts.shortcutmanager.throttling;
+
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.list;
+import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.retryUntil;
+
+import static junit.framework.Assert.assertTrue;
+
+import static org.junit.Assert.assertFalse;
+
+import android.content.Context;
+import android.content.pm.ShortcutManager;
+
+import java.util.function.BooleanSupplier;
+
+public class ThrottledTests {
+    public static String TAG = "ShortcutThrottledTests";
+
+    private ThrottledTests() {
+    }
+
+    public static void assertThrottled(Context context, BooleanSupplier apiCall) {
+        final ShortcutManager manager = context.getSystemService(ShortcutManager.class);
+
+        assertFalse("Throttling must be reset here", manager.isRateLimitingActive());
+
+        assertTrue("First call should succeed", apiCall.getAsBoolean());
+
+        // App can make 10 API calls between the interval, but there's a chance that the throttling
+        // gets reset within this loop, so we make 20 calls.
+        boolean throttled = false;
+        for (int i = 0; i < 19; i++) {
+            if (!apiCall.getAsBoolean()) {
+                throttled = true;
+                break;
+            }
+        }
+        assertTrue("API call not throttled", throttled);
+    }
+
+    /**
+     * Call shortcut manager APIs until throttled.
+     */
+    public static void ensureThrottled(Context context) {
+        final ShortcutManager manager = context.getSystemService(ShortcutManager.class);
+
+        retryUntil(() -> !manager.setDynamicShortcuts(list()), "Not throttled.");
+    }
+
+    public static void assertCallNotThrottled(Context context) {
+        final ShortcutManager manager = context.getSystemService(ShortcutManager.class);
+
+        assertFalse(manager.isRateLimitingActive());
+
+        for (int i = 0; i < 20; i++) {
+            assertTrue(manager.setDynamicShortcuts(list()));
+            assertTrue(manager.addDynamicShortcuts(list()));
+            assertTrue(manager.updateShortcuts(list()));
+        }
+    }
+}