mobile/android/base/tests/testAddSearchEngine.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 package org.mozilla.gecko.tests;
michael@0 2
michael@0 3 import java.util.ArrayList;
michael@0 4
michael@0 5 import org.json.JSONArray;
michael@0 6 import org.json.JSONException;
michael@0 7 import org.json.JSONObject;
michael@0 8 import org.mozilla.gecko.Actions;
michael@0 9
michael@0 10 import android.widget.ImageView;
michael@0 11 import android.widget.ListAdapter;
michael@0 12 import android.widget.ListView;
michael@0 13
michael@0 14 /**
michael@0 15 * Test adding a search engine from an input field context menu.
michael@0 16 * 1. Get the number of existing search engines from the SearchEngine:Data event and as displayed in about:home.
michael@0 17 * 2. Load a page with a text field, open the context menu and add a search engine from the page.
michael@0 18 * 3. Get the number of search engines after adding the new one and verify it has increased by 1.
michael@0 19 */
michael@0 20 public class testAddSearchEngine extends AboutHomeTest {
michael@0 21 private final int MAX_WAIT_TEST_MS = 5000;
michael@0 22 private final String SEARCH_TEXT = "Firefox for Android";
michael@0 23 private final String ADD_SEARCHENGINE_OPTION_TEXT = "Add Search Engine";
michael@0 24
michael@0 25 public void testAddSearchEngine() {
michael@0 26 String blankPageURL = getAbsoluteUrl(StringHelper.ROBOCOP_BLANK_PAGE_01_URL);
michael@0 27 String searchEngineURL = getAbsoluteUrl(StringHelper.ROBOCOP_SEARCH_URL);
michael@0 28
michael@0 29 blockForGeckoReady();
michael@0 30 int height = mDriver.getGeckoTop() + 150;
michael@0 31 int width = mDriver.getGeckoLeft() + 150;
michael@0 32
michael@0 33 inputAndLoadUrl(blankPageURL);
michael@0 34 waitForText(StringHelper.ROBOCOP_BLANK_PAGE_01_TITLE);
michael@0 35
michael@0 36 // Get the searchengine data by clicking the awesomebar - this causes Gecko to send Java the list
michael@0 37 // of search engines.
michael@0 38 Actions.EventExpecter searchEngineDataEventExpector = mActions.expectGeckoEvent("SearchEngines:Data");
michael@0 39 focusUrlBar();
michael@0 40 mActions.sendKeys(SEARCH_TEXT);
michael@0 41 String eventData = searchEngineDataEventExpector.blockForEventData();
michael@0 42 searchEngineDataEventExpector.unregisterListener();
michael@0 43
michael@0 44 ArrayList<String> searchEngines;
michael@0 45 try {
michael@0 46 // Parse the data to get the number of searchengines.
michael@0 47 searchEngines = getSearchEnginesNames(eventData);
michael@0 48 } catch (JSONException e) {
michael@0 49 mAsserter.ok(false, "Fatal exception in testAddSearchEngine while decoding JSON search engine string from Gecko prior to addition of new engine.", e.toString());
michael@0 50 return;
michael@0 51 }
michael@0 52 final int initialNumSearchEngines = searchEngines.size();
michael@0 53 mAsserter.dumpLog("Search Engines list = " + searchEngines.toString());
michael@0 54
michael@0 55 // Verify that the number of displayed search engines is the same as the one received through the SearchEngines:Data event.
michael@0 56 verifyDisplayedSearchEnginesCount(initialNumSearchEngines);
michael@0 57
michael@0 58 // Load the page for the search engine to add.
michael@0 59 inputAndLoadUrl(searchEngineURL);
michael@0 60 waitForText(StringHelper.ROBOCOP_SEARCH_TITLE);
michael@0 61 verifyPageTitle(StringHelper.ROBOCOP_SEARCH_TITLE);
michael@0 62
michael@0 63 // Used to long-tap on the search input box for the search engine to add.
michael@0 64 getInstrumentation().waitForIdleSync();
michael@0 65 mAsserter.dumpLog("Long Clicking at width = " + String.valueOf(width) + " and height = " + String.valueOf(height));
michael@0 66 mSolo.clickLongOnScreen(width,height);
michael@0 67
michael@0 68 ImageView view = waitForViewWithDescription(ImageView.class, ADD_SEARCHENGINE_OPTION_TEXT);
michael@0 69 mAsserter.isnot(view, null, "The action mode was opened");
michael@0 70
michael@0 71 // Add the search engine
michael@0 72 mSolo.clickOnView(view);
michael@0 73 waitForText("Cancel");
michael@0 74 clickOnButton("OK");
michael@0 75 mAsserter.ok(!mSolo.searchText(ADD_SEARCHENGINE_OPTION_TEXT), "Adding the Search Engine", "The add Search Engine pop-up has been closed");
michael@0 76 waitForText(StringHelper.ROBOCOP_SEARCH_TITLE); // Make sure the pop-up is closed and we are back at the searchengine page
michael@0 77
michael@0 78 // Load Robocop Blank 1 again to give the time for the searchengine to be added
michael@0 79 // TODO: This is a potential source of intermittent oranges - it's a race condition!
michael@0 80 loadUrl(blankPageURL);
michael@0 81 waitForText(StringHelper.ROBOCOP_BLANK_PAGE_01_TITLE);
michael@0 82
michael@0 83 // Load search engines again and check that the quantity of engines has increased by 1.
michael@0 84 searchEngineDataEventExpector = mActions.expectGeckoEvent("SearchEngines:Data");
michael@0 85 focusUrlBar();
michael@0 86 mActions.sendKeys(SEARCH_TEXT);
michael@0 87 eventData = searchEngineDataEventExpector.blockForEventData();
michael@0 88
michael@0 89 try {
michael@0 90 // Parse the data to get the number of searchengines
michael@0 91 searchEngines = getSearchEnginesNames(eventData);
michael@0 92 } catch (JSONException e) {
michael@0 93 mAsserter.ok(false, "Fatal exception in testAddSearchEngine while decoding JSON search engine string from Gecko after adding of new engine.", e.toString());
michael@0 94 return;
michael@0 95 }
michael@0 96
michael@0 97 mAsserter.dumpLog("Search Engines list = " + searchEngines.toString());
michael@0 98 mAsserter.is(searchEngines.size(), initialNumSearchEngines + 1, "Checking the number of Search Engines has increased");
michael@0 99
michael@0 100 // Verify that the number of displayed searchengines is the same as the one received through the SearchEngines:Data event.
michael@0 101 verifyDisplayedSearchEnginesCount(initialNumSearchEngines + 1);
michael@0 102 searchEngineDataEventExpector.unregisterListener();
michael@0 103 }
michael@0 104
michael@0 105 /**
michael@0 106 * Helper method to decode a list of search engine names from the provided search engine information
michael@0 107 * JSON string sent from Gecko.
michael@0 108 * @param searchEngineData The JSON string representing the search engine array to process
michael@0 109 * @return An ArrayList<String> containing the names of all the search engines represented in
michael@0 110 * the provided JSON message.
michael@0 111 * @throws JSONException In the event that the JSON provided cannot be decoded.
michael@0 112 */
michael@0 113 public ArrayList<String> getSearchEnginesNames(String searchEngineData) throws JSONException {
michael@0 114 JSONObject data = new JSONObject(searchEngineData);
michael@0 115 JSONArray engines = data.getJSONArray("searchEngines");
michael@0 116
michael@0 117 ArrayList<String> searchEngineNames = new ArrayList<String>();
michael@0 118 for (int i = 0; i < engines.length(); i++) {
michael@0 119 JSONObject engineJSON = engines.getJSONObject(i);
michael@0 120 searchEngineNames.add(engineJSON.getString("name"));
michael@0 121 }
michael@0 122 return searchEngineNames;
michael@0 123 }
michael@0 124
michael@0 125 /**
michael@0 126 * Method to verify that the displayed number of search engines matches the expected number.
michael@0 127 * @param expectedCount The expected number of search engines.
michael@0 128 */
michael@0 129 public void verifyDisplayedSearchEnginesCount(final int expectedCount) {
michael@0 130 mSolo.clearEditText(0);
michael@0 131 mActions.sendKeys(SEARCH_TEXT);
michael@0 132 boolean correctNumSearchEnginesDisplayed = waitForTest(new BooleanTest() {
michael@0 133 @Override
michael@0 134 public boolean test() {
michael@0 135 ListView list = findListViewWithTag("browser_search");
michael@0 136 if (list == null) {
michael@0 137 return false;
michael@0 138 }
michael@0 139 ListAdapter adapter = list.getAdapter();
michael@0 140 if (adapter == null) {
michael@0 141 return false;
michael@0 142 }
michael@0 143 return (adapter.getCount() == expectedCount);
michael@0 144 }
michael@0 145 }, MAX_WAIT_TEST_MS);
michael@0 146
michael@0 147 // Exit about:home
michael@0 148 mActions.sendSpecialKey(Actions.SpecialKey.BACK);
michael@0 149 waitForText(StringHelper.ROBOCOP_BLANK_PAGE_01_TITLE);
michael@0 150 mAsserter.ok(correctNumSearchEnginesDisplayed, expectedCount + " Search Engines should be displayed" , "The correct number of Search Engines has been displayed");
michael@0 151 }
michael@0 152 }

mercurial