mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-02-27 05:03:28 +00:00
Introduce SignalDatabase as the main database entrypoint.
This commit is contained in:
@@ -3,7 +3,6 @@ package org.thoughtcrime.securesms.conversationlist;
|
||||
import android.app.Application;
|
||||
import android.database.Cursor;
|
||||
|
||||
import androidx.test.core.app.ApplicationProvider;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
@@ -16,16 +15,14 @@ import org.powermock.modules.junit4.rule.PowerMockRule;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.thoughtcrime.securesms.conversationlist.model.ConversationReader;
|
||||
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
||||
import org.thoughtcrime.securesms.database.DatabaseObserver;
|
||||
import org.thoughtcrime.securesms.database.SignalDatabase;
|
||||
import org.thoughtcrime.securesms.database.ThreadDatabase;
|
||||
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
|
||||
import org.thoughtcrime.securesms.util.paging.Invalidator;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyLong;
|
||||
import static org.mockito.Matchers.eq;
|
||||
import static org.mockito.Mockito.verify;
|
||||
@@ -37,7 +34,7 @@ import static org.powermock.api.mockito.PowerMockito.when;
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(manifest = Config.NONE, application = Application.class)
|
||||
@PowerMockIgnore({ "org.mockito.*", "org.robolectric.*", "android.*", "androidx.*", "org.powermock.*" })
|
||||
@PrepareForTest({ ApplicationDependencies.class, DatabaseFactory.class, DatabaseObserver.class })
|
||||
@PrepareForTest({ ApplicationDependencies.class, SignalDatabase.class, DatabaseObserver.class })
|
||||
public class UnarchivedConversationListDataSourceTest {
|
||||
|
||||
@Rule
|
||||
@@ -50,11 +47,11 @@ public class UnarchivedConversationListDataSourceTest {
|
||||
@Before
|
||||
public void setUp() {
|
||||
mockStatic(ApplicationDependencies.class);
|
||||
mockStatic(DatabaseFactory.class);
|
||||
mockStatic(SignalDatabase.class);
|
||||
|
||||
threadDatabase = mock(ThreadDatabase.class);
|
||||
|
||||
when(DatabaseFactory.getThreadDatabase(any())).thenReturn(threadDatabase);
|
||||
when(SignalDatabase.threads()).thenReturn(threadDatabase);
|
||||
when(ApplicationDependencies.getDatabaseObserver()).thenReturn(mock(DatabaseObserver.class));
|
||||
|
||||
testSubject = new ConversationListDataSource.UnarchivedConversationListDataSource(mock(Application.class));
|
||||
|
||||
@@ -7,13 +7,8 @@ import androidx.test.core.app.ApplicationProvider;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.core.classloader.annotations.PowerMockIgnore;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
||||
import org.thoughtcrime.securesms.database.DatabaseObserver;
|
||||
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
|
||||
import org.whispersystems.signalservice.api.payments.Currency;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@@ -8,15 +8,14 @@ import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.powermock.core.classloader.annotations.PrepareForTest;
|
||||
import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
||||
import org.thoughtcrime.securesms.database.MmsSmsDatabase;
|
||||
import org.thoughtcrime.securesms.database.RecipientDatabase;
|
||||
import org.thoughtcrime.securesms.database.SignalDatabase;
|
||||
import org.thoughtcrime.securesms.database.ThreadDatabase;
|
||||
import org.thoughtcrime.securesms.util.FeatureFlags;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyLong;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
@@ -25,7 +24,7 @@ import static org.powermock.api.mockito.PowerMockito.mockStatic;
|
||||
import static org.powermock.api.mockito.PowerMockito.when;
|
||||
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({DatabaseFactory.class, FeatureFlags.class})
|
||||
@PrepareForTest({SignalDatabase.class, FeatureFlags.class})
|
||||
public class RecipientUtilTest {
|
||||
|
||||
private Context context = mock(Context.class);
|
||||
@@ -36,10 +35,10 @@ public class RecipientUtilTest {
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
mockStatic(DatabaseFactory.class);
|
||||
when(DatabaseFactory.getThreadDatabase(any())).thenReturn(mockThreadDatabase);
|
||||
when(DatabaseFactory.getMmsSmsDatabase(any())).thenReturn(mockMmsSmsDatabase);
|
||||
when(DatabaseFactory.getRecipientDatabase(any())).thenReturn(mockRecipientDatabase);
|
||||
mockStatic(SignalDatabase.class);
|
||||
when(SignalDatabase.threads()).thenReturn(mockThreadDatabase);
|
||||
when(SignalDatabase.mmsSms()).thenReturn(mockMmsSmsDatabase);
|
||||
when(SignalDatabase.recipients()).thenReturn(mockRecipientDatabase);
|
||||
mockStatic(FeatureFlags.class);
|
||||
|
||||
when(recipient.getId()).thenReturn(RecipientId.from(5));
|
||||
|
||||
@@ -1,23 +1,23 @@
|
||||
package org.thoughtcrime.securesms.testing
|
||||
|
||||
import android.content.Context
|
||||
import android.app.Application
|
||||
import org.thoughtcrime.securesms.crypto.AttachmentSecret
|
||||
import org.thoughtcrime.securesms.crypto.DatabaseSecret
|
||||
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper
|
||||
import org.thoughtcrime.securesms.database.SignalDatabase
|
||||
import java.security.SecureRandom
|
||||
import android.database.sqlite.SQLiteDatabase as AndroidSQLiteDatabase
|
||||
import net.zetetic.database.sqlcipher.SQLiteDatabase as SQLCipherSQLiteDatabase
|
||||
import org.thoughtcrime.securesms.database.SQLiteDatabase as SignalSQLiteDatabase
|
||||
|
||||
/**
|
||||
* Proxy [SQLCipherOpenHelper] to the [TestSQLiteOpenHelper] interface.
|
||||
* Proxy [SignalDatabase] to the [TestSQLiteOpenHelper] interface.
|
||||
*/
|
||||
class ProxySQLCipherOpenHelper(
|
||||
context: Context,
|
||||
context: Application,
|
||||
val readableDatabase: AndroidSQLiteDatabase,
|
||||
val writableDatabase: AndroidSQLiteDatabase,
|
||||
) : SQLCipherOpenHelper(context, DatabaseSecret(ByteArray(32).apply { SecureRandom().nextBytes(this) })) {
|
||||
) : SignalDatabase(context, DatabaseSecret(ByteArray(32).apply { SecureRandom().nextBytes(this) }), AttachmentSecret()) {
|
||||
|
||||
constructor(context: Context, testOpenHelper: TestSQLiteOpenHelper) : this(context, testOpenHelper.readableDatabase, testOpenHelper.writableDatabase)
|
||||
constructor(context: Application, testOpenHelper: TestSQLiteOpenHelper) : this(context, testOpenHelper.readableDatabase, testOpenHelper.writableDatabase)
|
||||
|
||||
override fun close() {
|
||||
throw UnsupportedOperationException()
|
||||
@@ -43,15 +43,15 @@ class ProxySQLCipherOpenHelper(
|
||||
throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
override fun onOpen(db: SQLCipherSQLiteDatabase?) {
|
||||
override fun onOpen(db: net.zetetic.database.sqlcipher.SQLiteDatabase) {
|
||||
throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
override fun onCreate(db: SQLCipherSQLiteDatabase?) {
|
||||
override fun onCreate(db: net.zetetic.database.sqlcipher.SQLiteDatabase) {
|
||||
throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
override fun onUpgrade(db: SQLCipherSQLiteDatabase?, oldVersion: Int, newVersion: Int) {
|
||||
override fun onUpgrade(db: net.zetetic.database.sqlcipher.SQLiteDatabase, oldVersion: Int, newVersion: Int) {
|
||||
throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
@@ -63,27 +63,23 @@ class ProxySQLCipherOpenHelper(
|
||||
throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
override fun getRawReadableDatabase(): SQLCipherSQLiteDatabase {
|
||||
throw UnsupportedOperationException()
|
||||
}
|
||||
override val rawReadableDatabase: net.zetetic.database.sqlcipher.SQLiteDatabase
|
||||
get() = throw UnsupportedOperationException()
|
||||
|
||||
override fun getRawWritableDatabase(): SQLCipherSQLiteDatabase {
|
||||
throw UnsupportedOperationException()
|
||||
}
|
||||
override val rawWritableDatabase: net.zetetic.database.sqlcipher.SQLiteDatabase
|
||||
get() = throw UnsupportedOperationException()
|
||||
|
||||
override fun getSignalReadableDatabase(): SignalSQLiteDatabase {
|
||||
return ProxySignalSQLiteDatabase(readableDatabase)
|
||||
}
|
||||
override val signalReadableDatabase: org.thoughtcrime.securesms.database.SQLiteDatabase
|
||||
get() = ProxySignalSQLiteDatabase(readableDatabase)
|
||||
|
||||
override fun getSignalWritableDatabase(): SignalSQLiteDatabase {
|
||||
return ProxySignalSQLiteDatabase(writableDatabase)
|
||||
}
|
||||
override val signalWritableDatabase: org.thoughtcrime.securesms.database.SQLiteDatabase
|
||||
get() = ProxySignalSQLiteDatabase(writableDatabase)
|
||||
|
||||
override fun getSqlCipherDatabase(): SQLCipherSQLiteDatabase {
|
||||
throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
override fun markCurrent(db: SQLCipherSQLiteDatabase?) {
|
||||
override fun markCurrent(db: net.zetetic.database.sqlcipher.SQLiteDatabase) {
|
||||
throw UnsupportedOperationException()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user