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