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