Bug 986114 - Pre: introduce BaseRobocopTest, rework testBrowserProviderPerf. r=nalexander

This commit is contained in:
Richard Newman 2014-03-28 12:01:47 -07:00
Родитель 994c287be7
Коммит 72eb2ce657
6 изменённых файлов: 268 добавлений и 123 удалений

Просмотреть файл

@ -1,24 +1,23 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko.tests;
import com.jayway.android.robotium.solo.Condition;
import org.mozilla.gecko.*;
import java.util.ArrayList;
import org.mozilla.gecko.Actions;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TabWidget;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TabWidget;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
import com.jayway.android.robotium.solo.Condition;
/**
* This class is an extension of BaseTest that helps with interaction with about:home
@ -36,7 +35,7 @@ abstract class AboutHomeTest extends PixelTest {
@Override
protected void setUp() throws Exception {
public void setUp() throws Exception {
super.setUp();
if (aboutHomeTabs.size() < 4) {

Просмотреть файл

@ -0,0 +1,57 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko.tests;
import java.util.Map;
import org.mozilla.gecko.Assert;
import org.mozilla.gecko.FennecInstrumentationTestRunner;
import org.mozilla.gecko.FennecMochitestAssert;
import org.mozilla.gecko.FennecNativeDriver;
import org.mozilla.gecko.FennecTalosAssert;
import android.app.Activity;
import android.test.ActivityInstrumentationTestCase2;
public abstract class BaseRobocopTest extends ActivityInstrumentationTestCase2<Activity> {
public static final int TEST_MOCHITEST = 0;
public static final int TEST_TALOS = 1;
protected static final String TARGET_PACKAGE_ID = "org.mozilla.gecko";
protected Assert mAsserter;
protected String mLogFile;
protected Map<?, ?> mConfig;
protected String mRootPath;
public BaseRobocopTest(Class<Activity> activityClass) {
super(activityClass);
}
@SuppressWarnings("deprecation")
public BaseRobocopTest(String targetPackageId, Class<Activity> activityClass) {
super(targetPackageId, activityClass);
}
protected abstract int getTestType();
@Override
protected void setUp() throws Exception {
// Load config file from root path (set up by Python script).
mRootPath = FennecInstrumentationTestRunner.getFennecArguments().getString("deviceroot");
String configFile = FennecNativeDriver.getFile(mRootPath + "/robotium.config");
mConfig = FennecNativeDriver.convertTextToTable(configFile);
mLogFile = (String) mConfig.get("logfile");
// Initialize the asserter.
if (getTestType() == TEST_TALOS) {
mAsserter = new FennecTalosAssert();
} else {
mAsserter = new FennecMochitestAssert();
}
mAsserter.setLogFile(mLogFile);
mAsserter.setTestName(this.getClass().getName());
}
}

Просмотреть файл

@ -1,63 +1,62 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko.tests;
import com.jayway.android.robotium.solo.Condition;
import com.jayway.android.robotium.solo.Solo;
import org.mozilla.gecko.*;
import org.mozilla.gecko.GeckoThread.LaunchState;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.mozilla.gecko.Actions;
import org.mozilla.gecko.Driver;
import org.mozilla.gecko.Element;
import org.mozilla.gecko.FennecNativeActions;
import org.mozilla.gecko.FennecNativeDriver;
import org.mozilla.gecko.GeckoAppShell;
import org.mozilla.gecko.GeckoThread;
import org.mozilla.gecko.GeckoThread.LaunchState;
import org.mozilla.gecko.R;
import org.mozilla.gecko.RobocopUtils;
import org.mozilla.gecko.Tabs;
import android.app.Activity;
import android.app.Instrumentation;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.ContentUris;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.SystemClock;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.text.InputType;
import android.text.TextUtils;
import android.test.ActivityInstrumentationTestCase2;
import android.util.DisplayMetrics;
import android.view.inputmethod.InputMethodManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import com.jayway.android.robotium.solo.Condition;
import com.jayway.android.robotium.solo.Solo;
/**
* A convenient base class suitable for most Robocop tests.
*/
abstract class BaseTest extends ActivityInstrumentationTestCase2<Activity> {
public static final int TEST_MOCHITEST = 0;
public static final int TEST_TALOS = 1;
private static final String TARGET_PACKAGE_ID = "org.mozilla.gecko";
@SuppressWarnings("unchecked")
abstract class BaseTest extends BaseRobocopTest {
private static final String LAUNCH_ACTIVITY_FULL_CLASSNAME = TestConstants.ANDROID_PACKAGE_NAME + ".App";
private static final int VERIFY_URL_TIMEOUT = 2000;
private static final int MAX_LIST_ATTEMPTS = 3;
private static final int MAX_WAIT_ENABLED_TEXT_MS = 10000;
private static final int MAX_WAIT_HOME_PAGER_HIDDEN_MS = 15000;
public static final int MAX_WAIT_MS = 4500;
@ -70,11 +69,9 @@ abstract class BaseTest extends ActivityInstrumentationTestCase2<Activity> {
private int mPreferenceRequestID = 0;
protected Solo mSolo;
protected Driver mDriver;
protected Assert mAsserter;
protected Actions mActions;
protected String mBaseUrl;
protected String mRawBaseUrl;
private String mLogFile;
protected String mProfile;
public Device mDevice;
protected DatabaseHelper mDatabaseHelper;
@ -104,30 +101,17 @@ abstract class BaseTest extends ActivityInstrumentationTestCase2<Activity> {
super(TARGET_PACKAGE_ID, mLauncherActivityClass);
}
protected abstract int getTestType();
@Override
protected void setUp() throws Exception {
// Load config file from root path (setup by python script)
String rootPath = FennecInstrumentationTestRunner.getFennecArguments().getString("deviceroot");
String configFile = FennecNativeDriver.getFile(rootPath + "/robotium.config");
HashMap config = FennecNativeDriver.convertTextToTable(configFile);
mLogFile = (String)config.get("logfile");
mBaseUrl = ((String)config.get("host")).replaceAll("(/$)", "");
mRawBaseUrl = ((String)config.get("rawhost")).replaceAll("(/$)", "");
// Initialize the asserter
if (getTestType() == TEST_TALOS) {
mAsserter = new FennecTalosAssert();
} else {
mAsserter = new FennecMochitestAssert();
}
mAsserter.setLogFile(mLogFile);
mAsserter.setTestName(this.getClass().getName());
public void setUp() throws Exception {
super.setUp();
// Create the intent to be used with all the important arguments.
mBaseUrl = ((String) mConfig.get("host")).replaceAll("(/$)", "");
mRawBaseUrl = ((String) mConfig.get("rawhost")).replaceAll("(/$)", "");
Intent i = new Intent(Intent.ACTION_MAIN);
mProfile = (String)config.get("profile");
mProfile = (String) mConfig.get("profile");
i.putExtra("args", "-no-remote -profile " + mProfile);
String envString = (String)config.get("envvars");
String envString = (String) mConfig.get("envvars");
if (envString != "") {
String[] envStrings = envString.split(",");
for (int iter = 0; iter < envStrings.length; iter++) {
@ -139,7 +123,7 @@ abstract class BaseTest extends ActivityInstrumentationTestCase2<Activity> {
mActivity = getActivity();
// Set up Robotium.solo and Driver objects
mSolo = new Solo(getInstrumentation(), mActivity);
mDriver = new FennecNativeDriver(mActivity, mSolo, rootPath);
mDriver = new FennecNativeDriver(mActivity, mSolo, mRootPath);
mActions = new FennecNativeActions(mActivity, mSolo, getInstrumentation(), mAsserter);
mDevice = new Device();
mDatabaseHelper = new DatabaseHelper(mActivity, mAsserter);
@ -333,7 +317,6 @@ abstract class BaseTest extends ActivityInstrumentationTestCase2<Activity> {
public boolean test();
}
@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();
@ -342,27 +325,6 @@ abstract class BaseTest extends ActivityInstrumentationTestCase2<Activity> {
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)) {
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;
}
}
}
}
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()) {
@ -374,7 +336,7 @@ abstract class BaseTest extends ActivityInstrumentationTestCase2<Activity> {
}
}
mAsserter.is(found, true, "Password was found");
} while(c.moveToNext());
} while (c.moveToNext());
}
}

