|
| 1 | +package au.com.shiftyjelly.pocketcasts.discover.util |
| 2 | + |
| 3 | +import android.content.res.Resources |
| 4 | +import au.com.shiftyjelly.pocketcasts.preferences.Settings |
| 5 | +import au.com.shiftyjelly.pocketcasts.preferences.UserSetting |
| 6 | +import au.com.shiftyjelly.pocketcasts.servers.model.Discover |
| 7 | +import au.com.shiftyjelly.pocketcasts.servers.model.DiscoverRegion |
| 8 | +import au.com.shiftyjelly.pocketcasts.servers.model.DiscoverRow |
| 9 | +import au.com.shiftyjelly.pocketcasts.servers.model.DisplayStyle |
| 10 | +import au.com.shiftyjelly.pocketcasts.servers.model.ExpandedStyle |
| 11 | +import au.com.shiftyjelly.pocketcasts.servers.model.ListType |
| 12 | +import au.com.shiftyjelly.pocketcasts.servers.model.SponsoredPodcast |
| 13 | +import au.com.shiftyjelly.pocketcasts.servers.server.ListRepository |
| 14 | +import au.com.shiftyjelly.pocketcasts.sharedtest.MainCoroutineRule |
| 15 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 16 | +import kotlinx.coroutines.flow.MutableStateFlow |
| 17 | +import kotlinx.coroutines.test.runTest |
| 18 | +import org.junit.Before |
| 19 | +import org.junit.Rule |
| 20 | +import org.junit.Test |
| 21 | +import org.mockito.Mock |
| 22 | +import org.mockito.MockitoAnnotations |
| 23 | +import org.mockito.kotlin.any |
| 24 | +import org.mockito.kotlin.mock |
| 25 | +import org.mockito.kotlin.whenever |
| 26 | + |
| 27 | +@ExperimentalCoroutinesApi |
| 28 | +class DiscoverDeepLinkManagerTest { |
| 29 | + |
| 30 | + @get:Rule |
| 31 | + val coroutineRule = MainCoroutineRule() |
| 32 | + |
| 33 | + @Mock |
| 34 | + private lateinit var mockRepository: ListRepository |
| 35 | + |
| 36 | + @Mock |
| 37 | + private lateinit var mockSettings: Settings |
| 38 | + |
| 39 | + @Mock |
| 40 | + private lateinit var mockResources: Resources |
| 41 | + |
| 42 | + private lateinit var manager: DiscoverDeepLinkManager |
| 43 | + |
| 44 | + @Before |
| 45 | + fun setUp() { |
| 46 | + MockitoAnnotations.openMocks(this) |
| 47 | + manager = DiscoverDeepLinkManager(mockRepository, mockSettings) |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + fun `loadStaffPicks should return transformed staff picks row`() = runTest { |
| 52 | + val staffPicksRow = createDiscoverRow( |
| 53 | + listUuid = "staff-picks", |
| 54 | + title = "Popular in {region_name}", |
| 55 | + source = "source_{region_code}", |
| 56 | + expandedTopItemLabel = "Top in {region_name}", |
| 57 | + ) |
| 58 | + val discover = createTestDiscover(layout = listOf(staffPicksRow)) |
| 59 | + |
| 60 | + whenever(mockRepository.getDiscoverFeedSuspend()).thenReturn(discover) |
| 61 | + |
| 62 | + val discoverCountryCodeMock: UserSetting<String> = mock() |
| 63 | + whenever(discoverCountryCodeMock.flow).thenReturn(MutableStateFlow("US")) |
| 64 | + whenever(mockSettings.discoverCountryCode).thenReturn(discoverCountryCodeMock) |
| 65 | + |
| 66 | + whenever(mockResources.getString(any())).thenAnswer { it.arguments[0].toString() } |
| 67 | + |
| 68 | + val result = manager.getDiscoverList("staff-picks", mockResources) |
| 69 | + |
| 70 | + assert(result != null) |
| 71 | + assert(result?.listUuid == "staff-picks") |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + fun `loadStaffPicks should return null when region not in row regions`() = runTest { |
| 76 | + val row = createDiscoverRow( |
| 77 | + listUuid = "staff-picks", |
| 78 | + regions = listOf("JP"), |
| 79 | + ) |
| 80 | + val discover = createTestDiscover(layout = listOf(row)) |
| 81 | + |
| 82 | + whenever(mockRepository.getDiscoverFeedSuspend()).thenReturn(discover) |
| 83 | + |
| 84 | + val discoverCountryCodeMock: UserSetting<String> = mock() |
| 85 | + whenever(discoverCountryCodeMock.flow).thenReturn(MutableStateFlow("US")) |
| 86 | + whenever(mockSettings.discoverCountryCode).thenReturn(discoverCountryCodeMock) |
| 87 | + |
| 88 | + val result = manager.getDiscoverList("staff-picks", mockResources) |
| 89 | + |
| 90 | + assert(result == null) |
| 91 | + } |
| 92 | + |
| 93 | + private fun createTestDiscover( |
| 94 | + layout: List<DiscoverRow> = listOf(createDiscoverRow(listUuid = "staff-picks")), |
| 95 | + regions: Map<String, DiscoverRegion> = mapOf( |
| 96 | + "US" to DiscoverRegion("United States", "US-flag", "US"), |
| 97 | + "UK" to DiscoverRegion("United Kingdom", "UK-flag", "UK"), |
| 98 | + ), |
| 99 | + regionCodeToken: String = "{region_code}", |
| 100 | + regionNameToken: String = "{region_name}", |
| 101 | + defaultRegionCode: String = "US", |
| 102 | + ): Discover { |
| 103 | + return Discover( |
| 104 | + layout = layout, |
| 105 | + regions = regions, |
| 106 | + regionCodeToken = regionCodeToken, |
| 107 | + regionNameToken = regionNameToken, |
| 108 | + defaultRegionCode = defaultRegionCode, |
| 109 | + ) |
| 110 | + } |
| 111 | + |
| 112 | + private fun createDiscoverRow( |
| 113 | + id: String? = null, |
| 114 | + type: ListType = ListType.PodcastList, |
| 115 | + displayStyle: DisplayStyle = DisplayStyle.SmallList(), |
| 116 | + expandedStyle: ExpandedStyle = ExpandedStyle.GridList(), |
| 117 | + expandedTopItemLabel: String? = null, |
| 118 | + title: String = "Title", |
| 119 | + source: String = "Source", |
| 120 | + listUuid: String? = "staff-picks", |
| 121 | + categoryId: Int? = null, |
| 122 | + regions: List<String> = listOf("US", "UK"), |
| 123 | + sponsored: Boolean = false, |
| 124 | + curated: Boolean = false, |
| 125 | + sponsoredPodcasts: List<SponsoredPodcast> = emptyList(), |
| 126 | + mostPopularCategoriesId: List<Int>? = null, |
| 127 | + ): DiscoverRow { |
| 128 | + return DiscoverRow( |
| 129 | + id = id, |
| 130 | + type = type, |
| 131 | + displayStyle = displayStyle, |
| 132 | + expandedStyle = expandedStyle, |
| 133 | + expandedTopItemLabel = expandedTopItemLabel, |
| 134 | + title = title, |
| 135 | + source = source, |
| 136 | + listUuid = listUuid, |
| 137 | + categoryId = categoryId, |
| 138 | + regions = regions, |
| 139 | + sponsored = sponsored, |
| 140 | + curated = curated, |
| 141 | + sponsoredPodcasts = sponsoredPodcasts, |
| 142 | + mostPopularCategoriesId = mostPopularCategoriesId, |
| 143 | + ) |
| 144 | + } |
| 145 | +} |
0 commit comments