PowerMeasurement.java revision 30412d1ce0d65acda8c65a7fbe09a55900647f6a
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 =
16        "http://75.17.48.204:10088/nyt/index.html";
17    private static final int TIME_OUT = 2 * 60 * 1000;
18    private static final int DELAY = 0;
19
20    public PowerMeasurement() {
21        super(PKG_NAME, PowerTestActivity.class);
22    }
23
24    public void testPageLoadStaticNYTimes() throws Throwable {
25        Instrumentation mInst = getInstrumentation();
26        PowerTestActivity act = getActivity();
27
28        Intent intent = new Intent(mInst.getContext(), PowerTestActivity.class);
29        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
30        long start = System.currentTimeMillis();
31        PowerTestActivity activity = (PowerTestActivity)mInst.startActivitySync(
32                intent);
33        activity.reset();
34        //send a message with the new URL
35        Handler handler = activity.getHandler();
36        Message msg = handler.obtainMessage(
37                PowerTestActivity.MSG_NAVIGATE, TIME_OUT, DELAY);
38        msg.getData().putString(PowerTestActivity.MSG_NAV_URL, TESTING_URL);
39        msg.getData().putBoolean(PowerTestActivity.MSG_NAV_LOGTIME, true);
40
41        handler.sendMessage(msg);
42        boolean timeoutFlag = activity.waitUntilDone();
43        long end = System.currentTimeMillis();
44        assertFalse(TESTING_URL + " failed to load", timeoutFlag);
45        boolean pageErrorFlag = activity.getPageError();
46        assertFalse(TESTING_URL + " is not available, either network is down or the server is down",
47                pageErrorFlag);
48        Log.v(LOGTAG, "Page is loaded in " + activity.getPageLoadTime() + " ms.");
49
50        // Force to clean up the cache dir so that it get back to the clean
51        // state
52        Runtime fileRemoval = Runtime.getRuntime();
53        String cmdBecomeSu = "su";
54        boolean clearCacheSuccess = false;
55        try{
56            Process runsum = fileRemoval.exec(cmdBecomeSu);
57            int exitVal = runsum.waitFor();
58            String rmfile = "rm -r /data/data/com.android.browserpowertest/cache";
59            Process removal = fileRemoval.exec(rmfile);
60            exitVal = removal.waitFor();
61            if (exitVal == 0) {
62                clearCacheSuccess = true;
63            }
64        } catch ( Exception e){
65            assertTrue("Fails to clear the cahche", false);
66        }
67        assertTrue("Fails to clear the cahche", clearCacheSuccess);
68        activity.finish();
69    }
70}
71