Md Haque | aec7abc | 2015-06-10 18:31:28 -0700 | [diff] [blame] | 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.sysapp.janktests; |
| 18 | |
Gopinath | fe6f813 | 2015-09-13 17:23:36 -0700 | [diff] [blame] | 19 | import java.io.File; |
| 20 | import java.io.IOException; |
| 21 | |
Md Haque | e3080cb | 2015-06-23 14:41:15 -0700 | [diff] [blame] | 22 | import android.content.Intent; |
| 23 | import android.content.pm.PackageManager; |
Gopinath | fe6f813 | 2015-09-13 17:23:36 -0700 | [diff] [blame] | 24 | import android.os.Bundle; |
| 25 | import android.os.Environment; |
Md Haque | aec7abc | 2015-06-10 18:31:28 -0700 | [diff] [blame] | 26 | import android.os.RemoteException; |
| 27 | import android.os.SystemClock; |
| 28 | import android.support.test.jank.GfxMonitor; |
| 29 | import android.support.test.jank.JankTest; |
| 30 | import android.support.test.jank.JankTestBase; |
Md Haque | aec7abc | 2015-06-10 18:31:28 -0700 | [diff] [blame] | 31 | import android.support.test.uiautomator.By; |
| 32 | import android.support.test.uiautomator.BySelector; |
| 33 | import android.support.test.uiautomator.Direction; |
| 34 | import android.support.test.uiautomator.UiDevice; |
| 35 | import android.support.test.uiautomator.UiObject2; |
| 36 | import android.support.test.uiautomator.UiObjectNotFoundException; |
| 37 | import android.support.test.uiautomator.Until; |
| 38 | import junit.framework.Assert; |
Gopinath | fe6f813 | 2015-09-13 17:23:36 -0700 | [diff] [blame] | 39 | import android.support.test.timeresulthelper.TimeResultLogger; |
Md Haque | aec7abc | 2015-06-10 18:31:28 -0700 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Jank test for Chorme apps |
| 43 | * Open overflow menu |
| 44 | */ |
| 45 | |
| 46 | public class ChromeJankTests extends JankTestBase { |
| 47 | private static final int SHORT_TIMEOUT = 1000; |
| 48 | private static final int LONG_TIMEOUT = 30000; |
| 49 | private static final int INNER_LOOP = 5; |
| 50 | private static final int EXPECTED_FRAMES = 100; |
| 51 | private static final String PACKAGE_NAME = "com.android.chrome"; |
Md Haque | aec7abc | 2015-06-10 18:31:28 -0700 | [diff] [blame] | 52 | private UiDevice mDevice; |
Gopinath | fe6f813 | 2015-09-13 17:23:36 -0700 | [diff] [blame] | 53 | private static final File TIMESTAMP_FILE = new File(Environment.getExternalStorageDirectory() |
| 54 | .getAbsolutePath(), "autotester.log"); |
| 55 | private static final File RESULTS_FILE = new File(Environment.getExternalStorageDirectory() |
| 56 | .getAbsolutePath(), "results.log"); |
Md Haque | aec7abc | 2015-06-10 18:31:28 -0700 | [diff] [blame] | 57 | |
| 58 | @Override |
| 59 | public void setUp() throws Exception { |
| 60 | super.setUp(); |
| 61 | mDevice = UiDevice.getInstance(getInstrumentation()); |
| 62 | try { |
| 63 | mDevice.setOrientationNatural(); |
| 64 | } catch (RemoteException e) { |
| 65 | throw new RuntimeException("failed to freeze device orientaion", e); |
| 66 | } |
Md Haque | aec7abc | 2015-06-10 18:31:28 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | @Override |
| 70 | protected void tearDown() throws Exception { |
| 71 | mDevice.unfreezeRotation(); |
| 72 | super.tearDown(); |
| 73 | } |
| 74 | |
Md Haque | e3080cb | 2015-06-23 14:41:15 -0700 | [diff] [blame] | 75 | public void launchApp(String packageName) throws UiObjectNotFoundException{ |
| 76 | PackageManager pm = getInstrumentation().getContext().getPackageManager(); |
| 77 | Intent appIntent = pm.getLaunchIntentForPackage(packageName); |
| 78 | appIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 79 | getInstrumentation().getContext().startActivity(appIntent); |
| 80 | SystemClock.sleep(SHORT_TIMEOUT); |
| 81 | } |
| 82 | |
Gopinath | fe6f813 | 2015-09-13 17:23:36 -0700 | [diff] [blame] | 83 | public void launchChrome() throws UiObjectNotFoundException, IOException{ |
Md Haque | e3080cb | 2015-06-23 14:41:15 -0700 | [diff] [blame] | 84 | launchApp(PACKAGE_NAME); |
Md Haque | aec7abc | 2015-06-10 18:31:28 -0700 | [diff] [blame] | 85 | getOverflowMenu(); |
Gopinath | fe6f813 | 2015-09-13 17:23:36 -0700 | [diff] [blame] | 86 | TimeResultLogger.writeTimeStampLogStart(String.format("%s-%s", |
| 87 | getClass().getSimpleName(), getName()), TIMESTAMP_FILE); |
| 88 | } |
| 89 | |
| 90 | public void afterTestChromeOverflowMenuTap(Bundle metrics) throws IOException { |
| 91 | TimeResultLogger.writeTimeStampLogEnd(String.format("%s-%s", |
| 92 | getClass().getSimpleName(), getName()), TIMESTAMP_FILE); |
| 93 | TimeResultLogger.writeResultToFile(String.format("%s-%s", |
| 94 | getClass().getSimpleName(), getName()), RESULTS_FILE, metrics); |
| 95 | super.afterTest(metrics); |
Md Haque | aec7abc | 2015-06-10 18:31:28 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | // Measures jank window render for overflow menu tap |
Gopinath | fe6f813 | 2015-09-13 17:23:36 -0700 | [diff] [blame] | 99 | @JankTest(beforeTest="launchChrome", expectedFrames=EXPECTED_FRAMES, |
| 100 | afterTest="afterTestChromeOverflowMenuTap") |
Md Haque | aec7abc | 2015-06-10 18:31:28 -0700 | [diff] [blame] | 101 | @GfxMonitor(processName=PACKAGE_NAME) |
| 102 | public void testChromeOverflowMenuTap() { |
| 103 | for (int i = 0; i < INNER_LOOP; i++) { |
| 104 | UiObject2 overflow = getOverflowMenu(); |
| 105 | overflow.click(); |
| 106 | SystemClock.sleep(100); |
| 107 | mDevice.pressBack(); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | public UiObject2 getOverflowMenu() { |
Md Haque | e3080cb | 2015-06-23 14:41:15 -0700 | [diff] [blame] | 112 | UiObject2 overflow = mDevice.wait( |
| 113 | Until.findObject(By.desc("More options")), 5 * SHORT_TIMEOUT); |
| 114 | Assert.assertNotNull("Failed to locate overflow menu", overflow); |
| 115 | return overflow; |
Md Haque | aec7abc | 2015-06-10 18:31:28 -0700 | [diff] [blame] | 116 | } |
| 117 | } |