Home | History | Annotate | Download | only in cts
      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  */
     16 
     17 package android.widget.cts;
     18 
     19 import static org.junit.Assert.assertEquals;
     20 import static org.junit.Assert.assertFalse;
     21 import static org.junit.Assert.assertNotNull;
     22 import static org.junit.Assert.assertTrue;
     23 import static org.mockito.Mockito.mock;
     24 import static org.mockito.Mockito.times;
     25 import static org.mockito.Mockito.verify;
     26 
     27 import android.app.Instrumentation;
     28 import android.graphics.Color;
     29 import android.graphics.drawable.Drawable;
     30 import android.support.test.InstrumentationRegistry;
     31 import android.support.test.annotation.UiThreadTest;
     32 import android.support.test.filters.MediumTest;
     33 import android.support.test.rule.ActivityTestRule;
     34 import android.support.test.runner.AndroidJUnit4;
     35 import android.view.Menu;
     36 import android.widget.ActionMenuView;
     37 import android.widget.cts.util.TestUtils;
     38 
     39 import com.android.compatibility.common.util.WidgetTestUtils;
     40 
     41 import org.junit.Before;
     42 import org.junit.Rule;
     43 import org.junit.Test;
     44 import org.junit.runner.RunWith;
     45 
     46 @MediumTest
     47 @RunWith(AndroidJUnit4.class)
     48 public class ActionMenuViewTest {
     49     private Instrumentation mInstrumentation;
     50     private ActionMenuViewCtsActivity mActivity;
     51     private ActionMenuView mActionMenuView;
     52 
     53     @Rule
     54     public ActivityTestRule<ActionMenuViewCtsActivity> mActivityRule =
     55             new ActivityTestRule<>(ActionMenuViewCtsActivity.class);
     56 
     57     @Before
     58     public void setup() {
     59         mInstrumentation = InstrumentationRegistry.getInstrumentation();
     60         mActivity = mActivityRule.getActivity();
     61         mActionMenuView = (ActionMenuView) mActivity.findViewById(R.id.action_menu_view);
     62     }
     63 
     64     @Test
     65     public void testConstructor() {
     66         new ActionMenuView(mActivity);
     67 
     68         new ActionMenuView(mActivity, null);
     69     }
     70 
     71     @UiThreadTest
     72     @Test
     73     public void testMenuContent() {
     74         final Menu menu = mActionMenuView.getMenu();
     75         assertNotNull(menu);
     76 
     77         mActivity.getMenuInflater().inflate(R.menu.toolbar_menu, menu);
     78 
     79         assertEquals(6, menu.size());
     80         assertEquals(R.id.action_highlight, menu.getItem(0).getItemId());
     81         assertEquals(R.id.action_edit, menu.getItem(1).getItemId());
     82         assertEquals(R.id.action_delete, menu.getItem(2).getItemId());
     83         assertEquals(R.id.action_ignore, menu.getItem(3).getItemId());
     84         assertEquals(R.id.action_share, menu.getItem(4).getItemId());
     85         assertEquals(R.id.action_print, menu.getItem(5).getItemId());
     86 
     87         ActionMenuView.OnMenuItemClickListener menuItemClickListener =
     88                 mock(ActionMenuView.OnMenuItemClickListener.class);
     89         mActionMenuView.setOnMenuItemClickListener(menuItemClickListener);
     90 
     91         menu.performIdentifierAction(R.id.action_highlight, 0);
     92         verify(menuItemClickListener, times(1)).onMenuItemClick(
     93                 menu.findItem(R.id.action_highlight));
     94 
     95         menu.performIdentifierAction(R.id.action_share, 0);
     96         verify(menuItemClickListener, times(1)).onMenuItemClick(
     97                 menu.findItem(R.id.action_share));
     98     }
     99 
    100     @Test
    101     public void testMenuOverflowShowHide() throws Throwable {
    102         // Inflate menu and check that we're not showing overflow menu yet
    103         mActivityRule.runOnUiThread(
    104                 () -> mActivity.getMenuInflater().inflate(
    105                         R.menu.toolbar_menu, mActionMenuView.getMenu()));
    106         assertFalse(mActionMenuView.isOverflowMenuShowing());
    107 
    108         // Ask to show overflow menu and check that it's showing
    109         mActivityRule.runOnUiThread(mActionMenuView::showOverflowMenu);
    110         mInstrumentation.waitForIdleSync();
    111         assertTrue(mActionMenuView.isOverflowMenuShowing());
    112 
    113         // Ask to hide the overflow menu and check that it's not showing
    114         mActivityRule.runOnUiThread(mActionMenuView::hideOverflowMenu);
    115         mInstrumentation.waitForIdleSync();
    116         assertFalse(mActionMenuView.isOverflowMenuShowing());
    117     }
    118 
    119     @Test
    120     public void testMenuOverflowSubmenu() throws Throwable {
    121         // Inflate menu and check that we're not showing overflow menu yet
    122         mActivityRule.runOnUiThread(
    123                 () -> mActivity.getMenuInflater().inflate(
    124                         R.menu.toolbar_menu, mActionMenuView.getMenu()));
    125         assertFalse(mActionMenuView.isOverflowMenuShowing());
    126 
    127         // Ask to show overflow menu and check that it's showing
    128         mActivityRule.runOnUiThread(mActionMenuView::showOverflowMenu);
    129         mInstrumentation.waitForIdleSync();
    130         assertTrue(mActionMenuView.isOverflowMenuShowing());
    131 
    132         // Register a mock menu item click listener on the toolbar
    133         ActionMenuView.OnMenuItemClickListener menuItemClickListener =
    134                 mock(ActionMenuView.OnMenuItemClickListener.class);
    135         mActionMenuView.setOnMenuItemClickListener(menuItemClickListener);
    136 
    137         final Menu menu = mActionMenuView.getMenu();
    138 
    139         // Ask to "perform" the share action and check that the menu click listener has
    140         // been notified
    141         mActivityRule.runOnUiThread(() -> menu.performIdentifierAction(R.id.action_share, 0));
    142         verify(menuItemClickListener, times(1)).onMenuItemClick(
    143                 menu.findItem(R.id.action_share));
    144 
    145         // Ask to dismiss all the popups and check that we're not showing the overflow menu
    146         mActivityRule.runOnUiThread(mActionMenuView::dismissPopupMenus);
    147         mInstrumentation.waitForIdleSync();
    148         assertFalse(mActionMenuView.isOverflowMenuShowing());
    149     }
    150 
    151     @Test
    152     public void testMenuOverflowIcon() throws Throwable {
    153         // Inflate menu and check that we're not showing overflow menu yet
    154         mActivityRule.runOnUiThread(
    155                 () -> mActivity.getMenuInflater().inflate(
    156                         R.menu.toolbar_menu, mActionMenuView.getMenu()));
    157 
    158         final Drawable overflowIcon = mActivity.getDrawable(R.drawable.icon_red);
    159         WidgetTestUtils.runOnMainAndDrawSync(mActivityRule, mActionMenuView,
    160                 () -> mActionMenuView.setOverflowIcon(overflowIcon));
    161 
    162         final Drawable toolbarOverflowIcon = mActionMenuView.getOverflowIcon();
    163         TestUtils.assertAllPixelsOfColor("Overflow icon is red", toolbarOverflowIcon,
    164                 toolbarOverflowIcon.getIntrinsicWidth(), toolbarOverflowIcon.getIntrinsicHeight(),
    165                 true, Color.RED, 1, false);
    166     }
    167 
    168     @UiThreadTest
    169     @Test
    170     public void testPopupTheme() {
    171         mActivity.getMenuInflater().inflate(R.menu.toolbar_menu, mActionMenuView.getMenu());
    172         mActionMenuView.setPopupTheme(R.style.ToolbarPopupTheme_Test);
    173         assertEquals(R.style.ToolbarPopupTheme_Test, mActionMenuView.getPopupTheme());
    174     }
    175 }
    176