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