Unhardcoded file paths.

Also added a sanity check when deleting old files to avoid a runtime
exception in the AsyncTask when the file doesn't exist.

BUG: 25752530
Change-Id: Ic4a118ae7cc5750cc96c2ac82f2c7dcc6a0cb506
This commit is contained in:
Felipe Leme
2015-12-11 09:35:03 -08:00
parent 3b9cac0dc7
commit a0bf0336f0
3 changed files with 41 additions and 20 deletions

View File

@@ -91,7 +91,7 @@ import android.widget.Toast;
* </ol>
*/
public class BugreportProgressService extends Service {
private static final String TAG = "Shell";
static final String TAG = "Shell";
private static final boolean DEBUG = false;
private static final String AUTHORITY = "com.android.shell";

View File

@@ -19,6 +19,7 @@ package com.android.shell;
import static com.android.shell.BugreportProgressService.EXTRA_BUGREPORT;
import static com.android.shell.BugreportProgressService.EXTRA_ORIGINAL_INTENT;
import static com.android.shell.BugreportProgressService.INTENT_BUGREPORT_FINISHED;
import static com.android.shell.BugreportProgressService.TAG;
import static com.android.shell.BugreportProgressService.getFileExtra;
import java.io.File;
@@ -29,6 +30,7 @@ import android.content.Intent;
import android.os.AsyncTask;
import android.os.FileUtils;
import android.text.format.DateUtils;
import android.util.Log;
/**
* Receiver that handles finished bugreports, usually by attaching them to an
@@ -63,6 +65,10 @@ public class BugreportReceiver extends BroadcastReceiver {
return;
}
final File bugreportFile = getFileExtra(intent, EXTRA_BUGREPORT);
if (bugreportFile == null || !bugreportFile.exists()) {
Log.e(TAG, "Not deleting old files because file " + bugreportFile + " doesn't exist");
return;
}
final PendingResult result = goAsync();
new AsyncTask<Void, Void, Void>() {
@Override

View File

@@ -29,6 +29,7 @@ import static com.android.shell.BugreportProgressService.INTENT_BUGREPORT_STARTE
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -45,6 +46,7 @@ import android.app.ActivityManager.RunningServiceInfo;
import android.app.Instrumentation;
import android.app.NotificationManager;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
@@ -80,16 +82,18 @@ public class BugreportReceiverTest extends InstrumentationTestCase {
// Timeout for UI operations, in milliseconds.
private static final int TIMEOUT = 1000;
private static final String ROOT_DIR = "/data/data/com.android.shell/files/bugreports";
private static final String BUGREPORTS_DIR = "bugreports";
private static final String BUGREPORT_FILE = "test_bugreport.txt";
private static final String ZIP_FILE = "test_bugreport.zip";
private static final String PLAIN_TEXT_PATH = ROOT_DIR + "/" + BUGREPORT_FILE;
private static final String ZIP_PATH = ROOT_DIR + "/" + ZIP_FILE;
private static final String SCREENSHOT_PATH = ROOT_DIR + "/test_screenshot.png";
private static final String SCREENSHOT_FILE = "test_screenshot.png";
private static final String BUGREPORT_CONTENT = "Dump, might as well dump!\n";
private static final String SCREENSHOT_CONTENT = "A picture is worth a thousand words!\n";
private String mPlainTextPath;
private String mZipPath;
private String mScreenshotPath;
private Context mContext;
private UiBot mUiBot;
private CustomActionSendMultipleListener mListener;
@@ -100,6 +104,9 @@ public class BugreportReceiverTest extends InstrumentationTestCase {
mContext = instrumentation.getTargetContext();
mUiBot = new UiBot(UiDevice.getInstance(instrumentation), TIMEOUT);
mListener = ActionSendMultipleConsumerActivity.getListener(mContext);
mPlainTextPath = getPath(BUGREPORT_FILE);
mZipPath = getPath(ZIP_FILE);
mScreenshotPath = getPath(SCREENSHOT_FILE);
cancelExistingNotifications();
BugreportPrefs.setWarningState(mContext, BugreportPrefs.STATE_HIDE);
}
@@ -127,9 +134,9 @@ public class BugreportReceiverTest extends InstrumentationTestCase {
SystemProperties.set("dumpstate.42.max", "2000");
assertProgressNotification(name, "25.00%");
createTextFile(PLAIN_TEXT_PATH, BUGREPORT_CONTENT);
createTextFile(SCREENSHOT_PATH, SCREENSHOT_CONTENT);
Bundle extras = sendBugreportFinishedIntent(42, PLAIN_TEXT_PATH, SCREENSHOT_PATH);
createTextFile(mPlainTextPath, BUGREPORT_CONTENT);
createTextFile(mScreenshotPath, SCREENSHOT_CONTENT);
Bundle extras = sendBugreportFinishedIntent(42, mPlainTextPath, mScreenshotPath);
assertActionSendMultiple(extras, BUGREPORT_CONTENT, SCREENSHOT_CONTENT);
String service = BugreportProgressService.class.getName();
@@ -141,9 +148,9 @@ public class BugreportReceiverTest extends InstrumentationTestCase {
BugreportPrefs.setWarningState(mContext, BugreportPrefs.STATE_SHOW);
// Send notification and click on share.
createTextFile(PLAIN_TEXT_PATH, BUGREPORT_CONTENT);
createTextFile(mPlainTextPath, BUGREPORT_CONTENT);
Intent intent = new Intent(INTENT_BUGREPORT_FINISHED);
intent.putExtra(EXTRA_BUGREPORT, PLAIN_TEXT_PATH);
intent.putExtra(EXTRA_BUGREPORT, mPlainTextPath);
mContext.sendBroadcast(intent);
mUiBot.clickOnNotification(mContext.getString(R.string.bugreport_finished_title));
@@ -167,28 +174,28 @@ public class BugreportReceiverTest extends InstrumentationTestCase {
}
public void testBugreportFinished_plainBugreportAndScreenshot() throws Exception {
createTextFile(PLAIN_TEXT_PATH, BUGREPORT_CONTENT);
createTextFile(SCREENSHOT_PATH, SCREENSHOT_CONTENT);
Bundle extras = sendBugreportFinishedIntent(PLAIN_TEXT_PATH, SCREENSHOT_PATH);
createTextFile(mPlainTextPath, BUGREPORT_CONTENT);
createTextFile(mScreenshotPath, SCREENSHOT_CONTENT);
Bundle extras = sendBugreportFinishedIntent(mPlainTextPath, mScreenshotPath);
assertActionSendMultiple(extras, BUGREPORT_CONTENT, SCREENSHOT_CONTENT);
}
public void testBugreportFinished_zippedBugreportAndScreenshot() throws Exception {
createZipFile(ZIP_PATH, BUGREPORT_FILE, BUGREPORT_CONTENT);
createTextFile(SCREENSHOT_PATH, SCREENSHOT_CONTENT);
Bundle extras = sendBugreportFinishedIntent(ZIP_PATH, SCREENSHOT_PATH);
createZipFile(mZipPath, BUGREPORT_FILE, BUGREPORT_CONTENT);
createTextFile(mScreenshotPath, SCREENSHOT_CONTENT);
Bundle extras = sendBugreportFinishedIntent(mZipPath, mScreenshotPath);
assertActionSendMultiple(extras, BUGREPORT_CONTENT, SCREENSHOT_CONTENT);
}
public void testBugreportFinished_plainBugreportAndNoScreenshot() throws Exception {
createTextFile(PLAIN_TEXT_PATH, BUGREPORT_CONTENT);
Bundle extras = sendBugreportFinishedIntent(PLAIN_TEXT_PATH, null);
createTextFile(mPlainTextPath, BUGREPORT_CONTENT);
Bundle extras = sendBugreportFinishedIntent(mPlainTextPath, null);
assertActionSendMultiple(extras, BUGREPORT_CONTENT, null);
}
public void testBugreportFinished_zippedBugreportAndNoScreenshot() throws Exception {
createZipFile(ZIP_PATH, BUGREPORT_FILE, BUGREPORT_CONTENT);
Bundle extras = sendBugreportFinishedIntent(ZIP_PATH, null);
createZipFile(mZipPath, BUGREPORT_FILE, BUGREPORT_CONTENT);
Bundle extras = sendBugreportFinishedIntent(mZipPath, null);
assertActionSendMultiple(extras, BUGREPORT_CONTENT, null);
}
@@ -339,4 +346,12 @@ public class BugreportReceiverTest extends InstrumentationTestCase {
zos.closeEntry();
}
}
private String getPath(String file) {
File rootDir = new ContextWrapper(mContext).getFilesDir();
File dir = new File(rootDir, BUGREPORTS_DIR);
String path = new File(dir, file).getAbsolutePath();
Log.v(TAG, "Path for '" + file + "': " + path);
return path;
}
}