Bug 1849073 - Part 2: Remove the History Search Dialog
[gecko.git] / mobile / android / fenix / app / src / test / java / org / mozilla / fenix / shopping / middleware / DefaultReviewQualityCheckServiceTest.kt
blob8de08b762cf48571e14183ef7757d859eaf71777
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 package org.mozilla.fenix.shopping.middleware
7 import kotlinx.coroutines.test.runTest
8 import mozilla.components.browser.state.state.BrowserState
9 import mozilla.components.browser.state.state.createTab
10 import mozilla.components.browser.state.store.BrowserStore
11 import mozilla.components.concept.engine.EngineSession
12 import mozilla.components.concept.engine.shopping.ProductAnalysis
13 import mozilla.components.support.test.any
14 import mozilla.components.support.test.mock
15 import mozilla.components.support.test.rule.MainCoroutineRule
16 import org.junit.Assert.assertEquals
17 import org.junit.Assert.assertNull
18 import org.junit.Ignore
19 import org.junit.Rule
20 import org.junit.Test
21 import org.mockito.Mockito.doAnswer
23 @Ignore("Intermittent failure, see https://bugzilla.mozilla.org/show_bug.cgi?id=1849525.")
24 class DefaultReviewQualityCheckServiceTest {
26     @get:Rule
27     val coroutinesTestRule = MainCoroutineRule()
29     @Test
30     fun `GIVEN fetch is called WHEN onResult is invoked THEN product analysis returns the same data`() =
31         runTest {
32             val engineSession = mock<EngineSession>()
33             val expected = mock<ProductAnalysis>()
35             doAnswer { invocation ->
36                 val onResult: (ProductAnalysis) -> Unit = invocation.getArgument(1)
37                 onResult(expected)
38             }.`when`(engineSession).requestProductAnalysis(any(), any(), any())
40             val tab = createTab(
41                 url = "https://www.shopping.org/product",
42                 id = "test-tab",
43                 engineSession = engineSession,
44             )
45             val browserState = BrowserState(
46                 tabs = listOf(tab),
47                 selectedTabId = tab.id,
48             )
50             val tested = DefaultReviewQualityCheckService(BrowserStore(browserState))
52             val actual = tested.fetchProductReview()
54             assertEquals(expected, actual)
55         }
57     @Test
58     fun `GIVEN fetch is called WHEN onException is invoked THEN product analysis returns null`() =
59         runTest {
60             val engineSession = mock<EngineSession>()
62             doAnswer { invocation ->
63                 val onException: (Throwable) -> Unit = invocation.getArgument(2)
64                 onException(RuntimeException())
65             }.`when`(engineSession).requestProductAnalysis(any(), any(), any())
67             val tab = createTab(
68                 url = "https://www.shopping.org/product",
69                 id = "test-tab",
70                 engineSession = engineSession,
71             )
72             val browserState = BrowserState(
73                 tabs = listOf(tab),
74                 selectedTabId = tab.id,
75             )
77             val tested = DefaultReviewQualityCheckService(BrowserStore(browserState))
79             assertNull(tested.fetchProductReview())
80         }
82     @Test
83     fun `WHEN fetch is called THEN fetch is called for the selected tab`() = runTest {
84         val engineSession = mock<EngineSession>()
86         val expected = mock<ProductAnalysis>()
87         doAnswer { invocation ->
88             val onResult: (ProductAnalysis) -> Unit = invocation.getArgument(1)
89             onResult(expected)
90         }.`when`(engineSession).requestProductAnalysis(any(), any(), any())
92         val tab1 = createTab(
93             url = "https://www.mozilla.org",
94             id = "1",
95         )
96         val tab2 = createTab(
97             url = "https://www.shopping.org/product",
98             id = "2",
99             engineSession = engineSession,
100         )
101         val browserState = BrowserState(
102             tabs = listOf(tab1, tab2),
103             selectedTabId = tab2.id,
104         )
106         val tested = DefaultReviewQualityCheckService(BrowserStore(browserState))
108         val actual = tested.fetchProductReview()
110         assertEquals(expected, actual)
111     }