- add sources.
[platform/framework/web/crosswalk.git] / src / base / test / android / javatests / src / org / chromium / base / test / util / UrlUtils.java
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.base.test.util;
6
7 import org.chromium.base.PathUtils;
8
9 import junit.framework.Assert;
10
11 /**
12  * Collection of URL utilities.
13  */
14 public class UrlUtils {
15     private final static String DATA_DIR = "/chrome/test/data/";
16
17     /**
18      * Construct the full path of a test data file.
19      * @param path Pathname relative to external/chrome/testing/data
20      */
21     public static String getTestFilePath(String path) {
22         return PathUtils.getExternalStorageDirectory() + DATA_DIR + path;
23     }
24
25     /**
26      * Construct a suitable URL for loading a test data file.
27      * @param path Pathname relative to external/chrome/testing/data
28      */
29     public static String getTestFileUrl(String path) {
30         return "file://" + getTestFilePath(path);
31     }
32
33     /**
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
37      */
38     public static String encodeHtmlDataUri(String html) {
39         try {
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.
43             String encoded =
44                     "data:text/html;utf-8," +
45                     java.net.URLEncoder.encode(html, "UTF-8");
46             encoded = encoded.replace("+", "%20");
47             return encoded;
48         } catch (java.io.UnsupportedEncodingException e) {
49             Assert.fail("Unsupported encoding: " + e.getMessage());
50             return null;
51         }
52     }
53 }