michael@0: package org.mozilla.gecko.tests; michael@0: michael@0: import java.util.ArrayList; michael@0: michael@0: import org.json.JSONArray; michael@0: import org.json.JSONException; michael@0: import org.json.JSONObject; michael@0: import org.mozilla.gecko.Actions; michael@0: michael@0: import android.widget.ImageView; michael@0: import android.widget.ListAdapter; michael@0: import android.widget.ListView; michael@0: michael@0: /** michael@0: * Test adding a search engine from an input field context menu. michael@0: * 1. Get the number of existing search engines from the SearchEngine:Data event and as displayed in about:home. michael@0: * 2. Load a page with a text field, open the context menu and add a search engine from the page. michael@0: * 3. Get the number of search engines after adding the new one and verify it has increased by 1. michael@0: */ michael@0: public class testAddSearchEngine extends AboutHomeTest { michael@0: private final int MAX_WAIT_TEST_MS = 5000; michael@0: private final String SEARCH_TEXT = "Firefox for Android"; michael@0: private final String ADD_SEARCHENGINE_OPTION_TEXT = "Add Search Engine"; michael@0: michael@0: public void testAddSearchEngine() { michael@0: String blankPageURL = getAbsoluteUrl(StringHelper.ROBOCOP_BLANK_PAGE_01_URL); michael@0: String searchEngineURL = getAbsoluteUrl(StringHelper.ROBOCOP_SEARCH_URL); michael@0: michael@0: blockForGeckoReady(); michael@0: int height = mDriver.getGeckoTop() + 150; michael@0: int width = mDriver.getGeckoLeft() + 150; michael@0: michael@0: inputAndLoadUrl(blankPageURL); michael@0: waitForText(StringHelper.ROBOCOP_BLANK_PAGE_01_TITLE); michael@0: michael@0: // Get the searchengine data by clicking the awesomebar - this causes Gecko to send Java the list michael@0: // of search engines. michael@0: Actions.EventExpecter searchEngineDataEventExpector = mActions.expectGeckoEvent("SearchEngines:Data"); michael@0: focusUrlBar(); michael@0: mActions.sendKeys(SEARCH_TEXT); michael@0: String eventData = searchEngineDataEventExpector.blockForEventData(); michael@0: searchEngineDataEventExpector.unregisterListener(); michael@0: michael@0: ArrayList searchEngines; michael@0: try { michael@0: // Parse the data to get the number of searchengines. michael@0: searchEngines = getSearchEnginesNames(eventData); michael@0: } catch (JSONException e) { michael@0: 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: return; michael@0: } michael@0: final int initialNumSearchEngines = searchEngines.size(); michael@0: mAsserter.dumpLog("Search Engines list = " + searchEngines.toString()); michael@0: michael@0: // Verify that the number of displayed search engines is the same as the one received through the SearchEngines:Data event. michael@0: verifyDisplayedSearchEnginesCount(initialNumSearchEngines); michael@0: michael@0: // Load the page for the search engine to add. michael@0: inputAndLoadUrl(searchEngineURL); michael@0: waitForText(StringHelper.ROBOCOP_SEARCH_TITLE); michael@0: verifyPageTitle(StringHelper.ROBOCOP_SEARCH_TITLE); michael@0: michael@0: // Used to long-tap on the search input box for the search engine to add. michael@0: getInstrumentation().waitForIdleSync(); michael@0: mAsserter.dumpLog("Long Clicking at width = " + String.valueOf(width) + " and height = " + String.valueOf(height)); michael@0: mSolo.clickLongOnScreen(width,height); michael@0: michael@0: ImageView view = waitForViewWithDescription(ImageView.class, ADD_SEARCHENGINE_OPTION_TEXT); michael@0: mAsserter.isnot(view, null, "The action mode was opened"); michael@0: michael@0: // Add the search engine michael@0: mSolo.clickOnView(view); michael@0: waitForText("Cancel"); michael@0: clickOnButton("OK"); michael@0: mAsserter.ok(!mSolo.searchText(ADD_SEARCHENGINE_OPTION_TEXT), "Adding the Search Engine", "The add Search Engine pop-up has been closed"); michael@0: waitForText(StringHelper.ROBOCOP_SEARCH_TITLE); // Make sure the pop-up is closed and we are back at the searchengine page michael@0: michael@0: // Load Robocop Blank 1 again to give the time for the searchengine to be added michael@0: // TODO: This is a potential source of intermittent oranges - it's a race condition! michael@0: loadUrl(blankPageURL); michael@0: waitForText(StringHelper.ROBOCOP_BLANK_PAGE_01_TITLE); michael@0: michael@0: // Load search engines again and check that the quantity of engines has increased by 1. michael@0: searchEngineDataEventExpector = mActions.expectGeckoEvent("SearchEngines:Data"); michael@0: focusUrlBar(); michael@0: mActions.sendKeys(SEARCH_TEXT); michael@0: eventData = searchEngineDataEventExpector.blockForEventData(); michael@0: michael@0: try { michael@0: // Parse the data to get the number of searchengines michael@0: searchEngines = getSearchEnginesNames(eventData); michael@0: } catch (JSONException e) { michael@0: mAsserter.ok(false, "Fatal exception in testAddSearchEngine while decoding JSON search engine string from Gecko after adding of new engine.", e.toString()); michael@0: return; michael@0: } michael@0: michael@0: mAsserter.dumpLog("Search Engines list = " + searchEngines.toString()); michael@0: mAsserter.is(searchEngines.size(), initialNumSearchEngines + 1, "Checking the number of Search Engines has increased"); michael@0: michael@0: // Verify that the number of displayed searchengines is the same as the one received through the SearchEngines:Data event. michael@0: verifyDisplayedSearchEnginesCount(initialNumSearchEngines + 1); michael@0: searchEngineDataEventExpector.unregisterListener(); michael@0: } michael@0: michael@0: /** michael@0: * Helper method to decode a list of search engine names from the provided search engine information michael@0: * JSON string sent from Gecko. michael@0: * @param searchEngineData The JSON string representing the search engine array to process michael@0: * @return An ArrayList containing the names of all the search engines represented in michael@0: * the provided JSON message. michael@0: * @throws JSONException In the event that the JSON provided cannot be decoded. michael@0: */ michael@0: public ArrayList getSearchEnginesNames(String searchEngineData) throws JSONException { michael@0: JSONObject data = new JSONObject(searchEngineData); michael@0: JSONArray engines = data.getJSONArray("searchEngines"); michael@0: michael@0: ArrayList searchEngineNames = new ArrayList(); michael@0: for (int i = 0; i < engines.length(); i++) { michael@0: JSONObject engineJSON = engines.getJSONObject(i); michael@0: searchEngineNames.add(engineJSON.getString("name")); michael@0: } michael@0: return searchEngineNames; michael@0: } michael@0: michael@0: /** michael@0: * Method to verify that the displayed number of search engines matches the expected number. michael@0: * @param expectedCount The expected number of search engines. michael@0: */ michael@0: public void verifyDisplayedSearchEnginesCount(final int expectedCount) { michael@0: mSolo.clearEditText(0); michael@0: mActions.sendKeys(SEARCH_TEXT); michael@0: boolean correctNumSearchEnginesDisplayed = waitForTest(new BooleanTest() { michael@0: @Override michael@0: public boolean test() { michael@0: ListView list = findListViewWithTag("browser_search"); michael@0: if (list == null) { michael@0: return false; michael@0: } michael@0: ListAdapter adapter = list.getAdapter(); michael@0: if (adapter == null) { michael@0: return false; michael@0: } michael@0: return (adapter.getCount() == expectedCount); michael@0: } michael@0: }, MAX_WAIT_TEST_MS); michael@0: michael@0: // Exit about:home michael@0: mActions.sendSpecialKey(Actions.SpecialKey.BACK); michael@0: waitForText(StringHelper.ROBOCOP_BLANK_PAGE_01_TITLE); michael@0: mAsserter.ok(correctNumSearchEnginesDisplayed, expectedCount + " Search Engines should be displayed" , "The correct number of Search Engines has been displayed"); michael@0: } michael@0: }