PowerMeasurement.java revision 7c6efa13f129dbae5319f0981a430d4662f43354
1package com.android.browserpowertest; 2 3import android.content.Intent; 4import android.app.Instrumentation; 5import android.os.Handler; 6import android.os.Message; 7import android.test.ActivityInstrumentationTestCase2; 8import android.util.Log; 9import junit.framework.*; 10 11public class PowerMeasurement extends ActivityInstrumentationTestCase2<PowerTestActivity> { 12 13 private static final String LOGTAG = "PowerMeasurement"; 14 private static final String PKG_NAME = "com.android.browserpowertest"; 15 private static final String TESTING_URL = "http://www.espn.com"; 16 private static final int TIME_OUT = 2 * 60 * 1000; 17 private static final int DELAY = 0; 18 19 public PowerMeasurement() { 20 super(PKG_NAME, PowerTestActivity.class); 21 } 22 23 public void testPageLoad() throws Throwable { 24 Instrumentation mInst = getInstrumentation(); 25 PowerTestActivity act = getActivity(); 26 27 Intent intent = new Intent(mInst.getContext(), PowerTestActivity.class); 28 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 29 long start = System.currentTimeMillis(); 30 PowerTestActivity activity = (PowerTestActivity)mInst.startActivitySync( 31 intent); 32 activity.reset(); 33 //send a message with the new URL 34 Handler handler = activity.getHandler(); 35 Message msg = handler.obtainMessage( 36 PowerTestActivity.MSG_NAVIGATE, TIME_OUT, DELAY); 37 msg.getData().putString(PowerTestActivity.MSG_NAV_URL, TESTING_URL); 38 msg.getData().putBoolean(PowerTestActivity.MSG_NAV_LOGTIME, true); 39 40 handler.sendMessage(msg); 41 boolean timeoutFlag = activity.waitUntilDone(); 42 long end = System.currentTimeMillis(); 43 assertFalse(TESTING_URL + " failed to load", timeoutFlag); 44 boolean pageErrorFlag = activity.getPageError(); 45 assertFalse(TESTING_URL + " is not available, either network is down or the server is down", 46 pageErrorFlag); 47 Log.v(LOGTAG, "Page is loaded in " + activity.getPageLoadTime() + " ms."); 48 49 activity.finish(); 50 } 51} 52