|
| 1 | +package com.woocommerce.android.ui.woopos.common.util |
| 2 | + |
| 3 | +import com.woocommerce.android.AppPrefsWrapper |
| 4 | +import com.woocommerce.android.notifications.local.LocalNotification |
| 5 | +import com.woocommerce.android.notifications.local.LocalNotificationScheduler |
| 6 | +import com.woocommerce.android.tools.SelectedSite |
| 7 | +import com.woocommerce.android.ui.woopos.util.WooPosCoroutineTestRule |
| 8 | +import com.woocommerce.android.ui.woopos.util.datastore.WooPosPreferencesRepository |
| 9 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 10 | +import kotlinx.coroutines.flow.flowOf |
| 11 | +import kotlinx.coroutines.test.runTest |
| 12 | +import org.junit.Before |
| 13 | +import org.junit.Rule |
| 14 | +import org.junit.Test |
| 15 | +import org.mockito.kotlin.any |
| 16 | +import org.mockito.kotlin.mock |
| 17 | +import org.mockito.kotlin.never |
| 18 | +import org.mockito.kotlin.verify |
| 19 | +import org.mockito.kotlin.whenever |
| 20 | +import org.wordpress.android.fluxc.model.LocalOrRemoteId.LocalId |
| 21 | +import org.wordpress.android.fluxc.model.SiteModel |
| 22 | +import org.wordpress.android.fluxc.store.WooCommerceStore |
| 23 | +import org.wordpress.android.fluxc.wc.settings.WCSettingsTestUtils |
| 24 | + |
| 25 | +@ExperimentalCoroutinesApi |
| 26 | +class WooPosSurveysNotificationSchedularTest { |
| 27 | + @Rule |
| 28 | + @JvmField |
| 29 | + val coroutinesTestRule = WooPosCoroutineTestRule() |
| 30 | + |
| 31 | + private lateinit var localNotificationScheduler: LocalNotificationScheduler |
| 32 | + private lateinit var appPrefs: AppPrefsWrapper |
| 33 | + private lateinit var wooPosPreferencesRepository: WooPosPreferencesRepository |
| 34 | + private lateinit var selectedSite: SelectedSite |
| 35 | + private lateinit var wooCommerceStore: WooCommerceStore |
| 36 | + private lateinit var schedular: WooPosSurveysNotificationSchedular |
| 37 | + private lateinit var siteModel: SiteModel |
| 38 | + |
| 39 | + @Before |
| 40 | + fun setUp() { |
| 41 | + localNotificationScheduler = mock() |
| 42 | + appPrefs = mock() |
| 43 | + wooPosPreferencesRepository = mock() |
| 44 | + selectedSite = mock() |
| 45 | + wooCommerceStore = mock() |
| 46 | + siteModel = mock { |
| 47 | + on { siteId }.thenReturn(123L) |
| 48 | + } |
| 49 | + |
| 50 | + whenever(selectedSite.get()).thenReturn(siteModel) |
| 51 | + |
| 52 | + schedular = WooPosSurveysNotificationSchedular( |
| 53 | + localNotificationScheduler = localNotificationScheduler, |
| 54 | + appPrefs = appPrefs, |
| 55 | + wooPosPreferencesRepository = wooPosPreferencesRepository, |
| 56 | + selectedSite = selectedSite, |
| 57 | + wooCommerceStore = wooCommerceStore |
| 58 | + ) |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + fun `given all conditions met, when schedularPotentialUserSurveyNotification called, then notification scheduled`() = |
| 63 | + runTest { |
| 64 | + val siteSettings = WCSettingsTestUtils.generateSettings(LocalId(1)).copy(countryCode = "US") |
| 65 | + whenever(appPrefs.isWooPosSurveyNotificationPotentialUserShown).thenReturn(false) |
| 66 | + whenever(wooPosPreferencesRepository.wasOpenedOnce).thenReturn(flowOf(false)) |
| 67 | + whenever(wooCommerceStore.getSiteSettingsAsync(siteModel)).thenReturn(siteSettings) |
| 68 | + |
| 69 | + schedular.schedularPotentialUserSurveyNotification() |
| 70 | + |
| 71 | + verify(localNotificationScheduler).scheduleNotification( |
| 72 | + LocalNotification.WooPosSurveyPotentialUserNotification(siteId = 123L) |
| 73 | + ) |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + fun `given notification already shown, when schedularPotentialUserSurveyNotification called, then notification not scheduled`() = |
| 78 | + runTest { |
| 79 | + val siteSettings = WCSettingsTestUtils.generateSettings(LocalId(1)).copy(countryCode = "US") |
| 80 | + whenever(appPrefs.isWooPosSurveyNotificationPotentialUserShown).thenReturn(true) |
| 81 | + whenever(wooPosPreferencesRepository.wasOpenedOnce).thenReturn(flowOf(false)) |
| 82 | + whenever(wooCommerceStore.getSiteSettingsAsync(siteModel)).thenReturn(siteSettings) |
| 83 | + |
| 84 | + schedular.schedularPotentialUserSurveyNotification() |
| 85 | + |
| 86 | + verify(localNotificationScheduler, never()).scheduleNotification(any()) |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + fun `given country not allowed, when schedularPotentialUserSurveyNotification called, then notification not scheduled`() = |
| 91 | + runTest { |
| 92 | + val siteSettings = WCSettingsTestUtils.generateSettings(LocalId(1)).copy(countryCode = "FR") |
| 93 | + whenever(appPrefs.isWooPosSurveyNotificationPotentialUserShown).thenReturn(false) |
| 94 | + whenever(wooPosPreferencesRepository.wasOpenedOnce).thenReturn(flowOf(false)) |
| 95 | + whenever(wooCommerceStore.getSiteSettingsAsync(siteModel)).thenReturn(siteSettings) |
| 96 | + |
| 97 | + schedular.schedularPotentialUserSurveyNotification() |
| 98 | + |
| 99 | + verify(localNotificationScheduler, never()).scheduleNotification(any()) |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + fun `given WooPOS opened before, when schedularPotentialUserSurveyNotification called, then notification not scheduled`() = |
| 104 | + runTest { |
| 105 | + val siteSettings = WCSettingsTestUtils.generateSettings(LocalId(1)).copy(countryCode = "US") |
| 106 | + whenever(appPrefs.isWooPosSurveyNotificationPotentialUserShown).thenReturn(false) |
| 107 | + whenever(wooPosPreferencesRepository.wasOpenedOnce).thenReturn(flowOf(true)) |
| 108 | + whenever(wooCommerceStore.getSiteSettingsAsync(siteModel)).thenReturn(siteSettings) |
| 109 | + |
| 110 | + schedular.schedularPotentialUserSurveyNotification() |
| 111 | + |
| 112 | + verify(localNotificationScheduler, never()).scheduleNotification(any()) |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + fun `given GB country code, when schedularPotentialUserSurveyNotification called, then notification scheduled`() = |
| 117 | + runTest { |
| 118 | + val siteSettings = WCSettingsTestUtils.generateSettings(LocalId(1)).copy(countryCode = "GB") |
| 119 | + whenever(appPrefs.isWooPosSurveyNotificationPotentialUserShown).thenReturn(false) |
| 120 | + whenever(wooPosPreferencesRepository.wasOpenedOnce).thenReturn(flowOf(false)) |
| 121 | + whenever(wooCommerceStore.getSiteSettingsAsync(siteModel)).thenReturn(siteSettings) |
| 122 | + |
| 123 | + schedular.schedularPotentialUserSurveyNotification() |
| 124 | + |
| 125 | + verify(localNotificationScheduler).scheduleNotification( |
| 126 | + LocalNotification.WooPosSurveyPotentialUserNotification(siteId = 123L) |
| 127 | + ) |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + fun `given lowercase country code, when schedularPotentialUserSurveyNotification called, then notification scheduled`() = |
| 132 | + runTest { |
| 133 | + val siteSettings = WCSettingsTestUtils.generateSettings(LocalId(1)).copy(countryCode = "us") |
| 134 | + whenever(appPrefs.isWooPosSurveyNotificationPotentialUserShown).thenReturn(false) |
| 135 | + whenever(wooPosPreferencesRepository.wasOpenedOnce).thenReturn(flowOf(false)) |
| 136 | + whenever(wooCommerceStore.getSiteSettingsAsync(siteModel)).thenReturn(siteSettings) |
| 137 | + |
| 138 | + schedular.schedularPotentialUserSurveyNotification() |
| 139 | + |
| 140 | + verify(localNotificationScheduler).scheduleNotification( |
| 141 | + LocalNotification.WooPosSurveyPotentialUserNotification(siteId = 123L) |
| 142 | + ) |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + fun `given null country code, when schedularPotentialUserSurveyNotification called, then notification not scheduled`() = |
| 147 | + runTest { |
| 148 | + val siteSettings = WCSettingsTestUtils.generateSettings(LocalId(1)).copy(countryCode = "") |
| 149 | + whenever(appPrefs.isWooPosSurveyNotificationPotentialUserShown).thenReturn(false) |
| 150 | + whenever(wooPosPreferencesRepository.wasOpenedOnce).thenReturn(flowOf(false)) |
| 151 | + whenever(wooCommerceStore.getSiteSettingsAsync(siteModel)).thenReturn(siteSettings) |
| 152 | + |
| 153 | + schedular.schedularPotentialUserSurveyNotification() |
| 154 | + |
| 155 | + verify(localNotificationScheduler, never()).scheduleNotification(any()) |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + fun `given null site settings, when schedularPotentialUserSurveyNotification called, then notification not scheduled`() = |
| 160 | + runTest { |
| 161 | + whenever(appPrefs.isWooPosSurveyNotificationPotentialUserShown).thenReturn(false) |
| 162 | + whenever(wooPosPreferencesRepository.wasOpenedOnce).thenReturn(flowOf(false)) |
| 163 | + whenever(wooCommerceStore.getSiteSettingsAsync(siteModel)).thenReturn(null) |
| 164 | + |
| 165 | + schedular.schedularPotentialUserSurveyNotification() |
| 166 | + |
| 167 | + verify(localNotificationScheduler, never()).scheduleNotification(any()) |
| 168 | + } |
| 169 | +} |
0 commit comments