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