TextResult.java revision 4ee7f4b19489f4dc9b87e90d1e5c7742cfa7ebe0
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.Handler;
20import android.os.Message;
21import android.webkit.WebView;
22
23/**
24 * A result object for which the expected output is text. It does not have an image
25 * expected result.
26 *
27 * <p>Created if layoutTestController.dumpAsText() was called.
28 */
29public class TextResult extends AbstractResult {
30
31    private static final int MSG_DOCUMENT_AS_TEXT = 0;
32
33    private String mExpectedResult;
34    private String mActualResult;
35    private String mRelativePath;
36    private ResultCode mResultCode;
37    private Message mResultObtainedMsg;
38
39    private Handler mHandler = new Handler() {
40        @Override
41        public void handleMessage(Message msg) {
42            if (msg.what == MSG_DOCUMENT_AS_TEXT) {
43                mActualResult = (String) msg.obj;
44                mResultObtainedMsg.sendToTarget();
45            }
46        }
47    };
48
49    public TextResult(String relativePath) {
50        mRelativePath = relativePath;
51    }
52
53    @Override
54    public ResultCode getResultCode() {
55        if (mResultCode != null) {
56            return mResultCode;
57        }
58
59        if (mExpectedResult == null) {
60            mResultCode = AbstractResult.ResultCode.FAIL_NO_EXPECTED_RESULT;
61        } else if (!mExpectedResult.equals(mActualResult)) {
62            mResultCode = AbstractResult.ResultCode.FAIL_RESULT_DIFFERS;
63        } else {
64            mResultCode = AbstractResult.ResultCode.PASS;
65        }
66
67        return mResultCode;
68    }
69
70    @Override
71    public byte[] getActualImageResult() {
72        return null;
73    }
74
75    @Override
76    public String getActualTextResult() {
77        return mActualResult;
78    }
79
80    @Override
81    public void setExpectedImageResult(byte[] expectedResult) {
82        /** This method is not applicable to this type of result */
83    }
84
85    @Override
86    public void setExpectedTextResult(String expectedResult) {
87        mExpectedResult = expectedResult;
88    }
89
90    @Override
91    public String getDiffAsHtml() {
92        /** TODO: just a stub
93         * Probably needs rethinking anyway - just one table would be much better
94         * This will require some changes in Summarizer in CSS as well */
95        StringBuilder html = new StringBuilder();
96        html.append("<h3>");
97        html.append(mRelativePath);
98        html.append("</h3>");
99        html.append("<table class=\"visual_diff\">");
100
101        html.append("<tr class=\"headers\">");
102        html.append("<td colspan=\"2\">Expected result:</td>");
103        html.append("<td class=\"space\"></td>");
104        html.append("<td colspan=\"2\">Actual result:</td>");
105        html.append("</tr>");
106
107        html.append("<tr class=\"results\">");
108        html.append("<td class=\"line_count\">1:</td>");
109        html.append("<td class=\"line\">");
110        if (mExpectedResult == null) {
111            html.append("NULL");
112        } else {
113            html.append(mExpectedResult.replace("\n", "<br />"));
114        }
115        html.append("</td>");
116        html.append("<td class=\"space\"></td>");
117        html.append("<td class=\"line_count\">1:</td>");
118        html.append("<td class=\"line\">");
119        if (mActualResult == null) {
120            html.append("NULL");
121        } else {
122            html.append(mActualResult.replace("\n", "<br />"));
123        }
124        html.append("</td>");
125        html.append("</tr>");
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
133        html.append("</table>");
134
135        return html.toString();
136    }
137
138    @Override
139    public TestType getType() {
140        return TestType.TEXT;
141    }
142
143    @Override
144    public void obtainActualResult(WebView webview, Message resultObtainedMsg) {
145        mResultObtainedMsg = resultObtainedMsg;
146        Message msg = mHandler.obtainMessage(MSG_DOCUMENT_AS_TEXT);
147
148        /** TODO: mDumpTopFrameAsText and mDumpChildFramesAsText */
149        msg.arg1 = 1;
150        msg.arg2 = 0;
151        webview.documentAsText(msg);
152    }
153}