FsUtils.java revision 5e8f52f5c5ac97cbc514e72c4fc84b6fa46ebc57
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 public static void findLayoutTestsRecursively(BufferedOutputStream bos, 46 String dir) throws IOException { 47 Log.v(LOGTAG, "Searching tests under " + dir); 48 49 File d = new File(dir); 50 if (!d.isDirectory()) { 51 throw new AssertionError("A directory expected, but got " + dir); 52 } 53 54 String[] files = d.list(); 55 for (int i = 0; i < files.length; i++) { 56 String s = dir + "/" + files[i]; 57 if (s.endsWith("TEMPLATE.html")) { 58 continue; 59 } 60 if (FileFilter.ignoreTest(s)) { 61 Log.v(LOGTAG, " Ignoring: " + s); 62 continue; 63 } 64 if (s.toLowerCase().endsWith(".html") 65 || s.toLowerCase().endsWith(".xml")) { 66 bos.write(s.getBytes()); 67 bos.write('\n'); 68 continue; 69 } 70 71 File f = new File(s); 72 if (f.isDirectory()) { 73 findLayoutTestsRecursively(bos, s); 74 continue; 75 } 76 77 Log.v(LOGTAG, "Skipping " + s); 78 } 79 } 80 81 public static void updateTestStatus(String statusFile, String s) { 82 try { 83 BufferedOutputStream bos = new BufferedOutputStream( 84 new FileOutputStream(statusFile)); 85 bos.write(s.getBytes()); 86 bos.close(); 87 } catch (Exception e) { 88 Log.e(LOGTAG, "Cannot update file " + statusFile); 89 } 90 } 91 92 public static String readTestStatus(String statusFile) { 93 // read out the test name it stopped last time. 94 String status = null; 95 File testStatusFile = new File(statusFile); 96 if(testStatusFile.exists()) { 97 try { 98 BufferedReader inReader = new BufferedReader( 99 new FileReader(testStatusFile)); 100 status = inReader.readLine(); 101 inReader.close(); 102 } catch (IOException e) { 103 Log.e(LOGTAG, "Error reading test status.", e); 104 } 105 } 106 return status; 107 } 108 109 public static String getTestUrl(String path) { 110 String url = null; 111 if (!path.startsWith(HTTP_TESTS_PREFIX)) { 112 url = "file://" + path; 113 } else { 114 ForwardService.getForwardService().startForwardService(); 115 if (path.startsWith(HTTPS_TESTS_PREFIX)) { 116 // still cut the URL after "http/tests/" 117 url = "https://127.0.0.1:8443/" + path.substring(HTTP_TESTS_PREFIX.length()); 118 } else if (!path.startsWith(HTTP_LOCAL_TESTS_PREFIX) 119 && !path.startsWith(HTTP_MEDIA_TESTS_PREFIX) 120 && !path.startsWith(HTTP_WML_TESTS_PREFIX)) { 121 url = "http://127.0.0.1:8000/" + path.substring(HTTP_TESTS_PREFIX.length()); 122 } else { 123 url = "file://" + path; 124 } 125 } 126 return url; 127 } 128 129 public static boolean diffIgnoreSpaces(String file1, String file2) throws IOException { 130 BufferedReader br1 = new BufferedReader(new FileReader(file1)); 131 BufferedReader br2 = new BufferedReader(new FileReader(file2)); 132 boolean same = true; 133 Pattern trailingSpace = Pattern.compile("\\s+$"); 134 135 while(true) { 136 String line1 = br1.readLine(); 137 String line2 = br2.readLine(); 138 139 if (line1 == null && line2 == null) 140 break; 141 if (line1 != null) { 142 line1 = trailingSpace.matcher(line1).replaceAll(""); 143 } else { 144 line1 = ""; 145 } 146 if (line2 != null) { 147 line2 = trailingSpace.matcher(line2).replaceAll(""); 148 } else { 149 line2 = ""; 150 } 151 if(!line1.equals(line2)) { 152 same = false; 153 break; 154 } 155 } 156 157 br1.close(); 158 br2.close(); 159 160 return same; 161 } 162 163 public static boolean isTestPageUrl(String url) { 164 int qmPostion = url.indexOf('?'); 165 int slashPostion = url.lastIndexOf('/'); 166 if (slashPostion < qmPostion) { 167 String fileName = url.substring(slashPostion + 1, qmPostion); 168 if ("index.html".equals(fileName)) { 169 return true; 170 } 171 } 172 return false; 173 } 174 175 public static String getLastSegmentInPath(String path) { 176 int endPos = path.lastIndexOf('/'); 177 path = path.substring(0, endPos); 178 endPos = path.lastIndexOf('/'); 179 return path.substring(endPos + 1); 180 } 181 182 public static void writeDrawTime(String fileName, String url, long[] times) { 183 StringBuffer lineBuffer = new StringBuffer(); 184 // grab the last segment of path in url 185 lineBuffer.append(getLastSegmentInPath(url)); 186 for (long time : times) { 187 lineBuffer.append('\t'); 188 lineBuffer.append(time); 189 } 190 lineBuffer.append('\n'); 191 String line = lineBuffer.toString(); 192 Log.v(LOGTAG, "logging draw times: " + line); 193 try { 194 FileWriter fw = new FileWriter(fileName, true); 195 fw.write(line); 196 fw.close(); 197 } catch (IOException ioe) { 198 Log.e(LOGTAG, "Failed to log draw times", ioe); 199 } 200 } 201 202} 203