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