NotificationsViewModelTestStatusDisplayOptions.kt 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright 2023 Tusky Contributors
  3. *
  4. * This file is a part of Tusky.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it under the terms of the
  7. * GNU General Public License as published by the Free Software Foundation; either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
  11. * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  12. * Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with Tusky; if not,
  15. * see <http://www.gnu.org/licenses>.
  16. */
  17. package com.keylesspalace.tusky.components.notifications
  18. import app.cash.turbine.test
  19. import com.google.common.truth.Truth.assertThat
  20. import com.keylesspalace.tusky.appstore.PreferenceChangedEvent
  21. import com.keylesspalace.tusky.settings.PrefKeys
  22. import com.keylesspalace.tusky.util.CardViewMode
  23. import com.keylesspalace.tusky.util.StatusDisplayOptions
  24. import kotlinx.coroutines.ExperimentalCoroutinesApi
  25. import kotlinx.coroutines.test.runTest
  26. import org.junit.Test
  27. /**
  28. * Verify that [StatusDisplayOptions] are handled correctly.
  29. *
  30. * - Is the initial value taken from values in sharedPreferences and account?
  31. * - Does the make() function correctly use an updated preference?
  32. * - Is the correct update emitted when a relevant preference changes?
  33. */
  34. @OptIn(ExperimentalCoroutinesApi::class)
  35. class NotificationsViewModelTestStatusDisplayOptions : NotificationsViewModelTestBase() {
  36. private val defaultStatusDisplayOptions = StatusDisplayOptions(
  37. animateAvatars = false,
  38. mediaPreviewEnabled = true, // setting in NotificationsViewModelTestBase
  39. useAbsoluteTime = false,
  40. showBotOverlay = true,
  41. useBlurhash = true,
  42. cardViewMode = CardViewMode.NONE,
  43. confirmReblogs = true,
  44. confirmFavourites = false,
  45. hideStats = false,
  46. animateEmojis = false,
  47. showSensitiveMedia = true, // setting in NotificationsViewModelTestBase
  48. openSpoiler = true // setting in NotificationsViewModelTestBase
  49. )
  50. @Test
  51. fun `initial settings are from sharedPreferences and activeAccount`() = runTest {
  52. viewModel.statusDisplayOptions.test {
  53. val item = awaitItem()
  54. assertThat(item).isEqualTo(defaultStatusDisplayOptions)
  55. }
  56. }
  57. @Test
  58. fun `make() uses updated preference`() = runTest {
  59. // Prior, should be false
  60. assertThat(defaultStatusDisplayOptions.animateAvatars).isFalse()
  61. // Given; just a change to one preferences
  62. sharedPreferencesMap[PrefKeys.ANIMATE_GIF_AVATARS] = true
  63. // When
  64. val updatedOptions = defaultStatusDisplayOptions.make(
  65. sharedPreferences,
  66. PrefKeys.ANIMATE_GIF_AVATARS,
  67. accountManager.activeAccount!!
  68. )
  69. // Then, should be true
  70. assertThat(updatedOptions.animateAvatars).isTrue()
  71. }
  72. @Test
  73. fun `PreferenceChangedEvent emits new StatusDisplayOptions`() = runTest {
  74. // Prior, should be false
  75. viewModel.statusDisplayOptions.test {
  76. val item = expectMostRecentItem()
  77. assertThat(item.animateAvatars).isFalse()
  78. }
  79. // Given
  80. sharedPreferencesMap[PrefKeys.ANIMATE_GIF_AVATARS] = true
  81. // When
  82. eventHub.dispatch(PreferenceChangedEvent(PrefKeys.ANIMATE_GIF_AVATARS))
  83. // Then, should be true
  84. viewModel.statusDisplayOptions.test {
  85. val item = expectMostRecentItem()
  86. assertThat(item.animateAvatars).isTrue()
  87. }
  88. }
  89. }