TextResult.java revision dd4bff62b54033bedc254f517397ae8f954d0dc9
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 boolean mDumpChildFramesAsText;
45
46    private Handler mHandler = new Handler() {
47        @Override
48        public void handleMessage(Message msg) {
49            if (msg.what == MSG_DOCUMENT_AS_TEXT) {
50                mActualResult = (String)msg.obj;
51                mResultObtainedMsg.sendToTarget();
52            }
53        }
54    };
55
56    public TextResult(String relativePath) {
57        mRelativePath = relativePath;
58    }
59
60    public void setDumpChildFramesAsText(boolean dumpChildFramesAsText) {
61        mDumpChildFramesAsText = dumpChildFramesAsText;
62    }
63
64    /**
65     * Used to recreate the Result when received by the service.
66     *
67     * @param bundle
68     *      bundle with data used to recreate the result
69     */
70    public TextResult(Bundle bundle) {
71        mExpectedResult = bundle.getString("expectedTextualResult");
72        mActualResult = bundle.getString("actualTextualResult");
73        setAdditionalTextOutputString(bundle.getString("additionalTextOutputString"));
74        mRelativePath = bundle.getString("relativePath");
75        String resultCode = bundle.getString("resultCode");
76        if (resultCode != null) {
77            mResultCode = ResultCode.valueOf(resultCode);
78        }
79    }
80
81    @Override
82    public ResultCode getResultCode() {
83        if (mResultCode != null) {
84            return mResultCode;
85        }
86
87        if (mExpectedResult == null) {
88            mResultCode = AbstractResult.ResultCode.FAIL_NO_EXPECTED_RESULT;
89        } else if (!mExpectedResult.equals(mActualResult)) {
90            mResultCode = AbstractResult.ResultCode.FAIL_RESULT_DIFFERS;
91        } else {
92            mResultCode = AbstractResult.ResultCode.PASS;
93        }
94
95        return mResultCode;
96    }
97
98    @Override
99    public byte[] getActualImageResult() {
100        return null;
101    }
102
103    @Override
104    public String getActualTextResult() {
105        String additionalTextResultString = getAdditionalTextOutputString();
106        if (additionalTextResultString != null) {
107            return additionalTextResultString+ mActualResult;
108        }
109
110        return mActualResult;
111    }
112
113    @Override
114    public void setExpectedImageResult(byte[] expectedResult) {
115        /** This method is not applicable to this type of result */
116    }
117
118    @Override
119    public void setExpectedTextResult(String expectedResult) {
120        mExpectedResult = expectedResult;
121    }
122
123    @Override
124    public String getDiffAsHtml() {
125        StringBuilder html = new StringBuilder();
126        html.append("<table class=\"visual_diff\">");
127        html.append("    <tr class=\"headers\">");
128        html.append("        <td colspan=\"2\">Expected result:</td>");
129        html.append("        <td class=\"space\"></td>");
130        html.append("        <td colspan=\"2\">Actual result:</td>");
131        html.append("    </tr>");
132
133        if (mExpectedResult == null || mActualResult == null) {
134            appendNullsHtml(html);
135        } else {
136            appendDiffHtml(html);
137        }
138
139        html.append("    <tr class=\"footers\">");
140        html.append("        <td colspan=\"2\"></td>");
141        html.append("        <td class=\"space\"></td>");
142        html.append("        <td colspan=\"2\"></td>");
143        html.append("    </tr>");
144        html.append("</table>");
145
146        return html.toString();
147    }
148
149    private void appendDiffHtml(StringBuilder html) {
150        LinkedList<diff_match_patch.Diff> diffs =
151                new diff_match_patch().diff_main(mExpectedResult, mActualResult);
152
153        diffs = VisualDiffUtils.splitDiffsOnNewline(diffs);
154
155        LinkedList<String> expectedLines = new LinkedList<String>();
156        LinkedList<Integer> expectedLineNums = new LinkedList<Integer>();
157        LinkedList<String> actualLines = new LinkedList<String>();
158        LinkedList<Integer> actualLineNums = new LinkedList<Integer>();
159
160        VisualDiffUtils.generateExpectedResultLines(diffs, expectedLineNums, expectedLines);
161        VisualDiffUtils.generateActualResultLines(diffs, actualLineNums, actualLines);
162
163        html.append(VisualDiffUtils.getHtml(expectedLineNums, expectedLines,
164                actualLineNums, actualLines));
165    }
166
167    private void appendNullsHtml(StringBuilder html) {
168        /** TODO: Create a separate row for each line of not null result */
169        html.append("    <tr class=\"results\">");
170        html.append("    <td class=\"line_count\">");
171        html.append("    </td>");
172        html.append("    <td class=\"line\">");
173        if (mExpectedResult == null) {
174            html.append("Expected result was NULL");
175        } else {
176            html.append(mExpectedResult.replace("\n", "<br />"));
177        }
178        html.append("        </td>");
179        html.append("        <td class=\"space\"></td>");
180        html.append("    <td class=\"line_count\">");
181        html.append("    </td>");
182        html.append("    <td class=\"line\">");
183        if (mActualResult == null) {
184            html.append("Actual result was NULL");
185        } else {
186            html.append(mActualResult.replace("\n", "<br />"));
187        }
188        html.append("        </td>");
189        html.append("    </tr>");
190    }
191
192    @Override
193    public TestType getType() {
194        return TestType.TEXT;
195    }
196
197    @Override
198    public void obtainActualResults(WebView webview, Message resultObtainedMsg) {
199        mResultObtainedMsg = resultObtainedMsg;
200        Message msg = mHandler.obtainMessage(MSG_DOCUMENT_AS_TEXT);
201
202        /**
203         * arg1 - should dump top frame as text
204         * arg2 - should dump child frames as text
205         */
206        msg.arg1 = 1;
207        msg.arg2 = mDumpChildFramesAsText ? 1 : 0;
208        webview.documentAsText(msg);
209    }
210
211    @Override
212    public Bundle getBundle() {
213        Bundle bundle = new Bundle();
214        bundle.putString("expectedTextualResult", mExpectedResult);
215        bundle.putString("actualTextualResult", getActualTextResult());
216        bundle.putString("additionalTextOutputString", getAdditionalTextOutputString());
217        bundle.putString("relativePath", mRelativePath);
218        if (mResultCode != null) {
219            bundle.putString("resultCode", mResultCode.name());
220        }
221        bundle.putString("type", getType().name());
222        return bundle;
223    }
224
225    @Override
226    public String getRelativePath() {
227        return mRelativePath;
228    }
229}