2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.browserpowertest;
19 import android.app.Activity;
20 import android.app.ActivityThread;
21 import android.graphics.Bitmap;
22 import android.os.Bundle;
23 import android.os.Handler;
24 import android.os.Message;
25 import android.util.Log;
26 import android.view.ViewGroup;
27 import android.webkit.WebChromeClient;
28 import android.webkit.WebView;
29 import android.webkit.WebViewClient;
30 import android.webkit.WebSettings.LayoutAlgorithm;
31 import android.widget.LinearLayout;
32 import android.widget.LinearLayout.LayoutParams;
34 public class PowerTestActivity extends Activity {
36 public static final String LOGTAG = "PowerTestActivity";
37 public static final String PARAM_URL = "URL";
38 public static final String PARAM_TIMEOUT = "Timeout";
39 public static final int RESULT_TIMEOUT = 0xDEAD;
40 public static final int MSG_TIMEOUT = 0xC001;
41 public static final int MSG_NAVIGATE = 0xC002;
42 public static final String MSG_NAV_URL = "url";
43 public static final String MSG_NAV_LOGTIME = "logtime";
45 private WebView webView;
46 private SimpleWebViewClient webViewClient;
47 private SimpleChromeClient chromeClient;
48 private Handler handler;
49 private boolean timeoutFlag;
50 private boolean logTime;
51 private boolean pageDone;
52 private Object pageDoneLock;
53 private int pageStartCount;
54 private int manualDelay;
55 private long startTime;
56 private long pageLoadTime;
57 private PageDoneRunner pageDoneRunner = new PageDoneRunner();
59 public PowerTestActivity() {
63 protected void onCreate(Bundle savedInstanceState) {
64 super.onCreate(savedInstanceState);
66 Log.v(LOGTAG, "onCreate, inst=" + Integer.toHexString(hashCode()));
68 LinearLayout contentView = new LinearLayout(this);
69 contentView.setOrientation(LinearLayout.VERTICAL);
70 setContentView(contentView);
73 webView = new WebView(this);
74 webView.getSettings().setJavaScriptEnabled(true);
75 webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(false);
76 webView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.NORMAL);
78 webViewClient = new SimpleWebViewClient();
79 chromeClient = new SimpleChromeClient();
80 webView.setWebViewClient(webViewClient);
81 webView.setWebChromeClient(chromeClient);
83 contentView.addView(webView, new LayoutParams(
84 ViewGroup.LayoutParams.MATCH_PARENT,
85 ViewGroup.LayoutParams.MATCH_PARENT, 0.0f));
87 handler = new Handler() {
89 public void handleMessage(Message msg) {
95 manualDelay = msg.arg2;
96 navigate(msg.getData().getString(MSG_NAV_URL), msg.arg1);
97 logTime = msg.getData().getBoolean(MSG_NAV_LOGTIME);
103 pageDoneLock = new Object();
106 public void reset() {
107 synchronized (pageDoneLock) {
112 chromeClient.resetJsTimeout();
115 private void navigate(String url, int timeout) {
117 Log.v(LOGTAG, "URL is null, cancelling...");
120 webView.stopLoading();
122 webView.clearCache(true);
124 startTime = System.currentTimeMillis();
125 Log.v(LOGTAG, "Navigating to URL: " + url);
126 webView.loadUrl(url);
129 //set a timer with specified timeout (in ms)
130 handler.sendMessageDelayed(handler.obtainMessage(MSG_TIMEOUT),
136 protected void onDestroy() {
138 Log.v(LOGTAG, "onDestroy, inst=" + Integer.toHexString(hashCode()));
139 webView.clearCache(true);
143 private boolean isPageDone() {
144 synchronized (pageDoneLock) {
149 private void setPageDone(boolean pageDone) {
150 synchronized (pageDoneLock) {
151 this.pageDone = pageDone;
152 pageDoneLock.notifyAll();
156 private void handleTimeout() {
157 int progress = webView.getProgress();
158 webView.stopLoading();
159 Log.v(LOGTAG, "Page timeout triggered, progress = " + progress);
161 handler.postDelayed(pageDoneRunner, manualDelay);
164 public boolean waitUntilDone() {
165 validateNotAppThread();
166 synchronized (pageDoneLock) {
167 while(!isPageDone()) {
170 } catch (InterruptedException ie) {
178 public Handler getHandler() {
182 private final void validateNotAppThread() {
183 if (ActivityThread.currentActivityThread() != null) {
184 throw new RuntimeException(
185 "This method can not be called from the main application thread");
189 public long getPageLoadTime() {
193 public boolean getPageError() {
194 return webViewClient.getPageErrorFlag();
197 class SimpleWebViewClient extends WebViewClient {
199 private boolean pageErrorFlag = false;
202 public void onReceivedError(WebView view, int errorCode, String description,
204 pageErrorFlag = true;
205 Log.v(LOGTAG, "WebCore error: code=" + errorCode
206 + ", description=" + description
207 + ", url=" + failingUrl);
211 public void onPageStarted(WebView view, String url, Bitmap favicon) {
213 Log.v(LOGTAG, "onPageStarted: " + url);
217 public void onPageFinished(WebView view, String url) {
218 Log.v(LOGTAG, "onPageFinished: " + url);
219 // let handleTimeout take care of finishing the page
221 handler.postDelayed(new WebViewStatusChecker(), 500);
224 // return true if the URL is not available or the page is down
225 public boolean getPageErrorFlag() {
226 return pageErrorFlag;
230 class SimpleChromeClient extends WebChromeClient {
232 private int timeoutCounter = 0;
234 public void resetJsTimeout() {
239 public void onReceivedTitle(WebView view, String title) {
240 PowerTestActivity.this.setTitle(title);
244 class WebViewStatusChecker implements Runnable {
246 private int initialStartCount;
248 public WebViewStatusChecker() {
249 initialStartCount = pageStartCount;
253 if (initialStartCount == pageStartCount && !isPageDone()) {
254 handler.removeMessages(MSG_TIMEOUT);
255 webView.stopLoading();
256 handler.postDelayed(pageDoneRunner, manualDelay);
261 class PageDoneRunner implements Runnable {
264 Log.v(LOGTAG, "Finishing URL: " + webView.getUrl());
265 pageLoadTime = System.currentTimeMillis() - startTime;