Home | History | Annotate | Download | only in util
      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.android_webview.test.util;
      6 
      7 import android.util.Pair;
      8 
      9 import java.util.ArrayList;
     10 import java.util.List;
     11 
     12 /**
     13  * Auxiliary class providing common HTML and base64 resources using for testing.
     14  */
     15 public class CommonResources {
     16 
     17     // Content-type headers used for HTML code.
     18     public static List<Pair<String, String>> getTextHtmlHeaders(boolean disableCache) {
     19         return getContentTypeAndCacheHeaders("text/html", disableCache);
     20     }
     21 
     22     // Content-type headers used for javascript code.
     23     public static List<Pair<String, String>> getTextJavascriptHeaders(boolean disableCache) {
     24         return getContentTypeAndCacheHeaders("text/javascript", disableCache);
     25     }
     26 
     27     // Content-type headers used for png images.
     28     public static List<Pair<String, String>> getImagePngHeaders(boolean disableCache) {
     29         return getContentTypeAndCacheHeaders("image/png", disableCache);
     30     }
     31 
     32     public static List<Pair<String, String>> getContentTypeAndCacheHeaders(
     33             String contentType, boolean disableCache) {
     34         List<Pair<String, String>> headers = new ArrayList<Pair<String, String>>();
     35         headers.add(Pair.create("Content-Type", contentType));
     36         if (disableCache) headers.add(Pair.create("Cache-Control", "no-store"));
     37         return headers;
     38     }
     39 
     40     // Returns the HTML code used to verify if an image has been successfully loaded.
     41     public static String getOnImageLoadedHtml(String imageSrc) {
     42         return "<html>" +
     43                "  <head>" +
     44                "    <script>" +
     45                "      function updateTitle() {" +
     46                "        document.title=document.getElementById('img').naturalHeight" +
     47                "      }" +
     48                "    </script>" +
     49                "  </head>" +
     50                "  <body onload='updateTitle();'>" +
     51                "    <img id='img' onload='updateTitle();' src='" + imageSrc + "'>" +
     52                "  </body>" +
     53                "</html>";
     54     }
     55 
     56     // Default name for the favicon image.
     57     public static final String FAVICON_FILENAME = "favicon.png";
     58 
     59     // HTML code of a static simple page with a favicon.
     60     public static final String FAVICON_STATIC_HTML =
     61         "<html><head><link rel=\"icon\" type=\"image/png\" href=\"" + FAVICON_FILENAME + "\">" +
     62         "</head><body>Favicon example</body></html>";
     63 
     64     // Base64 data of a favicon image resource.
     65     public static final String FAVICON_DATA_BASE64 =
     66         "iVBORw0KGgoAAAANSUhEUgAAABAAAAAFCAYAAABM6GxJAAAABHNCSVQICAgIfAhkiAAAASJJREFU" +
     67         "GJU9yDtLQnEYwOHfOZ40L3gZDJKgJCKaamvpGzS09wUaormh7xA0S5C0ZDTkZJsNUltkkpAUZkIX" +
     68         "L3g9FzzH/9vm9vAgoqRUGUu20JHTXFfafUdERJSIKJnOPFUTERHpqIYclY5nb2QKFumky95OlO+W" +
     69         "TSgATqOO5k3xr6ZxelXmDFDhdaqfLkPRWQglULaN/V5DPzl3iIb9xCI+Eskog/wdyhowLlb4vThE" +
     70         "giF8zRsurx55beg8lMfMezZW9hqz20M/Owhwe2/yUrPI5Ds8//mRehN7JYWxvIX6eWJkbLK9laL8" +
     71         "ZrKxFETzxTBNB5SOJjKV/mhCq+uSjGvE4hHc4QA9YGAEwnhWF1ePkCtOWFv0+PiasL8bR3QDr93h" +
     72         "HyFup9LWUksHAAAAAElFTkSuQmCC";
     73 
     74     // Default name for an example 'about' HTML page.
     75     public static final String ABOUT_FILENAME = "about.html";
     76 
     77     // Title used in the 'about' example.
     78     public static final String ABOUT_TITLE = "About the Google";
     79 
     80     // HTML code of an 'about' example.
     81     public static final String ABOUT_HTML =
     82         "<html>" +
     83         "  <head>" +
     84         "    <title>" + ABOUT_TITLE + "</title>" +
     85         "  </head>" +
     86         "  <body>" +
     87         "    This is the Google!" +
     88         "  </body>" +
     89         "</html>";
     90 
     91     public static String makeHtmlPageFrom(String headers, String body) {
     92         return "<html>" +
     93                  "<head>" +
     94                      "<style type=\"text/css\">" +
     95                          // Make the image take up all of the page so that we don't have to do
     96                          // any fancy hit target calculations when synthesizing the touch event
     97                          // to click it.
     98                          "img.big { width:100%; height:100%; background-color:blue; }" +
     99                          ".full_view { height:100%; width:100%; position:absolute; }" +
    100                      "</style>" +
    101                      headers +
    102                  "</head>" +
    103                  "<body>" +
    104                      body +
    105                  "</body>" +
    106              "</html>";
    107     }
    108 
    109     public static String makeHtmlPageWithSimpleLinkTo(String headers, String destination) {
    110         return makeHtmlPageFrom(headers,
    111                         "<a href=\"" + destination + "\" id=\"link\">" +
    112                            "<img class=\"big\" />" +
    113                         "</a>");
    114     }
    115 
    116     public static String makeHtmlPageWithSimpleLinkTo(String destination) {
    117         return makeHtmlPageWithSimpleLinkTo("", destination);
    118     }
    119 
    120     public static String makeHtmlPageWithSimplePostFormTo(String destination) {
    121         return makeHtmlPageFrom("",
    122                 "<form action=\"" + destination + "\" method=\"post\">" +
    123                   "<input type=\"submit\" value=\"post\" id=\"link\">" +
    124                 "</form>");
    125     }
    126 }
    127