TestsListActivity.java revision 2e64bb7afdd97c954491877306ccb8318f8ec3ce
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 com.android.dumprendertree2.scriptsupport.OnEverythingFinishedCallback;
28
29import java.util.ArrayList;
30
31/**
32 * An Activity that generates a list of tests and sends the intent to
33 * LayoutTestsExecuter to run them. It also restarts the LayoutTestsExecuter
34 * after it crashes.
35 */
36public class TestsListActivity extends Activity {
37
38    private static final int MSG_TEST_LIST_PRELOADER_DONE = 0;
39
40    /** Constants for adding extras to an intent */
41    public static final String EXTRA_TEST_PATH = "TestPath";
42
43    private static ProgressDialog sProgressDialog;
44
45    private Handler mHandler = new Handler() {
46        @Override
47        public void handleMessage(Message msg) {
48            switch (msg.what) {
49                case MSG_TEST_LIST_PRELOADER_DONE:
50                    sProgressDialog.dismiss();
51                    mTestsList = (ArrayList<String>)msg.obj;
52                    mTotalTestCount = mTestsList.size();
53                    restartExecutor(0);
54                    break;
55            }
56        }
57    };
58
59    private ArrayList<String> mTestsList;
60    private int mTotalTestCount;
61
62    private OnEverythingFinishedCallback mOnEverythingFinishedCallback;
63    private boolean mEverythingFinished;
64
65    @Override
66    protected void onCreate(Bundle savedInstanceState) {
67        super.onCreate(savedInstanceState);
68
69        /** Prepare the progress dialog */
70        sProgressDialog = new ProgressDialog(TestsListActivity.this);
71        sProgressDialog.setCancelable(false);
72        sProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
73        sProgressDialog.setTitle(R.string.dialog_progress_title);
74        sProgressDialog.setMessage(getText(R.string.dialog_progress_msg));
75
76        requestWindowFeature(Window.FEATURE_PROGRESS);
77
78        Intent intent = getIntent();
79        if (!intent.getAction().equals(Intent.ACTION_RUN)) {
80            return;
81        }
82        String path = intent.getStringExtra(EXTRA_TEST_PATH);
83
84        sProgressDialog.show();
85        Message doneMsg = Message.obtain(mHandler, MSG_TEST_LIST_PRELOADER_DONE);
86
87        Intent serviceIntent = new Intent(this, ManagerService.class);
88        serviceIntent.putExtra("path", path);
89        startService(serviceIntent);
90
91        new TestsListPreloaderThread(path, doneMsg).start();
92    }
93
94    @Override
95    protected void onNewIntent(Intent intent) {
96        if (intent.getAction().equals(Intent.ACTION_REBOOT)) {
97            onCrashIntent(intent);
98        } else if (intent.getAction().equals(Intent.ACTION_SHUTDOWN)) {
99            onEverythingFinishedIntent(intent);
100        }
101    }
102
103    /**
104     * This method handles an intent that comes from ManageService when crash is detected.
105     * The intent contains an index in mTestsList of the test that crashed. TestsListActivity
106     * restarts the LayoutTestsExecutor from the following test in mTestsList, by sending
107     * an intent to it. This new intent contains a list of remaining tests to run,
108     * total count of all tests, and the index of the first test to run after restarting.
109     * LayoutTestExecutor runs then as usual, sending reports to ManagerService. If it
110     * detects the crash it sends a new intent and the flow repeats.
111     */
112    private void onCrashIntent(Intent intent) {
113        int nextTestToRun = intent.getIntExtra("crashedTestIndex", -1) + 1;
114        if (nextTestToRun > 0 && nextTestToRun <= mTotalTestCount) {
115            restartExecutor(nextTestToRun);
116        }
117    }
118
119    public void registerOnEverythingFinishedCallback(OnEverythingFinishedCallback callback) {
120        mOnEverythingFinishedCallback = callback;
121        if (mEverythingFinished) {
122            mOnEverythingFinishedCallback.onFinished();
123        }
124    }
125
126    private void onEverythingFinishedIntent(Intent intent) {
127        /** TODO: Show some kind of summary to the user */
128        mEverythingFinished = true;
129        if (mOnEverythingFinishedCallback != null) {
130            mOnEverythingFinishedCallback.onFinished();
131        }
132    }
133
134    @Override
135    protected void onSaveInstanceState(Bundle outState) {
136        outState.putStringArrayList("testsList", mTestsList);
137        outState.putInt("totalCount", mTotalTestCount);
138
139        super.onSaveInstanceState(outState);
140    }
141
142    @Override
143    protected void onRestoreInstanceState(Bundle savedInstanceState) {
144        super.onRestoreInstanceState(savedInstanceState);
145
146        mTestsList = savedInstanceState.getStringArrayList("testsList");
147        mTotalTestCount = savedInstanceState.getInt("totalCount");
148    }
149
150    /**
151     * (Re)starts the executer activity from the given test number (inclusive, 0-based).
152     * This number is an index in mTestsList, not the sublist passed in the intent.
153     *
154     * @param startFrom
155     *      test index in mTestsList to start the tests from (inclusive, 0-based)
156     */
157    private void restartExecutor(int startFrom) {
158        Intent intent = new Intent();
159        intent.setClass(this, LayoutTestsExecutor.class);
160        intent.setAction(Intent.ACTION_RUN);
161
162        if (startFrom < mTotalTestCount) {
163            intent.putStringArrayListExtra(LayoutTestsExecutor.EXTRA_TESTS_LIST,
164                    new ArrayList<String>(mTestsList.subList(startFrom, mTotalTestCount)));
165            intent.putExtra(LayoutTestsExecutor.EXTRA_TEST_INDEX, startFrom);
166        } else {
167            intent.putStringArrayListExtra(LayoutTestsExecutor.EXTRA_TESTS_LIST,
168                    new ArrayList<String>());
169        }
170
171        startActivity(intent);
172    }
173}