1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
package org.robolectric.shadows;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
import static org.robolectric.Shadows.shadowOf;
import static org.robolectric.util.reflector.Reflector.reflector;
import android.app.backup.BackupDataOutput;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import java.io.IOException;
import java.util.Arrays;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.util.reflector.ForType;
@RunWith(AndroidJUnit4.class)
public final class ShadowBackupDataOutputTest {
private static final String TEST_PREFIX = "prefix";
private static final String TEST_KEY = "key";
private static final byte[] TEST_DATA = {1, 2, 3, 4};
private final BackupDataOutput backupDataOutput = BackupDataOutputFactory.newInstance();
@Test
public void writeEntityHeader_withUnfinishedData_throwsException() throws IOException {
backupDataOutput.writeEntityHeader(TEST_KEY, TEST_DATA.length);
assertThrows(IOException.class, () -> backupDataOutput.writeEntityHeader("key_2", 5));
}
@Test
public void writeEntityData_withKeyPrefix_hasPrefixInEntityKey() throws IOException {
reflector(BackupDataOutputReflector.class, backupDataOutput).setKeyPrefix(TEST_PREFIX);
backupDataOutput.writeEntityHeader(TEST_KEY, 0);
assertThat(shadowOf(backupDataOutput).getEntities())
.containsExactly(
BackupDataEntity.create(
TEST_PREFIX + ShadowBackupDataOutput.KEY_PREFIX_JOINER + TEST_KEY, new byte[0]));
}
@Test
public void writeEntityData_withNonNegativeDataSize_addsEntityOfSize() throws IOException {
backupDataOutput.writeEntityHeader(TEST_KEY, 5);
assertThat(shadowOf(backupDataOutput).getEntities())
.containsExactly(BackupDataEntity.create(TEST_KEY, new byte[5]));
}
@Test
public void writeEntityData_withNegativeDataSize_addsDeletedEntity() throws IOException {
backupDataOutput.writeEntityHeader(TEST_KEY, -1);
assertThat(shadowOf(backupDataOutput).getEntities())
.containsExactly(BackupDataEntity.createDeletedEntity(TEST_KEY));
}
@Test
public void writeEntityData_withGreaterSizeThanSource_throwsException() throws IOException {
backupDataOutput.writeEntityHeader(TEST_KEY, 10);
assertThrows(IOException.class, () -> backupDataOutput.writeEntityData(new byte[5], 10));
}
@Test
public void writeEntityData_withGreaterSizeThanDestination_throwsException() throws IOException {
backupDataOutput.writeEntityHeader(TEST_KEY, 2);
assertThrows(
IOException.class, () -> backupDataOutput.writeEntityData(TEST_DATA, TEST_DATA.length));
}
@Test
public void writeEntityData_withFullDataWrite_addsCorrectEntityData() throws IOException {
backupDataOutput.writeEntityHeader(TEST_KEY, TEST_DATA.length);
backupDataOutput.writeEntityData(TEST_DATA, TEST_DATA.length);
assertThat(shadowOf(backupDataOutput).getEntities())
.containsExactly(BackupDataEntity.create(TEST_KEY, TEST_DATA));
}
@Test
public void writeEntityData_withPartialDataWrites_addsCorrectEntityData() throws IOException {
backupDataOutput.writeEntityHeader(TEST_KEY, 4);
backupDataOutput.writeEntityData(Arrays.copyOfRange(TEST_DATA, 0, 2), 2);
backupDataOutput.writeEntityData(Arrays.copyOfRange(TEST_DATA, 2, 4), 2);
assertThat(shadowOf(backupDataOutput).getEntities())
.containsExactly(BackupDataEntity.create(TEST_KEY, TEST_DATA));
}
@ForType(BackupDataOutput.class)
private interface BackupDataOutputReflector {
void setKeyPrefix(String prefix);
}
}
|