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 java.io.File;
22import java.util.ArrayList;
23import java.util.LinkedList;
24import java.util.List;
25
26/**
27 * A Thread that is responsible for generating a lists of tests to run.
28 */
29public class TestsListPreloaderThread extends Thread {
30
31    private static final String LOG_TAG = "TestsListPreloaderThread";
32
33    /** A list containing relative paths of tests to run */
34    private ArrayList<String> mTestsList = new ArrayList<String>();
35
36    private FileFilter mFileFilter;
37
38    /**
39     * A relative path to the directory with the tests we want to run or particular test.
40     * Used up to and including preloadTests().
41     */
42    private String mRelativePath;
43
44    private Message mDoneMsg;
45
46    /**
47     * The given path must be relative to the root dir.
48     *
49     * @param path
50     * @param doneMsg
51     */
52    public TestsListPreloaderThread(String path, Message doneMsg) {
53        mRelativePath = path;
54        mDoneMsg = doneMsg;
55    }
56
57    @Override
58    public void run() {
59        mFileFilter = new FileFilter();
60        if (FileFilter.isTestFile(mRelativePath)) {
61            mTestsList.add(mRelativePath);
62        } else {
63            loadTestsFromUrl(mRelativePath);
64        }
65
66        mDoneMsg.obj = mTestsList;
67        mDoneMsg.sendToTarget();
68    }
69
70    /**
71     * Loads all the tests from the given directories and all the subdirectories
72     * into mTestsList.
73     *
74     * @param dirRelativePath
75     */
76    private void loadTestsFromUrl(String rootRelativePath) {
77        LinkedList<String> directoriesList = new LinkedList<String>();
78        directoriesList.add(rootRelativePath);
79
80        String relativePath;
81        String itemName;
82        while (!directoriesList.isEmpty()) {
83            relativePath = directoriesList.removeFirst();
84
85            List<String> dirRelativePaths = FsUtils.getLayoutTestsDirContents(relativePath, false, true);
86            if (dirRelativePaths != null) {
87                for (String dirRelativePath : dirRelativePaths) {
88                    itemName = new File(dirRelativePath).getName();
89                    if (FileFilter.isTestDir(itemName)) {
90                        directoriesList.add(dirRelativePath);
91                    }
92                }
93            }
94
95            List<String> testRelativePaths = FsUtils.getLayoutTestsDirContents(relativePath, false, false);
96            if (testRelativePaths != null) {
97                for (String testRelativePath : testRelativePaths) {
98                    itemName = new File(testRelativePath).getName();
99                    if (FileFilter.isTestFile(itemName)) {
100                        /** We choose to skip all the tests that are expected to crash. */
101                        if (!mFileFilter.isCrash(testRelativePath)) {
102                            mTestsList.add(testRelativePath);
103                        } else {
104                            /**
105                             * TODO: Summarizer is now in service - figure out how to send the info.
106                             * Previously: mSummarizer.addSkippedTest(relativePath);
107                             */
108                        }
109                    }
110                }
111            }
112        }
113    }
114}
115