blob: 33c4ef1ffbd54a53cf199cec2600e07758ab5051 [file] [log] [blame]
Felipe Lemee53e85f2015-11-17 17:37:53 -08001/*
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
17package com.android.shell;
18
19import static android.test.MoreAsserts.assertContainsRegex;
20import static com.android.shell.ActionSendMultipleConsumerActivity.UI_NAME;
Felipe Lemeb9238b32015-11-24 17:31:47 -080021import static com.android.shell.BugreportProgressService.EXTRA_BUGREPORT;
Felipe Leme69c02922015-11-24 17:48:05 -080022import static com.android.shell.BugreportProgressService.EXTRA_MAX;
23import static com.android.shell.BugreportProgressService.EXTRA_NAME;
24import static com.android.shell.BugreportProgressService.EXTRA_PID;
Felipe Lemeb9238b32015-11-24 17:31:47 -080025import static com.android.shell.BugreportProgressService.EXTRA_SCREENSHOT;
Felipe Leme69c02922015-11-24 17:48:05 -080026import static com.android.shell.BugreportProgressService.INTENT_BUGREPORT_FINISHED;
27import static com.android.shell.BugreportProgressService.INTENT_BUGREPORT_STARTED;
Felipe Lemee53e85f2015-11-17 17:37:53 -080028
29import java.io.BufferedOutputStream;
30import java.io.BufferedWriter;
31import java.io.ByteArrayOutputStream;
32import java.io.FileOutputStream;
33import java.io.IOException;
34import java.io.InputStream;
35import java.io.OutputStreamWriter;
36import java.io.Writer;
37import java.util.List;
38import java.util.zip.ZipEntry;
39import java.util.zip.ZipInputStream;
40import java.util.zip.ZipOutputStream;
41
42import libcore.io.Streams;
43import android.app.Instrumentation;
44import android.app.NotificationManager;
45import android.content.Context;
46import android.content.Intent;
47import android.net.Uri;
48import android.os.Bundle;
49import android.os.SystemProperties;
50import android.service.notification.StatusBarNotification;
51import android.support.test.uiautomator.UiDevice;
52import android.test.InstrumentationTestCase;
53import android.util.Log;
54
55import com.android.shell.ActionSendMultipleConsumerActivity.CustomActionSendMultipleListener;
56
57/**
58 * Integration tests for {@link BugreportReceiver}.
59 * <p>
60 * These tests don't mock any component and rely on external UI components (like the notification
61 * bar and activity chooser), which can make them unreliable and slow.
62 * <p>
63 * The general workflow is:
64 * <ul>
65 * <li>creates the bug report files
66 * <li>generates the BUGREPORT_FINISHED intent
67 * <li>emulate user actions to share the intent with a custom activity
68 * <li>asserts the extras received by the custom activity
69 * </ul>
70 * <p>
71 * TODO: currently, these tests only work if the bug report sharing warning is disabled and the
72 * device screen is unlocked.
73 */
74public class BugreportReceiverTest extends InstrumentationTestCase {
75
76 private static final String TAG = "BugreportReceiverTest";
77
78 // Timeout for UI operations, in milliseconds.
79 private static final int TIMEOUT = 1000;
80
81 private static final String ROOT_DIR = "/data/data/com.android.shell/files/bugreports";
82 private static final String BUGREPORT_FILE = "test_bugreport.txt";
83 private static final String ZIP_FILE = "test_bugreport.zip";
84 private static final String PLAIN_TEXT_PATH = ROOT_DIR + "/" + BUGREPORT_FILE;
85 private static final String ZIP_PATH = ROOT_DIR + "/" + ZIP_FILE;
86 private static final String SCREENSHOT_PATH = ROOT_DIR + "/test_screenshot.png";
87
88 private static final String BUGREPORT_CONTENT = "Dump, might as well dump!\n";
89 private static final String SCREENSHOT_CONTENT = "A picture is worth a thousand words!\n";
90
91 private Context mContext;
92 private UiBot mUiBot;
93 private CustomActionSendMultipleListener mListener;
94
95 @Override
96 protected void setUp() throws Exception {
97 Instrumentation instrumentation = getInstrumentation();
98 mContext = instrumentation.getTargetContext();
99 mUiBot = new UiBot(UiDevice.getInstance(instrumentation), TIMEOUT);
100 mListener = ActionSendMultipleConsumerActivity.getListener(mContext);
101 cancelExistingNotifications();
102 }
103
Felipe Leme69c02922015-11-24 17:48:05 -0800104 public void testFullWorkflow() throws Exception {
105 final String name = "BUG, Y U NO REPORT?";
106 // TODO: call method to remove property instead
107 SystemProperties.set("dumpstate.42.progress", "-1");
108
109 Intent intent = new Intent(INTENT_BUGREPORT_STARTED);
110 intent.putExtra(EXTRA_PID, 42);
111 intent.putExtra(EXTRA_NAME, name);
112 intent.putExtra(EXTRA_MAX, 1000);
113 mContext.sendBroadcast(intent);
114
115 assertProgressNotification(name, "0.00%");
116
117 SystemProperties.set("dumpstate.42.progress", "108");
118 assertProgressNotification(name, "10.80%");
119
120 SystemProperties.set("dumpstate.42.progress", "500");
121 assertProgressNotification(name, "50.00%");
122
123 createTextFile(PLAIN_TEXT_PATH, BUGREPORT_CONTENT);
124 createTextFile(SCREENSHOT_PATH, SCREENSHOT_CONTENT);
125 Bundle extras = sendBugreportFinishedIntent(42, PLAIN_TEXT_PATH, SCREENSHOT_PATH);
126 assertActionSendMultiple(extras, BUGREPORT_CONTENT, SCREENSHOT_CONTENT);
127
128 // TODO: assert service is down
129 }
130
Felipe Lemee53e85f2015-11-17 17:37:53 -0800131 public void testBugreportFinished_plainBugreportAndScreenshot() throws Exception {
132 createTextFile(PLAIN_TEXT_PATH, BUGREPORT_CONTENT);
133 createTextFile(SCREENSHOT_PATH, SCREENSHOT_CONTENT);
134 Bundle extras = sendBugreportFinishedIntent(PLAIN_TEXT_PATH, SCREENSHOT_PATH);
135 assertActionSendMultiple(extras, BUGREPORT_CONTENT, SCREENSHOT_CONTENT);
136 }
137
138 public void testBugreportFinished_zippedBugreportAndScreenshot() throws Exception {
139 createZipFile(ZIP_PATH, BUGREPORT_FILE, BUGREPORT_CONTENT);
140 createTextFile(SCREENSHOT_PATH, SCREENSHOT_CONTENT);
141 Bundle extras = sendBugreportFinishedIntent(ZIP_PATH, SCREENSHOT_PATH);
142 assertActionSendMultiple(extras, BUGREPORT_CONTENT, SCREENSHOT_CONTENT);
143 }
144
145 public void testBugreportFinished_plainBugreportAndNoScreenshot() throws Exception {
146 createTextFile(PLAIN_TEXT_PATH, BUGREPORT_CONTENT);
147 Bundle extras = sendBugreportFinishedIntent(PLAIN_TEXT_PATH, null);
148 assertActionSendMultiple(extras, BUGREPORT_CONTENT, null);
149 }
150
151 public void testBugreportFinished_zippedBugreportAndNoScreenshot() throws Exception {
152 createZipFile(ZIP_PATH, BUGREPORT_FILE, BUGREPORT_CONTENT);
153 Bundle extras = sendBugreportFinishedIntent(ZIP_PATH, null);
154 assertActionSendMultiple(extras, BUGREPORT_CONTENT, null);
155 }
156
157 private void cancelExistingNotifications() {
158 NotificationManager nm = NotificationManager.from(mContext);
159 for (StatusBarNotification notification : nm.getActiveNotifications()) {
160 int id = notification.getId();
161 Log.i(TAG, "Canceling existing notification (id=" + id + ")");
162 nm.cancel(id);
163 }
164 }
165
Felipe Leme69c02922015-11-24 17:48:05 -0800166 private void assertProgressNotification(String name, String percent) {
167 // TODO: it current looks for 3 distinct objects, without taking advantage of their
168 // relationship.
169 String title = mContext.getString(R.string.bugreport_in_progress_title);
170 Log.v(TAG, "Looking for progress notification title: '" + title+ "'");
171 mUiBot.getNotification(title);
172 Log.v(TAG, "Looking for progress notification details: '" + name + "-" + percent + "'");
173 mUiBot.getObject(name);
174 mUiBot.getObject(percent);
175 }
176
Felipe Lemee53e85f2015-11-17 17:37:53 -0800177 /**
178 * Sends a "bugreport finished" intent and waits for the result.
179 *
180 * @return extras sent to the bugreport finished consumer.
181 */
182 private Bundle sendBugreportFinishedIntent(String bugreportPath, String screenshotPath) {
Felipe Leme69c02922015-11-24 17:48:05 -0800183 return sendBugreportFinishedIntent(null, bugreportPath, screenshotPath);
184 }
185
186 private Bundle sendBugreportFinishedIntent(Integer pid, String bugreportPath,
187 String screenshotPath) {
188 Intent intent = new Intent(INTENT_BUGREPORT_FINISHED);
189 if (pid != null) {
190 intent.putExtra(EXTRA_PID, pid);
191 }
Felipe Lemee53e85f2015-11-17 17:37:53 -0800192 if (bugreportPath != null) {
193 intent.putExtra(EXTRA_BUGREPORT, bugreportPath);
194 }
195 if (screenshotPath != null) {
196 intent.putExtra(EXTRA_SCREENSHOT, screenshotPath);
197 }
198
199 mContext.sendBroadcast(intent);
200
201 mUiBot.clickOnNotification(mContext.getString(R.string.bugreport_finished_title));
202 mUiBot.chooseActivity(UI_NAME);
203 return mListener.getExtras();
204 }
205
206 /**
207 * Asserts the proper ACTION_SEND_MULTIPLE intent was sent.
208 */
209 private void assertActionSendMultiple(Bundle extras, String bugreportContent,
210 String screenshotContent) throws IOException {
211 String body = extras.getString(Intent.EXTRA_TEXT);
212 assertContainsRegex("missing build info",
213 SystemProperties.get("ro.build.description"), body);
214 assertContainsRegex("missing serial number",
215 SystemProperties.get("ro.serialno"), body);
216
217 assertEquals("wrong subject", ZIP_FILE, extras.getString(Intent.EXTRA_SUBJECT));
218
219 List<Uri> attachments = extras.getParcelableArrayList(Intent.EXTRA_STREAM);
220 int expectedSize = screenshotContent != null ? 2 : 1;
221 assertEquals("wrong number of attachments", expectedSize, attachments.size());
222
223 // Need to interact through all attachments, since order is not guaranteed.
224 Uri zipUri = null, screenshotUri = null;
225 for (Uri attachment : attachments) {
226 if (attachment.getPath().endsWith(".zip")) {
227 zipUri = attachment;
228 }
229 if (attachment.getPath().endsWith(".png")) {
230 screenshotUri = attachment;
231 }
232 }
233 assertNotNull("did not get .zip attachment", zipUri);
234 assertZipContent(zipUri, BUGREPORT_FILE, BUGREPORT_CONTENT);
235
236 if (screenshotContent != null) {
237 assertNotNull("did not get .png attachment", screenshotUri);
238 assertContent(screenshotUri, SCREENSHOT_CONTENT);
239 } else {
240 assertNull("should not have .png attachment", screenshotUri);
241 }
242 }
243
244 private void assertContent(Uri uri, String expectedContent) throws IOException {
245 Log.v(TAG, "assertContents(uri=" + uri);
246 try (InputStream is = mContext.getContentResolver().openInputStream(uri)) {
247 String actualContent = new String(Streams.readFully(is));
248 assertEquals("wrong content for '" + uri + "'", expectedContent, actualContent);
249 }
250 }
251
252 private void assertZipContent(Uri uri, String entryName, String expectedContent)
253 throws IOException, IOException {
254 Log.v(TAG, "assertZipEntry(uri=" + uri + ", entryName=" + entryName);
255 try (ZipInputStream zis = new ZipInputStream(mContext.getContentResolver().openInputStream(
256 uri))) {
257 ZipEntry entry;
258 while ((entry = zis.getNextEntry()) != null) {
259 Log.v(TAG, "Zip entry: " + entry.getName());
260 if (entry.getName().equals(entryName)) {
261 ByteArrayOutputStream bos = new ByteArrayOutputStream();
262 Streams.copy(zis, bos);
263 String actualContent = new String(bos.toByteArray(), "UTF-8");
264 bos.close();
265 assertEquals("wrong content for zip entry'" + entryName + "' on '" + uri + "'",
266 expectedContent, actualContent);
267 return;
268 }
269 }
270 }
271 fail("Did not find entry '" + entryName + "' on file '" + uri + "'");
272 }
273
274 private static void createTextFile(String path, String content) throws IOException {
275 Log.v(TAG, "createFile(" + path + ")");
276 try (Writer writer = new BufferedWriter(new OutputStreamWriter(
277 new FileOutputStream(path)))) {
278 writer.write(content);
279 }
280 }
281
282 private void createZipFile(String path, String entryName, String content) throws IOException {
283 Log.v(TAG, "createZipFile(" + path + ", " + entryName + ")");
284 try (ZipOutputStream zos = new ZipOutputStream(
285 new BufferedOutputStream(new FileOutputStream(path)))) {
286 ZipEntry entry = new ZipEntry(entryName);
287 zos.putNextEntry(entry);
288 byte[] data = content.getBytes();
289 zos.write(data, 0, data.length);
290 zos.closeEntry();
291 }
292 }
293}