Commit 86006905 authored by aruslan@chromium.org's avatar aruslan@chromium.org

Add waitUntilHasValue() to simplify usage.

BUG=161941


Review URL: https://chromiumcodereview.appspot.com/11280250

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@170927 0039d316-1c4b-4281-b951-d872f2087c98
parent 348c18bc
...@@ -61,7 +61,6 @@ public class JSUtils { ...@@ -61,7 +61,6 @@ public class JSUtils {
final AwContents awContents, final AwContents awContents,
final OnEvaluateJavaScriptResultHelper onEvaluateJavaScriptResultHelper, final OnEvaluateJavaScriptResultHelper onEvaluateJavaScriptResultHelper,
final String code) throws Throwable { final String code) throws Throwable {
int currentCallCount = onEvaluateJavaScriptResultHelper.getCallCount();
testCase.getInstrumentation().runOnMainSync(new Runnable() { testCase.getInstrumentation().runOnMainSync(new Runnable() {
@Override @Override
public void run() { public void run() {
...@@ -69,7 +68,7 @@ public class JSUtils { ...@@ -69,7 +68,7 @@ public class JSUtils {
awContents.getContentViewCore(), code); awContents.getContentViewCore(), code);
} }
}); });
onEvaluateJavaScriptResultHelper.waitForCallback(currentCallCount); onEvaluateJavaScriptResultHelper.waitUntilHasValue();
Assert.assertTrue("Failed to retrieve JavaScript evaluation results.", Assert.assertTrue("Failed to retrieve JavaScript evaluation results.",
onEvaluateJavaScriptResultHelper.hasValue()); onEvaluateJavaScriptResultHelper.hasValue());
return onEvaluateJavaScriptResultHelper.getJsonResultAndClear(); return onEvaluateJavaScriptResultHelper.getJsonResultAndClear();
......
...@@ -83,6 +83,31 @@ public class CallbackHelper { ...@@ -83,6 +83,31 @@ public class CallbackHelper {
waitForCallback(currentCallCount, 1); waitForCallback(currentCallCount, 1);
} }
/**
* Blocks until the criteria is satisfied or throws an exception
* if the specified time frame is exceeded.
* @param timeout timeout value.
* @param unit timeout unit.
*/
public void waitUntilCriteria(Criteria criteria, long timeout, TimeUnit unit)
throws InterruptedException, TimeoutException {
synchronized(mLock) {
final long startTime = System.currentTimeMillis();
boolean isSatisfied = criteria.isSatisfied();
while (!isSatisfied &&
System.currentTimeMillis() - startTime < unit.toMillis(timeout)) {
mLock.wait(unit.toMillis(timeout));
isSatisfied = criteria.isSatisfied();
}
if (!isSatisfied) throw new TimeoutException("waitUntilCriteria timed out!");
}
}
public void waitUntilCriteria(Criteria criteria)
throws InterruptedException, TimeoutException {
waitUntilCriteria(criteria, WAIT_TIMEOUT_SECONDS, TimeUnit.SECONDS);
}
/** /**
* Should be called when the callback associated with this helper object is called. * Should be called when the callback associated with this helper object is called.
*/ */
......
...@@ -121,8 +121,8 @@ public class DOMUtils { ...@@ -121,8 +121,8 @@ public class DOMUtils {
sb.append(" return [ node.textContent ];"); sb.append(" return [ node.textContent ];");
sb.append("})();"); sb.append("})();");
String jsonText = JavaScriptUtils.executeJavaScriptAndWaitForResult(view, viewClient, String jsonText = JavaScriptUtils.executeJavaScriptAndWaitForResult(
sb.toString()); view, viewClient, sb.toString());
Assert.assertFalse("Failed to retrieve contents for " + nodeId, Assert.assertFalse("Failed to retrieve contents for " + nodeId,
jsonText.trim().equalsIgnoreCase("null")); jsonText.trim().equalsIgnoreCase("null"));
......
...@@ -56,14 +56,13 @@ public class JavaScriptUtils { ...@@ -56,14 +56,13 @@ public class JavaScriptUtils {
final String code, final String code,
final long timeout, final TimeUnit timeoutUnits) final long timeout, final TimeUnit timeoutUnits)
throws InterruptedException, TimeoutException { throws InterruptedException, TimeoutException {
int currentCallCount = helper.getCallCount();
ThreadUtils.runOnUiThread(new Runnable() { ThreadUtils.runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
helper.evaluateJavaScript(viewCore, code); helper.evaluateJavaScript(viewCore, code);
} }
}); });
helper.waitForCallback(currentCallCount, 1, timeout, timeoutUnits); helper.waitUntilHasValue(timeout, timeoutUnits);
Assert.assertTrue("Failed to retrieve JavaScript evaluation results.", helper.hasValue()); Assert.assertTrue("Failed to retrieve JavaScript evaluation results.", helper.hasValue());
return helper.getJsonResultAndClear(); return helper.getJsonResultAndClear();
} }
......
...@@ -10,6 +10,9 @@ import android.util.Log; ...@@ -10,6 +10,9 @@ import android.util.Log;
import org.chromium.content.browser.ContentView; import org.chromium.content.browser.ContentView;
import org.chromium.content.browser.ContentViewCore; import org.chromium.content.browser.ContentViewCore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/** /**
* This class is used to provide callback hooks for tests and related classes. * This class is used to provide callback hooks for tests and related classes.
*/ */
...@@ -110,11 +113,37 @@ public class TestCallbackHelperContainer { ...@@ -110,11 +113,37 @@ public class TestCallbackHelperContainer {
return result; return result;
} }
/**
* Returns a criteria that checks that the evaluation has finished.
*/
public Criteria getHasValueCriteria() {
return new Criteria() {
@Override
public boolean isSatisfied() {
return hasValue();
}
};
}
/**
* Waits till the JavaScript evaluation finishes.
*/
public void waitUntilHasValue(long timeout, TimeUnit timeoutUnits)
throws InterruptedException, TimeoutException {
waitUntilCriteria(getHasValueCriteria(), timeout, timeoutUnits);
}
public void waitUntilHasValue() throws InterruptedException, TimeoutException {
waitUntilCriteria(getHasValueCriteria());
}
private void setRequestId(Integer requestId) { private void setRequestId(Integer requestId) {
mRequestId = requestId; mRequestId = requestId;
mId = null; mId = null;
mJsonResult = null; mJsonResult = null;
} }
public void notifyCalled(int id, String jsonResult) { public void notifyCalled(int id, String jsonResult) {
if (mRequestId == null) { if (mRequestId == null) {
Log.w("TestCallbackHelperContainer", Log.w("TestCallbackHelperContainer",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment