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