Bug 711591 - make the robotium test driver a bit more driver like. r=ctalbert

This commit is contained in:
Joel Maher 2011-12-30 15:57:34 -05:00
parent 686269c7c3
commit c431dbcb94
12 changed files with 349 additions and 25 deletions

View File

@ -0,0 +1,53 @@
#filter substitution
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Firefox Mobile Test Framework.
*
* The Initial Developer of the Original Code is Mozilla.
* Portions created by the Initial Developer are Copyright (C) 2011
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Trevor Fairey <tnfairey@gmail.com>
* David Burns <dburns@mozilla.com>
* Joel Maher <joel.maher@gmail.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
package @ANDROID_PACKAGE_NAME@;
public interface Assert {
void dumpLog(String message);
void setLogFile(String filename);
void ok(boolean condition, String name, String diag);
void is(Object a, Object b, String name);
void isnot(Object a, Object b, String name);
void todo(boolean condition, String name, String diag);
void todo_is(Object a, Object b, String name);
void todo_isnot(Object a, Object b, String name);
void info(String name, String message);
}

View File

@ -0,0 +1,212 @@
#filter substitution
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Firefox Mobile Test Framework.
*
* The Initial Developer of the Original Code is Mozilla.
* Portions created by the Initial Developer are Copyright (C) 2011
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Trevor Fairey <tnfairey@gmail.com>
* David Burns <dburns@mozilla.com>
* Joel Maher <joel.maher@gmail.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
package @ANDROID_PACKAGE_NAME@;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.HashMap;
import java.util.List;
import java.lang.Class;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.Long;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import org.json.*;
import com.jayway.android.robotium.solo.Solo;
public class FennecNativeAssert implements Assert {
// Map of IDs to element names.
private HashMap locators = null;
private String logFile = null;
// Objects for reflexive access of fennec classes.
private LinkedList<testInfo> testList = new LinkedList<testInfo>();
// If waiting for an event.
private boolean asleep = false;
public FennecNativeAssert(){
}
// Write information to a logfile and logcat
public void dumpLog(String message)
{
File file = new File(logFile);
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(logFile, true));
bw.write(message);
bw.newLine();
} catch(IOException e) {
Log.e("Robocop", "exception with file writer on: " + logFile);
} finally {
try {
if (bw != null) {
bw.flush();
bw.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
Log.i("Robocop", message);
}
// Set the filename used for dumpLog.
public void setLogFile(String filename)
{
logFile = filename;
}
class testInfo {
public boolean result;
public String name;
public String diag;
public boolean todo;
public testInfo(boolean r, String n, String d, boolean t) {
result = r;
name = n;
diag = d;
todo = t;
}
}
private void _logResult(testInfo test, String passString, String failString)
{
boolean isError = true;
String resultString = failString;
if(test.result || test.todo){
isError = false;
}
if(test.result)
{
resultString = passString;
}
String diag= test.name;
if(test.diag!=null) diag+= " - " + test.diag;
String message = resultString + " | " + "ROBOCOP" + " | " + diag;
if(isError) {
if(logFile == null)
{
assert(false);
}
else {
dumpLog(message);
}
}
else {
dumpLog(message);
}
}
public void ok(boolean condition, String name, String diag) {
testInfo test = new testInfo(condition, name, diag, false);
_logResult(test, "TEST-PASS", "TEST-UNEXPECTED-FAIL");
testList.add(test);
}
public void is(Object a, Object b, String name) {
boolean pass = a.equals(b);
String diag = "got " + a.toString() + ", expected " + b.toString();
if(pass) {
diag = a.toString() + " should equal " + b.toString();
}
ok(pass, name, diag);
}
public void isnot(Object a, Object b, String name) {
boolean pass = !a.equals(b);
String diag = "didn't expect " + a.toString() + ", but got it";
if(pass) {
diag = a.toString() + " should not equal " + b.toString();
}
ok(pass, name, diag);
}
public void todo(boolean condition, String name, String diag) {
testInfo test = new testInfo(condition, name, diag, true);
_logResult(test, "TEST-UNEXPECTED-PASS", "TEST-KNOWN-FAIL");
testList.add(test);
}
public void todo_is(Object a, Object b, String name) {
boolean pass = a.equals(b);
String diag = "got " + a.toString() + ", expected " + b.toString();
if(pass) {
diag = a.toString() + " should equal " + b.toString();
}
todo(pass, name, diag);
}
public void todo_isnot(Object a, Object b, String name) {
boolean pass = !a.equals(b);
String diag = "didn't expect " + a.toString() + ", but got it";
if(pass) {
diag = a.toString() + " should not equal " + b.toString();
}
todo(pass, name, diag);
}
public void info(String name, String message) {
testInfo test = new testInfo(true, name, message, false);
_logResult(test, "TEST-INFO", "INFO FAILED?");
}
}

View File

@ -51,16 +51,25 @@ JAVAFILES = \
R.java \
_JAVA_HARNESS = \
Actions.java \
Assert.java \
Driver.java \
Element.java \
Actions.java \
FennecNativeActions.java \
FennecNativeAssert.java \
FennecNativeDriver.java \
FennecNativeElement.java \
RoboCopException.java \
FennecNativeDriver.java \
FennecNativeActions.java \
$(NULL)
_JAVA_TESTS = $(patsubst $(TESTPATH)/%.in,%,$(wildcard $(TESTPATH)/*.java.in))
_TEST_FILES = \
$(TESTPATH)/robocop_blank_01.html \
$(TESTPATH)/robocop_blank_02.html \
$(TESTPATH)/robocop_blank_03.html \
$(NULL)
_ROBOCOP_TOOLS = \
$(TESTPATH)/robocop.ini \
parse_ids.py \
@ -104,13 +113,16 @@ $(_ROBOCOP_TOOLS):
cp $(TESTPATH)/robocop.ini robocop.ini
cp $(srcdir)/parse_ids.txt parse_ids.txt
libs:: $(_TEST_FILES)
$(NSINSTALL) -D $(DEPTH)/_tests/testing/mochitest/tests/robocop
$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/tests/robocop/
tools:: robocop.apk
classes.dex: robocop.ap_
classes.dex: $(_ROBOCOP_TOOLS)
classes.dex: $(_JAVA_HARNESS)
classes.dex: $(_JAVA_TESTS)
classes.dex: $(TEST_FILES)
$(NSINSTALL) -D classes
$(JAVAC) $(JAVAC_FLAGS) -d classes $(JAVAFILES) $(_JAVA_HARNESS) $(addprefix $(DEPTH)/mobile/android/base/tests/,$(_JAVA_TESTS))
$(DX) --dex --output=$@ classes $(ROBOTIUM_PATH)

View File

@ -0,0 +1,7 @@
[testAwesomebar]
[testBookmark]
[testLoad]
[testNewTab]
# Used for Talos, please don't use in mochitest
#[testPan]

View File

@ -0,0 +1,6 @@
<html>
<title>Browser Blank Page 01</title>
<body>
<p>Browser Blank Page 01</p>
</body>
</html>

View File

@ -0,0 +1,7 @@
<html>
<title>Browser Blank Page 02</title>
<link rel="shortcut icon" href="data:image/gif;base64,R0lGODlhCwALAIAAAAAA3pn/ZiH5BAEAAAEALAAAAAALAAsAAAIUhA+hkcuO4lmNVindo7qyrIXiGBYAOw==" />
<body>
<p>Browser Blank Page 02</p>
</body>
</html>

View File

@ -0,0 +1,6 @@
<html>
<title>Browser Blank Page 03</title>
<body>
<p>Browser Blank Page 03</p>
</body>
</html>

View File

@ -25,6 +25,7 @@ public class testAwesomebar extends ActivityInstrumentationTestCase2 {
private Solo solo;
private Activity activity;
private Driver driver;
private Assert asserter;
private Actions actions;
private static Class<?> launcherActivityClass;
@ -62,11 +63,13 @@ public class testAwesomebar extends ActivityInstrumentationTestCase2 {
driver = new FennecNativeDriver(activity, solo);
actions = new FennecNativeActions(activity, solo, getInstrumentation());
driver.setLogFile((String)config.get("logfile"));
asserter = new FennecNativeAssert();
asserter.setLogFile((String)config.get("logfile"));
}
public void testAwesomebar() {
// TODO: find a better way to not hardcode this url
String url = "http://mochi.test:8888/tests/robocop/robocop.html";
String url = "http://mochi.test:8888/tests/robocop/robocop_blank_01.html";
actions.waitForGeckoEvent("Gecko:Ready");
Element awesomebar = driver.findElement("awesome_bar");
awesomebar.click();
@ -74,11 +77,13 @@ public class testAwesomebar extends ActivityInstrumentationTestCase2 {
Element urlbar = driver.findElement("awesomebar_text");
getInstrumentation().waitForIdleSync();
actions.sendKeys(url);
driver.is(urlbar.getText(), url, "Awesomebar URL Typed Properly");
asserter.is(urlbar.getText(), url, "Awesomebar URL Typed Properly");
actions.sendSpecialKey(Actions.SpecialKey.ENTER);
//wait for screen to load
actions.waitForGeckoEvent("DOMContentLoaded");
driver.setupScrollHandling();
//Calculate where we should be dragging.
int midX = driver.getGeckoLeft() + driver.getGeckoWidth()/2;
int midY = driver.getGeckoTop() + driver.getGeckoHeight()/2;
@ -94,7 +99,7 @@ public class testAwesomebar extends ActivityInstrumentationTestCase2 {
//Click the awesomebar again
awesomebar.click();
getInstrumentation().waitForIdleSync();
driver.is(urlbar.getText(), url, "Aweosmebar URL stayed the same");
asserter.is(urlbar.getText(), url, "Awesomebar URL stayed the same");
}
@Override

View File

@ -25,6 +25,7 @@ public class testBookmark extends ActivityInstrumentationTestCase2 {
private Solo solo;
private Activity activity;
private Driver driver;
private Assert asserter;
private Actions actions;
private static Class<?> launcherActivityClass;
@ -62,11 +63,14 @@ public class testBookmark extends ActivityInstrumentationTestCase2 {
driver = new FennecNativeDriver(activity, solo);
actions = new FennecNativeActions(activity, solo, getInstrumentation());
driver.setLogFile((String)config.get("logfile"));
asserter = new FennecNativeAssert();
asserter.setLogFile((String)config.get("logfile"));
}
public void testBookmark(){
// TODO: find a better way to not hardcode this url
String url = "http://mochi.test:8888/tests/robocop/robocop.html";
String url = "http://mochi.test:8888/tests/robocop/robocop_blank_02.html";
actions.waitForGeckoEvent("Gecko:Ready");
Element awesomebar = driver.findElement("awesome_bar");
@ -75,7 +79,7 @@ public class testBookmark extends ActivityInstrumentationTestCase2 {
Element urlbar = driver.findElement("awesomebar_text");
getInstrumentation().waitForIdleSync();
actions.sendKeys(url);
driver.is(urlbar.getText(), url, "Awesomebar url typed properly");
asserter.is(urlbar.getText(), url, "Awesomebar url typed properly");
//Click the top item in the list.
actions.sendSpecialKey(Actions.SpecialKey.DOWN);
@ -84,8 +88,7 @@ public class testBookmark extends ActivityInstrumentationTestCase2 {
getInstrumentation().waitForIdleSync();
awesomebar.click();
driver.is(urlbar.getText(), url, "Awesomebar URL still on");
asserter.is(urlbar.getText(), url, "Awesomebar URL still on");
//Click the Top item in the history list.
getInstrumentation().waitForIdleSync();
@ -100,7 +103,7 @@ public class testBookmark extends ActivityInstrumentationTestCase2 {
getInstrumentation().waitForIdleSync();
awesomebar.click();
//Unfortunately, the item isn't constant so can't be tested.
//driver.is(url, urlbar.getText(),"Shouldn't this be the last url in the history?");
//asserter.is(url, urlbar.getText(),"Shouldn't this be the last url in the history?");
}
@Override

View File

@ -25,6 +25,7 @@ public class testLoad extends ActivityInstrumentationTestCase2 {
private Solo solo;
private Activity activity;
private Driver driver;
private Assert asserter;
private Actions actions;
private static Class<?> launcherActivityClass;
@ -62,11 +63,14 @@ public class testLoad extends ActivityInstrumentationTestCase2 {
driver = new FennecNativeDriver(activity, solo);
actions = new FennecNativeActions(activity, solo, getInstrumentation());
driver.setLogFile((String)config.get("logfile"));
asserter = new FennecNativeAssert();
asserter.setLogFile((String)config.get("logfile"));
}
public void testLoad(){
// TODO: find a better way to not hardcode this url
String url = "http://mochi.test:8888/tests/robocop/robocop.html";
String url = "http://mochi.test:8888/tests/robocop/robocop_blank_03.html";
actions.waitForGeckoEvent("Gecko:Ready");
Element awesomebar = driver.findElement("awesome_bar");
@ -76,7 +80,7 @@ public class testLoad extends ActivityInstrumentationTestCase2 {
getInstrumentation().waitForIdleSync();
actions.sendKeys(url);
getInstrumentation().waitForIdleSync();
driver.is(urlbar.getText(), url, "Awesomebar URL Correct");
asserter.is(urlbar.getText(), url, "Awesomebar URL Correct");
//Select top item in the list.
actions.sendSpecialKey(Actions.SpecialKey.DOWN);
@ -85,7 +89,7 @@ public class testLoad extends ActivityInstrumentationTestCase2 {
awesomebar.click();
getInstrumentation().waitForIdleSync();
driver.is(urlbar.getText(), url, "Awesomebar URL is still correct");
asserter.is(urlbar.getText(), url, "Awesomebar URL is still correct");
}
@Override

View File

@ -26,6 +26,7 @@ public class testNewTab extends ActivityInstrumentationTestCase2 {
private Activity activity;
private Actions actions;
private Driver driver;
private Assert asserter;
private static Class<?> launcherActivityClass;
static{
@ -62,11 +63,15 @@ public class testNewTab extends ActivityInstrumentationTestCase2 {
driver = new FennecNativeDriver(activity, solo);
actions = new FennecNativeActions(activity, solo, getInstrumentation());
driver.setLogFile((String)config.get("logfile"));
asserter = new FennecNativeAssert();
asserter.setLogFile((String)config.get("logfile"));
}
public void testNewTab(){
// TODO: find a better way to not hardcode this url
String url = "http://mochi.test:8888/tests/robocop/robocop.html";
String url = "http://mochi.test:8888/tests/robocop/robocop_blank_01.html";
String url2 = "http://mochi.test:8888/tests/robocop/robocop_blank_02.html";
actions.waitForGeckoEvent("Gecko:Ready");
Element tabs = driver.findElement("tabs");
//Add one tab
@ -75,14 +80,14 @@ public class testNewTab extends ActivityInstrumentationTestCase2 {
Element urlbar = driver.findElement("awesomebar_text");
getInstrumentation().waitForIdleSync();
actions.sendKeys(url);
driver.is(urlbar.getText(), url, "Awesomebar url is fine");
asserter.is(urlbar.getText(), url, "Awesomebar url is fine");
actions.sendSpecialKey(Actions.SpecialKey.ENTER);
actions.waitForGeckoEvent("DOMContentLoaded");
try{Thread.sleep(5000);}catch(Throwable e){};
//See tab count
Element tabCount = driver.findElement("tabs_count");
driver.is(tabCount.getText(), "2", "Number of tabs has increased");
asserter.is(tabCount.getText(), "2", "Number of tabs has increased");
//Click tab list
tabs.click();
@ -91,14 +96,14 @@ public class testNewTab extends ActivityInstrumentationTestCase2 {
//Add another tab
addTab.click();
getInstrumentation().waitForIdleSync();
actions.sendKeys(url);
actions.sendKeys(url2);
getInstrumentation().waitForIdleSync();
driver.is(urlbar.getText(), url, "URL is still fine");
asserter.is(urlbar.getText(), url2, "URL is still fine");
actions.sendSpecialKey(Actions.SpecialKey.ENTER);
actions.waitForGeckoEvent("DOMContentLoaded");
//Check tab count another time.
driver.is(tabCount.getText(), "3", "Number of tabs has increased");
asserter.is(tabCount.getText(), "3", "Number of tabs has increased");
}

View File

@ -28,6 +28,7 @@ public class testPan extends ActivityInstrumentationTestCase2 {
private Activity activity;
private Driver driver;
private Actions actions;
private Assert asserter;
private static Class<?> launcherActivityClass;
static{
@ -64,11 +65,14 @@ public class testPan extends ActivityInstrumentationTestCase2 {
driver = new FennecNativeDriver(activity, solo);
actions = new FennecNativeActions(activity, solo, getInstrumentation());
driver.setLogFile((String)config.get("logfile"));
asserter = new FennecNativeAssert();
asserter.setLogFile((String)config.get("logfile"));
}
public void testPan() {
// TODO: find a better way to not hardcode this url
String url = "http://mochi.test:8888/tests/robocop/pantest.html";
String url = "http://mochi.test:8888/startup_test/fenncmark/wikipedia.html";
actions.waitForGeckoEvent("Gecko:Ready");
Element awesomebar = driver.findElement("awesome_bar");
awesomebar.click();
@ -76,7 +80,7 @@ public class testPan extends ActivityInstrumentationTestCase2 {
Element urlbar = driver.findElement("awesomebar_text");
getInstrumentation().waitForIdleSync();
actions.sendKeys(url);
driver.is(url, urlbar.getText(),"Asserting Awesomebar typing works");
asserter.is(url, urlbar.getText(),"Asserting Awesomebar typing works");
actions.sendSpecialKey(Actions.SpecialKey.ENTER);
actions.waitForGeckoEvent("DOMContentLoaded");
@ -101,7 +105,7 @@ public class testPan extends ActivityInstrumentationTestCase2 {
}
i++;
} while( i < 1000 && driver.getScrollHeight() + 2*driver.getHeight() < driver.getPageHeight() );
driver.ok(i < 1000, "Less than 1000", "Should take less than 1000 drags to get to bottom of the page.");
// asserter.ok(i < 1000, "Less than 1000", "Should take less than 1000 drags to get to bottom of the page.");
int frames = driver.stopFrameRecording();
driver.dumpLog("__start_report" + Integer.toString(frames) + "__end_report");