ManagerService.java revision dd4bff62b54033bedc254f517397ae8f954d0dc9
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; 30 31/** 32 * A service that handles managing the results of tests, informing of crashes, generating 33 * summaries, etc. 34 */ 35public class ManagerService extends Service { 36 37 private static final String LOG_TAG = "ManagerService"; 38 39 /** TODO: make it a setting */ 40 static final String TESTS_ROOT_DIR_PATH = 41 Environment.getExternalStorageDirectory() + 42 File.separator + "android" + 43 File.separator + "LayoutTests"; 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 String EXPECTED_RESULT_SECONDARY_LOCATION_RELATIVE_DIR_PREFIX = 53 "platform" + File.separator + 54 "android-v8" + File.separator; 55 56 /** TODO: Make these settings */ 57 private static final String TEXT_RESULT_EXTENSION = "txt"; 58 private static final String IMAGE_RESULT_EXTENSION = "png"; 59 60 static final int MSG_PROCESS_ACTUAL_RESULTS = 0; 61 static final int MSG_ALL_TESTS_FINISHED = 1; 62 63 private Handler mIncomingHandler = new Handler() { 64 @Override 65 public void handleMessage(Message msg) { 66 switch (msg.what) { 67 case MSG_PROCESS_ACTUAL_RESULTS: 68 Log.d(LOG_TAG + ".mIncomingHandler", msg.getData().getString("relativePath")); 69 onActualResultsObtained(msg.getData()); 70 break; 71 72 case MSG_ALL_TESTS_FINISHED: 73 mSummarizer.summarize(); 74 break; 75 } 76 } 77 }; 78 79 private Messenger mMessenger = new Messenger(mIncomingHandler); 80 81 private FileFilter mFileFilter; 82 private Summarizer mSummarizer; 83 84 @Override 85 public void onCreate() { 86 super.onCreate(); 87 88 mFileFilter = new FileFilter(TESTS_ROOT_DIR_PATH); 89 mSummarizer = new Summarizer(mFileFilter, RESULTS_ROOT_DIR_PATH); 90 } 91 92 @Override 93 public IBinder onBind(Intent intent) { 94 return mMessenger.getBinder(); 95 } 96 97 private void onActualResultsObtained(Bundle bundle) { 98 AbstractResult results = 99 AbstractResult.TestType.valueOf(bundle.getString("type")).createResult(bundle); 100 String relativePath = results.getRelativePath(); 101 results.setExpectedTextResult(getExpectedTextResult(relativePath)); 102 results.setExpectedImageResult(getExpectedImageResult(relativePath)); 103 104 dumpActualTextResult(results); 105 dumpActualImageResult(results); 106 107 mSummarizer.appendTest(results); 108 } 109 110 private void dumpActualTextResult(AbstractResult result) { 111 String testPath = result.getRelativePath(); 112 String actualTextResult = result.getActualTextResult(); 113 if (actualTextResult == null) { 114 return; 115 } 116 117 String resultPath = FileFilter.setPathEnding(testPath, "-actual." + TEXT_RESULT_EXTENSION); 118 FsUtils.writeDataToStorage(new File(RESULTS_ROOT_DIR_PATH, resultPath), 119 actualTextResult.getBytes(), false); 120 } 121 122 private void dumpActualImageResult(AbstractResult result) { 123 String testPath = result.getRelativePath(); 124 byte[] actualImageResult = result.getActualImageResult(); 125 if (actualImageResult == null) { 126 return; 127 } 128 129 String resultPath = FileFilter.setPathEnding(testPath, "-actual." + IMAGE_RESULT_EXTENSION); 130 FsUtils.writeDataToStorage(new File(RESULTS_ROOT_DIR_PATH, resultPath), 131 actualImageResult, false); 132 } 133 134 public static String getExpectedTextResult(String relativePath) { 135 byte[] result = getExpectedResult(relativePath, TEXT_RESULT_EXTENSION); 136 if (result != null) { 137 return new String(result); 138 } 139 return null; 140 } 141 142 public static byte[] getExpectedImageResult(String relativePath) { 143 return getExpectedResult(relativePath, IMAGE_RESULT_EXTENSION); 144 } 145 146 private static byte[] getExpectedResult(String relativePath, String extension) { 147 relativePath = FileFilter.setPathEnding(relativePath, "-expected." + extension); 148 149 byte[] bytes = FsUtils.readDataFromStorage(new File(TESTS_ROOT_DIR_PATH, relativePath)); 150 if (bytes == null) { 151 relativePath = EXPECTED_RESULT_SECONDARY_LOCATION_RELATIVE_DIR_PREFIX + relativePath; 152 bytes = FsUtils.readDataFromStorage(new File(TESTS_ROOT_DIR_PATH, relativePath)); 153 } 154 155 return bytes; 156 } 157}