Convert SignalStore to kotlin.

This commit is contained in:
Greyson Parrelli
2024-06-20 11:30:30 -04:00
parent 341c474610
commit 863b443317
382 changed files with 1934 additions and 1952 deletions

View File

@@ -32,7 +32,7 @@ class SignalStoreRule @JvmOverloads constructor(private val defaultValues: KeyVa
}
dataSet = KeyValueDataSet()
SignalStore.inject(KeyValueStore(MockKeyValuePersistentStorage.withDataSet(dataSet)))
SignalStore.testInject(KeyValueStore(MockKeyValuePersistentStorage.withDataSet(dataSet)))
defaultValues.invoke(dataSet)
base.evaluate()

View File

@@ -1,91 +0,0 @@
package org.thoughtcrime.securesms.components.emoji;
import android.app.Application;
import androidx.test.core.app.ApplicationProvider;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockedConstruction;
import org.mockito.MockedStatic;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.ParameterizedRobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.thoughtcrime.securesms.crypto.AttachmentSecretProvider;
import org.thoughtcrime.securesms.dependencies.AppDependencies;
import org.thoughtcrime.securesms.emoji.EmojiSource;
import org.thoughtcrime.securesms.keyvalue.InternalValues;
import org.thoughtcrime.securesms.keyvalue.SignalStore;
import java.util.Arrays;
import java.util.Collection;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@RunWith(ParameterizedRobolectricTestRunner.class)
@Config(manifest = Config.NONE, application = Application.class)
public class EmojiUtilTest_isEmoji {
@Rule
public MockitoRule rule = MockitoJUnit.rule();
private final String input;
private final boolean output;
@ParameterizedRobolectricTestRunner.Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
{null, false},
{"", false},
{"cat", false},
{"ᑢᗩᖶ", false},
{"♍︎♋︎⧫︎", false},
{"", false},
{"¯\\_(ツ)_/¯", false},
{"\uD83D\uDE0D", true}, // Smiling face with heart-shaped eyes
{"\uD83D\uDD77", true}, // Spider
{"\uD83E\uDD37", true}, // Person shrugging
{"\uD83E\uDD37\uD83C\uDFFF\u200D♂", true}, // Man shrugging dark skin tone
{"\uD83D\uDC68\u200D\uD83D\uDC69\u200D\uD83D\uDC67\u200D\uD83D\uDC66", true}, // Family: Man, Woman, Girl, Boy
{"\uD83D\uDC68\uD83C\uDFFB\u200D\uD83D\uDC69\uD83C\uDFFB\u200D\uD83D\uDC67\uD83C\uDFFB\u200D\uD83D\uDC66\uD83C\uDFFB", true}, // Family - Man: Light Skin Tone, Woman: Light Skin Tone, Girl: Light Skin Tone, Boy: Light Skin Tone (NOTE: Not widely supported, good stretch test)
{"\uD83D\uDE0Dhi", false}, // Smiling face with heart-shaped eyes, text afterwards
{"\uD83D\uDE0D ", false}, // Smiling face with heart-shaped eyes, space afterwards
{"\uD83D\uDE0D\uD83D\uDE0D", false}, // Smiling face with heart-shaped eyes, twice
});
}
@Mock
private MockedStatic<AppDependencies> applicationDependenciesMockedStatic;
@Mock
private MockedStatic<AttachmentSecretProvider> attachmentSecretProviderMockedStatic;
@Mock
private MockedStatic<SignalStore> signalStoreMockedStatic;
@Mock
private MockedConstruction<SignalStore> signalStoreMockedConstruction;
public EmojiUtilTest_isEmoji(String input, boolean output) {
this.input = input;
this.output = output;
}
@Test
public void isEmoji() throws Exception {
Application application = ApplicationProvider.getApplicationContext();
when(AppDependencies.getApplication()).thenReturn(application);
when(AttachmentSecretProvider.getInstance(any())).thenThrow(RuntimeException.class);
when(SignalStore.internalValues()).thenReturn(mock(InternalValues.class));
EmojiSource.refresh();
assertEquals(output, EmojiUtil.isEmoji(input));
}
}

