This source file includes following definitions.
- setUp
- getLogMessageJavaScriptData
- Feature
- testAddMessageToConsoleCalledWithCorrectLevel
- Feature
- testAddMessageToConsoleCalledWithCorrectMessage
- Feature
- testAddMessageToConsoleCalledWithCorrectLineAndSource
package org.chromium.android_webview.test;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.Log;
import org.chromium.android_webview.AwContents;
import org.chromium.android_webview.AwWebContentsDelegate;
import org.chromium.base.test.util.Feature;
public class ClientAddMessageToConsoleTest extends AwTestBase {
private static final int LOG_MESSAGE_JAVASCRIPT_DATA_LINE_NUMBER = 4;
private static final String TEST_MESSAGE_ONE = "Test message one.";
private static final String TEST_MESSAGE_TWO = "The second test message.";
private TestAwContentsClient mContentsClient;
private AwContents mAwContents;
@Override
public void setUp() throws Exception {
super.setUp();
mContentsClient = new TestAwContentsClient();
final AwTestContainerView testContainerView =
createAwTestContainerViewOnMainSync(mContentsClient);
mAwContents = testContainerView.getAwContents();
getInstrumentation().runOnMainSync(new Runnable() {
@Override
public void run() {
mAwContents.getSettings().setJavaScriptEnabled(true);
}
});
}
private static String getLogMessageJavaScriptData(String consoleLogMethod, String message) {
return "<html>%0A" +
"<body>%0A" +
"<script>%0A" +
"console." + consoleLogMethod + "('" + message + "');%0A" +
"</script>%0A" +
"<div>%0A" +
"Logging the message [" + message + "] using console." + consoleLogMethod +
" method. " +
"</div>%0A" +
"</body>%0A" +
"</html>";
}
@SmallTest
@Feature({"AndroidWebView"})
public void testAddMessageToConsoleCalledWithCorrectLevel() throws Throwable {
TestAwContentsClient.AddMessageToConsoleHelper addMessageToConsoleHelper =
mContentsClient.getAddMessageToConsoleHelper();
int callCount = addMessageToConsoleHelper.getCallCount();
loadDataSync(mAwContents, mContentsClient.getOnPageFinishedHelper(),
getLogMessageJavaScriptData("error", "msg"),
"text/html", false);
addMessageToConsoleHelper.waitForCallback(callCount);
assertEquals(AwWebContentsDelegate.LOG_LEVEL_ERROR ,
addMessageToConsoleHelper.getLevel());
callCount = addMessageToConsoleHelper.getCallCount();
loadDataSync(mAwContents, mContentsClient.getOnPageFinishedHelper(),
getLogMessageJavaScriptData("warn", "msg"),
"text/html", false);
addMessageToConsoleHelper.waitForCallback(callCount);
assertEquals(AwWebContentsDelegate.LOG_LEVEL_WARNING ,
addMessageToConsoleHelper.getLevel());
callCount = addMessageToConsoleHelper.getCallCount();
loadDataSync(mAwContents, mContentsClient.getOnPageFinishedHelper(),
getLogMessageJavaScriptData("log", "msg"),
"text/html", false);
addMessageToConsoleHelper.waitForCallback(callCount);
assertEquals(AwWebContentsDelegate.LOG_LEVEL_LOG ,
addMessageToConsoleHelper.getLevel());
}
@SmallTest
@Feature({"AndroidWebView"})
public void testAddMessageToConsoleCalledWithCorrectMessage() throws Throwable {
TestAwContentsClient.AddMessageToConsoleHelper addMessageToConsoleHelper =
mContentsClient.getAddMessageToConsoleHelper();
int callCount = addMessageToConsoleHelper.getCallCount();
loadDataSync(mAwContents, mContentsClient.getOnPageFinishedHelper(),
getLogMessageJavaScriptData("log", TEST_MESSAGE_ONE),
"text/html", false);
Log.w("test", getLogMessageJavaScriptData("log", TEST_MESSAGE_ONE));
addMessageToConsoleHelper.waitForCallback(callCount);
assertEquals(TEST_MESSAGE_ONE, addMessageToConsoleHelper.getMessage());
callCount = addMessageToConsoleHelper.getCallCount();
loadDataSync(mAwContents, mContentsClient.getOnPageFinishedHelper(),
getLogMessageJavaScriptData("log", TEST_MESSAGE_TWO),
"text/html", false);
addMessageToConsoleHelper.waitForCallback(callCount);
assertEquals(TEST_MESSAGE_TWO, addMessageToConsoleHelper.getMessage());
}
@SmallTest
@Feature({"AndroidWebView"})
public void testAddMessageToConsoleCalledWithCorrectLineAndSource() throws Throwable {
TestAwContentsClient.AddMessageToConsoleHelper addMessageToConsoleHelper =
mContentsClient.getAddMessageToConsoleHelper();
int callCount = addMessageToConsoleHelper.getCallCount();
String data = getLogMessageJavaScriptData("log", TEST_MESSAGE_ONE);
loadDataSync(mAwContents, mContentsClient.getOnPageFinishedHelper(),
data, "text/html", false);
addMessageToConsoleHelper.waitForCallback(callCount);
assertTrue("Url [" + addMessageToConsoleHelper.getSourceId() + "] expected to end with [" +
data + "].", addMessageToConsoleHelper.getSourceId().endsWith(data));
assertEquals(LOG_MESSAGE_JAVASCRIPT_DATA_LINE_NUMBER,
addMessageToConsoleHelper.getLineNumber());
}
}