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 5 package org.chromium.base.test.util; 6 7 import org.junit.Assert; 8 9 import org.chromium.base.PathUtils; 10 import org.chromium.base.StrictModeContext; 11 import org.chromium.base.annotations.CalledByNative; 12 import org.chromium.base.annotations.MainDex; 13 14 /** 15 * Collection of URL utilities. 16 */ 17 @MainDex 18 public class UrlUtils { 19 private static final String DATA_DIR = "/chrome/test/data/"; 20 21 /** 22 * Construct the full path of a test data file. 23 * @param path Pathname relative to external/chrome/test/data 24 */ getTestFilePath(String path)25 public static String getTestFilePath(String path) { 26 // TODO(jbudorick): Remove DATA_DIR once everything has been isolated. crbug/400499 27 return getIsolatedTestFilePath(DATA_DIR + path); 28 } 29 30 // TODO(jbudorick): Remove this function once everything has been isolated and switched back 31 // to getTestFilePath. crbug/400499 32 /** 33 * Construct the full path of a test data file. 34 * @param path Pathname relative to external/ 35 */ getIsolatedTestFilePath(String path)36 public static String getIsolatedTestFilePath(String path) { 37 return getIsolatedTestRoot() + "/" + path; 38 } 39 40 /** 41 * Returns the root of the test data directory. 42 */ 43 @CalledByNative getIsolatedTestRoot()44 public static String getIsolatedTestRoot() { 45 try (StrictModeContext ignored = StrictModeContext.allowDiskReads()) { 46 return PathUtils.getExternalStorageDirectory() + "/chromium_tests_root"; 47 } 48 } 49 50 /** 51 * Construct a suitable URL for loading a test data file. 52 * @param path Pathname relative to external/chrome/test/data 53 */ getTestFileUrl(String path)54 public static String getTestFileUrl(String path) { 55 return "file://" + getTestFilePath(path); 56 } 57 58 // TODO(jbudorick): Remove this function once everything has been isolated and switched back 59 // to getTestFileUrl. crbug/400499 60 /** 61 * Construct a suitable URL for loading a test data file. 62 * @param path Pathname relative to external/ 63 */ getIsolatedTestFileUrl(String path)64 public static String getIsolatedTestFileUrl(String path) { 65 return "file://" + getIsolatedTestFilePath(path); 66 } 67 68 /** 69 * Construct a data:text/html URI for loading from an inline HTML. 70 * @param html An unencoded HTML 71 * @return String An URI that contains the given HTML 72 */ encodeHtmlDataUri(String html)73 public static String encodeHtmlDataUri(String html) { 74 try { 75 // URLEncoder encodes into application/x-www-form-encoded, so 76 // ' '->'+' needs to be undone and replaced with ' '->'%20' 77 // to match the Data URI requirements. 78 String encoded = 79 "data:text/html;utf-8," + java.net.URLEncoder.encode(html, "UTF-8"); 80 encoded = encoded.replace("+", "%20"); 81 return encoded; 82 } catch (java.io.UnsupportedEncodingException e) { 83 Assert.fail("Unsupported encoding: " + e.getMessage()); 84 return null; 85 } 86 } 87 } 88