View File

@@ -0,0 +1,76 @@
package org.thoughtcrime.securesms.components.emoji
import android.app.Application
import androidx.test.core.app.ApplicationProvider
import org.junit.Assert
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers
import org.mockito.Mock
import org.mockito.MockedStatic
import org.mockito.Mockito
import org.mockito.junit.MockitoJUnit
import org.mockito.junit.MockitoRule
import org.robolectric.ParameterizedRobolectricTestRunner
import org.robolectric.annotation.Config
import org.thoughtcrime.securesms.crypto.AttachmentSecretProvider
import org.thoughtcrime.securesms.dependencies.AppDependencies
import org.thoughtcrime.securesms.emoji.EmojiSource.Companion.refresh
import org.thoughtcrime.securesms.keyvalue.KeyValueDataSet
import org.thoughtcrime.securesms.keyvalue.KeyValueStore
import org.thoughtcrime.securesms.keyvalue.MockKeyValuePersistentStorage
import org.thoughtcrime.securesms.keyvalue.SignalStore
@RunWith(ParameterizedRobolectricTestRunner::class)
@Config(manifest = Config.NONE, application = Application::class)
class EmojiUtilTest_isEmoji(private val input: String?, private val output: Boolean) {
@Rule
@JvmField
val rule: MockitoRule = MockitoJUnit.rule()
@Mock
private val applicationDependenciesMockedStatic: MockedStatic<AppDependencies>? = null
@Mock
private val attachmentSecretProviderMockedStatic: MockedStatic<AttachmentSecretProvider>? = null
@Throws(Exception::class)
@Test
fun isEmoji() {
val application = ApplicationProvider.getApplicationContext<Application>()
Mockito.`when`(AppDependencies.application).thenReturn(application)
Mockito.`when`(AttachmentSecretProvider.getInstance(ArgumentMatchers.any())).thenThrow(RuntimeException::class.java)
SignalStore.testInject(KeyValueStore(MockKeyValuePersistentStorage.withDataSet(KeyValueDataSet())))
refresh()
Assert.assertEquals(output, EmojiUtil.isEmoji(input))
}
companion object {
@JvmStatic
@ParameterizedRobolectricTestRunner.Parameters
fun data(): Collection<Array<Any?>> {
return listOf(
arrayOf(null, false),
arrayOf("", false),
arrayOf("cat", false),
arrayOf("ᑢᗩᖶ", false),
arrayOf("♍︎♋︎⧫︎", false),
arrayOf("", false),
arrayOf("¯\\_(ツ)_/¯", false),
arrayOf("\uD83D\uDE0D", true), // Smiling face with heart-shaped eyes
arrayOf("\uD83D\uDD77", true), // Spider
arrayOf("\uD83E\uDD37", true), // Person shrugging
arrayOf("\uD83E\uDD37\uD83C\uDFFF\u200D♂️", true), // Man shrugging dark skin tone
arrayOf("\uD83D\uDC68\u200D\uD83D\uDC69\u200D\uD83D\uDC67\u200D\uD83D\uDC66", true), // Family: Man, Woman, Girl, Boy
arrayOf("\uD83D\uDC68\uD83C\uDFFB\u200D\uD83D\uDC69\uD83C\uDFFB\u200D\uD83D\uDC67\uD83C\uDFFB\u200D\uD83D\uDC66\uD83C\uDFFB", true), // Family - Man: Light Skin Tone, Woman: Light Skin Tone, Girl: Light Skin Tone, Boy: Light Skin Tone (NOTE: Not widely supported, good stretch test)
arrayOf("\uD83D\uDE0Dhi", false), // Smiling face with heart-shaped eyes, text afterwards
arrayOf("\uD83D\uDE0D ", false), // Smiling face with heart-shaped eyes, space afterwards
arrayOf("\uD83D\uDE0D\uD83D\uDE0D", false) // Smiling face with heart-shaped eyes, twice
)
}
}
}

View File

@@ -40,7 +40,7 @@ class CrashConfigTest {
)
)
SignalStore.inject(store)
SignalStore.testInject(store)
}
@Test

