ManagerService.java revision c83712e68642b9816a561916c6ec044509bd6759
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    /** TODO: make it a setting */
42    static final String TESTS_ROOT_DIR_PATH =
43            Environment.getExternalStorageDirectory() +
44            File.separator + "android" +
45            File.separator + "LayoutTests";
46
47    /** TODO: make it a setting */
48    static final String RESULTS_ROOT_DIR_PATH =
49            Environment.getExternalStorageDirectory() +
50            File.separator + "android" +
51            File.separator + "LayoutTests-results";
52
53    /** TODO: Make it a setting */
54    private static final List<String> EXPECTED_RESULT_LOCATION_RELATIVE_DIR_PREFIXES =
55            new ArrayList<String>(3);
56    {
57        EXPECTED_RESULT_LOCATION_RELATIVE_DIR_PREFIXES.add("platform" + File.separator +
58                "android-v8" + File.separator);
59        EXPECTED_RESULT_LOCATION_RELATIVE_DIR_PREFIXES.add("platform" + File.separator +
60                "android" + File.separator);
61        EXPECTED_RESULT_LOCATION_RELATIVE_DIR_PREFIXES.add("");
62    }
63
64    /** TODO: Make these settings */
65    private static final String TEXT_RESULT_EXTENSION = "txt";
66    private static final String IMAGE_RESULT_EXTENSION = "png";
67
68    static final int MSG_PROCESS_ACTUAL_RESULTS = 0;
69    static final int MSG_ALL_TESTS_FINISHED = 1;
70
71    private Handler mIncomingHandler = new Handler() {
72        @Override
73        public void handleMessage(Message msg) {
74            switch (msg.what) {
75                case MSG_PROCESS_ACTUAL_RESULTS:
76                    Log.d(LOG_TAG + ".mIncomingHandler", msg.getData().getString("relativePath"));
77                    onActualResultsObtained(msg.getData());
78                    break;
79
80                case MSG_ALL_TESTS_FINISHED:
81                    mSummarizer.summarize();
82                    break;
83            }
84        }
85    };
86
87    private Messenger mMessenger = new Messenger(mIncomingHandler);
88
89    private FileFilter mFileFilter;
90    private Summarizer mSummarizer;
91
92    @Override
93    public void onCreate() {
94        super.onCreate();
95
96        mFileFilter = new FileFilter(TESTS_ROOT_DIR_PATH);
97        mSummarizer = new Summarizer(mFileFilter, RESULTS_ROOT_DIR_PATH);
98    }
99
100    @Override
101    public IBinder onBind(Intent intent) {
102        return mMessenger.getBinder();
103    }
104
105    private void onActualResultsObtained(Bundle bundle) {
106        AbstractResult results =
107                AbstractResult.TestType.valueOf(bundle.getString("type")).createResult(bundle);
108        String relativePath = results.getRelativePath();
109        results.setExpectedTextResult(getExpectedTextResult(relativePath));
110        results.setExpectedImageResult(getExpectedImageResult(relativePath));
111
112        dumpActualTextResult(results);
113        dumpActualImageResult(results);
114
115        mSummarizer.appendTest(results);
116    }
117
118    private void dumpActualTextResult(AbstractResult result) {
119        String testPath = result.getRelativePath();
120        String actualTextResult = result.getActualTextResult();
121        if (actualTextResult == null) {
122            return;
123        }
124
125        String resultPath = FileFilter.setPathEnding(testPath, "-actual." + TEXT_RESULT_EXTENSION);
126        FsUtils.writeDataToStorage(new File(RESULTS_ROOT_DIR_PATH, resultPath),
127                actualTextResult.getBytes(), false);
128    }
129
130    private void dumpActualImageResult(AbstractResult result) {
131        String testPath = result.getRelativePath();
132        byte[] actualImageResult = result.getActualImageResult();
133        if (actualImageResult == null) {
134            return;
135        }
136
137        String resultPath = FileFilter.setPathEnding(testPath, "-actual." + IMAGE_RESULT_EXTENSION);
138        FsUtils.writeDataToStorage(new File(RESULTS_ROOT_DIR_PATH, resultPath),
139                actualImageResult, false);
140    }
141
142    public static String getExpectedTextResult(String relativePath) {
143        byte[] result = getExpectedResult(relativePath, TEXT_RESULT_EXTENSION);
144        if (result != null) {
145            return new String(result);
146        }
147        return null;
148    }
149
150    public static byte[] getExpectedImageResult(String relativePath) {
151        return getExpectedResult(relativePath, IMAGE_RESULT_EXTENSION);
152    }
153
154    private static byte[] getExpectedResult(String relativePath, String extension) {
155        String originalRelativePath =
156                FileFilter.setPathEnding(relativePath, "-expected." + extension);
157
158        byte[] bytes = null;
159        List<String> locations = EXPECTED_RESULT_LOCATION_RELATIVE_DIR_PREFIXES;
160
161        int size = EXPECTED_RESULT_LOCATION_RELATIVE_DIR_PREFIXES.size();
162        for (int i = 0; bytes == null && i < size; i++) {
163            relativePath = locations.get(i) + originalRelativePath;
164            bytes = FsUtils.readDataFromStorage(new File(TESTS_ROOT_DIR_PATH, relativePath));
165        }
166
167        return bytes;
168    }
169}