Home | History | Annotate | Download | only in view
      1 /*
      2  * Copyright (C) 2006 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.view;
     18 
     19 import com.android.internal.view.menu.MenuBuilder;
     20 
     21 import junit.framework.Assert;
     22 
     23 import android.test.AndroidTestCase;
     24 import android.test.MoreAsserts;
     25 import android.test.suitebuilder.annotation.LargeTest;
     26 import android.test.suitebuilder.annotation.SmallTest;
     27 import android.view.KeyEvent;
     28 import android.view.Menu;
     29 import android.view.MenuItem;
     30 import android.view.SubMenu;
     31 
     32 import com.android.frameworks.coretests.R;
     33 
     34 public class MenuTest extends AndroidTestCase {
     35 
     36     private MenuBuilder mMenu;
     37 
     38     public void setUp() throws Exception {
     39         super.setUp();
     40         mMenu = new MenuBuilder(super.getContext());
     41     }
     42 
     43     @SmallTest
     44     public void testItemId() {
     45         final int id = 512;
     46         final MenuItem item = mMenu.add(0, id, 0, "test");
     47 
     48         Assert.assertEquals(id, item.getItemId());
     49         Assert.assertEquals(item, mMenu.findItem(id));
     50         Assert.assertEquals(0, mMenu.findItemIndex(id));
     51     }
     52 
     53     @SmallTest
     54     public void testGroupId() {
     55         final int groupId = 541;
     56         final int item1Index = 1;
     57         final int item2Index = 3;
     58 
     59         mMenu.add(0, 0, item1Index - 1, "ignore");
     60         final MenuItem item = mMenu.add(groupId, 0, item1Index, "test");
     61         mMenu.add(0, 0, item2Index - 1, "ignore");
     62         final MenuItem item2 = mMenu.add(groupId, 0, item2Index, "test2");
     63 
     64         Assert.assertEquals(groupId, item.getGroupId());
     65         Assert.assertEquals(groupId, item2.getGroupId());
     66         Assert.assertEquals(item1Index, mMenu.findGroupIndex(groupId));
     67         Assert.assertEquals(item2Index, mMenu.findGroupIndex(groupId, item1Index + 1));
     68     }
     69 
     70     @SmallTest
     71     public void testGroup() {
     72         // This test does the following
     73         //  1. Create a grouped item in the menu
     74         //  2. Check that findGroupIndex() finds the grouped item.
     75         //  3. Check that findGroupIndex() doesn't find a non-existent group.
     76 
     77         final int GROUP_ONE = Menu.FIRST;
     78         final int GROUP_TWO = Menu.FIRST + 1;
     79 
     80         mMenu.add(GROUP_ONE, 0, 0, "Menu text");
     81         Assert.assertEquals(mMenu.findGroupIndex(GROUP_ONE), 0);
     82         Assert.assertEquals(mMenu.findGroupIndex(GROUP_TWO), -1);
     83         //TODO: expand this test case to do multiple groups,
     84         //adding and removing, hiding and showing, etc.
     85     }
     86 
     87     @SmallTest
     88     public void testIsShortcutWithAlpha() throws Exception {
     89         mMenu.setQwertyMode(true);
     90         mMenu.add(0, 0, 0, "test").setShortcut('2', 'a');
     91         Assert.assertTrue(mMenu.isShortcutKey(KeyEvent.KEYCODE_A,
     92                                               makeKeyEvent(KeyEvent.KEYCODE_A,  0)));
     93         Assert.assertFalse(mMenu.isShortcutKey(KeyEvent.KEYCODE_B,
     94                                                makeKeyEvent(KeyEvent.KEYCODE_B,  0)));
     95     }
     96 
     97     @SmallTest
     98     public void testIsShortcutWithNumeric() throws Exception {
     99         mMenu.setQwertyMode(false);
    100         mMenu.add(0, 0, 0, "test").setShortcut('2', 'a');
    101         Assert.assertTrue(mMenu.isShortcutKey(KeyEvent.KEYCODE_2,
    102                                               makeKeyEvent(KeyEvent.KEYCODE_2,  0)));
    103         Assert.assertFalse(mMenu.isShortcutKey(KeyEvent.KEYCODE_A,
    104                                                makeKeyEvent(KeyEvent.KEYCODE_A,  0)));
    105     }
    106 
    107     @SmallTest
    108     public void testIsShortcutWithAlt() throws Exception {
    109         mMenu.setQwertyMode(true);
    110         mMenu.add(0, 0, 0, "test").setShortcut('2', 'a');
    111         Assert.assertTrue(mMenu.isShortcutKey(KeyEvent.KEYCODE_A,
    112                                               makeKeyEvent(KeyEvent.KEYCODE_A,
    113                                                            KeyEvent.META_ALT_ON)));
    114         Assert.assertFalse(mMenu.isShortcutKey(KeyEvent.KEYCODE_A,
    115                                               makeKeyEvent(KeyEvent.KEYCODE_A,
    116                                                            KeyEvent.META_SYM_ON)));
    117     }
    118 
    119     @SmallTest
    120     public void testIsNotShortcutWithShift() throws Exception {
    121         mMenu.setQwertyMode(true);
    122         mMenu.add(0, 0, 0, "test").setShortcut('2', 'a');
    123         Assert.assertFalse(mMenu.isShortcutKey(KeyEvent.KEYCODE_A,
    124                                               makeKeyEvent(KeyEvent.KEYCODE_A,
    125                                                            KeyEvent.META_SHIFT_ON)));
    126     }
    127 
    128     @SmallTest
    129     public void testIsNotShortcutWithSym() throws Exception {
    130         mMenu.setQwertyMode(true);
    131         mMenu.add(0, 0, 0, "test").setShortcut('2', 'a');
    132         Assert.assertFalse(mMenu.isShortcutKey(KeyEvent.KEYCODE_A,
    133                                               makeKeyEvent(KeyEvent.KEYCODE_A,
    134                                                            KeyEvent.META_SYM_ON)));
    135     }
    136 
    137     @SmallTest
    138     public void testIsShortcutWithUpperCaseAlpha() throws Exception {
    139         mMenu.setQwertyMode(true);
    140         mMenu.add(0, 0, 0, "test").setShortcut('2', 'A');
    141         Assert.assertTrue(mMenu.isShortcutKey(KeyEvent.KEYCODE_A,
    142                                               makeKeyEvent(KeyEvent.KEYCODE_A,  0)));
    143     }
    144 
    145     @SmallTest
    146     public void testIsShortcutWithBackspace() throws Exception {
    147         mMenu.setQwertyMode(true);
    148         mMenu.add(0, 0, 0, "test").setShortcut('2', '\b');
    149         Assert.assertTrue(mMenu.isShortcutKey(KeyEvent.KEYCODE_DEL,
    150                                               makeKeyEvent(KeyEvent.KEYCODE_DEL,  0)));
    151     }
    152 
    153     @SmallTest
    154     public void testIsShortcutWithNewline() throws Exception {
    155         mMenu.setQwertyMode(true);
    156         mMenu.add(0, 0, 0, "test").setShortcut('2', '\n');
    157         Assert.assertTrue(mMenu.isShortcutKey(KeyEvent.KEYCODE_ENTER,
    158                                               makeKeyEvent(KeyEvent.KEYCODE_ENTER,  0)));
    159     }
    160 
    161     @SmallTest
    162     public void testOrder() {
    163         final String a = "a", b = "b", c = "c";
    164         final int firstOrder = 7, midOrder = 8, lastOrder = 9;
    165 
    166         mMenu.add(0, 0, lastOrder, c);
    167         mMenu.add(0, 0, firstOrder, a);
    168         mMenu.add(0, 0, midOrder, b);
    169 
    170         Assert.assertEquals(firstOrder, mMenu.getItem(0).getOrder());
    171         Assert.assertEquals(a, mMenu.getItem(0).getTitle());
    172         Assert.assertEquals(midOrder, mMenu.getItem(1).getOrder());
    173         Assert.assertEquals(b, mMenu.getItem(1).getTitle());
    174         Assert.assertEquals(lastOrder, mMenu.getItem(2).getOrder());
    175         Assert.assertEquals(c, mMenu.getItem(2).getTitle());
    176     }
    177 
    178     @SmallTest
    179     public void testTitle() {
    180         final String title = "test";
    181         final MenuItem stringItem = mMenu.add(title);
    182         final MenuItem resItem = mMenu.add(R.string.menu_test);
    183 
    184         Assert.assertEquals(title, stringItem.getTitle());
    185         Assert.assertEquals(getContext().getResources().getString(R.string.menu_test), resItem
    186                 .getTitle());
    187     }
    188 
    189     @SmallTest
    190     public void testCheckable() {
    191         final int groupId = 1;
    192         final MenuItem item1 = mMenu.add(groupId, 1, 0, "item1");
    193         final MenuItem item2 = mMenu.add(groupId, 2, 0, "item2");
    194 
    195         // Set to exclusive
    196         mMenu.setGroupCheckable(groupId, true, true);
    197         Assert.assertTrue("Item was not set to checkable", item1.isCheckable());
    198         item1.setChecked(true);
    199         Assert.assertTrue("Item did not get checked", item1.isChecked());
    200         Assert.assertFalse("Item was not unchecked due to exclusive checkable", item2.isChecked());
    201         mMenu.findItem(2).setChecked(true);
    202         Assert.assertTrue("Item did not get checked", item2.isChecked());
    203         Assert.assertFalse("Item was not unchecked due to exclusive checkable", item1.isChecked());
    204 
    205         // Multiple non-exlusive checkable items
    206         mMenu.setGroupCheckable(groupId, true, false);
    207         Assert.assertTrue("Item was not set to checkable", item1.isCheckable());
    208         item1.setChecked(false);
    209         Assert.assertFalse("Item did not get unchecked", item1.isChecked());
    210         item1.setChecked(true);
    211         Assert.assertTrue("Item did not get checked", item1.isChecked());
    212         mMenu.findItem(2).setChecked(true);
    213         Assert.assertTrue("Item did not get checked", item2.isChecked());
    214         Assert.assertTrue("Item was unchecked when it shouldnt have been", item1.isChecked());
    215     }
    216 
    217     @SmallTest
    218     public void testVisibility() {
    219         final MenuItem item1 = mMenu.add(0, 1, 0, "item1");
    220         final MenuItem item2 = mMenu.add(0, 2, 0, "item2");
    221 
    222         // Should start as visible
    223         Assert.assertTrue("Item did not start as visible", item1.isVisible());
    224         Assert.assertTrue("Item did not start as visible", item2.isVisible());
    225 
    226         // Hide
    227         item1.setVisible(false);
    228         Assert.assertFalse("Item did not become invisible", item1.isVisible());
    229         mMenu.findItem(2).setVisible(false);
    230         Assert.assertFalse("Item did not become invisible", item2.isVisible());
    231     }
    232 
    233     @SmallTest
    234     public void testSubMenu() {
    235         final SubMenu subMenu = mMenu.addSubMenu(0, 0, 0, "submenu");
    236         final MenuItem subMenuItem = subMenu.getItem();
    237         final MenuItem item1 = subMenu.add(0, 1, 0, "item1");
    238         final MenuItem item2 = subMenu.add(0, 2, 0, "item2");
    239 
    240         // findItem should recurse into submenus
    241         Assert.assertEquals(item1, mMenu.findItem(1));
    242         Assert.assertEquals(item2, mMenu.findItem(2));
    243     }
    244 
    245     @SmallTest
    246     public void testRemove() {
    247         final int groupId = 1;
    248         final MenuItem item1 = mMenu.add(groupId, 1, 0, "item1");
    249         final MenuItem item2 = mMenu.add(groupId, 2, 0, "item2");
    250         final MenuItem item3 = mMenu.add(groupId, 3, 0, "item3");
    251         final MenuItem item4 = mMenu.add(groupId, 4, 0, "item4");
    252         final MenuItem item5 = mMenu.add(groupId, 5, 0, "item5");
    253         final MenuItem item6 = mMenu.add(0, 6, 0, "item6");
    254 
    255         Assert.assertEquals(item1, mMenu.findItem(1));
    256         mMenu.removeItemAt(0);
    257         Assert.assertNull(mMenu.findItem(1));
    258 
    259         Assert.assertEquals(item2, mMenu.findItem(2));
    260         mMenu.removeItem(2);
    261         Assert.assertNull(mMenu.findItem(2));
    262 
    263         Assert.assertEquals(item3, mMenu.findItem(3));
    264         Assert.assertEquals(item4, mMenu.findItem(4));
    265         Assert.assertEquals(item5, mMenu.findItem(5));
    266         mMenu.removeGroup(groupId);
    267         Assert.assertNull(mMenu.findItem(3));
    268         Assert.assertNull(mMenu.findItem(4));
    269         Assert.assertNull(mMenu.findItem(5));
    270 
    271         Assert.assertEquals(item6, mMenu.findItem(6));
    272         mMenu.clear();
    273         Assert.assertNull(mMenu.findItem(6));
    274     }
    275 
    276     private KeyEvent makeKeyEvent(int keyCode, int metaState) {
    277         return new KeyEvent(0L, 0L, KeyEvent.ACTION_DOWN, keyCode, 0, metaState);
    278     }
    279 }
    280