Merge "Add restore logging to WallpaperBackupAgent" into udc-dev

This commit is contained in:
Niamh Walsh
2023-05-09 15:22:09 +00:00
committed by Android (Google) Code Review
4 changed files with 507 additions and 45 deletions

View File

@@ -365,6 +365,11 @@ public class WallpaperBackupAgent extends BackupAgent {
final int sysWhich = FLAG_SYSTEM | (lockImageStage.exists() ? 0 : FLAG_LOCK);
try {
// First parse the live component name so that we know for logging if we care about
// logging errors with the image restore.
ComponentName wpService = parseWallpaperComponent(infoStage, "wp");
mSystemHasLiveComponent = wpService != null;
// It is valid for the imagery to be absent; it means that we were not permitted
// to back up the original image on the source device, or there was no user-supplied
// wallpaper image present.
@@ -372,10 +377,10 @@ public class WallpaperBackupAgent extends BackupAgent {
restoreFromStage(lockImageStage, infoStage, "kwp", FLAG_LOCK);
// And reset to the wallpaper service we should be using
ComponentName wpService = parseWallpaperComponent(infoStage, "wp");
updateWallpaperComponent(wpService, !lockImageStage.exists());
} catch (Exception e) {
Slog.e(TAG, "Unable to restore wallpaper: " + e.getMessage());
mEventLogger.onRestoreException(e);
} finally {
Slog.v(TAG, "Restore finished; clearing backup bookkeeping");
infoStage.delete();
@@ -399,12 +404,15 @@ public class WallpaperBackupAgent extends BackupAgent {
// We have a live wallpaper and no static lock image,
// allow live wallpaper to show "through" on lock screen.
mWallpaperManager.clear(FLAG_LOCK);
mEventLogger.onLockLiveWallpaperRestored(wpService);
}
mEventLogger.onSystemLiveWallpaperRestored(wpService);
} else {
// If we've restored a live wallpaper, but the component doesn't exist,
// we should log it as an error so we can easily identify the problem
// in reports from users
if (wpService != null) {
// TODO(b/268471749): Handle delayed case
applyComponentAtInstall(wpService, applyToLock);
Slog.w(TAG, "Wallpaper service " + wpService + " isn't available. "
+ " Will try to apply later");
@@ -424,13 +432,37 @@ public class WallpaperBackupAgent extends BackupAgent {
try (FileInputStream in = new FileInputStream(stage)) {
mWallpaperManager.setStream(in, cropHint.isEmpty() ? null : cropHint, true,
which);
// And log the success
if ((which & FLAG_SYSTEM) > 0) {
mEventLogger.onSystemImageWallpaperRestored();
} else {
mEventLogger.onLockImageWallpaperRestored();
}
}
} else {
logRestoreError(which, ERROR_NO_METADATA);
}
} else {
Slog.d(TAG, "Restore data doesn't exist for file " + stage.getPath());
logRestoreErrorIfNoLiveComponent(which, ERROR_NO_WALLPAPER);
}
}
private void logRestoreErrorIfNoLiveComponent(int which, String error) {
if (mSystemHasLiveComponent) {
return;
}
logRestoreError(which, error);
}
private void logRestoreError(int which, String error) {
if ((which & FLAG_SYSTEM) == FLAG_SYSTEM) {
mEventLogger.onSystemImageWallpaperRestoreFailed(error);
} else if ((which & FLAG_LOCK) == FLAG_LOCK) {
mEventLogger.onLockImageWallpaperRestoreFailed(error);
}
}
private Rect parseCropHint(File wallpaperInfo, String sectionTag) {
Rect cropHint = new Rect();
try (FileInputStream stream = new FileInputStream(wallpaperInfo)) {

View File

@@ -22,6 +22,7 @@ import android.app.backup.BackupManager;
import android.app.backup.BackupRestoreEventLogger;
import android.app.backup.BackupRestoreEventLogger.BackupRestoreDataType;
import android.app.backup.BackupRestoreEventLogger.BackupRestoreError;
import android.content.ComponentName;
import com.android.internal.annotations.VisibleForTesting;
@@ -101,6 +102,39 @@ public class WallpaperEventLogger {
logBackupFailureInternal(WALLPAPER_LIVE_LOCK, error);
}
void onSystemImageWallpaperRestored() {
logRestoreSuccessInternal(WALLPAPER_IMG_SYSTEM, /* liveComponentWallpaperInfo */ null);
}
void onLockImageWallpaperRestored() {
logRestoreSuccessInternal(WALLPAPER_IMG_LOCK, /* liveComponentWallpaperInfo */ null);
}
void onSystemLiveWallpaperRestored(ComponentName wpService) {
logRestoreSuccessInternal(WALLPAPER_LIVE_SYSTEM, wpService);
}
void onLockLiveWallpaperRestored(ComponentName wpService) {
logRestoreSuccessInternal(WALLPAPER_LIVE_LOCK, wpService);
}
void onSystemImageWallpaperRestoreFailed(@BackupRestoreError String error) {
logRestoreFailureInternal(WALLPAPER_IMG_SYSTEM, error);
}
void onLockImageWallpaperRestoreFailed(@BackupRestoreError String error) {
logRestoreFailureInternal(WALLPAPER_IMG_LOCK, error);
}
void onSystemLiveWallpaperRestoreFailed(@BackupRestoreError String error) {
logRestoreFailureInternal(WALLPAPER_LIVE_SYSTEM, error);
}
void onLockLiveWallpaperRestoreFailed(@BackupRestoreError String error) {
logRestoreFailureInternal(WALLPAPER_LIVE_LOCK, error);
}
/**
* Called when the whole backup flow is interrupted by an exception.
@@ -117,6 +151,20 @@ public class WallpaperEventLogger {
}
}
/**
* Called when the whole restore flow is interrupted by an exception.
*/
void onRestoreException(Exception exception) {
String error = exception.getClass().getName();
if (!mProcessedDataTypes.contains(WALLPAPER_IMG_SYSTEM) && !mProcessedDataTypes.contains(
WALLPAPER_LIVE_SYSTEM)) {
mLogger.logItemsRestoreFailed(WALLPAPER_IMG_SYSTEM, /* count */ 1, error);
}
if (!mProcessedDataTypes.contains(WALLPAPER_IMG_LOCK) && !mProcessedDataTypes.contains(
WALLPAPER_LIVE_LOCK)) {
mLogger.logItemsRestoreFailed(WALLPAPER_IMG_LOCK, /* count */ 1, error);
}
}
private void logBackupSuccessInternal(@BackupRestoreDataType String which,
@Nullable WallpaperInfo liveComponentWallpaperInfo) {
mLogger.logItemsBackedUp(which, /* count */ 1);
@@ -130,10 +178,30 @@ public class WallpaperEventLogger {
mProcessedDataTypes.add(which);
}
private void logRestoreSuccessInternal(@BackupRestoreDataType String which,
@Nullable ComponentName liveComponentWallpaperInfo) {
mLogger.logItemsRestored(which, /* count */ 1);
logRestoredLiveWallpaperNameIfPresent(which, liveComponentWallpaperInfo);
mProcessedDataTypes.add(which);
}
private void logRestoreFailureInternal(@BackupRestoreDataType String which,
@BackupRestoreError String error) {
mLogger.logItemsRestoreFailed(which, /* count */ 1, error);
mProcessedDataTypes.add(which);
}
private void logLiveWallpaperNameIfPresent(@BackupRestoreDataType String wallpaperType,
WallpaperInfo wallpaperInfo) {
if (wallpaperInfo != null) {
mLogger.logBackupMetadata(wallpaperType, wallpaperInfo.getComponent().getClassName());
}
}
private void logRestoredLiveWallpaperNameIfPresent(@BackupRestoreDataType String wallpaperType,
ComponentName wpService) {
if (wpService != null) {
mLogger.logRestoreMetadata(wallpaperType, wpService.getClassName());
}
}
}

View File

@@ -24,6 +24,7 @@ import static com.android.wallpaperbackup.WallpaperBackupAgent.LOCK_WALLPAPER_ST
import static com.android.wallpaperbackup.WallpaperBackupAgent.SYSTEM_WALLPAPER_STAGE;
import static com.android.wallpaperbackup.WallpaperBackupAgent.WALLPAPER_INFO_STAGE;
import static com.android.wallpaperbackup.WallpaperEventLogger.ERROR_INELIGIBLE;
import static com.android.wallpaperbackup.WallpaperEventLogger.ERROR_NO_METADATA;
import static com.android.wallpaperbackup.WallpaperEventLogger.ERROR_NO_WALLPAPER;
import static com.android.wallpaperbackup.WallpaperEventLogger.ERROR_QUOTA_EXCEEDED;
import static com.android.wallpaperbackup.WallpaperEventLogger.WALLPAPER_IMG_LOCK;
@@ -34,6 +35,8 @@ import static com.android.wallpaperbackup.WallpaperEventLogger.WALLPAPER_LIVE_SY
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
@@ -55,12 +58,14 @@ import android.os.FileUtils;
import android.os.ParcelFileDescriptor;
import android.os.UserHandle;
import android.service.wallpaper.WallpaperService;
import android.util.Xml;
import androidx.test.InstrumentationRegistry;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.runner.AndroidJUnit4;
import com.android.internal.content.PackageMonitor;
import com.android.modules.utils.TypedXmlSerializer;
import com.android.wallpaperbackup.utils.ContextWithServiceOverrides;
import org.junit.After;
@@ -592,6 +597,123 @@ public class WallpaperBackupAgentTest {
assertThat(result.getSuccessCount()).isEqualTo(1);
}
@Test
public void testOnRestore_systemWallpaperImgSuccess_logsSuccess() throws Exception {
mockStagedWallpaperFile(WALLPAPER_INFO_STAGE);
mockStagedWallpaperFile(SYSTEM_WALLPAPER_STAGE);
mWallpaperBackupAgent.onCreate(USER_HANDLE, BackupAnnotations.BackupDestination.CLOUD,
BackupAnnotations.OperationType.RESTORE);
mWallpaperBackupAgent.onRestoreFinished();
DataTypeResult result = getLoggingResult(WALLPAPER_IMG_SYSTEM,
mWallpaperBackupAgent.getBackupRestoreEventLogger().getLoggingResults());
assertThat(result).isNotNull();
assertThat(result.getSuccessCount()).isEqualTo(1);
}
@Test
public void testOnRestore_lockWallpaperImgSuccess_logsSuccess() throws Exception {
mockStagedWallpaperFile(WALLPAPER_INFO_STAGE);
mockStagedWallpaperFile(LOCK_WALLPAPER_STAGE);
mWallpaperBackupAgent.onCreate(USER_HANDLE, BackupAnnotations.BackupDestination.CLOUD,
BackupAnnotations.OperationType.RESTORE);
mWallpaperBackupAgent.onRestoreFinished();
DataTypeResult result = getLoggingResult(WALLPAPER_IMG_LOCK,
mWallpaperBackupAgent.getBackupRestoreEventLogger().getLoggingResults());
assertThat(result).isNotNull();
assertThat(result.getSuccessCount()).isEqualTo(1);
}
@Test
public void testOnRestore_systemWallpaperImgMissingAndNoLive_logsFailure() throws Exception {
mockStagedWallpaperFile(WALLPAPER_INFO_STAGE);
mockStagedWallpaperFile(LOCK_WALLPAPER_STAGE);
mWallpaperBackupAgent.onCreate(USER_HANDLE, BackupAnnotations.BackupDestination.CLOUD,
BackupAnnotations.OperationType.RESTORE);
mWallpaperBackupAgent.onRestoreFinished();
DataTypeResult result = getLoggingResult(WALLPAPER_IMG_SYSTEM,
mWallpaperBackupAgent.getBackupRestoreEventLogger().getLoggingResults());
assertThat(result).isNotNull();
assertThat(result.getFailCount()).isEqualTo(1);
assertThat(result.getErrors()).containsKey(ERROR_NO_WALLPAPER);
}
@Test
public void testOnRestore_lockWallpaperImgMissingAndNoLive_logsFailure() throws Exception {
mockStagedWallpaperFile(WALLPAPER_INFO_STAGE);
mockStagedWallpaperFile(SYSTEM_WALLPAPER_STAGE);
mWallpaperBackupAgent.onCreate(USER_HANDLE, BackupAnnotations.BackupDestination.CLOUD,
BackupAnnotations.OperationType.RESTORE);
mWallpaperBackupAgent.onRestoreFinished();
DataTypeResult result = getLoggingResult(WALLPAPER_IMG_LOCK,
mWallpaperBackupAgent.getBackupRestoreEventLogger().getLoggingResults());
assertThat(result).isNotNull();
assertThat(result.getFailCount()).isEqualTo(1);
assertThat(result.getErrors()).containsKey(ERROR_NO_WALLPAPER);
}
@Test
public void testOnRestore_wallpaperInfoMissing_logsFailure() throws Exception {
mockStagedWallpaperFile(SYSTEM_WALLPAPER_STAGE);
mWallpaperBackupAgent.onCreate(USER_HANDLE, BackupAnnotations.BackupDestination.CLOUD,
BackupAnnotations.OperationType.RESTORE);
mWallpaperBackupAgent.onRestoreFinished();
DataTypeResult result = getLoggingResult(WALLPAPER_IMG_SYSTEM,
mWallpaperBackupAgent.getBackupRestoreEventLogger().getLoggingResults());
assertThat(result).isNotNull();
assertThat(result.getFailCount()).isEqualTo(1);
assertThat(result.getErrors()).containsKey(ERROR_NO_METADATA);
}
@Test
public void testOnRestore_imgMissingButWallpaperInfoHasLive_doesNotLogImg() throws Exception {
mockRestoredLiveWallpaperFile();
mWallpaperBackupAgent.onCreate(USER_HANDLE, BackupAnnotations.BackupDestination.CLOUD,
BackupAnnotations.OperationType.RESTORE);
mWallpaperBackupAgent.onRestoreFinished();
DataTypeResult system = getLoggingResult(WALLPAPER_IMG_SYSTEM,
mWallpaperBackupAgent.getBackupRestoreEventLogger().getLoggingResults());
DataTypeResult lock = getLoggingResult(WALLPAPER_IMG_LOCK,
mWallpaperBackupAgent.getBackupRestoreEventLogger().getLoggingResults());
assertThat(system).isNull();
assertThat(lock).isNull();
}
@Test
public void testOnRestore_throwsException_logsErrors() throws Exception {
when(mWallpaperManager.setStream(any(), any(), anyBoolean(), anyInt())).thenThrow(
new RuntimeException());
mockStagedWallpaperFile(SYSTEM_WALLPAPER_STAGE);
mockStagedWallpaperFile(WALLPAPER_INFO_STAGE);
mWallpaperBackupAgent.onCreate(USER_HANDLE, BackupAnnotations.BackupDestination.CLOUD,
BackupAnnotations.OperationType.RESTORE);
mWallpaperBackupAgent.onRestoreFinished();
DataTypeResult system = getLoggingResult(WALLPAPER_IMG_SYSTEM,
mWallpaperBackupAgent.getBackupRestoreEventLogger().getLoggingResults());
DataTypeResult lock = getLoggingResult(WALLPAPER_IMG_LOCK,
mWallpaperBackupAgent.getBackupRestoreEventLogger().getLoggingResults());
assertThat(system).isNotNull();
assertThat(system.getFailCount()).isEqualTo(1);
assertThat(system.getErrors()).containsKey(RuntimeException.class.getName());
assertThat(lock).isNotNull();
assertThat(lock.getFailCount()).isEqualTo(1);
assertThat(lock.getErrors()).containsKey(RuntimeException.class.getName());
}
private void mockCurrentWallpaperIds(int systemWallpaperId, int lockWallpaperId) {
when(mWallpaperManager.getWallpaperId(eq(FLAG_SYSTEM))).thenReturn(systemWallpaperId);
when(mWallpaperManager.getWallpaperId(eq(FLAG_LOCK))).thenReturn(lockWallpaperId);
@@ -636,6 +758,27 @@ public class WallpaperBackupAgentTest {
ParcelFileDescriptor.open(fakeLockWallpaperFile, MODE_READ_ONLY));
}
private void mockStagedWallpaperFile(String location) throws Exception {
File wallpaperFile = new File(mContext.getFilesDir(), location);
wallpaperFile.createNewFile();
}
private void mockRestoredLiveWallpaperFile() throws Exception {
File wallpaperFile = new File(mContext.getFilesDir(), WALLPAPER_INFO_STAGE);
wallpaperFile.createNewFile();
FileOutputStream fstream = new FileOutputStream(wallpaperFile, false);
TypedXmlSerializer out = Xml.resolveSerializer(fstream);
out.startDocument(null, true);
out.startTag(null, "wp");
out.attribute(null, "component",
getFakeWallpaperInfo().getComponent().flattenToShortString());
out.endTag(null, "wp");
out.endDocument();
fstream.flush();
FileUtils.sync(fstream);
fstream.close();
}
private WallpaperInfo getFakeWallpaperInfo() throws Exception {
Context context = InstrumentationRegistry.getTargetContext();
Intent intent = new Intent(WallpaperService.SERVICE_INTERFACE);

View File

@@ -21,16 +21,14 @@ import static com.android.wallpaperbackup.WallpaperEventLogger.WALLPAPER_IMG_SYS
import static com.android.wallpaperbackup.WallpaperEventLogger.WALLPAPER_LIVE_LOCK;
import static com.android.wallpaperbackup.WallpaperEventLogger.WALLPAPER_LIVE_SYSTEM;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.app.WallpaperInfo;
import android.app.backup.BackupAnnotations;
import android.app.backup.BackupManager;
import android.app.backup.BackupRestoreEventLogger;
import android.content.Context;
@@ -42,8 +40,6 @@ import android.service.wallpaper.WallpaperService;
import androidx.test.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;
import com.android.wallpaperbackup.utils.TestWallpaperService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -55,8 +51,7 @@ import java.util.List;
@RunWith(AndroidJUnit4.class)
public class WallpaperEventLoggerTest {
@Mock
private BackupRestoreEventLogger mMockLogger;
private BackupRestoreEventLogger mEventLogger;
@Mock
private BackupManager mMockBackupManager;
@@ -73,8 +68,8 @@ public class WallpaperEventLoggerTest {
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
when(mMockBackupAgent.getBackupRestoreEventLogger()).thenReturn(mMockLogger);
when(mMockBackupManager.getBackupRestoreEventLogger(any())).thenReturn(mMockLogger);
when(mMockBackupAgent.getBackupRestoreEventLogger()).thenReturn(mEventLogger);
when(mMockBackupManager.getBackupRestoreEventLogger(any())).thenReturn(mEventLogger);
mWallpaperInfo = getWallpaperInfo();
mWallpaperEventLogger = new WallpaperEventLogger(mMockBackupManager, mMockBackupAgent);
@@ -82,115 +77,339 @@ public class WallpaperEventLoggerTest {
@Test
public void onSystemImgWallpaperBackedUp_logsSuccess() {
mWallpaperEventLogger.onSystemImageWallpaperBackedUp();
setUpLoggerForBackup();
verify(mMockLogger).logItemsBackedUp(eq(WALLPAPER_IMG_SYSTEM), eq(1));
mWallpaperEventLogger.onSystemImageWallpaperBackedUp();
BackupRestoreEventLogger.DataTypeResult result = getLogsForType(WALLPAPER_IMG_SYSTEM);
assertThat(result).isNotNull();
assertThat(result.getSuccessCount()).isEqualTo(1);
}
@Test
public void onLockImgWallpaperBackedUp_logsSuccess() {
mWallpaperEventLogger.onLockImageWallpaperBackedUp();
setUpLoggerForBackup();
verify(mMockLogger).logItemsBackedUp(eq(WALLPAPER_IMG_LOCK), eq(1));
mWallpaperEventLogger.onLockImageWallpaperBackedUp();
BackupRestoreEventLogger.DataTypeResult result = getLogsForType(WALLPAPER_IMG_LOCK);
assertThat(result).isNotNull();
assertThat(result.getSuccessCount()).isEqualTo(1);
}
@Test
public void onSystemLiveWallpaperBackedUp_logsSuccess() {
mWallpaperEventLogger.onSystemLiveWallpaperBackedUp(mWallpaperInfo);
setUpLoggerForBackup();
verify(mMockLogger).logItemsBackedUp(eq(WALLPAPER_LIVE_SYSTEM), eq(1));
mWallpaperEventLogger.onSystemLiveWallpaperBackedUp(mWallpaperInfo);
BackupRestoreEventLogger.DataTypeResult result = getLogsForType(WALLPAPER_LIVE_SYSTEM);
assertThat(result).isNotNull();
assertThat(result.getSuccessCount()).isEqualTo(1);
}
@Test
public void onLockLiveWallpaperBackedUp_logsSuccess() {
mWallpaperEventLogger.onLockLiveWallpaperBackedUp(mWallpaperInfo);
setUpLoggerForBackup();
verify(mMockLogger).logItemsBackedUp(eq(WALLPAPER_LIVE_LOCK), eq(1));
mWallpaperEventLogger.onLockLiveWallpaperBackedUp(mWallpaperInfo);
BackupRestoreEventLogger.DataTypeResult result = getLogsForType(WALLPAPER_LIVE_LOCK);
assertThat(result).isNotNull();
assertThat(result.getSuccessCount()).isEqualTo(1);
}
@Test
public void onImgWallpaperBackedUp_nullInfo_doesNotLogMetadata() {
mWallpaperEventLogger.onSystemImageWallpaperBackedUp();
setUpLoggerForBackup();
verify(mMockLogger, never()).logBackupMetadata(eq(WALLPAPER_IMG_SYSTEM), anyString());
mWallpaperEventLogger.onSystemImageWallpaperBackedUp();
BackupRestoreEventLogger.DataTypeResult result = getLogsForType(WALLPAPER_IMG_SYSTEM);
assertThat(result).isNotNull();
assertThat(result.getMetadataHash()).isNull();
}
@Test
public void onLiveWallpaperBackedUp_logsMetadata() {
mWallpaperEventLogger.onSystemLiveWallpaperBackedUp(mWallpaperInfo);
setUpLoggerForBackup();
verify(mMockLogger).logBackupMetadata(eq(WALLPAPER_LIVE_SYSTEM),
eq(TestWallpaperService.class.getName()));
mWallpaperEventLogger.onSystemLiveWallpaperBackedUp(mWallpaperInfo);
BackupRestoreEventLogger.DataTypeResult result = getLogsForType(WALLPAPER_LIVE_SYSTEM);
assertThat(result).isNotNull();
assertThat(result.getMetadataHash()).isNotNull();
}
@Test
public void onSystemImgWallpaperBackupFailed_logsFail() {
mWallpaperEventLogger.onSystemImageWallpaperBackupFailed(WALLPAPER_ERROR);
setUpLoggerForBackup();
verify(mMockLogger).logItemsBackupFailed(eq(WALLPAPER_IMG_SYSTEM), eq(1),
eq(WALLPAPER_ERROR));
mWallpaperEventLogger.onSystemImageWallpaperBackupFailed(WALLPAPER_ERROR);
BackupRestoreEventLogger.DataTypeResult result = getLogsForType(WALLPAPER_IMG_SYSTEM);
assertThat(result).isNotNull();
assertThat(result.getFailCount()).isEqualTo(1);
assertThat(result.getErrors()).containsKey(WALLPAPER_ERROR);
}
@Test
public void onLockImgWallpaperBackupFailed_logsFail() {
mWallpaperEventLogger.onLockImageWallpaperBackupFailed(WALLPAPER_ERROR);
setUpLoggerForBackup();
verify(mMockLogger).logItemsBackupFailed(eq(WALLPAPER_IMG_LOCK), eq(1),
eq(WALLPAPER_ERROR));
mWallpaperEventLogger.onLockImageWallpaperBackupFailed(WALLPAPER_ERROR);
BackupRestoreEventLogger.DataTypeResult result = getLogsForType(WALLPAPER_IMG_LOCK);
assertThat(result).isNotNull();
assertThat(result.getFailCount()).isEqualTo(1);
assertThat(result.getErrors()).containsKey(WALLPAPER_ERROR);
}
@Test
public void onSystemLiveWallpaperBackupFailed_logsFail() {
mWallpaperEventLogger.onSystemLiveWallpaperBackupFailed(WALLPAPER_ERROR);
setUpLoggerForBackup();
verify(mMockLogger).logItemsBackupFailed(eq(WALLPAPER_LIVE_SYSTEM), eq(1),
eq(WALLPAPER_ERROR));
mWallpaperEventLogger.onSystemLiveWallpaperBackupFailed(WALLPAPER_ERROR);
BackupRestoreEventLogger.DataTypeResult result = getLogsForType(WALLPAPER_LIVE_SYSTEM);
assertThat(result).isNotNull();
assertThat(result.getFailCount()).isEqualTo(1);
assertThat(result.getErrors()).containsKey(WALLPAPER_ERROR);
}
@Test
public void onLockLiveWallpaperBackupFailed_logsFail() {
mWallpaperEventLogger.onLockLiveWallpaperBackupFailed(WALLPAPER_ERROR);
setUpLoggerForBackup();
verify(mMockLogger).logItemsBackupFailed(eq(WALLPAPER_LIVE_LOCK), eq(1),
eq(WALLPAPER_ERROR));
mWallpaperEventLogger.onLockLiveWallpaperBackupFailed(WALLPAPER_ERROR);
BackupRestoreEventLogger.DataTypeResult result = getLogsForType(WALLPAPER_LIVE_LOCK);
assertThat(result).isNotNull();
assertThat(result.getFailCount()).isEqualTo(1);
assertThat(result.getErrors()).containsKey(WALLPAPER_ERROR);
}
@Test
public void onWallpaperBackupException_someProcessed_doesNotLogErrorForProcessedType() {
setUpLoggerForBackup();
mWallpaperEventLogger.onSystemImageWallpaperBackedUp();
mWallpaperEventLogger.onBackupException(new Exception());
BackupRestoreEventLogger.DataTypeResult result = getLogsForType(WALLPAPER_IMG_SYSTEM);
verify(mMockLogger, never()).logItemsBackupFailed(eq(WALLPAPER_IMG_SYSTEM), anyInt(),
anyString());
assertThat(result).isNotNull();
assertThat(result.getFailCount()).isEqualTo(0);
}
@Test
public void onWallpaperBackupException_someProcessed_logsErrorForUnprocessedType() {
setUpLoggerForBackup();
mWallpaperEventLogger.onSystemImageWallpaperBackedUp();
mWallpaperEventLogger.onBackupException(new Exception());
BackupRestoreEventLogger.DataTypeResult result = getLogsForType(WALLPAPER_IMG_LOCK);
verify(mMockLogger).logItemsBackupFailed(eq(WALLPAPER_IMG_LOCK), eq(1),
eq(Exception.class.getName()));
assertThat(result).isNotNull();
assertThat(result.getFailCount()).isEqualTo(1);
}
@Test
public void onWallpaperBackupException_liveTypeProcessed_doesNotLogErrorForSameImgType() {
public void onWallpaperBackupException_liveTypeProcessed_doesNotLogForSameImgType() {
setUpLoggerForBackup();
mWallpaperEventLogger.onSystemLiveWallpaperBackedUp(mWallpaperInfo);
mWallpaperEventLogger.onBackupException(new Exception());
BackupRestoreEventLogger.DataTypeResult result = getLogsForType(WALLPAPER_IMG_SYSTEM);
verify(mMockLogger, never()).logItemsBackupFailed(eq(WALLPAPER_IMG_SYSTEM), anyInt(),
anyString());
assertThat(result).isNull();
}
@Test
public void onSystemImgWallpaperRestored_logsSuccess() {
setUpLoggerForRestore();
mWallpaperEventLogger.onSystemImageWallpaperRestored();
BackupRestoreEventLogger.DataTypeResult result = getLogsForType(WALLPAPER_IMG_SYSTEM);
assertThat(result).isNotNull();
assertThat(result.getSuccessCount()).isEqualTo(1);
}
@Test
public void onLockImgWallpaperRestored_logsSuccess() {
setUpLoggerForRestore();
mWallpaperEventLogger.onLockImageWallpaperRestored();
BackupRestoreEventLogger.DataTypeResult result = getLogsForType(WALLPAPER_IMG_LOCK);
assertThat(result).isNotNull();
assertThat(result.getSuccessCount()).isEqualTo(1);
}
@Test
public void onSystemLiveWallpaperRestored_logsSuccess() {
setUpLoggerForRestore();
mWallpaperEventLogger.onSystemLiveWallpaperRestored(mWallpaperInfo.getComponent());
BackupRestoreEventLogger.DataTypeResult result = getLogsForType(WALLPAPER_LIVE_SYSTEM);
assertThat(result).isNotNull();
assertThat(result.getSuccessCount()).isEqualTo(1);
}
@Test
public void onLockLiveWallpaperRestored_logsSuccess() {
setUpLoggerForRestore();
mWallpaperEventLogger.onLockLiveWallpaperRestored(mWallpaperInfo.getComponent());
BackupRestoreEventLogger.DataTypeResult result = getLogsForType(WALLPAPER_LIVE_LOCK);
assertThat(result).isNotNull();
assertThat(result.getSuccessCount()).isEqualTo(1);
}
@Test
public void onImgWallpaperRestored_nullInfo_doesNotLogMetadata() {
setUpLoggerForRestore();
mWallpaperEventLogger.onSystemImageWallpaperRestored();
BackupRestoreEventLogger.DataTypeResult result = getLogsForType(WALLPAPER_IMG_SYSTEM);
assertThat(result).isNotNull();
assertThat(result.getMetadataHash()).isNull();
}
@Test
public void onLiveWallpaperRestored_logsMetadata() {
setUpLoggerForRestore();
mWallpaperEventLogger.onSystemLiveWallpaperRestored(mWallpaperInfo.getComponent());
BackupRestoreEventLogger.DataTypeResult result = getLogsForType(WALLPAPER_LIVE_SYSTEM);
assertThat(result).isNotNull();
assertThat(result.getMetadataHash()).isNotNull();
}
@Test
public void onSystemImgWallpaperRestoreFailed_logsFail() {
setUpLoggerForRestore();
mWallpaperEventLogger.onSystemImageWallpaperRestoreFailed(WALLPAPER_ERROR);
BackupRestoreEventLogger.DataTypeResult result = getLogsForType(WALLPAPER_IMG_SYSTEM);
assertThat(result).isNotNull();
assertThat(result.getFailCount()).isEqualTo(1);
assertThat(result.getErrors()).containsKey(WALLPAPER_ERROR);
}
@Test
public void onLockImgWallpaperRestoreFailed_logsFail() {
setUpLoggerForRestore();
mWallpaperEventLogger.onLockImageWallpaperRestoreFailed(WALLPAPER_ERROR);
BackupRestoreEventLogger.DataTypeResult result = getLogsForType(WALLPAPER_IMG_LOCK);
assertThat(result).isNotNull();
assertThat(result.getFailCount()).isEqualTo(1);
assertThat(result.getErrors()).containsKey(WALLPAPER_ERROR);
}
@Test
public void onSystemLiveWallpaperRestoreFailed_logsFail() {
setUpLoggerForRestore();
mWallpaperEventLogger.onSystemLiveWallpaperRestoreFailed(WALLPAPER_ERROR);
BackupRestoreEventLogger.DataTypeResult result = getLogsForType(WALLPAPER_LIVE_SYSTEM);
assertThat(result).isNotNull();
assertThat(result.getFailCount()).isEqualTo(1);
assertThat(result.getErrors()).containsKey(WALLPAPER_ERROR);
}
@Test
public void onLockLiveWallpaperRestoreFailed_logsFail() {
setUpLoggerForRestore();
mWallpaperEventLogger.onLockLiveWallpaperRestoreFailed(WALLPAPER_ERROR);
BackupRestoreEventLogger.DataTypeResult result = getLogsForType(WALLPAPER_LIVE_LOCK);
assertThat(result).isNotNull();
assertThat(result.getFailCount()).isEqualTo(1);
assertThat(result.getErrors()).containsKey(WALLPAPER_ERROR);
}
@Test
public void onWallpaperRestoreException_someProcessed_doesNotLogErrorForProcessedType() {
setUpLoggerForRestore();
mWallpaperEventLogger.onSystemImageWallpaperRestored();
mWallpaperEventLogger.onRestoreException(new Exception());
BackupRestoreEventLogger.DataTypeResult result = getLogsForType(WALLPAPER_IMG_SYSTEM);
assertThat(result).isNotNull();
assertThat(result.getFailCount()).isEqualTo(0);
}
@Test
public void onWallpaperRestoreException_someProcessed_logsErrorForUnprocessedType() {
setUpLoggerForRestore();
mWallpaperEventLogger.onSystemImageWallpaperRestored();
mWallpaperEventLogger.onRestoreException(new Exception());
BackupRestoreEventLogger.DataTypeResult result = getLogsForType(WALLPAPER_IMG_LOCK);
assertThat(result).isNotNull();
assertThat(result.getFailCount()).isEqualTo(1);
}
@Test
public void onWallpaperRestoreException_liveTypeProcessed_doesNotLogForSameImgType() {
setUpLoggerForRestore();
mWallpaperEventLogger.onSystemLiveWallpaperRestored(mWallpaperInfo.getComponent());
mWallpaperEventLogger.onRestoreException(new Exception());
BackupRestoreEventLogger.DataTypeResult result = getLogsForType(WALLPAPER_IMG_SYSTEM);
assertThat(result).isNull();
}
private BackupRestoreEventLogger.DataTypeResult getLogsForType(String dataType) {
for (BackupRestoreEventLogger.DataTypeResult result : mEventLogger.getLoggingResults()) {
if ((result.getDataType()).equals(dataType)) {
return result;
}
}
return null;
}
private void setUpLoggerForBackup() {
mEventLogger = new BackupRestoreEventLogger(BackupAnnotations.OperationType.BACKUP);
createEventLogger();
}
private void setUpLoggerForRestore() {
mEventLogger = new BackupRestoreEventLogger(BackupAnnotations.OperationType.RESTORE);
createEventLogger();
}
private void createEventLogger() {
when(mMockBackupAgent.getBackupRestoreEventLogger()).thenReturn(mEventLogger);
when(mMockBackupManager.getBackupRestoreEventLogger(any())).thenReturn(mEventLogger);
mWallpaperEventLogger = new WallpaperEventLogger(mMockBackupManager, mMockBackupAgent);
}
private WallpaperInfo getWallpaperInfo() throws Exception {
Context context = InstrumentationRegistry.getTargetContext();
Intent intent = new Intent(WallpaperService.SERVICE_INTERFACE);