Bug 1855137 - Add review checker vendor ordering logic
parent
aa1e02bf8d
commit
0af50d1b52
@ -0,0 +1,78 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.shopping.middleware
|
||||
|
||||
import mozilla.components.browser.state.selector.selectedTab
|
||||
import mozilla.components.browser.state.store.BrowserStore
|
||||
import mozilla.components.support.base.log.logger.Logger
|
||||
import org.mozilla.fenix.shopping.store.ReviewQualityCheckState.ProductVendor
|
||||
import java.net.URI
|
||||
import java.net.URISyntaxException
|
||||
|
||||
private const val AMAZON_COM = "amazon.com"
|
||||
private const val BEST_BUY_COM = "bestbuy.com"
|
||||
private const val WALMART_COM = "walmart.com"
|
||||
private val defaultVendorsList = enumValues<ProductVendor>().toList()
|
||||
|
||||
/**
|
||||
* Service for getting the list of product vendors.
|
||||
*/
|
||||
interface ReviewQualityCheckVendorsService {
|
||||
|
||||
/**
|
||||
* Returns the list of product vendors in order.
|
||||
*/
|
||||
fun productVendors(): List<ProductVendor>
|
||||
}
|
||||
|
||||
/**
|
||||
* Default implementation of [ReviewQualityCheckVendorsService] that uses the [BrowserStore] to
|
||||
* identify the selected tab.
|
||||
*
|
||||
* @property browserStore The [BrowserStore] instance to use.
|
||||
*/
|
||||
class DefaultReviewQualityCheckVendorsService(
|
||||
private val browserStore: BrowserStore,
|
||||
) : ReviewQualityCheckVendorsService {
|
||||
|
||||
override fun productVendors(): List<ProductVendor> {
|
||||
val selectedTabUrl = browserStore.state.selectedTab?.content?.url
|
||||
|
||||
return if (selectedTabUrl == null) {
|
||||
defaultVendorsList
|
||||
} else {
|
||||
val host = selectedTabUrl.toJavaUri()?.host
|
||||
when {
|
||||
host == null -> defaultVendorsList
|
||||
host.contains(AMAZON_COM) -> createProductVendorsList(ProductVendor.AMAZON)
|
||||
host.contains(BEST_BUY_COM) -> createProductVendorsList(ProductVendor.BEST_BUY)
|
||||
host.contains(WALMART_COM) -> createProductVendorsList(ProductVendor.WALMART)
|
||||
else -> defaultVendorsList
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates list of product vendors using the firstVendor param as the first item in the list.
|
||||
*/
|
||||
private fun createProductVendorsList(firstVendor: ProductVendor): List<ProductVendor> =
|
||||
listOf(firstVendor) + defaultVendorsList.filterNot { it == firstVendor }
|
||||
|
||||
/**
|
||||
* Convenience function to converts a given string to a [URI] instance. Returns null if the
|
||||
* string is not a valid URI.
|
||||
*/
|
||||
private fun String.toJavaUri(): URI? {
|
||||
return try {
|
||||
URI.create(this)
|
||||
} catch (e: URISyntaxException) {
|
||||
Logger.error("Unable to create URI with the given string $this", e)
|
||||
null
|
||||
} catch (e: IllegalArgumentException) {
|
||||
Logger.error("Unable to create URI with the given string $this", e)
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.shopping.fake
|
||||
|
||||
import org.mozilla.fenix.shopping.middleware.ReviewQualityCheckVendorsService
|
||||
import org.mozilla.fenix.shopping.store.ReviewQualityCheckState.ProductVendor
|
||||
|
||||
class FakeReviewQualityCheckVendorsService(
|
||||
private val productVendors: List<ProductVendor> = listOf(
|
||||
ProductVendor.BEST_BUY,
|
||||
ProductVendor.WALMART,
|
||||
ProductVendor.AMAZON,
|
||||
),
|
||||
) : ReviewQualityCheckVendorsService {
|
||||
override fun productVendors(): List<ProductVendor> = productVendors
|
||||
}
|
@ -0,0 +1,140 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.shopping.middleware
|
||||
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import mozilla.components.browser.state.state.BrowserState
|
||||
import mozilla.components.browser.state.state.createTab
|
||||
import mozilla.components.browser.state.store.BrowserStore
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
import org.mozilla.fenix.shopping.store.ReviewQualityCheckState.ProductVendor
|
||||
|
||||
class DefaultReviewQualityCheckVendorsServiceTest {
|
||||
|
||||
@Test
|
||||
fun `WHEN selected tab is an amazon page THEN amazon is first in product vendors list`() =
|
||||
runTest {
|
||||
val tab = createTab(
|
||||
url = "https://www.amazon.com/product",
|
||||
id = "test-tab",
|
||||
)
|
||||
val browserState = BrowserState(
|
||||
tabs = listOf(tab),
|
||||
selectedTabId = tab.id,
|
||||
)
|
||||
|
||||
val tested = DefaultReviewQualityCheckVendorsService(BrowserStore(browserState))
|
||||
|
||||
val actual = tested.productVendors()
|
||||
val expected = listOf(
|
||||
ProductVendor.AMAZON,
|
||||
ProductVendor.BEST_BUY,
|
||||
ProductVendor.WALMART,
|
||||
)
|
||||
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN selected tab is a walmart page THEN walmart is first in product vendors list`() =
|
||||
runTest {
|
||||
val tab = createTab(
|
||||
url = "https://www.walmart.com/product",
|
||||
id = "test-tab",
|
||||
)
|
||||
|
||||
val browserState = BrowserState(
|
||||
tabs = listOf(tab),
|
||||
selectedTabId = tab.id,
|
||||
)
|
||||
|
||||
val tested = DefaultReviewQualityCheckVendorsService(BrowserStore(browserState))
|
||||
|
||||
val actual = tested.productVendors()
|
||||
val expected = listOf(
|
||||
ProductVendor.WALMART,
|
||||
ProductVendor.AMAZON,
|
||||
ProductVendor.BEST_BUY,
|
||||
)
|
||||
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN selected tab is a best buy page THEN best buy is first in product vendors list`() =
|
||||
runTest {
|
||||
val tab = createTab(
|
||||
url = "https://www.bestbuy.com/product",
|
||||
id = "test-tab",
|
||||
)
|
||||
|
||||
val browserState = BrowserState(
|
||||
tabs = listOf(tab),
|
||||
selectedTabId = tab.id,
|
||||
)
|
||||
|
||||
val tested = DefaultReviewQualityCheckVendorsService(BrowserStore(browserState))
|
||||
|
||||
val actual = tested.productVendors()
|
||||
val expected = listOf(
|
||||
ProductVendor.BEST_BUY,
|
||||
ProductVendor.AMAZON,
|
||||
ProductVendor.WALMART,
|
||||
)
|
||||
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN selected tab is a not a vendor page THEN default product vendors list is returned`() =
|
||||
runTest {
|
||||
val tab = createTab(
|
||||
url = "https://www.shopping.xyz/product",
|
||||
id = "test-tab",
|
||||
)
|
||||
|
||||
val browserState = BrowserState(
|
||||
tabs = listOf(tab),
|
||||
selectedTabId = tab.id,
|
||||
)
|
||||
|
||||
val tested = DefaultReviewQualityCheckVendorsService(BrowserStore(browserState))
|
||||
|
||||
val actual = tested.productVendors()
|
||||
val expected = listOf(
|
||||
ProductVendor.AMAZON,
|
||||
ProductVendor.BEST_BUY,
|
||||
ProductVendor.WALMART,
|
||||
)
|
||||
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `WHEN selected tab is a not a valid uri THEN default product vendors list is returned`() =
|
||||
runTest {
|
||||
val tab = createTab(
|
||||
url = "not a url",
|
||||
id = "test-tab",
|
||||
)
|
||||
|
||||
val browserState = BrowserState(
|
||||
tabs = listOf(tab),
|
||||
selectedTabId = tab.id,
|
||||
)
|
||||
|
||||
val tested = DefaultReviewQualityCheckVendorsService(BrowserStore(browserState))
|
||||
|
||||
val actual = tested.productVendors()
|
||||
val expected = listOf(
|
||||
ProductVendor.AMAZON,
|
||||
ProductVendor.BEST_BUY,
|
||||
ProductVendor.WALMART,
|
||||
)
|
||||
|
||||
assertEquals(expected, actual)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue