TextResult.java revision 6d0dae6a6534a01ee4c58d4f4ee1bf115c82319c
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.os.Bundle;
20import android.os.Handler;
21import android.os.Message;
22import android.webkit.WebView;
23
24import name.fraser.neil.plaintext.diff_match_patch;
25
26import java.util.LinkedList;
27
28/**
29 * A result object for which the expected output is text. It does not have an image
30 * expected result.
31 *
32 * <p>Created if layoutTestController.dumpAsText() was called.
33 */
34public class TextResult extends AbstractResult {
35
36    private static final int MSG_DOCUMENT_AS_TEXT = 0;
37
38    private String mExpectedResult;
39    private String mActualResult;
40    private String mRelativePath;
41    private ResultCode mResultCode;
42    private Message mResultObtainedMsg;
43
44    private Handler mHandler = new Handler() {
45        @Override
46        public void handleMessage(Message msg) {
47            if (msg.what == MSG_DOCUMENT_AS_TEXT) {
48                mActualResult = (String) msg.obj;
49                mResultObtainedMsg.sendToTarget();
50            }
51        }
52    };
53
54    public TextResult(String relativePath) {
55        mRelativePath = relativePath;
56    }
57
58    /**
59     * Used to recreate the Result when received by the service.
60     *
61     * @param bundle
62     *      bundle with data used to recreate the result
63     */
64    public TextResult(Bundle bundle) {
65        mExpectedResult = bundle.getString("expectedTextualResult");
66        mActualResult = bundle.getString("actualTextualResult");
67        mRelativePath = bundle.getString("relativePath");
68        String resultCode = bundle.getString("resultCode");
69        if (resultCode != null) {
70            mResultCode = ResultCode.valueOf(resultCode);
71        }
72    }
73
74    @Override
75    public ResultCode getResultCode() {
76        if (mResultCode != null) {
77            return mResultCode;
78        }
79
80        if (mExpectedResult == null) {
81            mResultCode = AbstractResult.ResultCode.FAIL_NO_EXPECTED_RESULT;
82        } else if (!mExpectedResult.equals(mActualResult)) {
83            mResultCode = AbstractResult.ResultCode.FAIL_RESULT_DIFFERS;
84        } else {
85            mResultCode = AbstractResult.ResultCode.PASS;
86        }
87
88        return mResultCode;
89    }
90
91    @Override
92    public byte[] getActualImageResult() {
93        return null;
94    }
95
96    @Override
97    public String getActualTextResult() {
98        return mActualResult;
99    }
100
101    @Override
102    public void setExpectedImageResult(byte[] expectedResult) {
103        /** This method is not applicable to this type of result */
104    }
105
106    @Override
107    public void setExpectedTextResult(String expectedResult) {
108        mExpectedResult = expectedResult;
109    }
110
111    @Override
112    public String getDiffAsHtml() {
113        StringBuilder html = new StringBuilder();
114        html.append("<table class=\"visual_diff\">");
115        html.append("    <tr class=\"headers\">");
116        html.append("        <td colspan=\"2\">Expected result:</td>");
117        html.append("        <td class=\"space\"></td>");
118        html.append("        <td colspan=\"2\">Actual result:</td>");
119        html.append("    </tr>");
120
121        if (mExpectedResult == null || mActualResult == null) {
122            appendNullsHtml(html);
123        } else {
124            appendDiffHtml(html);
125        }
126
127        html.append("    <tr class=\"footers\">");
128        html.append("        <td colspan=\"2\"></td>");
129        html.append("        <td class=\"space\"></td>");
130        html.append("        <td colspan=\"2\"></td>");
131        html.append("    </tr>");
132        html.append("</table>");
133
134        return html.toString();
135    }
136
137    private void appendDiffHtml(StringBuilder html) {
138        LinkedList<diff_match_patch.Diff> diffs =
139                new diff_match_patch().diff_main(mExpectedResult, mActualResult);
140
141        diffs = VisualDiffUtils.splitDiffsOnNewline(diffs);
142
143        LinkedList<String> expectedLines = new LinkedList<String>();
144        LinkedList<Integer> expectedLineNums = new LinkedList<Integer>();
145        LinkedList<String> actualLines = new LinkedList<String>();
146        LinkedList<Integer> actualLineNums = new LinkedList<Integer>();
147
148        VisualDiffUtils.generateExpectedResultLines(diffs, expectedLineNums, expectedLines);
149        VisualDiffUtils.generateActualResultLines(diffs, actualLineNums, actualLines);
150
151        html.append(VisualDiffUtils.getHtml(expectedLineNums, expectedLines,
152                actualLineNums, actualLines));
153    }
154
155    private void appendNullsHtml(StringBuilder html) {
156        /** TODO: Create a separate row for each line of not null result */
157        html.append("    <tr class=\"results\">");
158        html.append("    <td class=\"line_count\">");
159        html.append("    </td>");
160        html.append("    <td class=\"line\">");
161        if (mExpectedResult == null) {
162            html.append("Expected result was NULL");
163        } else {
164            html.append(mExpectedResult.replace("\n", "<br />"));
165        }
166        html.append("        </td>");
167        html.append("        <td class=\"space\"></td>");
168        html.append("    <td class=\"line_count\">");
169        html.append("    </td>");
170        html.append("    <td class=\"line\">");
171        if (mActualResult == null) {
172            html.append("Actual result was NULL");
173        } else {
174            html.append(mActualResult.replace("\n", "<br />"));
175        }
176        html.append("        </td>");
177        html.append("    </tr>");
178    }
179
180    @Override
181    public TestType getType() {
182        return TestType.TEXT;
183    }
184
185    @Override
186    public void obtainActualResults(WebView webview, Message resultObtainedMsg) {
187        mResultObtainedMsg = resultObtainedMsg;
188        Message msg = mHandler.obtainMessage(MSG_DOCUMENT_AS_TEXT);
189
190        /** TODO: mDumpTopFrameAsText and mDumpChildFramesAsText */
191        msg.arg1 = 1;
192        msg.arg2 = 0;
193        webview.documentAsText(msg);
194    }
195
196    @Override
197    public Bundle getBundle() {
198        Bundle bundle = new Bundle();
199        bundle.putString("expectedTextualResult", mExpectedResult);
200        bundle.putString("actualTextualResult", mActualResult);
201        bundle.putString("relativePath", mRelativePath);
202        if (mResultCode != null) {
203            bundle.putString("resultCode", mResultCode.name());
204        }
205        bundle.putString("type", getType().name());
206        return bundle;
207    }
208
209    @Override
210    public String getRelativePath() {
211        return mRelativePath;
212    }
213}