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.SystemClock; 23 import android.support.test.uiautomator.By; 24 import android.support.test.uiautomator.Direction; 25 import android.support.test.uiautomator.UiDevice; 26 import android.support.test.uiautomator.UiObject2; 27 import android.support.test.uiautomator.Until; 28 import junit.framework.Assert; 29 /** 30 * Jank benchmark tests helper for UiBench app 31 */ 32 33 public class UiBenchJankTestsHelper { 34 public static final int LONG_TIMEOUT = 5000; 35 public static final int TIMEOUT = 250; 36 public static final int SHORT_TIMEOUT = 2000; 37 public static final int EXPECTED_FRAMES = 100; 38 39 public static final String PACKAGE_NAME = "com.android.test.uibench"; 40 41 private static UiBenchJankTestsHelper mInstance; 42 private static UiDevice mDevice; 43 private Context mContext; 44 protected UiObject2 mContents; 45 UiBenchJankTestsHelper(Context context, UiDevice device)46 private UiBenchJankTestsHelper(Context context, UiDevice device) { 47 mContext = context; 48 mDevice = device; 49 } 50 getInstance(Context context, UiDevice device)51 public static UiBenchJankTestsHelper getInstance(Context context, UiDevice device) { 52 if (mInstance == null) { 53 mInstance = new UiBenchJankTestsHelper(context, device); 54 } 55 return mInstance; 56 } 57 58 /** 59 * Launch activity using intent 60 * @param activityName 61 * @param verifyText 62 */ launchActivity(String activityName, String verifyText)63 public void launchActivity(String activityName, String verifyText) { 64 ComponentName cn = new ComponentName(PACKAGE_NAME, 65 String.format("%s.%s", PACKAGE_NAME, activityName)); 66 Intent intent = new Intent(Intent.ACTION_MAIN); 67 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 68 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); 69 intent.setComponent(cn); 70 // Launch the activity 71 mContext.startActivity(intent); 72 UiObject2 expectedTextCmp = mDevice.wait(Until.findObject( 73 By.text(verifyText)), LONG_TIMEOUT); 74 Assert.assertNotNull(String.format("Issue in opening %s", activityName), 75 expectedTextCmp); 76 } 77 launchActivityAndAssert(String activityName, String verifyText)78 public void launchActivityAndAssert(String activityName, String verifyText) { 79 launchActivity(activityName, verifyText); 80 mContents = mDevice.wait(Until.findObject(By.res("android", "content")), TIMEOUT); 81 Assert.assertNotNull(activityName + " isn't found", mContents); 82 } 83 84 /** 85 * To perform the fling down and up on given content for flingCount number 86 * of times 87 * @param content 88 * @param timeout 89 * @param flingCount 90 */ flingUpDown(UiObject2 content, long timeout, int flingCount)91 public void flingUpDown(UiObject2 content, long timeout, int flingCount) { 92 flingUpDown(content, timeout, flingCount, false); 93 } 94 flingUpDown(UiObject2 content, long timeout, int flingCount, boolean reverse)95 public void flingUpDown(UiObject2 content, long timeout, int flingCount, boolean reverse) { 96 for (int count = 0; count < flingCount; count++) { 97 SystemClock.sleep(timeout); 98 content.fling(reverse ? Direction.UP : Direction.DOWN); 99 SystemClock.sleep(timeout); 100 content.fling(reverse ? Direction.DOWN : Direction.UP); 101 } 102 } 103 104 } 105