Просмотреть файл

@ -1,3 +1,7 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko.tests;
import android.content.ContentProvider;
@ -208,7 +212,7 @@ abstract class ContentProviderTest extends BaseTest {
mResolver.addProvider(mProviderAuthority, mProvider);
}
public Uri appendUriParam(Uri uri, String param, String value) {
public static Uri appendUriParam(Uri uri, String param, String value) {
return uri.buildUpon().appendQueryParameter(param, value).build();
}

Просмотреть файл

@ -1,22 +1,47 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.gecko.tests;
import java.io.File;
import java.util.Random;
import java.util.UUID;
import org.mozilla.gecko.db.BrowserContract;
import org.mozilla.gecko.db.BrowserProvider;
import org.mozilla.gecko.db.LocalBrowserDB;
import org.mozilla.gecko.util.FileUtils;
import android.app.Activity;
import android.content.ContentProvider;
import android.content.ContentProviderClient;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.SystemClock;
import android.util.Log;
import java.util.UUID;
import java.util.Random;
import org.mozilla.gecko.GeckoProfile;
import org.mozilla.gecko.db.BrowserContract;
import org.mozilla.gecko.db.BrowserDB;
import org.mozilla.gecko.db.BrowserProvider;
/*
* This test is meant to exercise the performance of Fennec's
* history and bookmarks content provider.
/**
* This test is meant to exercise the performance of Fennec's history and
* bookmarks content provider.
*
* It does not extend ContentProviderTest because that class is unable to
* accurately assess the performance of the ContentProvider -- it's a second
* instance of a class that's only supposed to exist once, wrapped in a bunch of
* junk.
*
* Instead, we directly use the existing ContentProvider, accessing a new
* profile directory that we initialize via BrowserDB.
*/
public class testBrowserProviderPerf extends ContentProviderTest {
@SuppressWarnings("unchecked")
public class testBrowserProviderPerf extends BaseRobocopTest {
private static final String LAUNCH_ACTIVITY_FULL_CLASSNAME = TestConstants.ANDROID_PACKAGE_NAME + ".App";
private static Class<Activity> mLauncherActivityClass;
private final int NUMBER_OF_BASIC_HISTORY_URLS = 10000;
private final int NUMBER_OF_BASIC_BOOKMARK_URLS = 500;
private final int NUMBER_OF_COMBINED_URLS = 500;
@ -25,13 +50,30 @@ public class testBrowserProviderPerf extends ContentProviderTest {
private final int BATCH_SIZE = 500;
// Include spaces in prefix to test performance querying with
// multiple constraint words
// multiple constraint words.
private final String KNOWN_PREFIX = "my mozilla test ";
private Random mGenerator;
private final String MOBILE_FOLDER_GUID = "mobile";
private long mMobileFolderId;
private ContentResolver mResolver;
private String mProfile;
private Uri mHistoryURI;
private Uri mBookmarksURI;
private Uri mFaviconsURI;
static {
try {
mLauncherActivityClass = (Class<Activity>) Class.forName(LAUNCH_ACTIVITY_FULL_CLASSNAME);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
public testBrowserProviderPerf() {
super(TARGET_PACKAGE_ID, mLauncherActivityClass);
}
@Override
protected int getTestType() {
@ -39,7 +81,7 @@ public class testBrowserProviderPerf extends ContentProviderTest {
}
private void loadMobileFolderId() throws Exception {
Cursor c = mProvider.query(BrowserContract.Bookmarks.CONTENT_URI, null,
Cursor c = mResolver.query(mBookmarksURI, null,
BrowserContract.Bookmarks.GUID + " = ?",
new String[] { MOBILE_FOLDER_GUID },
null);
@ -121,7 +163,7 @@ public class testBrowserProviderPerf extends ContentProviderTest {
}
private void addTonsOfUrls() throws Exception {
// Create some random bookmark entries
// Create some random bookmark entries.
ContentValues[] bookmarkEntries = new ContentValues[BATCH_SIZE];
for (int i = 0; i < NUMBER_OF_BASIC_BOOKMARK_URLS / BATCH_SIZE; i++) {
@ -131,10 +173,10 @@ public class testBrowserProviderPerf extends ContentProviderTest {
bookmarkEntries[j] = createRandomBookmarkEntry();
}
mProvider.bulkInsert(BrowserContract.Bookmarks.CONTENT_URI, bookmarkEntries);
mResolver.bulkInsert(mBookmarksURI, bookmarkEntries);
}
// Create some random history entries
// Create some random history entries.
ContentValues[] historyEntries = new ContentValues[BATCH_SIZE];
ContentValues[] faviconEntries = new ContentValues[BATCH_SIZE];
@ -147,12 +189,12 @@ public class testBrowserProviderPerf extends ContentProviderTest {
faviconEntries[j] = createFaviconEntryWithUrl(historyEntries[j].getAsString(BrowserContract.History.URL));
}
mProvider.bulkInsert(BrowserContract.History.CONTENT_URI, historyEntries);
mProvider.bulkInsert(BrowserContract.Favicons.CONTENT_URI, faviconEntries);
mResolver.bulkInsert(mHistoryURI, historyEntries);
mResolver.bulkInsert(mFaviconsURI, faviconEntries);
}
// Create random bookmark/history entries with the same url
// Create random bookmark/history entries with the same URL.
for (int i = 0; i < NUMBER_OF_COMBINED_URLS / BATCH_SIZE; i++) {
bookmarkEntries = new ContentValues[BATCH_SIZE];
historyEntries = new ContentValues[BATCH_SIZE];
@ -164,12 +206,12 @@ public class testBrowserProviderPerf extends ContentProviderTest {
faviconEntries[j] = createFaviconEntryWithUrl(url);
}
mProvider.bulkInsert(BrowserContract.Bookmarks.CONTENT_URI, bookmarkEntries);
mProvider.bulkInsert(BrowserContract.History.CONTENT_URI, historyEntries);
mProvider.bulkInsert(BrowserContract.Favicons.CONTENT_URI, faviconEntries);
mResolver.bulkInsert(mBookmarksURI, bookmarkEntries);
mResolver.bulkInsert(mHistoryURI, historyEntries);
mResolver.bulkInsert(mFaviconsURI, faviconEntries);
}
// Create some history entries with a known prefix
// Create some history entries with a known prefix.
historyEntries = new ContentValues[NUMBER_OF_KNOWN_URLS];
faviconEntries = new ContentValues[NUMBER_OF_KNOWN_URLS];
for (int i = 0; i < NUMBER_OF_KNOWN_URLS; i++) {
@ -177,34 +219,112 @@ public class testBrowserProviderPerf extends ContentProviderTest {
faviconEntries[i] = createFaviconEntryWithUrl(historyEntries[i].getAsString(BrowserContract.History.URL));
}
mProvider.bulkInsert(BrowserContract.History.CONTENT_URI, historyEntries);
mProvider.bulkInsert(BrowserContract.Favicons.CONTENT_URI, faviconEntries);
mResolver.bulkInsert(mHistoryURI, historyEntries);
mResolver.bulkInsert(mFaviconsURI, faviconEntries);
}
@Override
public void setUp() throws Exception {
super.setUp(sBrowserProviderCallable, BrowserContract.AUTHORITY, "browser.db");
super.setUp();
mProfile = "prof" + System.currentTimeMillis();
mHistoryURI = prepUri(BrowserContract.History.CONTENT_URI);
mBookmarksURI = prepUri(BrowserContract.Bookmarks.CONTENT_URI);
mFaviconsURI = prepUri(BrowserContract.Favicons.CONTENT_URI);
mResolver = getActivity().getApplicationContext().getContentResolver();
mGenerator = new Random(19580427);
}
public void testBrowserProviderPerf() throws Exception {
BrowserDB.initialize(GeckoProfile.DEFAULT_PROFILE);
@Override
public void tearDown() {
final ContentProviderClient client = mResolver.acquireContentProviderClient(mBookmarksURI);
try {
final ContentProvider cp = client.getLocalContentProvider();
final BrowserProvider bp = ((BrowserProvider) cp);
// This will be the DB we were just testing.
final SQLiteDatabase db = bp.getWritableDatabaseForTesting(mBookmarksURI);
try {
db.close();
} catch (Throwable e) {
// Nothing we can do.
}
} finally {
try {
client.release();
} catch (Throwable e) {
// Still go ahead and try to delete the profile.
}
try {
FileUtils.delTree(new File(mProfile), null, true);
} catch (Exception e) {
Log.w("GeckoTest", "Unable to delete profile " + mProfile, e);
}
}
}
public Uri prepUri(Uri uri) {
return uri.buildUpon()
.appendQueryParameter(BrowserContract.PARAM_PROFILE, mProfile)
.appendQueryParameter(BrowserContract.PARAM_IS_SYNC, "1") // So we don't trigger a sync.
.build();
}
/**
* This method:
*
* * Adds a bunch of test data via the ContentProvider API.
* * Runs a single query against that test data via BrowserDB.
* * Reports timing for Talos.
*/
public void testBrowserProviderQueryPerf() throws Exception {
// We add at least this many results.
final int limit = 100;
// Make sure we're querying the right profile.
final LocalBrowserDB db = new LocalBrowserDB(mProfile);
final Cursor before = db.filter(mResolver, KNOWN_PREFIX, limit);
try {
mAsserter.is(before.getCount(), 0, "Starts empty");
} finally {
before.close();
}
// Add data.
loadMobileFolderId();
addTonsOfUrls();
long start = SystemClock.uptimeMillis();
// Wait for a little while after inserting data. We do this because
// this test launches about:home, and Top Sites watches for DB changes.
// We don't have a good way for it to only watch changes related to
// its current profile, nor is it convenient for us to launch a different
// activity that doesn't depend on the DB.
// We can fix this by:
// * Adjusting the provider interface to allow a "don't notify" param.
// * Adjusting the interface schema to include the profile in the path,
// and only observe the correct path.
// * Launching a different activity.
Thread.sleep(5000);
final Cursor c = BrowserDB.filter(mResolver, KNOWN_PREFIX, 100);
c.getCount(); // ensure query is not lazy loaded
// Time the query.
final long start = SystemClock.uptimeMillis();
final Cursor c = db.filter(mResolver, KNOWN_PREFIX, limit);
long end = SystemClock.uptimeMillis();
try {
final int count = c.getCount();
final long end = SystemClock.uptimeMillis();
mAsserter.dumpLog("__start_report" + Long.toString(end - start) + "__end_report");
mAsserter.dumpLog("__startTimestamp" + Long.toString(end - start) + "__endTimestamp");
c.close();
mAsserter.is(count, limit, "Retrieved results");
mAsserter.dumpLog("Results: " + count);
mAsserter.dumpLog("__start_report" + Long.toString(end - start) + "__end_report");
mAsserter.dumpLog("__startTimestamp" + Long.toString(end - start) + "__endTimestamp");
} finally {
c.close();
}
}
}

Просмотреть файл

@ -10,6 +10,8 @@ import java.io.File;
import java.io.IOException;
import java.io.FilenameFilter;
import org.mozilla.gecko.mozglue.RobocopTarget;
public class FileUtils {
private static final String LOGTAG= "GeckoFileUtils";
/*
@ -38,6 +40,7 @@ public class FileUtils {
}
}
@RobocopTarget
public static void delTree(File dir, FilenameFilter filter, boolean recurse) {
String[] files = null;