michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko; michael@0: michael@0: import java.util.concurrent.BlockingQueue; michael@0: import java.util.concurrent.LinkedBlockingQueue; michael@0: import java.util.concurrent.TimeUnit; michael@0: michael@0: import org.json.JSONObject; michael@0: import org.mozilla.gecko.FennecNativeDriver.LogLevel; michael@0: import org.mozilla.gecko.gfx.LayerView; michael@0: import org.mozilla.gecko.gfx.LayerView.DrawListener; michael@0: import org.mozilla.gecko.mozglue.GeckoLoader; michael@0: import org.mozilla.gecko.sqlite.SQLiteBridge; michael@0: import org.mozilla.gecko.util.GeckoEventListener; michael@0: michael@0: import android.app.Activity; michael@0: import android.app.Instrumentation; michael@0: import android.database.Cursor; michael@0: import android.os.SystemClock; michael@0: import android.text.TextUtils; michael@0: import android.view.KeyEvent; michael@0: michael@0: import com.jayway.android.robotium.solo.Solo; michael@0: michael@0: public class FennecNativeActions implements Actions { michael@0: private static final String LOGTAG = "FennecNativeActions"; michael@0: michael@0: private Solo mSolo; michael@0: private Instrumentation mInstr; michael@0: private Assert mAsserter; michael@0: michael@0: public FennecNativeActions(Activity activity, Solo robocop, Instrumentation instrumentation, Assert asserter) { michael@0: mSolo = robocop; michael@0: mInstr = instrumentation; michael@0: mAsserter = asserter; michael@0: michael@0: GeckoLoader.loadSQLiteLibs(activity, activity.getApplication().getPackageResourcePath()); michael@0: } michael@0: michael@0: class GeckoEventExpecter implements RepeatedEventExpecter { michael@0: private static final int MAX_WAIT_MS = 90000; michael@0: michael@0: private volatile boolean mIsRegistered; michael@0: michael@0: private final String mGeckoEvent; michael@0: private final GeckoEventListener mListener; michael@0: michael@0: private volatile boolean mEventEverReceived; michael@0: private String mEventData; michael@0: private BlockingQueue mEventDataQueue; michael@0: michael@0: GeckoEventExpecter(final String geckoEvent) { michael@0: if (TextUtils.isEmpty(geckoEvent)) { michael@0: throw new IllegalArgumentException("geckoEvent must not be empty"); michael@0: } michael@0: michael@0: mGeckoEvent = geckoEvent; michael@0: mEventDataQueue = new LinkedBlockingQueue(); michael@0: michael@0: final GeckoEventExpecter expecter = this; michael@0: mListener = new GeckoEventListener() { michael@0: @Override michael@0: public void handleMessage(final String event, final JSONObject message) { michael@0: FennecNativeDriver.log(FennecNativeDriver.LogLevel.DEBUG, michael@0: "handleMessage called for: " + event + "; expecting: " + mGeckoEvent); michael@0: mAsserter.is(event, mGeckoEvent, "Given message occurred for registered event: " + message); michael@0: michael@0: expecter.notifyOfEvent(message); michael@0: } michael@0: }; michael@0: michael@0: GeckoAppShell.registerEventListener(mGeckoEvent, mListener); michael@0: mIsRegistered = true; michael@0: } michael@0: michael@0: public void blockForEvent() { michael@0: blockForEvent(MAX_WAIT_MS, true); michael@0: } michael@0: michael@0: public void blockForEvent(long millis, boolean failOnTimeout) { michael@0: if (!mIsRegistered) { michael@0: throw new IllegalStateException("listener not registered"); michael@0: } michael@0: michael@0: try { michael@0: mEventData = mEventDataQueue.poll(millis, TimeUnit.MILLISECONDS); michael@0: } catch (InterruptedException ie) { michael@0: FennecNativeDriver.log(LogLevel.ERROR, ie); michael@0: } michael@0: if (mEventData == null) { michael@0: if (failOnTimeout) { michael@0: FennecNativeDriver.logAllStackTraces(FennecNativeDriver.LogLevel.ERROR); michael@0: mAsserter.ok(false, "GeckoEventExpecter", michael@0: "blockForEvent timeout: "+mGeckoEvent); michael@0: } else { michael@0: FennecNativeDriver.log(FennecNativeDriver.LogLevel.DEBUG, michael@0: "blockForEvent timeout: "+mGeckoEvent); michael@0: } michael@0: } else { michael@0: FennecNativeDriver.log(FennecNativeDriver.LogLevel.DEBUG, michael@0: "unblocked on expecter for " + mGeckoEvent); michael@0: } michael@0: } michael@0: michael@0: public void blockUntilClear(long millis) { michael@0: if (!mIsRegistered) { michael@0: throw new IllegalStateException("listener not registered"); michael@0: } michael@0: if (millis <= 0) { michael@0: throw new IllegalArgumentException("millis must be > 0"); michael@0: } michael@0: michael@0: // wait for at least one event michael@0: try { michael@0: mEventData = mEventDataQueue.poll(MAX_WAIT_MS, TimeUnit.MILLISECONDS); michael@0: } catch (InterruptedException ie) { michael@0: FennecNativeDriver.log(LogLevel.ERROR, ie); michael@0: } michael@0: if (mEventData == null) { michael@0: FennecNativeDriver.logAllStackTraces(FennecNativeDriver.LogLevel.ERROR); michael@0: mAsserter.ok(false, "GeckoEventExpecter", "blockUntilClear timeout"); michael@0: return; michael@0: } michael@0: // now wait for a period of millis where we don't get an event michael@0: while (true) { michael@0: try { michael@0: mEventData = mEventDataQueue.poll(millis, TimeUnit.MILLISECONDS); michael@0: } catch (InterruptedException ie) { michael@0: FennecNativeDriver.log(LogLevel.INFO, ie); michael@0: } michael@0: if (mEventData == null) { michael@0: // success michael@0: break; michael@0: } michael@0: } michael@0: FennecNativeDriver.log(FennecNativeDriver.LogLevel.DEBUG, michael@0: "unblocked on expecter for " + mGeckoEvent); michael@0: } michael@0: michael@0: public String blockForEventData() { michael@0: blockForEvent(); michael@0: return mEventData; michael@0: } michael@0: michael@0: public String blockForEventDataWithTimeout(long millis) { michael@0: blockForEvent(millis, false); michael@0: return mEventData; michael@0: } michael@0: michael@0: public void unregisterListener() { michael@0: if (!mIsRegistered) { michael@0: throw new IllegalStateException("listener not registered"); michael@0: } michael@0: michael@0: FennecNativeDriver.log(LogLevel.INFO, michael@0: "EventExpecter: no longer listening for " + mGeckoEvent); michael@0: michael@0: GeckoAppShell.unregisterEventListener(mGeckoEvent, mListener); michael@0: mIsRegistered = false; michael@0: } michael@0: michael@0: public boolean eventReceived() { michael@0: return mEventEverReceived; michael@0: } michael@0: michael@0: void notifyOfEvent(final JSONObject message) { michael@0: FennecNativeDriver.log(FennecNativeDriver.LogLevel.DEBUG, michael@0: "received event " + mGeckoEvent); michael@0: michael@0: mEventEverReceived = true; michael@0: michael@0: try { michael@0: mEventDataQueue.put(message.toString()); michael@0: } catch (InterruptedException e) { michael@0: FennecNativeDriver.log(LogLevel.ERROR, michael@0: "EventExpecter dropped event: " + message.toString(), e); michael@0: } michael@0: } michael@0: } michael@0: michael@0: public RepeatedEventExpecter expectGeckoEvent(final String geckoEvent) { michael@0: FennecNativeDriver.log(FennecNativeDriver.LogLevel.DEBUG, "waiting for " + geckoEvent); michael@0: return new GeckoEventExpecter(geckoEvent); michael@0: } michael@0: michael@0: public void sendGeckoEvent(final String geckoEvent, final String data) { michael@0: GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent(geckoEvent, data)); michael@0: } michael@0: michael@0: public void sendPreferencesGetEvent(int requestId, String[] prefNames) { michael@0: GeckoAppShell.sendEventToGecko(GeckoEvent.createPreferencesGetEvent(requestId, prefNames)); michael@0: } michael@0: michael@0: public void sendPreferencesObserveEvent(int requestId, String[] prefNames) { michael@0: GeckoAppShell.sendEventToGecko(GeckoEvent.createPreferencesObserveEvent(requestId, prefNames)); michael@0: } michael@0: michael@0: public void sendPreferencesRemoveObserversEvent(int requestId) { michael@0: GeckoAppShell.sendEventToGecko(GeckoEvent.createPreferencesRemoveObserversEvent(requestId)); michael@0: } michael@0: michael@0: class PaintExpecter implements RepeatedEventExpecter { michael@0: private static final int MAX_WAIT_MS = 90000; michael@0: michael@0: private boolean mPaintDone; michael@0: private boolean mListening; michael@0: michael@0: private final LayerView mLayerView; michael@0: private final DrawListener mDrawListener; michael@0: michael@0: PaintExpecter() { michael@0: final PaintExpecter expecter = this; michael@0: mLayerView = GeckoAppShell.getLayerView(); michael@0: mDrawListener = new DrawListener() { michael@0: @Override michael@0: public void drawFinished() { michael@0: FennecNativeDriver.log(FennecNativeDriver.LogLevel.DEBUG, michael@0: "Received drawFinished notification"); michael@0: expecter.notifyOfEvent(); michael@0: } michael@0: }; michael@0: mLayerView.addDrawListener(mDrawListener); michael@0: mListening = true; michael@0: } michael@0: michael@0: private synchronized void notifyOfEvent() { michael@0: mPaintDone = true; michael@0: this.notifyAll(); michael@0: } michael@0: michael@0: public synchronized void blockForEvent(long millis, boolean failOnTimeout) { michael@0: if (!mListening) { michael@0: throw new IllegalStateException("draw listener not registered"); michael@0: } michael@0: long startTime = SystemClock.uptimeMillis(); michael@0: long endTime = 0; michael@0: while (!mPaintDone) { michael@0: try { michael@0: this.wait(millis); michael@0: } catch (InterruptedException ie) { michael@0: FennecNativeDriver.log(LogLevel.ERROR, ie); michael@0: break; michael@0: } michael@0: endTime = SystemClock.uptimeMillis(); michael@0: if (!mPaintDone && (endTime - startTime >= millis)) { michael@0: if (failOnTimeout) { michael@0: FennecNativeDriver.logAllStackTraces(FennecNativeDriver.LogLevel.ERROR); michael@0: mAsserter.ok(false, "PaintExpecter", "blockForEvent timeout"); michael@0: } michael@0: return; michael@0: } michael@0: } michael@0: } michael@0: michael@0: public synchronized void blockForEvent() { michael@0: blockForEvent(MAX_WAIT_MS, true); michael@0: } michael@0: michael@0: public synchronized String blockForEventData() { michael@0: blockForEvent(); michael@0: return null; michael@0: } michael@0: michael@0: public synchronized String blockForEventDataWithTimeout(long millis) { michael@0: blockForEvent(millis, false); michael@0: return null; michael@0: } michael@0: michael@0: public synchronized boolean eventReceived() { michael@0: return mPaintDone; michael@0: } michael@0: michael@0: public synchronized void blockUntilClear(long millis) { michael@0: if (!mListening) { michael@0: throw new IllegalStateException("draw listener not registered"); michael@0: } michael@0: if (millis <= 0) { michael@0: throw new IllegalArgumentException("millis must be > 0"); michael@0: } michael@0: // wait for at least one event michael@0: long startTime = SystemClock.uptimeMillis(); michael@0: long endTime = 0; michael@0: while (!mPaintDone) { michael@0: try { michael@0: this.wait(MAX_WAIT_MS); michael@0: } catch (InterruptedException ie) { michael@0: FennecNativeDriver.log(LogLevel.ERROR, ie); michael@0: break; michael@0: } michael@0: endTime = SystemClock.uptimeMillis(); michael@0: if (!mPaintDone && (endTime - startTime >= MAX_WAIT_MS)) { michael@0: FennecNativeDriver.logAllStackTraces(FennecNativeDriver.LogLevel.ERROR); michael@0: mAsserter.ok(false, "PaintExpecter", "blockUtilClear timeout"); michael@0: return; michael@0: } michael@0: } michael@0: // now wait for a period of millis where we don't get an event michael@0: startTime = SystemClock.uptimeMillis(); michael@0: while (true) { michael@0: try { michael@0: this.wait(millis); michael@0: } catch (InterruptedException ie) { michael@0: FennecNativeDriver.log(LogLevel.ERROR, ie); michael@0: break; michael@0: } michael@0: endTime = SystemClock.uptimeMillis(); michael@0: if (endTime - startTime >= millis) { michael@0: // success michael@0: break; michael@0: } michael@0: michael@0: // we got a notify() before we could wait long enough, so we need to start over michael@0: // Note, moving the goal post might have us race against a "drawFinished" flood michael@0: startTime = endTime; michael@0: } michael@0: } michael@0: michael@0: public synchronized void unregisterListener() { michael@0: if (!mListening) { michael@0: throw new IllegalStateException("listener not registered"); michael@0: } michael@0: michael@0: FennecNativeDriver.log(LogLevel.INFO, michael@0: "PaintExpecter: no longer listening for events"); michael@0: mLayerView.removeDrawListener(mDrawListener); michael@0: mListening = false; michael@0: } michael@0: } michael@0: michael@0: public RepeatedEventExpecter expectPaint() { michael@0: return new PaintExpecter(); michael@0: } michael@0: michael@0: public void sendSpecialKey(SpecialKey button) { michael@0: switch(button) { michael@0: case DOWN: michael@0: sendKeyCode(KeyEvent.KEYCODE_DPAD_DOWN); michael@0: break; michael@0: case UP: michael@0: sendKeyCode(KeyEvent.KEYCODE_DPAD_UP); michael@0: break; michael@0: case LEFT: michael@0: sendKeyCode(KeyEvent.KEYCODE_DPAD_LEFT); michael@0: break; michael@0: case RIGHT: michael@0: sendKeyCode(KeyEvent.KEYCODE_DPAD_RIGHT); michael@0: break; michael@0: case ENTER: michael@0: sendKeyCode(KeyEvent.KEYCODE_ENTER); michael@0: break; michael@0: case MENU: michael@0: sendKeyCode(KeyEvent.KEYCODE_MENU); michael@0: break; michael@0: case BACK: michael@0: sendKeyCode(KeyEvent.KEYCODE_BACK); michael@0: break; michael@0: default: michael@0: mAsserter.ok(false, "sendSpecialKey", "Unknown SpecialKey " + button); michael@0: break; michael@0: } michael@0: } michael@0: michael@0: public void sendKeyCode(int keyCode) { michael@0: if (keyCode <= 0 || keyCode > KeyEvent.getMaxKeyCode()) { michael@0: mAsserter.ok(false, "sendKeyCode", "Unknown keyCode " + keyCode); michael@0: } michael@0: mInstr.sendCharacterSync(keyCode); michael@0: } michael@0: michael@0: @Override michael@0: public void sendKeys(String input) { michael@0: mInstr.sendStringSync(input); michael@0: } michael@0: michael@0: public void drag(int startingX, int endingX, int startingY, int endingY) { michael@0: mSolo.drag(startingX, endingX, startingY, endingY, 10); michael@0: } michael@0: michael@0: public Cursor querySql(final String dbPath, final String sql) { michael@0: return new SQLiteBridge(dbPath).rawQuery(sql, null); michael@0: } michael@0: }