1 /*
2  * Copyright (C) 2017 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.app.cts;
17 
18 import android.app.stubs.R;
19 import android.app.stubs.ToolbarActivity;
20 import android.test.ActivityInstrumentationTestCase2;
21 import android.view.KeyEvent;
22 
23 import android.view.Window;
24 
25 public class ToolbarActionBarTest extends ActivityInstrumentationTestCase2<ToolbarActivity> {
26 
27     private ToolbarActivity mActivity;
28 
ToolbarActionBarTest()29     public ToolbarActionBarTest() {
30         super(ToolbarActivity.class);
31     }
32 
33     @Override
setUp()34     protected void setUp() throws Exception {
35         super.setUp();
36         mActivity = getActivity();
37         getInstrumentation().runOnMainSync(
38                 () -> mActivity.getToolbar().inflateMenu(R.menu.flat_menu));
39     }
40 
testOptionsMenuKey()41     public void testOptionsMenuKey() {
42         if (!mActivity.getWindow().hasFeature(Window.FEATURE_OPTIONS_PANEL)) {
43             return;
44         }
45         final boolean menuIsVisible[] = {false};
46         mActivity.getActionBar().addOnMenuVisibilityListener(
47                 isVisible -> menuIsVisible[0] = isVisible);
48         getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);
49         getInstrumentation().waitForIdleSync();
50         assertTrue(menuIsVisible[0]);
51         assertTrue(mActivity.getToolbar().isOverflowMenuShowing());
52         getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);
53         getInstrumentation().waitForIdleSync();
54         assertFalse(menuIsVisible[0]);
55         assertFalse(mActivity.getToolbar().isOverflowMenuShowing());
56     }
57 
testOpenOptionsMenu()58     public void testOpenOptionsMenu() {
59         if (!mActivity.getWindow().hasFeature(Window.FEATURE_OPTIONS_PANEL)) {
60             return;
61         }
62         final boolean menuIsVisible[] = {false};
63         mActivity.getActionBar().addOnMenuVisibilityListener(
64                 isVisible -> menuIsVisible[0] = isVisible);
65         getInstrumentation().runOnMainSync(() -> mActivity.openOptionsMenu());
66         getInstrumentation().waitForIdleSync();
67         assertTrue(menuIsVisible[0]);
68         assertTrue(mActivity.getToolbar().isOverflowMenuShowing());
69         getInstrumentation().runOnMainSync(() -> mActivity.closeOptionsMenu());
70         getInstrumentation().waitForIdleSync();
71         assertFalse(menuIsVisible[0]);
72         assertFalse(mActivity.getToolbar().isOverflowMenuShowing());
73     }
74 }
75