• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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.uibench.janktests;
18 
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.Bundle;
23 import android.os.SystemClock;
24 import android.support.test.uiautomator.By;
25 import android.support.test.uiautomator.Direction;
26 import android.support.test.uiautomator.UiDevice;
27 import android.support.test.uiautomator.UiObject2;
28 import android.support.test.uiautomator.Until;
29 import android.util.DisplayMetrics;
30 
31 import junit.framework.Assert;
32 
33 /**
34  * Jank benchmark tests helper for UiBench app
35  */
36 public class UiBenchJankTestsHelper {
37     public static final int LONG_TIMEOUT = 5000;
38     public static final int FULL_TEST_DURATION = 25000;
39     public static final int TIMEOUT = 250;
40     public static final int SHORT_TIMEOUT = 2000;
41     public static final int EXPECTED_FRAMES = 100;
42     public static final int KEY_DELAY = 1000;
43 
44     /**
45      * Only to be used for initial-fling tests, or similar cases
46      * where perf during brief experience is important.
47      */
48     public static final int SHORT_EXPECTED_FRAMES = 30;
49 
50     public static final String PACKAGE_NAME = "com.android.test.uibench";
51 
52     private static final int SLOW_FLING_SPEED = 3000; // compare to UiObject2#DEFAULT_FLING_SPEED
53 
54     private static UiBenchJankTestsHelper sInstance;
55     private UiDevice mDevice;
56     private Context mContext;
57     private DisplayMetrics mDisplayMetrics;
58     protected UiObject2 mContents;
59 
UiBenchJankTestsHelper(Context context, UiDevice device)60     private UiBenchJankTestsHelper(Context context, UiDevice device) {
61         mContext = context;
62         mDevice = device;
63         mDisplayMetrics = context.getResources().getDisplayMetrics();
64     }
65 
getInstance(Context context, UiDevice device)66     public static UiBenchJankTestsHelper getInstance(Context context, UiDevice device) {
67         if (sInstance == null) {
68             sInstance = new UiBenchJankTestsHelper(context, device);
69         }
70         return sInstance;
71     }
72 
73     /**
74      * Launch activity using intent
75      */
launchActivity(String activityName, Bundle extras, String verifyText)76     public void launchActivity(String activityName, Bundle extras, String verifyText) {
77         ComponentName cn = new ComponentName(PACKAGE_NAME,
78                 String.format("%s.%s", PACKAGE_NAME, activityName));
79         Intent intent = new Intent(Intent.ACTION_MAIN);
80         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
81         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
82         if (extras != null) {
83             intent.putExtras(extras);
84         }
85         intent.setComponent(cn);
86         // Launch the activity
87         mContext.startActivity(intent);
88         UiObject2 expectedTextCmp = mDevice.wait(Until.findObject(
89                 By.text(verifyText)), LONG_TIMEOUT);
90         Assert.assertNotNull(String.format("Issue in opening %s", activityName),
91                 expectedTextCmp);
92     }
93 
launchActivity(String activityName, String verifyText)94     public void launchActivity(String activityName, String verifyText) {
95         launchActivity(activityName, null, verifyText);
96     }
97 
launchActivityAndAssert(String activityName, String verifyText)98     public void launchActivityAndAssert(String activityName, String verifyText) {
99         launchActivity(activityName, verifyText);
100         mContents = mDevice.wait(Until.findObject(By.res("android", "content")), TIMEOUT);
101         Assert.assertNotNull(activityName + " isn't found", mContents);
102     }
103 
104     /**
105      * To perform the fling down and up on given content for flingCount number
106      * of times
107      */
flingUpDown(UiObject2 content, int flingCount)108     public void flingUpDown(UiObject2 content, int flingCount) {
109         flingUpDown(content, flingCount, false);
110     }
111 
flingUpDown(UiObject2 content, int flingCount, boolean reverse)112     public void flingUpDown(UiObject2 content, int flingCount, boolean reverse) {
113         for (int count = 0; count < flingCount; count++) {
114             SystemClock.sleep(SHORT_TIMEOUT);
115             content.fling(reverse ? Direction.UP : Direction.DOWN);
116             SystemClock.sleep(SHORT_TIMEOUT);
117             content.fling(reverse ? Direction.DOWN : Direction.UP);
118         }
119     }
120 
121     /**
122      * To perform the swipe right and left on given content for swipeCount number
123      * of times
124      */
swipeRightLeft(UiObject2 content, int swipeCount)125     public void swipeRightLeft(UiObject2 content, int swipeCount) {
126         for (int count = 0; count < swipeCount; count++) {
127             SystemClock.sleep(SHORT_TIMEOUT);
128             content.swipe(Direction.RIGHT, 1);
129             SystemClock.sleep(SHORT_TIMEOUT);
130             content.swipe(Direction.LEFT, 1);
131         }
132     }
133 
slowSingleFlingDown(UiObject2 content)134     public void slowSingleFlingDown(UiObject2 content) {
135         SystemClock.sleep(SHORT_TIMEOUT);
136         content.fling(Direction.DOWN, (int)(SLOW_FLING_SPEED * mDisplayMetrics.density));
137     }
138 
pressKeyCode(int keyCode)139     public void pressKeyCode(int keyCode) {
140         SystemClock.sleep(KEY_DELAY);
141         mDevice.pressKeyCode(keyCode);
142     }
143 }
144