View File

@@ -35,7 +35,7 @@ class PaymentsValuesTest {
}
)
assertEquals(PaymentsAvailability.NOT_IN_REGION, SignalStore.paymentsValues().paymentsAvailability)
assertEquals(PaymentsAvailability.NOT_IN_REGION, SignalStore.payments.paymentsAvailability)
}
@Test
@@ -51,7 +51,7 @@ class PaymentsValuesTest {
every { RemoteConfig.payments } returns false
every { RemoteConfig.paymentsCountryBlocklist } returns ""
assertEquals(PaymentsAvailability.DISABLED_REMOTELY, SignalStore.paymentsValues().paymentsAvailability)
assertEquals(PaymentsAvailability.DISABLED_REMOTELY, SignalStore.payments.paymentsAvailability)
}
@Test
@@ -67,7 +67,7 @@ class PaymentsValuesTest {
every { RemoteConfig.payments } returns false
every { RemoteConfig.paymentsCountryBlocklist } returns ""
assertEquals(PaymentsAvailability.WITHDRAW_ONLY, SignalStore.paymentsValues().paymentsAvailability)
assertEquals(PaymentsAvailability.WITHDRAW_ONLY, SignalStore.payments.paymentsAvailability)
}
@Test
@@ -83,7 +83,7 @@ class PaymentsValuesTest {
every { RemoteConfig.payments } returns true
every { RemoteConfig.paymentsCountryBlocklist } returns ""
assertEquals(PaymentsAvailability.REGISTRATION_AVAILABLE, SignalStore.paymentsValues().paymentsAvailability)
assertEquals(PaymentsAvailability.REGISTRATION_AVAILABLE, SignalStore.payments.paymentsAvailability)
}
@Test
@@ -99,7 +99,7 @@ class PaymentsValuesTest {
every { RemoteConfig.payments } returns true
every { RemoteConfig.paymentsCountryBlocklist } returns ""
assertEquals(PaymentsAvailability.WITHDRAW_AND_SEND, SignalStore.paymentsValues().paymentsAvailability)
assertEquals(PaymentsAvailability.WITHDRAW_AND_SEND, SignalStore.payments.paymentsAvailability)
}
@Test
@@ -115,7 +115,7 @@ class PaymentsValuesTest {
every { RemoteConfig.payments } returns true
every { RemoteConfig.paymentsCountryBlocklist } returns "1"
assertEquals(PaymentsAvailability.NOT_IN_REGION, SignalStore.paymentsValues().paymentsAvailability)
assertEquals(PaymentsAvailability.NOT_IN_REGION, SignalStore.payments.paymentsAvailability)
}
@Test
@@ -131,7 +131,7 @@ class PaymentsValuesTest {
every { RemoteConfig.payments } returns true
every { RemoteConfig.paymentsCountryBlocklist } returns "1"
assertEquals(PaymentsAvailability.WITHDRAW_ONLY, SignalStore.paymentsValues().paymentsAvailability)
assertEquals(PaymentsAvailability.WITHDRAW_ONLY, SignalStore.payments.paymentsAvailability)
}
/**
@@ -145,6 +145,6 @@ class PaymentsValuesTest {
}
)
)
SignalStore.inject(store)
SignalStore.testInject(store)
}
}

View File

@@ -1,11 +1,11 @@
package org.thoughtcrime.securesms.recipients
import android.graphics.Color
import io.mockk.every
import io.mockk.mockk
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.mockito.Mockito.mock
import org.mockito.Mockito.`when`
import org.thoughtcrime.securesms.conversation.colors.ChatColors
import org.thoughtcrime.securesms.conversation.colors.ChatColorsPalette
import org.thoughtcrime.securesms.database.RecipientDatabaseTestUtils.createRecipient
@@ -26,14 +26,17 @@ class Recipient_getChatColorsTest : BaseRecipientTest() {
@Before
fun setUp() {
wallpaperValues = mock(WallpaperValues::class.java)
chatColorsValues = mock(ChatColorsValues::class.java)
wallpaperValues = mockk<WallpaperValues>()
chatColorsValues = mockk<ChatColorsValues>()
val globalWallpaper = createWallpaper(globalWallpaperChatColor)
`when`(wallpaperValues.wallpaper).thenReturn(globalWallpaper)
`when`(chatColorsValues.chatColors).thenReturn(globalChatColor)
`when`(SignalStore.wallpaper()).thenReturn(wallpaperValues)
`when`(SignalStore.chatColorsValues()).thenReturn(chatColorsValues)
every { wallpaperValues.wallpaper } answers { globalWallpaper }
every { chatColorsValues.chatColors } answers { globalChatColor }
val mockStore = mockk<SignalStore>()
SignalStore.testInject(mockStore)
every { SignalStore.wallpaper } returns wallpaperValues
every { SignalStore.chatColors } returns chatColorsValues
}
@Test
@@ -93,7 +96,7 @@ class Recipient_getChatColorsTest : BaseRecipientTest() {
@Test
fun `Given recipient has auto chat color set and no wallpaper set and no global wallpaper set, when I getChatColors, then I expect the default chat color`() {
// GIVEN
`when`(wallpaperValues.wallpaper).thenReturn(null)
every { wallpaperValues.wallpaper } answers { null }
val auto = ChatColors.forColor(ChatColors.Id.Auto, Color.BLACK)
val recipient = createRecipient(chatColors = auto)
@@ -108,7 +111,7 @@ class Recipient_getChatColorsTest : BaseRecipientTest() {
fun `Given recipient has no chat color set and there is a custom global chat color, when I getChatColors, then I expect the global chat color`() {
// GIVEN
val expected = globalChatColor.withId(ChatColors.Id.Custom(12))
`when`(chatColorsValues.chatColors).thenReturn(expected)
every { chatColorsValues.chatColors } answers { expected }
val recipient = createRecipient()
// WHEN
@@ -133,7 +136,7 @@ class Recipient_getChatColorsTest : BaseRecipientTest() {
@Test
fun `Given recipient has no chat color set and there is an auto global chat color and the recipient has a wallpaper, when I getChatColors, then I expect the wallpaper chat color`() {
// GIVEN
`when`(chatColorsValues.chatColors).thenReturn(globalChatColor.withId(ChatColors.Id.Auto))
every { chatColorsValues.chatColors } answers { globalChatColor.withId(ChatColors.Id.Auto) }
val color = ChatColors.forColor(ChatColors.Id.BuiltIn, Color.CYAN)
val recipient = createRecipient(wallpaper = createWallpaper(color))
@@ -147,7 +150,7 @@ class Recipient_getChatColorsTest : BaseRecipientTest() {
@Test
fun `Given recipient has no chat color set and there is no global chat color and the recipient has a wallpaper, when I getChatColors, then I expect the wallpaper chat color`() {
// GIVEN
`when`(chatColorsValues.chatColors).thenReturn(null)
every { chatColorsValues.chatColors } answers { null }
val color = ChatColors.forColor(ChatColors.Id.BuiltIn, Color.CYAN)
val recipient = createRecipient(wallpaper = createWallpaper(color))
@@ -161,7 +164,7 @@ class Recipient_getChatColorsTest : BaseRecipientTest() {
@Test
fun `Given recipient has no chat color set and there is an auto global chat color and the recipient has no wallpaper and global wallpaper set, when I getChatColors, then I expect the global wallpaper chat color`() {
// GIVEN
`when`(chatColorsValues.chatColors).thenReturn(globalChatColor.withId(ChatColors.Id.Auto))
every { chatColorsValues.chatColors } answers { globalChatColor.withId(ChatColors.Id.Auto) }
val recipient = createRecipient()
// WHEN
@@ -174,7 +177,7 @@ class Recipient_getChatColorsTest : BaseRecipientTest() {
@Test
fun `Given recipient has no chat color set and there is no global chat color and the recipient has no wallpaper and global wallpaper set, when I getChatColors, then I expect the global wallpaper chat color`() {
// GIVEN
`when`(chatColorsValues.chatColors).thenReturn(null)
every { chatColorsValues.chatColors } answers { null }
val recipient = createRecipient()
// WHEN
@@ -187,8 +190,8 @@ class Recipient_getChatColorsTest : BaseRecipientTest() {
@Test
fun `Given no recipient colors and auto global colors and no wallpaper set, when I getChatColors, then I expect default blue`() {
// GIVEN
`when`(wallpaperValues.wallpaper).thenReturn(null)
`when`(chatColorsValues.chatColors).thenReturn(globalChatColor.withId(ChatColors.Id.Auto))
every { wallpaperValues.wallpaper } answers { null }
every { chatColorsValues.chatColors } answers { globalChatColor.withId(ChatColors.Id.Auto) }
val recipient = createRecipient()
// WHEN
@@ -201,8 +204,8 @@ class Recipient_getChatColorsTest : BaseRecipientTest() {
@Test
fun `Given no colors or wallpaper set, when I getChatColors, then I expect default blue`() {
// GIVEN
`when`(wallpaperValues.wallpaper).thenReturn(null)
`when`(chatColorsValues.chatColors).thenReturn(null)
every { wallpaperValues.wallpaper } answers { null }
every { chatColorsValues.chatColors } answers { null }
val recipient = createRecipient()
// WHEN
@@ -213,8 +216,10 @@ class Recipient_getChatColorsTest : BaseRecipientTest() {
}
private fun createWallpaper(
chatColors: ChatColors?
): ChatWallpaper = mock(ChatWallpaper::class.java).apply {
`when`(autoChatColors).thenReturn(chatColors)
chatColors: ChatColors
): ChatWallpaper {
return mockk<ChatWallpaper>().apply {
every { autoChatColors } answers { chatColors }
}
}
}

View File

@@ -1,5 +1,7 @@
package org.thoughtcrime.securesms.storage
import android.app.Application
import androidx.test.core.app.ApplicationProvider
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
@@ -7,6 +9,7 @@ import org.junit.Before
import org.junit.BeforeClass
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.MockedStatic
import org.mockito.Mockito
@@ -15,9 +18,16 @@ import org.mockito.internal.configuration.plugins.Plugins
import org.mockito.internal.junit.JUnitRule
import org.mockito.junit.MockitoRule
import org.mockito.quality.Strictness
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import org.signal.core.util.logging.Log
import org.thoughtcrime.securesms.database.RecipientTable
import org.thoughtcrime.securesms.dependencies.AppDependencies
import org.thoughtcrime.securesms.dependencies.MockApplicationDependencyProvider
import org.thoughtcrime.securesms.keyvalue.AccountValues
import org.thoughtcrime.securesms.keyvalue.KeyValueDataSet
import org.thoughtcrime.securesms.keyvalue.KeyValueStore
import org.thoughtcrime.securesms.keyvalue.MockKeyValuePersistentStorage
import org.thoughtcrime.securesms.keyvalue.SignalStore
import org.thoughtcrime.securesms.testutil.EmptyLogger
import org.thoughtcrime.securesms.util.RemoteConfig
@@ -28,6 +38,8 @@ import org.whispersystems.signalservice.api.storage.StorageId
import org.whispersystems.signalservice.internal.storage.protos.ContactRecord
import java.util.UUID
@RunWith(RobolectricTestRunner::class)
@Config(application = Application::class)
class ContactRecordProcessorTest {
@Rule
@@ -40,14 +52,14 @@ class ContactRecordProcessorTest {
@Mock
lateinit var remoteConfig: MockedStatic<RemoteConfig>
@Mock
lateinit var signalStore: MockedStatic<SignalStore>
@Before
fun setup() {
val mockAccountValues = mock(AccountValues::class.java)
Mockito.lenient().`when`(mockAccountValues.isPrimaryDevice).thenReturn(true)
signalStore.`when`<AccountValues> { SignalStore.account() }.thenReturn(mockAccountValues)
if (!AppDependencies.isInitialized) {
AppDependencies.init(ApplicationProvider.getApplicationContext(), MockApplicationDependencyProvider())
}
SignalStore.testInject(KeyValueStore(MockKeyValuePersistentStorage.withDataSet(KeyValueDataSet())))
}
@Test