### Question:
AppMetadataBackupWriter { public void backupWidget( PackageInfo packageInfo, File metadataFile, File filesDir, byte[] widgetData) throws IOException { Preconditions.checkArgument(widgetData.length > 0, "Can't backup widget with no data."); String packageName = packageInfo.packageName; FileOutputStream fileOutputStream = new FileOutputStream(metadataFile); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream); DataOutputStream dataOutputStream = new DataOutputStream(bufferedOutputStream); byte[] metadata = getMetadataBytes(packageName); bufferedOutputStream.write(metadata); writeWidgetData(dataOutputStream, widgetData); bufferedOutputStream.flush(); dataOutputStream.close(); metadataFile.setLastModified(0); FullBackup.backupToTar( packageName, null, null, filesDir.getAbsolutePath(), metadataFile.getAbsolutePath(), mOutput); } AppMetadataBackupWriter(FullBackupDataOutput output, PackageManager packageManager); void backupManifest(
            PackageInfo packageInfo, File manifestFile, File filesDir, boolean withApk); void backupManifest(
            PackageInfo packageInfo,
            File manifestFile,
            File filesDir,
            @Nullable String domain,
            @Nullable String linkDomain,
            boolean withApk); void backupWidget(
            PackageInfo packageInfo, File metadataFile, File filesDir, byte[] widgetData); void backupApk(PackageInfo packageInfo); void backupObb(@UserIdInt int userId, PackageInfo packageInfo);  }### Answer:
@Test public void testBackupWidget_writesCorrectData() throws Exception { PackageInfo packageInfo = createPackageInfo(TEST_PACKAGE, TEST_PACKAGE_INSTALLER, TEST_PACKAGE_VERSION_CODE); File metadataFile = createFile(BACKUP_METADATA_FILENAME); byte[] widgetBytes = "widget".getBytes(); mBackupWriter.backupWidget(packageInfo, metadataFile, mFilesDir, widgetBytes); byte[] writtenBytes = getWrittenBytes(mBackupDataOutputFile, false); String[] widgetData = new String(writtenBytes, StandardCharsets.UTF_8).split("\n"); assertThat(widgetData.length).isEqualTo(3); assertThat(widgetData[0]).isEqualTo(Integer.toString(BACKUP_METADATA_VERSION)); assertThat(widgetData[1]).isEqualTo(packageInfo.packageName); ByteArrayOutputStream expectedBytes = new ByteArrayOutputStream(); DataOutputStream stream = new DataOutputStream(expectedBytes); stream.writeInt(BACKUP_WIDGET_METADATA_TOKEN); stream.writeInt(widgetBytes.length); stream.write(widgetBytes); stream.flush(); assertThat(widgetData[2]).isEqualTo(expectedBytes.toString()); metadataFile.delete(); }
@Test public void testBackupWidget_withNullWidgetData_throwsNullPointerException() throws Exception { PackageInfo packageInfo = createPackageInfo(TEST_PACKAGE, TEST_PACKAGE_INSTALLER, TEST_PACKAGE_VERSION_CODE); File metadataFile = createFile(BACKUP_METADATA_FILENAME); expectThrows( NullPointerException.class, () -> mBackupWriter.backupWidget( packageInfo, metadataFile, mFilesDir, null)); metadataFile.delete(); }
@Test public void testBackupWidget_withEmptyWidgetData_throwsIllegalArgumentException() throws Exception { PackageInfo packageInfo = createPackageInfo(TEST_PACKAGE, TEST_PACKAGE_INSTALLER, TEST_PACKAGE_VERSION_CODE); File metadataFile = createFile(BACKUP_METADATA_FILENAME); expectThrows( IllegalArgumentException.class, () -> mBackupWriter.backupWidget( packageInfo, metadataFile, mFilesDir, new byte[0])); metadataFile.delete(); }
@Test public void testBackupWidget_whenRunPreviouslyWithSameData_producesSameBytesOnSecondRun() throws Exception { PackageInfo packageInfo = createPackageInfo(TEST_PACKAGE, TEST_PACKAGE_INSTALLER, TEST_PACKAGE_VERSION_CODE); File metadataFile = createFile(BACKUP_METADATA_FILENAME); byte[] widgetBytes = "widget".getBytes(); mBackupWriter.backupWidget(packageInfo, metadataFile, mFilesDir, widgetBytes); byte[] firstRunBytes = getWrittenBytes(mBackupDataOutputFile, true); modifyFileMetadata(metadataFile); mBackupWriter.backupWidget(packageInfo, metadataFile, mFilesDir, widgetBytes); byte[] secondRunBytes = getWrittenBytes(mBackupDataOutputFile, true); assertThat(firstRunBytes).isEqualTo(secondRunBytes); metadataFile.delete(); }