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.

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

mercurial