2013-04-25 08:09:41 -07:00
# 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
2013-05-08 05:21:47 -07:00
* 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
2013-04-25 08:09:41 -07:00
* /
2013-07-17 02:37:14 -07:00
public class testAddSearchEngine extends PixelTest {
2013-04-25 08:09:41 -07:00
2013-05-08 05:21:47 -07:00
final int MAX_TRIES = 5 ;
2013-04-25 08:09:41 -07:00
@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 " ) ;
2013-07-17 02:37:14 -07:00
// Get the searchengine data
Actions . EventExpecter searchEngineDataEventExpector = mActions . expectGeckoEvent ( " SearchEngines:Data " ) ;
clickOnAwesomeBar ( ) ;
String eventData = searchEngineDataEventExpector . blockForEventData ( ) ;
searchEngineDataEventExpector . unregisterListener ( ) ;
// Parse the data to get the number of searchengines
ArrayList < String > parsedData = parseEventData ( eventData , " [ \" {}], " ) ;
ArrayList < String > searchEngines = getSearchEnginesNames ( parsedData ) ;
initialNumSearchEngines = searchEngines . size ( ) ;
mAsserter . dumpLog ( " Search Engines list = " + searchEngines . toString ( ) ) ;
// Verify that the number of displayed searchengines is the same as the one received through the SearchEngines:Data event
verifyDisplayedSearchEnginesCount ( " Browser Blank Page 01 " , initialNumSearchEngines ) ;
2013-05-08 05:21:47 -07:00
2013-04-25 08:09:41 -07:00
loadUrl ( url ) ;
waitForText ( " Robocop Search Engine " ) ;
2013-05-24 06:27:00 -07:00
// Open the context menu for the input field
2013-04-25 08:09:41 -07:00
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 " ) ) {
2013-07-17 02:37:14 -07:00
// TODO: clickLongOnScreen does not always work - known Robotium issue - . Clicking a second time seems to work
2013-04-25 08:09:41 -07:00
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 " ) ;
2013-06-04 05:16:07 -07:00
clickOnButton ( " OK " ) ;
2013-07-17 02:37:14 -07:00
mAsserter . ok ( ! mSolo . searchText ( " Add Search Engine " ) , " Adding the Search Engine " , " The add Search Engine pop-up has been closed " ) ;
// Load Robocop Blank 1 again to give the time for the searchengine to be added
loadAndPaint ( blank ) ;
waitForText ( " Browser Blank Page 01 " ) ;
2013-04-25 08:09:41 -07:00
// Check that the number of search results has increased
2013-07-17 02:37:14 -07:00
searchEngineDataEventExpector = mActions . expectGeckoEvent ( " SearchEngines:Data " ) ;
clickOnAwesomeBar ( ) ;
eventData = searchEngineDataEventExpector . blockForEventData ( ) ;
parsedData = parseEventData ( eventData , " [ \" {}], " ) ;
searchEngines = getSearchEnginesNames ( parsedData ) ;
mAsserter . dumpLog ( " Search Engines list = " + searchEngines . toString ( ) ) ;
mAsserter . is ( searchEngines . size ( ) , initialNumSearchEngines + 1 , " Checking the number of Search Engines has increased " ) ;
// Verify that the number of displayed searchengines is the same as the one received through the SearchEngines:Data event
verifyDisplayedSearchEnginesCount ( " Browser Blank Page 01 " , initialNumSearchEngines + 1 ) ;
searchEngineDataEventExpector . unregisterListener ( ) ;
2013-04-25 08:09:41 -07:00
}
2013-07-17 02:37:14 -07:00
public ArrayList < String > getSearchEnginesNames ( ArrayList < String > parsedSearchEngineData ) {
ArrayList < String > searchEngineNames = new ArrayList < String > ( ) ;
for ( int i = 0 ; i < parsedSearchEngineData . size ( ) ; i + + ) {
if ( parsedSearchEngineData . get ( i ) . equals ( " name " ) ) {
searchEngineNames . add ( parsedSearchEngineData . get ( i + 1 ) ) ;
2013-05-08 05:21:47 -07:00
}
2013-04-25 08:09:41 -07:00
}
2013-07-17 02:37:14 -07:00
return searchEngineNames ;
2013-04-25 08:09:41 -07:00
}
2013-07-17 02:37:14 -07:00
public void verifyDisplayedSearchEnginesCount ( String waitText , int expectedCountParam ) {
final int expectedCount = expectedCountParam ;
mActions . sendKeys ( " Firefox for Android " ) ;
boolean correctNumSearchEnginesDisplayed = waitForTest ( new BooleanTest ( ) {
@Override
public boolean test ( ) {
ArrayList < ListView > views ;
int searchEngineCount = 0 ;
views = mSolo . getCurrentListViews ( ) ;
for ( ListView view : views ) {
ListAdapter adapter = view . getAdapter ( ) ;
if ( adapter ! = null ) {
searchEngineCount = adapter . getCount ( ) ;
if ( searchEngineCount = = expectedCount ) {
return true ;
} else {
mAsserter . dumpLog ( " Found " + searchEngineCount + " search engines " ) ;
}
}
}
return false ;
}
} , MAX_WAIT_MS ) ;
// Close the Awesomescreen
mActions . sendSpecialKey ( Actions . SpecialKey . BACK ) ;
waitForText ( waitText ) ;
if ( correctNumSearchEnginesDisplayed ) {
mAsserter . ok ( correctNumSearchEnginesDisplayed , expectedCount + " Search Engines should be displayed " , " The correct number of Search Engines has been displayed " ) ;
} else {
// TODO: Investigate more why the number of displayed searchengines is not correctly determined here
mAsserter . todo_is ( correctNumSearchEnginesDisplayed , true , " The number of Search Engines displayed is wrong " ) ;
}
}
2013-04-25 08:09:41 -07:00
}