1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
|
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.dumprendertree2;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.util.Log;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* A service that handles managing the results of tests, informing of crashes, generating
* summaries, etc.
*/
public class ManagerService extends Service {
private static final String LOG_TAG = "ManagerService";
private static final int MSG_TEST_CRASHED = 0;
private static final int CRASH_TIMEOUT_MS = 20 * 1000;
/** TODO: make it a setting */
static final String TESTS_ROOT_DIR_PATH =
Environment.getExternalStorageDirectory() +
File.separator + "android" +
File.separator + "LayoutTests";
/** TODO: make it a setting */
static final String RESULTS_ROOT_DIR_PATH =
Environment.getExternalStorageDirectory() +
File.separator + "android" +
File.separator + "LayoutTests-results";
/** TODO: Make it a setting */
private static final List<String> EXPECTED_RESULT_LOCATION_RELATIVE_DIR_PREFIXES =
new ArrayList<String>(3);
{
EXPECTED_RESULT_LOCATION_RELATIVE_DIR_PREFIXES.add("platform" + File.separator +
"android-v8" + File.separator);
EXPECTED_RESULT_LOCATION_RELATIVE_DIR_PREFIXES.add("platform" + File.separator +
"android" + File.separator);
EXPECTED_RESULT_LOCATION_RELATIVE_DIR_PREFIXES.add("");
}
/** TODO: Make these settings */
private static final String TEXT_RESULT_EXTENSION = "txt";
private static final String IMAGE_RESULT_EXTENSION = "png";
static final int MSG_PROCESS_ACTUAL_RESULTS = 0;
static final int MSG_ALL_TESTS_FINISHED = 1;
static final int MSG_FIRST_TEST = 2;
/**
* This handler is purely for IPC. It is used to create mMessenger
* that generates a binder returned in onBind method.
*/
private Handler mIncomingHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MSG_FIRST_TEST:
mSummarizer.reset();
Bundle bundle = msg.getData();
ensureNextTestSetup(bundle.getString("firstTest"), bundle.getInt("index"));
break;
case MSG_PROCESS_ACTUAL_RESULTS:
Log.d(LOG_TAG + ".mIncomingHandler", msg.getData().getString("relativePath"));
onActualResultsObtained(msg.getData());
break;
case MSG_ALL_TESTS_FINISHED:
mSummarizer.setTestsRelativePath(mAllTestsRelativePath);
mSummarizer.summarize();
Intent intent = new Intent(ManagerService.this, TestsListActivity.class);
intent.setAction(Intent.ACTION_SHUTDOWN);
/** This flag is needed because we send the intent from the service */
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
break;
}
}
};
private Messenger mMessenger = new Messenger(mIncomingHandler);
private Handler mCrashMessagesHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == MSG_TEST_CRASHED) {
onTestCrashed();
}
}
};
private FileFilter mFileFilter;
private Summarizer mSummarizer;
private String mCurrentlyRunningTest;
private int mCurrentlyRunningTestIndex;
private String mAllTestsRelativePath;
@Override
public void onCreate() {
super.onCreate();
mFileFilter = new FileFilter(TESTS_ROOT_DIR_PATH);
mSummarizer = new Summarizer(mFileFilter, RESULTS_ROOT_DIR_PATH);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mAllTestsRelativePath = intent.getStringExtra("path");
assert mAllTestsRelativePath != null;
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return mMessenger.getBinder();
}
private void onActualResultsObtained(Bundle bundle) {
mCrashMessagesHandler.removeMessages(MSG_TEST_CRASHED);
ensureNextTestSetup(bundle.getString("nextTest"), bundle.getInt("testIndex") + 1);
AbstractResult results =
AbstractResult.TestType.valueOf(bundle.getString("type")).createResult(bundle);
handleResults(results);
}
private void ensureNextTestSetup(String nextTest, int index) {
if (nextTest == null) {
return;
}
mCurrentlyRunningTest = nextTest;
mCurrentlyRunningTestIndex = index;
mCrashMessagesHandler.sendEmptyMessageDelayed(MSG_TEST_CRASHED, CRASH_TIMEOUT_MS);
}
/**
* This sends an intent to TestsListActivity to restart LayoutTestsExecutor.
* The more detailed description of the flow is in the comment of onNewIntent
* method in TestsListActivity.
*/
private void onTestCrashed() {
handleResults(new CrashedDummyResult(mCurrentlyRunningTest));
Log.w(LOG_TAG + "::onTestCrashed", mCurrentlyRunningTest +
"(" + mCurrentlyRunningTestIndex + ")");
Intent intent = new Intent(this, TestsListActivity.class);
intent.setAction(Intent.ACTION_REBOOT);
/** This flag is needed because we send the intent from the service */
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("crashedTestIndex", mCurrentlyRunningTestIndex);
startActivity(intent);
}
private void handleResults(AbstractResult results) {
String relativePath = results.getRelativePath();
results.setExpectedTextResult(getExpectedTextResult(relativePath));
results.setExpectedImageResult(getExpectedImageResult(relativePath));
dumpActualTextResult(results);
dumpActualImageResult(results);
mSummarizer.appendTest(results);
}
private void dumpActualTextResult(AbstractResult result) {
String testPath = result.getRelativePath();
String actualTextResult = result.getActualTextResult();
if (actualTextResult == null) {
return;
}
String resultPath = FileFilter.setPathEnding(testPath, "-actual." + TEXT_RESULT_EXTENSION);
FsUtils.writeDataToStorage(new File(RESULTS_ROOT_DIR_PATH, resultPath),
actualTextResult.getBytes(), false);
}
private void dumpActualImageResult(AbstractResult result) {
String testPath = result.getRelativePath();
byte[] actualImageResult = result.getActualImageResult();
if (actualImageResult == null) {
return;
}
String resultPath = FileFilter.setPathEnding(testPath,
"-actual." + IMAGE_RESULT_EXTENSION);
FsUtils.writeDataToStorage(new File(RESULTS_ROOT_DIR_PATH, resultPath),
actualImageResult, false);
}
public static String getExpectedTextResult(String relativePath) {
byte[] result = getExpectedResult(relativePath, TEXT_RESULT_EXTENSION);
if (result != null) {
return new String(result);
}
return null;
}
public static byte[] getExpectedImageResult(String relativePath) {
return getExpectedResult(relativePath, IMAGE_RESULT_EXTENSION);
}
private static byte[] getExpectedResult(String relativePath, String extension) {
String originalRelativePath =
FileFilter.setPathEnding(relativePath, "-expected." + extension);
byte[] bytes = null;
List<String> locations = EXPECTED_RESULT_LOCATION_RELATIVE_DIR_PREFIXES;
int size = EXPECTED_RESULT_LOCATION_RELATIVE_DIR_PREFIXES.size();
for (int i = 0; bytes == null && i < size; i++) {
relativePath = locations.get(i) + originalRelativePath;
bytes = FsUtils.readDataFromStorage(new File(TESTS_ROOT_DIR_PATH, relativePath));
}
return bytes;
}
}
|