gecko/build/mobile/robocop/FennecNativeActions.java

391 lines
14 KiB
Java
Raw Normal View History

2012-05-21 04:12:37 -07:00
/* 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/. */
Bug 905703 - Remove preprocessing from robocop harness and tests. r=nalexander --HG-- rename : build/mobile/robocop/Actions.java.in => build/mobile/robocop/Actions.java rename : build/mobile/robocop/Assert.java.in => build/mobile/robocop/Assert.java rename : build/mobile/robocop/Driver.java.in => build/mobile/robocop/Driver.java rename : build/mobile/robocop/Element.java.in => build/mobile/robocop/Element.java rename : build/mobile/robocop/FennecInstrumentationTestRunner.java.in => build/mobile/robocop/FennecInstrumentationTestRunner.java rename : build/mobile/robocop/FennecMochitestAssert.java.in => build/mobile/robocop/FennecMochitestAssert.java rename : build/mobile/robocop/FennecNativeActions.java.in => build/mobile/robocop/FennecNativeActions.java rename : build/mobile/robocop/FennecNativeDriver.java.in => build/mobile/robocop/FennecNativeDriver.java rename : build/mobile/robocop/FennecNativeElement.java.in => build/mobile/robocop/FennecNativeElement.java rename : build/mobile/robocop/FennecTalosAssert.java.in => build/mobile/robocop/FennecTalosAssert.java rename : build/mobile/robocop/PaintedSurface.java.in => build/mobile/robocop/PaintedSurface.java rename : build/mobile/robocop/RoboCopException.java.in => build/mobile/robocop/RoboCopException.java rename : build/mobile/robocop/RobocopUtils.java.in => build/mobile/robocop/RobocopUtils.java rename : mobile/android/base/tests/AboutHomeTest.java.in => mobile/android/base/tests/AboutHomeTest.java rename : mobile/android/base/tests/BaseTest.java.in => mobile/android/base/tests/BaseTest.java rename : mobile/android/base/tests/ContentContextMenuTest.java.in => mobile/android/base/tests/ContentContextMenuTest.java rename : mobile/android/base/tests/ContentProviderTest.java.in => mobile/android/base/tests/ContentProviderTest.java rename : mobile/android/base/tests/DatabaseHelper.java.in => mobile/android/base/tests/DatabaseHelper.java rename : mobile/android/base/tests/JavascriptTest.java.in => mobile/android/base/tests/JavascriptTest.java rename : mobile/android/base/tests/MotionEventHelper.java.in => mobile/android/base/tests/MotionEventHelper.java rename : mobile/android/base/tests/MotionEventReplayer.java.in => mobile/android/base/tests/MotionEventReplayer.java rename : mobile/android/base/tests/PixelTest.java.in => mobile/android/base/tests/PixelTest.java rename : mobile/android/base/tests/StringHelper.java.in => mobile/android/base/tests/StringHelper.java rename : mobile/android/base/tests/testAboutPage.java.in => mobile/android/base/tests/testAboutPage.java rename : mobile/android/base/tests/testAddSearchEngine.java.in => mobile/android/base/tests/testAddSearchEngine.java rename : mobile/android/base/tests/testAddonManager.java.in => mobile/android/base/tests/testAddonManager.java rename : mobile/android/base/tests/testAwesomebar.java.in => mobile/android/base/tests/testAwesomebar.java rename : mobile/android/base/tests/testAwesomebarSwipes.java.in => mobile/android/base/tests/testAwesomebarSwipes.java rename : mobile/android/base/tests/testAxisLocking.java.in => mobile/android/base/tests/testAxisLocking.java rename : mobile/android/base/tests/testBookmark.java.in => mobile/android/base/tests/testBookmark.java rename : mobile/android/base/tests/testBookmarkFolders.java.in => mobile/android/base/tests/testBookmarkFolders.java rename : mobile/android/base/tests/testBookmarkKeyword.java.in => mobile/android/base/tests/testBookmarkKeyword.java rename : mobile/android/base/tests/testBookmarklets.java.in => mobile/android/base/tests/testBookmarklets.java rename : mobile/android/base/tests/testBookmarksPage.java.in => mobile/android/base/tests/testBookmarksPage.java rename : mobile/android/base/tests/testBrowserProvider.java.in => mobile/android/base/tests/testBrowserProvider.java rename : mobile/android/base/tests/testBrowserProviderPerf.java.in => mobile/android/base/tests/testBrowserProviderPerf.java rename : mobile/android/base/tests/testBrowserSearchVisibility.java.in => mobile/android/base/tests/testBrowserSearchVisibility.java rename : mobile/android/base/tests/testCheck.java.in => mobile/android/base/tests/testCheck.java rename : mobile/android/base/tests/testCheck2.java.in => mobile/android/base/tests/testCheck2.java rename : mobile/android/base/tests/testClearPrivateData.java.in => mobile/android/base/tests/testClearPrivateData.java rename : mobile/android/base/tests/testDeviceSearchEngine.java.in => mobile/android/base/tests/testDeviceSearchEngine.java rename : mobile/android/base/tests/testDistribution.java.in => mobile/android/base/tests/testDistribution.java rename : mobile/android/base/tests/testDoorHanger.java.in => mobile/android/base/tests/testDoorHanger.java rename : mobile/android/base/tests/testFindInPage.java.in => mobile/android/base/tests/testFindInPage.java rename : mobile/android/base/tests/testFlingCorrectness.java.in => mobile/android/base/tests/testFlingCorrectness.java rename : mobile/android/base/tests/testFormHistory.java.in => mobile/android/base/tests/testFormHistory.java rename : mobile/android/base/tests/testGetUserMedia.java.in => mobile/android/base/tests/testGetUserMedia.java rename : mobile/android/base/tests/testHistory.java.in => mobile/android/base/tests/testHistory.java rename : mobile/android/base/tests/testHomeBanner.java.in => mobile/android/base/tests/testHomeBanner.java rename : mobile/android/base/tests/testImportFromAndroid.java.in => mobile/android/base/tests/testImportFromAndroid.java rename : mobile/android/base/tests/testInputUrlBar.java.in => mobile/android/base/tests/testInputUrlBar.java rename : mobile/android/base/tests/testJNI.java.in => mobile/android/base/tests/testJNI.java rename : mobile/android/base/tests/testJarReader.java.in => mobile/android/base/tests/testJarReader.java rename : mobile/android/base/tests/testLinkContextMenu.java.in => mobile/android/base/tests/testLinkContextMenu.java rename : mobile/android/base/tests/testLoad.java.in => mobile/android/base/tests/testLoad.java rename : mobile/android/base/tests/testMailToContextMenu.java.in => mobile/android/base/tests/testMailToContextMenu.java rename : mobile/android/base/tests/testMasterPassword.java.in => mobile/android/base/tests/testMasterPassword.java rename : mobile/android/base/tests/testNewTab.java.in => mobile/android/base/tests/testNewTab.java rename : mobile/android/base/tests/testOrderedBroadcast.java.in => mobile/android/base/tests/testOrderedBroadcast.java rename : mobile/android/base/tests/testOverscroll.java.in => mobile/android/base/tests/testOverscroll.java rename : mobile/android/base/tests/testPan.java.in => mobile/android/base/tests/testPan.java rename : mobile/android/base/tests/testPanCorrectness.java.in => mobile/android/base/tests/testPanCorrectness.java rename : mobile/android/base/tests/testPasswordEncrypt.java.in => mobile/android/base/tests/testPasswordEncrypt.java rename : mobile/android/base/tests/testPasswordProvider.java.in => mobile/android/base/tests/testPasswordProvider.java rename : mobile/android/base/tests/testPermissions.java.in => mobile/android/base/tests/testPermissions.java rename : mobile/android/base/tests/testPictureLinkContextMenu.java.in => mobile/android/base/tests/testPictureLinkContextMenu.java rename : mobile/android/base/tests/testPrefsObserver.java.in => mobile/android/base/tests/testPrefsObserver.java rename : mobile/android/base/tests/testPrivateBrowsing.java.in => mobile/android/base/tests/testPrivateBrowsing.java rename : mobile/android/base/tests/testPromptGridInput.java.in => mobile/android/base/tests/testPromptGridInput.java rename : mobile/android/base/tests/testReaderMode.java.in => mobile/android/base/tests/testReaderMode.java rename : mobile/android/base/tests/testSearchSuggestions.java.in => mobile/android/base/tests/testSearchSuggestions.java rename : mobile/android/base/tests/testSettingsMenuItems.java.in => mobile/android/base/tests/testSettingsMenuItems.java rename : mobile/android/base/tests/testShareLink.java.in => mobile/android/base/tests/testShareLink.java rename : mobile/android/base/tests/testSharedPreferences.java.in => mobile/android/base/tests/testSharedPreferences.java rename : mobile/android/base/tests/testSystemPages.java.in => mobile/android/base/tests/testSystemPages.java rename : mobile/android/base/tests/testTabHistory.java.in => mobile/android/base/tests/testTabHistory.java rename : mobile/android/base/tests/testThumbnails.java.in => mobile/android/base/tests/testThumbnails.java rename : mobile/android/base/tests/testVkbOverlap.java.in => mobile/android/base/tests/testVkbOverlap.java rename : mobile/android/base/tests/test_bug720538.java.in => mobile/android/base/tests/test_bug720538.java extra : rebase_source : c8d715aa110b07c6010054a674879e143a5f04e7 extra : amend_source : fb5714b78e055e336b5e0c6a0da8eb8c931ff09e extra : histedit_source : ec57d8ba94000092438a2ba0bf4fb66091e98513%2C114ade72bb9b72b96557da74f1179734fa347e5c
2013-11-07 08:18:51 -08:00
package org.mozilla.gecko;
import org.mozilla.gecko.GeckoAppShell;
import org.mozilla.gecko.gfx.GeckoLayerClient;
import org.mozilla.gecko.gfx.GeckoLayerClient.DrawListener;
import org.mozilla.gecko.mozglue.GeckoLoader;
import org.mozilla.gecko.sqlite.SQLiteBridge;
import org.mozilla.gecko.util.GeckoEventListener;
import android.app.Activity;
import android.app.Instrumentation;
import android.content.Context;
import android.database.Cursor;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.ArrayList;
import org.json.JSONObject;
import com.jayway.android.robotium.solo.Solo;
Bug 905703 - Remove preprocessing from robocop harness and tests. r=nalexander --HG-- rename : build/mobile/robocop/Actions.java.in => build/mobile/robocop/Actions.java rename : build/mobile/robocop/Assert.java.in => build/mobile/robocop/Assert.java rename : build/mobile/robocop/Driver.java.in => build/mobile/robocop/Driver.java rename : build/mobile/robocop/Element.java.in => build/mobile/robocop/Element.java rename : build/mobile/robocop/FennecInstrumentationTestRunner.java.in => build/mobile/robocop/FennecInstrumentationTestRunner.java rename : build/mobile/robocop/FennecMochitestAssert.java.in => build/mobile/robocop/FennecMochitestAssert.java rename : build/mobile/robocop/FennecNativeActions.java.in => build/mobile/robocop/FennecNativeActions.java rename : build/mobile/robocop/FennecNativeDriver.java.in => build/mobile/robocop/FennecNativeDriver.java rename : build/mobile/robocop/FennecNativeElement.java.in => build/mobile/robocop/FennecNativeElement.java rename : build/mobile/robocop/FennecTalosAssert.java.in => build/mobile/robocop/FennecTalosAssert.java rename : build/mobile/robocop/PaintedSurface.java.in => build/mobile/robocop/PaintedSurface.java rename : build/mobile/robocop/RoboCopException.java.in => build/mobile/robocop/RoboCopException.java rename : build/mobile/robocop/RobocopUtils.java.in => build/mobile/robocop/RobocopUtils.java rename : mobile/android/base/tests/AboutHomeTest.java.in => mobile/android/base/tests/AboutHomeTest.java rename : mobile/android/base/tests/BaseTest.java.in => mobile/android/base/tests/BaseTest.java rename : mobile/android/base/tests/ContentContextMenuTest.java.in => mobile/android/base/tests/ContentContextMenuTest.java rename : mobile/android/base/tests/ContentProviderTest.java.in => mobile/android/base/tests/ContentProviderTest.java rename : mobile/android/base/tests/DatabaseHelper.java.in => mobile/android/base/tests/DatabaseHelper.java rename : mobile/android/base/tests/JavascriptTest.java.in => mobile/android/base/tests/JavascriptTest.java rename : mobile/android/base/tests/MotionEventHelper.java.in => mobile/android/base/tests/MotionEventHelper.java rename : mobile/android/base/tests/MotionEventReplayer.java.in => mobile/android/base/tests/MotionEventReplayer.java rename : mobile/android/base/tests/PixelTest.java.in => mobile/android/base/tests/PixelTest.java rename : mobile/android/base/tests/StringHelper.java.in => mobile/android/base/tests/StringHelper.java rename : mobile/android/base/tests/testAboutPage.java.in => mobile/android/base/tests/testAboutPage.java rename : mobile/android/base/tests/testAddSearchEngine.java.in => mobile/android/base/tests/testAddSearchEngine.java rename : mobile/android/base/tests/testAddonManager.java.in => mobile/android/base/tests/testAddonManager.java rename : mobile/android/base/tests/testAwesomebar.java.in => mobile/android/base/tests/testAwesomebar.java rename : mobile/android/base/tests/testAwesomebarSwipes.java.in => mobile/android/base/tests/testAwesomebarSwipes.java rename : mobile/android/base/tests/testAxisLocking.java.in => mobile/android/base/tests/testAxisLocking.java rename : mobile/android/base/tests/testBookmark.java.in => mobile/android/base/tests/testBookmark.java rename : mobile/android/base/tests/testBookmarkFolders.java.in => mobile/android/base/tests/testBookmarkFolders.java rename : mobile/android/base/tests/testBookmarkKeyword.java.in => mobile/android/base/tests/testBookmarkKeyword.java rename : mobile/android/base/tests/testBookmarklets.java.in => mobile/android/base/tests/testBookmarklets.java rename : mobile/android/base/tests/testBookmarksPage.java.in => mobile/android/base/tests/testBookmarksPage.java rename : mobile/android/base/tests/testBrowserProvider.java.in => mobile/android/base/tests/testBrowserProvider.java rename : mobile/android/base/tests/testBrowserProviderPerf.java.in => mobile/android/base/tests/testBrowserProviderPerf.java rename : mobile/android/base/tests/testBrowserSearchVisibility.java.in => mobile/android/base/tests/testBrowserSearchVisibility.java rename : mobile/android/base/tests/testCheck.java.in => mobile/android/base/tests/testCheck.java rename : mobile/android/base/tests/testCheck2.java.in => mobile/android/base/tests/testCheck2.java rename : mobile/android/base/tests/testClearPrivateData.java.in => mobile/android/base/tests/testClearPrivateData.java rename : mobile/android/base/tests/testDeviceSearchEngine.java.in => mobile/android/base/tests/testDeviceSearchEngine.java rename : mobile/android/base/tests/testDistribution.java.in => mobile/android/base/tests/testDistribution.java rename : mobile/android/base/tests/testDoorHanger.java.in => mobile/android/base/tests/testDoorHanger.java rename : mobile/android/base/tests/testFindInPage.java.in => mobile/android/base/tests/testFindInPage.java rename : mobile/android/base/tests/testFlingCorrectness.java.in => mobile/android/base/tests/testFlingCorrectness.java rename : mobile/android/base/tests/testFormHistory.java.in => mobile/android/base/tests/testFormHistory.java rename : mobile/android/base/tests/testGetUserMedia.java.in => mobile/android/base/tests/testGetUserMedia.java rename : mobile/android/base/tests/testHistory.java.in => mobile/android/base/tests/testHistory.java rename : mobile/android/base/tests/testHomeBanner.java.in => mobile/android/base/tests/testHomeBanner.java rename : mobile/android/base/tests/testImportFromAndroid.java.in => mobile/android/base/tests/testImportFromAndroid.java rename : mobile/android/base/tests/testInputUrlBar.java.in => mobile/android/base/tests/testInputUrlBar.java rename : mobile/android/base/tests/testJNI.java.in => mobile/android/base/tests/testJNI.java rename : mobile/android/base/tests/testJarReader.java.in => mobile/android/base/tests/testJarReader.java rename : mobile/android/base/tests/testLinkContextMenu.java.in => mobile/android/base/tests/testLinkContextMenu.java rename : mobile/android/base/tests/testLoad.java.in => mobile/android/base/tests/testLoad.java rename : mobile/android/base/tests/testMailToContextMenu.java.in => mobile/android/base/tests/testMailToContextMenu.java rename : mobile/android/base/tests/testMasterPassword.java.in => mobile/android/base/tests/testMasterPassword.java rename : mobile/android/base/tests/testNewTab.java.in => mobile/android/base/tests/testNewTab.java rename : mobile/android/base/tests/testOrderedBroadcast.java.in => mobile/android/base/tests/testOrderedBroadcast.java rename : mobile/android/base/tests/testOverscroll.java.in => mobile/android/base/tests/testOverscroll.java rename : mobile/android/base/tests/testPan.java.in => mobile/android/base/tests/testPan.java rename : mobile/android/base/tests/testPanCorrectness.java.in => mobile/android/base/tests/testPanCorrectness.java rename : mobile/android/base/tests/testPasswordEncrypt.java.in => mobile/android/base/tests/testPasswordEncrypt.java rename : mobile/android/base/tests/testPasswordProvider.java.in => mobile/android/base/tests/testPasswordProvider.java rename : mobile/android/base/tests/testPermissions.java.in => mobile/android/base/tests/testPermissions.java rename : mobile/android/base/tests/testPictureLinkContextMenu.java.in => mobile/android/base/tests/testPictureLinkContextMenu.java rename : mobile/android/base/tests/testPrefsObserver.java.in => mobile/android/base/tests/testPrefsObserver.java rename : mobile/android/base/tests/testPrivateBrowsing.java.in => mobile/android/base/tests/testPrivateBrowsing.java rename : mobile/android/base/tests/testPromptGridInput.java.in => mobile/android/base/tests/testPromptGridInput.java rename : mobile/android/base/tests/testReaderMode.java.in => mobile/android/base/tests/testReaderMode.java rename : mobile/android/base/tests/testSearchSuggestions.java.in => mobile/android/base/tests/testSearchSuggestions.java rename : mobile/android/base/tests/testSettingsMenuItems.java.in => mobile/android/base/tests/testSettingsMenuItems.java rename : mobile/android/base/tests/testShareLink.java.in => mobile/android/base/tests/testShareLink.java rename : mobile/android/base/tests/testSharedPreferences.java.in => mobile/android/base/tests/testSharedPreferences.java rename : mobile/android/base/tests/testSystemPages.java.in => mobile/android/base/tests/testSystemPages.java rename : mobile/android/base/tests/testTabHistory.java.in => mobile/android/base/tests/testTabHistory.java rename : mobile/android/base/tests/testThumbnails.java.in => mobile/android/base/tests/testThumbnails.java rename : mobile/android/base/tests/testVkbOverlap.java.in => mobile/android/base/tests/testVkbOverlap.java rename : mobile/android/base/tests/test_bug720538.java.in => mobile/android/base/tests/test_bug720538.java extra : rebase_source : c8d715aa110b07c6010054a674879e143a5f04e7 extra : amend_source : fb5714b78e055e336b5e0c6a0da8eb8c931ff09e extra : histedit_source : ec57d8ba94000092438a2ba0bf4fb66091e98513%2C114ade72bb9b72b96557da74f1179734fa347e5c
2013-11-07 08:18:51 -08:00
import static org.mozilla.gecko.FennecNativeDriver.LogLevel;
public class FennecNativeActions implements Actions {
private static final String LOGTAG = "FennecNativeActions";
private Solo mSolo;
private Instrumentation mInstr;
private Assert mAsserter;
public FennecNativeActions(Activity activity, Solo robocop, Instrumentation instrumentation, Assert asserter) {
mSolo = robocop;
mInstr = instrumentation;
mAsserter = asserter;
GeckoLoader.loadSQLiteLibs(activity, activity.getApplication().getPackageResourcePath());
}
class GeckoEventExpecter implements RepeatedEventExpecter {
private static final int MAX_WAIT_MS = 90000;
private volatile boolean mIsRegistered;
private final String mGeckoEvent;
private final GeckoEventListener mListener;
private volatile boolean mEventEverReceived;
private String mEventData;
private BlockingQueue<String> mEventDataQueue;
GeckoEventExpecter(final String geckoEvent) {
if (TextUtils.isEmpty(geckoEvent)) {
throw new IllegalArgumentException("geckoEvent must not be empty");
}
mGeckoEvent = geckoEvent;
mEventDataQueue = new LinkedBlockingQueue<String>();
final GeckoEventExpecter expecter = this;
mListener = new GeckoEventListener() {
@Override
public void handleMessage(final String event, final JSONObject message) {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.DEBUG,
"handleMessage called for: " + event + "; expecting: " + mGeckoEvent);
mAsserter.is(event, mGeckoEvent, "Given message occurred for registered event");
expecter.notifyOfEvent(message);
}
};
GeckoAppShell.registerEventListener(mGeckoEvent, mListener);
mIsRegistered = true;
}
public void blockForEvent() {
blockForEvent(MAX_WAIT_MS, true);
}
private void blockForEvent(long millis, boolean failOnTimeout) {
if (!mIsRegistered) {
throw new IllegalStateException("listener not registered");
}
try {
mEventData = mEventDataQueue.poll(millis, TimeUnit.MILLISECONDS);
} catch (InterruptedException ie) {
FennecNativeDriver.log(LogLevel.ERROR, ie);
}
if (mEventData == null) {
if (failOnTimeout) {
FennecNativeDriver.logAllStackTraces(FennecNativeDriver.LogLevel.ERROR);
mAsserter.ok(false, "GeckoEventExpecter",
"blockForEvent timeout: "+mGeckoEvent);
} else {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.DEBUG,
"blockForEvent timeout: "+mGeckoEvent);
}
} else {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.DEBUG,
"unblocked on expecter for " + mGeckoEvent);
}
}
public void blockUntilClear(long millis) {
if (!mIsRegistered) {
throw new IllegalStateException("listener not registered");
}
if (millis <= 0) {
throw new IllegalArgumentException("millis must be > 0");
}
// wait for at least one event
try {
mEventData = mEventDataQueue.poll(MAX_WAIT_MS, TimeUnit.MILLISECONDS);
} catch (InterruptedException ie) {
FennecNativeDriver.log(LogLevel.ERROR, ie);
}
if (mEventData == null) {
FennecNativeDriver.logAllStackTraces(FennecNativeDriver.LogLevel.ERROR);
mAsserter.ok(false, "GeckoEventExpecter", "blockUntilClear timeout");
return;
}
// now wait for a period of millis where we don't get an event
while (true) {
try {
mEventData = mEventDataQueue.poll(millis, TimeUnit.MILLISECONDS);
} catch (InterruptedException ie) {
FennecNativeDriver.log(LogLevel.INFO, ie);
}
if (mEventData == null) {
// success
break;
}
}
FennecNativeDriver.log(FennecNativeDriver.LogLevel.DEBUG,
"unblocked on expecter for " + mGeckoEvent);
}
public String blockForEventData() {
blockForEvent();
return mEventData;
}
public String blockForEventDataWithTimeout(long millis) {
blockForEvent(millis, false);
return mEventData;
}
public void unregisterListener() {
if (!mIsRegistered) {
throw new IllegalStateException("listener not registered");
}
FennecNativeDriver.log(LogLevel.INFO,
"EventExpecter: no longer listening for " + mGeckoEvent);
GeckoAppShell.unregisterEventListener(mGeckoEvent, mListener);
mIsRegistered = false;
}
public boolean eventReceived() {
return mEventEverReceived;
}
void notifyOfEvent(final JSONObject message) {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.DEBUG,
"received event " + mGeckoEvent);
mEventEverReceived = true;
try {
mEventDataQueue.put(message.toString());
} catch (InterruptedException e) {
FennecNativeDriver.log(LogLevel.ERROR,
"EventExpecter dropped event: " + message.toString(), e);
}
}
}
public RepeatedEventExpecter expectGeckoEvent(final String geckoEvent) {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.DEBUG, "waiting for " + geckoEvent);
return new GeckoEventExpecter(geckoEvent);
}
public void sendGeckoEvent(final String geckoEvent, final String data) {
GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent(geckoEvent, data));
}
public void sendPreferencesGetEvent(int requestId, String[] prefNames) {
GeckoAppShell.sendEventToGecko(GeckoEvent.createPreferencesGetEvent(requestId, prefNames));
}
public void sendPreferencesObserveEvent(int requestId, String[] prefNames) {
GeckoAppShell.sendEventToGecko(GeckoEvent.createPreferencesObserveEvent(requestId, prefNames));
}
public void sendPreferencesRemoveObserversEvent(int requestId) {
GeckoAppShell.sendEventToGecko(GeckoEvent.createPreferencesRemoveObserversEvent(requestId));
}
class PaintExpecter implements RepeatedEventExpecter {
private static final int MAX_WAIT_MS = 90000;
private boolean mPaintDone;
private boolean mListening;
private final GeckoLayerClient mLayerClient;
PaintExpecter() {
final PaintExpecter expecter = this;
mLayerClient = GeckoAppShell.getLayerView().getLayerClient();
mLayerClient.setDrawListener(new DrawListener() {
@Override
public void drawFinished() {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.DEBUG,
"Received drawFinished notification");
expecter.notifyOfEvent();
}
});
mListening = true;
}
private synchronized void notifyOfEvent() {
mPaintDone = true;
this.notifyAll();
}
private synchronized void blockForEvent(long millis, boolean failOnTimeout) {
if (!mListening) {
throw new IllegalStateException("draw listener not registered");
}
long startTime = SystemClock.uptimeMillis();
long endTime = 0;
while (!mPaintDone) {
try {
this.wait(millis);
} catch (InterruptedException ie) {
FennecNativeDriver.log(LogLevel.ERROR, ie);
break;
}
endTime = SystemClock.uptimeMillis();
if (!mPaintDone && (endTime - startTime >= millis)) {
if (failOnTimeout) {
FennecNativeDriver.logAllStackTraces(FennecNativeDriver.LogLevel.ERROR);
mAsserter.ok(false, "PaintExpecter", "blockForEvent timeout");
}
return;
}
}
}
public synchronized void blockForEvent() {
blockForEvent(MAX_WAIT_MS, true);
}
public synchronized String blockForEventData() {
blockForEvent();
return null;
}
public synchronized String blockForEventDataWithTimeout(long millis) {
blockForEvent(millis, false);
return null;
}
public synchronized boolean eventReceived() {
return mPaintDone;
}
public synchronized void blockUntilClear(long millis) {
if (!mListening) {
throw new IllegalStateException("draw listener not registered");
}
if (millis <= 0) {
throw new IllegalArgumentException("millis must be > 0");
}
// wait for at least one event
long startTime = SystemClock.uptimeMillis();
long endTime = 0;
while (!mPaintDone) {
try {
this.wait(MAX_WAIT_MS);
} catch (InterruptedException ie) {
FennecNativeDriver.log(LogLevel.ERROR, ie);
break;
}
endTime = SystemClock.uptimeMillis();
if (!mPaintDone && (endTime - startTime >= MAX_WAIT_MS)) {
FennecNativeDriver.logAllStackTraces(FennecNativeDriver.LogLevel.ERROR);
mAsserter.ok(false, "PaintExpecter", "blockUtilClear timeout");
return;
}
}
// now wait for a period of millis where we don't get an event
startTime = SystemClock.uptimeMillis();
while (true) {
try {
this.wait(millis);
} catch (InterruptedException ie) {
FennecNativeDriver.log(LogLevel.ERROR, ie);
break;
}
endTime = SystemClock.uptimeMillis();
if (endTime - startTime >= millis) {
// success
break;
}
// we got a notify() before we could wait long enough, so we need to start over
// Note, moving the goal post might have us race against a "drawFinished" flood
startTime = endTime;
}
}
public synchronized void unregisterListener() {
if (!mListening) {
throw new IllegalStateException("listener not registered");
}
FennecNativeDriver.log(LogLevel.INFO,
"PaintExpecter: no longer listening for events");
mLayerClient.setDrawListener(null);
mListening = false;
}
}
public RepeatedEventExpecter expectPaint() {
return new PaintExpecter();
}
public void sendSpecialKey(SpecialKey button) {
switch(button) {
case DOWN:
sendKeyCode(KeyEvent.KEYCODE_DPAD_DOWN);
break;
case UP:
sendKeyCode(KeyEvent.KEYCODE_DPAD_UP);
break;
case LEFT:
sendKeyCode(KeyEvent.KEYCODE_DPAD_LEFT);
break;
case RIGHT:
sendKeyCode(KeyEvent.KEYCODE_DPAD_RIGHT);
break;
case ENTER:
sendKeyCode(KeyEvent.KEYCODE_ENTER);
break;
case MENU:
sendKeyCode(KeyEvent.KEYCODE_MENU);
break;
case BACK:
sendKeyCode(KeyEvent.KEYCODE_BACK);
break;
default:
mAsserter.ok(false, "sendSpecialKey", "Unknown SpecialKey " + button);
break;
}
}
public void sendKeyCode(int keyCode) {
if (keyCode <= 0 || keyCode > KeyEvent.getMaxKeyCode()) {
mAsserter.ok(false, "sendKeyCode", "Unknown keyCode " + keyCode);
}
mInstr.sendCharacterSync(keyCode);
}
@Override
public void sendKeys(String input) {
mInstr.sendStringSync(input);
}
public void drag(int startingX, int endingX, int startingY, int endingY) {
mSolo.drag(startingX, endingX, startingY, endingY, 10);
}
public Cursor querySql(final String dbPath, final String sql) {
return new SQLiteBridge(dbPath).rawQuery(sql, null);
}
}