mobile/android/base/tests/testReaderMode.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 package org.mozilla.gecko.tests;
michael@0 2
michael@0 3 import org.json.JSONException;
michael@0 4 import org.json.JSONObject;
michael@0 5 import org.mozilla.gecko.Actions;
michael@0 6 import org.mozilla.gecko.R;
michael@0 7
michael@0 8 import android.view.View;
michael@0 9 import android.view.ViewGroup;
michael@0 10 import android.widget.ListView;
michael@0 11
michael@0 12 import com.jayway.android.robotium.solo.Condition;
michael@0 13 import com.jayway.android.robotium.solo.Solo;
michael@0 14
michael@0 15 /**
michael@0 16 * This patch tests the Reader Mode feature by adding and removing items in reading list
michael@0 17 * checks the reader toolbar functionality(share, add/remove to reading list, go to reading list)
michael@0 18 * accessing a page from reading list menu, checks that the reader icon is associated in History tab
michael@0 19 * and that the reading list is properly populated after adding or removing reader items
michael@0 20 */
michael@0 21 public class testReaderMode extends AboutHomeTest {
michael@0 22 static final int EVENT_CLEAR_DELAY_MS = 3000;
michael@0 23 static final int READER_ICON_MAX_WAIT_MS = 15000;
michael@0 24
michael@0 25 public void testReaderMode() {
michael@0 26 blockForGeckoReady();
michael@0 27
michael@0 28 Actions.EventExpecter contentEventExpecter;
michael@0 29 Actions.EventExpecter contentReaderAddedExpecter;
michael@0 30 Actions.EventExpecter faviconExpecter;
michael@0 31 Actions.EventExpecter contentPageShowExpecter;
michael@0 32 Actions.RepeatedEventExpecter paintExpecter;
michael@0 33 ListView list;
michael@0 34 View child;
michael@0 35 View readerIcon;
michael@0 36 String textUrl = getAbsoluteUrl(StringHelper.ROBOCOP_TEXT_PAGE_URL);
michael@0 37 String devType = mDevice.type;
michael@0 38 int childNo;
michael@0 39 int height;
michael@0 40 int width;
michael@0 41
michael@0 42 loadAndPaint(textUrl);
michael@0 43
michael@0 44 // Add the page to the Reading List using long click on the reader icon
michael@0 45 readerIcon = getReaderIcon();
michael@0 46 contentReaderAddedExpecter = mActions.expectGeckoEvent("Reader:Added");
michael@0 47 mSolo.clickLongOnView(readerIcon);
michael@0 48 String eventData = contentReaderAddedExpecter.blockForEventData();
michael@0 49 isAdded(eventData);
michael@0 50 contentReaderAddedExpecter.unregisterListener();
michael@0 51
michael@0 52 // Try to add the page to the Reading List using long click on the reader icon a second time
michael@0 53 readerIcon = getReaderIcon();
michael@0 54 contentReaderAddedExpecter = mActions.expectGeckoEvent("Reader:Added");
michael@0 55 mSolo.clickLongOnView(readerIcon);
michael@0 56 eventData = contentReaderAddedExpecter.blockForEventData();
michael@0 57 isAdded(eventData);
michael@0 58 contentReaderAddedExpecter.unregisterListener();
michael@0 59
michael@0 60 // Waiting for the favicon since is the last element loaded usually
michael@0 61 faviconExpecter = mActions.expectGeckoEvent("Reader:FaviconRequest");
michael@0 62 contentPageShowExpecter = mActions.expectGeckoEvent("Content:PageShow");
michael@0 63 readerIcon = getReaderIcon();
michael@0 64 paintExpecter = mActions.expectPaint();
michael@0 65 mSolo.clickOnView(readerIcon);
michael@0 66
michael@0 67 // Changing devices orientation to be sure that all devices are in portrait when will access the reader toolbar
michael@0 68 mSolo.setActivityOrientation(Solo.PORTRAIT);
michael@0 69 faviconExpecter.blockForEvent();
michael@0 70 faviconExpecter.unregisterListener();
michael@0 71 contentPageShowExpecter.blockForEvent();
michael@0 72 contentPageShowExpecter.unregisterListener();
michael@0 73 paintExpecter.blockUntilClear(EVENT_CLEAR_DELAY_MS);
michael@0 74 paintExpecter.unregisterListener();
michael@0 75 verifyPageTitle("Robocop Text Page");
michael@0 76
michael@0 77 // Open the share menu for the reader toolbar
michael@0 78 height = mDriver.getGeckoTop() + mDriver.getGeckoHeight() - 10;
michael@0 79 width = mDriver.getGeckoLeft() + mDriver.getGeckoWidth() - 10;
michael@0 80 mAsserter.dumpLog("Long Clicking at width = " + String.valueOf(width) + " and height = " + String.valueOf(height));
michael@0 81 mSolo.clickOnScreen(width,height);
michael@0 82 mAsserter.ok(mSolo.waitForText("Share via"), "Waiting for the share menu", "The share menu is present");
michael@0 83 mActions.sendSpecialKey(Actions.SpecialKey.BACK); // Close the share menu
michael@0 84
michael@0 85 // Remove page from the Reading List using reader toolbar
michael@0 86 height = mDriver.getGeckoTop() + mDriver.getGeckoHeight() - 10;
michael@0 87 width = mDriver.getGeckoLeft() + 50;
michael@0 88 mAsserter.dumpLog("Long Clicking at width = " + String.valueOf(width) + " and height = " + String.valueOf(height));
michael@0 89 mSolo.clickOnScreen(width,height);
michael@0 90 mAsserter.ok(mSolo.waitForText("Page removed from your Reading List"), "Waiting for the page to removed from your Reading List", "The page is removed from your Reading List");
michael@0 91
michael@0 92 //Add page to the Reading List using reader toolbar
michael@0 93 mSolo.clickOnScreen(width,height);
michael@0 94 mAsserter.ok(mSolo.waitForText("Page added to your Reading List"), "Waiting for the page to be added to your Reading List", "The page was added to your Reading List");
michael@0 95
michael@0 96 // Open the Reading List menu for the toolbar
michael@0 97 height = mDriver.getGeckoTop() + mDriver.getGeckoHeight() - 10;
michael@0 98 width = mDriver.getGeckoLeft() + mDriver.getGeckoWidth()/2 - 10;
michael@0 99 mAsserter.dumpLog("Long Clicking at width = " + String.valueOf(width) + " and height = " + String.valueOf(height));
michael@0 100 contentEventExpecter = mActions.expectGeckoEvent("DOMContentLoaded");
michael@0 101 mSolo.clickOnScreen(width,height);
michael@0 102 contentEventExpecter.blockForEvent();
michael@0 103 contentEventExpecter.unregisterListener();
michael@0 104
michael@0 105 // Check if the page is present in the Reading List
michael@0 106 mAsserter.ok(mSolo.waitForText("Robocop Text Page"), "Verify if the page is added to your Reading List", "The page is present in your Reading List");
michael@0 107
michael@0 108 // Check if the page is added in History tab like a Reading List item
michael@0 109 openAboutHomeTab(AboutHomeTabs.MOST_RECENT);
michael@0 110 list = findListViewWithTag("most_recent");
michael@0 111 child = list.getChildAt(1);
michael@0 112 mAsserter.ok(child != null, "item can be retrieved", child != null ? child.toString() : "null!");
michael@0 113 mSolo.clickLongOnView(child);
michael@0 114 mAsserter.ok(mSolo.waitForText("Open in Reader"), "Verify if the page is present in history as a Reading List item", "The page is present in history as a Reading List item");
michael@0 115 mActions.sendSpecialKey(Actions.SpecialKey.BACK); // Dismiss the context menu
michael@0 116 mSolo.waitForText("Robocop Text Page");
michael@0 117
michael@0 118 // Verify separately the Reading List entries for tablets and phone because for tablets there is an extra child in UI design
michael@0 119 if (devType.equals("phone")) {
michael@0 120 childNo = 1;
michael@0 121 }
michael@0 122 else {
michael@0 123 childNo = 2;
michael@0 124 }
michael@0 125 // Verify if the page is present to your Reading List
michael@0 126 openAboutHomeTab(AboutHomeTabs.READING_LIST);
michael@0 127 list = findListViewWithTag("reading_list");
michael@0 128 child = list.getChildAt(childNo-1);
michael@0 129 mAsserter.ok(child != null, "Verify if the page is present to your Reading List", "The page is present in your Reading List");
michael@0 130 contentEventExpecter = mActions.expectGeckoEvent("DOMContentLoaded");
michael@0 131 mSolo.clickOnView(child);
michael@0 132 contentEventExpecter.blockForEvent();
michael@0 133 contentEventExpecter.unregisterListener();
michael@0 134 verifyPageTitle("Robocop Text Page");
michael@0 135
michael@0 136 // Verify that we are in reader mode and remove the page from Reading List
michael@0 137 height = mDriver.getGeckoTop() + mDriver.getGeckoHeight() - 10;
michael@0 138 width = mDriver.getGeckoLeft() + 50;
michael@0 139 mAsserter.dumpLog("Long Clicking at width = " + String.valueOf(width) + " and height = " + String.valueOf(height));
michael@0 140 mSolo.clickOnScreen(width,height);
michael@0 141 mAsserter.ok(mSolo.waitForText("Page removed from your Reading List"), "Waiting for the page to removed from your Reading List", "The page is removed from your Reading List");
michael@0 142 verifyPageTitle("Robocop Text Page");
michael@0 143
michael@0 144 //Check if the Reading List is empty
michael@0 145 openAboutHomeTab(AboutHomeTabs.READING_LIST);
michael@0 146 list = findListViewWithTag("reading_list");
michael@0 147 child = list.getChildAt(childNo-1);
michael@0 148 mAsserter.ok(child == null, "Verify if the Reading List is empty", "The Reading List is empty");
michael@0 149 }
michael@0 150
michael@0 151 // Get the reader icon method
michael@0 152 protected View getReaderIcon() {
michael@0 153 View pageActionLayout = mSolo.getView(R.id.page_action_layout);
michael@0 154 final ViewGroup actionLayoutEntry = (ViewGroup)pageActionLayout;
michael@0 155 View icon = actionLayoutEntry.getChildAt(1);
michael@0 156 if (icon == null || icon.getVisibility() != View.VISIBLE) {
michael@0 157 // wait for the view to be visible, otherwise it may not respond
michael@0 158 // to clicks -- see bug 927578
michael@0 159 mAsserter.dumpLog("reader icon not visible -- waiting for visibility");
michael@0 160 Condition visibilityCondition = new Condition() {
michael@0 161 @Override
michael@0 162 public boolean isSatisfied() {
michael@0 163 View conditionIcon = actionLayoutEntry.getChildAt(1);
michael@0 164 if (conditionIcon == null ||
michael@0 165 conditionIcon.getVisibility() != View.VISIBLE)
michael@0 166 return false;
michael@0 167 return true;
michael@0 168 }
michael@0 169 };
michael@0 170 waitForCondition(visibilityCondition, READER_ICON_MAX_WAIT_MS);
michael@0 171 icon = actionLayoutEntry.getChildAt(1);
michael@0 172 mAsserter.ok(icon != null, "checking reader icon view", "reader icon view not null");
michael@0 173 mAsserter.ok(icon.getVisibility() == View.VISIBLE, "checking reader icon visible", "reader icon visible");
michael@0 174 }
michael@0 175 return icon;
michael@0 176 }
michael@0 177
michael@0 178 // This method check to see if a reader item is added to the reader list
michael@0 179 private boolean isAdded(String eventData) {
michael@0 180 try {
michael@0 181 JSONObject data = new JSONObject(eventData);
michael@0 182 if (data.getInt("result") == 0) {
michael@0 183 mAsserter.ok(true, "Waiting for the page to be added to your Reading List", "The page was added to your Reading List");
michael@0 184 }
michael@0 185 else {
michael@0 186 if (data.getInt("result") == 2) {
michael@0 187 mAsserter.ok(true, "Trying to add a second time the page in your Reading List", "The page is already in your Reading List");
michael@0 188 }
michael@0 189 }
michael@0 190 } catch (JSONException e) {
michael@0 191 mAsserter.ok(false, "Error parsing the event data", e.toString());
michael@0 192 return false;
michael@0 193 }
michael@0 194 return true;
michael@0 195 }
michael@0 196 }

mercurial