TestsListActivity.java revision c8fb818b947f15d4eb467c229ea43806dd75c01e
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.
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        Intent serviceIntent = new Intent(this, ManagerService.class);
83        startService(serviceIntent);
84
85        new TestsListPreloaderThread(path, doneMsg).start();
86    }
87
88    @Override
89    protected void onNewIntent(Intent intent) {
90        if (intent.getAction().equals(Intent.ACTION_REBOOT)) {
91            onCrashIntent(intent);
92        } else if (intent.getAction().equals(Intent.ACTION_SHUTDOWN)) {
93            onEverythingFinishedIntent(intent);
94        }
95    }
96
97    /**
98     * This method handles an intent that comes from ManageService when crash is detected.
99     * The intent contains an index in mTestsList of the test that crashed. TestsListActivity
100     * restarts the LayoutTestsExecutor from the following test in mTestsList, by sending
101     * an intent to it. This new intent contains a list of remaining tests to run,
102     * total count of all tests, and the index of the first test to run after restarting.
103     * LayoutTestExecutor runs then as usual, sending reports to ManagerService. If it
104     * detects the crash it sends a new intent and the flow repeats.
105     */
106    private void onCrashIntent(Intent intent) {
107        int nextTestToRun = intent.getIntExtra("crashedTestIndex", -1) + 1;
108        if (nextTestToRun > 0 && nextTestToRun <= mTotalTestCount) {
109            restartExecutor(nextTestToRun);
110        }
111    }
112
113    private void onEverythingFinishedIntent(Intent intent) {
114        /** TODO: Show some kind of summary to the user */
115    }
116
117    @Override
118    protected void onSaveInstanceState(Bundle outState) {
119        outState.putStringArrayList("testsList", mTestsList);
120        outState.putInt("totalCount", mTotalTestCount);
121
122        super.onSaveInstanceState(outState);
123    }
124
125    @Override
126    protected void onRestoreInstanceState(Bundle savedInstanceState) {
127        super.onRestoreInstanceState(savedInstanceState);
128
129        mTestsList = savedInstanceState.getStringArrayList("testsList");
130        mTotalTestCount = savedInstanceState.getInt("totalCount");
131    }
132
133    /**
134     * (Re)starts the executer activity from the given test number (inclusive, 0-based).
135     * This number is an index in mTestsList, not the sublist passed in the intent.
136     *
137     * @param startFrom
138     *      test index in mTestsList to start the tests from (inclusive, 0-based)
139     */
140    private void restartExecutor(int startFrom) {
141        Intent intent = new Intent();
142        intent.setClass(this, LayoutTestsExecutor.class);
143        intent.setAction(Intent.ACTION_RUN);
144
145        if (startFrom < mTotalTestCount) {
146            intent.putStringArrayListExtra(LayoutTestsExecutor.EXTRA_TESTS_LIST,
147                    new ArrayList<String>(mTestsList.subList(startFrom, mTotalTestCount)));
148            intent.putExtra(LayoutTestsExecutor.EXTRA_TEST_INDEX, startFrom);
149        } else {
150            intent.putStringArrayListExtra(LayoutTestsExecutor.EXTRA_TESTS_LIST,
151                    new ArrayList<String>());
152        }
153
154        startActivity(intent);
155    }
156}