TestsListPreloaderThread.java revision 5f0ccd76a88586ce85c17cb4db058934e693a4fc
1/*
2 * Copyright (C) 2010 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.dumprendertree2;
18
19import android.os.Environment;
20import android.os.Message;
21import android.util.Log;
22
23import java.io.File;
24import java.util.ArrayList;
25import java.util.LinkedList;
26
27/**
28 * A Thread that is responsible for generating a lists of tests to run.
29 */
30public class TestsListPreloaderThread extends Thread {
31
32    private static final String LOG_TAG = "TestsListPreloaderThread";
33
34    /** TODO: make it a setting */
35    private static final String TESTS_ROOT_DIR_PATH =
36            Environment.getExternalStorageDirectory() +
37            File.separator + "android" +
38            File.separator + "LayoutTests";
39
40    /** A list containing relative paths of tests to run */
41    private ArrayList<String> mTestsList = new ArrayList<String>();
42
43    private FileFilter mFileFilter;
44
45    /**
46     * A relative path to the folder with the tests we want to run or particular test.
47     * Used up to and including preloadTests().
48     */
49    private String mRelativePath;
50
51    private Message mDoneMsg;
52
53    /**
54     * The given path must be relative to the root dir.
55     *
56     * @param path
57     * @param doneMsg
58     */
59    public TestsListPreloaderThread(String path, Message doneMsg) {
60        mFileFilter = new FileFilter(TESTS_ROOT_DIR_PATH);
61        mRelativePath = path;
62        mDoneMsg = doneMsg;
63    }
64
65    @Override
66    public void run() {
67        /** Check if the path is correct */
68        File file = new File(TESTS_ROOT_DIR_PATH, mRelativePath);
69        if (!file.exists()) {
70            Log.e(LOG_TAG + "::run", "Path does not exist: " + mRelativePath);
71            return;
72        }
73
74        /** Populate the tests' list accordingly */
75        if (file.isDirectory()) {
76            preloadTests(mRelativePath);
77        } else {
78            mTestsList.add(mRelativePath);
79        }
80        mDoneMsg.obj = mTestsList;
81        mDoneMsg.sendToTarget();
82    }
83
84    /**
85     * Loads all the tests from the given folders and all the subfolders
86     * into mTestsList.
87     *
88     * @param dirRelativePath
89     */
90    private void preloadTests(String dirRelativePath) {
91        LinkedList<String> foldersList = new LinkedList<String>();
92        foldersList.add(dirRelativePath);
93
94        String relativePath;
95        String currentDirRelativePath;
96        String itemName;
97        File[] items;
98        while (!foldersList.isEmpty()) {
99            currentDirRelativePath = foldersList.removeFirst();
100            items = new File(TESTS_ROOT_DIR_PATH, currentDirRelativePath).listFiles();
101            for (File item : items) {
102                itemName = item.getName();
103                relativePath = currentDirRelativePath + File.separator + itemName;
104
105                if (item.isDirectory() && FileFilter.isTestDir(itemName)) {
106                    foldersList.add(relativePath);
107                    continue;
108                }
109
110                if (FileFilter.isTestFile(itemName)) {
111                    if (!mFileFilter.isSkip(relativePath)) {
112                        mTestsList.add(relativePath);
113                    } else {
114                        //mSummarizer.addSkippedTest(relativePath);
115                        /** TODO: Summarizer is now in service - figure out how to send the info */
116                    }
117                }
118            }
119        }
120    }
121}