@ -7,12 +7,14 @@ package org.mozilla.fenix.home.pocket
import androidx.navigation.NavController
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.spyk
import io.mockk.verify
import io.mockk.verifyOrder
import mozilla.components.service.pocket.PocketStory
import mozilla.components.service.pocket.PocketStory.PocketRecommendedStory
import mozilla.components.service.pocket.PocketStory.PocketSponsoredStory
import mozilla.components.service.pocket.ext.getCurrentFlightImpressions
import mozilla.components.support.test.robolectric.testContext
import mozilla.telemetry.glean.testing.GleanTestRule
import org.junit.Assert.assertEquals
@ -23,6 +25,7 @@ import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mozilla.fenix.BrowserDirection
import org.mozilla.fenix.GleanMetrics.Pings
import org.mozilla.fenix.GleanMetrics.Pocket
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.R
@ -173,13 +176,31 @@ class DefaultPocketStoriesControllerTest {
fun `WHEN a new sponsored story is shown THEN update the State and record telemetry` ( ) {
val store = spyk ( AppStore ( ) )
val controller = DefaultPocketStoriesController ( mockk ( ) , store , mockk ( ) )
val storyShown : PocketSponsoredStory = mockk ( relaxed = true )
val storyGridLocation = 1 to 2
controller . handleStoryShown ( storyShown , storyGridLocation )
verify { store . dispatch ( AppAction . PocketStoriesShown ( listOf ( storyShown ) ) ) }
assertNotNull ( Pocket . homeRecsSpocShown . testGetValue ( ) )
val storyShown : PocketSponsoredStory = mockk {
every { shim . click } returns " testClickShim "
every { shim . impression } returns " testImpressionShim "
}
var wasPingSent = false
mockkStatic ( " mozilla.components.service.pocket.ext.PocketStoryKt " ) {
// Simulate that the story was already shown 3 times.
every { storyShown . getCurrentFlightImpressions ( ) } returns listOf ( 2L , 3L , 7L )
// Test that the spoc ping is immediately sent with the needed data.
Pings . spoc . testBeforeNextSubmit { reason ->
assertEquals ( storyShown . shim . impression , Pocket . spocShim . testGetValue ( ) )
assertEquals ( Pings . spocReasonCodes . impression . name , reason ?. name )
wasPingSent = true
}
controller . handleStoryShown ( storyShown , 1 to 2 )
verify { store . dispatch ( AppAction . PocketStoriesShown ( listOf ( storyShown ) ) ) }
assertNotNull ( Pocket . homeRecsSpocShown . testGetValue ( ) )
assertEquals ( 1 , Pocket . homeRecsSpocShown . testGetValue ( ) !! . size )
val data = Pocket . homeRecsSpocShown . testGetValue ( ) !! . single ( ) . extra
assertEquals ( " 1x2 " , data ?. entries ?. first { it . key == " position " } ?. value )
assertEquals ( " 4 " , data ?. entries ?. first { it . key == " times_shown " } ?. value )
assertTrue ( wasPingSent )
}
}
@Test
@ -227,24 +248,43 @@ class DefaultPocketStoriesControllerTest {
@Test
fun `WHEN a sponsored story is clicked THEN open that story's url using HomeActivity and record telemetry` ( ) {
val story = PocketSponsoredStory (
val story Clicked = PocketSponsoredStory (
id = 7 ,
title = " " ,
url = " testLink " ,
imageUrl = " " ,
sponsor = " " ,
shim = mockk ( ) ,
shim = mockk {
every { click } returns " testClickShim "
every { impression } returns " testImpressionShim "
} ,
priority = 3 ,
caps = mockk ( relaxed = true ) ,
)
val homeActivity : HomeActivity = mockk ( relaxed = true )
val controller = DefaultPocketStoriesController ( homeActivity , mockk ( ) , mockk ( relaxed = true ) )
var wasPingSent = false
assertNull ( Pocket . homeRecsSpocClicked . testGetValue ( ) )
controller . handleStoryClicked ( story , 1 to 2 )
verify { homeActivity . openToBrowserAndLoad ( story . url , true , BrowserDirection . FromHome ) }
assertNull ( Pocket . homeRecsStoryClicked . testGetValue ( ) )
mockkStatic ( " mozilla.components.service.pocket.ext.PocketStoryKt " ) {
// Simulate that the story was already shown 2 times.
every { storyClicked . getCurrentFlightImpressions ( ) } returns listOf ( 2L , 3L )
// Test that the spoc ping is immediately sent with the needed data.
Pings . spoc . testBeforeNextSubmit { reason ->
assertEquals ( storyClicked . shim . click , Pocket . spocShim . testGetValue ( ) )
assertEquals ( Pings . spocReasonCodes . click . name , reason ?. name )
wasPingSent = true
}
controller . handleStoryClicked ( storyClicked , 2 to 3 )
verify { homeActivity . openToBrowserAndLoad ( storyClicked . url , true , BrowserDirection . FromHome ) }
assertNotNull ( Pocket . homeRecsSpocClicked . testGetValue ( ) )
assertEquals ( 1 , Pocket . homeRecsSpocClicked . testGetValue ( ) !! . size )
val data = Pocket . homeRecsSpocClicked . testGetValue ( ) !! . single ( ) . extra
assertEquals ( " 2x3 " , data ?. entries ?. first { it . key == " position " } ?. value )
assertEquals ( " 3 " , data ?. entries ?. first { it . key == " times_shown " } ?. value )
assertTrue ( wasPingSent )
}
}
@Test