michael@0: package org.mozilla.gecko.tests; michael@0: michael@0: import org.json.JSONException; michael@0: import org.json.JSONObject; michael@0: import org.mozilla.gecko.Actions; michael@0: import org.mozilla.gecko.R; michael@0: michael@0: import android.view.View; michael@0: import android.view.ViewGroup; michael@0: import android.widget.ListView; michael@0: michael@0: import com.jayway.android.robotium.solo.Condition; michael@0: import com.jayway.android.robotium.solo.Solo; michael@0: michael@0: /** michael@0: * This patch tests the Reader Mode feature by adding and removing items in reading list michael@0: * checks the reader toolbar functionality(share, add/remove to reading list, go to reading list) michael@0: * accessing a page from reading list menu, checks that the reader icon is associated in History tab michael@0: * and that the reading list is properly populated after adding or removing reader items michael@0: */ michael@0: public class testReaderMode extends AboutHomeTest { michael@0: static final int EVENT_CLEAR_DELAY_MS = 3000; michael@0: static final int READER_ICON_MAX_WAIT_MS = 15000; michael@0: michael@0: public void testReaderMode() { michael@0: blockForGeckoReady(); michael@0: michael@0: Actions.EventExpecter contentEventExpecter; michael@0: Actions.EventExpecter contentReaderAddedExpecter; michael@0: Actions.EventExpecter faviconExpecter; michael@0: Actions.EventExpecter contentPageShowExpecter; michael@0: Actions.RepeatedEventExpecter paintExpecter; michael@0: ListView list; michael@0: View child; michael@0: View readerIcon; michael@0: String textUrl = getAbsoluteUrl(StringHelper.ROBOCOP_TEXT_PAGE_URL); michael@0: String devType = mDevice.type; michael@0: int childNo; michael@0: int height; michael@0: int width; michael@0: michael@0: loadAndPaint(textUrl); michael@0: michael@0: // Add the page to the Reading List using long click on the reader icon michael@0: readerIcon = getReaderIcon(); michael@0: contentReaderAddedExpecter = mActions.expectGeckoEvent("Reader:Added"); michael@0: mSolo.clickLongOnView(readerIcon); michael@0: String eventData = contentReaderAddedExpecter.blockForEventData(); michael@0: isAdded(eventData); michael@0: contentReaderAddedExpecter.unregisterListener(); michael@0: michael@0: // Try to add the page to the Reading List using long click on the reader icon a second time michael@0: readerIcon = getReaderIcon(); michael@0: contentReaderAddedExpecter = mActions.expectGeckoEvent("Reader:Added"); michael@0: mSolo.clickLongOnView(readerIcon); michael@0: eventData = contentReaderAddedExpecter.blockForEventData(); michael@0: isAdded(eventData); michael@0: contentReaderAddedExpecter.unregisterListener(); michael@0: michael@0: // Waiting for the favicon since is the last element loaded usually michael@0: faviconExpecter = mActions.expectGeckoEvent("Reader:FaviconRequest"); michael@0: contentPageShowExpecter = mActions.expectGeckoEvent("Content:PageShow"); michael@0: readerIcon = getReaderIcon(); michael@0: paintExpecter = mActions.expectPaint(); michael@0: mSolo.clickOnView(readerIcon); michael@0: michael@0: // Changing devices orientation to be sure that all devices are in portrait when will access the reader toolbar michael@0: mSolo.setActivityOrientation(Solo.PORTRAIT); michael@0: faviconExpecter.blockForEvent(); michael@0: faviconExpecter.unregisterListener(); michael@0: contentPageShowExpecter.blockForEvent(); michael@0: contentPageShowExpecter.unregisterListener(); michael@0: paintExpecter.blockUntilClear(EVENT_CLEAR_DELAY_MS); michael@0: paintExpecter.unregisterListener(); michael@0: verifyPageTitle("Robocop Text Page"); michael@0: michael@0: // Open the share menu for the reader toolbar michael@0: height = mDriver.getGeckoTop() + mDriver.getGeckoHeight() - 10; michael@0: width = mDriver.getGeckoLeft() + mDriver.getGeckoWidth() - 10; michael@0: mAsserter.dumpLog("Long Clicking at width = " + String.valueOf(width) + " and height = " + String.valueOf(height)); michael@0: mSolo.clickOnScreen(width,height); michael@0: mAsserter.ok(mSolo.waitForText("Share via"), "Waiting for the share menu", "The share menu is present"); michael@0: mActions.sendSpecialKey(Actions.SpecialKey.BACK); // Close the share menu michael@0: michael@0: // Remove page from the Reading List using reader toolbar michael@0: height = mDriver.getGeckoTop() + mDriver.getGeckoHeight() - 10; michael@0: width = mDriver.getGeckoLeft() + 50; michael@0: mAsserter.dumpLog("Long Clicking at width = " + String.valueOf(width) + " and height = " + String.valueOf(height)); michael@0: mSolo.clickOnScreen(width,height); michael@0: 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: michael@0: //Add page to the Reading List using reader toolbar michael@0: mSolo.clickOnScreen(width,height); michael@0: 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: michael@0: // Open the Reading List menu for the toolbar michael@0: height = mDriver.getGeckoTop() + mDriver.getGeckoHeight() - 10; michael@0: width = mDriver.getGeckoLeft() + mDriver.getGeckoWidth()/2 - 10; michael@0: mAsserter.dumpLog("Long Clicking at width = " + String.valueOf(width) + " and height = " + String.valueOf(height)); michael@0: contentEventExpecter = mActions.expectGeckoEvent("DOMContentLoaded"); michael@0: mSolo.clickOnScreen(width,height); michael@0: contentEventExpecter.blockForEvent(); michael@0: contentEventExpecter.unregisterListener(); michael@0: michael@0: // Check if the page is present in the Reading List michael@0: 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: michael@0: // Check if the page is added in History tab like a Reading List item michael@0: openAboutHomeTab(AboutHomeTabs.MOST_RECENT); michael@0: list = findListViewWithTag("most_recent"); michael@0: child = list.getChildAt(1); michael@0: mAsserter.ok(child != null, "item can be retrieved", child != null ? child.toString() : "null!"); michael@0: mSolo.clickLongOnView(child); michael@0: 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: mActions.sendSpecialKey(Actions.SpecialKey.BACK); // Dismiss the context menu michael@0: mSolo.waitForText("Robocop Text Page"); michael@0: michael@0: // Verify separately the Reading List entries for tablets and phone because for tablets there is an extra child in UI design michael@0: if (devType.equals("phone")) { michael@0: childNo = 1; michael@0: } michael@0: else { michael@0: childNo = 2; michael@0: } michael@0: // Verify if the page is present to your Reading List michael@0: openAboutHomeTab(AboutHomeTabs.READING_LIST); michael@0: list = findListViewWithTag("reading_list"); michael@0: child = list.getChildAt(childNo-1); michael@0: mAsserter.ok(child != null, "Verify if the page is present to your Reading List", "The page is present in your Reading List"); michael@0: contentEventExpecter = mActions.expectGeckoEvent("DOMContentLoaded"); michael@0: mSolo.clickOnView(child); michael@0: contentEventExpecter.blockForEvent(); michael@0: contentEventExpecter.unregisterListener(); michael@0: verifyPageTitle("Robocop Text Page"); michael@0: michael@0: // Verify that we are in reader mode and remove the page from Reading List michael@0: height = mDriver.getGeckoTop() + mDriver.getGeckoHeight() - 10; michael@0: width = mDriver.getGeckoLeft() + 50; michael@0: mAsserter.dumpLog("Long Clicking at width = " + String.valueOf(width) + " and height = " + String.valueOf(height)); michael@0: mSolo.clickOnScreen(width,height); michael@0: 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: verifyPageTitle("Robocop Text Page"); michael@0: michael@0: //Check if the Reading List is empty michael@0: openAboutHomeTab(AboutHomeTabs.READING_LIST); michael@0: list = findListViewWithTag("reading_list"); michael@0: child = list.getChildAt(childNo-1); michael@0: mAsserter.ok(child == null, "Verify if the Reading List is empty", "The Reading List is empty"); michael@0: } michael@0: michael@0: // Get the reader icon method michael@0: protected View getReaderIcon() { michael@0: View pageActionLayout = mSolo.getView(R.id.page_action_layout); michael@0: final ViewGroup actionLayoutEntry = (ViewGroup)pageActionLayout; michael@0: View icon = actionLayoutEntry.getChildAt(1); michael@0: if (icon == null || icon.getVisibility() != View.VISIBLE) { michael@0: // wait for the view to be visible, otherwise it may not respond michael@0: // to clicks -- see bug 927578 michael@0: mAsserter.dumpLog("reader icon not visible -- waiting for visibility"); michael@0: Condition visibilityCondition = new Condition() { michael@0: @Override michael@0: public boolean isSatisfied() { michael@0: View conditionIcon = actionLayoutEntry.getChildAt(1); michael@0: if (conditionIcon == null || michael@0: conditionIcon.getVisibility() != View.VISIBLE) michael@0: return false; michael@0: return true; michael@0: } michael@0: }; michael@0: waitForCondition(visibilityCondition, READER_ICON_MAX_WAIT_MS); michael@0: icon = actionLayoutEntry.getChildAt(1); michael@0: mAsserter.ok(icon != null, "checking reader icon view", "reader icon view not null"); michael@0: mAsserter.ok(icon.getVisibility() == View.VISIBLE, "checking reader icon visible", "reader icon visible"); michael@0: } michael@0: return icon; michael@0: } michael@0: michael@0: // This method check to see if a reader item is added to the reader list michael@0: private boolean isAdded(String eventData) { michael@0: try { michael@0: JSONObject data = new JSONObject(eventData); michael@0: if (data.getInt("result") == 0) { michael@0: mAsserter.ok(true, "Waiting for the page to be added to your Reading List", "The page was added to your Reading List"); michael@0: } michael@0: else { michael@0: if (data.getInt("result") == 2) { michael@0: 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: } michael@0: } michael@0: } catch (JSONException e) { michael@0: mAsserter.ok(false, "Error parsing the event data", e.toString()); michael@0: return false; michael@0: } michael@0: return true; michael@0: } michael@0: }