ManagerService.java revision 2e5982a55ac031110ed39515a76f7a5ec9ff2c14
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_TEST_CRASHED = 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
76    /**
77     * This handler is purely for IPC. It is used to create mMessenger
78     * that generates a binder returned in onBind method.
79     */
80    private Handler mIncomingHandler = new Handler() {
81        @Override
82        public void handleMessage(Message msg) {
83            switch (msg.what) {
84                case MSG_FIRST_TEST:
85                    mSummarizer.reset();
86                    Bundle bundle = msg.getData();
87                    ensureNextTestSetup(bundle.getString("firstTest"), bundle.getInt("index"));
88                    break;
89
90                case MSG_PROCESS_ACTUAL_RESULTS:
91                    Log.d(LOG_TAG,"mIncomingHandler: " + msg.getData().getString("relativePath"));
92                    onActualResultsObtained(msg.getData());
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_TEST_CRASHED) {
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    private String mAllTestsRelativePath;
126
127    @Override
128    public void onCreate() {
129        super.onCreate();
130
131        mFileFilter = new FileFilter(TESTS_ROOT_DIR_PATH);
132        mSummarizer = new Summarizer(mFileFilter, RESULTS_ROOT_DIR_PATH);
133    }
134
135    @Override
136    public int onStartCommand(Intent intent, int flags, int startId) {
137        mAllTestsRelativePath = intent.getStringExtra("path");
138        assert mAllTestsRelativePath != null;
139        return START_STICKY;
140    }
141
142    @Override
143    public IBinder onBind(Intent intent) {
144        return mMessenger.getBinder();
145    }
146
147    private void onActualResultsObtained(Bundle bundle) {
148        mCrashMessagesHandler.removeMessages(MSG_TEST_CRASHED);
149        ensureNextTestSetup(bundle.getString("nextTest"), bundle.getInt("testIndex") + 1);
150
151        AbstractResult results =
152                AbstractResult.TestType.valueOf(bundle.getString("type")).createResult(bundle);
153
154        Log.i(LOG_TAG, "onActualResultObtained: " + results.getRelativePath());
155        handleResults(results);
156    }
157
158    private void ensureNextTestSetup(String nextTest, int index) {
159        if (nextTest == null) {
160            Log.w(LOG_TAG, "ensureNextTestSetup(): nextTest=null");
161            return;
162        }
163
164        mCurrentlyRunningTest = nextTest;
165        mCurrentlyRunningTestIndex = index;
166        mCrashMessagesHandler.sendEmptyMessageDelayed(MSG_TEST_CRASHED, CRASH_TIMEOUT_MS);
167    }
168
169    /**
170     * This sends an intent to TestsListActivity to restart LayoutTestsExecutor.
171     * The more detailed description of the flow is in the comment of onNewIntent
172     * method in TestsListActivity.
173     */
174    private void onTestCrashed() {
175        handleResults(new CrashedDummyResult(mCurrentlyRunningTest));
176
177        Log.w(LOG_TAG, "onTestCrashed(): " + mCurrentlyRunningTest +
178                " (" + mCurrentlyRunningTestIndex + ")");
179
180        Intent intent = new Intent(this, TestsListActivity.class);
181        intent.setAction(Intent.ACTION_REBOOT);
182        /** This flag is needed because we send the intent from the service */
183        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
184        intent.putExtra("crashedTestIndex", mCurrentlyRunningTestIndex);
185        startActivity(intent);
186    }
187
188    private void handleResults(AbstractResult results) {
189        String relativePath = results.getRelativePath();
190        results.setExpectedTextResult(getExpectedTextResult(relativePath));
191        results.setExpectedImageResult(getExpectedImageResult(relativePath));
192
193        dumpActualTextResult(results);
194        dumpActualImageResult(results);
195
196        mSummarizer.appendTest(results);
197    }
198
199    private void dumpActualTextResult(AbstractResult result) {
200        String testPath = result.getRelativePath();
201        String actualTextResult = result.getActualTextResult();
202        if (actualTextResult == null) {
203            return;
204        }
205
206        String resultPath = FileFilter.setPathEnding(testPath, "-actual." + TEXT_RESULT_EXTENSION);
207        FsUtils.writeDataToStorage(new File(RESULTS_ROOT_DIR_PATH, resultPath),
208                actualTextResult.getBytes(), false);
209    }
210
211    private void dumpActualImageResult(AbstractResult result) {
212        String testPath = result.getRelativePath();
213        byte[] actualImageResult = result.getActualImageResult();
214        if (actualImageResult == null) {
215            return;
216        }
217
218        String resultPath = FileFilter.setPathEnding(testPath,
219                "-actual." + IMAGE_RESULT_EXTENSION);
220        FsUtils.writeDataToStorage(new File(RESULTS_ROOT_DIR_PATH, resultPath),
221                actualImageResult, false);
222    }
223
224    public static String getExpectedTextResult(String relativePath) {
225        byte[] result = getExpectedResult(relativePath, TEXT_RESULT_EXTENSION);
226        if (result != null) {
227            return new String(result);
228        }
229        return null;
230    }
231
232    public static byte[] getExpectedImageResult(String relativePath) {
233        return getExpectedResult(relativePath, IMAGE_RESULT_EXTENSION);
234    }
235
236    private static byte[] getExpectedResult(String relativePath, String extension) {
237        String originalRelativePath =
238                FileFilter.setPathEnding(relativePath, "-expected." + extension);
239
240        byte[] bytes = null;
241        List<String> locations = EXPECTED_RESULT_LOCATION_RELATIVE_DIR_PREFIXES;
242
243        int size = EXPECTED_RESULT_LOCATION_RELATIVE_DIR_PREFIXES.size();
244        for (int i = 0; bytes == null && i < size; i++) {
245            relativePath = locations.get(i) + originalRelativePath;
246            bytes = FsUtils.readDataFromStorage(new File(TESTS_ROOT_DIR_PATH, relativePath));
247        }
248
249        return bytes;
250    }
251}