1/*
2 * Copyright (C) 2016 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 */
16package androidx.appcompat.view;
17
18import static org.junit.Assert.assertEquals;
19
20import android.support.test.filters.SmallTest;
21import android.support.test.rule.ActivityTestRule;
22import android.support.test.runner.AndroidJUnit4;
23import android.view.KeyEvent;
24import android.view.Menu;
25import android.view.MenuInflater;
26
27import androidx.appcompat.test.R;
28import androidx.appcompat.widget.PopupMenu;
29import androidx.core.internal.view.SupportMenuItem;
30
31import org.junit.Before;
32import org.junit.Rule;
33import org.junit.Test;
34import org.junit.runner.RunWith;
35
36/**
37 * Test SupportMenuInflater
38 */
39@SmallTest
40@RunWith(AndroidJUnit4.class)
41public class SupportMenuInflaterTest {
42
43    private SupportMenuInflaterTestActivity mActivity;
44    private MenuInflater mMenuInflater;
45    private Menu mMenu;
46
47    @Rule
48    public ActivityTestRule<SupportMenuInflaterTestActivity> mActivityTestRule =
49            new ActivityTestRule<>(SupportMenuInflaterTestActivity.class);
50
51    @Before
52    public void setup() {
53        mActivity = mActivityTestRule.getActivity();
54        mMenuInflater = mActivity.getMenuInflater();
55        mMenu = new PopupMenu(mActivity, null).getMenu();
56    }
57
58    @Test
59    public void testInflateFromXml() {
60        mMenuInflater.inflate(R.menu.shortcut, mMenu);
61        SupportMenuItem mMenuItem;
62
63        mMenuItem = (SupportMenuItem) mMenu.findItem(R.id.no_modifiers);
64        assertEquals('a', mMenuItem.getAlphabeticShortcut());
65        assertEquals(KeyEvent.META_CTRL_ON, mMenuItem.getAlphabeticModifiers());
66        assertEquals('1', mMenuItem.getNumericShortcut());
67        assertEquals(KeyEvent.META_CTRL_ON, mMenuItem.getNumericModifiers());
68
69        mMenuItem = (SupportMenuItem) mMenu.findItem(R.id.default_modifiers);
70        assertEquals('b', mMenuItem.getAlphabeticShortcut());
71        assertEquals(KeyEvent.META_CTRL_ON, mMenuItem.getAlphabeticModifiers());
72        assertEquals('2', mMenuItem.getNumericShortcut());
73        assertEquals(KeyEvent.META_CTRL_ON, mMenuItem.getNumericModifiers());
74
75        mMenuItem = (SupportMenuItem) mMenu.findItem(R.id.single_modifier);
76        assertEquals('c', mMenuItem.getAlphabeticShortcut());
77        assertEquals(KeyEvent.META_SHIFT_ON, mMenuItem.getAlphabeticModifiers());
78        assertEquals('3', mMenuItem.getNumericShortcut());
79        assertEquals(KeyEvent.META_SHIFT_ON, mMenuItem.getNumericModifiers());
80
81        mMenuItem = (SupportMenuItem) mMenu.findItem(R.id.multiple_modifiers);
82        assertEquals('d', mMenuItem.getAlphabeticShortcut());
83        assertEquals(KeyEvent.META_CTRL_ON | KeyEvent.META_SHIFT_ON,
84                mMenuItem.getAlphabeticModifiers());
85        assertEquals('4', mMenuItem.getNumericShortcut());
86        assertEquals(KeyEvent.META_CTRL_ON | KeyEvent.META_SHIFT_ON,
87                mMenuItem.getNumericModifiers());
88    }
89}
90