This source file includes following definitions.
- executeJavaScriptAndWaitForResult
- executeJavaScriptAndWaitForResult
- executeJavaScriptAndWaitForResult
- executeJavaScript
package org.chromium.content.browser.test.util;
import junit.framework.Assert;
import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
import org.chromium.base.ThreadUtils;
import org.chromium.content.browser.ContentView;
import org.chromium.content.browser.ContentViewCore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class JavaScriptUtils {
private static final long EVALUATION_TIMEOUT_SECONDS = scaleTimeout(5);
public static String executeJavaScriptAndWaitForResult(
final ContentView view, TestCallbackHelperContainer viewClient,
final String code) throws InterruptedException, TimeoutException {
return executeJavaScriptAndWaitForResult(
view.getContentViewCore(),
viewClient.getOnEvaluateJavaScriptResultHelper(),
code);
}
public static String executeJavaScriptAndWaitForResult(
final ContentViewCore viewCore,
final TestCallbackHelperContainer.OnEvaluateJavaScriptResultHelper helper,
final String code) throws InterruptedException, TimeoutException {
return executeJavaScriptAndWaitForResult(
viewCore, helper, code, EVALUATION_TIMEOUT_SECONDS, TimeUnit.SECONDS);
}
public static String executeJavaScriptAndWaitForResult(
final ContentViewCore viewCore,
final TestCallbackHelperContainer.OnEvaluateJavaScriptResultHelper helper,
final String code,
final long timeout, final TimeUnit timeoutUnits)
throws InterruptedException, TimeoutException {
Assert.assertFalse("Executing JavaScript should be done from the test thread, "
+ " not the UI thread", ThreadUtils.runningOnUiThread());
ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
helper.evaluateJavaScript(viewCore, code);
}
});
helper.waitUntilHasValue(timeout, timeoutUnits);
Assert.assertTrue("Failed to retrieve JavaScript evaluation results.", helper.hasValue());
return helper.getJsonResultAndClear();
}
public static void executeJavaScript(final ContentView view, final String code) {
ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
view.evaluateJavaScript(code);
}
});
}
}