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