1 /* 2 * Copyright (C) 2018 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.traceur.uitest; 18 19 import static org.junit.Assert.assertNotNull; 20 import static org.junit.Assert.assertTrue; 21 22 import android.content.Context; 23 import android.content.Intent; 24 import android.os.RemoteException; 25 import android.platform.test.annotations.Presubmit; 26 import android.support.test.uiautomator.By; 27 import android.support.test.uiautomator.UiDevice; 28 import android.support.test.uiautomator.UiObjectNotFoundException; 29 import android.support.test.uiautomator.UiSelector; 30 import android.support.test.uiautomator.UiScrollable; 31 import android.support.test.uiautomator.Until; 32 33 import androidx.test.InstrumentationRegistry; 34 import androidx.test.runner.AndroidJUnit4; 35 36 import org.junit.After; 37 import org.junit.Before; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 41 import java.util.regex.Pattern; 42 43 @RunWith(AndroidJUnit4.class) 44 public class TraceurAppTests { 45 46 private static final String TRACEUR_PACKAGE = "com.android.traceur"; 47 private static final int TIMEOUT = 20000; // milliseconds 48 49 private UiDevice mDevice; 50 51 @Before setUp()52 public void setUp() throws Exception { 53 mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); 54 55 try { 56 if (!mDevice.isScreenOn()) { 57 mDevice.wakeUp(); 58 } 59 60 // Press Menu to skip the lock screen. 61 // In case we weren't on the lock screen, press Home to return to a clean launcher. 62 mDevice.pressMenu(); 63 mDevice.pressHome(); 64 65 mDevice.setOrientationNatural(); 66 } catch (RemoteException e) { 67 throw new RuntimeException("Failed to freeze device orientation.", e); 68 } 69 70 mDevice.waitForIdle(); 71 72 Context context = InstrumentationRegistry.getContext(); 73 Intent intent = context.getPackageManager().getLaunchIntentForPackage(TRACEUR_PACKAGE); 74 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); // Clear out any previous instances 75 context.startActivity(intent); 76 77 // Wait for the app to appear 78 mDevice.wait(Until.hasObject(By.pkg(TRACEUR_PACKAGE).depth(0)), TIMEOUT); 79 } 80 81 @After tearDown()82 public void tearDown() throws Exception { 83 mDevice.unfreezeRotation(); 84 // Finish Traceur activity. 85 mDevice.pressBack(); 86 mDevice.pressHome(); 87 } 88 89 @Presubmit 90 @Test testElementsOnMainScreen()91 public void testElementsOnMainScreen() throws Exception { 92 UiScrollable scrollableMainScreen = new UiScrollable(new UiSelector().scrollable(true)); 93 94 if (scrollableMainScreen.exists()) { 95 scrollableMainScreen.setAsVerticalList(); 96 scrollableMainScreen.setMaxSearchSwipes(10); 97 98 boolean recordFound = scrollableMainScreen.scrollTextIntoView("Record trace"); 99 assertTrue("Record trace switch not found.", recordFound); 100 101 boolean applicationsFound = 102 scrollableMainScreen.scrollTextIntoView("Trace debuggable applications"); 103 assertTrue("Applications element not found.", applicationsFound); 104 105 boolean categoriesFound = scrollableMainScreen.scrollTextIntoView("Categories"); 106 assertTrue("Categories element not found.", categoriesFound); 107 108 boolean restoreFound = scrollableMainScreen.scrollTextIntoView("Restore default categories"); 109 assertTrue("Restore default categories element not found.", restoreFound); 110 111 boolean bufferSizeFound = scrollableMainScreen.scrollTextIntoView("Per-CPU buffer size"); 112 assertTrue("Per-CPU buffer size element not found.", bufferSizeFound); 113 114 boolean clearFound = scrollableMainScreen.scrollTextIntoView("Clear saved traces"); 115 assertTrue("Clear saved traces element not found.", clearFound); 116 117 boolean longTraceFound = scrollableMainScreen.scrollTextIntoView("Long traces"); 118 assertTrue("Long traces element not found.", longTraceFound); 119 120 boolean maxTraceSizeFound = scrollableMainScreen.scrollTextIntoView("Maximum long trace size"); 121 assertTrue("Maximum long trace size element not found.", maxTraceSizeFound); 122 123 boolean maxTraceDurationFound = 124 scrollableMainScreen.scrollTextIntoView("Maximum long trace duration"); 125 assertTrue("Maximum long trace duration element not found.", maxTraceDurationFound); 126 127 boolean quickSettingsFound = scrollableMainScreen.scrollTextIntoView("Show Quick Settings tile"); 128 assertTrue("Show Quick Settings tile switch not found.", quickSettingsFound); 129 } else { 130 assertNotNull("Record trace switch not found.", 131 mDevice.wait(Until.findObject(By.text("Record trace")), 132 TIMEOUT)); 133 assertNotNull("Applications element not found.", 134 mDevice.wait(Until.findObject(By.text("Trace debuggable applications")), 135 TIMEOUT)); 136 assertNotNull("Categories element not found.", 137 mDevice.wait(Until.findObject(By.text("Categories")), 138 TIMEOUT)); 139 assertNotNull("Restore default categories element not found.", 140 mDevice.wait(Until.findObject(By.text("Restore default categories")), 141 TIMEOUT)); 142 assertNotNull("Per-CPU buffer size element not found.", 143 mDevice.wait(Until.findObject(By.text("Per-CPU buffer size")), 144 TIMEOUT)); 145 assertNotNull("Clear saved traces element not found.", 146 mDevice.wait(Until.findObject(By.text("Clear saved traces")), 147 TIMEOUT)); 148 assertNotNull("Long traces element not found.", 149 mDevice.wait(Until.findObject(By.text("Long traces")), 150 TIMEOUT)); 151 assertNotNull("Maximum long trace size element not found.", 152 mDevice.wait(Until.findObject(By.text("Maximum long trace size")), 153 TIMEOUT)); 154 assertNotNull("Maximum long trace duration element not found.", 155 mDevice.wait(Until.findObject(By.text("Maximum long trace duration")), 156 TIMEOUT)); 157 assertNotNull("Show Quick Settings tile switch not found.", 158 mDevice.wait(Until.findObject(By.text("Show Quick Settings tile")), 159 TIMEOUT)); 160 } 161 } 162 163 /* 164 * In this test: 165 * Take a trace by toggling 'Record trace' in the UI 166 * Tap the notification once the trace is saved, and verify the share dialog appears. 167 */ 168 @Presubmit 169 @Test testSuccessfulTracing()170 public void testSuccessfulTracing() throws Exception { 171 mDevice.wait(Until.findObject(By.text("Record trace")), TIMEOUT); 172 173 mDevice.findObject(By.text("Record trace")).click(); 174 mDevice.wait(Until.hasObject(By.text("Trace is being recorded")), TIMEOUT); 175 mDevice.wait(Until.gone(By.text("Trace is being recorded")), TIMEOUT); 176 mDevice.findObject(By.text("Record trace")).click(); 177 178 // Wait for the popover notification to appear and then disappear, 179 // so we can reliably click the notification in the notification shade. 180 mDevice.wait(Until.hasObject(By.text("Tap to share your trace")), TIMEOUT); 181 mDevice.wait(Until.gone(By.text("Tap to share your trace")), TIMEOUT); 182 183 mDevice.openNotification(); 184 mDevice.wait(Until.hasObject(By.text("Tap to share your trace")), TIMEOUT); 185 mDevice.findObject(By.text("Tap to share your trace")).click(); 186 187 mDevice.wait(Until.hasObject(By.text("Only share system traces with people and apps you trust.")), TIMEOUT); 188 // The buttons on dialogs sometimes have their capitalization manipulated by themes. 189 mDevice.findObject(By.text(Pattern.compile("share", Pattern.CASE_INSENSITIVE))).click(); 190 191 mDevice.wait(Until.hasObject(By.text("Just once")), TIMEOUT); 192 } 193 } 194