TestsListActivity.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.app.Activity;
20import android.app.ProgressDialog;
21import android.content.Intent;
22import android.os.Bundle;
23import android.os.Handler;
24import android.os.Message;
25import android.view.Window;
26
27import java.util.ArrayList;
28
29/**
30 * An Activity that generates a list of tests and sends the intent to
31 * LayoutTestsExecuter to run them. It also restarts the LayoutTestsExecuter
32 * after it crashes (TODO).
33 */
34public class TestsListActivity extends Activity {
35
36    private static final int MSG_TEST_LIST_PRELOADER_DONE = 0;
37
38    /** Constants for adding extras to an intent */
39    public static final String EXTRA_TEST_PATH = "TestPath";
40
41    private static ProgressDialog sProgressDialog;
42
43    private Handler mHandler = new Handler() {
44        @Override
45        public void handleMessage(Message msg) {
46            switch (msg.what) {
47                case MSG_TEST_LIST_PRELOADER_DONE:
48                    sProgressDialog.dismiss();
49                    mTestsList = (ArrayList<String>)msg.obj;
50                    mTotalTestCount = mTestsList.size();
51                    restartExecutor(0);
52                    break;
53            }
54        }
55    };
56
57    private ArrayList<String> mTestsList;
58    private int mTotalTestCount;
59
60    @Override
61    protected void onCreate(Bundle savedInstanceState) {
62        super.onCreate(savedInstanceState);
63
64        /** Prepare the progress dialog */
65        sProgressDialog = new ProgressDialog(TestsListActivity.this);
66        sProgressDialog.setCancelable(false);
67        sProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
68        sProgressDialog.setTitle(R.string.dialog_progress_title);
69        sProgressDialog.setMessage(getText(R.string.dialog_progress_msg));
70
71        requestWindowFeature(Window.FEATURE_PROGRESS);
72
73        Intent intent = getIntent();
74        if (!intent.getAction().equals(Intent.ACTION_RUN)) {
75            return;
76        }
77        String path = intent.getStringExtra(EXTRA_TEST_PATH);
78
79        sProgressDialog.show();
80        Message doneMsg = Message.obtain(mHandler, MSG_TEST_LIST_PRELOADER_DONE);
81
82        new TestsListPreloaderThread(path, doneMsg).start();
83    }
84
85    /**
86     * (Re)starts the executer activity from the given test number (inclusive, 0-based).
87     * This number is an index in mTestsList, not the sublist passed in the intent.
88     *
89     * @param startFrom
90     *      test index in mTestsList to start the tests from (inclusive, 0-based)
91     */
92    private void restartExecutor(int startFrom) {
93        Intent intent = new Intent();
94        intent.setClass(this, LayoutTestsExecutor.class);
95        intent.setAction(Intent.ACTION_RUN);
96        intent.putStringArrayListExtra(LayoutTestsExecutor.EXTRA_TESTS_LIST,
97                new ArrayList<String>(mTestsList.subList(startFrom, mTotalTestCount)));
98        intent.putExtra(LayoutTestsExecutor.EXTRA_TEST_INDEX, startFrom);
99        startActivity(intent);
100    }
101}