ManagerService.java revision 34c68912be678ad50a70c1bfa54a91444e993df5
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.Service; 20import android.content.Intent; 21import android.os.Bundle; 22import android.os.Environment; 23import android.os.Handler; 24import android.os.IBinder; 25import android.os.Message; 26import android.os.Messenger; 27import android.util.Log; 28 29import java.io.File; 30import java.util.ArrayList; 31import java.util.List; 32 33/** 34 * A service that handles managing the results of tests, informing of crashes, generating 35 * summaries, etc. 36 */ 37public class ManagerService extends Service { 38 39 private static final String LOG_TAG = "ManagerService"; 40 41 private static final int MSG_CRASH_TIMEOUT_EXPIRED = 0; 42 43 private static final int CRASH_TIMEOUT_MS = 20 * 1000; 44 45 /** TODO: make it a setting */ 46 static final String RESULTS_ROOT_DIR_PATH = 47 Environment.getExternalStorageDirectory() + 48 File.separator + "android" + 49 File.separator + "LayoutTests-results"; 50 51 /** TODO: Make it a setting */ 52 private static final List<String> EXPECTED_RESULT_LOCATION_RELATIVE_DIR_PREFIXES = 53 new ArrayList<String>(3); 54 { 55 EXPECTED_RESULT_LOCATION_RELATIVE_DIR_PREFIXES.add("platform" + File.separator + 56 "android-v8" + File.separator); 57 EXPECTED_RESULT_LOCATION_RELATIVE_DIR_PREFIXES.add("platform" + File.separator + 58 "android" + File.separator); 59 EXPECTED_RESULT_LOCATION_RELATIVE_DIR_PREFIXES.add(""); 60 } 61 62 /** TODO: Make these settings */ 63 private static final String TEXT_RESULT_EXTENSION = "txt"; 64 private static final String IMAGE_RESULT_EXTENSION = "png"; 65 66 static final int MSG_PROCESS_ACTUAL_RESULTS = 0; 67 static final int MSG_ALL_TESTS_FINISHED = 1; 68 static final int MSG_FIRST_TEST = 2; 69 static final int MSG_CURRENT_TEST_CRASHED = 3; 70 71 /** 72 * This handler is purely for IPC. It is used to create mMessenger 73 * that generates a binder returned in onBind method. 74 */ 75 private Handler mIncomingHandler = new Handler() { 76 @Override 77 public void handleMessage(Message msg) { 78 switch (msg.what) { 79 case MSG_FIRST_TEST: 80 mSummarizer.reset(); 81 Bundle bundle = msg.getData(); 82 ensureNextTestSetup(bundle.getString("firstTest"), bundle.getInt("index")); 83 break; 84 85 case MSG_PROCESS_ACTUAL_RESULTS: 86 Log.d(LOG_TAG,"mIncomingHandler: " + msg.getData().getString("relativePath")); 87 onActualResultsObtained(msg.getData()); 88 break; 89 90 case MSG_CURRENT_TEST_CRASHED: 91 mCrashMessagesHandler.removeMessages(MSG_CRASH_TIMEOUT_EXPIRED); 92 onTestCrashed(); 93 break; 94 95 case MSG_ALL_TESTS_FINISHED: 96 mSummarizer.setTestsRelativePath(mAllTestsRelativePath); 97 mSummarizer.summarize(); 98 Intent intent = new Intent(ManagerService.this, TestsListActivity.class); 99 intent.setAction(Intent.ACTION_SHUTDOWN); 100 /** This flag is needed because we send the intent from the service */ 101 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 102 startActivity(intent); 103 break; 104 } 105 } 106 }; 107 108 private Messenger mMessenger = new Messenger(mIncomingHandler); 109 110 private Handler mCrashMessagesHandler = new Handler() { 111 @Override 112 public void handleMessage(Message msg) { 113 if (msg.what == MSG_CRASH_TIMEOUT_EXPIRED) { 114 onTestCrashed(); 115 } 116 } 117 }; 118 119 private FileFilter mFileFilter; 120 private Summarizer mSummarizer; 121 122 private String mCurrentlyRunningTest; 123 private int mCurrentlyRunningTestIndex; 124 125 /** 126 * These are implementation details of getExpectedResultPath() used to reduce the number 127 * of requests required to the host server. 128 */ 129 private String mLastExpectedResultPathRequested; 130 private String mLastExpectedResultPathFetched; 131 132 private String mAllTestsRelativePath; 133 134 @Override 135 public void onCreate() { 136 super.onCreate(); 137 138 mFileFilter = new FileFilter(); 139 mSummarizer = new Summarizer(mFileFilter, RESULTS_ROOT_DIR_PATH); 140 } 141 142 @Override 143 public int onStartCommand(Intent intent, int flags, int startId) { 144 mAllTestsRelativePath = intent.getStringExtra("path"); 145 assert mAllTestsRelativePath != null; 146 return START_STICKY; 147 } 148 149 @Override 150 public IBinder onBind(Intent intent) { 151 return mMessenger.getBinder(); 152 } 153 154 private void onActualResultsObtained(Bundle bundle) { 155 mCrashMessagesHandler.removeMessages(MSG_CRASH_TIMEOUT_EXPIRED); 156 ensureNextTestSetup(bundle.getString("nextTest"), bundle.getInt("testIndex") + 1); 157 158 AbstractResult results = 159 AbstractResult.TestType.valueOf(bundle.getString("type")).createResult(bundle); 160 161 Log.i(LOG_TAG, "onActualResultObtained: " + results.getRelativePath()); 162 handleResults(results); 163 } 164 165 private void ensureNextTestSetup(String nextTest, int index) { 166 if (nextTest == null) { 167 Log.w(LOG_TAG, "ensureNextTestSetup(): nextTest=null"); 168 return; 169 } 170 171 mCurrentlyRunningTest = nextTest; 172 mCurrentlyRunningTestIndex = index; 173 mCrashMessagesHandler.sendEmptyMessageDelayed(MSG_CRASH_TIMEOUT_EXPIRED, CRASH_TIMEOUT_MS); 174 } 175 176 /** 177 * This sends an intent to TestsListActivity to restart LayoutTestsExecutor. 178 * The more detailed description of the flow is in the comment of onNewIntent 179 * method in TestsListActivity. 180 */ 181 private void onTestCrashed() { 182 handleResults(new CrashedDummyResult(mCurrentlyRunningTest)); 183 184 Log.w(LOG_TAG, "onTestCrashed(): " + mCurrentlyRunningTest + 185 " (" + mCurrentlyRunningTestIndex + ")"); 186 187 Intent intent = new Intent(this, TestsListActivity.class); 188 intent.setAction(Intent.ACTION_REBOOT); 189 /** This flag is needed because we send the intent from the service */ 190 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 191 intent.putExtra("crashedTestIndex", mCurrentlyRunningTestIndex); 192 startActivity(intent); 193 } 194 195 private void handleResults(AbstractResult results) { 196 String relativePath = results.getRelativePath(); 197 results.setExpectedTextResult(getExpectedTextResult(relativePath)); 198 results.setExpectedTextResultPath(getExpectedTextResultPath(relativePath)); 199 results.setExpectedImageResult(getExpectedImageResult(relativePath)); 200 results.setExpectedImageResultPath(getExpectedImageResultPath(relativePath)); 201 202 dumpActualTextResult(results); 203 dumpActualImageResult(results); 204 205 mSummarizer.appendTest(results); 206 } 207 208 private void dumpActualTextResult(AbstractResult result) { 209 String testPath = result.getRelativePath(); 210 String actualTextResult = result.getActualTextResult(); 211 if (actualTextResult == null) { 212 return; 213 } 214 215 String resultPath = FileFilter.setPathEnding(testPath, "-actual." + TEXT_RESULT_EXTENSION); 216 FsUtils.writeDataToStorage(new File(RESULTS_ROOT_DIR_PATH, resultPath), 217 actualTextResult.getBytes(), false); 218 } 219 220 private void dumpActualImageResult(AbstractResult result) { 221 String testPath = result.getRelativePath(); 222 byte[] actualImageResult = result.getActualImageResult(); 223 if (actualImageResult == null) { 224 return; 225 } 226 227 String resultPath = FileFilter.setPathEnding(testPath, 228 "-actual." + IMAGE_RESULT_EXTENSION); 229 FsUtils.writeDataToStorage(new File(RESULTS_ROOT_DIR_PATH, resultPath), 230 actualImageResult, false); 231 } 232 233 public String getExpectedTextResult(String relativePath) { 234 byte[] result = getExpectedResult(relativePath, TEXT_RESULT_EXTENSION); 235 if (result != null) { 236 return new String(result); 237 } 238 return null; 239 } 240 241 public byte[] getExpectedImageResult(String relativePath) { 242 return getExpectedResult(relativePath, IMAGE_RESULT_EXTENSION); 243 } 244 245 private byte[] getExpectedResult(String relativePath, String extension) { 246 String originalRelativePath = 247 FileFilter.setPathEnding(relativePath, "-expected." + extension); 248 mLastExpectedResultPathRequested = originalRelativePath; 249 250 byte[] bytes = null; 251 List<String> locations = EXPECTED_RESULT_LOCATION_RELATIVE_DIR_PREFIXES; 252 253 int size = EXPECTED_RESULT_LOCATION_RELATIVE_DIR_PREFIXES.size(); 254 for (int i = 0; bytes == null && i < size; i++) { 255 relativePath = locations.get(i) + originalRelativePath; 256 bytes = FsUtils.readDataFromUrl(FileFilter.getUrl(relativePath)); 257 } 258 259 mLastExpectedResultPathFetched = relativePath; 260 return bytes; 261 } 262 263 private String getExpectedTextResultPath(String relativePath) { 264 return getExpectedResultPath(relativePath, TEXT_RESULT_EXTENSION); 265 } 266 267 private String getExpectedImageResultPath(String relativePath) { 268 return getExpectedResultPath(relativePath, IMAGE_RESULT_EXTENSION); 269 } 270 271 private String getExpectedResultPath(String relativePath, String extension) { 272 String originalRelativePath = 273 FileFilter.setPathEnding(relativePath, "-expected." + extension); 274 if (!originalRelativePath.equals(mLastExpectedResultPathRequested)) { 275 getExpectedResult(relativePath, extension); 276 } 277 278 return mLastExpectedResultPathFetched; 279 } 280} 281