TextResult.java revision 7ddc0b7a72aa66d699fecce3d855a6c70f844647
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 24/** 25 * A result object for which the expected output is text. It does not have an image 26 * expected result. 27 * 28 * <p>Created if layoutTestController.dumpAsText() was called. 29 */ 30public class TextResult extends AbstractResult { 31 32 private static final int MSG_DOCUMENT_AS_TEXT = 0; 33 34 private String mExpectedResult; 35 private String mActualResult; 36 private String mRelativePath; 37 private ResultCode mResultCode; 38 private Message mResultObtainedMsg; 39 40 private Handler mHandler = new Handler() { 41 @Override 42 public void handleMessage(Message msg) { 43 if (msg.what == MSG_DOCUMENT_AS_TEXT) { 44 mActualResult = (String) msg.obj; 45 mResultObtainedMsg.sendToTarget(); 46 } 47 } 48 }; 49 50 public TextResult(String relativePath) { 51 mRelativePath = relativePath; 52 } 53 54 /** 55 * Used to recreate the Result when received by the service. 56 * 57 * @param bundle 58 * bundle with data used to recreate the result 59 */ 60 public TextResult(Bundle bundle) { 61 mExpectedResult = bundle.getString("expectedTextualResult"); 62 mActualResult = bundle.getString("actualTextualResult"); 63 mRelativePath = bundle.getString("relativePath"); 64 mResultCode = ResultCode.valueOf(bundle.getString("resultCode")); 65 } 66 67 @Override 68 public ResultCode getResultCode() { 69 if (mResultCode != null) { 70 return mResultCode; 71 } 72 73 if (mExpectedResult == null) { 74 mResultCode = AbstractResult.ResultCode.FAIL_NO_EXPECTED_RESULT; 75 } else if (!mExpectedResult.equals(mActualResult)) { 76 mResultCode = AbstractResult.ResultCode.FAIL_RESULT_DIFFERS; 77 } else { 78 mResultCode = AbstractResult.ResultCode.PASS; 79 } 80 81 return mResultCode; 82 } 83 84 @Override 85 public byte[] getActualImageResult() { 86 return null; 87 } 88 89 @Override 90 public String getActualTextResult() { 91 return mActualResult; 92 } 93 94 @Override 95 public void setExpectedImageResult(byte[] expectedResult) { 96 /** This method is not applicable to this type of result */ 97 } 98 99 @Override 100 public void setExpectedTextResult(String expectedResult) { 101 mExpectedResult = expectedResult; 102 } 103 104 @Override 105 public String getDiffAsHtml() { 106 /** TODO: just a stub 107 * Probably needs rethinking anyway - just one table would be much better 108 * This will require some changes in Summarizer in CSS as well */ 109 StringBuilder html = new StringBuilder(); 110 html.append("<h3>"); 111 html.append(mRelativePath); 112 html.append("</h3>"); 113 html.append("<table class=\"visual_diff\">"); 114 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 html.append("<tr class=\"results\">"); 122 html.append("<td class=\"line_count\">1:</td>"); 123 html.append("<td class=\"line\">"); 124 if (mExpectedResult == null) { 125 html.append("NULL"); 126 } else { 127 html.append(mExpectedResult.replace("\n", "<br />")); 128 } 129 html.append("</td>"); 130 html.append("<td class=\"space\"></td>"); 131 html.append("<td class=\"line_count\">1:</td>"); 132 html.append("<td class=\"line\">"); 133 if (mActualResult == null) { 134 html.append("NULL"); 135 } else { 136 html.append(mActualResult.replace("\n", "<br />")); 137 } 138 html.append("</td>"); 139 html.append("</tr>"); 140 141 html.append("<tr class=\"footers\">"); 142 html.append("<td colspan=\"2\"></td>"); 143 html.append("<td class=\"space\"></td>"); 144 html.append("<td colspan=\"2\"></td>"); 145 html.append("</tr>"); 146 147 html.append("</table>"); 148 149 return html.toString(); 150 } 151 152 @Override 153 public TestType getType() { 154 return TestType.TEXT; 155 } 156 157 @Override 158 public void obtainActualResults(WebView webview, Message resultObtainedMsg) { 159 mResultObtainedMsg = resultObtainedMsg; 160 Message msg = mHandler.obtainMessage(MSG_DOCUMENT_AS_TEXT); 161 162 /** TODO: mDumpTopFrameAsText and mDumpChildFramesAsText */ 163 msg.arg1 = 1; 164 msg.arg2 = 0; 165 webview.documentAsText(msg); 166 } 167 168 @Override 169 public Bundle getBundle() { 170 Bundle bundle = new Bundle(); 171 bundle.putString("expectedTextualResult", mExpectedResult); 172 bundle.putString("actualTextualResult", mActualResult); 173 bundle.putString("relativePath", mRelativePath); 174 if (mResultCode != null) { 175 bundle.putString("resultCode", mResultCode.name()); 176 } 177 bundle.putString("type", getType().name()); 178 return bundle; 179 } 180}