Home | History | Annotate | Download | only in shortcutmanager
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package android.content.pm.cts.shortcutmanager;
     17 
     18 
     19 import static android.content.pm.cts.shortcutmanager.common.Constants.INLINE_REPLY_REMOTE_INPUT_CAPTION;
     20 
     21 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.resetThrottling;
     22 import static com.android.server.pm.shortcutmanagertest.ShortcutManagerTestUtils.runCommandForNoOutput;
     23 
     24 import android.content.ComponentName;
     25 import android.content.Intent;
     26 import android.content.pm.cts.shortcutmanager.common.Constants;
     27 import android.content.pm.cts.shortcutmanager.common.ReplyUtil;
     28 import android.support.test.uiautomator.By;
     29 import android.support.test.uiautomator.UiDevice;
     30 import android.support.test.uiautomator.UiObject2;
     31 import android.support.test.uiautomator.Until;
     32 import android.test.suitebuilder.annotation.SmallTest;
     33 import android.test.suitebuilder.annotation.Suppress;
     34 import android.view.KeyEvent;
     35 
     36 /**
     37  * The actual test is implemented in the CtsShortcutManagerThrottlingTest module.
     38  * This class uses broadcast receivers to communicate with it, because if we just used an
     39  * instrumentation test, the target process would never been throttled.
     40  */
     41 @SmallTest
     42 public class ShortcutManagerThrottlingTest extends ShortcutManagerCtsTestsBase {
     43 
     44     private static final int UI_TIMEOUT = 5000;
     45 
     46     private static final String TARGET_PACKAGE =
     47             "android.content.pm.cts.shortcutmanager.throttling";
     48 
     49     private void callTest(String method) {
     50         ReplyUtil.invokeAndWaitForReply(getTestContext(), (replyAction) -> {
     51             final Intent i = new Intent(Constants.ACTION_THROTTLING_TEST);
     52             i.putExtra(Constants.EXTRA_METHOD, method);
     53             i.putExtra(Constants.EXTRA_REPLY_ACTION, replyAction);
     54             i.setComponent(ComponentName.unflattenFromString(
     55                     TARGET_PACKAGE + "/.ShortcutManagerThrottlingTestReceiver"));
     56             getTestContext().sendBroadcast(i);
     57         });
     58     }
     59 
     60     @Override
     61     protected void setUp() throws Exception {
     62         super.setUp();
     63 
     64         resetThrottling(getInstrumentation());
     65 
     66         UiDevice.getInstance(getInstrumentation()).pressHome();
     67 
     68         runCommandForNoOutput(getInstrumentation(), "am force-stop " + TARGET_PACKAGE);
     69     }
     70 
     71     public void testSetDynamicShortcuts() throws InterruptedException {
     72         callTest(Constants.TEST_SET_DYNAMIC_SHORTCUTS);
     73     }
     74 
     75     public void testAddDynamicShortcuts() throws InterruptedException {
     76         callTest(Constants.TEST_ADD_DYNAMIC_SHORTCUTS);
     77     }
     78 
     79     public void testUpdateShortcuts() throws InterruptedException {
     80         callTest(Constants.TEST_UPDATE_SHORTCUTS);
     81     }
     82 
     83     public void testBgServiceThrottled() throws InterruptedException {
     84         callTest(Constants.TEST_BG_SERVICE_THROTTLED);
     85     }
     86 
     87     public void testActivityUnthrottled() throws InterruptedException {
     88         callTest(Constants.TEST_ACTIVITY_UNTHROTTLED);
     89     }
     90 
     91     public void testFgServiceUnthrottled() throws InterruptedException {
     92         callTest(Constants.TEST_FG_SERVICE_UNTHROTTLED);
     93     }
     94 
     95     /**
     96      * Flakey and may not work on OEM devices, so disabled.
     97      */
     98     @Suppress
     99     public void testInlineReply() throws Exception {
    100         clearNotifications();
    101 
    102         callTest(Constants.TEST_INLINE_REPLY_SHOW);
    103 
    104         performInlineReply();
    105 
    106         callTest(Constants.TEST_INLINE_REPLY_CHECK);
    107     }
    108 
    109     private void clearNotifications() throws InterruptedException {
    110         final UiDevice ud = UiDevice.getInstance(getInstrumentation());
    111 
    112         // Open the notification shade.
    113         ud.openNotification();
    114 
    115         // Press "clear all", if found.
    116         final UiObject2 clearAll = ud.wait(Until.findObject(By.text("CLEAR ALL")), UI_TIMEOUT);
    117 
    118         // Just skip if not found.
    119         if (clearAll != null) {
    120             clearAll.clear();
    121             ud.wait(Until.gone(By.text("CLEAR ALL")), UI_TIMEOUT);
    122             Thread.sleep(1000);
    123         }
    124         // Close the notification.
    125         ud.pressHome();
    126         Thread.sleep(1000);
    127     }
    128 
    129     private void performInlineReply() throws InterruptedException {
    130         final UiDevice ud = UiDevice.getInstance(getInstrumentation());
    131 
    132         // Open the notification shade.
    133         Thread.sleep(1000);
    134         ud.openNotification();
    135 
    136         // Find the inline reply part.
    137         ud.wait(Until.findObject(By.text(INLINE_REPLY_REMOTE_INPUT_CAPTION)), UI_TIMEOUT).click();
    138 
    139         Thread.sleep(1000);
    140 
    141         // Type something.
    142         ud.pressKeyCode(KeyEvent.KEYCODE_A);
    143         ud.pressKeyCode(KeyEvent.KEYCODE_B);
    144         ud.pressKeyCode(KeyEvent.KEYCODE_C);
    145         ud.pressEnter();
    146 
    147         Thread.sleep(1000);
    148         ud.pressHome();
    149 
    150         ud.wait(Until.gone(By.text(INLINE_REPLY_REMOTE_INPUT_CAPTION)), UI_TIMEOUT);
    151 
    152         Thread.sleep(1000);
    153     }
    154 }
    155