The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | |
| 17 | package com.android.dumprendertree; |
| 18 | |
Guang Zhu | 6bf18ba | 2009-09-15 23:47:20 -0700 | [diff] [blame] | 19 | import com.android.dumprendertree.forwarder.ForwardService; |
| 20 | |
Guang Zhu | 1871fe0 | 2009-04-29 14:49:03 -0700 | [diff] [blame] | 21 | import android.app.Activity; |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 22 | import android.app.AlertDialog; |
Andrei Popescu | 4950b2b | 2009-09-03 13:56:07 +0100 | [diff] [blame] | 23 | import android.content.Context; |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 24 | import android.content.DialogInterface; |
Guang Zhu | 1871fe0 | 2009-04-29 14:49:03 -0700 | [diff] [blame] | 25 | import android.content.Intent; |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 26 | import android.content.DialogInterface.OnClickListener; |
Guang Zhu | 1871fe0 | 2009-04-29 14:49:03 -0700 | [diff] [blame] | 27 | import android.graphics.Bitmap; |
Guang Zhu | 5dc4f21 | 2009-10-29 18:24:54 -0700 | [diff] [blame] | 28 | import android.graphics.Canvas; |
| 29 | import android.graphics.Bitmap.CompressFormat; |
| 30 | import android.graphics.Bitmap.Config; |
Guang Zhu | 1871fe0 | 2009-04-29 14:49:03 -0700 | [diff] [blame] | 31 | import android.net.http.SslError; |
| 32 | import android.os.Bundle; |
Christian Mehlmauer | 8b85dce | 2010-07-19 20:11:27 +0200 | [diff] [blame] | 33 | import android.os.Environment; |
Guang Zhu | 1871fe0 | 2009-04-29 14:49:03 -0700 | [diff] [blame] | 34 | import android.os.Handler; |
| 35 | import android.os.Message; |
| 36 | import android.util.Log; |
| 37 | import android.view.ViewGroup; |
Ben Murdoch | 9b815d0 | 2010-06-01 18:45:10 +0100 | [diff] [blame] | 38 | import android.view.Window; |
Kristian Monsen | 16d041c | 2010-12-06 13:43:51 +0000 | [diff] [blame] | 39 | import android.webkit.CookieManager; |
Guang Zhu | 6c15f60 | 2010-05-03 11:49:04 -0700 | [diff] [blame] | 40 | import android.webkit.ConsoleMessage; |
Ben Murdoch | 7e73ce9 | 2011-01-20 11:21:24 +0000 | [diff] [blame] | 41 | import android.webkit.CookieManager; |
Steve Block | dad347c | 2009-08-19 18:46:34 +0100 | [diff] [blame] | 42 | import android.webkit.GeolocationPermissions; |
Guang Zhu | 1871fe0 | 2009-04-29 14:49:03 -0700 | [diff] [blame] | 43 | import android.webkit.HttpAuthHandler; |
| 44 | import android.webkit.JsPromptResult; |
| 45 | import android.webkit.JsResult; |
| 46 | import android.webkit.SslErrorHandler; |
| 47 | import android.webkit.WebChromeClient; |
| 48 | import android.webkit.WebSettings; |
Ben Murdoch | e6f3e45 | 2009-04-22 16:02:31 +0100 | [diff] [blame] | 49 | import android.webkit.WebStorage; |
Guang Zhu | 1871fe0 | 2009-04-29 14:49:03 -0700 | [diff] [blame] | 50 | import android.webkit.WebView; |
| 51 | import android.webkit.WebViewClient; |
| 52 | import android.widget.LinearLayout; |
| 53 | |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 54 | import java.io.BufferedReader; |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 55 | import java.io.File; |
| 56 | import java.io.FileOutputStream; |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 57 | import java.io.FileReader; |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 58 | import java.io.IOException; |
Ben Murdoch | e6f3e45 | 2009-04-22 16:02:31 +0100 | [diff] [blame] | 59 | import java.net.MalformedURLException; |
| 60 | import java.net.URL; |
Andrei Popescu | 4950b2b | 2009-09-03 13:56:07 +0100 | [diff] [blame] | 61 | import java.util.HashMap; |
Steve Block | f0f30c6 | 2010-08-18 15:15:42 +0100 | [diff] [blame] | 62 | import java.util.Iterator; |
Andrei Popescu | 4950b2b | 2009-09-03 13:56:07 +0100 | [diff] [blame] | 63 | import java.util.Map; |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 64 | import java.util.Vector; |
| 65 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 66 | public class TestShellActivity extends Activity implements LayoutTestController { |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 67 | |
Guang Zhu | 1871fe0 | 2009-04-29 14:49:03 -0700 | [diff] [blame] | 68 | static enum DumpDataType {DUMP_AS_TEXT, EXT_REPR, NO_OP} |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 69 | |
Steve Block | fb0de34 | 2010-03-19 18:48:35 +0000 | [diff] [blame] | 70 | // String constants for use with layoutTestController.overridePreferences |
Steve Block | 5f37cc43 | 2010-10-13 18:44:31 +0100 | [diff] [blame] | 71 | private final String WEBKIT_OFFLINE_WEB_APPLICATION_CACHE_ENABLED = |
| 72 | "WebKitOfflineWebApplicationCacheEnabled"; |
| 73 | private final String WEBKIT_USES_PAGE_CACHE_PREFERENCE_KEY = "WebKitUsesPageCachePreferenceKey"; |
Steve Block | fb0de34 | 2010-03-19 18:48:35 +0000 | [diff] [blame] | 74 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 75 | public class AsyncHandler extends Handler { |
| 76 | @Override |
| 77 | public void handleMessage(Message msg) { |
| 78 | if (msg.what == MSG_TIMEOUT) { |
| 79 | mTimedOut = true; |
Guang Zhu | 4ce6e7c | 2011-01-14 16:17:20 -0800 | [diff] [blame] | 80 | mWebView.stopLoading(); |
Guang Zhu | 56a3693 | 2009-09-28 09:53:48 -0700 | [diff] [blame] | 81 | if (mCallback != null) |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 82 | mCallback.timedOut(mWebView.getUrl()); |
Guang Zhu | 56a3693 | 2009-09-28 09:53:48 -0700 | [diff] [blame] | 83 | if (!mRequestedWebKitData) { |
Guang Zhu | 7216047 | 2009-09-25 17:34:12 -0700 | [diff] [blame] | 84 | requestWebKitData(); |
| 85 | } else { |
| 86 | // if timed out and webkit data has been dumped before |
| 87 | // finish directly |
| 88 | finished(); |
| 89 | } |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 90 | return; |
| 91 | } else if (msg.what == MSG_WEBKIT_DATA) { |
Guang Zhu | f2f7f56 | 2010-12-23 16:25:47 -0800 | [diff] [blame] | 92 | mHandler.removeMessages(MSG_DUMP_TIMEOUT); |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 93 | TestShellActivity.this.dump(mTimedOut, (String)msg.obj); |
| 94 | return; |
Guang Zhu | f37a574 | 2010-12-23 14:37:31 -0800 | [diff] [blame] | 95 | } else if (msg.what == MSG_DUMP_TIMEOUT) { |
| 96 | throw new RuntimeException("WebView dump timeout, is it pegged?"); |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 97 | } |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 98 | super.handleMessage(msg); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | public void requestWebKitData() { |
| 103 | Message callback = mHandler.obtainMessage(MSG_WEBKIT_DATA); |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 104 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 105 | if (mRequestedWebKitData) |
| 106 | throw new AssertionError("Requested webkit data twice: " + mWebView.getUrl()); |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 107 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 108 | mRequestedWebKitData = true; |
Guang Zhu | 7216047 | 2009-09-25 17:34:12 -0700 | [diff] [blame] | 109 | Log.v(LOGTAG, "message sent to WebView to dump text."); |
Guang Zhu | 1871fe0 | 2009-04-29 14:49:03 -0700 | [diff] [blame] | 110 | switch (mDumpDataType) { |
| 111 | case DUMP_AS_TEXT: |
Shimeng (Simon) Wang | ad45686 | 2010-06-16 17:32:24 -0700 | [diff] [blame] | 112 | callback.arg1 = mDumpTopFrameAsText ? 1 : 0; |
| 113 | callback.arg2 = mDumpChildFramesAsText ? 1 : 0; |
Guang Zhu | f37a574 | 2010-12-23 14:37:31 -0800 | [diff] [blame] | 114 | setDumpTimeout(DUMP_TIMEOUT_MS); |
Guang Zhu | 1871fe0 | 2009-04-29 14:49:03 -0700 | [diff] [blame] | 115 | mWebView.documentAsText(callback); |
| 116 | break; |
| 117 | case EXT_REPR: |
| 118 | mWebView.externalRepresentation(callback); |
Guang Zhu | f37a574 | 2010-12-23 14:37:31 -0800 | [diff] [blame] | 119 | setDumpTimeout(DUMP_TIMEOUT_MS); |
Guang Zhu | 1871fe0 | 2009-04-29 14:49:03 -0700 | [diff] [blame] | 120 | break; |
| 121 | default: |
| 122 | finished(); |
| 123 | break; |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 124 | } |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 125 | } |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 126 | |
Guang Zhu | f37a574 | 2010-12-23 14:37:31 -0800 | [diff] [blame] | 127 | private void setDumpTimeout(long timeout) { |
| 128 | Message msg = mHandler.obtainMessage(MSG_DUMP_TIMEOUT); |
| 129 | mHandler.sendMessageDelayed(msg, timeout); |
| 130 | } |
| 131 | |
Guang Zhu | 889181d | 2009-07-09 12:55:15 -0700 | [diff] [blame] | 132 | public void clearCache() { |
Guang Zhu | f56e1a1 | 2009-07-20 11:16:28 -0700 | [diff] [blame] | 133 | mWebView.freeMemory(); |
Guang Zhu | 889181d | 2009-07-09 12:55:15 -0700 | [diff] [blame] | 134 | } |
| 135 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 136 | @Override |
| 137 | protected void onCreate(Bundle icicle) { |
| 138 | super.onCreate(icicle); |
Ben Murdoch | 9b815d0 | 2010-06-01 18:45:10 +0100 | [diff] [blame] | 139 | requestWindowFeature(Window.FEATURE_PROGRESS); |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 140 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 141 | LinearLayout contentView = new LinearLayout(this); |
| 142 | contentView.setOrientation(LinearLayout.VERTICAL); |
| 143 | setContentView(contentView); |
| 144 | |
Kristian Monsen | 16d041c | 2010-12-06 13:43:51 +0000 | [diff] [blame] | 145 | CookieManager.setAcceptFileSchemeCookies(true); |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 146 | mWebView = new WebView(this); |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 147 | mEventSender = new WebViewEventSender(mWebView); |
| 148 | mCallbackProxy = new CallbackProxy(mEventSender, this); |
| 149 | |
Andrei Popescu | 4950b2b | 2009-09-03 13:56:07 +0100 | [diff] [blame] | 150 | mWebView.addJavascriptInterface(mCallbackProxy, "layoutTestController"); |
| 151 | mWebView.addJavascriptInterface(mCallbackProxy, "eventSender"); |
Ben Murdoch | e6f3e45 | 2009-04-22 16:02:31 +0100 | [diff] [blame] | 152 | setupWebViewForLayoutTests(mWebView, mCallbackProxy); |
| 153 | |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 154 | contentView.addView(mWebView, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 0.0f)); |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 155 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 156 | mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL); |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 157 | |
Feng Qian | 2890920 | 2009-08-13 14:54:56 -0700 | [diff] [blame] | 158 | // Expose window.gc function to JavaScript. JSC build exposes |
| 159 | // this function by default, but V8 requires the flag to turn it on. |
| 160 | // WebView::setJsFlags is noop in JSC build. |
| 161 | mWebView.setJsFlags("--expose_gc"); |
| 162 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 163 | mHandler = new AsyncHandler(); |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 164 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 165 | Intent intent = getIntent(); |
| 166 | if (intent != null) { |
| 167 | executeIntent(intent); |
| 168 | } |
Steve Block | 68dede3 | 2010-08-04 10:28:46 +0100 | [diff] [blame] | 169 | |
| 170 | // This is asynchronous, but it gets processed by WebCore before it starts loading pages. |
| 171 | mWebView.useMockDeviceOrientation(); |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 172 | } |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 173 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 174 | @Override |
| 175 | protected void onNewIntent(Intent intent) { |
| 176 | super.onNewIntent(intent); |
| 177 | executeIntent(intent); |
| 178 | } |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 179 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 180 | private void executeIntent(Intent intent) { |
| 181 | resetTestStatus(); |
| 182 | if (!Intent.ACTION_VIEW.equals(intent.getAction())) { |
| 183 | return; |
| 184 | } |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 185 | |
Ben Murdoch | 9b815d0 | 2010-06-01 18:45:10 +0100 | [diff] [blame] | 186 | mTotalTestCount = intent.getIntExtra(TOTAL_TEST_COUNT, mTotalTestCount); |
| 187 | mCurrentTestNumber = intent.getIntExtra(CURRENT_TEST_NUMBER, mCurrentTestNumber); |
| 188 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 189 | mTestUrl = intent.getStringExtra(TEST_URL); |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 190 | if (mTestUrl == null) { |
| 191 | mUiAutoTestPath = intent.getStringExtra(UI_AUTO_TEST); |
| 192 | if(mUiAutoTestPath != null) { |
| 193 | beginUiAutoTest(); |
| 194 | } |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 195 | return; |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 196 | } |
| 197 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 198 | mResultFile = intent.getStringExtra(RESULT_FILE); |
| 199 | mTimeoutInMillis = intent.getIntExtra(TIMEOUT_IN_MILLIS, 0); |
Guang Zhu | 5dc4f21 | 2009-10-29 18:24:54 -0700 | [diff] [blame] | 200 | mGetDrawtime = intent.getBooleanExtra(GET_DRAW_TIME, false); |
| 201 | mSaveImagePath = intent.getStringExtra(SAVE_IMAGE); |
Guang Zhu | 5d53c19 | 2010-07-14 17:17:54 -0700 | [diff] [blame] | 202 | mStopOnRefError = intent.getBooleanExtra(STOP_ON_REF_ERROR, false); |
Ben Murdoch | 9b815d0 | 2010-06-01 18:45:10 +0100 | [diff] [blame] | 203 | setTitle("Test " + mCurrentTestNumber + " of " + mTotalTestCount); |
| 204 | float ratio = (float)mCurrentTestNumber / mTotalTestCount; |
| 205 | int progress = (int)(ratio * Window.PROGRESS_END); |
| 206 | getWindow().setFeatureInt(Window.FEATURE_PROGRESS, progress); |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 207 | |
| 208 | Log.v(LOGTAG, " Loading " + mTestUrl); |
| 209 | mWebView.loadUrl(mTestUrl); |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 210 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 211 | if (mTimeoutInMillis > 0) { |
| 212 | // Create a timeout timer |
| 213 | Message m = mHandler.obtainMessage(MSG_TIMEOUT); |
| 214 | mHandler.sendMessageDelayed(m, mTimeoutInMillis); |
| 215 | } |
| 216 | } |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 217 | |
| 218 | private void beginUiAutoTest() { |
| 219 | try { |
| 220 | mTestListReader = new BufferedReader( |
| 221 | new FileReader(mUiAutoTestPath)); |
| 222 | } catch (IOException ioe) { |
| 223 | Log.e(LOGTAG, "Failed to open test list for read.", ioe); |
| 224 | finishUiAutoTest(); |
| 225 | return; |
| 226 | } |
| 227 | moveToNextTest(); |
| 228 | } |
| 229 | |
| 230 | private void finishUiAutoTest() { |
| 231 | try { |
| 232 | if(mTestListReader != null) |
| 233 | mTestListReader.close(); |
| 234 | } catch (IOException ioe) { |
| 235 | Log.w(LOGTAG, "Failed to close test list file.", ioe); |
| 236 | } |
Guang Zhu | 6bf18ba | 2009-09-15 23:47:20 -0700 | [diff] [blame] | 237 | ForwardService.getForwardService().stopForwardService(); |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 238 | finished(); |
| 239 | } |
| 240 | |
| 241 | private void moveToNextTest() { |
| 242 | String url = null; |
| 243 | try { |
| 244 | url = mTestListReader.readLine(); |
| 245 | } catch (IOException ioe) { |
| 246 | Log.e(LOGTAG, "Failed to read next test.", ioe); |
| 247 | finishUiAutoTest(); |
| 248 | return; |
| 249 | } |
| 250 | if (url == null) { |
| 251 | mUiAutoTestPath = null; |
| 252 | finishUiAutoTest(); |
| 253 | AlertDialog.Builder builder = new AlertDialog.Builder(this); |
| 254 | builder.setMessage("All tests finished. Exit?") |
| 255 | .setCancelable(false) |
| 256 | .setPositiveButton("Yes", new OnClickListener(){ |
| 257 | public void onClick(DialogInterface dialog, int which) { |
| 258 | TestShellActivity.this.finish(); |
| 259 | } |
| 260 | }) |
| 261 | .setNegativeButton("No", new OnClickListener(){ |
| 262 | public void onClick(DialogInterface dialog, int which) { |
| 263 | dialog.cancel(); |
| 264 | } |
| 265 | }); |
| 266 | builder.create().show(); |
| 267 | return; |
| 268 | } |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 269 | Intent intent = new Intent(Intent.ACTION_VIEW); |
| 270 | intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); |
Guang Zhu | 6bf18ba | 2009-09-15 23:47:20 -0700 | [diff] [blame] | 271 | intent.putExtra(TestShellActivity.TEST_URL, FsUtils.getTestUrl(url)); |
Ben Murdoch | 9b815d0 | 2010-06-01 18:45:10 +0100 | [diff] [blame] | 272 | intent.putExtra(TestShellActivity.CURRENT_TEST_NUMBER, ++mCurrentTestNumber); |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 273 | intent.putExtra(TIMEOUT_IN_MILLIS, 10000); |
| 274 | executeIntent(intent); |
| 275 | } |
| 276 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 277 | @Override |
| 278 | protected void onStop() { |
| 279 | super.onStop(); |
| 280 | mWebView.stopLoading(); |
| 281 | } |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 282 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 283 | @Override |
| 284 | protected void onDestroy() { |
| 285 | super.onDestroy(); |
| 286 | mWebView.destroy(); |
| 287 | mWebView = null; |
| 288 | } |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 289 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 290 | @Override |
| 291 | public void onLowMemory() { |
| 292 | super.onLowMemory(); |
Feng Qian | 0aeed6a | 2009-07-16 20:53:31 -0700 | [diff] [blame] | 293 | Log.e(LOGTAG, "Low memory, clearing caches"); |
| 294 | mWebView.freeMemory(); |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | // Dump the page |
| 298 | public void dump(boolean timeout, String webkitData) { |
Ben Murdoch | 2eea5f3 | 2009-09-23 15:28:57 +0100 | [diff] [blame] | 299 | mDumpWebKitData = true; |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 300 | if (mResultFile == null || mResultFile.length() == 0) { |
| 301 | finished(); |
| 302 | return; |
| 303 | } |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 304 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 305 | try { |
| 306 | File parentDir = new File(mResultFile).getParentFile(); |
| 307 | if (!parentDir.exists()) { |
| 308 | parentDir.mkdirs(); |
| 309 | } |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 310 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 311 | FileOutputStream os = new FileOutputStream(mResultFile); |
| 312 | if (timeout) { |
| 313 | Log.w("Layout test: Timeout", mResultFile); |
| 314 | os.write(TIMEOUT_STR.getBytes()); |
| 315 | os.write('\n'); |
| 316 | } |
| 317 | if (mDumpTitleChanges) |
| 318 | os.write(mTitleChanges.toString().getBytes()); |
| 319 | if (mDialogStrings != null) |
| 320 | os.write(mDialogStrings.toString().getBytes()); |
| 321 | mDialogStrings = null; |
Ben Murdoch | e6f3e45 | 2009-04-22 16:02:31 +0100 | [diff] [blame] | 322 | if (mDatabaseCallbackStrings != null) |
| 323 | os.write(mDatabaseCallbackStrings.toString().getBytes()); |
| 324 | mDatabaseCallbackStrings = null; |
| 325 | if (mConsoleMessages != null) |
| 326 | os.write(mConsoleMessages.toString().getBytes()); |
| 327 | mConsoleMessages = null; |
Feng Qian | be42388b | 2009-04-16 10:05:38 -0700 | [diff] [blame] | 328 | if (webkitData != null) |
| 329 | os.write(webkitData.getBytes()); |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 330 | os.flush(); |
| 331 | os.close(); |
| 332 | } catch (IOException ex) { |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 333 | Log.e(LOGTAG, "Cannot write to " + mResultFile + ", " + ex.getMessage()); |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | finished(); |
| 337 | } |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 338 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 339 | public void setCallback(TestShellCallback callback) { |
| 340 | mCallback = callback; |
| 341 | } |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 342 | |
Ben Murdoch | 2eea5f3 | 2009-09-23 15:28:57 +0100 | [diff] [blame] | 343 | public boolean finished() { |
| 344 | if (canMoveToNextTest()) { |
| 345 | mHandler.removeMessages(MSG_TIMEOUT); |
Ben Murdoch | 977f769 | 2009-09-16 20:13:07 +0100 | [diff] [blame] | 346 | if (mUiAutoTestPath != null) { |
| 347 | //don't really finish here |
| 348 | moveToNextTest(); |
| 349 | } else { |
| 350 | if (mCallback != null) { |
| 351 | mCallback.finished(); |
| 352 | } |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 353 | } |
Ben Murdoch | 2eea5f3 | 2009-09-23 15:28:57 +0100 | [diff] [blame] | 354 | return true; |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 355 | } |
Ben Murdoch | 2eea5f3 | 2009-09-23 15:28:57 +0100 | [diff] [blame] | 356 | return false; |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 357 | } |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 358 | |
Guang Zhu | 1871fe0 | 2009-04-29 14:49:03 -0700 | [diff] [blame] | 359 | public void setDefaultDumpDataType(DumpDataType defaultDumpDataType) { |
| 360 | mDefaultDumpDataType = defaultDumpDataType; |
| 361 | } |
| 362 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 363 | // ....................................... |
| 364 | // LayoutTestController Functions |
Ben Murdoch | 357d280 | 2010-07-23 16:18:58 +0100 | [diff] [blame] | 365 | public void dumpAsText(boolean enablePixelTests) { |
| 366 | // Added after webkit update to r63859. See trac.webkit.org/changeset/63730. |
| 367 | if (enablePixelTests) { |
| 368 | Log.v(LOGTAG, "dumpAsText(enablePixelTests == true) not implemented on Android!"); |
| 369 | } |
| 370 | |
Guang Zhu | 1871fe0 | 2009-04-29 14:49:03 -0700 | [diff] [blame] | 371 | mDumpDataType = DumpDataType.DUMP_AS_TEXT; |
Shimeng (Simon) Wang | ad45686 | 2010-06-16 17:32:24 -0700 | [diff] [blame] | 372 | mDumpTopFrameAsText = true; |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 373 | if (mWebView != null) { |
| 374 | String url = mWebView.getUrl(); |
| 375 | Log.v(LOGTAG, "dumpAsText called: "+url); |
| 376 | } |
| 377 | } |
| 378 | |
Shimeng (Simon) Wang | ad45686 | 2010-06-16 17:32:24 -0700 | [diff] [blame] | 379 | public void dumpChildFramesAsText() { |
| 380 | mDumpDataType = DumpDataType.DUMP_AS_TEXT; |
| 381 | mDumpChildFramesAsText = true; |
| 382 | if (mWebView != null) { |
| 383 | String url = mWebView.getUrl(); |
| 384 | Log.v(LOGTAG, "dumpChildFramesAsText called: "+url); |
| 385 | } |
| 386 | } |
| 387 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 388 | public void waitUntilDone() { |
| 389 | mWaitUntilDone = true; |
| 390 | String url = mWebView.getUrl(); |
| 391 | Log.v(LOGTAG, "waitUntilDone called: " + url); |
| 392 | } |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 393 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 394 | public void notifyDone() { |
| 395 | String url = mWebView.getUrl(); |
| 396 | Log.v(LOGTAG, "notifyDone called: " + url); |
| 397 | if (mWaitUntilDone) { |
| 398 | mWaitUntilDone = false; |
Ben Murdoch | 1620bcd | 2010-06-03 11:51:54 +0100 | [diff] [blame] | 399 | if (!mRequestedWebKitData && !mTimedOut && !finished()) { |
| 400 | requestWebKitData(); |
| 401 | } |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 402 | } |
| 403 | } |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 404 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 405 | public void display() { |
| 406 | mWebView.invalidate(); |
| 407 | } |
| 408 | |
| 409 | public void clearBackForwardList() { |
| 410 | mWebView.clearHistory(); |
| 411 | |
| 412 | } |
| 413 | |
| 414 | public void dumpBackForwardList() { |
| 415 | //printf("\n============== Back Forward List ==============\n"); |
| 416 | // mWebHistory |
| 417 | //printf("===============================================\n"); |
| 418 | |
| 419 | } |
| 420 | |
| 421 | public void dumpChildFrameScrollPositions() { |
| 422 | // TODO Auto-generated method stub |
| 423 | |
| 424 | } |
| 425 | |
| 426 | public void dumpEditingCallbacks() { |
| 427 | // TODO Auto-generated method stub |
| 428 | |
| 429 | } |
| 430 | |
| 431 | public void dumpSelectionRect() { |
| 432 | // TODO Auto-generated method stub |
| 433 | |
| 434 | } |
| 435 | |
| 436 | public void dumpTitleChanges() { |
| 437 | if (!mDumpTitleChanges) { |
| 438 | mTitleChanges = new StringBuffer(); |
| 439 | } |
| 440 | mDumpTitleChanges = true; |
| 441 | } |
| 442 | |
| 443 | public void keepWebHistory() { |
| 444 | if (!mKeepWebHistory) { |
| 445 | mWebHistory = new Vector(); |
| 446 | } |
| 447 | mKeepWebHistory = true; |
| 448 | } |
| 449 | |
| 450 | public void queueBackNavigation(int howfar) { |
| 451 | // TODO Auto-generated method stub |
| 452 | |
| 453 | } |
| 454 | |
| 455 | public void queueForwardNavigation(int howfar) { |
| 456 | // TODO Auto-generated method stub |
| 457 | |
| 458 | } |
| 459 | |
| 460 | public void queueLoad(String Url, String frameTarget) { |
| 461 | // TODO Auto-generated method stub |
| 462 | |
| 463 | } |
| 464 | |
| 465 | public void queueReload() { |
| 466 | mWebView.reload(); |
| 467 | } |
| 468 | |
| 469 | public void queueScript(String scriptToRunInCurrentContext) { |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 470 | mWebView.loadUrl("javascript:"+scriptToRunInCurrentContext); |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | public void repaintSweepHorizontally() { |
| 474 | // TODO Auto-generated method stub |
| 475 | |
| 476 | } |
| 477 | |
| 478 | public void setAcceptsEditing(boolean b) { |
| 479 | // TODO Auto-generated method stub |
| 480 | |
| 481 | } |
| 482 | |
| 483 | public void setMainFrameIsFirstResponder(boolean b) { |
| 484 | // TODO Auto-generated method stub |
| 485 | |
| 486 | } |
| 487 | |
| 488 | public void setWindowIsKey(boolean b) { |
| 489 | // This is meant to show/hide the window. The best I can find |
| 490 | // is setEnabled() |
| 491 | mWebView.setEnabled(b); |
| 492 | } |
| 493 | |
| 494 | public void testRepaint() { |
| 495 | mWebView.invalidate(); |
| 496 | } |
Ben Murdoch | e6f3e45 | 2009-04-22 16:02:31 +0100 | [diff] [blame] | 497 | |
| 498 | public void dumpDatabaseCallbacks() { |
| 499 | Log.v(LOGTAG, "dumpDatabaseCallbacks called."); |
| 500 | mDumpDatabaseCallbacks = true; |
| 501 | } |
| 502 | |
| 503 | public void setCanOpenWindows() { |
| 504 | Log.v(LOGTAG, "setCanOpenWindows called."); |
| 505 | mCanOpenWindows = true; |
| 506 | } |
| 507 | |
Steve Block | dad347c | 2009-08-19 18:46:34 +0100 | [diff] [blame] | 508 | /** |
| 509 | * Sets the Geolocation permission state to be used for all future requests. |
| 510 | */ |
| 511 | public void setGeolocationPermission(boolean allow) { |
Steve Block | f0f30c6 | 2010-08-18 15:15:42 +0100 | [diff] [blame] | 512 | mIsGeolocationPermissionSet = true; |
Steve Block | dad347c | 2009-08-19 18:46:34 +0100 | [diff] [blame] | 513 | mGeolocationPermission = allow; |
Steve Block | f0f30c6 | 2010-08-18 15:15:42 +0100 | [diff] [blame] | 514 | |
| 515 | if (mPendingGeolocationPermissionCallbacks != null) { |
| 516 | Iterator iter = mPendingGeolocationPermissionCallbacks.keySet().iterator(); |
| 517 | while (iter.hasNext()) { |
| 518 | GeolocationPermissions.Callback callback = |
| 519 | (GeolocationPermissions.Callback) iter.next(); |
| 520 | String origin = (String) mPendingGeolocationPermissionCallbacks.get(callback); |
| 521 | callback.invoke(origin, mGeolocationPermission, false); |
| 522 | } |
| 523 | mPendingGeolocationPermissionCallbacks = null; |
| 524 | } |
Steve Block | dad347c | 2009-08-19 18:46:34 +0100 | [diff] [blame] | 525 | } |
| 526 | |
Steve Block | 68dede3 | 2010-08-04 10:28:46 +0100 | [diff] [blame] | 527 | public void setMockDeviceOrientation(boolean canProvideAlpha, double alpha, |
| 528 | boolean canProvideBeta, double beta, boolean canProvideGamma, double gamma) { |
| 529 | mWebView.setMockDeviceOrientation(canProvideAlpha, alpha, canProvideBeta, beta, |
| 530 | canProvideGamma, gamma); |
| 531 | } |
| 532 | |
Steve Block | fb0de34 | 2010-03-19 18:48:35 +0000 | [diff] [blame] | 533 | public void overridePreference(String key, boolean value) { |
| 534 | // TODO: We should look up the correct WebView for the frame which |
| 535 | // called the layoutTestController method. Currently, we just use the |
| 536 | // WebView for the main frame. EventSender suffers from the same |
| 537 | // problem. |
Steve Block | 5f37cc43 | 2010-10-13 18:44:31 +0100 | [diff] [blame] | 538 | if (WEBKIT_OFFLINE_WEB_APPLICATION_CACHE_ENABLED.equals(key)) { |
Steve Block | fb0de34 | 2010-03-19 18:48:35 +0000 | [diff] [blame] | 539 | mWebView.getSettings().setAppCacheEnabled(value); |
Steve Block | 5f37cc43 | 2010-10-13 18:44:31 +0100 | [diff] [blame] | 540 | } else if (WEBKIT_USES_PAGE_CACHE_PREFERENCE_KEY.equals(key)) { |
| 541 | // Cache the maximum possible number of pages. |
| 542 | mWebView.getSettings().setPageCacheCapacity(Integer.MAX_VALUE); |
| 543 | } else { |
| 544 | Log.w(LOGTAG, "LayoutTestController.overridePreference(): " + |
| 545 | "Unsupported preference '" + key + "'"); |
Steve Block | fb0de34 | 2010-03-19 18:48:35 +0000 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
Elliott Slaughter | 5dc0c82 | 2010-06-22 11:31:54 -0700 | [diff] [blame] | 549 | public void setXSSAuditorEnabled (boolean flag) { |
| 550 | mWebView.getSettings().setXSSAuditorEnabled(flag); |
| 551 | } |
| 552 | |
Ben Murdoch | e6f3e45 | 2009-04-22 16:02:31 +0100 | [diff] [blame] | 553 | private final WebViewClient mViewClient = new WebViewClient(){ |
| 554 | @Override |
| 555 | public void onPageFinished(WebView view, String url) { |
| 556 | Log.v(LOGTAG, "onPageFinished, url=" + url); |
Ben Murdoch | 2eea5f3 | 2009-09-23 15:28:57 +0100 | [diff] [blame] | 557 | mPageFinished = true; |
Guang Zhu | 5dc4f21 | 2009-10-29 18:24:54 -0700 | [diff] [blame] | 558 | // get page draw time |
| 559 | if (FsUtils.isTestPageUrl(url)) { |
| 560 | if (mGetDrawtime) { |
| 561 | long[] times = new long[DRAW_RUNS]; |
| 562 | times = getDrawWebViewTime(mWebView, DRAW_RUNS); |
| 563 | FsUtils.writeDrawTime(DRAW_TIME_LOG, url, times); |
| 564 | } |
| 565 | if (mSaveImagePath != null) { |
| 566 | String name = FsUtils.getLastSegmentInPath(url); |
| 567 | drawPageToFile(mSaveImagePath + "/" + name + ".png", mWebView); |
| 568 | } |
| 569 | } |
Ben Murdoch | 1620bcd | 2010-06-03 11:51:54 +0100 | [diff] [blame] | 570 | |
Ben Murdoch | 2eea5f3 | 2009-09-23 15:28:57 +0100 | [diff] [blame] | 571 | // Calling finished() will check if we've met all the conditions for completing |
Ben Murdoch | 1620bcd | 2010-06-03 11:51:54 +0100 | [diff] [blame] | 572 | // this test and move to the next one if we are ready. Otherwise we ask WebCore to |
| 573 | // dump the page. |
Ben Murdoch | 2eea5f3 | 2009-09-23 15:28:57 +0100 | [diff] [blame] | 574 | if (finished()) { |
| 575 | return; |
| 576 | } |
Ben Murdoch | 1620bcd | 2010-06-03 11:51:54 +0100 | [diff] [blame] | 577 | |
| 578 | if (!mWaitUntilDone && !mRequestedWebKitData && !mTimedOut) { |
| 579 | requestWebKitData(); |
| 580 | } else { |
| 581 | if (mWaitUntilDone) { |
| 582 | Log.v(LOGTAG, "page finished loading but waiting for notifyDone to be called: " + url); |
| 583 | } |
| 584 | |
| 585 | if (mRequestedWebKitData) { |
| 586 | Log.v(LOGTAG, "page finished loading but webkit data has already been requested: " + url); |
| 587 | } |
| 588 | |
| 589 | if (mTimedOut) { |
| 590 | Log.v(LOGTAG, "page finished loading but already timed out: " + url); |
| 591 | } |
| 592 | } |
| 593 | |
Ben Murdoch | e6f3e45 | 2009-04-22 16:02:31 +0100 | [diff] [blame] | 594 | super.onPageFinished(view, url); |
| 595 | } |
| 596 | |
| 597 | @Override |
| 598 | public void onPageStarted(WebView view, String url, Bitmap favicon) { |
| 599 | Log.v(LOGTAG, "onPageStarted, url=" + url); |
Ben Murdoch | 2eea5f3 | 2009-09-23 15:28:57 +0100 | [diff] [blame] | 600 | mPageFinished = false; |
Ben Murdoch | e6f3e45 | 2009-04-22 16:02:31 +0100 | [diff] [blame] | 601 | super.onPageStarted(view, url, favicon); |
| 602 | } |
| 603 | |
| 604 | @Override |
| 605 | public void onReceivedError(WebView view, int errorCode, String description, |
| 606 | String failingUrl) { |
| 607 | Log.v(LOGTAG, "onReceivedError, errorCode=" + errorCode |
| 608 | + ", desc=" + description + ", url=" + failingUrl); |
| 609 | super.onReceivedError(view, errorCode, description, failingUrl); |
| 610 | } |
| 611 | |
| 612 | @Override |
| 613 | public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, |
| 614 | String host, String realm) { |
Steve Block | 2bb2553 | 2010-03-25 14:48:13 +0000 | [diff] [blame] | 615 | if (handler.useHttpAuthUsernamePassword() && view != null) { |
| 616 | String[] credentials = view.getHttpAuthUsernamePassword(host, realm); |
| 617 | if (credentials != null && credentials.length == 2) { |
| 618 | handler.proceed(credentials[0], credentials[1]); |
| 619 | return; |
| 620 | } |
| 621 | } |
Ben Murdoch | e6f3e45 | 2009-04-22 16:02:31 +0100 | [diff] [blame] | 622 | handler.cancel(); |
| 623 | } |
| 624 | |
| 625 | @Override |
| 626 | public void onReceivedSslError(WebView view, SslErrorHandler handler, |
| 627 | SslError error) { |
| 628 | handler.proceed(); |
| 629 | } |
| 630 | }; |
| 631 | |
| 632 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 633 | private final WebChromeClient mChromeClient = new WebChromeClient() { |
| 634 | @Override |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 635 | public void onReceivedTitle(WebView view, String title) { |
Ben Murdoch | 9b815d0 | 2010-06-01 18:45:10 +0100 | [diff] [blame] | 636 | setTitle("Test " + mCurrentTestNumber + " of " + mTotalTestCount + ": "+ title); |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 637 | if (mDumpTitleChanges) { |
| 638 | mTitleChanges.append("TITLE CHANGED: "); |
| 639 | mTitleChanges.append(title); |
| 640 | mTitleChanges.append("\n"); |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | @Override |
| 645 | public boolean onJsAlert(WebView view, String url, String message, |
| 646 | JsResult result) { |
| 647 | if (mDialogStrings == null) { |
| 648 | mDialogStrings = new StringBuffer(); |
| 649 | } |
| 650 | mDialogStrings.append("ALERT: "); |
| 651 | mDialogStrings.append(message); |
| 652 | mDialogStrings.append('\n'); |
| 653 | result.confirm(); |
| 654 | return true; |
| 655 | } |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 656 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 657 | @Override |
| 658 | public boolean onJsConfirm(WebView view, String url, String message, |
| 659 | JsResult result) { |
| 660 | if (mDialogStrings == null) { |
| 661 | mDialogStrings = new StringBuffer(); |
| 662 | } |
| 663 | mDialogStrings.append("CONFIRM: "); |
| 664 | mDialogStrings.append(message); |
| 665 | mDialogStrings.append('\n'); |
| 666 | result.confirm(); |
| 667 | return true; |
| 668 | } |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 669 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 670 | @Override |
| 671 | public boolean onJsPrompt(WebView view, String url, String message, |
| 672 | String defaultValue, JsPromptResult result) { |
| 673 | if (mDialogStrings == null) { |
| 674 | mDialogStrings = new StringBuffer(); |
| 675 | } |
| 676 | mDialogStrings.append("PROMPT: "); |
| 677 | mDialogStrings.append(message); |
| 678 | mDialogStrings.append(", default text: "); |
| 679 | mDialogStrings.append(defaultValue); |
| 680 | mDialogStrings.append('\n'); |
| 681 | result.confirm(); |
| 682 | return true; |
| 683 | } |
Ben Murdoch | e6f3e45 | 2009-04-22 16:02:31 +0100 | [diff] [blame] | 684 | |
| 685 | @Override |
Guang Zhu | f4bf552 | 2009-07-23 14:19:35 -0700 | [diff] [blame] | 686 | public boolean onJsTimeout() { |
| 687 | Log.v(LOGTAG, "JavaScript timeout"); |
| 688 | return false; |
| 689 | } |
| 690 | |
| 691 | @Override |
Ben Murdoch | e6f3e45 | 2009-04-22 16:02:31 +0100 | [diff] [blame] | 692 | public void onExceededDatabaseQuota(String url_str, |
Ben Murdoch | d497d87 | 2009-08-25 19:32:54 +0100 | [diff] [blame] | 693 | String databaseIdentifier, long currentQuota, |
| 694 | long estimatedSize, long totalUsedQuota, |
Ben Murdoch | e6f3e45 | 2009-04-22 16:02:31 +0100 | [diff] [blame] | 695 | WebStorage.QuotaUpdater callback) { |
| 696 | if (mDumpDatabaseCallbacks) { |
| 697 | if (mDatabaseCallbackStrings == null) { |
| 698 | mDatabaseCallbackStrings = new StringBuffer(); |
| 699 | } |
| 700 | |
| 701 | String protocol = ""; |
| 702 | String host = ""; |
| 703 | int port = 0; |
| 704 | |
| 705 | try { |
| 706 | URL url = new URL(url_str); |
| 707 | protocol = url.getProtocol(); |
| 708 | host = url.getHost(); |
| 709 | if (url.getPort() > -1) { |
| 710 | port = url.getPort(); |
| 711 | } |
| 712 | } catch (MalformedURLException e) {} |
| 713 | |
| 714 | String databaseCallbackString = |
| 715 | "UI DELEGATE DATABASE CALLBACK: " + |
| 716 | "exceededDatabaseQuotaForSecurityOrigin:{" + protocol + |
| 717 | ", " + host + ", " + port + "} database:" + |
| 718 | databaseIdentifier + "\n"; |
| 719 | Log.v(LOGTAG, "LOG: "+databaseCallbackString); |
| 720 | mDatabaseCallbackStrings.append(databaseCallbackString); |
| 721 | } |
| 722 | // Give 5MB more quota. |
| 723 | callback.updateQuota(currentQuota + 1024 * 1024 * 5); |
| 724 | } |
| 725 | |
Steve Block | dad347c | 2009-08-19 18:46:34 +0100 | [diff] [blame] | 726 | /** |
| 727 | * Instructs the client to show a prompt to ask the user to set the |
| 728 | * Geolocation permission state for the specified origin. |
| 729 | */ |
| 730 | @Override |
| 731 | public void onGeolocationPermissionsShowPrompt(String origin, |
| 732 | GeolocationPermissions.Callback callback) { |
Steve Block | f0f30c6 | 2010-08-18 15:15:42 +0100 | [diff] [blame] | 733 | if (mIsGeolocationPermissionSet) { |
Steve Block | dad347c | 2009-08-19 18:46:34 +0100 | [diff] [blame] | 734 | callback.invoke(origin, mGeolocationPermission, false); |
Steve Block | f0f30c6 | 2010-08-18 15:15:42 +0100 | [diff] [blame] | 735 | return; |
Steve Block | dad347c | 2009-08-19 18:46:34 +0100 | [diff] [blame] | 736 | } |
Steve Block | f0f30c6 | 2010-08-18 15:15:42 +0100 | [diff] [blame] | 737 | if (mPendingGeolocationPermissionCallbacks == null) { |
| 738 | mPendingGeolocationPermissionCallbacks = |
| 739 | new HashMap<GeolocationPermissions.Callback, String>(); |
| 740 | } |
| 741 | mPendingGeolocationPermissionCallbacks.put(callback, origin); |
Steve Block | dad347c | 2009-08-19 18:46:34 +0100 | [diff] [blame] | 742 | } |
| 743 | |
Ben Murdoch | e6f3e45 | 2009-04-22 16:02:31 +0100 | [diff] [blame] | 744 | @Override |
Guang Zhu | 6c15f60 | 2010-05-03 11:49:04 -0700 | [diff] [blame] | 745 | public boolean onConsoleMessage(ConsoleMessage consoleMessage) { |
| 746 | String msg = "CONSOLE MESSAGE: line " + consoleMessage.lineNumber() + ": " |
| 747 | + consoleMessage.message() + "\n"; |
Ben Murdoch | e6f3e45 | 2009-04-22 16:02:31 +0100 | [diff] [blame] | 748 | if (mConsoleMessages == null) { |
| 749 | mConsoleMessages = new StringBuffer(); |
| 750 | } |
Guang Zhu | 6c15f60 | 2010-05-03 11:49:04 -0700 | [diff] [blame] | 751 | mConsoleMessages.append(msg); |
| 752 | Log.v(LOGTAG, "LOG: " + msg); |
| 753 | // the rationale here is that if there's an error of either type, and the test was |
| 754 | // waiting for "notifyDone" signal to finish, then there's no point in waiting |
| 755 | // anymore because the JS execution is already terminated at this point and a |
| 756 | // "notifyDone" will never come out so it's just wasting time till timeout kicks in |
Guang Zhu | 5d53c19 | 2010-07-14 17:17:54 -0700 | [diff] [blame] | 757 | if ((msg.contains("Uncaught ReferenceError:") || msg.contains("Uncaught TypeError:")) |
| 758 | && mWaitUntilDone && mStopOnRefError) { |
Guang Zhu | 6c15f60 | 2010-05-03 11:49:04 -0700 | [diff] [blame] | 759 | Log.w(LOGTAG, "Terminating test case on uncaught ReferenceError or TypeError."); |
| 760 | mHandler.postDelayed(new Runnable() { |
| 761 | public void run() { |
| 762 | notifyDone(); |
| 763 | } |
| 764 | }, 500); |
| 765 | } |
| 766 | return true; |
Ben Murdoch | e6f3e45 | 2009-04-22 16:02:31 +0100 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | @Override |
| 770 | public boolean onCreateWindow(WebView view, boolean dialog, |
| 771 | boolean userGesture, Message resultMsg) { |
| 772 | if (!mCanOpenWindows) { |
Ben Murdoch | fc4a69e | 2010-02-04 16:35:10 +0000 | [diff] [blame] | 773 | // We can't open windows, so just send null back. |
| 774 | WebView.WebViewTransport transport = |
| 775 | (WebView.WebViewTransport) resultMsg.obj; |
| 776 | transport.setWebView(null); |
| 777 | resultMsg.sendToTarget(); |
| 778 | return true; |
Ben Murdoch | e6f3e45 | 2009-04-22 16:02:31 +0100 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | // We never display the new window, just create the view and |
| 782 | // allow it's content to execute and be recorded by the test |
| 783 | // runner. |
| 784 | |
Andrei Popescu | 4950b2b | 2009-09-03 13:56:07 +0100 | [diff] [blame] | 785 | HashMap<String, Object> jsIfaces = new HashMap<String, Object>(); |
| 786 | jsIfaces.put("layoutTestController", mCallbackProxy); |
| 787 | jsIfaces.put("eventSender", mCallbackProxy); |
| 788 | WebView newWindowView = new NewWindowWebView(TestShellActivity.this, jsIfaces); |
Ben Murdoch | e6f3e45 | 2009-04-22 16:02:31 +0100 | [diff] [blame] | 789 | setupWebViewForLayoutTests(newWindowView, mCallbackProxy); |
| 790 | WebView.WebViewTransport transport = |
| 791 | (WebView.WebViewTransport) resultMsg.obj; |
| 792 | transport.setWebView(newWindowView); |
| 793 | resultMsg.sendToTarget(); |
| 794 | return true; |
| 795 | } |
Ben Murdoch | fc4a69e | 2010-02-04 16:35:10 +0000 | [diff] [blame] | 796 | |
| 797 | @Override |
| 798 | public void onCloseWindow(WebView view) { |
| 799 | view.destroy(); |
| 800 | } |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 801 | }; |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 802 | |
Andrei Popescu | 4950b2b | 2009-09-03 13:56:07 +0100 | [diff] [blame] | 803 | private static class NewWindowWebView extends WebView { |
| 804 | public NewWindowWebView(Context context, Map<String, Object> jsIfaces) { |
Elliott Slaughter | f21d2e3 | 2010-07-14 18:08:54 -0700 | [diff] [blame] | 805 | super(context, null, 0, jsIfaces, false); |
Andrei Popescu | 4950b2b | 2009-09-03 13:56:07 +0100 | [diff] [blame] | 806 | } |
| 807 | } |
| 808 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 809 | private void resetTestStatus() { |
| 810 | mWaitUntilDone = false; |
Guang Zhu | 1871fe0 | 2009-04-29 14:49:03 -0700 | [diff] [blame] | 811 | mDumpDataType = mDefaultDumpDataType; |
Shimeng (Simon) Wang | ad45686 | 2010-06-16 17:32:24 -0700 | [diff] [blame] | 812 | mDumpTopFrameAsText = false; |
| 813 | mDumpChildFramesAsText = false; |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 814 | mTimedOut = false; |
| 815 | mDumpTitleChanges = false; |
| 816 | mRequestedWebKitData = false; |
Ben Murdoch | e6f3e45 | 2009-04-22 16:02:31 +0100 | [diff] [blame] | 817 | mDumpDatabaseCallbacks = false; |
| 818 | mCanOpenWindows = false; |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 819 | mEventSender.resetMouse(); |
Ben Murdoch | ecbc65c | 2010-01-13 10:54:56 +0000 | [diff] [blame] | 820 | mEventSender.clearTouchPoints(); |
Ben Murdoch | 8a032a3 | 2010-02-02 18:20:11 +0000 | [diff] [blame] | 821 | mEventSender.clearTouchMetaState(); |
Ben Murdoch | 2eea5f3 | 2009-09-23 15:28:57 +0100 | [diff] [blame] | 822 | mPageFinished = false; |
Ben Murdoch | 2eea5f3 | 2009-09-23 15:28:57 +0100 | [diff] [blame] | 823 | mDumpWebKitData = false; |
Guang Zhu | 5dc4f21 | 2009-10-29 18:24:54 -0700 | [diff] [blame] | 824 | mGetDrawtime = false; |
| 825 | mSaveImagePath = null; |
Ben Murdoch | a8b9768 | 2010-04-15 12:20:48 -0700 | [diff] [blame] | 826 | setDefaultWebSettings(mWebView); |
Steve Block | f0f30c6 | 2010-08-18 15:15:42 +0100 | [diff] [blame] | 827 | mIsGeolocationPermissionSet = false; |
| 828 | mPendingGeolocationPermissionCallbacks = null; |
Ben Murdoch | 7e73ce9 | 2011-01-20 11:21:24 +0000 | [diff] [blame] | 829 | CookieManager.getInstance().removeAllCookie(); |
Guang Zhu | 5dc4f21 | 2009-10-29 18:24:54 -0700 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | private long[] getDrawWebViewTime(WebView view, int count) { |
| 833 | if (count == 0) |
| 834 | return null; |
| 835 | long[] ret = new long[count]; |
| 836 | long start; |
| 837 | Canvas canvas = new Canvas(); |
| 838 | Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Config.ARGB_8888); |
| 839 | canvas.setBitmap(bitmap); |
| 840 | for (int i = 0; i < count; i++) { |
| 841 | start = System.currentTimeMillis(); |
| 842 | view.draw(canvas); |
| 843 | ret[i] = System.currentTimeMillis() - start; |
| 844 | } |
| 845 | return ret; |
| 846 | } |
| 847 | |
| 848 | private void drawPageToFile(String fileName, WebView view) { |
| 849 | Canvas canvas = new Canvas(); |
| 850 | Bitmap bitmap = Bitmap.createBitmap(view.getContentWidth(), view.getContentHeight(), |
| 851 | Config.ARGB_8888); |
| 852 | canvas.setBitmap(bitmap); |
| 853 | view.drawPage(canvas); |
| 854 | try { |
| 855 | FileOutputStream fos = new FileOutputStream(fileName); |
| 856 | if(!bitmap.compress(CompressFormat.PNG, 90, fos)) { |
| 857 | Log.w(LOGTAG, "Failed to compress and save image."); |
| 858 | } |
| 859 | } catch (IOException ioe) { |
| 860 | Log.e(LOGTAG, "", ioe); |
| 861 | } |
| 862 | bitmap.recycle(); |
Ben Murdoch | 2eea5f3 | 2009-09-23 15:28:57 +0100 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | private boolean canMoveToNextTest() { |
Ben Murdoch | 1620bcd | 2010-06-03 11:51:54 +0100 | [diff] [blame] | 866 | return (mDumpWebKitData && mPageFinished && !mWaitUntilDone) || mTimedOut; |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 867 | } |
Ben Murdoch | e6f3e45 | 2009-04-22 16:02:31 +0100 | [diff] [blame] | 868 | |
| 869 | private void setupWebViewForLayoutTests(WebView webview, CallbackProxy callbackProxy) { |
| 870 | if (webview == null) { |
| 871 | return; |
| 872 | } |
| 873 | |
Ben Murdoch | a8b9768 | 2010-04-15 12:20:48 -0700 | [diff] [blame] | 874 | setDefaultWebSettings(webview); |
| 875 | |
| 876 | webview.setWebChromeClient(mChromeClient); |
| 877 | webview.setWebViewClient(mViewClient); |
| 878 | // Setting a touch interval of -1 effectively disables the optimisation in WebView |
| 879 | // that stops repeated touch events flooding WebCore. The Event Sender only sends a |
| 880 | // single event rather than a stream of events (like what would generally happen in |
| 881 | // a real use of touch events in a WebView) and so if the WebView drops the event, |
| 882 | // the test will fail as the test expects one callback for every touch it synthesizes. |
| 883 | webview.setTouchInterval(-1); |
| 884 | } |
| 885 | |
| 886 | public void setDefaultWebSettings(WebView webview) { |
Ben Murdoch | e6f3e45 | 2009-04-22 16:02:31 +0100 | [diff] [blame] | 887 | WebSettings settings = webview.getSettings(); |
Guang Zhu | f4bf552 | 2009-07-23 14:19:35 -0700 | [diff] [blame] | 888 | settings.setAppCacheEnabled(true); |
| 889 | settings.setAppCachePath(getApplicationContext().getCacheDir().getPath()); |
| 890 | settings.setAppCacheMaxSize(Long.MAX_VALUE); |
Ben Murdoch | e6f3e45 | 2009-04-22 16:02:31 +0100 | [diff] [blame] | 891 | settings.setJavaScriptEnabled(true); |
| 892 | settings.setJavaScriptCanOpenWindowsAutomatically(true); |
| 893 | settings.setSupportMultipleWindows(true); |
| 894 | settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL); |
| 895 | settings.setDatabaseEnabled(true); |
| 896 | settings.setDatabasePath(getDir("databases",0).getAbsolutePath()); |
| 897 | settings.setDomStorageEnabled(true); |
Guang Zhu | b028f92 | 2009-08-11 14:27:32 -0700 | [diff] [blame] | 898 | settings.setWorkersEnabled(false); |
Elliott Slaughter | be1304f | 2010-06-23 11:29:02 -0700 | [diff] [blame] | 899 | settings.setXSSAuditorEnabled(false); |
Steve Block | 5f37cc43 | 2010-10-13 18:44:31 +0100 | [diff] [blame] | 900 | settings.setPageCacheCapacity(0); |
Ben Murdoch | e6f3e45 | 2009-04-22 16:02:31 +0100 | [diff] [blame] | 901 | } |
| 902 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 903 | private WebView mWebView; |
| 904 | private WebViewEventSender mEventSender; |
| 905 | private AsyncHandler mHandler; |
| 906 | private TestShellCallback mCallback; |
| 907 | |
| 908 | private CallbackProxy mCallbackProxy; |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 909 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 910 | private String mTestUrl; |
| 911 | private String mResultFile; |
| 912 | private int mTimeoutInMillis; |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 913 | private String mUiAutoTestPath; |
Guang Zhu | 5dc4f21 | 2009-10-29 18:24:54 -0700 | [diff] [blame] | 914 | private String mSaveImagePath; |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 915 | private BufferedReader mTestListReader; |
Guang Zhu | 5dc4f21 | 2009-10-29 18:24:54 -0700 | [diff] [blame] | 916 | private boolean mGetDrawtime; |
Ben Murdoch | 9b815d0 | 2010-06-01 18:45:10 +0100 | [diff] [blame] | 917 | private int mTotalTestCount; |
| 918 | private int mCurrentTestNumber; |
Guang Zhu | 5d53c19 | 2010-07-14 17:17:54 -0700 | [diff] [blame] | 919 | private boolean mStopOnRefError; |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 920 | |
| 921 | // States |
| 922 | private boolean mTimedOut; |
| 923 | private boolean mRequestedWebKitData; |
| 924 | private boolean mFinishedRunning; |
| 925 | |
| 926 | // Layout test controller variables. |
Guang Zhu | 1871fe0 | 2009-04-29 14:49:03 -0700 | [diff] [blame] | 927 | private DumpDataType mDumpDataType; |
| 928 | private DumpDataType mDefaultDumpDataType = DumpDataType.EXT_REPR; |
Shimeng (Simon) Wang | ad45686 | 2010-06-16 17:32:24 -0700 | [diff] [blame] | 929 | private boolean mDumpTopFrameAsText; |
| 930 | private boolean mDumpChildFramesAsText; |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 931 | private boolean mWaitUntilDone; |
| 932 | private boolean mDumpTitleChanges; |
| 933 | private StringBuffer mTitleChanges; |
| 934 | private StringBuffer mDialogStrings; |
| 935 | private boolean mKeepWebHistory; |
| 936 | private Vector mWebHistory; |
Ben Murdoch | e6f3e45 | 2009-04-22 16:02:31 +0100 | [diff] [blame] | 937 | private boolean mDumpDatabaseCallbacks; |
| 938 | private StringBuffer mDatabaseCallbackStrings; |
| 939 | private StringBuffer mConsoleMessages; |
| 940 | private boolean mCanOpenWindows; |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 941 | |
Ben Murdoch | 2eea5f3 | 2009-09-23 15:28:57 +0100 | [diff] [blame] | 942 | private boolean mPageFinished = false; |
| 943 | private boolean mDumpWebKitData = false; |
Ben Murdoch | 977f769 | 2009-09-16 20:13:07 +0100 | [diff] [blame] | 944 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 945 | static final String TIMEOUT_STR = "**Test timeout"; |
Steve Block | 8e8cf73 | 2011-02-10 10:51:45 -0800 | [diff] [blame] | 946 | static final long DUMP_TIMEOUT_MS = 100000; // 100s timeout for dumping webview content |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 947 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 948 | static final int MSG_TIMEOUT = 0; |
| 949 | static final int MSG_WEBKIT_DATA = 1; |
Guang Zhu | f37a574 | 2010-12-23 14:37:31 -0800 | [diff] [blame] | 950 | static final int MSG_DUMP_TIMEOUT = 2; |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 951 | |
| 952 | static final String LOGTAG="TestShell"; |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 953 | |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 954 | static final String TEST_URL = "TestUrl"; |
| 955 | static final String RESULT_FILE = "ResultFile"; |
| 956 | static final String TIMEOUT_IN_MILLIS = "TimeoutInMillis"; |
Guang Zhu | f92bd42 | 2009-06-29 10:40:55 -0700 | [diff] [blame] | 957 | static final String UI_AUTO_TEST = "UiAutoTest"; |
Guang Zhu | 5dc4f21 | 2009-10-29 18:24:54 -0700 | [diff] [blame] | 958 | static final String GET_DRAW_TIME = "GetDrawTime"; |
| 959 | static final String SAVE_IMAGE = "SaveImage"; |
Ben Murdoch | 9b815d0 | 2010-06-01 18:45:10 +0100 | [diff] [blame] | 960 | static final String TOTAL_TEST_COUNT = "TestCount"; |
| 961 | static final String CURRENT_TEST_NUMBER = "TestNumber"; |
Guang Zhu | 5d53c19 | 2010-07-14 17:17:54 -0700 | [diff] [blame] | 962 | static final String STOP_ON_REF_ERROR = "StopOnReferenceError"; |
Guang Zhu | 5dc4f21 | 2009-10-29 18:24:54 -0700 | [diff] [blame] | 963 | |
| 964 | static final int DRAW_RUNS = 5; |
Christian Mehlmauer | 8b85dce | 2010-07-19 20:11:27 +0200 | [diff] [blame] | 965 | static final String DRAW_TIME_LOG = Environment.getExternalStorageDirectory() + |
| 966 | "/android/page_draw_time.txt"; |
Steve Block | dad347c | 2009-08-19 18:46:34 +0100 | [diff] [blame] | 967 | |
Steve Block | f0f30c6 | 2010-08-18 15:15:42 +0100 | [diff] [blame] | 968 | private boolean mIsGeolocationPermissionSet; |
Steve Block | dad347c | 2009-08-19 18:46:34 +0100 | [diff] [blame] | 969 | private boolean mGeolocationPermission; |
Steve Block | f0f30c6 | 2010-08-18 15:15:42 +0100 | [diff] [blame] | 970 | private Map mPendingGeolocationPermissionCallbacks; |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 971 | } |