Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(46)

Side by Side Diff: chrome/android/javatests/src/org/chromium/chrome/browser/UrlSchemeTest.java

Issue 2476823002: Increase test coverage for Content:// urls (Closed)
Patch Set: move the common code to a separate method Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.chrome.browser; 5 package org.chromium.chrome.browser;
6 6
7 import android.content.Context; 7 import android.content.Context;
8 import android.net.Uri; 8 import android.net.Uri;
9 import android.os.Environment; 9 import android.os.Environment;
10 import android.test.suitebuilder.annotation.MediumTest; 10 import android.test.suitebuilder.annotation.MediumTest;
11 11
12 import org.chromium.base.ThreadUtils; 12 import org.chromium.base.ThreadUtils;
13 import org.chromium.base.test.util.Feature; 13 import org.chromium.base.test.util.Feature;
14 import org.chromium.base.test.util.RetryOnFailure; 14 import org.chromium.base.test.util.RetryOnFailure;
15 import org.chromium.base.test.util.TestFileUtil; 15 import org.chromium.base.test.util.TestFileUtil;
16 import org.chromium.base.test.util.UrlUtils; 16 import org.chromium.base.test.util.UrlUtils;
17 import org.chromium.chrome.test.ChromeActivityTestCaseBase; 17 import org.chromium.chrome.test.ChromeActivityTestCaseBase;
18 import org.chromium.chrome.test.TestContentProvider; 18 import org.chromium.chrome.test.TestContentProvider;
19 import org.chromium.content.browser.test.util.Criteria;
20 import org.chromium.content.browser.test.util.CriteriaHelper;
21 import org.chromium.net.test.EmbeddedTestServer;
19 22
20 import java.io.File; 23 import java.io.File;
21 import java.io.IOException; 24 import java.io.IOException;
22 import java.io.InputStream; 25 import java.io.InputStream;
23 import java.util.concurrent.Callable; 26 import java.util.concurrent.Callable;
24 27
25
26 /** Test suite for different Android URL schemes. */ 28 /** Test suite for different Android URL schemes. */
27 @RetryOnFailure 29 @RetryOnFailure
28 public class UrlSchemeTest extends ChromeActivityTestCaseBase<ChromeActivity> { 30 public class UrlSchemeTest extends ChromeActivityTestCaseBase<ChromeActivity> {
31 private static final String SIMPLE_SRC = "simple.html";
32 private static final String SIMPLE_IMAGE = "google.png";
33
34 private EmbeddedTestServer mTestServer;
29 35
30 public UrlSchemeTest() { 36 public UrlSchemeTest() {
31 super(ChromeActivity.class); 37 super(ChromeActivity.class);
32 } 38 }
33 39
40 @Override
41 public void setUp() throws Exception {
42 super.setUp();
43 TestContentProvider.resetResourceRequestCounts(getInstrumentation().getT argetContext());
44 mTestServer = EmbeddedTestServer.createAndStartServer(getInstrumentation ().getContext());
45 }
46
47 @Override
48 protected void tearDown() throws Exception {
49 mTestServer.stopAndDestroyServer();
50 super.tearDown();
51 }
52
34 /** 53 /**
35 * Test that resource request count in the content provider works. 54 * Test that resource request count in the content provider works.
36 * This is to make sure that attempts to access the content provider 55 * This is to make sure that attempts to access the content provider
37 * will be detected. 56 * will be detected.
38 */ 57 */
39 @MediumTest 58 @MediumTest
40 @Feature({"Navigation"}) 59 @Feature({"Navigation"})
41 public void testContentProviderResourceRequestCount() throws IOException { 60 public void testContentProviderResourceRequestCount() throws IOException {
42 String resource = "test_reset"; 61 String resource = SIMPLE_SRC;
43 resetResourceRequestCountInContentProvider(resource);
44 ensureResourceRequestCountInContentProvider(resource, 0); 62 ensureResourceRequestCountInContentProvider(resource, 0);
45 // Make a request to the content provider. 63 // Make a request to the content provider.
46 Uri uri = Uri.parse(createContentUrl(resource)); 64 Uri uri = Uri.parse(createContentUrl(resource));
47 Context context = getInstrumentation().getContext(); 65 Context context = getInstrumentation().getContext();
48 InputStream inputStream = null; 66 InputStream inputStream = null;
49 try { 67 try {
50 inputStream = context.getContentResolver().openInputStream(uri); 68 inputStream = context.getContentResolver().openInputStream(uri);
51 assertNotNull(inputStream); 69 assertNotNull(inputStream);
52 } finally { 70 } finally {
53 if (inputStream != null) inputStream.close(); 71 if (inputStream != null) inputStream.close();
54 } 72 }
55 ensureResourceRequestCountInContentProvider(resource, 1); 73 ensureResourceRequestCountInContentProvider(resource, 1);
56 resetResourceRequestCountInContentProvider(resource);
57 ensureResourceRequestCountInContentProvider(resource, 0);
58 } 74 }
59 75
60 /** 76 /**
61 * Make sure content URL access works. 77 * Make sure content URL access works.
62 */ 78 */
63 @MediumTest 79 @MediumTest
64 @Feature({"Navigation"}) 80 @Feature({"Navigation"})
65 public void testContentUrlAccess() throws InterruptedException { 81 public void testContentUrlAccess() throws InterruptedException {
66 String resource = "content_disabled_by_default"; 82 String resource = SIMPLE_SRC;
67 resetResourceRequestCountInContentProvider(resource);
68 loadUrl(createContentUrl(resource)); 83 loadUrl(createContentUrl(resource));
69 ensureResourceRequestCountInContentProviderNotLessThan(resource, 1); 84 ensureResourceRequestCountInContentProviderNotLessThan(resource, 1);
70 } 85 }
71 86
72 private String getTitleOnUiThread() { 87 /**
73 return ThreadUtils.runOnUiThreadBlockingNoException(new Callable<String> () { 88 * Make sure a Content url *CANNOT* access to the contents of an iframe that is loaded as a
Ted C 2016/11/04 22:04:45 s/access to the contents/access the contents
sgurun-gerrit only 2016/11/04 22:40:44 Done.
89 * content URL.
90 */
91 @MediumTest
92 @Feature({"Navigation"})
93 public void testContentUrlIframeAccessFromContentUrl() throws Throwable {
94 final String resource = "page_with_iframe_as_content_url.html";
95 final String iframe = "simple_iframe.html";
96 final String iframeId = "iframe_test_id";
97
98 final String script = "var ifrm = document.getElementById('" + iframeId + "');"
99 + "try {"
100 + " var a = ifrm.contentWindow.document.body.textContent;"
101 + "} catch (e) {"
102 + " document.title = 'fail';"
103 + "}";
104
105 loadUrl(createContentUrl(resource));
106
107 // Make sure iframe is really loaded by verifying the title
108 CriteriaHelper.pollUiThread(new Criteria() {
74 @Override 109 @Override
75 public String call() throws Exception { 110 public boolean isSatisfied() {
76 return getActivity().getActivityTab().getTitle(); 111 return getActivity().getActivityTab().getTitle().equals("iframe loaded");
77 } 112 }
78 }); 113 });
114 // Make sure that content provider was asked to provide the content.
115 ensureResourceRequestCountInContentProvider(iframe, 1);
116 runJavaScriptCodeInCurrentTab(script);
117
118 // Make sure content access failed by verifying that title is set to fai l.
119 CriteriaHelper.pollUiThread(new Criteria() {
120 @Override
121 public boolean isSatisfied() {
122 return getActivity().getActivityTab().getTitle().equals("fail");
123 }
124 });
125 }
126
127 /**
128 * Test that a content URL is *ALLOWED* to access an image provided by a con tent URL.
129 */
130 @MediumTest
131 @Feature({"Navigation"})
132 public void testContentUrlImageFromContentUrl() throws Throwable {
133 verifyImageLoadRules(createContentUrl(SIMPLE_SRC), "success", 1);
134 }
135
136 /**
137 * Test that a HTTP URL is *NOT ALLOWED* to access an image provided by a co ntent URL.
138 */
139 @MediumTest
140 @Feature({"Navigation"})
141 public void testContentUrlImageFromHttpUrl() throws Throwable {
142 final String main = mTestServer.getURL("/chrome/test/data/android/" + SI MPLE_SRC);
143 verifyImageLoadRules(main, "error", 0);
144 }
145
146 private void verifyImageLoadRules(String url, final String expectedTitle, in t expectedLoadCount)
147 throws Throwable {
148 final String resource = SIMPLE_IMAGE;
149 final String script = "var img = new Image();"
150 + " img.onerror = function() { document.title = 'error' };"
151 + " img.onabort = function() { document.title = 'error' };"
152 + " img.onload = function() { document.title = 'success' };"
153 + " img.src = '" + createContentUrl(resource) + "';"
154 + " document.body.appendChild(img);";
155 loadUrl(url);
156 runJavaScriptCodeInCurrentTab(script);
157
158 // Make sure image is not loaded by verifying that title is set to error .
sgurun-gerrit only 2016/11/04 22:40:44 removed stale comment.
159 CriteriaHelper.pollUiThread(new Criteria() {
160 @Override
161 public boolean isSatisfied() {
162 return getActivity().getActivityTab().getTitle().equals(expected Title);
163 }
164 });
165 ensureResourceRequestCountInContentProviderNotLessThan(resource, expecte dLoadCount);
79 } 166 }
80 167
81 /** 168 /**
82 * Test that a content URL is not allowed within a data URL. 169 * Test that a content URL is not allowed within a data URL.
83 */ 170 */
84 @MediumTest 171 @MediumTest
85 @Feature({"Navigation"}) 172 @Feature({"Navigation"})
86 public void testContentUrlFromData() throws InterruptedException { 173 public void testContentUrlFromData() throws InterruptedException {
87 final String target = "content_from_data"; 174 final String target = SIMPLE_IMAGE;
88 resetResourceRequestCountInContentProvider(target);
89 loadUrl(UrlUtils.encodeHtmlDataUri( 175 loadUrl(UrlUtils.encodeHtmlDataUri(
90 "<img src=\"" + createContentUrl(target) + "\">")); 176 "<img src=\"" + createContentUrl(target) + "\">"));
91 ensureResourceRequestCountInContentProvider(target, 0); 177 ensureResourceRequestCountInContentProvider(target, 0);
92 } 178 }
93 179
94 /** 180 /**
95 * Test that a content URL is not allowed within a local file. 181 * Test that a content URL is not allowed within a local file.
96 */ 182 */
97 @MediumTest 183 @MediumTest
98 @Feature({"Navigation"}) 184 @Feature({"Navigation"})
99 public void testContentUrlFromFile() throws InterruptedException, IOExceptio n { 185 public void testContentUrlFromFile() throws InterruptedException, IOExceptio n {
100 final String target = "content_from_file"; 186 final String target = SIMPLE_IMAGE;
101 final File file = new File(Environment.getExternalStorageDirectory(), ta rget + ".html"); 187 final File file = new File(Environment.getExternalStorageDirectory(), ta rget + ".html");
102 try { 188 try {
103 TestFileUtil.createNewHtmlFile( 189 TestFileUtil.createNewHtmlFile(
104 file, target, "<img src=\"" + createContentUrl(target) + "\" >"); 190 file, target, "<img src=\"" + createContentUrl(target) + "\" >");
105 resetResourceRequestCountInContentProvider(target);
106 loadUrl("file:///" + file.getAbsolutePath()); 191 loadUrl("file:///" + file.getAbsolutePath());
107 ensureResourceRequestCountInContentProvider(target, 0); 192 ensureResourceRequestCountInContentProvider(target, 0);
108 } finally { 193 } finally {
109 TestFileUtil.deleteFile(file); 194 TestFileUtil.deleteFile(file);
110 } 195 }
111 } 196 }
112 197
198 private String getTitleOnUiThread() {
199 return ThreadUtils.runOnUiThreadBlockingNoException(new Callable<String> () {
200 @Override
201 public String call() throws Exception {
202 return getActivity().getActivityTab().getTitle();
203 }
204 });
205 }
206
113 /** 207 /**
114 * Test that the browser can be navigated to a file URL. 208 * Test that the browser can be navigated to a file URL.
115 */ 209 */
116 @MediumTest 210 @MediumTest
117 @Feature({"Navigation"}) 211 @Feature({"Navigation"})
118 public void testFileUrlNavigation() throws InterruptedException, IOException { 212 public void testFileUrlNavigation() throws InterruptedException, IOException {
119 final File file = new File(Environment.getExternalStorageDirectory(), 213 final File file = new File(Environment.getExternalStorageDirectory(),
120 "url_navigation_test.html"); 214 "url_navigation_test.html");
121 215
122 try { 216 try {
(...skipping 22 matching lines...) Expand all
145 * @param expectedMinimalCount Expected minimal resource requests count 239 * @param expectedMinimalCount Expected minimal resource requests count
146 */ 240 */
147 private void ensureResourceRequestCountInContentProviderNotLessThan( 241 private void ensureResourceRequestCountInContentProviderNotLessThan(
148 String resource, int expectedMinimalCount) { 242 String resource, int expectedMinimalCount) {
149 Context context = getInstrumentation().getTargetContext(); 243 Context context = getInstrumentation().getTargetContext();
150 int actualCount = TestContentProvider.getResourceRequestCount(context, r esource); 244 int actualCount = TestContentProvider.getResourceRequestCount(context, r esource);
151 assertTrue("Minimal expected: " + expectedMinimalCount + ", actual: " + actualCount, 245 assertTrue("Minimal expected: " + expectedMinimalCount + ", actual: " + actualCount,
152 actualCount >= expectedMinimalCount); 246 actualCount >= expectedMinimalCount);
153 } 247 }
154 248
155 private void resetResourceRequestCountInContentProvider(String resource) {
156 Context context = getInstrumentation().getTargetContext();
157 TestContentProvider.resetResourceRequestCount(context, resource);
158 }
159
160 private String createContentUrl(final String target) { 249 private String createContentUrl(final String target) {
161 return TestContentProvider.createContentUrl(target); 250 return TestContentProvider.createContentUrl(target);
162 } 251 }
163 252
164 @Override 253 @Override
165 public void startMainActivity() throws InterruptedException { 254 public void startMainActivity() throws InterruptedException {
166 startMainActivityFromLauncher(); 255 startMainActivityFromLauncher();
167 } 256 }
168 } 257 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698