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 17package com.android.tv.testing.uihelper; 18 19import static com.android.tv.testing.uihelper.Constants.MENU; 20 21import android.content.res.Resources; 22import android.support.test.uiautomator.By; 23import android.support.test.uiautomator.BySelector; 24import android.support.test.uiautomator.Direction; 25import android.support.test.uiautomator.UiDevice; 26import android.support.test.uiautomator.UiObject2; 27import android.support.test.uiautomator.Until; 28 29import com.android.tv.R; 30 31import junit.framework.Assert; 32 33/** 34 * Helper for testing {@link com.android.tv.menu.Menu}. 35 */ 36public class MenuHelper extends BaseUiDeviceHelper { 37 private final BySelector byChannels; 38 39 public MenuHelper(UiDevice uiDevice, Resources targetResources) { 40 super(uiDevice, targetResources); 41 byChannels = ByResource.id(mTargetResources, R.id.item_list) 42 .hasDescendant(ByResource.text(mTargetResources, R.string.menu_title_channels)); 43 } 44 45 public BySelector getByChannels() { 46 return byChannels; 47 } 48 49 50 /** 51 * Navigate to the menu item with the text {@code itemTextResId} in the row with text 52 * {@code rowTitleResId}. 53 * <p> 54 * Fails if the menu item can not be navigated to. 55 * 56 * @param rowTitleResId the resource id of the string in the desired row title. 57 * @param itemTextResId the resource id of the string in the desired item. 58 * @return the item navigated to. 59 */ 60 public UiObject2 assertNavigateToMenuItem(int rowTitleResId, int itemTextResId) { 61 UiObject2 row = assertNavigateToRow(rowTitleResId); 62 BySelector byListView = ByResource.id(mTargetResources, R.id.list_view); 63 UiObject2 listView = row.findObject(byListView); 64 Assert.assertNotNull( 65 "Menu row '" + mTargetResources.getString(rowTitleResId) + "' does not have a " 66 + byListView, listView); 67 return assertNavigateToRowItem(listView, itemTextResId); 68 } 69 70 /** 71 * Navigate to the menu row with the text title {@code rowTitleResId}. 72 * <p> 73 * Fails if the menu row can not be navigated to. 74 * We can't navigate to the Play controls row with this method, because the row doesn't have the 75 * title when it is selected. Use {@link #assertNavigateToPlayControlsRow} for the row instead. 76 * 77 * @param rowTitleResId the resource id of the string in the desired row title. 78 * @return the row navigated to. 79 */ 80 public UiObject2 assertNavigateToRow(int rowTitleResId) { 81 UiDeviceAsserts.assertHas(mUiDevice, MENU, true); 82 UiObject2 menu = mUiDevice.findObject(MENU); 83 // TODO: handle play controls. They have a different dom structure and navigation sometimes 84 // can get stuck on that row. 85 return UiDeviceAsserts.assertNavigateTo(mUiDevice, menu, 86 By.hasDescendant(ByResource.text(mTargetResources, rowTitleResId)), Direction.DOWN); 87 } 88 89 /** 90 * Navigate to the Play controls row. 91 * <p> 92 * Fails if the row can not be navigated to. 93 * 94 * @see #assertNavigateToRow 95 */ 96 public void assertNavigateToPlayControlsRow() { 97 UiDeviceAsserts.assertHas(mUiDevice, MENU, true); 98 // The play controls row doesn't have title when selected, so can't use 99 // MenuHelper.assertNavigateToRow(). 100 assertNavigateToRow(R.string.menu_title_channels); 101 mUiDevice.pressDPadUp(); 102 } 103 104 /** 105 * Navigate to the menu item in the given {@code row} with the text {@code itemTextResId} . 106 * <p> 107 * Fails if the menu item can not be navigated to. 108 * 109 * @param row the container to look for menu items in. 110 * @param itemTextResId the resource id of the string in the desired item. 111 * @return the item navigated to. 112 */ 113 public UiObject2 assertNavigateToRowItem(UiObject2 row, int itemTextResId) { 114 return UiDeviceAsserts.assertNavigateTo(mUiDevice, row, 115 By.hasDescendant(ByResource.text(mTargetResources, itemTextResId)), 116 Direction.RIGHT); 117 } 118 119 public UiObject2 assertPressOptionsSettings() { 120 return assertPressMenuItem(R.string.menu_title_options, 121 R.string.options_item_settings); 122 } 123 124 public UiObject2 assertPressOptionsClosedCaptions() { 125 return assertPressMenuItem(R.string.menu_title_options, 126 R.string.options_item_closed_caption); 127 } 128 129 public UiObject2 assertPressOptionsDisplayMode() { 130 return assertPressMenuItem(R.string.menu_title_options, R.string.options_item_display_mode); 131 } 132 133 public UiObject2 assertPressOptionsMultiAudio() { 134 return assertPressMenuItem(R.string.menu_title_options, R.string.options_item_multi_audio); 135 } 136 137 public UiObject2 assertPressProgramGuide() { 138 return assertPressMenuItem(R.string.menu_title_channels, 139 R.string.channels_item_program_guide); 140 } 141 142 /** 143 * Navigate to the menu item with the text {@code itemTextResId} in the row with text 144 * {@code rowTitleResId}. 145 * <p> 146 * Fails if the menu item can not be navigated to. 147 * 148 * @param rowTitleResId the resource id of the string in the desired row title. 149 * @param itemTextResId the resource id of the string in the desired item. 150 * @return the item navigated to. 151 */ 152 public UiObject2 assertPressMenuItem(int rowTitleResId, int itemTextResId) { 153 showMenu(); 154 UiObject2 item = assertNavigateToMenuItem(rowTitleResId, itemTextResId); 155 mUiDevice.pressDPadCenter(); 156 return item; 157 } 158 159 /** 160 * Waits until the menu is visible. 161 */ 162 public void assertWaitForMenu() { 163 UiDeviceAsserts.assertWaitForCondition(mUiDevice, Until.hasObject(MENU)); 164 } 165 166 /** 167 * Show the menu. 168 * <p> 169 * Fails if the menu does not appear in {@link Constants#MAX_SHOW_DELAY_MILLIS}. 170 */ 171 public void showMenu() { 172 if (!mUiDevice.hasObject(MENU)) { 173 mUiDevice.pressMenu(); 174 UiDeviceAsserts.assertWaitForCondition(mUiDevice, Until.hasObject(MENU)); 175 } 176 } 177} 178