Bug 855146 - Part 2: Extract runOnUiThreadSync() test method. r=gbrown

--HG--
extra : rebase_source : 8014e5438596d8981e074ad847a675bacb18eb2c
This commit is contained in:
Chris Peterson 2013-03-21 18:43:04 -07:00
parent 0030db5412
commit aeecff5b62
4 changed files with 51 additions and 52 deletions

View File

@ -40,9 +40,8 @@ public class FennecNativeElement implements Element {
private boolean mClickSuccess;
public boolean click() {
final SynchronousQueue syncQueue = new SynchronousQueue();
mClickSuccess = false;
mActivity.runOnUiThread(
RobocopUtils.runOnUiThreadSync(mActivity,
new Runnable() {
public void run() {
View view = (View)mActivity.findViewById(mId);
@ -57,32 +56,16 @@ public class FennecNativeElement implements Element {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR,
"click: unable to find view "+mId);
}
try {
syncQueue.put(new Object());
} catch (InterruptedException e) {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e);
}
}
});
try {
// Wait for the UiThread code to finish running
if (syncQueue.poll(MAX_WAIT_MS, TimeUnit.MILLISECONDS) == null) {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR,
"click: time-out waiting for UI thread");
FennecNativeDriver.logAllStackTraces(FennecNativeDriver.LogLevel.ERROR);
}
} catch (InterruptedException e) {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e);
}
return mClickSuccess;
}
private Object mText;
public String getText() {
final SynchronousQueue syncQueue = new SynchronousQueue();
mText = null;
mActivity.runOnUiThread(
RobocopUtils.runOnUiThreadSync(mActivity,
new Runnable() {
public void run() {
View v = mActivity.findViewById(mId);
@ -109,24 +92,9 @@ public class FennecNativeElement implements Element {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR,
"getText: unhandled type for view "+mId);
}
try {
syncQueue.put(new Object());
} catch (InterruptedException e) {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e);
}
} // end of run() method definition
} // end of anonymous Runnable object instantiation
);
try {
// Wait for the UiThread code to finish running
if (syncQueue.poll(MAX_WAIT_MS, TimeUnit.MILLISECONDS) == null) {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR,
"getText: time-out waiting for UI thread");
FennecNativeDriver.logAllStackTraces(FennecNativeDriver.LogLevel.ERROR);
}
} catch (InterruptedException e) {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e);
}
if (mText == null) {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.WARN,
"getText: Text is null for view "+mId);
@ -138,33 +106,16 @@ public class FennecNativeElement implements Element {
private boolean mDisplayed;
public boolean isDisplayed() {
final SynchronousQueue syncQueue = new SynchronousQueue();
mDisplayed = false;
mActivity.runOnUiThread(
RobocopUtils.runOnUiThreadSync(mActivity,
new Runnable() {
public void run() {
View view = (View)mActivity.findViewById(mId);
if (view != null) {
mDisplayed = true;
}
try {
syncQueue.put(new Object());
} catch (InterruptedException e) {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e);
}
}
});
try {
// Wait for the UiThread code to finish running
if (syncQueue.poll(MAX_WAIT_MS, TimeUnit.MILLISECONDS) == null) {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR,
"isDisplayed: time-out waiting for UI thread");
FennecNativeDriver.logAllStackTraces(FennecNativeDriver.LogLevel.ERROR);
}
} catch (InterruptedException e) {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e);
}
return mDisplayed;
}
}

View File

@ -35,6 +35,7 @@ _JAVA_HARNESS = \
FennecNativeDriver.java \
FennecNativeElement.java \
RoboCopException.java \
RobocopUtils.java \
PaintedSurface.java \
$(NULL)

View File

@ -0,0 +1,43 @@
#filter substitution
/* 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 @ANDROID_PACKAGE_NAME@;
import android.app.Activity;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;
public final class RobocopUtils {
private static final int MAX_WAIT_MS = 3000;
private RobocopUtils() {}
public static void runOnUiThreadSync(Activity activity, final Runnable runnable) {
final SynchronousQueue syncQueue = new SynchronousQueue();
activity.runOnUiThread(
new Runnable() {
public void run() {
runnable.run();
try {
syncQueue.put(new Object());
} catch (InterruptedException e) {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e);
}
}
});
try {
// Wait for the UiThread code to finish running
if (syncQueue.poll(MAX_WAIT_MS, TimeUnit.MILLISECONDS) == null) {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR,
"time-out waiting for UI thread");
FennecNativeDriver.logAllStackTraces(FennecNativeDriver.LogLevel.ERROR);
}
} catch (InterruptedException e) {
FennecNativeDriver.log(FennecNativeDriver.LogLevel.ERROR, e);
}
}
}

4
mobile/android/base/tests/BaseTest.java.in Executable file → Normal file
View File

@ -566,6 +566,10 @@ abstract class BaseTest extends ActivityInstrumentationTestCase2<Activity> {
hitEnterAndWait();
}
public final void runOnUiThreadSync(Runnable runnable) {
RobocopUtils.runOnUiThreadSync(mActivity, runnable);
}
/**
* This method will edit the bookmark with index = bookmarkIndex from the list of bookmarks
* For the field index: