Fixes an error handling in BugreportProgressService

Calls onError function when bugreport is finished and file is empty.

Bug: 174314124
Bug: 175287931
Test: atest BugreportReceiverTest
Change-Id: I4542568fd2d2ad1c75c7c3b223accca4995938a3
Merged-In: I4542568fd2d2ad1c75c7c3b223accca4995938a3
This commit is contained in:
Rhed Jao
2020-12-07 17:58:26 +08:00
parent e9a2038f8a
commit 39b6acbb55
2 changed files with 19 additions and 5 deletions

View File

@@ -380,6 +380,11 @@ public class BugreportProgressService extends Service {
public void onFinished() { public void onFinished() {
mInfo.renameBugreportFile(); mInfo.renameBugreportFile();
mInfo.renameScreenshots(); mInfo.renameScreenshots();
if (mInfo.bugreportFile.length() == 0) {
Log.e(TAG, "Bugreport file empty. File path = " + mInfo.bugreportFile);
onError(BUGREPORT_ERROR_RUNTIME);
return;
}
synchronized (mLock) { synchronized (mLock) {
sendBugreportFinishedBroadcastLocked(); sendBugreportFinishedBroadcastLocked();
mMainThreadHandler.post(() -> mInfoDialog.onBugreportFinished(mInfo)); mMainThreadHandler.post(() -> mInfoDialog.onBugreportFinished(mInfo));
@@ -408,10 +413,6 @@ public class BugreportProgressService extends Service {
@GuardedBy("mLock") @GuardedBy("mLock")
private void sendBugreportFinishedBroadcastLocked() { private void sendBugreportFinishedBroadcastLocked() {
final String bugreportFilePath = mInfo.bugreportFile.getAbsolutePath(); final String bugreportFilePath = mInfo.bugreportFile.getAbsolutePath();
if (mInfo.bugreportFile.length() == 0) {
Log.e(TAG, "Bugreport file empty. File path = " + bugreportFilePath);
return;
}
if (mInfo.type == BugreportParams.BUGREPORT_MODE_REMOTE) { if (mInfo.type == BugreportParams.BUGREPORT_MODE_REMOTE) {
sendRemoteBugreportFinishedBroadcast(mContext, bugreportFilePath, sendRemoteBugreportFinishedBroadcast(mContext, bugreportFilePath,
mInfo.bugreportFile); mInfo.bugreportFile);

View File

@@ -512,6 +512,17 @@ public class BugreportReceiverTest {
assertEquals("Didn't change state", STATE_HIDE, newState); assertEquals("Didn't change state", STATE_HIDE, newState);
} }
@Test
public void testBugreportFinished_withEmptyBugreportFile() throws Exception {
sendBugreportStarted();
IoUtils.closeQuietly(mBugreportFd);
mBugreportFd = null;
sendBugreportFinished();
assertServiceNotRunning();
}
@Test @Test
public void testShareBugreportAfterServiceDies() throws Exception { public void testShareBugreportAfterServiceDies() throws Exception {
sendBugreportStarted(); sendBugreportStarted();
@@ -647,7 +658,9 @@ public class BugreportReceiverTest {
* Callbacks to service to finish the bugreport. * Callbacks to service to finish the bugreport.
*/ */
private void sendBugreportFinished() throws Exception { private void sendBugreportFinished() throws Exception {
writeZipFile(mBugreportFd, BUGREPORT_FILE, BUGREPORT_CONTENT); if (mBugreportFd != null) {
writeZipFile(mBugreportFd, BUGREPORT_FILE, BUGREPORT_CONTENT);
}
if (mScreenshotFd != null) { if (mScreenshotFd != null) {
writeScreenshotFile(mScreenshotFd, SCREENSHOT_CONTENT); writeScreenshotFile(mScreenshotFd, SCREENSHOT_CONTENT);
} }