Bug 1859593 - Use ProductAnalysis and ProductRecommendation as data classes

fenix/120.0
rahulsainani 7 months ago committed by mergify[bot]
parent e66ef87316
commit 0c1039e14b

@ -4,8 +4,7 @@
package org.mozilla.fenix.shopping.middleware
import mozilla.components.browser.engine.gecko.shopping.GeckoProductAnalysis
import mozilla.components.browser.engine.gecko.shopping.Highlight
import mozilla.components.concept.engine.shopping.Highlight
import mozilla.components.concept.engine.shopping.ProductAnalysis
import org.mozilla.fenix.shopping.store.ReviewQualityCheckState
import org.mozilla.fenix.shopping.store.ReviewQualityCheckState.HighlightType
@ -15,10 +14,10 @@ import org.mozilla.fenix.shopping.store.ReviewQualityCheckState.OptedIn.ProductR
/**
* Maps [ProductAnalysis] to [ProductReviewState].
*/
fun GeckoProductAnalysis?.toProductReviewState(isInitialAnalysis: Boolean = true): ProductReviewState =
fun ProductAnalysis?.toProductReviewState(isInitialAnalysis: Boolean = true): ProductReviewState =
this?.toProductReview(isInitialAnalysis) ?: ProductReviewState.Error.GenericError
private fun GeckoProductAnalysis.toProductReview(isInitialAnalysis: Boolean): ProductReviewState =
private fun ProductAnalysis.toProductReview(isInitialAnalysis: Boolean): ProductReviewState =
if (pageNotSupported) {
ProductReviewState.Error.UnsupportedProductTypeError
} else if (productId == null) {

@ -6,7 +6,7 @@ package org.mozilla.fenix.shopping.middleware
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import mozilla.components.browser.engine.gecko.shopping.GeckoProductAnalysis
import mozilla.components.concept.engine.shopping.ProductAnalysis
import mozilla.components.lib.state.MiddlewareContext
import mozilla.components.lib.state.Store
import org.mozilla.fenix.components.AppStore
@ -138,7 +138,7 @@ class ReviewQualityCheckNetworkMiddleware(
private fun Store<ReviewQualityCheckState, ReviewQualityCheckAction>.restoreAnalysingStateIfRequired(
productPageUrl: String,
productReviewState: ProductReviewState,
productAnalysis: GeckoProductAnalysis?,
productAnalysis: ProductAnalysis?,
) {
if (productReviewState.isAnalysisPresentOrNoAnalysisPresent() &&
productAnalysis?.needsAnalysis == true &&

@ -6,7 +6,6 @@ package org.mozilla.fenix.shopping.middleware
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import mozilla.components.browser.engine.gecko.shopping.GeckoProductAnalysis
import mozilla.components.browser.state.selector.selectedTab
import mozilla.components.browser.state.store.BrowserStore
import mozilla.components.concept.engine.shopping.ProductAnalysis
@ -24,7 +23,7 @@ interface ReviewQualityCheckService {
*
* @return [ProductAnalysis] if the request succeeds, null otherwise.
*/
suspend fun fetchProductReview(): GeckoProductAnalysis?
suspend fun fetchProductReview(): ProductAnalysis?
/**
* Triggers a reanalysis of the product review for the current tab.
@ -57,16 +56,13 @@ class DefaultReviewQualityCheckService(
private val logger = Logger("DefaultReviewQualityCheckService")
override suspend fun fetchProductReview(): GeckoProductAnalysis? = withContext(Dispatchers.Main) {
override suspend fun fetchProductReview(): ProductAnalysis? = withContext(Dispatchers.Main) {
suspendCoroutine { continuation ->
browserStore.state.selectedTab?.let { tab ->
tab.engineState.engineSession?.requestProductAnalysis(
url = tab.content.url,
onResult = {
when (it) {
is GeckoProductAnalysis -> continuation.resume(it)
else -> continuation.resume(null)
}
continuation.resume(it)
},
onException = {
logger.error("Error fetching product review", it)

@ -4,8 +4,8 @@
package org.mozilla.fenix.shopping
import mozilla.components.browser.engine.gecko.shopping.GeckoProductAnalysis
import mozilla.components.browser.engine.gecko.shopping.Highlight
import mozilla.components.concept.engine.shopping.Highlight
import mozilla.components.concept.engine.shopping.ProductAnalysis
import org.mozilla.fenix.shopping.store.ReviewQualityCheckState
import org.mozilla.fenix.shopping.store.ReviewQualityCheckState.OptedIn.ProductReviewState.AnalysisPresent.AnalysisStatus
import java.util.SortedMap
@ -23,7 +23,7 @@ object ProductAnalysisTestData {
deletedProductReported: Boolean = false,
deletedProduct: Boolean = false,
highlights: Highlight? = null,
): GeckoProductAnalysis = GeckoProductAnalysis(
): ProductAnalysis = ProductAnalysis(
productId = productId,
analysisURL = analysisURL,
grade = grade,

@ -4,12 +4,12 @@
package org.mozilla.fenix.shopping.fake
import mozilla.components.browser.engine.gecko.shopping.GeckoProductAnalysis
import mozilla.components.concept.engine.shopping.ProductAnalysis
import org.mozilla.fenix.shopping.middleware.AnalysisStatusDto
import org.mozilla.fenix.shopping.middleware.ReviewQualityCheckService
class FakeReviewQualityCheckService(
private val productAnalysis: (Int) -> GeckoProductAnalysis? = { null },
private val productAnalysis: (Int) -> ProductAnalysis? = { null },
private val reanalysis: AnalysisStatusDto? = null,
private val status: AnalysisStatusDto? = null,
private val selectedTabUrl: String? = null,
@ -17,7 +17,7 @@ class FakeReviewQualityCheckService(
private var analysisCount = 0
override suspend fun fetchProductReview(): GeckoProductAnalysis? {
override suspend fun fetchProductReview(): ProductAnalysis? {
return productAnalysis(analysisCount).also {
analysisCount++
}

@ -110,33 +110,4 @@ class DefaultReviewQualityCheckServiceTest {
assertEquals(expected, actual)
}
@Test
fun `GIVEN fetch is called WHEN onResult is invoked with an unexpected type THEN product analysis returns null`() =
runTest {
val engineSession = mockk<EngineSession>()
val randomAnalysis = object : ProductAnalysis {
override val productId: String = "id1"
}
every {
engineSession.requestProductAnalysis(any(), any(), any())
}.answers {
secondArg<(ProductAnalysis) -> Unit>().invoke(randomAnalysis)
}
val tab = createTab(
url = "https://www.shopping.org/product",
id = "test-tab",
engineSession = engineSession,
)
val browserState = BrowserState(
tabs = listOf(tab),
selectedTabId = tab.id,
)
val tested = DefaultReviewQualityCheckService(BrowserStore(browserState))
assertNull(tested.fetchProductReview())
}
}

@ -4,7 +4,7 @@
package org.mozilla.fenix.shopping.middleware
import mozilla.components.browser.engine.gecko.shopping.Highlight
import mozilla.components.concept.engine.shopping.Highlight
import org.junit.Assert.assertEquals
import org.junit.Test
import org.mozilla.fenix.shopping.ProductAnalysisTestData
@ -15,7 +15,7 @@ import org.mozilla.fenix.shopping.store.ReviewQualityCheckState.OptedIn.ProductR
class ProductAnalysisMapperTest {
@Test
fun `WHEN GeckoProductAnalysis has data THEN it is mapped to AnalysisPresent`() {
fun `WHEN ProductAnalysis has data THEN it is mapped to AnalysisPresent`() {
val actual = ProductAnalysisTestData.productAnalysis(
productId = "id1",
grade = "C",
@ -50,7 +50,7 @@ class ProductAnalysisMapperTest {
}
@Test
fun `WHEN GeckoProductAnalysis has data with some missing highlights THEN it is mapped to AnalysisPresent with the non null highlights`() {
fun `WHEN ProductAnalysis has data with some missing highlights THEN it is mapped to AnalysisPresent with the non null highlights`() {
val actual = ProductAnalysisTestData.productAnalysis(
productId = "id1",
grade = "C",
@ -83,7 +83,7 @@ class ProductAnalysisMapperTest {
}
@Test
fun `WHEN GeckoProductAnalysis has an invalid grade THEN it is mapped to AnalysisPresent with grade as null`() {
fun `WHEN ProductAnalysis has an invalid grade THEN it is mapped to AnalysisPresent with grade as null`() {
val actual = ProductAnalysisTestData.productAnalysis(
productId = "id1",
grade = "?",

Loading…
Cancel
Save