Bug 996227 - Add "Save as PDF" button test. r=mcomella

This commit is contained in:
vivek 2014-07-28 11:54:00 +02:00
parent 45ff6cf5af
commit 0692009b1d
3 changed files with 169 additions and 18 deletions

View File

@ -14,6 +14,7 @@ import org.mozilla.gecko.R;
import org.mozilla.gecko.menu.MenuItemActionBar;
import org.mozilla.gecko.menu.MenuItemDefault;
import org.mozilla.gecko.tests.UITestContext;
import org.mozilla.gecko.tests.helpers.DeviceHelper;
import org.mozilla.gecko.tests.helpers.WaitHelper;
import org.mozilla.gecko.util.HardwareUtils;
@ -27,9 +28,12 @@ import com.jayway.android.robotium.solo.Solo;
* A class representing any interactions that take place on the app menu.
*/
public class AppMenuComponent extends BaseComponent {
private Boolean hasLegacyMenu = null;
public enum MenuItem {
FORWARD(R.string.forward),
NEW_TAB(R.string.new_tab),
PAGE(R.string.page),
RELOAD(R.string.reload);
private final int resourceID;
@ -48,6 +52,27 @@ public class AppMenuComponent extends BaseComponent {
}
};
public enum PageMenuItem {
SAVE_AS_PDF(R.string.save_as_pdf);
private static final MenuItem PARENT_MENU = MenuItem.PAGE;
private final int resourceID;
private String stringResource;
PageMenuItem(final int resourceID) {
this.resourceID = resourceID;
}
public String getString(final Solo solo) {
if (stringResource == null) {
stringResource = solo.getString(resourceID);
}
return stringResource;
}
};
public AppMenuComponent(final UITestContext testContext) {
super(testContext);
}
@ -56,6 +81,54 @@ public class AppMenuComponent extends BaseComponent {
fAssertFalse("Menu is not open", isMenuOpen());
}
/**
* Legacy Android devices doesn't have hierarchical menus. Sub-menus, such as "Page", are missing in these devices.
* Try to determine if the menu item "Page" is present.
*
* TODO : This fragile way to determine legacy menus should be replaced with a check for 6-panel menu item.
*
* @return true if there is a legacy menu.
*/
private boolean hasLegacyMenu() {
if (hasLegacyMenu == null) {
hasLegacyMenu = findAppMenuItemView(MenuItem.PAGE.getString(mSolo)) == null;
}
return hasLegacyMenu;
}
public void assertMenuItemIsDisabledAndVisible(PageMenuItem pageMenuItem) {
openAppMenu();
if (!hasLegacyMenu()) {
// Non-legacy devices have hierarchical menu, check for parent menu item "page".
final View parentMenuItemView = findAppMenuItemView(MenuItem.PAGE.getString(mSolo));
if (parentMenuItemView.isEnabled()) {
fAssertTrue("The parent 'page' menu item is enabled", parentMenuItemView.isEnabled());
fAssertEquals("The parent 'page' menu item is visible", View.VISIBLE,
parentMenuItemView.getVisibility());
// Parent menu "page" is enabled, open page menu and check for menu item represented by pageMenuItem.
pressMenuItem(MenuItem.PAGE.getString(mSolo));
final View pageMenuItemView = findAppMenuItemView(pageMenuItem.getString(mSolo));
fAssertFalse("The page menu item is not enabled", pageMenuItemView.isEnabled());
fAssertEquals("The page menu item is visible", View.VISIBLE, pageMenuItemView.getVisibility());
} else {
fAssertFalse("The parent 'page' menu item is not enabled", parentMenuItemView.isEnabled());
fAssertEquals("The parent 'page' menu item is visible", View.VISIBLE, parentMenuItemView.getVisibility());
}
} else {
// Legacy devices don't have parent menu item "page", check for menu item represented by pageMenuItem.
final View pageMenuItemView = findAppMenuItemView(pageMenuItem.getString(mSolo));
fAssertFalse("The page menu item is not enabled", pageMenuItemView.isEnabled());
fAssertEquals("The page menu item is visible", View.VISIBLE, pageMenuItemView.getVisibility());
}
// Close the App Menu.
mSolo.goBack();
}
private View getOverflowMenuButtonView() {
return mSolo.getView(R.id.menu);
}
@ -87,32 +160,61 @@ public class AppMenuComponent extends BaseComponent {
return null;
}
public void pressMenuItem(MenuItem menuItem) {
openAppMenu();
/**
* Helper function to let Robotium locate and click menu item from legacy Android menu (devices with Android 2.x).
*
* Robotium will also try to open the menu if there are no open dialog.
*
* @param menuItemText, The title of menu item to open.
*/
private void pressLegacyMenuItem(final String menuItemTitle) {
mSolo.clickOnMenuItem(menuItemTitle, true);
}
final String text = menuItem.getString(mSolo);
final View menuItemView = findAppMenuItemView(text);
private void pressMenuItem(final String menuItemTitle) {
fAssertTrue("Menu is open", isMenuOpen(menuItemTitle));
if (menuItemView != null) {
fAssertTrue("The menu item is enabled", menuItemView.isEnabled());
fAssertEquals("The menu item is visible", View.VISIBLE, menuItemView.getVisibility());
if (!hasLegacyMenu()) {
final View menuItemView = findAppMenuItemView(menuItemTitle);
fAssertTrue(String.format("The menu item %s is enabled", menuItemTitle), menuItemView.isEnabled());
fAssertEquals(String.format("The menu item %s is visible", menuItemTitle), View.VISIBLE,
menuItemView.getVisibility());
mSolo.clickOnView(menuItemView);
} else {
// We could not find a view representing this menu item: Let's let Robotium try to
// locate and click it in the legacy Android menu (devices with Android 2.x).
//
// Even though we already opened the menu to see if we can locate the menu item,
// Robotium will also try to open the menu if it doesn't find an open dialog (Does
// not happen in this case).
mSolo.clickOnMenuItem(text, true);
pressLegacyMenuItem(menuItemTitle);
}
}
private void pressSubMenuItem(final String parentMenuItemTitle, final String childMenuItemTitle) {
openAppMenu();
if (!hasLegacyMenu()) {
pressMenuItem(parentMenuItemTitle);
// Child menu item is not pressed yet, Click on it.
pressMenuItem(childMenuItemTitle);
} else {
pressLegacyMenuItem(childMenuItemTitle);
}
}
public void pressMenuItem(MenuItem menuItem) {
openAppMenu();
pressMenuItem(menuItem.getString(mSolo));
}
public void pressMenuItem(final PageMenuItem pageMenuItem) {
pressSubMenuItem(PageMenuItem.PARENT_MENU.getString(mSolo), pageMenuItem.getString(mSolo));
}
private void openAppMenu() {
assertMenuIsNotOpen();
if (HardwareUtils.hasMenuButton()) {
// This is a hack needed for tablets where the OverflowMenuButton is always in the GONE state,
// so we press the menu key instead.
if (HardwareUtils.hasMenuButton() || DeviceHelper.isTablet()) {
mSolo.sendKey(Solo.MENU);
} else {
pressOverflowMenuButton();
@ -130,10 +232,24 @@ public class AppMenuComponent extends BaseComponent {
mSolo.clickOnView(overflowMenuButton, true);
}
/**
* Determines whether the app menu is open by searching for the text "New tab".
*
* @return true if app menu is open.
*/
private boolean isMenuOpen() {
// The presence of the "New tab" menu item is our best guess about whether
// the menu is open or not.
return mSolo.searchText(MenuItem.NEW_TAB.getString(mSolo));
return isMenuOpen(MenuItem.NEW_TAB.getString(mSolo));
}
/**
* Determines whether the app menu is open by searching for the text in menuItemTitle.
*
* @param menuItemTitle, The contentDescription of menu item to search.
*
* @return true if app menu is open.
*/
private boolean isMenuOpen(String menuItemTitle) {
return mSolo.searchText(menuItemTitle);
}
private void waitForMenuOpen() {

View File

@ -122,6 +122,7 @@ skip-if = android_version == "10"
[testAboutHomeVisibility]
# disabled on Android 2.3; bug 946656
skip-if = android_version == "10"
[testAppMenuPathways]
[testEventDispatcher]
[testInputConnection]
# disabled on Android 2.3; bug 1025968

View File

@ -0,0 +1,34 @@
package org.mozilla.gecko.tests;
import org.mozilla.gecko.tests.components.AppMenuComponent;
import org.mozilla.gecko.tests.helpers.GeckoHelper;
import org.mozilla.gecko.tests.helpers.NavigationHelper;
/**
* Set of tests to test UI App menu and submenus the user interact with.
*/
public class testAppMenuPathways extends UITest {
/**
* Robocop supports only a single test function per test class. Therefore, we
* have a single top-level test function that dispatches to sub-tests.
*/
public void testAppMenuPathways() {
GeckoHelper.blockForReady();
_testSaveAsPDFPathway();
}
public void _testSaveAsPDFPathway() {
// Page menu should be disabled in about:home.
mAppMenu.assertMenuItemIsDisabledAndVisible(AppMenuComponent.PageMenuItem.SAVE_AS_PDF);
// Navigate to a page to test save as pdf functionality.
NavigationHelper.enterAndLoadUrl(StringHelper.ROBOCOP_BLANK_PAGE_01_URL);
mToolbar.assertTitle(StringHelper.ROBOCOP_BLANK_PAGE_01_TITLE);
// Test save as pdf functionality.
// The following call doesn't wait for the resulting pdf but checks that no exception are thrown.
mAppMenu.pressMenuItem(AppMenuComponent.PageMenuItem.SAVE_AS_PDF);
}
}