Home | History | Annotate | Download | only in dumprendertree
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.dumprendertree;
     18 
     19 import com.android.dumprendertree.forwarder.ForwardService;
     20 
     21 import android.os.Environment;
     22 import android.util.Log;
     23 
     24 import java.io.BufferedOutputStream;
     25 import java.io.BufferedReader;
     26 import java.io.File;
     27 import java.io.FileOutputStream;
     28 import java.io.FileReader;
     29 import java.io.FileWriter;
     30 import java.io.IOException;
     31 import java.util.regex.Pattern;
     32 
     33 public class FsUtils {
     34 
     35     private static final String LOGTAG = "FsUtils";
     36     static final String EXTERNAL_DIR = Environment.getExternalStorageDirectory().toString();
     37     static final String HTTP_TESTS_PREFIX =
     38         EXTERNAL_DIR + "/webkit/layout_tests/http/tests/";
     39     static final String HTTPS_TESTS_PREFIX =
     40         EXTERNAL_DIR + "/webkit/layout_tests/http/tests/ssl/";
     41     static final String HTTP_LOCAL_TESTS_PREFIX =
     42         EXTERNAL_DIR + "/webkit/layout_tests/http/tests/local/";
     43     static final String HTTP_MEDIA_TESTS_PREFIX =
     44         EXTERNAL_DIR + "/webkit/layout_tests/http/tests/media/";
     45     static final String HTTP_WML_TESTS_PREFIX =
     46         EXTERNAL_DIR + "/webkit/layout_tests/http/tests/wml/";
     47 
     48     private FsUtils() {
     49         //no creation of instances
     50     }
     51 
     52     /**
     53      * @return the number of tests in the list.
     54      */
     55     public static int writeLayoutTestListRecursively(BufferedOutputStream bos,
     56             String dir, boolean ignoreResultsInDir) throws IOException {
     57 
     58         int testCount = 0;
     59         Log.v(LOGTAG, "Searching tests under " + dir);
     60 
     61         File d = new File(dir);
     62         if (!d.isDirectory()) {
     63             throw new AssertionError("A directory expected, but got " + dir);
     64         }
     65         ignoreResultsInDir |= FileFilter.ignoreResult(dir);
     66 
     67         String[] files = d.list();
     68         for (int i = 0; i < files.length; i++) {
     69             String s = dir + "/" + files[i];
     70 
     71             File f = new File(s);
     72             if (f.isDirectory()) {
     73                 // If this is not a test directory, we don't recurse into it.
     74                 if (!FileFilter.isNonTestDir(s)) {
     75                     Log.v(LOGTAG, "Recursing on " + s);
     76                     testCount += writeLayoutTestListRecursively(bos, s, ignoreResultsInDir);
     77                 }
     78                 continue;
     79             }
     80 
     81             // If this test should be ignored, we skip it completely.
     82             if (FileFilter.ignoreTest(s)) {
     83                 Log.v(LOGTAG, "Ignoring: " + s);
     84                 continue;
     85             }
     86 
     87             if ((s.toLowerCase().endsWith(".html")
     88                     || s.toLowerCase().endsWith(".xml")
     89                     || s.toLowerCase().endsWith(".xhtml"))
     90                     && !s.endsWith("TEMPLATE.html")) {
     91                 Log.v(LOGTAG, "Recording " + s);
     92                 bos.write(s.getBytes());
     93                 // If the result of this test should be ignored, we still run the test.
     94                 if (ignoreResultsInDir || FileFilter.ignoreResult(s)) {
     95                     bos.write((" IGNORE_RESULT").getBytes());
     96                 }
     97                 bos.write('\n');
     98                 testCount++;
     99             }
    100         }
    101         return testCount;
    102     }
    103 
    104     public static void updateTestStatus(String statusFile, String s) {
    105         try {
    106             BufferedOutputStream bos = new BufferedOutputStream(
    107                     new FileOutputStream(statusFile));
    108             bos.write(s.getBytes());
    109             bos.close();
    110         } catch (Exception e) {
    111             Log.e(LOGTAG, "Cannot update file " + statusFile);
    112         }
    113     }
    114 
    115     public static String readTestStatus(String statusFile) {
    116         // read out the test name it stopped last time.
    117         String status = null;
    118         File testStatusFile = new File(statusFile);
    119         if(testStatusFile.exists()) {
    120             try {
    121                 BufferedReader inReader = new BufferedReader(
    122                         new FileReader(testStatusFile));
    123                 status = inReader.readLine();
    124                 inReader.close();
    125             } catch (IOException e) {
    126                 Log.e(LOGTAG, "Error reading test status.", e);
    127             }
    128         }
    129         return status;
    130     }
    131 
    132     public static String getTestUrl(String path) {
    133         String url = null;
    134         if (!path.startsWith(HTTP_TESTS_PREFIX)) {
    135             url = "file://" + path;
    136         } else {
    137             ForwardService.getForwardService().startForwardService();
    138             if (path.startsWith(HTTPS_TESTS_PREFIX)) {
    139                 // still cut the URL after "http/tests/"
    140                 url = "https://127.0.0.1:8443/" + path.substring(HTTP_TESTS_PREFIX.length());
    141             } else if (!path.startsWith(HTTP_LOCAL_TESTS_PREFIX)
    142                     && !path.startsWith(HTTP_MEDIA_TESTS_PREFIX)
    143                     && !path.startsWith(HTTP_WML_TESTS_PREFIX)) {
    144                 url = "http://127.0.0.1:8000/" + path.substring(HTTP_TESTS_PREFIX.length());
    145             } else {
    146                 url = "file://" + path;
    147             }
    148         }
    149         return url;
    150     }
    151 
    152     public static boolean diffIgnoreSpaces(String file1, String file2)  throws IOException {
    153         BufferedReader br1 = new BufferedReader(new FileReader(file1));
    154         BufferedReader br2 = new BufferedReader(new FileReader(file2));
    155         boolean same = true;
    156         Pattern trailingSpace = Pattern.compile("\\s+$");
    157 
    158         while(true) {
    159             String line1 = br1.readLine();
    160             String line2 = br2.readLine();
    161 
    162             if (line1 == null && line2 == null)
    163                 break;
    164             if (line1 != null) {
    165                 line1 = trailingSpace.matcher(line1).replaceAll("");
    166             } else {
    167                 line1 = "";
    168             }
    169             if (line2 != null) {
    170                 line2 = trailingSpace.matcher(line2).replaceAll("");
    171             } else {
    172                 line2 = "";
    173             }
    174             if(!line1.equals(line2)) {
    175                 same = false;
    176                 break;
    177             }
    178         }
    179 
    180         br1.close();
    181         br2.close();
    182 
    183         return same;
    184     }
    185 
    186     public static boolean isTestPageUrl(String url) {
    187         int qmPostion = url.indexOf('?');
    188         int slashPostion = url.lastIndexOf('/');
    189         if (slashPostion < qmPostion) {
    190             String fileName = url.substring(slashPostion + 1, qmPostion);
    191             if ("index.html".equals(fileName)) {
    192                 return true;
    193             }
    194         }
    195         return false;
    196     }
    197 
    198     public static String getLastSegmentInPath(String path) {
    199         int endPos = path.lastIndexOf('/');
    200         path = path.substring(0, endPos);
    201         endPos = path.lastIndexOf('/');
    202         return path.substring(endPos + 1);
    203     }
    204 
    205     public static void writeDrawTime(String fileName, String url, long[] times) {
    206         StringBuffer lineBuffer = new StringBuffer();
    207         // grab the last segment of path in url
    208         lineBuffer.append(getLastSegmentInPath(url));
    209         for (long time : times) {
    210             lineBuffer.append('\t');
    211             lineBuffer.append(time);
    212         }
    213         lineBuffer.append('\n');
    214         String line = lineBuffer.toString();
    215         Log.v(LOGTAG, "logging draw times: " + line);
    216         try {
    217             FileWriter fw = new FileWriter(fileName, true);
    218             fw.write(line);
    219             fw.close();
    220         } catch (IOException ioe) {
    221             Log.e(LOGTAG, "Failed to log draw times", ioe);
    222         }
    223     }
    224 
    225 }
    226