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