Skip to content

Commit 1b2b3ce

Browse files
committed
test: add tests covering SupportHelperTest
1 parent f38c145 commit 1b2b3ce

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package net.zetetic.database.sqlcipher_cts;
2+
3+
import android.content.Context;
4+
import androidx.annotation.CallSuper;
5+
import androidx.annotation.NonNull;
6+
import androidx.sqlite.db.SupportSQLiteDatabase;
7+
import androidx.sqlite.db.SupportSQLiteOpenHelper;
8+
import androidx.test.ext.junit.runners.AndroidJUnit4;
9+
import androidx.test.platform.app.InstrumentationRegistry;
10+
import net.zetetic.database.sqlcipher.SupportHelper;
11+
import org.junit.Before;
12+
import org.junit.Test;
13+
import org.junit.runner.RunWith;
14+
15+
import static org.junit.Assert.assertEquals;
16+
17+
@RunWith(AndroidJUnit4.class)
18+
public class SupportHelperTest {
19+
20+
private static final String DATABASE_NAME = "DB-Test.db";
21+
private static final int CREATION_INDEX = 0;
22+
private static final int UPGRADE_INDEX = 0;
23+
24+
@Before
25+
public void setup() {
26+
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
27+
for (String databaseName : context.databaseList()) {
28+
context.deleteDatabase(databaseName);
29+
}
30+
}
31+
32+
@Test
33+
public void shouldCreateDatabaseNormallyWithInitialVersion() {
34+
FakeCallback callbackWrapper = new FakeCallback(1);
35+
36+
SupportSQLiteOpenHelper.Configuration configuration = createConfiguration(callbackWrapper);
37+
SupportHelper helper = new SupportHelper(configuration, null, null, true);
38+
39+
helper.getWritableDatabase();
40+
helper.close();
41+
42+
assertEquals(1, callbackWrapper.callbackCount[CREATION_INDEX]);
43+
assertEquals(1, callbackWrapper.callbackCount[UPGRADE_INDEX]);
44+
}
45+
46+
@Test
47+
public void shouldRunUpgradeFromVersion1ToVersion2() {
48+
FakeCallback initialCallback = new FakeCallback(1);
49+
50+
SupportHelper initialHelper = new SupportHelper(createConfiguration(initialCallback), null, null, true);
51+
52+
initialHelper.getWritableDatabase();
53+
initialHelper.close();
54+
55+
FakeCallback callbackWrapper = new FakeCallback(2);
56+
57+
SupportHelper helper = new SupportHelper(createConfiguration(callbackWrapper), null, null, true);
58+
59+
helper.getWritableDatabase();
60+
helper.close();
61+
62+
assertEquals(0, callbackWrapper.callbackCount[CREATION_INDEX]);
63+
assertEquals(1, callbackWrapper.callbackCount[UPGRADE_INDEX]);
64+
}
65+
66+
private SupportSQLiteOpenHelper.Configuration createConfiguration(SupportSQLiteOpenHelper.Callback callback) {
67+
Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
68+
return SupportSQLiteOpenHelper.Configuration.builder(context)
69+
.name(DATABASE_NAME)
70+
.callback(callback)
71+
.build();
72+
}
73+
74+
private static class FakeCallback extends SupportSQLiteOpenHelper.Callback {
75+
public final int[] callbackCount = {0, 0};
76+
77+
public FakeCallback(int version) {
78+
super(version);
79+
}
80+
81+
SupportSQLiteOpenHelper.Callback callback = new SupportSQLiteOpenHelper.Callback(version) {
82+
@Override
83+
public void onCreate(@NonNull SupportSQLiteDatabase db) {
84+
callbackCount[CREATION_INDEX] += 1;
85+
}
86+
87+
@Override
88+
public void onUpgrade(@NonNull SupportSQLiteDatabase db, int oldVersion, int newVersion) {
89+
callbackCount[UPGRADE_INDEX] += 1;
90+
}
91+
};
92+
93+
@Override
94+
@CallSuper
95+
public void onCreate(@NonNull SupportSQLiteDatabase db) {
96+
callback.onCreate(db);
97+
}
98+
99+
@Override
100+
@CallSuper
101+
public void onUpgrade(@NonNull SupportSQLiteDatabase db, int oldVersion, int newVersion) {
102+
callback.onUpgrade(db, oldVersion, newVersion);
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)