1// Copyright 2012 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5package org.chromium.android_webview.test; 6 7import android.test.suitebuilder.annotation.SmallTest; 8import android.test.suitebuilder.annotation.Smoke; 9 10import org.chromium.android_webview.AwContents; 11import org.chromium.base.test.util.Feature; 12import org.chromium.net.test.util.TestWebServer; 13 14/** 15 * A test suite for ContentView.getTitle(). 16 */ 17public class GetTitleTest extends AwTestBase { 18 private static final String TITLE = "TITLE"; 19 20 private static final String GET_TITLE_TEST_PATH = "/get_title_test.html"; 21 private static final String GET_TITLE_TEST_EMPTY_PATH = "/get_title_test_empty.html"; 22 private static final String GET_TITLE_TEST_NO_TITLE_PATH = "/get_title_test_no_title.html"; 23 24 private TestAwContentsClient mContentsClient; 25 private AwContents mAwContents; 26 27 private static class PageInfo { 28 public final String mTitle; 29 public final String mUrl; 30 31 public PageInfo(String title, String url) { 32 mTitle = title; 33 mUrl = url; 34 } 35 } 36 37 @Override 38 public void setUp() throws Exception { 39 super.setUp(); 40 mContentsClient = new TestAwContentsClient(); 41 final AwTestContainerView testContainerView = 42 createAwTestContainerViewOnMainSync(mContentsClient); 43 mAwContents = testContainerView.getAwContents(); 44 } 45 46 private static final String getHtml(String title) { 47 StringBuilder html = new StringBuilder(); 48 html.append("<html><head>"); 49 if (title != null) { 50 html.append("<title>" + title + "</title>"); 51 } 52 html.append("</head><body>BODY</body></html>"); 53 return html.toString(); 54 } 55 56 private String loadFromDataAndGetTitle(String html) throws Throwable { 57 loadDataSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), 58 html, "text/html", false); 59 return getTitleOnUiThread(mAwContents); 60 } 61 62 private PageInfo loadFromUrlAndGetTitle(String html, String filename) throws Throwable { 63 TestWebServer webServer = null; 64 try { 65 webServer = new TestWebServer(false); 66 67 final String url = webServer.setResponse(filename, html, null); 68 loadUrlSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), url); 69 return new PageInfo(getTitleOnUiThread(mAwContents), 70 url.replaceAll("http:\\/\\/", "")); 71 72 } finally { 73 if (webServer != null) webServer.shutdown(); 74 } 75 } 76 77 /** 78 * When the data has title info, the page title is set to it. 79 * @throws Throwable 80 */ 81 @Smoke 82 @SmallTest 83 @Feature({"AndroidWebView", "Main"}) 84 public void testLoadDataGetTitle() throws Throwable { 85 final String title = loadFromDataAndGetTitle(getHtml(TITLE)); 86 assertEquals("Title should be " + TITLE, TITLE, title); 87 } 88 89 /** 90 * When the data has empty title, the page title is set to the loaded content. 91 * @throws Throwable 92 */ 93 @SmallTest 94 @Feature({"AndroidWebView"}) 95 public void testGetTitleOnDataContainingEmptyTitle() throws Throwable { 96 final String content = getHtml(""); 97 final String expectedTitle = "data:text/html," + content; 98 final String title = loadFromDataAndGetTitle(content); 99 assertEquals("Title should be set to the loaded data:text/html content", expectedTitle, 100 title); 101 } 102 103 /** 104 * When the data has no title, the page title is set to the loaded content. 105 * @throws Throwable 106 */ 107 @SmallTest 108 @Feature({"AndroidWebView"}) 109 public void testGetTitleOnDataContainingNoTitle() throws Throwable { 110 final String content = getHtml(null); 111 final String expectedTitle = "data:text/html," + content; 112 final String title = loadFromDataAndGetTitle(content); 113 assertEquals("Title should be set to the data:text/html content", expectedTitle, title); 114 } 115 116 /** 117 * When url-file has the title info, the page title is set to it. 118 * @throws Throwable 119 */ 120 @SmallTest 121 @Feature({"AndroidWebView"}) 122 public void testLoadUrlGetTitle() throws Throwable { 123 final PageInfo info = loadFromUrlAndGetTitle(getHtml(TITLE), GET_TITLE_TEST_PATH); 124 assertEquals("Title should be " + TITLE, TITLE, info.mTitle); 125 } 126 127 /** 128 * When the loaded file has empty title, the page title is set to the url it loads from. 129 * It also contains: hostName, portNumber information if its part of the loaded URL. 130 * @throws Throwable 131 */ 132 @SmallTest 133 @Feature({"AndroidWebView"}) 134 public void testGetTitleOnLoadUrlFileContainingEmptyTitle() throws Throwable { 135 final PageInfo info = loadFromUrlAndGetTitle(getHtml(""), GET_TITLE_TEST_EMPTY_PATH); 136 assertEquals("Incorrect title :: " , info.mUrl, info.mTitle); 137 } 138 139 /** 140 * When the loaded file has no title, the page title is set to the urk it loads from. 141 * It also contains: hostName, portNumber information if its part of the loaded URL. 142 * @throws Throwable 143 */ 144 @SmallTest 145 @Feature({"AndroidWebView"}) 146 public void testGetTitleOnLoadUrlFileContainingNoTitle() throws Throwable { 147 final PageInfo info = loadFromUrlAndGetTitle(getHtml(null), GET_TITLE_TEST_NO_TITLE_PATH); 148 assertEquals("Incorrect title :: " , info.mUrl, info.mTitle); 149 } 150 151 @SmallTest 152 @Feature({"AndroidWebView"}) 153 public void testGetTitleSetFromJS() throws Throwable { 154 final String expectedTitle = "Expected"; 155 final String page = 156 "<html><head>" + 157 "<script>document.title=\"" + expectedTitle + "\"</script>" + 158 "</head><body>" + 159 "</body></html>"; 160 getAwSettingsOnUiThread(mAwContents).setJavaScriptEnabled(true); 161 final String title = loadFromDataAndGetTitle(page); 162 assertEquals("Incorrect title :: ", expectedTitle, title); 163 } 164} 165