Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [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.shell; |
| 18 | |
| 19 | import static android.test.MoreAsserts.assertContainsRegex; |
Felipe Leme | fcca68d | 2016-04-12 14:00:07 -0700 | [diff] [blame] | 20 | |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 21 | import static com.android.shell.ActionSendMultipleConsumerActivity.UI_NAME; |
Felipe Leme | fcca68d | 2016-04-12 14:00:07 -0700 | [diff] [blame] | 22 | import static com.android.shell.BugreportPrefs.PREFS_BUGREPORT; |
| 23 | import static com.android.shell.BugreportPrefs.STATE_HIDE; |
| 24 | import static com.android.shell.BugreportPrefs.STATE_SHOW; |
| 25 | import static com.android.shell.BugreportPrefs.STATE_UNKNOWN; |
| 26 | import static com.android.shell.BugreportPrefs.getWarningState; |
| 27 | import static com.android.shell.BugreportPrefs.setWarningState; |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 28 | import static com.android.shell.BugreportProgressService.EXTRA_BUGREPORT; |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 29 | import static com.android.shell.BugreportProgressService.EXTRA_ID; |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 30 | import static com.android.shell.BugreportProgressService.EXTRA_MAX; |
| 31 | import static com.android.shell.BugreportProgressService.EXTRA_NAME; |
| 32 | import static com.android.shell.BugreportProgressService.EXTRA_PID; |
Felipe Leme | b9238b3 | 2015-11-24 17:31:47 -0800 | [diff] [blame] | 33 | import static com.android.shell.BugreportProgressService.EXTRA_SCREENSHOT; |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 34 | import static com.android.shell.BugreportProgressService.INTENT_BUGREPORT_FINISHED; |
| 35 | import static com.android.shell.BugreportProgressService.INTENT_BUGREPORT_STARTED; |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 36 | import static com.android.shell.BugreportProgressService.SCREENSHOT_DELAY_SECONDS; |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 37 | |
Felipe Leme | 47ec8be | 2016-08-12 15:55:17 -0700 | [diff] [blame] | 38 | import static org.junit.Assert.assertEquals; |
| 39 | import static org.junit.Assert.assertFalse; |
| 40 | import static org.junit.Assert.assertNotNull; |
| 41 | import static org.junit.Assert.assertNull; |
| 42 | import static org.junit.Assert.assertTrue; |
| 43 | import static org.junit.Assert.fail; |
| 44 | |
KOUSHIK PANUGANTI | 390f4c0 | 2019-03-13 16:53:33 -0700 | [diff] [blame^] | 45 | import android.app.ActivityManager; |
| 46 | import android.app.ActivityManager.RunningServiceInfo; |
| 47 | import android.app.Instrumentation; |
| 48 | import android.app.NotificationManager; |
| 49 | import android.content.Context; |
| 50 | import android.content.Intent; |
| 51 | import android.net.Uri; |
| 52 | import android.os.Build; |
| 53 | import android.os.Bundle; |
| 54 | import android.os.SystemClock; |
| 55 | import android.os.SystemProperties; |
| 56 | import android.service.notification.StatusBarNotification; |
| 57 | import android.support.test.uiautomator.UiDevice; |
| 58 | import android.support.test.uiautomator.UiObject; |
| 59 | import android.support.test.uiautomator.UiObjectNotFoundException; |
| 60 | import android.text.TextUtils; |
| 61 | import android.text.format.DateUtils; |
| 62 | import android.util.Log; |
| 63 | |
| 64 | import androidx.test.InstrumentationRegistry; |
| 65 | import androidx.test.filters.LargeTest; |
| 66 | import androidx.test.runner.AndroidJUnit4; |
| 67 | |
| 68 | import com.android.shell.ActionSendMultipleConsumerActivity.CustomActionSendMultipleListener; |
| 69 | |
| 70 | import libcore.io.Streams; |
| 71 | |
| 72 | import org.junit.After; |
| 73 | import org.junit.Before; |
| 74 | import org.junit.Rule; |
| 75 | import org.junit.Test; |
| 76 | import org.junit.rules.TestName; |
| 77 | import org.junit.runner.RunWith; |
| 78 | |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 79 | import java.io.BufferedOutputStream; |
| 80 | import java.io.BufferedWriter; |
| 81 | import java.io.ByteArrayOutputStream; |
Felipe Leme | a0bf033 | 2015-12-11 09:35:03 -0800 | [diff] [blame] | 82 | import java.io.File; |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 83 | import java.io.FileOutputStream; |
| 84 | import java.io.IOException; |
| 85 | import java.io.InputStream; |
| 86 | import java.io.OutputStreamWriter; |
| 87 | import java.io.Writer; |
Felipe Leme | e86b63b | 2016-02-08 09:39:50 -0800 | [diff] [blame] | 88 | import java.text.NumberFormat; |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 89 | import java.util.ArrayList; |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 90 | import java.util.List; |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 91 | import java.util.SortedSet; |
| 92 | import java.util.TreeSet; |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 93 | import java.util.zip.ZipEntry; |
| 94 | import java.util.zip.ZipInputStream; |
| 95 | import java.util.zip.ZipOutputStream; |
| 96 | |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 97 | /** |
| 98 | * Integration tests for {@link BugreportReceiver}. |
| 99 | * <p> |
| 100 | * These tests don't mock any component and rely on external UI components (like the notification |
| 101 | * bar and activity chooser), which can make them unreliable and slow. |
| 102 | * <p> |
| 103 | * The general workflow is: |
| 104 | * <ul> |
| 105 | * <li>creates the bug report files |
| 106 | * <li>generates the BUGREPORT_FINISHED intent |
| 107 | * <li>emulate user actions to share the intent with a custom activity |
| 108 | * <li>asserts the extras received by the custom activity |
| 109 | * </ul> |
| 110 | * <p> |
Felipe Leme | 6bbb6b9 | 2015-12-07 17:43:55 -0800 | [diff] [blame] | 111 | * <strong>NOTE</strong>: these tests only work if the device is unlocked. |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 112 | */ |
Felipe Leme | 2bfa085 | 2015-12-10 12:43:31 -0800 | [diff] [blame] | 113 | @LargeTest |
Felipe Leme | 47ec8be | 2016-08-12 15:55:17 -0700 | [diff] [blame] | 114 | @RunWith(AndroidJUnit4.class) |
| 115 | public class BugreportReceiverTest { |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 116 | private static final String TAG = "BugreportReceiverTest"; |
| 117 | |
| 118 | // Timeout for UI operations, in milliseconds. |
Felipe Leme | fa26a0e | 2016-10-28 09:32:03 -0700 | [diff] [blame] | 119 | private static final int TIMEOUT = (int) (5 * DateUtils.SECOND_IN_MILLIS); |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 120 | |
Felipe Leme | c4f64677 | 2016-01-12 18:12:09 -0800 | [diff] [blame] | 121 | // Timeout for when waiting for a screenshot to finish. |
| 122 | private static final int SAFE_SCREENSHOT_DELAY = SCREENSHOT_DELAY_SECONDS + 10; |
| 123 | |
Felipe Leme | a0bf033 | 2015-12-11 09:35:03 -0800 | [diff] [blame] | 124 | private static final String BUGREPORTS_DIR = "bugreports"; |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 125 | private static final String BUGREPORT_FILE = "test_bugreport.txt"; |
| 126 | private static final String ZIP_FILE = "test_bugreport.zip"; |
Felipe Leme | 2628878 | 2016-02-25 12:10:43 -0800 | [diff] [blame] | 127 | private static final String ZIP_FILE2 = "test_bugreport2.zip"; |
Felipe Leme | a0bf033 | 2015-12-11 09:35:03 -0800 | [diff] [blame] | 128 | private static final String SCREENSHOT_FILE = "test_screenshot.png"; |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 129 | |
| 130 | private static final String BUGREPORT_CONTENT = "Dump, might as well dump!\n"; |
| 131 | private static final String SCREENSHOT_CONTENT = "A picture is worth a thousand words!\n"; |
| 132 | |
Felipe Leme | 93702ab | 2015-12-11 13:06:45 -0800 | [diff] [blame] | 133 | private static final int PID = 42; |
Felipe Leme | 2628878 | 2016-02-25 12:10:43 -0800 | [diff] [blame] | 134 | private static final int PID2 = 24; |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 135 | private static final int ID = 108; |
Felipe Leme | 2628878 | 2016-02-25 12:10:43 -0800 | [diff] [blame] | 136 | private static final int ID2 = 801; |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 137 | private static final String PROGRESS_PROPERTY = "dumpstate." + PID + ".progress"; |
| 138 | private static final String MAX_PROPERTY = "dumpstate." + PID + ".max"; |
| 139 | private static final String NAME_PROPERTY = "dumpstate." + PID + ".name"; |
Felipe Leme | 93702ab | 2015-12-11 13:06:45 -0800 | [diff] [blame] | 140 | private static final String NAME = "BUG, Y U NO REPORT?"; |
Felipe Leme | 2628878 | 2016-02-25 12:10:43 -0800 | [diff] [blame] | 141 | private static final String NAME2 = "A bugreport's life"; |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 142 | private static final String NEW_NAME = "Bug_Forrest_Bug"; |
Felipe Leme | 2628878 | 2016-02-25 12:10:43 -0800 | [diff] [blame] | 143 | private static final String NEW_NAME2 = "BugsyReportsy"; |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 144 | private static final String TITLE = "Wimbugdom Champion 2015"; |
Felipe Leme | 2628878 | 2016-02-25 12:10:43 -0800 | [diff] [blame] | 145 | private static final String TITLE2 = "Master of the Universe"; |
| 146 | private static final String DESCRIPTION = "One's description..."; |
| 147 | private static final String DESCRIPTION2 = "...is another's treasure."; |
Felipe Leme | 4967f73 | 2016-01-06 11:38:53 -0800 | [diff] [blame] | 148 | |
| 149 | private static final String NO_DESCRIPTION = null; |
| 150 | private static final String NO_NAME = null; |
| 151 | private static final String NO_SCREENSHOT = null; |
| 152 | private static final String NO_TITLE = null; |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 153 | private static final int NO_ID = 0; |
Felipe Leme | c8e2b60 | 2016-01-29 13:55:35 -0800 | [diff] [blame] | 154 | private static final boolean RENAMED_SCREENSHOTS = true; |
| 155 | private static final boolean DIDNT_RENAME_SCREENSHOTS = false; |
Felipe Leme | 4967f73 | 2016-01-06 11:38:53 -0800 | [diff] [blame] | 156 | |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 157 | private String mDescription; |
Felipe Leme | 93702ab | 2015-12-11 13:06:45 -0800 | [diff] [blame] | 158 | |
Felipe Leme | a0bf033 | 2015-12-11 09:35:03 -0800 | [diff] [blame] | 159 | private String mPlainTextPath; |
| 160 | private String mZipPath; |
Felipe Leme | 2628878 | 2016-02-25 12:10:43 -0800 | [diff] [blame] | 161 | private String mZipPath2; |
Felipe Leme | a0bf033 | 2015-12-11 09:35:03 -0800 | [diff] [blame] | 162 | private String mScreenshotPath; |
| 163 | |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 164 | private Context mContext; |
| 165 | private UiBot mUiBot; |
| 166 | private CustomActionSendMultipleListener mListener; |
| 167 | |
Felipe Leme | 47ec8be | 2016-08-12 15:55:17 -0700 | [diff] [blame] | 168 | @Rule public TestName mName = new TestName(); |
| 169 | |
| 170 | @Before |
| 171 | public void setUp() throws Exception { |
Felipe Leme | ca5002f | 2016-08-03 11:51:00 -0700 | [diff] [blame] | 172 | Log.i(TAG, getName() + ".setup()"); |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 173 | Instrumentation instrumentation = getInstrumentation(); |
| 174 | mContext = instrumentation.getTargetContext(); |
Felipe Leme | a86a301 | 2016-07-15 10:40:00 -0700 | [diff] [blame] | 175 | mUiBot = new UiBot(instrumentation, TIMEOUT); |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 176 | mListener = ActionSendMultipleConsumerActivity.getListener(mContext); |
Felipe Leme | 93702ab | 2015-12-11 13:06:45 -0800 | [diff] [blame] | 177 | |
| 178 | cancelExistingNotifications(); |
| 179 | |
Felipe Leme | a0bf033 | 2015-12-11 09:35:03 -0800 | [diff] [blame] | 180 | mPlainTextPath = getPath(BUGREPORT_FILE); |
| 181 | mZipPath = getPath(ZIP_FILE); |
Felipe Leme | 2628878 | 2016-02-25 12:10:43 -0800 | [diff] [blame] | 182 | mZipPath2 = getPath(ZIP_FILE2); |
Felipe Leme | a0bf033 | 2015-12-11 09:35:03 -0800 | [diff] [blame] | 183 | mScreenshotPath = getPath(SCREENSHOT_FILE); |
Felipe Leme | 93702ab | 2015-12-11 13:06:45 -0800 | [diff] [blame] | 184 | createTextFile(mPlainTextPath, BUGREPORT_CONTENT); |
| 185 | createTextFile(mScreenshotPath, SCREENSHOT_CONTENT); |
| 186 | createZipFile(mZipPath, BUGREPORT_FILE, BUGREPORT_CONTENT); |
Felipe Leme | 2628878 | 2016-02-25 12:10:43 -0800 | [diff] [blame] | 187 | createZipFile(mZipPath2, BUGREPORT_FILE, BUGREPORT_CONTENT); |
Felipe Leme | 93702ab | 2015-12-11 13:06:45 -0800 | [diff] [blame] | 188 | |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 189 | // Creates a multi-line description. |
| 190 | StringBuilder sb = new StringBuilder(); |
| 191 | for (int i = 1; i <= 20; i++) { |
| 192 | sb.append("All work and no play makes Shell a dull app!\n"); |
| 193 | } |
| 194 | mDescription = sb.toString(); |
| 195 | |
Felipe Leme | fcca68d | 2016-04-12 14:00:07 -0700 | [diff] [blame] | 196 | setWarningState(mContext, STATE_HIDE); |
Felipe Leme | 91699af | 2016-07-28 15:16:55 -0700 | [diff] [blame] | 197 | |
| 198 | mUiBot.turnScreenOn(); |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Felipe Leme | 47ec8be | 2016-08-12 15:55:17 -0700 | [diff] [blame] | 201 | @After |
| 202 | public void tearDown() throws Exception { |
Felipe Leme | ca5002f | 2016-08-03 11:51:00 -0700 | [diff] [blame] | 203 | Log.i(TAG, getName() + ".tearDown()"); |
Felipe Leme | 1ac5441 | 2016-10-11 09:17:50 -0700 | [diff] [blame] | 204 | try { |
| 205 | cancelExistingNotifications(); |
| 206 | } finally { |
| 207 | // Collapses just in case, so a failure here does not compromise tests on other classes. |
| 208 | mUiBot.collapseStatusBar(); |
| 209 | } |
Felipe Leme | ca5002f | 2016-08-03 11:51:00 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Felipe Leme | fa26a0e | 2016-10-28 09:32:03 -0700 | [diff] [blame] | 212 | /* |
| 213 | * TODO: this test is incomplete because: |
| 214 | * - the assertProgressNotification() is not really asserting the progress because the |
| 215 | * UI automation API doesn't provide a way to check the notification progress bar value |
| 216 | * - it should use the binder object instead of SystemProperties to update progress |
| 217 | */ |
Felipe Leme | 47ec8be | 2016-08-12 15:55:17 -0700 | [diff] [blame] | 218 | @Test |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 219 | public void testProgress() throws Exception { |
Felipe Leme | 93702ab | 2015-12-11 13:06:45 -0800 | [diff] [blame] | 220 | resetProperties(); |
| 221 | sendBugreportStarted(1000); |
Felipe Leme | 2288129 | 2016-01-06 09:57:23 -0800 | [diff] [blame] | 222 | waitForScreenshotButtonEnabled(true); |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 223 | |
Felipe Leme | 9ca5e56 | 2016-04-11 10:27:02 -0700 | [diff] [blame] | 224 | assertProgressNotification(NAME, 0f); |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 225 | |
Felipe Leme | 93702ab | 2015-12-11 13:06:45 -0800 | [diff] [blame] | 226 | SystemProperties.set(PROGRESS_PROPERTY, "108"); |
Felipe Leme | 9ca5e56 | 2016-04-11 10:27:02 -0700 | [diff] [blame] | 227 | assertProgressNotification(NAME, 10.80f); |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 228 | |
Felipe Leme | 9ca5e56 | 2016-04-11 10:27:02 -0700 | [diff] [blame] | 229 | assertProgressNotification(NAME, 50.00f); |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 230 | |
Felipe Leme | 3fc44b9 | 2016-03-21 17:34:21 -0700 | [diff] [blame] | 231 | SystemProperties.set(PROGRESS_PROPERTY, "950"); |
Felipe Leme | 9ca5e56 | 2016-04-11 10:27:02 -0700 | [diff] [blame] | 232 | assertProgressNotification(NAME, 95.00f); |
Felipe Leme | 3fc44b9 | 2016-03-21 17:34:21 -0700 | [diff] [blame] | 233 | |
| 234 | // Make sure progress never goes back... |
Felipe Leme | 93702ab | 2015-12-11 13:06:45 -0800 | [diff] [blame] | 235 | SystemProperties.set(MAX_PROPERTY, "2000"); |
Felipe Leme | 9ca5e56 | 2016-04-11 10:27:02 -0700 | [diff] [blame] | 236 | assertProgressNotification(NAME, 95.00f); |
Felipe Leme | 3fc44b9 | 2016-03-21 17:34:21 -0700 | [diff] [blame] | 237 | |
| 238 | SystemProperties.set(PROGRESS_PROPERTY, "1000"); |
Felipe Leme | 9ca5e56 | 2016-04-11 10:27:02 -0700 | [diff] [blame] | 239 | assertProgressNotification(NAME, 95.00f); |
Felipe Leme | 3fc44b9 | 2016-03-21 17:34:21 -0700 | [diff] [blame] | 240 | |
| 241 | // ...only forward... |
| 242 | SystemProperties.set(PROGRESS_PROPERTY, "1902"); |
Felipe Leme | 9ca5e56 | 2016-04-11 10:27:02 -0700 | [diff] [blame] | 243 | assertProgressNotification(NAME, 95.10f); |
Felipe Leme | 3fc44b9 | 2016-03-21 17:34:21 -0700 | [diff] [blame] | 244 | |
| 245 | SystemProperties.set(PROGRESS_PROPERTY, "1960"); |
Felipe Leme | 9ca5e56 | 2016-04-11 10:27:02 -0700 | [diff] [blame] | 246 | assertProgressNotification(NAME, 98.00f); |
Felipe Leme | 3fc44b9 | 2016-03-21 17:34:21 -0700 | [diff] [blame] | 247 | |
| 248 | // ...but never more than the capped value. |
| 249 | SystemProperties.set(PROGRESS_PROPERTY, "2000"); |
Felipe Leme | 9ca5e56 | 2016-04-11 10:27:02 -0700 | [diff] [blame] | 250 | assertProgressNotification(NAME, 99.00f); |
Felipe Leme | 3fc44b9 | 2016-03-21 17:34:21 -0700 | [diff] [blame] | 251 | |
| 252 | SystemProperties.set(PROGRESS_PROPERTY, "3000"); |
Felipe Leme | 9ca5e56 | 2016-04-11 10:27:02 -0700 | [diff] [blame] | 253 | assertProgressNotification(NAME, 99.00f); |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 254 | |
Felipe Leme | 93702ab | 2015-12-11 13:06:45 -0800 | [diff] [blame] | 255 | Bundle extras = |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 256 | sendBugreportFinishedAndGetSharedIntent(ID, mPlainTextPath, mScreenshotPath); |
| 257 | assertActionSendMultiple(extras, BUGREPORT_CONTENT, SCREENSHOT_CONTENT, ID, PID, ZIP_FILE, |
Felipe Leme | 079f896 | 2016-04-18 12:04:23 -0700 | [diff] [blame] | 258 | NAME, NO_TITLE, NO_DESCRIPTION, 0, RENAMED_SCREENSHOTS); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 259 | |
| 260 | assertServiceNotRunning(); |
| 261 | } |
| 262 | |
Felipe Leme | 47ec8be | 2016-08-12 15:55:17 -0700 | [diff] [blame] | 263 | @Test |
Felipe Leme | db31363 | 2016-02-25 17:06:58 -0800 | [diff] [blame] | 264 | public void testProgress_cancel() throws Exception { |
| 265 | resetProperties(); |
| 266 | sendBugreportStarted(1000); |
| 267 | waitForScreenshotButtonEnabled(true); |
| 268 | |
| 269 | final NumberFormat nf = NumberFormat.getPercentInstance(); |
| 270 | nf.setMinimumFractionDigits(2); |
| 271 | nf.setMaximumFractionDigits(2); |
| 272 | |
Felipe Leme | 9ca5e56 | 2016-04-11 10:27:02 -0700 | [diff] [blame] | 273 | assertProgressNotification(NAME, 00.00f); |
Felipe Leme | db31363 | 2016-02-25 17:06:58 -0800 | [diff] [blame] | 274 | |
Felipe Leme | a86a301 | 2016-07-15 10:40:00 -0700 | [diff] [blame] | 275 | cancelFromNotification(); |
Felipe Leme | db31363 | 2016-02-25 17:06:58 -0800 | [diff] [blame] | 276 | |
| 277 | waitForService(false); |
| 278 | } |
| 279 | |
Felipe Leme | 47ec8be | 2016-08-12 15:55:17 -0700 | [diff] [blame] | 280 | @Test |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 281 | public void testProgress_takeExtraScreenshot() throws Exception { |
| 282 | resetProperties(); |
| 283 | sendBugreportStarted(1000); |
| 284 | |
| 285 | waitForScreenshotButtonEnabled(true); |
| 286 | takeScreenshot(); |
| 287 | assertScreenshotButtonEnabled(false); |
| 288 | waitForScreenshotButtonEnabled(true); |
| 289 | |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 290 | sendBugreportFinished(ID, mPlainTextPath, mScreenshotPath); |
Felipe Leme | c4f64677 | 2016-01-12 18:12:09 -0800 | [diff] [blame] | 291 | |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 292 | Bundle extras = acceptBugreportAndGetSharedIntent(ID); |
| 293 | assertActionSendMultiple(extras, BUGREPORT_CONTENT, SCREENSHOT_CONTENT, ID, PID, ZIP_FILE, |
Felipe Leme | 079f896 | 2016-04-18 12:04:23 -0700 | [diff] [blame] | 294 | NAME, NO_TITLE, NO_DESCRIPTION, 1, RENAMED_SCREENSHOTS); |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 295 | |
Felipe Leme | 93702ab | 2015-12-11 13:06:45 -0800 | [diff] [blame] | 296 | assertServiceNotRunning(); |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Felipe Leme | 47ec8be | 2016-08-12 15:55:17 -0700 | [diff] [blame] | 299 | @Test |
Felipe Leme | c4f64677 | 2016-01-12 18:12:09 -0800 | [diff] [blame] | 300 | public void testScreenshotFinishesAfterBugreport() throws Exception { |
Felipe Leme | c4f64677 | 2016-01-12 18:12:09 -0800 | [diff] [blame] | 301 | resetProperties(); |
| 302 | |
| 303 | sendBugreportStarted(1000); |
Felipe Leme | 079f896 | 2016-04-18 12:04:23 -0700 | [diff] [blame] | 304 | waitForScreenshotButtonEnabled(true); |
| 305 | takeScreenshot(); |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 306 | sendBugreportFinished(ID, mPlainTextPath, NO_SCREENSHOT); |
| 307 | waitShareNotification(ID); |
Felipe Leme | c4f64677 | 2016-01-12 18:12:09 -0800 | [diff] [blame] | 308 | |
| 309 | // There's no indication in the UI about the screenshot finish, so just sleep like a baby... |
Felipe Leme | ca5002f | 2016-08-03 11:51:00 -0700 | [diff] [blame] | 310 | sleep(SAFE_SCREENSHOT_DELAY * DateUtils.SECOND_IN_MILLIS); |
Felipe Leme | c4f64677 | 2016-01-12 18:12:09 -0800 | [diff] [blame] | 311 | |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 312 | Bundle extras = acceptBugreportAndGetSharedIntent(ID); |
| 313 | assertActionSendMultiple(extras, BUGREPORT_CONTENT, NO_SCREENSHOT, ID, PID, ZIP_FILE, |
Felipe Leme | c8e2b60 | 2016-01-29 13:55:35 -0800 | [diff] [blame] | 314 | NAME, NO_TITLE, NO_DESCRIPTION, 1, RENAMED_SCREENSHOTS); |
Felipe Leme | c4f64677 | 2016-01-12 18:12:09 -0800 | [diff] [blame] | 315 | |
| 316 | assertServiceNotRunning(); |
| 317 | } |
| 318 | |
Felipe Leme | 47ec8be | 2016-08-12 15:55:17 -0700 | [diff] [blame] | 319 | @Test |
Felipe Leme | 4967f73 | 2016-01-06 11:38:53 -0800 | [diff] [blame] | 320 | public void testProgress_changeDetailsInvalidInput() throws Exception { |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 321 | resetProperties(); |
| 322 | sendBugreportStarted(1000); |
Felipe Leme | 2288129 | 2016-01-06 09:57:23 -0800 | [diff] [blame] | 323 | waitForScreenshotButtonEnabled(true); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 324 | |
Felipe Leme | ca5002f | 2016-08-03 11:51:00 -0700 | [diff] [blame] | 325 | DetailsUi detailsUi = new DetailsUi(mUiBot, ID, NAME); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 326 | |
| 327 | // Check initial name. |
Felipe Leme | 2628878 | 2016-02-25 12:10:43 -0800 | [diff] [blame] | 328 | detailsUi.assertName(NAME); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 329 | |
| 330 | // Change name - it should have changed system property once focus is changed. |
Felipe Leme | 208b188 | 2016-03-14 18:03:41 -0700 | [diff] [blame] | 331 | detailsUi.focusOnName(); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 332 | detailsUi.nameField.setText(NEW_NAME); |
| 333 | detailsUi.focusAwayFromName(); |
| 334 | assertPropertyValue(NAME_PROPERTY, NEW_NAME); |
| 335 | |
| 336 | // Cancel the dialog to make sure property was restored. |
| 337 | detailsUi.clickCancel(); |
| 338 | assertPropertyValue(NAME_PROPERTY, NAME); |
| 339 | |
| 340 | // Now try to set an invalid name. |
Felipe Leme | ca5002f | 2016-08-03 11:51:00 -0700 | [diff] [blame] | 341 | detailsUi.reOpen(NAME); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 342 | detailsUi.nameField.setText("/etc/passwd"); |
| 343 | detailsUi.clickOk(); |
| 344 | assertPropertyValue(NAME_PROPERTY, "_etc_passwd"); |
| 345 | |
| 346 | // Finally, make the real changes. |
Felipe Leme | ca5002f | 2016-08-03 11:51:00 -0700 | [diff] [blame] | 347 | detailsUi.reOpen("_etc_passwd"); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 348 | detailsUi.nameField.setText(NEW_NAME); |
| 349 | detailsUi.titleField.setText(TITLE); |
| 350 | detailsUi.descField.setText(mDescription); |
| 351 | |
| 352 | detailsUi.clickOk(); |
| 353 | |
| 354 | assertPropertyValue(NAME_PROPERTY, NEW_NAME); |
Felipe Leme | 9ca5e56 | 2016-04-11 10:27:02 -0700 | [diff] [blame] | 355 | assertProgressNotification(NEW_NAME, 00.00f); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 356 | |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 357 | Bundle extras = sendBugreportFinishedAndGetSharedIntent(ID, mPlainTextPath, |
Felipe Leme | 6540574 | 2017-05-24 11:51:10 -0700 | [diff] [blame] | 358 | mScreenshotPath, TITLE); |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 359 | assertActionSendMultiple(extras, BUGREPORT_CONTENT, SCREENSHOT_CONTENT, ID, PID, TITLE, |
Felipe Leme | 079f896 | 2016-04-18 12:04:23 -0700 | [diff] [blame] | 360 | NEW_NAME, TITLE, mDescription, 0, RENAMED_SCREENSHOTS); |
Felipe Leme | 4967f73 | 2016-01-06 11:38:53 -0800 | [diff] [blame] | 361 | |
| 362 | assertServiceNotRunning(); |
| 363 | } |
| 364 | |
Felipe Leme | 47ec8be | 2016-08-12 15:55:17 -0700 | [diff] [blame] | 365 | @Test |
Felipe Leme | a86a301 | 2016-07-15 10:40:00 -0700 | [diff] [blame] | 366 | public void testProgress_cancelBugClosesDetailsDialog() throws Exception { |
| 367 | resetProperties(); |
| 368 | sendBugreportStarted(1000); |
| 369 | waitForScreenshotButtonEnabled(true); |
| 370 | |
Felipe Leme | ca5002f | 2016-08-03 11:51:00 -0700 | [diff] [blame] | 371 | DetailsUi detailsUi = new DetailsUi(mUiBot, ID, NAME); |
Felipe Leme | a86a301 | 2016-07-15 10:40:00 -0700 | [diff] [blame] | 372 | detailsUi.assertName(NAME); // Sanity check |
| 373 | |
| 374 | cancelFromNotification(); |
Felipe Leme | 1ac5441 | 2016-10-11 09:17:50 -0700 | [diff] [blame] | 375 | mUiBot.collapseStatusBar(); |
Felipe Leme | a86a301 | 2016-07-15 10:40:00 -0700 | [diff] [blame] | 376 | |
| 377 | assertDetailsUiClosed(); |
| 378 | assertServiceNotRunning(); |
| 379 | } |
| 380 | |
Felipe Leme | 47ec8be | 2016-08-12 15:55:17 -0700 | [diff] [blame] | 381 | @Test |
Felipe Leme | 4967f73 | 2016-01-06 11:38:53 -0800 | [diff] [blame] | 382 | public void testProgress_changeDetailsPlainBugreport() throws Exception { |
| 383 | changeDetailsTest(true); |
| 384 | } |
| 385 | |
Felipe Leme | 47ec8be | 2016-08-12 15:55:17 -0700 | [diff] [blame] | 386 | @Test |
Felipe Leme | 4967f73 | 2016-01-06 11:38:53 -0800 | [diff] [blame] | 387 | public void testProgress_changeDetailsZippedBugreport() throws Exception { |
| 388 | changeDetailsTest(false); |
| 389 | } |
| 390 | |
Felipe Leme | ca5002f | 2016-08-03 11:51:00 -0700 | [diff] [blame] | 391 | private void changeDetailsTest(boolean plainText) throws Exception { |
Felipe Leme | 4967f73 | 2016-01-06 11:38:53 -0800 | [diff] [blame] | 392 | resetProperties(); |
| 393 | sendBugreportStarted(1000); |
| 394 | waitForScreenshotButtonEnabled(true); |
| 395 | |
Felipe Leme | ca5002f | 2016-08-03 11:51:00 -0700 | [diff] [blame] | 396 | DetailsUi detailsUi = new DetailsUi(mUiBot, ID, NAME); |
Felipe Leme | 4967f73 | 2016-01-06 11:38:53 -0800 | [diff] [blame] | 397 | |
| 398 | // Check initial name. |
Felipe Leme | 2628878 | 2016-02-25 12:10:43 -0800 | [diff] [blame] | 399 | detailsUi.assertName(NAME); |
Felipe Leme | 4967f73 | 2016-01-06 11:38:53 -0800 | [diff] [blame] | 400 | |
| 401 | // Change fields. |
Felipe Leme | ca5002f | 2016-08-03 11:51:00 -0700 | [diff] [blame] | 402 | detailsUi.reOpen(NAME); |
Felipe Leme | 4967f73 | 2016-01-06 11:38:53 -0800 | [diff] [blame] | 403 | detailsUi.nameField.setText(NEW_NAME); |
| 404 | detailsUi.titleField.setText(TITLE); |
| 405 | detailsUi.descField.setText(mDescription); |
| 406 | |
| 407 | detailsUi.clickOk(); |
| 408 | |
| 409 | assertPropertyValue(NAME_PROPERTY, NEW_NAME); |
Felipe Leme | 9ca5e56 | 2016-04-11 10:27:02 -0700 | [diff] [blame] | 410 | assertProgressNotification(NEW_NAME, 00.00f); |
Felipe Leme | 4967f73 | 2016-01-06 11:38:53 -0800 | [diff] [blame] | 411 | |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 412 | Bundle extras = sendBugreportFinishedAndGetSharedIntent(ID, |
Felipe Leme | 6540574 | 2017-05-24 11:51:10 -0700 | [diff] [blame] | 413 | plainText? mPlainTextPath : mZipPath, mScreenshotPath, TITLE); |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 414 | assertActionSendMultiple(extras, BUGREPORT_CONTENT, SCREENSHOT_CONTENT, ID, PID, TITLE, |
Felipe Leme | 079f896 | 2016-04-18 12:04:23 -0700 | [diff] [blame] | 415 | NEW_NAME, TITLE, mDescription, 0, RENAMED_SCREENSHOTS); |
Felipe Leme | c8e2b60 | 2016-01-29 13:55:35 -0800 | [diff] [blame] | 416 | |
| 417 | assertServiceNotRunning(); |
| 418 | } |
| 419 | |
Felipe Leme | 47ec8be | 2016-08-12 15:55:17 -0700 | [diff] [blame] | 420 | @Test |
Felipe Leme | db31363 | 2016-02-25 17:06:58 -0800 | [diff] [blame] | 421 | public void testProgress_changeJustDetailsTouchingDetails() throws Exception { |
| 422 | changeJustDetailsTest(true); |
| 423 | } |
| 424 | |
Felipe Leme | 47ec8be | 2016-08-12 15:55:17 -0700 | [diff] [blame] | 425 | @Test |
Felipe Leme | db31363 | 2016-02-25 17:06:58 -0800 | [diff] [blame] | 426 | public void testProgress_changeJustDetailsTouchingNotification() throws Exception { |
| 427 | changeJustDetailsTest(false); |
| 428 | } |
| 429 | |
| 430 | private void changeJustDetailsTest(boolean touchDetails) throws Exception { |
Felipe Leme | c8e2b60 | 2016-01-29 13:55:35 -0800 | [diff] [blame] | 431 | resetProperties(); |
| 432 | sendBugreportStarted(1000); |
| 433 | waitForScreenshotButtonEnabled(true); |
| 434 | |
Felipe Leme | ca5002f | 2016-08-03 11:51:00 -0700 | [diff] [blame] | 435 | DetailsUi detailsUi = new DetailsUi(mUiBot, ID, NAME, touchDetails); |
Felipe Leme | c8e2b60 | 2016-01-29 13:55:35 -0800 | [diff] [blame] | 436 | |
| 437 | detailsUi.nameField.setText(""); |
| 438 | detailsUi.titleField.setText(""); |
| 439 | detailsUi.descField.setText(mDescription); |
| 440 | detailsUi.clickOk(); |
| 441 | |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 442 | Bundle extras = sendBugreportFinishedAndGetSharedIntent(ID, mZipPath, mScreenshotPath); |
| 443 | assertActionSendMultiple(extras, BUGREPORT_CONTENT, SCREENSHOT_CONTENT, ID, PID, ZIP_FILE, |
Felipe Leme | 079f896 | 2016-04-18 12:04:23 -0700 | [diff] [blame] | 444 | NO_NAME, NO_TITLE, mDescription, 0, DIDNT_RENAME_SCREENSHOTS); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 445 | |
| 446 | assertServiceNotRunning(); |
| 447 | } |
| 448 | |
Felipe Leme | 47ec8be | 2016-08-12 15:55:17 -0700 | [diff] [blame] | 449 | @Test |
Felipe Leme | 2628878 | 2016-02-25 12:10:43 -0800 | [diff] [blame] | 450 | public void testProgress_changeJustDetailsIsClearedOnSecondBugreport() throws Exception { |
| 451 | resetProperties(); |
| 452 | sendBugreportStarted(ID, PID, NAME, 1000); |
| 453 | waitForScreenshotButtonEnabled(true); |
| 454 | |
Felipe Leme | ca5002f | 2016-08-03 11:51:00 -0700 | [diff] [blame] | 455 | DetailsUi detailsUi = new DetailsUi(mUiBot, ID, NAME); |
Felipe Leme | 2628878 | 2016-02-25 12:10:43 -0800 | [diff] [blame] | 456 | detailsUi.assertName(NAME); |
Felipe Leme | bbd91e5 | 2016-02-26 16:48:22 -0800 | [diff] [blame] | 457 | detailsUi.assertTitle(""); |
| 458 | detailsUi.assertDescription(""); |
Felipe Leme | da1fbdd | 2016-08-03 17:39:01 -0700 | [diff] [blame] | 459 | assertTrue("didn't enable name on UI", detailsUi.nameField.isEnabled()); |
Felipe Leme | 2628878 | 2016-02-25 12:10:43 -0800 | [diff] [blame] | 460 | detailsUi.nameField.setText(NEW_NAME); |
| 461 | detailsUi.titleField.setText(TITLE); |
| 462 | detailsUi.descField.setText(DESCRIPTION); |
| 463 | detailsUi.clickOk(); |
| 464 | |
| 465 | sendBugreportStarted(ID2, PID2, NAME2, 1000); |
| 466 | |
Felipe Leme | 5ee846d | 2016-03-09 10:14:27 -0800 | [diff] [blame] | 467 | sendBugreportFinished(ID, mZipPath, mScreenshotPath); |
Felipe Leme | 6540574 | 2017-05-24 11:51:10 -0700 | [diff] [blame] | 468 | Bundle extras = acceptBugreportAndGetSharedIntent(TITLE); |
Felipe Leme | 2628878 | 2016-02-25 12:10:43 -0800 | [diff] [blame] | 469 | |
Felipe Leme | ca5002f | 2016-08-03 11:51:00 -0700 | [diff] [blame] | 470 | detailsUi = new DetailsUi(mUiBot, ID2, NAME2); |
Felipe Leme | 2628878 | 2016-02-25 12:10:43 -0800 | [diff] [blame] | 471 | detailsUi.assertName(NAME2); |
Felipe Leme | bbd91e5 | 2016-02-26 16:48:22 -0800 | [diff] [blame] | 472 | detailsUi.assertTitle(""); |
| 473 | detailsUi.assertDescription(""); |
Felipe Leme | da1fbdd | 2016-08-03 17:39:01 -0700 | [diff] [blame] | 474 | assertTrue("didn't enable name on UI", detailsUi.nameField.isEnabled()); |
Felipe Leme | 2628878 | 2016-02-25 12:10:43 -0800 | [diff] [blame] | 475 | detailsUi.nameField.setText(NEW_NAME2); |
| 476 | detailsUi.titleField.setText(TITLE2); |
| 477 | detailsUi.descField.setText(DESCRIPTION2); |
| 478 | detailsUi.clickOk(); |
| 479 | |
| 480 | // Must use a different zip file otherwise it will fail because zip already contains |
| 481 | // title.txt and description.txt entries. |
Felipe Leme | 6540574 | 2017-05-24 11:51:10 -0700 | [diff] [blame] | 482 | extras = sendBugreportFinishedAndGetSharedIntent(ID2, mZipPath2, NO_SCREENSHOT, TITLE2); |
Felipe Leme | 2628878 | 2016-02-25 12:10:43 -0800 | [diff] [blame] | 483 | assertActionSendMultiple(extras, BUGREPORT_CONTENT, NO_SCREENSHOT, ID2, PID2, TITLE2, |
Felipe Leme | 079f896 | 2016-04-18 12:04:23 -0700 | [diff] [blame] | 484 | NEW_NAME2, TITLE2, DESCRIPTION2, 0, RENAMED_SCREENSHOTS); |
Felipe Leme | 2628878 | 2016-02-25 12:10:43 -0800 | [diff] [blame] | 485 | |
| 486 | assertServiceNotRunning(); |
| 487 | } |
| 488 | |
Felipe Leme | 2288129 | 2016-01-06 09:57:23 -0800 | [diff] [blame] | 489 | /** |
| 490 | * Tests the scenario where the initial screenshot and dumpstate are finished while the user |
| 491 | * is changing the info in the details screen. |
| 492 | */ |
Felipe Leme | 47ec8be | 2016-08-12 15:55:17 -0700 | [diff] [blame] | 493 | @Test |
Felipe Leme | 2288129 | 2016-01-06 09:57:23 -0800 | [diff] [blame] | 494 | public void testProgress_bugreportAndScreenshotFinishedWhileChangingDetails() throws Exception { |
| 495 | bugreportFinishedWhileChangingDetailsTest(false); |
| 496 | } |
| 497 | |
| 498 | /** |
| 499 | * Tests the scenario where dumpstate is finished while the user is changing the info in the |
| 500 | * details screen, but the initial screenshot finishes afterwards. |
| 501 | */ |
Felipe Leme | 47ec8be | 2016-08-12 15:55:17 -0700 | [diff] [blame] | 502 | @Test |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 503 | public void testProgress_bugreportFinishedWhileChangingDetails() throws Exception { |
Felipe Leme | 2288129 | 2016-01-06 09:57:23 -0800 | [diff] [blame] | 504 | bugreportFinishedWhileChangingDetailsTest(true); |
| 505 | } |
| 506 | |
| 507 | private void bugreportFinishedWhileChangingDetailsTest(boolean waitScreenshot) throws Exception { |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 508 | resetProperties(); |
| 509 | sendBugreportStarted(1000); |
Felipe Leme | 2288129 | 2016-01-06 09:57:23 -0800 | [diff] [blame] | 510 | if (waitScreenshot) { |
| 511 | waitForScreenshotButtonEnabled(true); |
| 512 | } |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 513 | |
Felipe Leme | ca5002f | 2016-08-03 11:51:00 -0700 | [diff] [blame] | 514 | DetailsUi detailsUi = new DetailsUi(mUiBot, ID, NAME); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 515 | |
| 516 | // Finish the bugreport while user's still typing the name. |
| 517 | detailsUi.nameField.setText(NEW_NAME); |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 518 | sendBugreportFinished(ID, mPlainTextPath, mScreenshotPath); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 519 | |
Felipe Leme | 2288129 | 2016-01-06 09:57:23 -0800 | [diff] [blame] | 520 | // Wait until the share notification is received... |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 521 | waitShareNotification(ID); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 522 | // ...then close notification bar. |
| 523 | mContext.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)); |
| 524 | |
| 525 | // Make sure UI was updated properly. |
| 526 | assertFalse("didn't disable name on UI", detailsUi.nameField.isEnabled()); |
| 527 | assertEquals("didn't revert name on UI", NAME, detailsUi.nameField.getText().toString()); |
| 528 | |
| 529 | // Finish changing other fields. |
| 530 | detailsUi.titleField.setText(TITLE); |
| 531 | detailsUi.descField.setText(mDescription); |
| 532 | detailsUi.clickOk(); |
| 533 | |
| 534 | // Finally, share bugreport. |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 535 | Bundle extras = acceptBugreportAndGetSharedIntent(ID); |
| 536 | assertActionSendMultiple(extras, BUGREPORT_CONTENT, SCREENSHOT_CONTENT, ID, PID, TITLE, |
Felipe Leme | 079f896 | 2016-04-18 12:04:23 -0700 | [diff] [blame] | 537 | NAME, TITLE, mDescription, 0, RENAMED_SCREENSHOTS); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 538 | |
| 539 | assertServiceNotRunning(); |
| 540 | } |
| 541 | |
Felipe Leme | 47ec8be | 2016-08-12 15:55:17 -0700 | [diff] [blame] | 542 | @Test |
Felipe Leme | fcca68d | 2016-04-12 14:00:07 -0700 | [diff] [blame] | 543 | public void testBugreportFinished_withWarningFirstTime() throws Exception { |
| 544 | bugreportFinishedWithWarningTest(null); |
| 545 | } |
| 546 | |
Felipe Leme | 47ec8be | 2016-08-12 15:55:17 -0700 | [diff] [blame] | 547 | @Test |
Felipe Leme | fcca68d | 2016-04-12 14:00:07 -0700 | [diff] [blame] | 548 | public void testBugreportFinished_withWarningUnknownState() throws Exception { |
| 549 | bugreportFinishedWithWarningTest(STATE_UNKNOWN); |
| 550 | } |
| 551 | |
Felipe Leme | 47ec8be | 2016-08-12 15:55:17 -0700 | [diff] [blame] | 552 | @Test |
Felipe Leme | fcca68d | 2016-04-12 14:00:07 -0700 | [diff] [blame] | 553 | public void testBugreportFinished_withWarningShowAgain() throws Exception { |
| 554 | bugreportFinishedWithWarningTest(STATE_SHOW); |
| 555 | } |
| 556 | |
| 557 | private void bugreportFinishedWithWarningTest(Integer propertyState) throws Exception { |
| 558 | if (propertyState == null) { |
| 559 | // Clear properties |
| 560 | mContext.getSharedPreferences(PREFS_BUGREPORT, Context.MODE_PRIVATE) |
| 561 | .edit().clear().commit(); |
| 562 | // Sanity check... |
| 563 | assertEquals("Did not reset properties", STATE_UNKNOWN, |
| 564 | getWarningState(mContext, STATE_UNKNOWN)); |
| 565 | } else { |
| 566 | setWarningState(mContext, propertyState); |
| 567 | } |
Felipe Leme | 6bbb6b9 | 2015-12-07 17:43:55 -0800 | [diff] [blame] | 568 | |
| 569 | // Send notification and click on share. |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 570 | sendBugreportFinished(NO_ID, mPlainTextPath, null); |
Felipe Leme | 6540574 | 2017-05-24 11:51:10 -0700 | [diff] [blame] | 571 | mUiBot.clickOnNotification(mContext.getString(R.string.bugreport_finished_title, NO_ID)); |
Felipe Leme | 6bbb6b9 | 2015-12-07 17:43:55 -0800 | [diff] [blame] | 572 | |
| 573 | // Handle the warning |
| 574 | mUiBot.getVisibleObject(mContext.getString(R.string.bugreport_confirm)); |
Felipe Leme | fcca68d | 2016-04-12 14:00:07 -0700 | [diff] [blame] | 575 | // TODO: get ok and dontShowAgain from the dialog reference above |
| 576 | UiObject dontShowAgain = |
| 577 | mUiBot.getVisibleObject(mContext.getString(R.string.bugreport_confirm_dont_repeat)); |
| 578 | final boolean firstTime = propertyState == null || propertyState == STATE_UNKNOWN; |
| 579 | if (firstTime) { |
Jeff Sharkey | 5ab0243 | 2017-06-27 11:01:36 -0600 | [diff] [blame] | 580 | if (Build.IS_USER) { |
Felipe Leme | af09671 | 2016-04-25 13:36:44 -0700 | [diff] [blame] | 581 | assertFalse("Checkbox should NOT be checked by default on user builds", |
| 582 | dontShowAgain.isChecked()); |
| 583 | mUiBot.click(dontShowAgain, "dont-show-again"); |
| 584 | } else { |
| 585 | assertTrue("Checkbox should be checked by default on build type " + Build.TYPE, |
| 586 | dontShowAgain.isChecked()); |
| 587 | } |
Felipe Leme | fcca68d | 2016-04-12 14:00:07 -0700 | [diff] [blame] | 588 | } else { |
| 589 | assertFalse("Checkbox should not be checked", dontShowAgain.isChecked()); |
| 590 | mUiBot.click(dontShowAgain, "dont-show-again"); |
| 591 | } |
Felipe Leme | 6bbb6b9 | 2015-12-07 17:43:55 -0800 | [diff] [blame] | 592 | UiObject ok = mUiBot.getVisibleObject(mContext.getString(com.android.internal.R.string.ok)); |
| 593 | mUiBot.click(ok, "ok"); |
| 594 | |
| 595 | // Share the bugreport. |
| 596 | mUiBot.chooseActivity(UI_NAME); |
| 597 | Bundle extras = mListener.getExtras(); |
Felipe Leme | 4967f73 | 2016-01-06 11:38:53 -0800 | [diff] [blame] | 598 | assertActionSendMultiple(extras, BUGREPORT_CONTENT, NO_SCREENSHOT); |
Felipe Leme | 6bbb6b9 | 2015-12-07 17:43:55 -0800 | [diff] [blame] | 599 | |
| 600 | // Make sure it's hidden now. |
Felipe Leme | fcca68d | 2016-04-12 14:00:07 -0700 | [diff] [blame] | 601 | int newState = getWarningState(mContext, STATE_UNKNOWN); |
| 602 | assertEquals("Didn't change state", STATE_HIDE, newState); |
Felipe Leme | 6bbb6b9 | 2015-12-07 17:43:55 -0800 | [diff] [blame] | 603 | } |
| 604 | |
Felipe Leme | 47ec8be | 2016-08-12 15:55:17 -0700 | [diff] [blame] | 605 | @Test |
Felipe Leme | c4f64677 | 2016-01-12 18:12:09 -0800 | [diff] [blame] | 606 | public void testShareBugreportAfterServiceDies() throws Exception { |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 607 | sendBugreportFinished(NO_ID, mPlainTextPath, NO_SCREENSHOT); |
Felipe Leme | 69c53e6 | 2016-04-15 12:49:34 -0700 | [diff] [blame] | 608 | waitForService(false); |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 609 | Bundle extras = acceptBugreportAndGetSharedIntent(NO_ID); |
Felipe Leme | c4f64677 | 2016-01-12 18:12:09 -0800 | [diff] [blame] | 610 | assertActionSendMultiple(extras, BUGREPORT_CONTENT, NO_SCREENSHOT); |
| 611 | } |
| 612 | |
Felipe Leme | 47ec8be | 2016-08-12 15:55:17 -0700 | [diff] [blame] | 613 | @Test |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 614 | public void testBugreportFinished_plainBugreportAndScreenshot() throws Exception { |
Felipe Leme | 93702ab | 2015-12-11 13:06:45 -0800 | [diff] [blame] | 615 | Bundle extras = sendBugreportFinishedAndGetSharedIntent(mPlainTextPath, mScreenshotPath); |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 616 | assertActionSendMultiple(extras, BUGREPORT_CONTENT, SCREENSHOT_CONTENT); |
| 617 | } |
| 618 | |
Felipe Leme | 47ec8be | 2016-08-12 15:55:17 -0700 | [diff] [blame] | 619 | @Test |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 620 | public void testBugreportFinished_zippedBugreportAndScreenshot() throws Exception { |
Felipe Leme | 93702ab | 2015-12-11 13:06:45 -0800 | [diff] [blame] | 621 | Bundle extras = sendBugreportFinishedAndGetSharedIntent(mZipPath, mScreenshotPath); |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 622 | assertActionSendMultiple(extras, BUGREPORT_CONTENT, SCREENSHOT_CONTENT); |
| 623 | } |
| 624 | |
Felipe Leme | 47ec8be | 2016-08-12 15:55:17 -0700 | [diff] [blame] | 625 | @Test |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 626 | public void testBugreportFinished_plainBugreportAndNoScreenshot() throws Exception { |
Felipe Leme | 4967f73 | 2016-01-06 11:38:53 -0800 | [diff] [blame] | 627 | Bundle extras = sendBugreportFinishedAndGetSharedIntent(mPlainTextPath, NO_SCREENSHOT); |
| 628 | assertActionSendMultiple(extras, BUGREPORT_CONTENT, NO_SCREENSHOT); |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 629 | } |
| 630 | |
Felipe Leme | 47ec8be | 2016-08-12 15:55:17 -0700 | [diff] [blame] | 631 | @Test |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 632 | public void testBugreportFinished_zippedBugreportAndNoScreenshot() throws Exception { |
Felipe Leme | 4967f73 | 2016-01-06 11:38:53 -0800 | [diff] [blame] | 633 | Bundle extras = sendBugreportFinishedAndGetSharedIntent(mZipPath, NO_SCREENSHOT); |
| 634 | assertActionSendMultiple(extras, BUGREPORT_CONTENT, NO_SCREENSHOT); |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | private void cancelExistingNotifications() { |
Felipe Leme | 2c0f3a3 | 2016-08-08 15:39:13 -0700 | [diff] [blame] | 638 | // Must kill service first, because notifications from a foreground service cannot be |
| 639 | // canceled. |
| 640 | killService(); |
| 641 | |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 642 | NotificationManager nm = NotificationManager.from(mContext); |
Felipe Leme | ca5002f | 2016-08-03 11:51:00 -0700 | [diff] [blame] | 643 | StatusBarNotification[] activeNotifications = nm.getActiveNotifications(); |
| 644 | if (activeNotifications.length == 0) { |
| 645 | return; |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 646 | } |
Felipe Leme | ca5002f | 2016-08-03 11:51:00 -0700 | [diff] [blame] | 647 | |
| 648 | Log.w(TAG, getName() + ": " + activeNotifications.length + " active notifications"); |
| 649 | |
| 650 | nm.cancelAll(); |
| 651 | |
| 652 | // Wait a little bit... |
| 653 | for (int i = 1; i < 5; i++) { |
| 654 | int total = nm.getActiveNotifications().length; |
| 655 | if (total == 0) { |
| 656 | return; |
| 657 | } |
| 658 | Log.d(TAG, total + "notifications are still active; sleeping "); |
| 659 | nm.cancelAll(); |
| 660 | sleep(1000); |
| 661 | } |
| 662 | assertEquals("old notifications were not cancelled", 0, nm.getActiveNotifications().length); |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 663 | } |
| 664 | |
Felipe Leme | a86a301 | 2016-07-15 10:40:00 -0700 | [diff] [blame] | 665 | private void cancelFromNotification() { |
Felipe Leme | ca5002f | 2016-08-03 11:51:00 -0700 | [diff] [blame] | 666 | openProgressNotification(NAME); |
Felipe Leme | a86a301 | 2016-07-15 10:40:00 -0700 | [diff] [blame] | 667 | UiObject cancelButton = mUiBot.getVisibleObject(mContext.getString( |
| 668 | com.android.internal.R.string.cancel).toUpperCase()); |
| 669 | mUiBot.click(cancelButton, "cancel_button"); |
| 670 | } |
| 671 | |
Felipe Leme | 9ca5e56 | 2016-04-11 10:27:02 -0700 | [diff] [blame] | 672 | private void assertProgressNotification(String name, float percent) { |
Felipe Leme | ca5002f | 2016-08-03 11:51:00 -0700 | [diff] [blame] | 673 | openProgressNotification(name); |
Felipe Leme | 9ca5e56 | 2016-04-11 10:27:02 -0700 | [diff] [blame] | 674 | // TODO: need a way to get the ProgresBar from the "android:id/progress" UIObject... |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 675 | } |
| 676 | |
Felipe Leme | ca5002f | 2016-08-03 11:51:00 -0700 | [diff] [blame] | 677 | private UiObject openProgressNotification(String bugreportName) { |
| 678 | Log.v(TAG, "Looking for progress notification for '" + bugreportName + "'"); |
| 679 | return mUiBot.getNotification(bugreportName); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 680 | } |
| 681 | |
Felipe Leme | 93702ab | 2015-12-11 13:06:45 -0800 | [diff] [blame] | 682 | void resetProperties() { |
| 683 | // TODO: call method to remove property instead |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 684 | SystemProperties.set(PROGRESS_PROPERTY, "Reset"); |
| 685 | SystemProperties.set(MAX_PROPERTY, "Reset"); |
| 686 | SystemProperties.set(NAME_PROPERTY, "Reset"); |
Felipe Leme | 93702ab | 2015-12-11 13:06:45 -0800 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | /** |
| 690 | * Sends a "bugreport started" intent with the default values. |
| 691 | */ |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 692 | private void sendBugreportStarted(int max) throws Exception { |
Felipe Leme | 2628878 | 2016-02-25 12:10:43 -0800 | [diff] [blame] | 693 | sendBugreportStarted(ID, PID, NAME, max); |
| 694 | } |
| 695 | |
| 696 | private void sendBugreportStarted(int id, int pid, String name, int max) throws Exception { |
Felipe Leme | 93702ab | 2015-12-11 13:06:45 -0800 | [diff] [blame] | 697 | Intent intent = new Intent(INTENT_BUGREPORT_STARTED); |
Felipe Leme | 8775eaa | 2017-02-10 13:06:58 -0800 | [diff] [blame] | 698 | intent.setPackage("com.android.shell"); |
Felipe Leme | ec17538 | 2016-01-26 18:17:15 -0800 | [diff] [blame] | 699 | intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
Felipe Leme | 2628878 | 2016-02-25 12:10:43 -0800 | [diff] [blame] | 700 | intent.putExtra(EXTRA_ID, id); |
| 701 | intent.putExtra(EXTRA_PID, pid); |
| 702 | intent.putExtra(EXTRA_NAME, name); |
Felipe Leme | 93702ab | 2015-12-11 13:06:45 -0800 | [diff] [blame] | 703 | intent.putExtra(EXTRA_MAX, max); |
| 704 | mContext.sendBroadcast(intent); |
| 705 | } |
| 706 | |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 707 | /** |
| 708 | * Sends a "bugreport finished" intent and waits for the result. |
| 709 | * |
Felipe Leme | 93702ab | 2015-12-11 13:06:45 -0800 | [diff] [blame] | 710 | * @return extras sent in the shared intent. |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 711 | */ |
Felipe Leme | 93702ab | 2015-12-11 13:06:45 -0800 | [diff] [blame] | 712 | private Bundle sendBugreportFinishedAndGetSharedIntent(String bugreportPath, |
| 713 | String screenshotPath) { |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 714 | return sendBugreportFinishedAndGetSharedIntent(NO_ID, bugreportPath, screenshotPath); |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 715 | } |
| 716 | |
Felipe Leme | 93702ab | 2015-12-11 13:06:45 -0800 | [diff] [blame] | 717 | /** |
| 718 | * Sends a "bugreport finished" intent and waits for the result. |
| 719 | * |
| 720 | * @return extras sent in the shared intent. |
| 721 | */ |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 722 | private Bundle sendBugreportFinishedAndGetSharedIntent(int id, String bugreportPath, |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 723 | String screenshotPath) { |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 724 | sendBugreportFinished(id, bugreportPath, screenshotPath); |
Felipe Leme | a43d1393 | 2016-04-12 17:28:06 -0700 | [diff] [blame] | 725 | return acceptBugreportAndGetSharedIntent(id); |
Felipe Leme | 93702ab | 2015-12-11 13:06:45 -0800 | [diff] [blame] | 726 | } |
| 727 | |
Felipe Leme | 6540574 | 2017-05-24 11:51:10 -0700 | [diff] [blame] | 728 | // TODO: document / merge these 3 sendBugreportFinishedAndGetSharedIntent methods |
| 729 | private Bundle sendBugreportFinishedAndGetSharedIntent(int id, String bugreportPath, |
| 730 | String screenshotPath, String notificationTitle) { |
| 731 | sendBugreportFinished(id, bugreportPath, screenshotPath); |
| 732 | return acceptBugreportAndGetSharedIntent(notificationTitle); |
| 733 | } |
| 734 | |
Felipe Leme | 93702ab | 2015-12-11 13:06:45 -0800 | [diff] [blame] | 735 | /** |
| 736 | * Accepts the notification to share the finished bugreport and waits for the result. |
| 737 | * |
| 738 | * @return extras sent in the shared intent. |
| 739 | */ |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 740 | private Bundle acceptBugreportAndGetSharedIntent(int id) { |
Felipe Leme | 6540574 | 2017-05-24 11:51:10 -0700 | [diff] [blame] | 741 | final String notificationTitle = mContext.getString(R.string.bugreport_finished_title, id); |
| 742 | return acceptBugreportAndGetSharedIntent(notificationTitle); |
| 743 | } |
| 744 | |
| 745 | // TODO: document and/or merge these 2 acceptBugreportAndGetSharedIntent methods |
| 746 | private Bundle acceptBugreportAndGetSharedIntent(String notificationTitle) { |
| 747 | mUiBot.clickOnNotification(notificationTitle); |
Felipe Leme | 93702ab | 2015-12-11 13:06:45 -0800 | [diff] [blame] | 748 | mUiBot.chooseActivity(UI_NAME); |
| 749 | return mListener.getExtras(); |
| 750 | } |
| 751 | |
| 752 | /** |
Felipe Leme | c4f64677 | 2016-01-12 18:12:09 -0800 | [diff] [blame] | 753 | * Waits for the notification to share the finished bugreport. |
| 754 | */ |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 755 | private void waitShareNotification(int id) { |
| 756 | mUiBot.getNotification(mContext.getString(R.string.bugreport_finished_title, id)); |
Felipe Leme | c4f64677 | 2016-01-12 18:12:09 -0800 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | /** |
Felipe Leme | 93702ab | 2015-12-11 13:06:45 -0800 | [diff] [blame] | 760 | * Sends a "bugreport finished" intent. |
Felipe Leme | 93702ab | 2015-12-11 13:06:45 -0800 | [diff] [blame] | 761 | */ |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 762 | private void sendBugreportFinished(int id, String bugreportPath, String screenshotPath) { |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 763 | Intent intent = new Intent(INTENT_BUGREPORT_FINISHED); |
Felipe Leme | 8775eaa | 2017-02-10 13:06:58 -0800 | [diff] [blame] | 764 | intent.setPackage("com.android.shell"); |
Felipe Leme | ec17538 | 2016-01-26 18:17:15 -0800 | [diff] [blame] | 765 | intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND); |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 766 | if (id != NO_ID) { |
| 767 | intent.putExtra(EXTRA_ID, id); |
Felipe Leme | 69c0292 | 2015-11-24 17:48:05 -0800 | [diff] [blame] | 768 | } |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 769 | if (bugreportPath != null) { |
| 770 | intent.putExtra(EXTRA_BUGREPORT, bugreportPath); |
| 771 | } |
| 772 | if (screenshotPath != null) { |
| 773 | intent.putExtra(EXTRA_SCREENSHOT, screenshotPath); |
| 774 | } |
| 775 | |
| 776 | mContext.sendBroadcast(intent); |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | /** |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 780 | * Asserts the proper {@link Intent#ACTION_SEND_MULTIPLE} intent was sent. |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 781 | */ |
| 782 | private void assertActionSendMultiple(Bundle extras, String bugreportContent, |
| 783 | String screenshotContent) throws IOException { |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 784 | assertActionSendMultiple(extras, bugreportContent, screenshotContent, ID, PID, ZIP_FILE, |
Felipe Leme | c8e2b60 | 2016-01-29 13:55:35 -0800 | [diff] [blame] | 785 | NO_NAME, NO_TITLE, NO_DESCRIPTION, 0, DIDNT_RENAME_SCREENSHOTS); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 786 | } |
| 787 | |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 788 | /** |
| 789 | * Asserts the proper {@link Intent#ACTION_SEND_MULTIPLE} intent was sent. |
| 790 | * |
| 791 | * @param extras extras received in the intent |
| 792 | * @param bugreportContent expected content in the bugreport file |
| 793 | * @param screenshotContent expected content in the screenshot file (sent by dumpstate), if any |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 794 | * @param id emulated dumpstate id |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 795 | * @param pid emulated dumpstate pid |
Felipe Leme | 4967f73 | 2016-01-06 11:38:53 -0800 | [diff] [blame] | 796 | * @param name expected subject |
| 797 | * @param name bugreport name as provided by the user (or received by dumpstate) |
| 798 | * @param title bugreport name as provided by the user |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 799 | * @param description bugreport description as provided by the user |
| 800 | * @param numberScreenshots expected number of screenshots taken by Shell. |
Felipe Leme | 2288129 | 2016-01-06 09:57:23 -0800 | [diff] [blame] | 801 | * @param renamedScreenshots whether the screenshots are expected to be renamed |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 802 | */ |
| 803 | private void assertActionSendMultiple(Bundle extras, String bugreportContent, |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 804 | String screenshotContent, int id, int pid, String subject, |
Felipe Leme | 4967f73 | 2016-01-06 11:38:53 -0800 | [diff] [blame] | 805 | String name, String title, String description, |
Felipe Leme | 2288129 | 2016-01-06 09:57:23 -0800 | [diff] [blame] | 806 | int numberScreenshots, boolean renamedScreenshots) throws IOException { |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 807 | String body = extras.getString(Intent.EXTRA_TEXT); |
| 808 | assertContainsRegex("missing build info", |
| 809 | SystemProperties.get("ro.build.description"), body); |
| 810 | assertContainsRegex("missing serial number", |
| 811 | SystemProperties.get("ro.serialno"), body); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 812 | if (description != null) { |
| 813 | assertContainsRegex("missing description", description, body); |
| 814 | } |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 815 | |
Felipe Leme | 4967f73 | 2016-01-06 11:38:53 -0800 | [diff] [blame] | 816 | assertEquals("wrong subject", subject, extras.getString(Intent.EXTRA_SUBJECT)); |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 817 | |
| 818 | List<Uri> attachments = extras.getParcelableArrayList(Intent.EXTRA_STREAM); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 819 | int expectedNumberScreenshots = numberScreenshots; |
| 820 | if (screenshotContent != null) { |
| 821 | expectedNumberScreenshots ++; // Add screenshot received by dumpstate |
| 822 | } |
| 823 | int expectedSize = expectedNumberScreenshots + 1; // All screenshots plus the bugreport file |
Felipe Leme | c4f64677 | 2016-01-12 18:12:09 -0800 | [diff] [blame] | 824 | assertEquals("wrong number of attachments (" + attachments + ")", |
| 825 | expectedSize, attachments.size()); |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 826 | |
| 827 | // Need to interact through all attachments, since order is not guaranteed. |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 828 | Uri zipUri = null; |
| 829 | List<Uri> screenshotUris = new ArrayList<>(expectedNumberScreenshots); |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 830 | for (Uri attachment : attachments) { |
| 831 | if (attachment.getPath().endsWith(".zip")) { |
| 832 | zipUri = attachment; |
| 833 | } |
| 834 | if (attachment.getPath().endsWith(".png")) { |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 835 | screenshotUris.add(attachment); |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 836 | } |
| 837 | } |
| 838 | assertNotNull("did not get .zip attachment", zipUri); |
| 839 | assertZipContent(zipUri, BUGREPORT_FILE, BUGREPORT_CONTENT); |
Felipe Leme | 4967f73 | 2016-01-06 11:38:53 -0800 | [diff] [blame] | 840 | if (!TextUtils.isEmpty(title)) { |
| 841 | assertZipContent(zipUri, "title.txt", title); |
| 842 | } |
| 843 | if (!TextUtils.isEmpty(description)) { |
| 844 | assertZipContent(zipUri, "description.txt", description); |
| 845 | } |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 846 | |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 847 | // URI of the screenshot taken by dumpstate. |
| 848 | Uri externalScreenshotUri = null; |
| 849 | SortedSet<String> internalScreenshotNames = new TreeSet<>(); |
| 850 | for (Uri screenshotUri : screenshotUris) { |
| 851 | String screenshotName = screenshotUri.getLastPathSegment(); |
| 852 | if (screenshotName.endsWith(SCREENSHOT_FILE)) { |
| 853 | externalScreenshotUri = screenshotUri; |
| 854 | } else { |
| 855 | internalScreenshotNames.add(screenshotName); |
| 856 | } |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 857 | } |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 858 | // Check external screenshot |
| 859 | if (screenshotContent != null) { |
| 860 | assertNotNull("did not get .png attachment for external screenshot", |
| 861 | externalScreenshotUri); |
| 862 | assertContent(externalScreenshotUri, SCREENSHOT_CONTENT); |
| 863 | } else { |
| 864 | assertNull("should not have .png attachment for external screenshot", |
| 865 | externalScreenshotUri); |
| 866 | } |
| 867 | // Check internal screenshots. |
| 868 | SortedSet<String> expectedNames = new TreeSet<>(); |
| 869 | for (int i = 1 ; i <= numberScreenshots; i++) { |
Felipe Leme | 2288129 | 2016-01-06 09:57:23 -0800 | [diff] [blame] | 870 | String prefix = renamedScreenshots ? name : Integer.toString(pid); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 871 | String expectedName = "screenshot-" + prefix + "-" + i + ".png"; |
| 872 | expectedNames.add(expectedName); |
| 873 | } |
| 874 | // Ideally we should use MoreAsserts, but the error message in case of failure is not |
| 875 | // really useful. |
| 876 | assertEquals("wrong names for internal screenshots", |
| 877 | expectedNames, internalScreenshotNames); |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | private void assertContent(Uri uri, String expectedContent) throws IOException { |
| 881 | Log.v(TAG, "assertContents(uri=" + uri); |
| 882 | try (InputStream is = mContext.getContentResolver().openInputStream(uri)) { |
| 883 | String actualContent = new String(Streams.readFully(is)); |
| 884 | assertEquals("wrong content for '" + uri + "'", expectedContent, actualContent); |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | private void assertZipContent(Uri uri, String entryName, String expectedContent) |
| 889 | throws IOException, IOException { |
| 890 | Log.v(TAG, "assertZipEntry(uri=" + uri + ", entryName=" + entryName); |
| 891 | try (ZipInputStream zis = new ZipInputStream(mContext.getContentResolver().openInputStream( |
| 892 | uri))) { |
| 893 | ZipEntry entry; |
| 894 | while ((entry = zis.getNextEntry()) != null) { |
| 895 | Log.v(TAG, "Zip entry: " + entry.getName()); |
| 896 | if (entry.getName().equals(entryName)) { |
| 897 | ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 898 | Streams.copy(zis, bos); |
| 899 | String actualContent = new String(bos.toByteArray(), "UTF-8"); |
| 900 | bos.close(); |
| 901 | assertEquals("wrong content for zip entry'" + entryName + "' on '" + uri + "'", |
| 902 | expectedContent, actualContent); |
| 903 | return; |
| 904 | } |
| 905 | } |
| 906 | } |
| 907 | fail("Did not find entry '" + entryName + "' on file '" + uri + "'"); |
| 908 | } |
| 909 | |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 910 | private void assertPropertyValue(String key, String expectedValue) { |
Felipe Leme | ca5002f | 2016-08-03 11:51:00 -0700 | [diff] [blame] | 911 | // Since the property is set in a different thread by BugreportProgressService, we need to |
| 912 | // poll it a couple times... |
| 913 | |
| 914 | for (int i = 1; i <= 5; i++) { |
| 915 | String actualValue = SystemProperties.get(key); |
| 916 | if (expectedValue.equals(actualValue)) { |
| 917 | return; |
| 918 | } |
| 919 | Log.d(TAG, "Value of property " + key + " (" + actualValue |
| 920 | + ") does not match expected value (" + expectedValue |
| 921 | + ") on attempt " + i + ". Sleeping before next attempt..."); |
| 922 | sleep(1000); |
| 923 | } |
| 924 | // Final try... |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 925 | String actualValue = SystemProperties.get(key); |
| 926 | assertEquals("Wrong value for property '" + key + "'", expectedValue, actualValue); |
| 927 | } |
| 928 | |
Felipe Leme | 93702ab | 2015-12-11 13:06:45 -0800 | [diff] [blame] | 929 | private void assertServiceNotRunning() { |
| 930 | String service = BugreportProgressService.class.getName(); |
| 931 | assertFalse("Service '" + service + "' is still running", isServiceRunning(service)); |
| 932 | } |
| 933 | |
Felipe Leme | ba47793 | 2015-12-09 11:04:59 -0800 | [diff] [blame] | 934 | private boolean isServiceRunning(String name) { |
| 935 | ActivityManager manager = (ActivityManager) mContext |
| 936 | .getSystemService(Context.ACTIVITY_SERVICE); |
| 937 | for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { |
| 938 | if (service.service.getClassName().equals(name)) { |
| 939 | return true; |
| 940 | } |
| 941 | } |
| 942 | return false; |
| 943 | } |
| 944 | |
Felipe Leme | c4f64677 | 2016-01-12 18:12:09 -0800 | [diff] [blame] | 945 | private void waitForService(boolean expectRunning) { |
| 946 | String service = BugreportProgressService.class.getName(); |
| 947 | boolean actualRunning; |
| 948 | for (int i = 1; i <= 5; i++) { |
| 949 | actualRunning = isServiceRunning(service); |
| 950 | Log.d(TAG, "Attempt " + i + " to check status of service '" |
| 951 | + service + "': expected=" + expectRunning + ", actual= " + actualRunning); |
| 952 | if (actualRunning == expectRunning) { |
| 953 | return; |
| 954 | } |
Felipe Leme | ca5002f | 2016-08-03 11:51:00 -0700 | [diff] [blame] | 955 | sleep(DateUtils.SECOND_IN_MILLIS); |
Felipe Leme | c4f64677 | 2016-01-12 18:12:09 -0800 | [diff] [blame] | 956 | } |
Felipe Leme | c4f64677 | 2016-01-12 18:12:09 -0800 | [diff] [blame] | 957 | |
| 958 | fail("Service status didn't change to " + expectRunning); |
| 959 | } |
| 960 | |
Felipe Leme | 2c0f3a3 | 2016-08-08 15:39:13 -0700 | [diff] [blame] | 961 | private void killService() { |
| 962 | String service = BugreportProgressService.class.getName(); |
| 963 | |
| 964 | if (!isServiceRunning(service)) return; |
| 965 | |
| 966 | Log.w(TAG, "Service '" + service + "' is still running, killing it"); |
| 967 | silentlyExecuteShellCommand("am stopservice com.android.shell/.BugreportProgressService"); |
| 968 | |
| 969 | waitForService(false); |
| 970 | } |
| 971 | |
| 972 | private void silentlyExecuteShellCommand(String cmd) { |
| 973 | Log.w(TAG, "silentlyExecuteShellCommand: '" + cmd + "'"); |
| 974 | try { |
| 975 | UiDevice.getInstance(getInstrumentation()).executeShellCommand(cmd); |
| 976 | } catch (IOException e) { |
| 977 | Log.w(TAG, "error executing shell comamand '" + cmd + "'", e); |
| 978 | } |
| 979 | } |
| 980 | |
Felipe Leme | 2628878 | 2016-02-25 12:10:43 -0800 | [diff] [blame] | 981 | private void createTextFile(String path, String content) throws IOException { |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 982 | Log.v(TAG, "createFile(" + path + ")"); |
| 983 | try (Writer writer = new BufferedWriter(new OutputStreamWriter( |
| 984 | new FileOutputStream(path)))) { |
| 985 | writer.write(content); |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | private void createZipFile(String path, String entryName, String content) throws IOException { |
| 990 | Log.v(TAG, "createZipFile(" + path + ", " + entryName + ")"); |
| 991 | try (ZipOutputStream zos = new ZipOutputStream( |
| 992 | new BufferedOutputStream(new FileOutputStream(path)))) { |
| 993 | ZipEntry entry = new ZipEntry(entryName); |
| 994 | zos.putNextEntry(entry); |
| 995 | byte[] data = content.getBytes(); |
| 996 | zos.write(data, 0, data.length); |
| 997 | zos.closeEntry(); |
| 998 | } |
| 999 | } |
Felipe Leme | a0bf033 | 2015-12-11 09:35:03 -0800 | [diff] [blame] | 1000 | |
| 1001 | private String getPath(String file) { |
Felipe Leme | fcca68d | 2016-04-12 14:00:07 -0700 | [diff] [blame] | 1002 | final File rootDir = mContext.getFilesDir(); |
| 1003 | final File dir = new File(rootDir, BUGREPORTS_DIR); |
Felipe Leme | e2b4f49 | 2015-12-11 18:19:26 -0800 | [diff] [blame] | 1004 | if (!dir.exists()) { |
| 1005 | Log.i(TAG, "Creating directory " + dir); |
| 1006 | assertTrue("Could not create directory " + dir, dir.mkdir()); |
| 1007 | } |
Felipe Leme | a0bf033 | 2015-12-11 09:35:03 -0800 | [diff] [blame] | 1008 | String path = new File(dir, file).getAbsolutePath(); |
| 1009 | Log.v(TAG, "Path for '" + file + "': " + path); |
| 1010 | return path; |
| 1011 | } |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 1012 | |
| 1013 | /** |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 1014 | * Gets the notification button used to take a screenshot. |
| 1015 | */ |
| 1016 | private UiObject getScreenshotButton() { |
Felipe Leme | ca5002f | 2016-08-03 11:51:00 -0700 | [diff] [blame] | 1017 | openProgressNotification(NAME); |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 1018 | return mUiBot.getVisibleObject( |
| 1019 | mContext.getString(R.string.bugreport_screenshot_action).toUpperCase()); |
| 1020 | } |
| 1021 | |
| 1022 | /** |
| 1023 | * Takes a screenshot using the system notification. |
| 1024 | */ |
| 1025 | private void takeScreenshot() throws Exception { |
| 1026 | UiObject screenshotButton = getScreenshotButton(); |
| 1027 | mUiBot.click(screenshotButton, "screenshot_button"); |
| 1028 | } |
| 1029 | |
| 1030 | private UiObject waitForScreenshotButtonEnabled(boolean expectedEnabled) throws Exception { |
| 1031 | UiObject screenshotButton = getScreenshotButton(); |
Felipe Leme | c4f64677 | 2016-01-12 18:12:09 -0800 | [diff] [blame] | 1032 | int maxAttempts = SAFE_SCREENSHOT_DELAY; |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 1033 | int i = 0; |
| 1034 | do { |
| 1035 | boolean enabled = screenshotButton.isEnabled(); |
| 1036 | if (enabled == expectedEnabled) { |
| 1037 | return screenshotButton; |
| 1038 | } |
| 1039 | i++; |
| 1040 | Log.v(TAG, "Sleeping for 1 second while waiting for screenshot.enable to be " |
| 1041 | + expectedEnabled + " (attempt " + i + ")"); |
| 1042 | Thread.sleep(DateUtils.SECOND_IN_MILLIS); |
| 1043 | } while (i <= maxAttempts); |
| 1044 | fail("screenshot.enable didn't change to " + expectedEnabled + " in " + maxAttempts + "s"); |
| 1045 | return screenshotButton; |
| 1046 | } |
| 1047 | |
| 1048 | private void assertScreenshotButtonEnabled(boolean expectedEnabled) throws Exception { |
| 1049 | UiObject screenshotButton = getScreenshotButton(); |
| 1050 | assertEquals("wrong state for screenshot button ", expectedEnabled, |
| 1051 | screenshotButton.isEnabled()); |
| 1052 | } |
| 1053 | |
Felipe Leme | a86a301 | 2016-07-15 10:40:00 -0700 | [diff] [blame] | 1054 | private void assertDetailsUiClosed() { |
| 1055 | // TODO: unhardcode resource ids |
| 1056 | mUiBot.assertNotVisibleById("android:id/alertTitle"); |
| 1057 | } |
| 1058 | |
Felipe Leme | 47ec8be | 2016-08-12 15:55:17 -0700 | [diff] [blame] | 1059 | private String getName() { |
| 1060 | return mName.getMethodName(); |
| 1061 | } |
| 1062 | |
| 1063 | private Instrumentation getInstrumentation() { |
| 1064 | return InstrumentationRegistry.getInstrumentation(); |
| 1065 | } |
| 1066 | |
Felipe Leme | ca5002f | 2016-08-03 11:51:00 -0700 | [diff] [blame] | 1067 | private static void sleep(long ms) { |
| 1068 | Log.d(TAG, "sleeping for " + ms + "ms"); |
| 1069 | SystemClock.sleep(ms); |
| 1070 | Log.d(TAG, "woke up"); |
| 1071 | } |
| 1072 | |
Felipe Leme | d1e0f12 | 2015-12-18 16:12:41 -0800 | [diff] [blame] | 1073 | /** |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 1074 | * Helper class containing the UiObjects present in the bugreport info dialog. |
| 1075 | */ |
| 1076 | private final class DetailsUi { |
| 1077 | |
| 1078 | final UiObject detailsButton; |
| 1079 | final UiObject nameField; |
| 1080 | final UiObject titleField; |
| 1081 | final UiObject descField; |
| 1082 | final UiObject okButton; |
| 1083 | final UiObject cancelButton; |
| 1084 | |
| 1085 | /** |
Felipe Leme | ca5002f | 2016-08-03 11:51:00 -0700 | [diff] [blame] | 1086 | * Gets the UI objects by opening the progress notification and clicking on DETAILS. |
| 1087 | * |
| 1088 | * @param id bugreport id |
| 1089 | * @param id bugreport name |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 1090 | */ |
Felipe Leme | ca5002f | 2016-08-03 11:51:00 -0700 | [diff] [blame] | 1091 | DetailsUi(UiBot uiBot, int id, String name) throws UiObjectNotFoundException { |
| 1092 | this(uiBot, id, name, true); |
Felipe Leme | db31363 | 2016-02-25 17:06:58 -0800 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | /** |
| 1096 | * Gets the UI objects by opening the progress notification and clicking on DETAILS or in |
| 1097 | * the notification itself. |
Felipe Leme | ca5002f | 2016-08-03 11:51:00 -0700 | [diff] [blame] | 1098 | * |
| 1099 | * @param id bugreport id |
| 1100 | * @param id bugreport name |
Felipe Leme | db31363 | 2016-02-25 17:06:58 -0800 | [diff] [blame] | 1101 | */ |
Felipe Leme | ca5002f | 2016-08-03 11:51:00 -0700 | [diff] [blame] | 1102 | DetailsUi(UiBot uiBot, int id, String name, boolean clickDetails) |
| 1103 | throws UiObjectNotFoundException { |
| 1104 | final UiObject notification = openProgressNotification(name); |
Felipe Leme | 2628878 | 2016-02-25 12:10:43 -0800 | [diff] [blame] | 1105 | detailsButton = mUiBot.getVisibleObject(mContext.getString( |
| 1106 | R.string.bugreport_info_action).toUpperCase()); |
| 1107 | |
Felipe Leme | db31363 | 2016-02-25 17:06:58 -0800 | [diff] [blame] | 1108 | if (clickDetails) { |
| 1109 | mUiBot.click(detailsButton, "details_button"); |
| 1110 | } else { |
| 1111 | mUiBot.click(notification, "notification"); |
| 1112 | } |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 1113 | // TODO: unhardcode resource ids |
Felipe Leme | e86b63b | 2016-02-08 09:39:50 -0800 | [diff] [blame] | 1114 | UiObject dialogTitle = mUiBot.getVisibleObjectById("android:id/alertTitle"); |
| 1115 | assertEquals("Wrong title", mContext.getString(R.string.bugreport_info_dialog_title, |
Felipe Leme | 85ae3cf | 2016-02-24 15:36:50 -0800 | [diff] [blame] | 1116 | id), dialogTitle.getText().toString()); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 1117 | nameField = mUiBot.getVisibleObjectById("com.android.shell:id/name"); |
| 1118 | titleField = mUiBot.getVisibleObjectById("com.android.shell:id/title"); |
| 1119 | descField = mUiBot.getVisibleObjectById("com.android.shell:id/description"); |
| 1120 | okButton = mUiBot.getObjectById("android:id/button1"); |
| 1121 | cancelButton = mUiBot.getObjectById("android:id/button2"); |
| 1122 | } |
| 1123 | |
Felipe Leme | 208b188 | 2016-03-14 18:03:41 -0700 | [diff] [blame] | 1124 | private void assertField(String name, UiObject field, String expected) |
| 1125 | throws UiObjectNotFoundException { |
| 1126 | String actual = field.getText().toString(); |
| 1127 | assertEquals("Wrong value on field '" + name + "'", expected, actual); |
Felipe Leme | 2628878 | 2016-02-25 12:10:43 -0800 | [diff] [blame] | 1128 | } |
| 1129 | |
Felipe Leme | 208b188 | 2016-03-14 18:03:41 -0700 | [diff] [blame] | 1130 | void assertName(String expected) throws UiObjectNotFoundException { |
Felipe Leme | 2628878 | 2016-02-25 12:10:43 -0800 | [diff] [blame] | 1131 | assertField("name", nameField, expected); |
| 1132 | } |
| 1133 | |
Felipe Leme | 208b188 | 2016-03-14 18:03:41 -0700 | [diff] [blame] | 1134 | void assertTitle(String expected) throws UiObjectNotFoundException { |
Felipe Leme | 2628878 | 2016-02-25 12:10:43 -0800 | [diff] [blame] | 1135 | assertField("title", titleField, expected); |
| 1136 | } |
| 1137 | |
Felipe Leme | 208b188 | 2016-03-14 18:03:41 -0700 | [diff] [blame] | 1138 | void assertDescription(String expected) throws UiObjectNotFoundException { |
Felipe Leme | 2628878 | 2016-02-25 12:10:43 -0800 | [diff] [blame] | 1139 | assertField("description", descField, expected); |
| 1140 | } |
| 1141 | |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 1142 | /** |
Felipe Leme | 208b188 | 2016-03-14 18:03:41 -0700 | [diff] [blame] | 1143 | * Set focus on the name field so it can be validated once focus is lost. |
| 1144 | */ |
| 1145 | void focusOnName() throws UiObjectNotFoundException { |
| 1146 | mUiBot.click(nameField, "name_field"); |
| 1147 | assertTrue("name_field not focused", nameField.isFocused()); |
| 1148 | } |
| 1149 | |
| 1150 | /** |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 1151 | * Takes focus away from the name field so it can be validated. |
| 1152 | */ |
Felipe Leme | 208b188 | 2016-03-14 18:03:41 -0700 | [diff] [blame] | 1153 | void focusAwayFromName() throws UiObjectNotFoundException { |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 1154 | mUiBot.click(titleField, "title_field"); // Change focus. |
Felipe Leme | 208b188 | 2016-03-14 18:03:41 -0700 | [diff] [blame] | 1155 | assertFalse("name_field is focused", nameField.isFocused()); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 1156 | } |
| 1157 | |
Felipe Leme | ca5002f | 2016-08-03 11:51:00 -0700 | [diff] [blame] | 1158 | void reOpen(String name) { |
| 1159 | openProgressNotification(name); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 1160 | mUiBot.click(detailsButton, "details_button"); |
Felipe Leme | bc73ffc | 2015-12-11 15:07:14 -0800 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | void clickOk() { |
| 1164 | mUiBot.click(okButton, "details_ok_button"); |
| 1165 | } |
| 1166 | |
| 1167 | void clickCancel() { |
| 1168 | mUiBot.click(cancelButton, "details_cancel_button"); |
| 1169 | } |
| 1170 | } |
Felipe Leme | e53e85f | 2015-11-17 17:37:53 -0800 | [diff] [blame] | 1171 | } |