ManagerService.java revision 5f0ccd76a88586ce85c17cb4db058934e693a4fc
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        return new String(getExpectedResult(relativePath, TEXT_RESULT_EXTENSION));
136    }
137
138    public static byte[] getExpectedImageResult(String relativePath) {
139        return getExpectedResult(relativePath, IMAGE_RESULT_EXTENSION);
140    }
141
142    private static byte[] getExpectedResult(String relativePath, String extension) {
143        relativePath = FileFilter.setPathEnding(relativePath, "-expected." + extension);
144
145        byte[] bytes = FsUtils.readDataFromStorage(new File(TESTS_ROOT_DIR_PATH, relativePath));
146        if (bytes == null) {
147            relativePath = EXPECTED_RESULT_SECONDARY_LOCATION_RELATIVE_DIR_PREFIX + relativePath;
148            bytes = FsUtils.readDataFromStorage(new File(TESTS_ROOT_DIR_PATH, relativePath));
149        }
150
151        return bytes;
152    }
153}