Skip to content

StreamVault is a TV-first free IPTV player for Android TV built with Kotlin, Jetpack Compose, Room, Hilt, and Media3.

test(system): lock existing presentation behavior

David Nashashb189b97747a4
diff --git a/app/src/test/java/com/streamvault/app/ui/screens/downloads/DownloadsViewModelTest.kt b/app/src/test/java/com/streamvault/app/ui/screens/downloads/DownloadsViewModelTest.kt
new file mode 100644
index 00000000..b539f6e8
--- /dev/null
+++ b/app/src/test/java/com/streamvault/app/ui/screens/downloads/DownloadsViewModelTest.kt
@@ -0,0 +1,137 @@
+package com.streamvault.app.ui.screens.downloads
+
+import android.content.Context
+import android.os.Build
+import com.google.common.truth.Truth.assertThat
+import com.streamvault.app.R
+import com.streamvault.domain.model.DownloadContentType
+import com.streamvault.domain.model.DownloadItem
+import com.streamvault.domain.model.DownloadRequest
+import com.streamvault.domain.model.DownloadStorageConfig
+import com.streamvault.domain.model.Result
+import com.streamvault.domain.repository.DownloadManager
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.ExperimentalCoroutinesApi
+import kotlinx.coroutines.flow.Flow
+import kotlinx.coroutines.flow.MutableStateFlow
+import kotlinx.coroutines.flow.flowOf
+import kotlinx.coroutines.test.StandardTestDispatcher
+import kotlinx.coroutines.test.advanceUntilIdle
+import kotlinx.coroutines.test.resetMain
+import kotlinx.coroutines.test.runTest
+import kotlinx.coroutines.test.setMain
+import org.junit.After
+import org.junit.Before
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.robolectric.RobolectricTestRunner
+import org.robolectric.annotation.Config
+import org.mockito.kotlin.mock
+import org.mockito.kotlin.whenever
+
+@RunWith(RobolectricTestRunner::class)
+@Config(sdk = [Build.VERSION_CODES.P])
+@OptIn(ExperimentalCoroutinesApi::class)
+class DownloadsViewModelTest {
+ private val dispatcher = StandardTestDispatcher()
+
+ @Before
+ fun setUp() {
+ Dispatchers.setMain(dispatcher)
+ }
+
+ @After
+ fun tearDown() {
+ Dispatchers.resetMain()
+ }
+
+ @Test
+ fun confirmDeleteClearsDialogAndDeletesSelectedItem() = runTest {
+ val item = downloadFixture("download-7")
+ val manager = RecordingDownloadManager()
+ val viewModel = createViewModel(manager)
+
+ viewModel.showDeleteConfirm(item)
+ viewModel.confirmDelete()
+ advanceUntilIdle()
+
+ assertThat(viewModel.uiState.value.deleteConfirmItem).isNull()
+ assertThat(manager.deletedIds).containsExactly("download-7")
+ }
+
+ @Test
+ fun missingOutputUriReturnsNullWithoutStoppingPlayback() {
+ val manager = RecordingDownloadManager()
+ val viewModel = createViewModel(manager)
+
+ val result = viewModel.playDownload(downloadFixture("missing-output"))
+
+ assertThat(result).isNull()
+ assertThat(manager.playbackStoppedCount).isEqualTo(0)
+ }
+
+ @Test
+ fun changeDownloadFolderCreatesDocumentTreeIntent() {
+ val viewModel = createViewModel(RecordingDownloadManager())
+
+ assertThat(viewModel.changeDownloadFolder().action)
+ .isEqualTo("android.intent.action.OPEN_DOCUMENT_TREE")
+ }
+
+ private fun createViewModel(manager: RecordingDownloadManager): DownloadsViewModel {
+ val application = mock<Context>()
+ whenever(application.getString(R.string.downloads_deleted)).thenReturn("Downloads deleted")
+ whenever(application.getString(R.string.downloads_resumed)).thenReturn("Downloads resumed")
+ return DownloadsViewModel(
+ downloadManager = manager,
+ application = application
+ )
+ }
+
+ private fun downloadFixture(id: String) = DownloadItem(
+ id = id,
+ providerId = 7L,
+ contentType = DownloadContentType.MOVIE,
+ contentId = 42L,
+ contentName = "Test movie",
+ streamUrl = "https://example.test/movie.mp4"
+ )
+
+ private class RecordingDownloadManager : DownloadManager {
+ private val downloads = MutableStateFlow<List<DownloadItem>>(emptyList())
+ private val storage = MutableStateFlow(DownloadStorageConfig())
+ val deletedIds = mutableListOf<String>()
+ var playbackStoppedCount = 0
+
+ override fun observeAllDownloads(): Flow<List<DownloadItem>> = downloads
+
+ override fun observeDownload(id: String): Flow<DownloadItem?> = flowOf(null)
+
+ override fun observeStorageState(): Flow<DownloadStorageConfig> = storage
+
+ override suspend fun enqueueDownload(request: DownloadRequest): Result<DownloadItem> =
+ Result.error("not used")
+
+ override suspend fun resumeDownload(id: String): Result<Unit> = Result.Success(Unit)
+
+ override suspend fun recoverInterruptedDownloads(): Result<Int> = Result.Success(0)
+
+ override suspend fun cancelDownload(id: String): Result<Unit> = Result.Success(Unit)
+
+ override fun onPlaybackStarted() = Unit
+
+ override fun onPlaybackStopped() {
+ playbackStoppedCount++
+ }
+
+ override suspend fun deleteDownload(id: String): Result<Unit> {
+ deletedIds += id
+ return Result.Success(Unit)
+ }
+
+ override suspend fun updateStorageConfig(
+ treeUri: String?,
+ displayName: String?
+ ): Result<DownloadStorageConfig> = Result.Success(storage.value)
+ }
+}
diff --git a/app/src/test/java/com/streamvault/app/ui/screens/plugins/PluginConfigurationDraftTest.kt b/app/src/test/java/com/streamvault/app/ui/screens/plugins/PluginConfigurationDraftTest.kt
new file mode 100644
index 00000000..64207a06
--- /dev/null
+++ b/app/src/test/java/com/streamvault/app/ui/screens/plugins/PluginConfigurationDraftTest.kt
@@ -0,0 +1,157 @@
+package com.streamvault.app.ui.screens.plugins
+
+import com.google.common.truth.Truth.assertThat
+import com.streamvault.app.plugins.InstalledStreamVaultPlugin
+import com.streamvault.app.plugins.PluginActionResult
+import com.streamvault.app.plugins.PluginConfigurationField
+import com.streamvault.app.plugins.PluginConfigurationSchema
+import com.streamvault.app.plugins.PluginConfigurationSection
+import com.streamvault.app.plugins.PluginConfigurationSnapshot
+import com.streamvault.app.plugins.StreamVaultPluginContract
+import com.streamvault.app.plugins.StreamVaultPluginManager
+import com.streamvault.app.plugins.StreamVaultPluginManifest
+import com.streamvault.domain.model.Result
+import com.streamvault.domain.provider.ProviderSourceRegistry
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.ExperimentalCoroutinesApi
+import kotlinx.coroutines.test.StandardTestDispatcher
+import kotlinx.coroutines.test.advanceUntilIdle
+import kotlinx.coroutines.test.resetMain
+import kotlinx.coroutines.test.runTest
+import kotlinx.coroutines.test.setMain
+import kotlinx.serialization.json.JsonObject
+import kotlinx.serialization.json.buildJsonObject
+import kotlinx.serialization.json.put
+import org.junit.After
+import org.junit.Before
+import org.junit.Test
+import org.mockito.kotlin.any
+import org.mockito.kotlin.eq
+import org.mockito.kotlin.mock
+import org.mockito.kotlin.wheneverBlocking
+
+@OptIn(ExperimentalCoroutinesApi::class)
+class PluginConfigurationDraftTest {
+ private val dispatcher = StandardTestDispatcher()
+
+ @Before
+ fun setUp() {
+ Dispatchers.setMain(dispatcher)
+ }
+
+ @After
+ fun tearDown() {
+ Dispatchers.resetMain()
+ }
+
+ @Test
+ fun requiredBlankValueIsRejectedBeforePluginSave() = runTest {
+ val plugin = pluginFixture()
+ val manager = mock<StreamVaultPluginManager>()
+ val registry = mock<ProviderSourceRegistry>()
+ val snapshot = configurationSnapshot(plugin)
+ wheneverBlocking { manager.discoverPlugins() }.thenReturn(listOf(plugin))
+ wheneverBlocking { registry.sources() }.thenReturn(emptyList())
+ wheneverBlocking { manager.loadPluginConfiguration(plugin) }.thenReturn(Result.Success(snapshot))
+
+ val viewModel = PluginsViewModel(manager, registry)
+ advanceUntilIdle()
+ viewModel.openPluginConfiguration(plugin)
+ advanceUntilIdle()
+ viewModel.updateConfigurationValue("server", "")
+ viewModel.savePluginConfiguration()
+
+ assertThat(viewModel.uiState.value.configuration?.validationErrors)
+ .containsEntry("server", "Server is required")
+ assertThat(viewModel.uiState.value.userMessage)
+ .isEqualTo("Review the highlighted plugin settings")
+ }
+
+ @Test
+ fun validDraftSerializesTypedFieldsAndRefreshesSavedValues() = runTest {
+ val plugin = pluginFixture()
+ val manager = mock<StreamVaultPluginManager>()
+ val registry = mock<ProviderSourceRegistry>()
+ val snapshot = configurationSnapshot(plugin)
+ val updatedValues = buildJsonObject {
+ put("server", "https://updated.example")
+ put("port", 9090)
+ put("enabled", true)
+ }
+ wheneverBlocking { manager.discoverPlugins() }.thenReturn(listOf(plugin))
+ wheneverBlocking { registry.sources() }.thenReturn(emptyList())
+ wheneverBlocking { manager.loadPluginConfiguration(plugin) }.thenReturn(Result.Success(snapshot))
+ wheneverBlocking { manager.savePluginConfiguration(eq(plugin), any()) }
+ .thenReturn(PluginActionResult(true, "Plugin settings saved"))
+ wheneverBlocking { manager.loadPluginConfigurationValues(plugin) }
+ .thenReturn(Result.Success(updatedValues))
+
+ val viewModel = PluginsViewModel(manager, registry)
+ advanceUntilIdle()
+ viewModel.openPluginConfiguration(plugin)
+ advanceUntilIdle()
+ viewModel.updateConfigurationValue("server", "https://updated.example")
+ viewModel.updateConfigurationValue("port", "9090")
+ viewModel.updateConfigurationValue("enabled", "true")
+ viewModel.savePluginConfiguration()
+ advanceUntilIdle()
+
+ assertThat(viewModel.uiState.value.configuration?.draftValues)
+ .containsAtLeast(
+ "server", "https://updated.example",
+ "port", "9090",
+ "enabled", "true"
+ )
+ assertThat(viewModel.uiState.value.userMessage).isEqualTo("Plugin settings saved")
+ }
+
+ private fun configurationSnapshot(plugin: InstalledStreamVaultPlugin) =
+ PluginConfigurationSnapshot(
+ plugin = plugin,
+ schema = PluginConfigurationSchema(
+ sections = listOf(
+ PluginConfigurationSection(
+ id = "connection",
+ title = "Connection",
+ fields = listOf(
+ PluginConfigurationField(
+ key = "server",
+ label = "Server",
+ type = PluginConfigurationField.TYPE_URL,
+ required = true
+ ),
+ PluginConfigurationField(
+ key = "port",
+ label = "Port",
+ type = PluginConfigurationField.TYPE_NUMBER,
+ required = true
+ ),
+ PluginConfigurationField(
+ key = "enabled",
+ label = "Enabled",
+ type = PluginConfigurationField.TYPE_BOOLEAN
+ )
+ )
+ )
+ )
+ ),
+ values = buildJsonObject {
+ put("server", "https://initial.example")
+ put("port", 8080)
+ put("enabled", false)
+ }
+ )
+
+ private fun pluginFixture() = InstalledStreamVaultPlugin(
+ packageName = "com.example.plugin",
+ serviceClassName = "com.example.PluginService",
+ appLabel = "Example plugin",
+ manifest = StreamVaultPluginManifest(
+ id = "example",
+ name = "Example plugin",
+ capabilities = listOf(StreamVaultPluginContract.CAPABILITY_CONFIGURATION_SCHEMA),
+ configurationMode = StreamVaultPluginContract.CONFIGURATION_MODE_HOST_SCHEMA
+ ),
+ enabled = false
+ )
+}
diff --git a/app/src/test/java/com/streamvault/app/ui/screens/plugins/PluginsViewModelTest.kt b/app/src/test/java/com/streamvault/app/ui/screens/plugins/PluginsViewModelTest.kt
new file mode 100644
index 00000000..15f63910
--- /dev/null
+++ b/app/src/test/java/com/streamvault/app/ui/screens/plugins/PluginsViewModelTest.kt
@@ -0,0 +1,100 @@
+package com.streamvault.app.ui.screens.plugins
+
+import com.google.common.truth.Truth.assertThat
+import com.streamvault.app.plugins.InstalledStreamVaultPlugin
+import com.streamvault.app.plugins.StreamVaultPluginContract
+import com.streamvault.app.plugins.StreamVaultPluginManager
+import com.streamvault.app.plugins.StreamVaultPluginManifest
+import com.streamvault.domain.provider.ProviderSourceRegistry
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.ExperimentalCoroutinesApi
+import kotlinx.coroutines.test.StandardTestDispatcher
+import kotlinx.coroutines.test.advanceUntilIdle
+import kotlinx.coroutines.test.resetMain
+import kotlinx.coroutines.test.runTest
+import kotlinx.coroutines.test.setMain
+import org.junit.After
+import org.junit.Before
+import org.junit.Test
+import org.mockito.kotlin.mock
+import org.mockito.kotlin.verifyBlocking
+import org.mockito.kotlin.wheneverBlocking
+
+@OptIn(ExperimentalCoroutinesApi::class)
+class PluginsViewModelTest {
+ private val dispatcher = StandardTestDispatcher()
+
+ @Before
+ fun setUp() {
+ Dispatchers.setMain(dispatcher)
+ }
+
+ @After
+ fun tearDown() {
+ Dispatchers.resetMain()
+ }
+
+ @Test
+ fun refreshPublishesDiscoveredPluginsAndProviderSources() = runTest {
+ val plugin = pluginFixture()
+ val manager = mock<StreamVaultPluginManager>()
+ val registry = mock<ProviderSourceRegistry>()
+ wheneverBlocking { manager.discoverPlugins() }.thenReturn(listOf(plugin))
+ wheneverBlocking { registry.sources() }.thenReturn(emptyList())
+
+ val viewModel = PluginsViewModel(manager, registry)
+ advanceUntilIdle()
+
+ assertThat(viewModel.uiState.value.isLoading).isFalse()
+ assertThat(viewModel.uiState.value.plugins).containsExactly(plugin)
+ assertThat(viewModel.uiState.value.providerSources).isEmpty()
+ }
+
+ @Test
+ fun blankInstallUrlPublishesExistingValidationMessage() = runTest {
+ val manager = mock<StreamVaultPluginManager>()
+ val registry = mock<ProviderSourceRegistry>()
+ wheneverBlocking { manager.discoverPlugins() }.thenReturn(emptyList())
+ wheneverBlocking { registry.sources() }.thenReturn(emptyList())
+
+ val viewModel = PluginsViewModel(manager, registry)
+ advanceUntilIdle()
+
+ viewModel.updateInstallUrl(" ")
+ viewModel.installFromUrl()
+
+ assertThat(viewModel.uiState.value.userMessage)
+ .isEqualTo("Enter a plugin APK URL first")
+ }
+
+ @Test
+ fun localInstallDelegatesUriAndRefreshesState() = runTest {
+ val uri = mock<android.net.Uri>()
+ val manager = mock<StreamVaultPluginManager>()
+ val registry = mock<ProviderSourceRegistry>()
+ wheneverBlocking { manager.discoverPlugins() }.thenReturn(emptyList())
+ wheneverBlocking { registry.sources() }.thenReturn(emptyList())
+ wheneverBlocking { manager.installApkFromUri(uri) }
+ .thenReturn(com.streamvault.domain.model.Result.Success(Unit))
+
+ val viewModel = PluginsViewModel(manager, registry)
+ advanceUntilIdle()
+ viewModel.installFromLocalUri(uri)
+ advanceUntilIdle()
+
+ verifyBlocking(manager) { installApkFromUri(uri) }
+ assertThat(viewModel.uiState.value.isLoading).isFalse()
+ }
+
+ private fun pluginFixture() = InstalledStreamVaultPlugin(
+ packageName = "com.example.plugin",
+ serviceClassName = "com.example.PluginService",
+ appLabel = "Example plugin",
+ manifest = StreamVaultPluginManifest(
+ id = "example",
+ name = "Example plugin",
+ capabilities = listOf(StreamVaultPluginContract.CAPABILITY_CONFIGURATION_SCHEMA)
+ ),
+ enabled = false
+ )
+}
diff --git a/app/src/test/java/com/streamvault/app/ui/screens/welcome/WelcomeViewModelTest.kt b/app/src/test/java/com/streamvault/app/ui/screens/welcome/WelcomeViewModelTest.kt
new file mode 100644
index 00000000..4aee8d16
--- /dev/null
+++ b/app/src/test/java/com/streamvault/app/ui/screens/welcome/WelcomeViewModelTest.kt
@@ -0,0 +1,88 @@
+package com.streamvault.app.ui.screens.welcome
+
+import com.google.common.truth.Truth.assertThat
+import com.streamvault.data.sync.SyncProgressBus
+import com.streamvault.domain.model.LegacyProvider
+import com.streamvault.domain.model.ProviderType
+import com.streamvault.domain.repository.ProviderRepository
+import com.streamvault.domain.usecase.ValidateAndAddProvider
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.ExperimentalCoroutinesApi
+import kotlinx.coroutines.flow.MutableStateFlow
+import kotlinx.coroutines.test.StandardTestDispatcher
+import kotlinx.coroutines.test.advanceUntilIdle
+import kotlinx.coroutines.test.resetMain
+import kotlinx.coroutines.test.runTest
+import kotlinx.coroutines.test.setMain
+import org.junit.After
+import org.junit.Before
+import org.junit.Test
+import org.mockito.kotlin.any
+import org.mockito.kotlin.mock
+import org.mockito.kotlin.never
+import org.mockito.kotlin.verify
+import org.mockito.kotlin.verifyBlocking
+import org.mockito.kotlin.whenever
+import org.mockito.kotlin.anyOrNull
+
+@OptIn(ExperimentalCoroutinesApi::class)
+class WelcomeViewModelTest {
+ private val dispatcher = StandardTestDispatcher()
+
+ @Before
+ fun setUp() {
+ Dispatchers.setMain(dispatcher)
+ }
+
+ @After
+ fun tearDown() {
+ Dispatchers.resetMain()
+ }
+
+ @Test
+ fun providerObservationPublishesPresenceAndStopsWelcomeProgress() = runTest {
+ val providers = MutableStateFlow<List<LegacyProvider>>(emptyList())
+ val repository = mock<ProviderRepository>()
+ whenever(repository.getProviders()).thenReturn(providers)
+ val validateAndAddProvider = mock<ValidateAndAddProvider>()
+
+ val viewModel = WelcomeViewModel(
+ providerRepository = repository,
+ validateAndAddProvider = validateAndAddProvider,
+ syncProgressBus = SyncProgressBus()
+ )
+
+ advanceUntilIdle()
+ assertThat(viewModel.hasProviders.value).isFalse()
+
+ providers.value = listOf(providerFixture())
+ advanceUntilIdle()
+
+ assertThat(viewModel.hasProviders.value).isTrue()
+ assertThat(viewModel.syncProgress.value).isNull()
+ }
+
+ @Test
+ fun existingProviderPreventsDevelopmentSeeding() = runTest {
+ val repository = mock<ProviderRepository>()
+ whenever(repository.getProviders()).thenReturn(MutableStateFlow(listOf(providerFixture())))
+ val validateAndAddProvider = mock<ValidateAndAddProvider>()
+
+ WelcomeViewModel(
+ providerRepository = repository,
+ validateAndAddProvider = validateAndAddProvider,
+ syncProgressBus = SyncProgressBus()
+ )
+ advanceUntilIdle()
+
+ verifyBlocking(validateAndAddProvider, never()) { loginXtream(any(), anyOrNull()) }
+ verifyBlocking(validateAndAddProvider, never()) { addM3u(any(), anyOrNull()) }
+ }
+
+ private fun providerFixture() = LegacyProvider(
+ id = 7L,
+ name = "Test provider",
+ type = ProviderType.M3U,
+ serverUrl = "https://example.test/playlist.m3u"
+ )
+}