1 /* 2 * Copyright (C) 2015 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.espresso; 18 19 import static android.support.test.espresso.Espresso.onView; 20 import static android.support.test.espresso.assertion.ViewAssertions.matches; 21 import static android.support.test.espresso.matcher.RootMatchers.withDecorView; 22 import static android.support.test.espresso.matcher.ViewMatchers.hasDescendant; 23 import static android.support.test.espresso.matcher.ViewMatchers.hasFocus; 24 import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom; 25 import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; 26 import static android.support.test.espresso.matcher.ViewMatchers.isEnabled; 27 import static android.support.test.espresso.matcher.ViewMatchers.withText; 28 import static org.hamcrest.Matchers.allOf; 29 import static org.hamcrest.Matchers.not; 30 31 import com.android.internal.view.menu.ListMenuItemView; 32 33 import android.support.test.espresso.NoMatchingRootException; 34 import android.support.test.espresso.NoMatchingViewException; 35 import android.support.test.espresso.ViewInteraction; 36 import android.support.test.espresso.matcher.ViewMatchers; 37 import android.widget.MenuPopupWindow.MenuDropDownListView; 38 39 /** 40 * Espresso utility methods for the context menu. 41 */ 42 public final class ContextMenuUtils { 43 private ContextMenuUtils() {} 44 45 private static ViewInteraction onContextMenu() { 46 // TODO: Have more reliable way to get context menu. 47 return onView(ViewMatchers.isAssignableFrom(MenuDropDownListView.class)) 48 .inRoot(withDecorView(hasFocus())); 49 } 50 51 /** 52 * Asserts that the context menu is displayed 53 * 54 * @throws AssertionError if the assertion fails 55 */ 56 private static void assertContextMenuIsDisplayed() { 57 onContextMenu().check(matches(isDisplayed())); 58 } 59 60 /** 61 * Asserts that the context menu is not displayed 62 * 63 * @throws AssertionError if the assertion fails 64 */ 65 public static void assertContextMenuIsNotDisplayed() { 66 try { 67 assertContextMenuIsDisplayed(); 68 } catch (NoMatchingRootException | NoMatchingViewException | AssertionError e) { 69 return; 70 } 71 throw new AssertionError("Context menu is displayed"); 72 } 73 74 /** 75 * Asserts that the context menu contains the specified item and the item has specified enabled 76 * state. 77 * 78 * @param itemLabel label of the item. 79 * @param enabled enabled state of the item. 80 * @throws AssertionError if the assertion fails 81 */ 82 private static void asssertContextMenuContainsItemWithEnabledState(String itemLabel, 83 boolean enabled) { 84 onContextMenu().check(matches( 85 hasDescendant(allOf( 86 isAssignableFrom(ListMenuItemView.class), 87 enabled ? isEnabled() : not(isEnabled()), 88 hasDescendant(withText(itemLabel)))))); 89 } 90 91 /** 92 * Asserts that the context menu contains the specified item and the item is enabled. 93 * 94 * @param itemLabel label of the item. 95 * @throws AssertionError if the assertion fails 96 */ 97 public static void assertContextMenuContainsItemEnabled(String itemLabel) { 98 asssertContextMenuContainsItemWithEnabledState(itemLabel, true); 99 } 100 101 /** 102 * Asserts that the context menu contains the specified item and the item is disabled. 103 * 104 * @param itemLabel label of the item. 105 * @throws AssertionError if the assertion fails 106 */ 107 public static void assertContextMenuContainsItemDisabled(String itemLabel) { 108 asssertContextMenuContainsItemWithEnabledState(itemLabel, false); 109 } 110 } 111