Home | History | Annotate | Download | only in cts
      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 
     17 package android.app.cts;
     18 
     19 import android.app.stubs.KeyboardShortcutsActivity;
     20 import android.content.pm.PackageManager;
     21 import android.test.ActivityInstrumentationTestCase2;
     22 import android.view.KeyEvent;
     23 import android.view.KeyboardShortcutGroup;
     24 import android.view.Menu;
     25 import android.widget.PopupMenu;
     26 
     27 import java.util.ArrayList;
     28 import java.util.List;
     29 
     30 /**
     31  * Tests functionality in Activity related to Keyboard Shortcuts.
     32  */
     33 public class ActivityKeyboardShortcutsTest
     34         extends ActivityInstrumentationTestCase2<KeyboardShortcutsActivity> {
     35 
     36     private KeyboardShortcutsActivity mActivity;
     37     private Menu mMenu;
     38 
     39     public ActivityKeyboardShortcutsTest() {
     40         super(KeyboardShortcutsActivity.class);
     41     }
     42 
     43     @Override
     44     protected void setUp() throws Exception {
     45         super.setUp();
     46         mActivity = getActivity();
     47         mMenu = new PopupMenu(mActivity, null).getMenu();
     48     }
     49 
     50     /**
     51      * Tests that requestShowKeyboardShortcuts fetches app specific shortcuts even when triggered
     52      * from an overflow menu (options menu in the test)
     53      */
     54     public void testRequestShowKeyboardShortcuts() throws InterruptedException {
     55         if (!keyboardShortcutsSupported()) {
     56             return;
     57         }
     58         // Open activity's options menu
     59         getInstrumentation().runOnMainSync(() -> mActivity.openOptionsMenu());
     60         mActivity.waitForMenuToBeOpen();
     61 
     62         // Request keyboard shortcuts
     63         getInstrumentation().runOnMainSync(() -> mActivity.requestShowKeyboardShortcuts());
     64         mActivity.waitForKeyboardShortcutsToBeRequested();
     65 
     66         // Close the shortcuts helper
     67         getInstrumentation().runOnMainSync(() -> mActivity.dismissKeyboardShortcutsHelper());
     68 
     69         // THEN the activity's onProvideKeyboardShortcuts should have been
     70         // triggered to get app specific shortcuts
     71         assertTrue(mActivity.onProvideKeyboardShortcutsCalled());
     72     }
     73 
     74     public void testOnProvideKeyboardShortcuts() {
     75         if (!keyboardShortcutsSupported()) {
     76             return;
     77         }
     78         List<KeyboardShortcutGroup> data = new ArrayList<>();
     79         mActivity.onCreateOptionsMenu(mMenu);
     80         mActivity.onProvideKeyboardShortcuts(data, mMenu, -1);
     81 
     82         assertEquals(1, data.size());
     83         assertEquals(1, data.get(0).getItems().size());
     84         assertEquals(KeyboardShortcutsActivity.ITEM_1_NAME,
     85             data.get(0).getItems().get(0).getLabel());
     86         assertEquals(KeyboardShortcutsActivity.ITEM_1_SHORTCUT,
     87             data.get(0).getItems().get(0).getBaseCharacter());
     88         assertEquals(KeyEvent.META_CTRL_ON, data.get(0).getItems().get(0).getModifiers());
     89     }
     90 
     91     private boolean keyboardShortcutsSupported() {
     92       // Keyboard shortcuts API is not supported on watches.
     93       // TODO(b/62257073): Provide a more granular feature to check here.
     94       // 2017-10-17: Updated to also exclude EMBEDDED
     95       return !mActivity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WATCH) &&
     96              !mActivity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_EMBEDDED);
     97     }
     98 }
     99