1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14
15package com.android.uiautomator.platform;
16
17import com.android.uiautomator.core.UiObjectNotFoundException;
18import com.android.uiautomator.core.UiScrollable;
19import com.android.uiautomator.core.UiSelector;
20import com.android.uiautomator.janktesthelper.JankTestBase;
21import java.io.File;
22import java.io.IOException;
23
24/**
25 * Jank test for Android Webview.
26 *
27 * To run
28 * 1) Install the test application (com.android.webview.chromium.shell)
29 * 2) Place a directories containing the test pages on the test device in
30 *    $EXTERNAL_STORAGE/AwJankPages. Each directory should contain an index.html
31 *    file as the main file of the test page.
32 * 3) Build this test and push the resulting Jar file to /data/local/tmp/WebViewJankTests.jar
33 * 4) Run the test using the command:
34 *    adb shell uiautomator runtest WebViewJankTests.jar
35 *
36 * The test will run for each of the test pages. The results will be saved on the device in
37 * /data/local/tmp/jankoutput.txt.
38 */
39public class WebViewFlingTest extends JankTestBase {
40
41    private static final long TEST_DELAY_TIME_MS = 2 * 1000; // 2 seconds
42    private static final long PAGE_LOAD_DELAY_TIMEOUT_MS = 10 * 1000; // 10 seconds
43    private static final long PAGE_LOAD_DELAY_TIME_MS = 20 * 1000; // 20 seconds
44    private static final int MIN_DATA_SIZE = 50;
45    private static final String AW_WINDOW_NAME =
46            "com.android.webview.chromium.shell/com.android.webview.chromium.shell.JankActivity";
47    private static final String AW_CONTAINER = "com.android.webview.chromium.shell:id/container";
48    private static final String START_CMD =
49            "am start -n com.android.webview.chromium.shell/.JankActivity -d ";
50    private UiScrollable mWebPageDisplay = null;
51
52    public void testBrowserPageFling() throws UiObjectNotFoundException, IOException {
53        String url = mParams.getString("Url");
54        File webpage = new File(url);
55        assertNotNull("No test pages", webpage);
56        runBrowserPageFling(webpage);
57    }
58
59    private void resetFlingTest() throws UiObjectNotFoundException {
60        getContainer().flingToBeginning(20);
61    }
62
63    private void loadUrl(String url) throws IOException {
64        Runtime.getRuntime().exec(START_CMD + url);
65        // Need to find a good way of detecting when the page is loaded
66        sleep(PAGE_LOAD_DELAY_TIME_MS);
67    }
68
69    private void flingForward() throws UiObjectNotFoundException {
70        getContainer().flingForward();
71    }
72
73    private UiScrollable getContainer() {
74        if (mWebPageDisplay == null) {
75            mWebPageDisplay =
76                    new UiScrollable(new UiSelector().resourceId(AW_CONTAINER).instance(0));
77            assertTrue("Failed to get web container",
78                mWebPageDisplay.waitForExists(PAGE_LOAD_DELAY_TIMEOUT_MS));
79        }
80        return mWebPageDisplay;
81    }
82
83    /**
84    * {@inheritDoc}
85    */
86    @Override
87    protected void setUp() throws Exception {
88        super.setUp();
89        getUiDevice().setOrientationNatural();
90    }
91
92    /**
93     * {@inheritDoc}
94     */
95    @Override
96    protected void tearDown() throws Exception {
97        getUiDevice().unfreezeRotation();
98        super.tearDown();
99    }
100
101    private void runBrowserPageFling(File testFile) throws UiObjectNotFoundException, IOException {
102        loadUrl("file://" + testFile.getAbsolutePath());
103        for (int i = 0; i < getIteration(); i++) {
104            resetFlingTest();
105            sleep(TEST_DELAY_TIME_MS);
106            startTrace(mTestCaseName, i);
107            getSurfaceFlingerHelper().clearBuffer(AW_WINDOW_NAME);
108            flingForward();
109            sleep(DEFAULT_ANIMATION_TIME);
110            boolean result =
111                    getSurfaceFlingerHelper().dumpFrameLatency(AW_WINDOW_NAME, true);
112            assertTrue("dump frame latency failed", result);
113
114            waitForTrace();
115            assertTrue(String.format("Sample size is less than expected: %d", MIN_DATA_SIZE),
116                    validateResults(MIN_DATA_SIZE));
117            // record the result in an array
118            recordResults(mTestCaseName, i);
119        }
120        // calculate average and save the results
121        saveResults(mTestCaseName);
122    }
123}
124