/mobile/android/tests/browser/robocop/src/org/mozilla/gecko/tests/ContentContextMenuTest.java

https://github.com/rillian/firefox · Java · 135 lines · 101 code · 24 blank · 10 comment · 10 complexity · 5816023ebdb8bc71fe233b690ccb9475 MD5 · raw file

  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. package org.mozilla.gecko.tests;
  5. import org.mozilla.gecko.Actions;
  6. import org.mozilla.gecko.util.Clipboard;
  7. import org.mozilla.gecko.Element;
  8. import org.mozilla.gecko.R;
  9. import android.util.DisplayMetrics;
  10. import com.robotium.solo.Condition;
  11. /**
  12. * This class covers interactions with the context menu opened from web content
  13. */
  14. abstract class ContentContextMenuTest extends PixelTest {
  15. private static final int MAX_TEST_TIMEOUT = 30000; // 30 seconds (worst case)
  16. // This method opens the context menu of any web content. It assumes that the page is already loaded
  17. protected void openWebContentContextMenu(String waitText) {
  18. DisplayMetrics dm = new DisplayMetrics();
  19. getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
  20. // The web content we are trying to open the context menu for should be positioned at the top of the page, at least 60px high and aligned to the middle
  21. float top = mDriver.getGeckoTop() + 30 * dm.density;
  22. float left = mDriver.getGeckoLeft() + mDriver.getGeckoWidth() / 2;
  23. mAsserter.dumpLog("long-clicking at "+left+", "+top);
  24. mSolo.clickLongOnScreen(left, top);
  25. waitForText(waitText);
  26. }
  27. protected void verifyContextMenuItems(String[] items) {
  28. // Test that the menu items are displayed
  29. if (!mSolo.searchText(items[0])) {
  30. openWebContentContextMenu(items[0]); // Open the context menu if it is not already
  31. }
  32. for (String option:items) {
  33. mAsserter.ok(mSolo.searchText(option), "Checking that the option: " + option + " is available", "The option is available");
  34. }
  35. }
  36. protected void openTabFromContextMenu(String contextMenuOption, int expectedTabCount) {
  37. if (!mSolo.searchText(contextMenuOption)) {
  38. openWebContentContextMenu(contextMenuOption); // Open the context menu if it is not already
  39. }
  40. Actions.EventExpecter tabEventExpecter = mActions.expectGlobalEvent(Actions.EventType.UI, "Tab:Added");
  41. mSolo.clickOnText(contextMenuOption);
  42. tabEventExpecter.blockForEvent();
  43. tabEventExpecter.unregisterListener();
  44. verifyTabCount(expectedTabCount);
  45. }
  46. protected void verifyTabs(String[] items) {
  47. if (!mSolo.searchText(items[0])) {
  48. openWebContentContextMenu(items[0]);
  49. }
  50. for (String option:items) {
  51. mAsserter.ok(mSolo.searchText(option), "Checking that the option: " + option + " is available", "The option is available");
  52. }
  53. }
  54. protected void switchTabs(String tab) {
  55. if (!mSolo.searchText(tab)) {
  56. openWebContentContextMenu(tab);
  57. }
  58. mSolo.clickOnText(tab);
  59. }
  60. protected void verifyCopyOption(String copyOption, final String copiedText) {
  61. if (!mSolo.searchText(copyOption)) {
  62. openWebContentContextMenu(copyOption); // Open the context menu if it is not already
  63. }
  64. mSolo.clickOnText(copyOption);
  65. boolean correctText = waitForCondition(new Condition() {
  66. @Override
  67. public boolean isSatisfied() {
  68. final String clipboardText = Clipboard.getText();
  69. mAsserter.dumpLog("Clipboard text = " + clipboardText + " , expected text = " + copiedText);
  70. return clipboardText.contains(copiedText);
  71. }
  72. }, MAX_TEST_TIMEOUT);
  73. mAsserter.ok(correctText, "Checking if the text is correctly copied", "The text was correctly copied");
  74. }
  75. protected void verifyShareOption(String shareOption, String pageTitle) {
  76. waitForText(pageTitle); // Even if this fails, it won't assert
  77. if (!mSolo.searchText(shareOption)) {
  78. openWebContentContextMenu(shareOption); // Open the context menu if it is not already
  79. }
  80. mSolo.clickOnText(shareOption);
  81. mAsserter.ok(waitForText(shareOption), "Checking that the share pop-up is displayed", "The pop-up has been displayed");
  82. // Close the Share Link option menu and wait for the page to be focused again
  83. mSolo.goBack();
  84. waitForText(pageTitle);
  85. }
  86. protected void verifyViewImageOption(String viewImageOption, final String imageUrl, String pageTitle) {
  87. if (!mSolo.searchText(viewImageOption)) {
  88. openWebContentContextMenu(viewImageOption);
  89. }
  90. mSolo.clickOnText(viewImageOption);
  91. boolean viewedImage = waitForCondition(new Condition() {
  92. @Override
  93. public boolean isSatisfied() {
  94. final Element urlBarElement = mDriver.findElement(getActivity(), R.id.url_edit_text);
  95. final String loadedUrl = urlBarElement.getText();
  96. return loadedUrl.contentEquals(imageUrl);
  97. }
  98. }, MAX_TEST_TIMEOUT);
  99. mAsserter.ok(viewedImage, "Checking if the image is correctly viewed", "The image was correctly viewed");
  100. mSolo.goBack();
  101. waitForText(pageTitle);
  102. }
  103. protected void verifyBookmarkLinkOption(String bookmarkOption, String link) {
  104. if (!mSolo.searchText(bookmarkOption)) {
  105. openWebContentContextMenu(bookmarkOption); // Open the context menu if it is not already
  106. }
  107. mSolo.clickOnText(bookmarkOption);
  108. mAsserter.ok(waitForText("Bookmark added"), "Waiting for the Bookmark added toaster notification", "The notification has been displayed");
  109. mAsserter.ok(mDatabaseHelper.isBookmark(link), "Checking if the link has been added as a bookmark", "The link has been bookmarked");
  110. }
  111. }