This source file includes following definitions.
- setUp
- getHtml
- loadFromDataAndGetTitle
- loadFromUrlAndGetTitle
- Feature
- testLoadDataGetTitle
- Feature
- testGetTitleOnDataContainingEmptyTitle
- Feature
- testGetTitleOnDataContainingNoTitle
- Feature
- testLoadUrlGetTitle
- Feature
- testGetTitleOnLoadUrlFileContainingEmptyTitle
- Feature
- testGetTitleOnLoadUrlFileContainingNoTitle
- Feature
- testGetTitleSetFromJS
package org.chromium.android_webview.test;
import android.test.suitebuilder.annotation.SmallTest;
import android.test.suitebuilder.annotation.Smoke;
import org.chromium.android_webview.AwContents;
import org.chromium.base.test.util.Feature;
import org.chromium.net.test.util.TestWebServer;
public class GetTitleTest extends AwTestBase {
private static final String TITLE = "TITLE";
private static final String GET_TITLE_TEST_PATH = "/get_title_test.html";
private static final String GET_TITLE_TEST_EMPTY_PATH = "/get_title_test_empty.html";
private static final String GET_TITLE_TEST_NO_TITLE_PATH = "/get_title_test_no_title.html";
private TestAwContentsClient mContentsClient;
private AwContents mAwContents;
private static class PageInfo {
public final String mTitle;
public final String mUrl;
public PageInfo(String title, String url) {
mTitle = title;
mUrl = url;
}
}
@Override
public void setUp() throws Exception {
super.setUp();
mContentsClient = new TestAwContentsClient();
final AwTestContainerView testContainerView =
createAwTestContainerViewOnMainSync(mContentsClient);
mAwContents = testContainerView.getAwContents();
}
private static final String getHtml(String title) {
StringBuilder html = new StringBuilder();
html.append("<html><head>");
if (title != null) {
html.append("<title>" + title + "</title>");
}
html.append("</head><body>BODY</body></html>");
return html.toString();
}
private String loadFromDataAndGetTitle(String html) throws Throwable {
loadDataSync(mAwContents, mContentsClient.getOnPageFinishedHelper(),
html, "text/html", false);
return getTitleOnUiThread(mAwContents);
}
private PageInfo loadFromUrlAndGetTitle(String html, String filename) throws Throwable {
TestWebServer webServer = null;
try {
webServer = new TestWebServer(false);
final String url = webServer.setResponse(filename, html, null);
loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url);
return new PageInfo(getTitleOnUiThread(mAwContents),
url.replaceAll("http:\\/\\/", ""));
} finally {
if (webServer != null) webServer.shutdown();
}
}
@Smoke
@SmallTest
@Feature({"AndroidWebView", "Main"})
public void testLoadDataGetTitle() throws Throwable {
final String title = loadFromDataAndGetTitle(getHtml(TITLE));
assertEquals("Title should be " + TITLE, TITLE, title);
}
@SmallTest
@Feature({"AndroidWebView"})
public void testGetTitleOnDataContainingEmptyTitle() throws Throwable {
final String content = getHtml("");
final String expectedTitle = "data:text/html," + content;
final String title = loadFromDataAndGetTitle(content);
assertEquals("Title should be set to the loaded data:text/html content", expectedTitle,
title);
}
@SmallTest
@Feature({"AndroidWebView"})
public void testGetTitleOnDataContainingNoTitle() throws Throwable {
final String content = getHtml(null);
final String expectedTitle = "data:text/html," + content;
final String title = loadFromDataAndGetTitle(content);
assertEquals("Title should be set to the data:text/html content", expectedTitle, title);
}
@SmallTest
@Feature({"AndroidWebView"})
public void testLoadUrlGetTitle() throws Throwable {
final PageInfo info = loadFromUrlAndGetTitle(getHtml(TITLE), GET_TITLE_TEST_PATH);
assertEquals("Title should be " + TITLE, TITLE, info.mTitle);
}
@SmallTest
@Feature({"AndroidWebView"})
public void testGetTitleOnLoadUrlFileContainingEmptyTitle() throws Throwable {
final PageInfo info = loadFromUrlAndGetTitle(getHtml(""), GET_TITLE_TEST_EMPTY_PATH);
assertEquals("Incorrect title :: " , info.mUrl, info.mTitle);
}
@SmallTest
@Feature({"AndroidWebView"})
public void testGetTitleOnLoadUrlFileContainingNoTitle() throws Throwable {
final PageInfo info = loadFromUrlAndGetTitle(getHtml(null), GET_TITLE_TEST_NO_TITLE_PATH);
assertEquals("Incorrect title :: " , info.mUrl, info.mTitle);
}
@SmallTest
@Feature({"AndroidWebView"})
public void testGetTitleSetFromJS() throws Throwable {
final String expectedTitle = "Expected";
final String page =
"<html><head>" +
"<script>document.title=\"" + expectedTitle + "\"</script>" +
"</head><body>" +
"</body></html>";
getAwSettingsOnUiThread(mAwContents).setJavaScriptEnabled(true);
final String title = loadFromDataAndGetTitle(page);
assertEquals("Incorrect title :: ", expectedTitle, title);
}
}