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