1/*
2 * Copyright (C) 2007 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 android.content.Intent;
20import android.os.Bundle;
21import android.os.Environment;
22import android.util.Log;
23
24import java.io.BufferedOutputStream;
25import java.io.File;
26import java.io.FileOutputStream;
27
28public class Menu extends FileList {
29
30    private static final int MENU_START = 0x01;
31    private static String LOGTAG = "MenuActivity";
32    static final String LAYOUT_TESTS_LIST_FILE =
33        Environment.getExternalStorageDirectory() + "/android/layout_tests_list.txt";
34
35    public void onCreate(Bundle icicle) {
36        super.onCreate(icicle);
37    }
38
39    boolean fileFilter(File f) {
40    	if (f.getName().startsWith("."))
41    		return false;
42    	if (f.getName().equalsIgnoreCase("resources"))
43    		return false;
44    	if (f.isDirectory())
45    		return true;
46    	if (f.getPath().toLowerCase().endsWith("ml"))
47    		return true;
48    	return false;
49    }
50
51    void processFile(String filename, boolean selection) {
52        Intent intent = new Intent(Intent.ACTION_VIEW);
53        intent.setClass(this, TestShellActivity.class);
54        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
55        intent.putExtra(TestShellActivity.TEST_URL, "file://" + filename);
56        intent.putExtra(TestShellActivity.TOTAL_TEST_COUNT, 1);
57        intent.putExtra(TestShellActivity.CURRENT_TEST_NUMBER, 1);
58        startActivity(intent);
59    }
60
61    @Override
62    void processDirectory(String path, boolean selection) {
63        int testCount = generateTestList(path);
64        Intent intent = new Intent(Intent.ACTION_VIEW);
65        intent.setClass(this, TestShellActivity.class);
66        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
67        intent.putExtra(TestShellActivity.UI_AUTO_TEST, LAYOUT_TESTS_LIST_FILE);
68        intent.putExtra(TestShellActivity.TOTAL_TEST_COUNT, testCount);
69        // TestShellActivity will process this intent once and increment the test index
70        // before running the first test, so pass 0 here to allow for that.
71        intent.putExtra(TestShellActivity.CURRENT_TEST_NUMBER, 0);
72        startActivity(intent);
73    }
74
75    private int generateTestList(String path) {
76        int testCount = 0;
77        try {
78            File tests_list = new File(LAYOUT_TESTS_LIST_FILE);
79            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(tests_list, false));
80            testCount = FsUtils.writeLayoutTestListRecursively(
81                    bos, path, false); // Don't ignore results
82            bos.flush();
83            bos.close();
84       } catch (Exception e) {
85           Log.e(LOGTAG, "Error when creating test list: " + e.getMessage());
86       }
87       return testCount;
88    }
89
90}
91