2012-01-05 15:20:22 +00:00
|
|
|
#filter substitution
|
|
|
|
package @ANDROID_PACKAGE_NAME@.tests;
|
|
|
|
|
|
|
|
import com.jayway.android.robotium.solo.Solo;
|
|
|
|
import @ANDROID_PACKAGE_NAME@.*;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
2012-01-06 02:36:17 +00:00
|
|
|
import android.app.Instrumentation;
|
2012-03-08 18:25:44 +00:00
|
|
|
import android.content.ContentValues;
|
2012-01-05 15:20:22 +00:00
|
|
|
import android.content.Intent;
|
2012-03-27 20:16:13 +00:00
|
|
|
import android.content.res.AssetManager;
|
|
|
|
import android.database.Cursor;
|
2012-02-23 02:26:04 +00:00
|
|
|
import android.os.SystemClock;
|
|
|
|
import android.test.ActivityInstrumentationTestCase2;
|
2012-03-08 18:25:44 +00:00
|
|
|
import java.io.File;
|
2012-03-27 20:16:13 +00:00
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.IOException;
|
2012-02-23 02:26:04 +00:00
|
|
|
|
2012-01-05 15:20:22 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
|
2012-05-22 23:25:30 +00:00
|
|
|
/**
|
|
|
|
* A convenient base class suitable for most Robocop tests.
|
|
|
|
*/
|
2012-01-05 15:20:22 +00:00
|
|
|
abstract class BaseTest extends ActivityInstrumentationTestCase2<Activity> {
|
|
|
|
private static final String TARGET_PACKAGE_ID = "org.mozilla.gecko";
|
|
|
|
private static final String LAUNCH_ACTIVITY_FULL_CLASSNAME="@ANDROID_PACKAGE_NAME@.App";
|
|
|
|
|
|
|
|
private static Class<Activity> mLauncherActivityClass;
|
|
|
|
private Activity mActivity;
|
2012-01-18 18:44:47 +00:00
|
|
|
protected Solo mSolo;
|
2012-01-05 15:20:22 +00:00
|
|
|
protected Driver mDriver;
|
|
|
|
protected Assert mAsserter;
|
|
|
|
protected Actions mActions;
|
2012-02-02 15:09:26 +00:00
|
|
|
protected String mBaseUrl;
|
2012-02-02 15:47:17 +00:00
|
|
|
private String mLogFile;
|
2012-03-08 18:25:44 +00:00
|
|
|
protected String mProfile;
|
2012-01-05 15:20:22 +00:00
|
|
|
|
2012-05-26 00:26:04 +00:00
|
|
|
protected static final int TEST_MOCHITEST = 0;
|
|
|
|
protected static final int TEST_TALOS = 1;
|
|
|
|
|
2012-01-05 15:20:22 +00:00
|
|
|
static {
|
|
|
|
try {
|
|
|
|
mLauncherActivityClass = (Class<Activity>)Class.forName(LAUNCH_ACTIVITY_FULL_CLASSNAME);
|
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public BaseTest() {
|
|
|
|
super(TARGET_PACKAGE_ID, mLauncherActivityClass);
|
|
|
|
}
|
|
|
|
|
2012-05-26 00:26:04 +00:00
|
|
|
// Must return either TEST_MOCHITEST or TEST_TALOS
|
|
|
|
protected abstract int getTestType();
|
|
|
|
|
2012-01-05 15:20:22 +00:00
|
|
|
@Override
|
|
|
|
protected void setUp() throws Exception {
|
|
|
|
// Load config file from sdcard (setup by python script)
|
|
|
|
String configFile = FennecNativeDriver.getFile("/mnt/sdcard/robotium.config");
|
|
|
|
HashMap config = FennecNativeDriver.convertTextToTable(configFile);
|
|
|
|
|
|
|
|
// Create the intent to be used with all the important arguments.
|
|
|
|
Intent i = new Intent(Intent.ACTION_MAIN);
|
2012-03-08 18:25:44 +00:00
|
|
|
mProfile = (String)config.get("profile");
|
|
|
|
i.putExtra("args", "-no-remote -profile " + mProfile);
|
2012-01-05 15:20:22 +00:00
|
|
|
|
|
|
|
// Start the activity
|
|
|
|
setActivityIntent(i);
|
|
|
|
mActivity = getActivity();
|
|
|
|
|
|
|
|
// Set up Robotium.solo and Driver objects
|
2012-01-06 02:36:17 +00:00
|
|
|
mSolo = new Solo(getInstrumentation());
|
2012-01-05 15:20:22 +00:00
|
|
|
mDriver = new FennecNativeDriver(mActivity, mSolo);
|
|
|
|
mActions = new FennecNativeActions(mActivity, mSolo, getInstrumentation());
|
|
|
|
|
2012-02-02 15:09:26 +00:00
|
|
|
mLogFile = (String)config.get("logfile");
|
|
|
|
mBaseUrl = ((String)config.get("host")).replaceAll("(/$)", "");
|
|
|
|
|
2012-05-26 00:26:04 +00:00
|
|
|
// Initialize the asserter
|
|
|
|
if (getTestType() == TEST_TALOS) {
|
2012-02-02 15:09:26 +00:00
|
|
|
mAsserter = new FennecTalosAssert();
|
|
|
|
} else {
|
|
|
|
mAsserter = new FennecMochitestAssert();
|
|
|
|
}
|
|
|
|
mAsserter.setLogFile(mLogFile);
|
2012-01-07 23:41:08 +00:00
|
|
|
mAsserter.setTestName(this.getClass().getName());
|
2012-01-05 15:20:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void tearDown() throws Exception {
|
|
|
|
try {
|
2012-01-07 23:41:08 +00:00
|
|
|
mAsserter.finalize();
|
2012-01-05 15:20:22 +00:00
|
|
|
mSolo.finalize();
|
|
|
|
} catch (Throwable e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
getActivity().finish();
|
|
|
|
super.tearDown();
|
|
|
|
}
|
2012-01-05 15:20:22 +00:00
|
|
|
|
2012-05-22 23:25:30 +00:00
|
|
|
/**
|
|
|
|
* Click on the specified element and return the resulting activity.
|
|
|
|
* @return The created activity, or null if the element cannot be clicked.
|
|
|
|
*/
|
2012-01-06 02:36:17 +00:00
|
|
|
protected final Activity getActivityFromClick(Element element) {
|
|
|
|
Instrumentation inst = getInstrumentation();
|
|
|
|
Instrumentation.ActivityMonitor monitor = inst.addMonitor((String)null, null, false);
|
2012-05-22 23:25:30 +00:00
|
|
|
boolean clicked = element.click();
|
|
|
|
if (!clicked) {
|
|
|
|
mAsserter.ok(clicked != false, "checking that awesome bar clicked", "awesome bar was clicked");
|
|
|
|
return null;
|
|
|
|
}
|
2012-01-24 14:46:36 +00:00
|
|
|
// wait for click to take effect before waiting for activity
|
|
|
|
// (otherwise we sometimes get the previous activity)
|
|
|
|
getInstrumentation().waitForIdleSync();
|
2012-01-10 19:15:18 +00:00
|
|
|
Activity activity = inst.waitForMonitor(monitor);
|
2012-01-24 14:46:36 +00:00
|
|
|
// give the activity time to render itself and initialize views
|
|
|
|
// before continuing, so that views are created before access
|
|
|
|
// attempts are made
|
2012-01-10 19:15:18 +00:00
|
|
|
getInstrumentation().waitForIdleSync();
|
|
|
|
return activity;
|
|
|
|
}
|
|
|
|
|
2012-05-22 23:25:30 +00:00
|
|
|
/**
|
|
|
|
* Click on the awesome bar element and return the resulting activity.
|
|
|
|
* @return The created activity, or null if the awesome bar cannot be clicked.
|
|
|
|
*/
|
2012-01-10 19:15:18 +00:00
|
|
|
protected final Activity clickOnAwesomeBar() {
|
2012-05-22 23:25:30 +00:00
|
|
|
Activity activity = null;
|
2012-01-10 19:15:18 +00:00
|
|
|
Element awesomebar = mDriver.findElement(mActivity, "awesome_bar");
|
2012-05-22 23:25:30 +00:00
|
|
|
if (awesomebar != null) {
|
|
|
|
activity = getActivityFromClick(awesomebar);
|
|
|
|
if (activity == null) {
|
|
|
|
mAsserter.dumpLog("failed to click on awesome bar!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return activity;
|
2012-01-06 02:36:17 +00:00
|
|
|
}
|
|
|
|
|
2012-01-05 15:20:22 +00:00
|
|
|
protected final void enterUrl(String url) {
|
2012-01-10 19:15:18 +00:00
|
|
|
Activity awesomeBarActivity = clickOnAwesomeBar();
|
2012-01-06 02:36:17 +00:00
|
|
|
Element urlbar = mDriver.findElement(awesomeBarActivity, "awesomebar_text");
|
2012-01-05 15:20:22 +00:00
|
|
|
mActions.sendKeys(url);
|
2012-05-22 23:25:30 +00:00
|
|
|
String urlbarText = null;
|
|
|
|
if (urlbar != null) {
|
|
|
|
urlbarText = urlbar.getText();
|
|
|
|
}
|
|
|
|
mAsserter.is(urlbarText, url, "Awesomebar URL typed properly");
|
2012-01-05 15:20:22 +00:00
|
|
|
}
|
|
|
|
|
2012-01-06 02:36:17 +00:00
|
|
|
protected final void hitEnterAndWait() {
|
|
|
|
Actions.EventExpecter contentEventExpecter = mActions.expectGeckoEvent("DOMContentLoaded");
|
2012-01-05 15:20:22 +00:00
|
|
|
mActions.sendSpecialKey(Actions.SpecialKey.ENTER);
|
|
|
|
// wait for screen to load
|
2012-01-06 02:36:17 +00:00
|
|
|
contentEventExpecter.blockForEvent();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected final void loadUrl(String url) {
|
|
|
|
enterUrl(url);
|
|
|
|
hitEnterAndWait();
|
2012-01-05 15:20:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected final void verifyUrl(String url) {
|
2012-01-10 19:15:18 +00:00
|
|
|
Activity awesomeBarActivity = clickOnAwesomeBar();
|
2012-01-06 02:36:17 +00:00
|
|
|
Element urlbar = mDriver.findElement(awesomeBarActivity, "awesomebar_text");
|
2012-05-22 23:25:30 +00:00
|
|
|
String urlbarText = null;
|
|
|
|
if (urlbar != null) {
|
|
|
|
urlbarText = urlbar.getText();
|
|
|
|
}
|
|
|
|
mAsserter.is(urlbarText, url, "Awesomebar URL stayed the same");
|
2012-01-05 15:20:22 +00:00
|
|
|
}
|
2012-02-02 15:09:26 +00:00
|
|
|
|
|
|
|
protected final String getAbsoluteUrl(String url) {
|
|
|
|
return mBaseUrl + "/" + url.replaceAll("(^/)", "");
|
|
|
|
}
|
2012-02-23 02:26:04 +00:00
|
|
|
|
|
|
|
protected final boolean waitForTest(BooleanTest t, int timeout) {
|
|
|
|
long end = SystemClock.uptimeMillis() + timeout;
|
|
|
|
while (SystemClock.uptimeMillis() < end) {
|
|
|
|
if (t.test())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
mSolo.sleep(100);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected interface BooleanTest {
|
|
|
|
public boolean test();
|
|
|
|
}
|
2012-03-08 18:25:44 +00:00
|
|
|
|
|
|
|
@SuppressWarnings({"unchecked", "non-varargs"})
|
|
|
|
public void SqliteCompare(String dbName, String sqlCommand, ContentValues[] cvs) {
|
|
|
|
File profile = new File(mProfile);
|
|
|
|
String dbPath = new File(profile, dbName).getPath();
|
|
|
|
|
|
|
|
Cursor c = mActions.querySql(dbPath, sqlCommand);
|
|
|
|
SqliteCompare(c, cvs);
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean CursorMatches(Cursor c, String[] columns, ContentValues cv) {
|
|
|
|
for (int i = 0; i < columns.length; i++) {
|
|
|
|
String column = columns[i];
|
|
|
|
if (cv.containsKey(column)) {
|
2012-04-09 17:47:47 +00:00
|
|
|
mAsserter.info("Comparing", "Column values for: " + column);
|
|
|
|
Object value = cv.get(column);
|
|
|
|
if (value == null) {
|
|
|
|
if (!c.isNull(i))
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
if (c.isNull(i) || !value.toString().equals(c.getString(i)))
|
|
|
|
return false;
|
2012-03-08 18:25:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings({"unchecked", "non-varargs"})
|
|
|
|
public void SqliteCompare(Cursor c, ContentValues[] cvs) {
|
|
|
|
mAsserter.is(c.getCount(), cvs.length, "List is correct length");
|
|
|
|
if (c.moveToFirst()) {
|
|
|
|
do {
|
|
|
|
boolean found = false;
|
|
|
|
for (int i = 0; !found && i < cvs.length; i++) {
|
|
|
|
if (CursorMatches(c, cvs[i])) {
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mAsserter.is(found, true, "Password was found");
|
|
|
|
} while(c.moveToNext());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean CursorMatches(Cursor c, ContentValues cv) {
|
|
|
|
for (int i = 0; i < c.getColumnCount(); i++) {
|
|
|
|
String column = c.getColumnName(i);
|
|
|
|
if (cv.containsKey(column)) {
|
2012-04-09 17:47:47 +00:00
|
|
|
mAsserter.info("Comparing", "Column values for: " + column);
|
|
|
|
Object value = cv.get(column);
|
|
|
|
if (value == null) {
|
|
|
|
if (!c.isNull(i))
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
if (c.isNull(i) || !value.toString().equals(c.getString(i)))
|
|
|
|
return false;
|
2012-03-08 18:25:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2012-03-27 20:16:13 +00:00
|
|
|
|
|
|
|
public InputStream getAsset(String filename) throws IOException {
|
|
|
|
AssetManager assets = getInstrumentation().getContext().getAssets();
|
|
|
|
return assets.open(filename);
|
|
|
|
}
|
2012-01-05 15:20:22 +00:00
|
|
|
}
|