Bug 586885 - Search suggestions test case. r=gbrown

This commit is contained in:
Brian Nicholson 2012-06-05 14:07:14 -07:00
parent 355e9fdee1
commit 0c90b684cd
4 changed files with 159 additions and 1 deletions

View File

@ -36,11 +36,15 @@ public class SuggestClient {
// the maximum number of suggestions to return
private final int mMaxResults;
// used by robocop for testing; referenced via reflection
private boolean mCheckNetwork;
public SuggestClient(Context context, String suggestTemplate, int timeout, int maxResults) {
mContext = context;
mMaxResults = maxResults;
mSuggestTemplate = suggestTemplate;
mTimeout = timeout;
mCheckNetwork = true;
}
public SuggestClient(Context context, String suggestTemplate, int timeout) {
@ -56,7 +60,7 @@ public class SuggestClient {
return suggestions;
}
if (!isNetworkConnected()) {
if (!isNetworkConnected() && mCheckNetwork) {
Log.i(LOGTAG, "Not connected to network");
return suggestions;
}

View File

@ -14,6 +14,7 @@
[testPasswordEncrypt]
[testFormHistory]
[testBrowserProvider]
[testSearchSuggestions]
# [testPermissions] # see bug 757475
# [testJarReader] # see bug 738890

View File

@ -0,0 +1,32 @@
/**
* Used with testSearchSuggestions.
* Returns a set of pre-defined suggestions for given prefixes.
*/
function handleRequest(request, response) {
let query = request.queryString.match(/^query=(.*)$/)[1];
query = decodeURIComponent(query).replace(/\+/g, " ");
let suggestMap = {
"f": ["facebook", "fandango", "frys", "forever 21", "fafsa"],
"fo": ["forever 21", "food network", "fox news", "foothill college", "fox"],
"foo": ["food network", "foothill college", "foot locker", "footloose", "foo fighters"],
"foo ": ["foo fighters", "foo bar", "foo bat", "foo bay"],
"foo b": ["foo bar", "foo bat", "foo bay"],
"foo ba": ["foo bar", "foo bat", "foo bay"],
"foo bar": ["foo bar"]
};
let suggestions = suggestMap[query];
if (!suggestions)
suggestions = [];
suggestions = [query, suggestions];
/*
* Sample result:
* ["foo",["food network","foothill college","foot locker",...]]
*/
response.setHeader("Content-Type", "text/json", false);
response.setHeader("Cache-Control", "no-cache", false);
response.write(JSON.stringify(suggestions));
}

View File

@ -0,0 +1,121 @@
#filter substitution
package @ANDROID_PACKAGE_NAME@.tests;
import @ANDROID_PACKAGE_NAME@.*;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.util.Log;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.RuntimeException;
import java.util.ArrayList;
import java.util.HashMap;
/**
* Test for search suggestions.
* Sends queries from AwesomeBar input and verifies that suggestions match
* expected values.
*/
public class testSearchSuggestions extends BaseTest {
private static final int SUGGESTION_MAX = 3;
private static final int SUGGESTION_TIMEOUT = 5000;
private static final String TEST_QUERY = "foo barz";
private static final String SUGGESTION_TEMPLATE = "/robocop/robocop_suggestions.sjs?query=__searchTerms__";
public void testSearchSuggestions() {
setTestType("mochitest");
mActions.expectGeckoEvent("Gecko:Ready").blockForEvent();
// Map of expected values. See robocop_suggestions.sjs.
final HashMap<String, ArrayList<String>> suggestMap = new HashMap<String, ArrayList<String>>();
buildSuggestMap(suggestMap);
final int suggestionLayoutId = mDriver.findElement(getActivity(), "suggestion_layout").getId();
final int suggestionTextId = mDriver.findElement(getActivity(), "suggestion_text").getId();
Actions.EventExpecter enginesEventExpecter = mActions.expectGeckoEvent("SearchEngines:Data");
final Activity awesomeBarActivity = clickOnAwesomeBar();
enginesEventExpecter.blockForEvent();
connectSuggestClient(awesomeBarActivity);
for (int i = 0; i < TEST_QUERY.length(); i++) {
mActions.sendKeys(TEST_QUERY.substring(i, i+1));
final String query = TEST_QUERY.substring(0, i+1);
boolean success = waitForTest(new BooleanTest() {
public boolean test() {
// get the first suggestion row
ViewGroup suggestionGroup = (ViewGroup) awesomeBarActivity.findViewById(suggestionLayoutId);
if (suggestionGroup == null)
return false;
ArrayList<String> expected = suggestMap.get(query);
for (int i = 0; i < expected.size(); i++) {
View queryChild = suggestionGroup.getChildAt(i);
if (queryChild == null || queryChild.getVisibility() == View.GONE)
return false;
String suggestion = ((TextView) queryChild.findViewById(suggestionTextId)).getText().toString();
if (!suggestion.equals(expected.get(i)))
return false;
}
return true;
}
}, SUGGESTION_TIMEOUT);
mAsserter.is(success, true, "Results for query '" + query + "' matched expected suggestions");
}
}
private void buildSuggestMap(HashMap<String, ArrayList<String>> suggestMap) {
// these values assume SUGGESTION_MAX = 3
suggestMap.put("f", new ArrayList<String>() {{ add("f"); add("facebook"); add("fandango"); add("frys"); }});
suggestMap.put("fo", new ArrayList<String>() {{ add("fo"); add("forever 21"); add("food network"); add("fox news"); }});
suggestMap.put("foo", new ArrayList<String>() {{ add("foo"); add("food network"); add("foothill college"); add("foot locker"); }});
suggestMap.put("foo ", new ArrayList<String>() {{ add("foo "); add("foo fighters"); add("foo bar"); add("foo bat"); }});
suggestMap.put("foo b", new ArrayList<String>() {{ add("foo b"); add("foo bar"); add("foo bat"); add("foo bay"); }});
suggestMap.put("foo ba", new ArrayList<String>() {{ add("foo ba"); add("foo bar"); add("foo bat"); add("foo bay"); }});
suggestMap.put("foo bar", new ArrayList<String>() {{ add("foo bar"); }});
suggestMap.put("foo barz", new ArrayList<String>() {{ add("foo barz"); }});
}
private void connectSuggestClient(final Activity awesomeBarActivity) {
try {
// create a SuggestClient that uses robocop_suggestions.sjs
ClassLoader classLoader = getActivity().getApplicationContext().getClassLoader();
Class suggestClass = classLoader.loadClass("org.mozilla.gecko.SuggestClient");
Constructor suggestConstructor = suggestClass.getConstructor(
new Class[] { Context.class, String.class, int.class, int.class });
String suggestTemplate = getAbsoluteRawUrl(SUGGESTION_TEMPLATE);
Object client = suggestConstructor.newInstance(awesomeBarActivity, suggestTemplate, SUGGESTION_TIMEOUT, SUGGESTION_MAX);
// enable offline HTTP requests for testing
final Field checkNetworkField = suggestClass.getDeclaredField("mCheckNetwork");
checkNetworkField.setAccessible(true);
checkNetworkField.set(false, client);
// replace mSuggestClient with test client
Class awesomeBarClass = classLoader.loadClass("org.mozilla.gecko.AwesomeBar");
final Field suggestClientField = awesomeBarClass.getDeclaredField("mSuggestClient");
suggestClientField.setAccessible(true);
waitForTest(new BooleanTest() {
public boolean test() {
// wait for mSuggestClient to be set before we replace it
try {
return suggestClientField.get(awesomeBarActivity) != null;
} catch (IllegalAccessException e) {
return false;
}
}
}, SUGGESTION_TIMEOUT);
suggestClientField.set(awesomeBarActivity, client);
} catch (Exception e) {
throw new RuntimeException("Error setting SuggestClient", e);
}
}
}