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