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