Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2015 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 androidx.appcompat.app;
     18 
     19 import static org.junit.Assert.assertFalse;
     20 import static org.junit.Assert.assertTrue;
     21 
     22 import android.support.test.InstrumentationRegistry;
     23 import android.support.test.filters.SmallTest;
     24 import android.view.KeyEvent;
     25 import android.view.Window;
     26 
     27 import androidx.appcompat.widget.Toolbar;
     28 
     29 import org.junit.Test;
     30 
     31 public class KeyEventsTestCaseWithToolbar extends BaseKeyEventsTestCase<ToolbarAppCompatActivity> {
     32     public KeyEventsTestCaseWithToolbar() {
     33         super(ToolbarAppCompatActivity.class);
     34     }
     35 
     36     @Test
     37     @SmallTest
     38     @Override
     39     public void testMenuKeyEventReachesActivity() throws InterruptedException {
     40         // With Toolbar, MENU key gets sent-to (and consumed by) Toolbar rather than Activity
     41     }
     42 
     43     @Test
     44     @SmallTest
     45     public void testMenuKeyOpensToolbarMenu() {
     46         // Base test only checks that *a* menu is opened, we check here that the toolbar's menu
     47         // specifically is opened.
     48         Toolbar toolbar = mActivityTestRule.getActivity().getToolbar();
     49         assertFalse(toolbar.isOverflowMenuShowing());
     50 
     51         InstrumentationRegistry.getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);
     52         InstrumentationRegistry.getInstrumentation().waitForIdleSync();
     53         assertTrue(toolbar.isOverflowMenuShowing());
     54 
     55         InstrumentationRegistry.getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);
     56         InstrumentationRegistry.getInstrumentation().waitForIdleSync();
     57         assertFalse(toolbar.isOverflowMenuShowing());
     58     }
     59 
     60     @Test
     61     @SmallTest
     62     public void testOpenMenuOpensToolbarMenu() throws Throwable {
     63         if (!mActivityTestRule.getActivity().getWindow().hasFeature(Window.FEATURE_OPTIONS_PANEL)) {
     64             return;
     65         }
     66         Toolbar toolbar = mActivityTestRule.getActivity().getToolbar();
     67         assertFalse(toolbar.isOverflowMenuShowing());
     68 
     69         mActivityTestRule.runOnUiThread(new Runnable() {
     70             @Override
     71             public void run() {
     72                 mActivityTestRule.getActivity().openOptionsMenu();
     73             }
     74         });
     75         InstrumentationRegistry.getInstrumentation().waitForIdleSync();
     76         assertTrue(toolbar.isOverflowMenuShowing());
     77 
     78         mActivityTestRule.runOnUiThread(new Runnable() {
     79             @Override
     80             public void run() {
     81                 mActivityTestRule.getActivity().closeOptionsMenu();
     82             }
     83         });
     84         InstrumentationRegistry.getInstrumentation().waitForIdleSync();
     85         assertFalse(toolbar.isOverflowMenuShowing());
     86     }
     87 }
     88