From 0f04c67997c2e1d9157d95ef8ba847b84cc158bf Mon Sep 17 00:00:00 2001 From: Robin Ricard Date: Thu, 29 Jan 2015 05:26:00 +0100 Subject: [PATCH] Bug 1015395 - Make testFindInPage more robust. r=margaret Use a JS based test relying on document.getSelection Use promises+generators to wait during UI changes done by Java --- mobile/android/base/tests/robocop.ini | 4 +- .../android/base/tests/robocop_text_page.html | 8 +- mobile/android/base/tests/testFindInPage.java | 74 +++++++++------ mobile/android/base/tests/testFindInPage.js | 89 +++++++++++++++++++ 4 files changed, 140 insertions(+), 35 deletions(-) create mode 100644 mobile/android/base/tests/testFindInPage.js diff --git a/mobile/android/base/tests/robocop.ini b/mobile/android/base/tests/robocop.ini index f760c0e4463a..e151c41bac19 100644 --- a/mobile/android/base/tests/robocop.ini +++ b/mobile/android/base/tests/robocop.ini @@ -35,7 +35,9 @@ skip-if = android_version == "10" || processor == "x86" [testDistribution] [testDoorHanger] [testFilterOpenTab] -# [testFindInPage] # see bug 975155, bug 1014708 +[testFindInPage] +# disabled on 2.3 +skip-if = android_version == "10" [testFlingCorrectness] # disabled on x86 only; bug 927476 skip-if = processor == "x86" diff --git a/mobile/android/base/tests/robocop_text_page.html b/mobile/android/base/tests/robocop_text_page.html index f06b61adcdab..db30144ddfe1 100644 --- a/mobile/android/base/tests/robocop_text_page.html +++ b/mobile/android/base/tests/robocop_text_page.html @@ -5,16 +5,14 @@ -
-

Text taken from Wikipedia.org

-

Will be searching for this string: Robocop

+

Will be searching for this string: Robocop 1

Mozilla is a free software community best known for producing the Firefox web browser. The Mozilla community uses, develops, spreads and supports Mozilla products and works to advance the goals of the Open Web described in the Mozilla Manifesto.[1] The community is supported institutionally by the Mozilla Foundation and its tax-paying subsidiary, the Mozilla Corporation.[2]

-

Will be searching for this string: Robocop

+

Will be searching for this string: Robocop 2

In addition to the Firefox browser, Mozilla also produces Firefox Mobile, the Firefox OS mobile operating system, the bug tracking system Bugzilla and a number of other projects.

-

Will be searching for this string: Robocop

+

Will be searching for this string: Robocop 3

On February 23, 1998, Netscape Communications Corporation created a project called Mozilla (after the original code name of the Netscape Navigator browser which — according to Pascal Finette — is a mashup of "Mosaic Killer") to co-ordinate the development of the Mozilla Application Suite, the open source version of Netscape's internet software, Netscape Communicator.[3][4] Jamie Zawinski says he came up with the name "Mozilla" at a Netscape staff meeting.[5][6] A small group of Netscape employees were tasked with coordination of the new community.

Will be searching for this string: Robocop

