#filter substitution package @ANDROID_PACKAGE_NAME@.tests; import @ANDROID_PACKAGE_NAME@.*; import android.view.View; import android.widget.ListAdapter; import android.widget.ListView; import java.util.ArrayList; /** * Test adding a search engine from an input field context menu * 1. Get the number of existing search engines * 2. Load a page with a text field, open the context menu and add a search engine from the page * 3. Get the number of search engines after adding the new one */ public class testAddSearchEngine extends BaseTest { final int MAX_TRIES = 5; @Override protected int getTestType() { return TEST_MOCHITEST; } public void testAddSearchEngine() { int height,width; final int initialNumSearchEngines; String blank = getAbsoluteUrl("/robocop/robocop_blank_01.html"); String url = getAbsoluteUrl("/robocop/robocop_search.html"); blockForGeckoReady(); loadUrl(blank); waitForText("Browser Blank Page 01"); initialNumSearchEngines = getNumSearchEngines("Browser Blank Page 01"); loadUrl(url); waitForText("Robocop Search Engine"); // Open the context menu for the input field height = mDriver.getGeckoTop() + 10; width = mDriver.getGeckoLeft() + 20; mAsserter.dumpLog("Long Clicking at width = " + String.valueOf(width) + " and height = " + String.valueOf(height)); mSolo.clickLongOnScreen(width,height); if (!waitForText("Add Search Engine")) { // TODO: clickLongOnScreen does not always work - known Robotium issue. Clicking a second time seems to work mAsserter.dumpLog("Something went wrong and the context menu was not opened. Trying again"); mSolo.clickLongOnScreen(width,height); } mAsserter.ok(waitForText("Add Search Engine"), "Waiting for the context menu to be opened", "The context menu was opened"); // Add the search engine mSolo.clickOnText("Add Search Engine"); waitForText("Cancel"); clickOnButton("OK"); mAsserter.ok(!mSolo.searchText("Add Search Engine"), "Adding the search engine", "The add search engine pop-up has been closed"); // Check that the number of search results has increased mAsserter.is(getNumSearchEngines("Robocop Search Engine"), initialNumSearchEngines + 1 , "The number of search results has increased"); } public int getNumSearchEngines(String waitText) { ArrayList views; int searchEngineCount = 0; int oldSearchEngineCount = -1; for (int i = 0; i < MAX_TRIES; i++ ) { // Start a search and wait for the search engine data to be displayed Actions.EventExpecter enginesEventExpecter = mActions.expectGeckoEvent("SearchEngines:Data"); clickOnAwesomeBar(); waitForText("Bookmarks"); mActions.sendKeys("Firefox for Android"); enginesEventExpecter.blockForEvent(); views = mSolo.getCurrentListViews(); for (ListView view : views) { ListAdapter adapter = view.getAdapter(); if (adapter != null) { searchEngineCount = adapter.getCount(); } else { searchEngineCount = -1; } } // Close the Awesomescreen mActions.sendSpecialKey(Actions.SpecialKey.BACK); waitForText(waitText); if (searchEngineCount == oldSearchEngineCount) { mAsserter.isnot(searchEngineCount, -1, "Search engine number could be determined correctly"); return searchEngineCount; } else { oldSearchEngineCount = searchEngineCount; } } // Since the number of Search Engine count was different on every try fail the test mAsserter.is(searchEngineCount,oldSearchEngineCount, "The search engine count could not be established correctly"); return searchEngineCount; } }