1 /*
2  * Copyright (C) 2014 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 
17 package com.android.webview.chromium.tests.jank;
18 
19 import android.content.Intent;
20 import android.net.Uri;
21 import android.os.SystemClock;
22 import android.support.test.jank.JankTest;
23 import android.support.test.jank.JankTestBase;
24 import android.support.test.jank.GfxMonitor;
25 import android.support.test.jank.WindowContentFrameStatsMonitor;
26 import android.support.test.uiautomator.By;
27 import android.support.test.uiautomator.Direction;
28 import android.support.test.uiautomator.UiDevice;
29 import android.support.test.uiautomator.UiObject2;
30 import android.support.test.uiautomator.UiScrollable;
31 import android.support.test.uiautomator.UiSelector;
32 
33 import java.io.File;
34 import java.io.IOException;
35 
36 /**
37  * Jank test for Android Webview.
38  *
39  * To run
40  * 1) Install the test application (com.android.webview.chromium.shell)
41  * 2) Place a directories containing the test pages on the test device in
42  *    $EXTERNAL_STORAGE/AwJankPages. Each directory should contain an index.html
43  *    file as the main file of the test page.
44  * 3) Build this test and install the resulting apk file
45  * 4) Run the test using the command:
46  *    adb shell am instrument -e Url URL -w \
47  *            com.android.webview.chromium.tests.jank/android.test.InstrumentationTestRunner
48  *
49  */
50 public class WebViewFlingTest extends JankTestBase {
51 
52     private static final long TEST_DELAY_TIME_MS = 2 * 1000; // 2 seconds
53     private static final long PAGE_LOAD_DELAY_TIME_MS = 20 * 1000; // 20 seconds
54     private static final int MIN_DATA_SIZE = 50;
55     private static final long DEFAULT_ANIMATION_TIME = 2 * 1000;
56     private static final String CHROMIUM_SHELL_APP = "com.android.webview.chromium.shell";
57     private static final String CHROMIUM_SHELL_ACTIVITY = CHROMIUM_SHELL_APP + ".JankActivity";
58     private static final String RES_PACKAGE = "com.android.webview.chromium.shell";
59 
60     private UiDevice mDevice;
61 
62     /**
63     * {@inheritDoc}
64     */
65     @Override
setUp()66     protected void setUp() throws Exception {
67         super.setUp();
68 
69         mDevice = UiDevice.getInstance(getInstrumentation());
70         mDevice.setOrientationNatural();
71 
72         // Get the URL argument
73         String url = getArguments().getString("Url");
74         File webpage = new File(url);
75         assertNotNull("No test pages", webpage);
76 
77         // Launch the chromium shell
78         Intent intent = new Intent(Intent.ACTION_DEFAULT,
79                 Uri.parse("file://" + webpage.getAbsolutePath()));
80         intent.setClassName(CHROMIUM_SHELL_APP, CHROMIUM_SHELL_ACTIVITY);
81         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
82         getInstrumentation().getContext().startActivity(intent);
83         SystemClock.sleep(PAGE_LOAD_DELAY_TIME_MS);
84     }
85 
86     @Override
beforeLoop()87     public void beforeLoop() {
88         UiObject2 container = mDevice.findObject(By.res(RES_PACKAGE, "container"));
89 
90         // Fling to the top
91         while (container.fling(Direction.UP)) {
92         }
93 
94         SystemClock.sleep(TEST_DELAY_TIME_MS);
95     }
96 
97     @JankTest(expectedFrames=MIN_DATA_SIZE)
98     @WindowContentFrameStatsMonitor
testBrowserPageFling()99     public void testBrowserPageFling() throws IOException {
100         mDevice.findObject(By.res(RES_PACKAGE, "container")).fling(Direction.DOWN);
101         SystemClock.sleep(DEFAULT_ANIMATION_TIME);
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     @Override
tearDown()108     protected void tearDown() throws Exception {
109         mDevice.unfreezeRotation();
110 
111         super.tearDown();
112     }
113 }
114