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