diff --git a/mobile/android/base/tests/testFindInPage.java b/mobile/android/base/tests/testFindInPage.java index 32ae1f305289..c6cf2b29f25f 100644 --- a/mobile/android/base/tests/testFindInPage.java +++ b/mobile/android/base/tests/testFindInPage.java @@ -4,47 +4,64 @@ package org.mozilla.gecko.tests; +import static org.mozilla.gecko.tests.helpers.AssertionHelper.fFail; + import org.mozilla.gecko.Actions; import org.mozilla.gecko.Element; -import org.mozilla.gecko.PaintedSurface; import org.mozilla.gecko.R; -public class testFindInPage extends PixelTest { +import org.mozilla.gecko.EventDispatcher; +import org.mozilla.gecko.util.GeckoEventListener; + +import org.json.JSONObject; + +public class testFindInPage extends JavascriptTest implements GeckoEventListener { private static final int WAIT_FOR_TEST = 3000; protected Element next, close; - int height,width; - public void testFindInPage() { - blockForGeckoReady(); - String url = getAbsoluteUrl(StringHelper.ROBOCOP_TEXT_PAGE_URL); - loadAndPaint(url); + public testFindInPage() { + super("testFindInPage.js"); + } - height = mDriver.getGeckoHeight()/8; - width = mDriver.getGeckoWidth()/2; - - // Search that does not find the term and therefor should not pan the page - Actions.RepeatedEventExpecter paintExpecter = mActions.expectPaint(); - findText("Robocoop", 3); // This will be close enough to existing text to test that search finds just what it should - PaintedSurface painted = waitForPaint(paintExpecter); - paintExpecter.unregisterListener(); - try { - mAsserter.ispixel(painted.getPixelAt(width,height), 255, 0, 0, "Pixel at " + String.valueOf(width) + "," + String.valueOf(height)); - } finally { - painted.close(); + @Override + public void handleMessage(String event, final JSONObject message) { + if (event.equals("Test:FindInPage")) { + try { + final String text = message.getString("text"); + final int nrOfMatches = Integer.parseInt(message.getString("nrOfMatches")); + findText(text, nrOfMatches); + } catch (Exception e) { + fFail("Can't extract find query from JSON"); + } } - // Search that finds matches and therefor pans the page - paintExpecter = mActions.expectPaint(); - findText("Robocop", 3); - painted = waitForPaint(paintExpecter); - paintExpecter.unregisterListener(); - try { - mAsserter.isnotpixel(painted.getPixelAt(width,height), 255, 0, 0, "Pixel at " + String.valueOf(width) + "," + String.valueOf(height)); - } finally { - painted.close(); + if (event.equals("Test:CloseFindInPage")) { + try { + close.click(); + } catch (Exception e) { + fFail("FindInPage prompt not opened"); + } } } + @Override + public void setUp() throws Exception { + super.setUp(); + + EventDispatcher.getInstance().registerGeckoThreadListener(this, + "Test:FindInPage", + "Test:CloseFindInPage"); + } + + @Override + public void tearDown() throws Exception { + super.tearDown(); + + EventDispatcher.getInstance().unregisterGeckoThreadListener(this, + "Test:FindInPage", + "Test:CloseFindInPage"); + } + public void findText(String text, int nrOfMatches){ selectMenuItem(StringHelper.FIND_IN_PAGE_LABEL); close = mDriver.findElement(getActivity(), R.id.find_close); @@ -83,6 +100,5 @@ public class testFindInPage extends PixelTest { mSolo.sleep(500); // TODO: Find a better way to wait here because waitForTest is not enough mAsserter.ok(success, "Checking if the next button was clicked", "button was clicked"); } - close.click(); // Close find in page bar } } diff --git a/mobile/android/base/tests/testFindInPage.js b/mobile/android/base/tests/testFindInPage.js new file mode 100644 index 000000000000..6e14522975cc --- /dev/null +++ b/mobile/android/base/tests/testFindInPage.js @@ -0,0 +1,89 @@ +// -*- indent-tabs-mode: nil; js-indent-level: 2 -*- +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +"use strict"; + +const Cu = Components.utils; + +Cu.import("resource://gre/modules/Messaging.jsm"); +Cu.import("resource://gre/modules/Services.jsm"); + +const TEST_URL = "http://mochi.test:8888/tests/robocop/robocop_text_page.html"; + +function promiseBrowserEvent(browser, eventType) { + return new Promise((resolve) => { + function handle(event) { + do_print("Received event " + eventType + " from browser"); + browser.removeEventListener(eventType, handle, true); + resolve(event); + } + + browser.addEventListener(eventType, handle, true); + do_print("Now waiting for " + eventType + " event from browser"); + }); +} + +function openTabWithUrl(url) { + do_print("Going to open " + url); + let browserApp = Services.wm.getMostRecentWindow("navigator:browser").BrowserApp; + let browser = browserApp.addTab(url, { selected: true, parentId: browserApp.selectedTab.id }).browser; + + return promiseBrowserEvent(browser, "load") + .then(() => { return browser; }); +} + +function findInPage(browser, text, nrOfMatches) { + let repaintPromise = promiseBrowserEvent(browser, "MozAfterPaint"); + do_print("Send findInPageMessage: " + text + " nth: " + nrOfMatches); + Messaging.sendRequest({ type: "Test:FindInPage", text: text, nrOfMatches: nrOfMatches }); + return repaintPromise; +} + +function closeFindInPage(browser) { + let repaintPromise = promiseBrowserEvent(browser, "MozAfterPaint"); + do_print("Send closeFindInPageMessage"); + Messaging.sendRequest({ type: "Test:CloseFindInPage" }); + return repaintPromise; +} + +function assertSelection(document, expectedSelection = false, expectedAnchorText = false) { + let sel = document.getSelection(); + if (!expectedSelection) { + do_print("Assert empty selection"); + do_check_eq(sel.toString(), ""); + } else { + do_print("Assert selection to be " + expectedSelection); + do_check_eq(sel.toString(), expectedSelection); + } + if (expectedAnchorText) { + do_print("Assert anchor text to be " + expectedAnchorText); + do_check_eq(sel.anchorNode.textContent, expectedAnchorText); + } +} + +add_task(function* testFindInPage() { + let browser = yield openTabWithUrl(TEST_URL); + let document = browser.contentDocument; + + yield findInPage(browser, "Robocoop", 1); + assertSelection(document); + + yield closeFindInPage(browser); + assertSelection(document); + + yield findInPage(browser, "Robocop", 1); + assertSelection(document, "Robocop", " Robocop 1 "); + + yield closeFindInPage(browser); + assertSelection(document); + + yield findInPage(browser, "Robocop", 3); + assertSelection(document, "Robocop", " Robocop 3 "); + + yield closeFindInPage(browser); + assertSelection(document); +}); + +run_next_test();