1555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik/*
2555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik * Copyright (C) 2011 The Android Open Source Project
3555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik *
4555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik * Licensed under the Apache License, Version 2.0 (the "License");
5555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik * you may not use this file except in compliance with the License.
6555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik * You may obtain a copy of the License at
7555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik *
8555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik *      http://www.apache.org/licenses/LICENSE-2.0
9555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik *
10555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik * Unless required by applicable law or agreed to in writing, software
11555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik * distributed under the License is distributed on an "AS IS" BASIS,
12555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik * See the License for the specific language governing permissions and
14555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik * limitations under the License.
15555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik */
16555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik
17555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craikpackage com.test.tilebenchmark;
18555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik
19555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craikimport com.test.tilebenchmark.ProfileActivity.ProfileCallback;
20555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik
21555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craikimport java.io.File;
22555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craikimport java.util.HashMap;
23555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craikimport java.util.Map;
24555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik
25555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craikimport android.content.res.Resources;
26555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craikimport android.os.Bundle;
27555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craikimport android.os.Environment;
28555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craikimport android.test.ActivityInstrumentationTestCase2;
29555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craikimport android.util.Log;
30702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craikimport android.webkit.WebSettings;
31702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craikimport android.widget.Spinner;
32555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik
33555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craikpublic class PerformanceTest extends
34555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik        ActivityInstrumentationTestCase2<ProfileActivity> {
35555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik
36702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik    public static class AnimStat {
37702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        double aggVal = 0;
38702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        double aggSqrVal = 0;
39702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        double count = 0;
40702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik    }
41702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik
42555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik    private class StatAggregator extends PlaybackGraphs {
43555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik        private HashMap<String, Double> mDataMap = new HashMap<String, Double>();
44702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        private HashMap<String, AnimStat> mAnimDataMap = new HashMap<String, AnimStat>();
45555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik        private int mCount = 0;
46555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik
47702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik
48555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik        public void aggregate() {
49702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik            boolean inAnimTests = mAnimTests != null;
50702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik            Resources resources = mWeb.getResources();
51702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik            String animFramerateString = resources.getString(R.string.animation_framerate);
52702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik            for (Map.Entry<String, Double> e : mSingleStats.entrySet()) {
53702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                String name = e.getKey();
54702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                if (inAnimTests) {
55702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                    if (name.equals(animFramerateString)) {
56702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                        // in animation testing phase, record animation framerate and aggregate
57702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                        // stats, differentiating on values of mAnimTestNr and mDoubleBuffering
58702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                        String fullName = ANIM_TEST_NAMES[mAnimTestNr] + " " + name;
59702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                        fullName += mDoubleBuffering ? " tiled" : " webkit";
60702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik
61702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                        if (!mAnimDataMap.containsKey(fullName)) {
62702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                            mAnimDataMap.put(fullName, new AnimStat());
63702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                        }
64702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                        AnimStat statVals = mAnimDataMap.get(fullName);
65702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                        statVals.aggVal += e.getValue();
66702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                        statVals.aggSqrVal += e.getValue() * e.getValue();
67702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                        statVals.count += 1;
68702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                    }
69702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                } else {
70702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                    double aggVal = mDataMap.containsKey(name)
71702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                            ? mDataMap.get(name) : 0;
72702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                    mDataMap.put(name, aggVal + e.getValue());
73702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                }
74702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik            }
75702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik
76702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik            if (inAnimTests) {
77702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                return;
78702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik            }
79702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik
80555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik            mCount++;
81555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik            for (int metricIndex = 0; metricIndex < Metrics.length; metricIndex++) {
82555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik                for (int statIndex = 0; statIndex < Stats.length; statIndex++) {
83555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik                    String metricLabel = resources.getString(
84555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik                            Metrics[metricIndex].getLabelId());
85555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik                    String statLabel = resources.getString(
86555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik                            Stats[statIndex].getLabelId());
87555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik
88555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik                    String label = metricLabel + " " + statLabel;
89555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik                    double aggVal = mDataMap.containsKey(label) ? mDataMap
90555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik                            .get(label) : 0;
91555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik
92555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik                    aggVal += mStats[metricIndex][statIndex];
93555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik                    mDataMap.put(label, aggVal);
94555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik                }
95555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik            }
96702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik
97555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik        }
98555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik
99702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        // build the final bundle of results
100555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik        public Bundle getBundle() {
101555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik            Bundle b = new Bundle();
102702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik            int count = (0 == mCount) ? Integer.MAX_VALUE : mCount;
103555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik            for (Map.Entry<String, Double> e : mDataMap.entrySet()) {
104555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik                b.putDouble(e.getKey(), e.getValue() / count);
105555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik            }
106702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik
107702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik            for (Map.Entry<String, AnimStat> e : mAnimDataMap.entrySet()) {
108702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                String statName = e.getKey();
109702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                AnimStat statVals = e.getValue();
110702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik
111702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                double avg = statVals.aggVal/statVals.count;
112702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                double stdDev = Math.sqrt((statVals.aggSqrVal / statVals.count) - avg * avg);
113702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik
114702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                b.putDouble(statName, avg);
115702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                b.putDouble(statName + " STD DEV", stdDev);
116702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik            }
117702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik
118555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik            return b;
119555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik        }
120555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik    }
121555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik
122555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik    ProfileActivity mActivity;
123702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik    ProfiledWebView mWeb;
124702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik    Spinner mMovementSpinner;
125702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik    StatAggregator mStats;
126555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik
127555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik    private static final String LOGTAG = "PerformanceTest";
128555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik    private static final String TEST_LOCATION = "webkit/page_cycler";
129555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik    private static final String URL_PREFIX = "file://";
130555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik    private static final String URL_POSTFIX = "/index.html?skip=true";
131555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik    private static final int MAX_ITERATIONS = 4;
132702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik    private static final String SCROLL_TEST_DIRS[] = {
133702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        "alexa25_2011"
134702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik    };
135702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik    private static final String ANIM_TEST_DIRS[] = {
136702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        "dhtml"
137555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik    };
138555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik
139555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik    public PerformanceTest() {
140555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik        super(ProfileActivity.class);
141555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik    }
142555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik
143555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik    @Override
144555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik    protected void setUp() throws Exception {
145555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik        super.setUp();
146555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik        mActivity = getActivity();
147702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        mWeb = (ProfiledWebView) mActivity.findViewById(R.id.web);
148702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        mMovementSpinner = (Spinner) mActivity.findViewById(R.id.movement);
149702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        mStats = new StatAggregator();
150702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik
151702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        // use mStats as a condition variable between the UI thread and
152702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        // this(the testing) thread
153702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        mActivity.setCallback(new ProfileCallback() {
154702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik            @Override
155702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik            public void profileCallback(RunData data) {
156702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                mStats.setData(data);
157702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                synchronized (mStats) {
158702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                    mStats.notify();
159702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                }
160702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik            }
161702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        });
162702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik
163555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik    }
164555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik
165555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik    private boolean loadUrl(final String url) {
166555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik        try {
167555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik            Log.d(LOGTAG, "test starting for url " + url);
168555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik            mActivity.runOnUiThread(new Runnable() {
169555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik                @Override
170555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik                public void run() {
171702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                    mWeb.loadUrl(url);
172555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik                }
173555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik            });
174555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik            synchronized (mStats) {
175555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik                mStats.wait();
176555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik            }
177702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik
178555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik            mStats.aggregate();
179555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik        } catch (InterruptedException e) {
180555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik            e.printStackTrace();
181555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik            return false;
182555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik        }
183555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik        return true;
184555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik    }
185555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik
186702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik    private boolean validTest(String nextTest) {
187702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        // if testing animations, test must be in mAnimTests
188702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        if (mAnimTests == null)
189702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik            return true;
190702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik
191702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        for (String test : mAnimTests) {
192702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik            if (test.equals(nextTest)) {
193702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                return true;
194702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik            }
195702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        }
196702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        return false;
197702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik    }
198702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik
199702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik    private boolean runIteration(String[] testDirs) {
200555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik        File sdFile = Environment.getExternalStorageDirectory();
201702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        for (String testDirName : testDirs) {
202555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik            File testDir = new File(sdFile, TEST_LOCATION + "/" + testDirName);
203555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik            Log.d(LOGTAG, "Testing dir: '" + testDir.getAbsolutePath()
204555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik                    + "', exists=" + testDir.exists());
205702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik
206555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik            for (File siteDir : testDir.listFiles()) {
207702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                if (!siteDir.isDirectory() || !validTest(siteDir.getName())) {
208555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik                    continue;
209702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                }
210555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik
211555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik                if (!loadUrl(URL_PREFIX + siteDir.getAbsolutePath()
212555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik                        + URL_POSTFIX)) {
213555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik                    return false;
214555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik                }
215555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik            }
216555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik        }
217555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik        return true;
218555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik    }
219555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik
220702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik    private boolean  runTestDirs(String[] testDirs) {
221702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        for (int i = 0; i < MAX_ITERATIONS; i++)
222702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik            if (!runIteration(testDirs)) {
223702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                return false;
224702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik            }
225702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        return true;
226702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik    }
227702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik
228702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik    private void pushDoubleBuffering() {
229702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        getInstrumentation().runOnMainSync(new Runnable() {
230702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik            public void run() {
231702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                mWeb.setDoubleBuffering(mDoubleBuffering);
232702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik            }
233702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        });
234702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik    }
235702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik
236702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik    private void setScrollingTestingMode(final boolean scrolled) {
237702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        getInstrumentation().runOnMainSync(new Runnable() {
238702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik            public void run() {
239702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                mMovementSpinner.setSelection(scrolled ? 0 : 2);
240702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik            }
241702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        });
242702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik    }
243702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik
244702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik
245702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik    private String[] mAnimTests = null;
246702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik    private int mAnimTestNr = -1;
247702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik    private boolean mDoubleBuffering = true;
248702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik    private static final String[] ANIM_TEST_NAMES = {
249702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        "slow", "fast"
250702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik    };
251702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik    private static final String[][] ANIM_TESTS = {
252702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        {"scrolling", "replaceimages", "layers5", "layers1"},
253702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        {"slidingballs", "meter", "slidein", "fadespacing", "colorfade",
254702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                "mozilla", "movingtext", "diagball", "zoom", "imageslide"},
255702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik    };
256702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik
257702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik    private boolean checkMedia() {
258555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik        String state = Environment.getExternalStorageState();
259555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik
260555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik        if (!Environment.MEDIA_MOUNTED.equals(state)
261555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik                && !Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
262555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik            Log.d(LOGTAG, "ARG Can't access sd card!");
263555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik            // Can't read the SD card, fail and die!
264555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik            getInstrumentation().sendStatus(1, null);
265702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik            return false;
266555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik        }
267702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        return true;
268702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik    }
269555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik
270702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik    public void testMetrics() {
271702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        setScrollingTestingMode(true);
272702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        if (checkMedia() && runTestDirs(SCROLL_TEST_DIRS)) {
273702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik            getInstrumentation().sendStatus(0, mStats.getBundle());
2749e6fa17b475a537605f9673c3a065c28ea29bb91Chris Craik        } else {
2759e6fa17b475a537605f9673c3a065c28ea29bb91Chris Craik            getInstrumentation().sendStatus(1, null);
2769e6fa17b475a537605f9673c3a065c28ea29bb91Chris Craik        }
2779e6fa17b475a537605f9673c3a065c28ea29bb91Chris Craik    }
2789e6fa17b475a537605f9673c3a065c28ea29bb91Chris Craik
2799e6fa17b475a537605f9673c3a065c28ea29bb91Chris Craik    public void testMetricsMinimalMemory() {
2809e6fa17b475a537605f9673c3a065c28ea29bb91Chris Craik        mActivity.runOnUiThread(new Runnable() {
2819e6fa17b475a537605f9673c3a065c28ea29bb91Chris Craik            @Override
2829e6fa17b475a537605f9673c3a065c28ea29bb91Chris Craik            public void run() {
2839e6fa17b475a537605f9673c3a065c28ea29bb91Chris Craik                mWeb.setUseMinimalMemory(true);
2849e6fa17b475a537605f9673c3a065c28ea29bb91Chris Craik            }
2859e6fa17b475a537605f9673c3a065c28ea29bb91Chris Craik        });
2869e6fa17b475a537605f9673c3a065c28ea29bb91Chris Craik
2879e6fa17b475a537605f9673c3a065c28ea29bb91Chris Craik        setScrollingTestingMode(true);
2889e6fa17b475a537605f9673c3a065c28ea29bb91Chris Craik        if (checkMedia() && runTestDirs(SCROLL_TEST_DIRS)) {
2899e6fa17b475a537605f9673c3a065c28ea29bb91Chris Craik            getInstrumentation().sendStatus(0, mStats.getBundle());
290702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        } else {
291702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik            getInstrumentation().sendStatus(1, null);
292702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        }
293702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik    }
294702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik
295702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik    private boolean runAnimationTests() {
296702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        for (int doubleBuffer = 0; doubleBuffer <= 1; doubleBuffer++) {
297702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik            mDoubleBuffering = doubleBuffer == 1;
298702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik            pushDoubleBuffering();
299702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik            for (mAnimTestNr = 0; mAnimTestNr < ANIM_TESTS.length; mAnimTestNr++) {
300702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                mAnimTests = ANIM_TESTS[mAnimTestNr];
301702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                if (!runTestDirs(ANIM_TEST_DIRS)) {
302702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik                    return false;
303555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik                }
304555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik            }
305702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        }
306702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        return true;
307702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik    }
308555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik
309702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik    public void testAnimations() {
310702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        // instead of autoscrolling, load each page until either an timer fires,
311702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        // or the animation signals complete via javascript
312702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        setScrollingTestingMode(false);
313702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik
314702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        if (checkMedia() && runAnimationTests()) {
315702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik            getInstrumentation().sendStatus(0, mStats.getBundle());
316702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        } else {
317702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik            getInstrumentation().sendStatus(1, null);
318702c6fdc4799593c16eb0051703e3bd62086ff42Chris Craik        }
319555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik    }
320555c55ed65056e2236430f92e81c1fb6acd05dcdChris Craik}
321