Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2012 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.ActionBar;
     19 import android.app.ActionBar.Tab;
     20 import android.app.ActionBar.TabListener;
     21 import android.app.FragmentTransaction;
     22 import android.app.stubs.ActionBarActivity;
     23 import android.test.ActivityInstrumentationTestCase2;
     24 import android.test.UiThreadTest;
     25 import android.view.KeyEvent;
     26 import android.view.ViewConfiguration;
     27 import android.view.Window;
     28 
     29 import java.util.concurrent.TimeUnit;
     30 
     31 public class ActionBarTest extends ActivityInstrumentationTestCase2<ActionBarActivity> {
     32 
     33     private ActionBarActivity mActivity;
     34     private ActionBar mBar;
     35 
     36     public ActionBarTest() {
     37         super(ActionBarActivity.class);
     38     }
     39 
     40     @Override
     41     protected void setUp() throws Exception {
     42         super.setUp();
     43         mActivity = getActivity();
     44         mBar = mActivity.getActionBar();
     45     }
     46 
     47     @UiThreadTest
     48     public void testAddTab() {
     49         if (mBar == null) {
     50             return;
     51         }
     52         assertEquals(0, mBar.getTabCount());
     53 
     54         Tab t1 = createTab("Tab 1");
     55         mBar.addTab(t1);
     56         assertEquals(1, mBar.getTabCount());
     57         assertEquals(t1, mBar.getSelectedTab());
     58         assertEquals(t1, mBar.getTabAt(0));
     59 
     60         Tab t2 = createTab("Tab 2");
     61         mBar.addTab(t2);
     62         assertEquals(2, mBar.getTabCount());
     63         assertEquals(t1, mBar.getSelectedTab());
     64         assertEquals(t2, mBar.getTabAt(1));
     65 
     66         Tab t3 = createTab("Tab 3");
     67         mBar.addTab(t3, true);
     68         assertEquals(3, mBar.getTabCount());
     69         assertEquals(t3, mBar.getSelectedTab());
     70         assertEquals(t3, mBar.getTabAt(2));
     71 
     72         Tab t4 = createTab("Tab 2.5");
     73         mBar.addTab(t4, 2);
     74         assertEquals(4, mBar.getTabCount());
     75         assertEquals(t4, mBar.getTabAt(2));
     76         assertEquals(t3, mBar.getTabAt(3));
     77 
     78         Tab t5 = createTab("Tab 0.5");
     79         mBar.addTab(t5, 0, true);
     80         assertEquals(5, mBar.getTabCount());
     81         assertEquals(t5, mBar.getSelectedTab());
     82         assertEquals(t5, mBar.getTabAt(0));
     83         assertEquals(t1, mBar.getTabAt(1));
     84         assertEquals(t2, mBar.getTabAt(2));
     85         assertEquals(t4, mBar.getTabAt(3));
     86         assertEquals(t3, mBar.getTabAt(4));
     87     }
     88 
     89     public void testOptionsMenuKey() throws Exception {
     90         boolean hasPermanentMenuKey = ViewConfiguration.get(getActivity()).hasPermanentMenuKey();
     91         if (!mActivity.getWindow().hasFeature(Window.FEATURE_OPTIONS_PANEL)
     92                 || hasPermanentMenuKey) {
     93             return;
     94         }
     95         final boolean menuIsVisible[] = {false};
     96         mActivity.getActionBar().addOnMenuVisibilityListener(
     97                 isVisible -> menuIsVisible[0] = isVisible);
     98         // Wait here for test activity to gain focus before sending keyevent.
     99         // Visibility listener needs the action bar to be visible.
    100         getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);
    101         getInstrumentation().waitForIdleSync();
    102         assertTrue(menuIsVisible[0]);
    103         assertTrue(mActivity.windowFocusSignal.await(1000, TimeUnit.MILLISECONDS));
    104         getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_MENU);
    105         getInstrumentation().waitForIdleSync();
    106         assertTrue(mActivity.windowFocusSignal.await(1000, TimeUnit.MILLISECONDS));
    107         assertFalse(menuIsVisible[0]);
    108     }
    109 
    110     public void testOpenOptionsMenu() {
    111         boolean hasPermanentMenuKey = ViewConfiguration.get(getActivity()).hasPermanentMenuKey();
    112         if (!mActivity.getWindow().hasFeature(Window.FEATURE_OPTIONS_PANEL)
    113                 || hasPermanentMenuKey) {
    114             return;
    115         }
    116         final boolean menuIsVisible[] = {false};
    117         mActivity.getActionBar().addOnMenuVisibilityListener(
    118                 isVisible -> menuIsVisible[0] = isVisible);
    119         getInstrumentation().runOnMainSync(() -> mActivity.openOptionsMenu());
    120         getInstrumentation().waitForIdleSync();
    121         assertTrue(menuIsVisible[0]);
    122         getInstrumentation().runOnMainSync(() -> mActivity.closeOptionsMenu());
    123         getInstrumentation().waitForIdleSync();
    124         assertFalse(menuIsVisible[0]);
    125     }
    126 
    127     private Tab createTab(String name) {
    128         return mBar.newTab().setText("Tab 1").setTabListener(new TestTabListener());
    129     }
    130 
    131     static class TestTabListener implements TabListener {
    132         @Override
    133         public void onTabSelected(Tab tab, FragmentTransaction ft) {
    134         }
    135 
    136         @Override
    137         public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    138         }
    139 
    140         @Override
    141         public void onTabReselected(Tab tab, FragmentTransaction ft) {
    142         }
    143     }
    144 }
    145