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.
5 package org.chromium.base.test.util;
7 import org.chromium.base.PathUtils;
9 import junit.framework.Assert;
12 * Collection of URL utilities.
14 public class UrlUtils {
15 private final static String DATA_DIR = "/chrome/test/data/";
18 * Construct the full path of a test data file.
19 * @param path Pathname relative to external/chrome/testing/data
21 public static String getTestFilePath(String path) {
22 return PathUtils.getExternalStorageDirectory() + DATA_DIR + path;
26 * Construct a suitable URL for loading a test data file.
27 * @param path Pathname relative to external/chrome/testing/data
29 public static String getTestFileUrl(String path) {
30 return "file://" + getTestFilePath(path);
34 * Construct a data:text/html URI for loading from an inline HTML.
35 * @param html An unencoded HTML
36 * @return String An URI that contains the given HTML
38 public static String encodeHtmlDataUri(String html) {
40 // URLEncoder encodes into application/x-www-form-encoded, so
41 // ' '->'+' needs to be undone and replaced with ' '->'%20'
42 // to match the Data URI requirements.
44 "data:text/html;utf-8," +
45 java.net.URLEncoder.encode(html, "UTF-8");
46 encoded = encoded.replace("+", "%20");
48 } catch (java.io.UnsupportedEncodingException e) {
49 Assert.fail("Unsupported encoding: " + e.getMessage());