Skip to content

Commit

Permalink
Bug 1553515 - Make sure runtime has started before running tests. r=s…
Browse files Browse the repository at this point in the history
…norp

Differential Revision: https://phabricator.services.mozilla.com/D32584

--HG--
extra : moz-landing-system : lando
  • Loading branch information
agi committed Jul 8, 2019
1 parent f23e6ab commit 55537ad
Showing 1 changed file with 83 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

package org.mozilla.geckoview.test.rule;

import org.mozilla.gecko.GeckoThread;
import org.mozilla.gecko.util.ThreadUtils;
import org.mozilla.geckoview.ContentBlocking;
import org.mozilla.geckoview.GeckoDisplay;
import org.mozilla.geckoview.GeckoResult;
import org.mozilla.geckoview.GeckoResult.OnValueListener;
import org.mozilla.geckoview.GeckoRuntime;
import org.mozilla.geckoview.GeckoSession;
import org.mozilla.geckoview.GeckoSessionSettings;
Expand Down Expand Up @@ -37,15 +37,14 @@
import org.junit.runners.model.Statement;

import android.app.Instrumentation;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.SurfaceTexture;
import android.net.LocalSocketAddress;
import android.os.Looper;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.test.InstrumentationRegistry;
import android.util.Log;
import android.util.Pair;
import android.view.MotionEvent;
import android.view.Surface;
Expand All @@ -70,6 +69,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReference;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -1212,32 +1212,58 @@ protected void prepareSession(final GeckoSession session) {
* @param session Session to open.
*/
public void openSession(final GeckoSession session) {
session.open(getRuntime());
waitForOpenSession(session);
}

/* package */ void waitForOpenSession(final GeckoSession session) {
waitForInitialLoad(session);

if (mWithDevTools) {
if (sRDPConnection == null) {
final String packageName = InstrumentationRegistry.getTargetContext()
.getPackageName();
final LocalSocketAddress address = new LocalSocketAddress(
packageName + "/firefox-debugger-socket",
LocalSocketAddress.Namespace.ABSTRACT);
sRDPConnection = new RDPConnection(address);
sRDPConnection.setTimeout(mTimeoutMillis);
}
if (mRDPTabs == null) {
mRDPTabs = new HashMap<>();
}
final Tab tab = sRDPConnection.getMostRecentTab();
mRDPTabs.put(session, tab);
ThreadUtils.assertOnUiThread();
// We receive an initial about:blank load; don't expose that to the test. The initial
// load ends with the first onPageStop call, so ignore everything from the session
// until the first onPageStop call.

try {
// We cannot detect initial page load without progress delegate.
assertThat("ProgressDelegate cannot be null-delegate when opening session",
GeckoSession.ProgressDelegate.class, not(isIn(mNullDelegates)));
mCallRecordHandler = (method, args) -> {
Log.e(LOGTAG, "method: " + method);
final boolean matching = DEFAULT_DELEGATES.contains(
method.getDeclaringClass()) && session.equals(args[0]);
if (matching && sOnPageStop.equals(method)) {
mCallRecordHandler = null;
}
return matching;
};

session.open(getRuntime());

UiThreadUtils.waitForCondition(() -> mCallRecordHandler == null,
env.getDefaultTimeoutMillis());
} finally {
mCallRecordHandler = null;
}

attachDevTools(session);
}

private void waitForInitialLoad(final GeckoSession session) {
private void attachDevTools(final GeckoSession session) {
if (!mWithDevTools) {
return;
}

if (sRDPConnection == null) {
final String packageName = InstrumentationRegistry.getTargetContext()
.getPackageName();
final LocalSocketAddress address = new LocalSocketAddress(
packageName + "/firefox-debugger-socket",
LocalSocketAddress.Namespace.ABSTRACT);
sRDPConnection = new RDPConnection(address);
sRDPConnection.setTimeout(mTimeoutMillis);
}
if (mRDPTabs == null) {
mRDPTabs = new HashMap<>();
}
final Tab tab = sRDPConnection.getMostRecentTab();
mRDPTabs.put(session, tab);
}

private void waitForOpenSession(final GeckoSession session) {
ThreadUtils.assertOnUiThread();
// We receive an initial about:blank load; don't expose that to the test. The initial
// load ends with the first onPageStop call, so ignore everything from the session
Expand All @@ -1247,23 +1273,23 @@ private void waitForInitialLoad(final GeckoSession session) {
// We cannot detect initial page load without progress delegate.
assertThat("ProgressDelegate cannot be null-delegate when opening session",
GeckoSession.ProgressDelegate.class, not(isIn(mNullDelegates)));
mCallRecordHandler = new CallRecordHandler() {
@Override
public boolean handleCall(final Method method, final Object[] args) {
final boolean matching = DEFAULT_DELEGATES.contains(
method.getDeclaringClass()) && session.equals(args[0]);
if (matching && sOnPageStop.equals(method)) {
mCallRecordHandler = null;
}
return matching;
mCallRecordHandler = (method, args) -> {
Log.e(LOGTAG, "method: " + method);
final boolean matching = DEFAULT_DELEGATES.contains(
method.getDeclaringClass()) && session.equals(args[0]);
if (matching && sOnPageStop.equals(method)) {
mCallRecordHandler = null;
}
return matching;
};

UiThreadUtils.waitForCondition(() -> mCallRecordHandler == null,
env.getDefaultTimeoutMillis());
} finally {
mCallRecordHandler = null;
}

attachDevTools(session);
}

/**
Expand Down Expand Up @@ -1347,10 +1373,32 @@ public void evaluate() throws Throwable {

mInstrumentation.runOnMainSync(() -> {
try {
getRuntime();

long timeout = env.getDefaultTimeoutMillis() + System.currentTimeMillis();
while (!GeckoThread.isStateAtLeast(GeckoThread.State.PROFILE_READY)) {
if (System.currentTimeMillis() > timeout) {
throw new TimeoutException("Could not startup runtime after "
+ env.getDefaultTimeoutMillis() + ".ms");
}
Log.e(LOGTAG, "GeckoThread not ready, sleeping 1000ms.");
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
}
}

Log.e(LOGTAG, "====");
Log.e(LOGTAG, "before prepareStatement " + description);
prepareStatement(description);
Log.e(LOGTAG, "after prepareStatement");
base.evaluate();
Log.e(LOGTAG, "after evaluate");
performTestEndCheck();
Log.e(LOGTAG, "after performTestEndCheck");
Log.e(LOGTAG, "====");
} catch (Throwable t) {
Log.e(LOGTAG, "====", t);
exceptionRef.set(t);
} finally {
try {
Expand Down

0 comments on commit 55537ad

Please sign in to comment.