Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko.tests.components; |
michael@0 | 6 | |
michael@0 | 7 | import static org.mozilla.gecko.tests.helpers.AssertionHelper.fAssertEquals; |
michael@0 | 8 | import static org.mozilla.gecko.tests.helpers.AssertionHelper.fAssertFalse; |
michael@0 | 9 | import static org.mozilla.gecko.tests.helpers.AssertionHelper.fAssertTrue; |
michael@0 | 10 | |
michael@0 | 11 | import java.util.List; |
michael@0 | 12 | |
michael@0 | 13 | import org.mozilla.gecko.R; |
michael@0 | 14 | import org.mozilla.gecko.menu.MenuItemActionBar; |
michael@0 | 15 | import org.mozilla.gecko.menu.MenuItemDefault; |
michael@0 | 16 | import org.mozilla.gecko.tests.UITestContext; |
michael@0 | 17 | import org.mozilla.gecko.tests.helpers.WaitHelper; |
michael@0 | 18 | import org.mozilla.gecko.util.HardwareUtils; |
michael@0 | 19 | |
michael@0 | 20 | import android.view.View; |
michael@0 | 21 | |
michael@0 | 22 | import com.jayway.android.robotium.solo.Condition; |
michael@0 | 23 | import com.jayway.android.robotium.solo.RobotiumUtils; |
michael@0 | 24 | import com.jayway.android.robotium.solo.Solo; |
michael@0 | 25 | |
michael@0 | 26 | /** |
michael@0 | 27 | * A class representing any interactions that take place on the app menu. |
michael@0 | 28 | */ |
michael@0 | 29 | public class AppMenuComponent extends BaseComponent { |
michael@0 | 30 | public enum MenuItem { |
michael@0 | 31 | FORWARD(R.string.forward), |
michael@0 | 32 | NEW_TAB(R.string.new_tab); |
michael@0 | 33 | |
michael@0 | 34 | private final int resourceID; |
michael@0 | 35 | private String stringResource; |
michael@0 | 36 | |
michael@0 | 37 | MenuItem(final int resourceID) { |
michael@0 | 38 | this.resourceID = resourceID; |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | public String getString(final Solo solo) { |
michael@0 | 42 | if (stringResource == null) { |
michael@0 | 43 | stringResource = solo.getString(resourceID); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | return stringResource; |
michael@0 | 47 | } |
michael@0 | 48 | }; |
michael@0 | 49 | |
michael@0 | 50 | public AppMenuComponent(final UITestContext testContext) { |
michael@0 | 51 | super(testContext); |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | private void assertMenuIsNotOpen() { |
michael@0 | 55 | fAssertFalse("Menu is not open", isMenuOpen()); |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | private View getOverflowMenuButtonView() { |
michael@0 | 59 | return mSolo.getView(R.id.menu); |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | /** |
michael@0 | 63 | * Try to find a MenuItemActionBar/MenuItemDefault with the given text set as contentDescription / text. |
michael@0 | 64 | * |
michael@0 | 65 | * Will return null when the Android legacy menu is in use. |
michael@0 | 66 | * |
michael@0 | 67 | * This method is dependent on not having two views with equivalent contentDescription / text. |
michael@0 | 68 | */ |
michael@0 | 69 | private View findAppMenuItemView(String text) { |
michael@0 | 70 | final List<View> views = mSolo.getViews(); |
michael@0 | 71 | |
michael@0 | 72 | final List<MenuItemActionBar> menuItemActionBarList = RobotiumUtils.filterViews(MenuItemActionBar.class, views); |
michael@0 | 73 | for (MenuItemActionBar menuItem : menuItemActionBarList) { |
michael@0 | 74 | if (menuItem.getContentDescription().equals(text)) { |
michael@0 | 75 | return menuItem; |
michael@0 | 76 | } |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | final List<MenuItemDefault> menuItemDefaultList = RobotiumUtils.filterViews(MenuItemDefault.class, views); |
michael@0 | 80 | for (MenuItemDefault menuItem : menuItemDefaultList) { |
michael@0 | 81 | if (menuItem.getText().equals(text)) { |
michael@0 | 82 | return menuItem; |
michael@0 | 83 | } |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | return null; |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | public void pressMenuItem(MenuItem menuItem) { |
michael@0 | 90 | openAppMenu(); |
michael@0 | 91 | |
michael@0 | 92 | final String text = menuItem.getString(mSolo); |
michael@0 | 93 | final View menuItemView = findAppMenuItemView(text); |
michael@0 | 94 | |
michael@0 | 95 | if (menuItemView != null) { |
michael@0 | 96 | fAssertTrue("The menu item is enabled", menuItemView.isEnabled()); |
michael@0 | 97 | fAssertEquals("The menu item is visible", View.VISIBLE, menuItemView.getVisibility()); |
michael@0 | 98 | |
michael@0 | 99 | mSolo.clickOnView(menuItemView); |
michael@0 | 100 | } else { |
michael@0 | 101 | // We could not find a view representing this menu item: Let's let Robotium try to |
michael@0 | 102 | // locate and click it in the legacy Android menu (devices with Android 2.x). |
michael@0 | 103 | // |
michael@0 | 104 | // Even though we already opened the menu to see if we can locate the menu item, |
michael@0 | 105 | // Robotium will also try to open the menu if it doesn't find an open dialog (Does |
michael@0 | 106 | // not happen in this case). |
michael@0 | 107 | mSolo.clickOnMenuItem(text, true); |
michael@0 | 108 | } |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | private void openAppMenu() { |
michael@0 | 112 | assertMenuIsNotOpen(); |
michael@0 | 113 | |
michael@0 | 114 | if (HardwareUtils.hasMenuButton()) { |
michael@0 | 115 | mSolo.sendKey(Solo.MENU); |
michael@0 | 116 | } else { |
michael@0 | 117 | pressOverflowMenuButton(); |
michael@0 | 118 | } |
michael@0 | 119 | |
michael@0 | 120 | waitForMenuOpen(); |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | private void pressOverflowMenuButton() { |
michael@0 | 124 | final View overflowMenuButton = getOverflowMenuButtonView(); |
michael@0 | 125 | |
michael@0 | 126 | fAssertTrue("The overflow menu button is enabled", overflowMenuButton.isEnabled()); |
michael@0 | 127 | fAssertEquals("The overflow menu button is visible", View.VISIBLE, overflowMenuButton.getVisibility()); |
michael@0 | 128 | |
michael@0 | 129 | mSolo.clickOnView(overflowMenuButton, true); |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | private boolean isMenuOpen() { |
michael@0 | 133 | // The presence of the "New tab" menu item is our best guess about whether |
michael@0 | 134 | // the menu is open or not. |
michael@0 | 135 | return mSolo.searchText(MenuItem.NEW_TAB.getString(mSolo)); |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | private void waitForMenuOpen() { |
michael@0 | 139 | WaitHelper.waitFor("menu to open", new Condition() { |
michael@0 | 140 | @Override |
michael@0 | 141 | public boolean isSatisfied() { |
michael@0 | 142 | return isMenuOpen(); |
michael@0 | 143 | } |
michael@0 | 144 | }); |
michael@0 | 145 | } |
michael@0 | 146 | } |