This source file includes following definitions.
- getNodeBounds
- focusNode
- clickNode
- longPressNode
- scrollNodeIntoView
- getNodeContents
- getNodeValue
- getNodeField
- waitForNonZeroNodeBounds
- getClickTargetForNode
package org.chromium.content.browser.test.util;
import android.graphics.Rect;
import android.test.ActivityInstrumentationTestCase2;
import android.util.JsonReader;
import junit.framework.Assert;
import org.chromium.content.browser.ContentView;
import java.io.IOException;
import java.io.StringReader;
import java.util.concurrent.TimeoutException;
public class DOMUtils {
public static Rect getNodeBounds(
final ContentView view, TestCallbackHelperContainer viewClient, String nodeId)
throws InterruptedException, TimeoutException {
StringBuilder sb = new StringBuilder();
sb.append("(function() {");
sb.append(" var node = document.getElementById('" + nodeId + "');");
sb.append(" if (!node) return null;");
sb.append(" var width = node.offsetWidth;");
sb.append(" var height = node.offsetHeight;");
sb.append(" var x = -window.scrollX;");
sb.append(" var y = -window.scrollY;");
sb.append(" do {");
sb.append(" x += node.offsetLeft;");
sb.append(" y += node.offsetTop;");
sb.append(" } while (node = node.offsetParent);");
sb.append(" return [ x, y, width, height ];");
sb.append("})();");
String jsonText = JavaScriptUtils.executeJavaScriptAndWaitForResult(
view, viewClient, sb.toString());
Assert.assertFalse("Failed to retrieve bounds for " + nodeId,
jsonText.trim().equalsIgnoreCase("null"));
JsonReader jsonReader = new JsonReader(new StringReader(jsonText));
int[] bounds = new int[4];
try {
jsonReader.beginArray();
int i = 0;
while (jsonReader.hasNext()) {
bounds[i++] = jsonReader.nextInt();
}
jsonReader.endArray();
Assert.assertEquals("Invalid bounds returned.", 4, i);
jsonReader.close();
} catch (IOException exception) {
Assert.fail("Failed to evaluate JavaScript: " + jsonText + "\n" + exception);
}
return new Rect(bounds[0], bounds[1], bounds[0] + bounds[2], bounds[1] + bounds[3]);
}
public static void focusNode(ActivityInstrumentationTestCase2 activityTestCase,
final ContentView view, TestCallbackHelperContainer viewClient, String nodeId)
throws InterruptedException, TimeoutException {
StringBuilder sb = new StringBuilder();
sb.append("(function() {");
sb.append(" var node = document.getElementById('" + nodeId + "');");
sb.append(" if (node) node.focus();");
sb.append("})();");
JavaScriptUtils.executeJavaScriptAndWaitForResult(view, viewClient, sb.toString());
}
public static void clickNode(ActivityInstrumentationTestCase2 activityTestCase,
final ContentView view, TestCallbackHelperContainer viewClient, String nodeId)
throws InterruptedException, TimeoutException {
int[] clickTarget = getClickTargetForNode(view, viewClient, nodeId);
TouchCommon touchCommon = new TouchCommon(activityTestCase);
touchCommon.singleClickView(view, clickTarget[0], clickTarget[1]);
}
public static void longPressNode(ActivityInstrumentationTestCase2 activityTestCase,
final ContentView view, TestCallbackHelperContainer viewClient, String nodeId)
throws InterruptedException, TimeoutException {
int[] clickTarget = getClickTargetForNode(view, viewClient, nodeId);
TouchCommon touchCommon = new TouchCommon(activityTestCase);
touchCommon.longPressView(view, clickTarget[0], clickTarget[1]);
}
public static void scrollNodeIntoView(final ContentView view,
TestCallbackHelperContainer viewClient, String nodeId)
throws InterruptedException, TimeoutException {
JavaScriptUtils.executeJavaScriptAndWaitForResult(view, viewClient,
"document.getElementById('" + nodeId + "').scrollIntoView()");
}
public static String getNodeContents(final ContentView view,
TestCallbackHelperContainer viewClient, String nodeId)
throws InterruptedException, TimeoutException {
return getNodeField("textContent", view, viewClient, nodeId);
}
public static String getNodeValue(final ContentView view,
TestCallbackHelperContainer viewClient, String nodeId)
throws InterruptedException, TimeoutException {
return getNodeField("value", view, viewClient, nodeId);
}
private static String getNodeField(String fieldName, final ContentView view,
TestCallbackHelperContainer viewClient, String nodeId)
throws InterruptedException, TimeoutException {
StringBuilder sb = new StringBuilder();
sb.append("(function() {");
sb.append(" var node = document.getElementById('" + nodeId + "');");
sb.append(" if (!node) return null;");
sb.append(" if (!node." + fieldName + ") return null;");
sb.append(" return [ node." + fieldName + " ];");
sb.append("})();");
String jsonText = JavaScriptUtils.executeJavaScriptAndWaitForResult(
view, viewClient, sb.toString());
Assert.assertFalse("Failed to retrieve contents for " + nodeId,
jsonText.trim().equalsIgnoreCase("null"));
JsonReader jsonReader = new JsonReader(new StringReader(jsonText));
String value = null;
try {
jsonReader.beginArray();
if (jsonReader.hasNext()) value = jsonReader.nextString();
jsonReader.endArray();
Assert.assertNotNull("Invalid contents returned.", value);
jsonReader.close();
} catch (IOException exception) {
Assert.fail("Failed to evaluate JavaScript: " + jsonText + "\n" + exception);
}
return value;
}
public static boolean waitForNonZeroNodeBounds(final ContentView view,
final TestCallbackHelperContainer viewClient, final String nodeName)
throws InterruptedException {
return CriteriaHelper.pollForCriteria(new Criteria() {
@Override
public boolean isSatisfied() {
try {
return !DOMUtils.getNodeBounds(view, viewClient, nodeName).isEmpty();
} catch (InterruptedException e) {
return false;
} catch (TimeoutException e) {
return false;
}
}
});
}
private static int[] getClickTargetForNode(final ContentView view,
TestCallbackHelperContainer viewClient, String nodeName)
throws InterruptedException, TimeoutException {
Rect bounds = getNodeBounds(view, viewClient, nodeName);
Assert.assertNotNull("Failed to get DOM element bounds of '" + nodeName + "'.", bounds);
int clickX = (int) view.getRenderCoordinates().fromLocalCssToPix(bounds.exactCenterX())
+ view.getContentViewCore().getViewportSizeOffsetWidthPix();
int clickY = (int) view.getRenderCoordinates().fromLocalCssToPix(bounds.exactCenterY())
+ view.getContentViewCore().getViewportSizeOffsetHeightPix();
return new int[] { clickX, clickY };
}
}