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