Bug 1862294 - Translations UI Integration Integration Never Translate these Sites...
[gecko.git] / mobile / android / fenix / app / src / test / java / org / mozilla / fenix / translations / TranslationsDialogMiddlewareTest.kt
blob9c57debd7d43e029241e3689fb964943604b9962
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.translations
7 import io.mockk.mockk
8 import io.mockk.verify
9 import kotlinx.coroutines.test.runTest
10 import mozilla.components.browser.state.action.TranslationsAction
11 import mozilla.components.browser.state.store.BrowserStore
12 import mozilla.components.concept.engine.translate.Language
13 import mozilla.components.concept.engine.translate.TranslationOperation
14 import mozilla.components.concept.engine.translate.TranslationPageSettingOperation
15 import mozilla.components.support.test.ext.joinBlocking
16 import mozilla.components.support.test.libstate.ext.waitUntilIdle
17 import mozilla.components.support.test.robolectric.testContext
18 import org.junit.Assert.assertFalse
19 import org.junit.Assert.assertTrue
20 import org.junit.Test
21 import org.junit.runner.RunWith
22 import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
23 import org.mozilla.fenix.utils.Settings
25 @RunWith(FenixRobolectricTestRunner::class)
26 class TranslationsDialogMiddlewareTest {
27     private val browserStore = mockk<BrowserStore>(relaxed = true)
28     private val settings = Settings(testContext)
29     private val translationsDialogMiddleware =
30         TranslationsDialogMiddleware(browserStore = browserStore, sessionId = "tab1", settings = settings)
32     @Test
33     fun `GIVEN translationState WHEN FetchSupportedLanguages action is called THEN call OperationRequestedAction from BrowserStore`() =
34         runTest {
35             val translationStore = TranslationsDialogStore(
36                 initialState = TranslationsDialogState(),
37                 middlewares = listOf(translationsDialogMiddleware),
38             )
39             translationStore.dispatch(TranslationsDialogAction.FetchSupportedLanguages)
40                 .joinBlocking()
42             translationStore.waitUntilIdle()
44             verify {
45                 browserStore.dispatch(
46                     TranslationsAction.OperationRequestedAction(
47                         tabId = "tab1",
48                         operation = TranslationOperation.FETCH_SUPPORTED_LANGUAGES,
49                     ),
50                 )
51             }
52         }
54     @Test
55     fun `GIVEN translationState WHEN TranslateAction from TranslationDialogStore is called THEN call TranslateAction from BrowserStore`() =
56         runTest {
57             val translationStore = TranslationsDialogStore(
58                 initialState = TranslationsDialogState(
59                     initialFrom = Language("en", "English"),
60                     initialTo = Language("fr", "France"),
61                 ),
62                 middlewares = listOf(translationsDialogMiddleware),
63             )
64             translationStore.dispatch(TranslationsDialogAction.TranslateAction).joinBlocking()
66             translationStore.waitUntilIdle()
68             verify {
69                 browserStore.dispatch(
70                     TranslationsAction.TranslateAction(
71                         tabId = "tab1",
72                         fromLanguage = "en",
73                         toLanguage = "fr",
74                         options = null,
75                     ),
76                 )
77             }
78         }
80     @Test
81     fun `GIVEN translationState WHEN RestoreTranslation from TranslationDialogStore is called THEN call TranslateRestoreAction from BrowserStore`() =
82         runTest {
83             val translationStore = TranslationsDialogStore(
84                 initialState = TranslationsDialogState(),
85                 middlewares = listOf(translationsDialogMiddleware),
86             )
87             translationStore.dispatch(TranslationsDialogAction.RestoreTranslation).joinBlocking()
89             translationStore.waitUntilIdle()
91             verify {
92                 browserStore.dispatch(
93                     TranslationsAction.TranslateRestoreAction(
94                         tabId = "tab1",
95                     ),
96                 )
97             }
98         }
100     @Test
101     fun `GIVEN translationState WHEN FetchDownloadFileSizeAction from TranslationDialogStore is called THEN call FetchTranslationDownloadSizeAction from BrowserStore`() =
102         runTest {
103             val translationStore = TranslationsDialogStore(
104                 initialState = TranslationsDialogState(),
105                 middlewares = listOf(translationsDialogMiddleware),
106             )
107             translationStore.dispatch(
108                 TranslationsDialogAction.FetchDownloadFileSizeAction(
109                     toLanguage = Language("en", "English"),
110                     fromLanguage = Language("fr", "France"),
111                 ),
112             ).joinBlocking()
114             translationStore.waitUntilIdle()
116             verify {
117                 browserStore.dispatch(
118                     TranslationsAction.FetchTranslationDownloadSizeAction(
119                         tabId = "tab1",
120                         fromLanguage = Language("fr", "France"),
121                         toLanguage = Language("en", "English"),
122                     ),
123                 )
124             }
125         }
127     @Test
128     fun `GIVEN translationState WHEN FetchPageSettings from TranslationDialogStore is called THEN call FETCH_PAGE_SETTINGS from BrowserStore`() =
129         runTest {
130             val translationStore = TranslationsDialogStore(
131                 initialState = TranslationsDialogState(),
132                 middlewares = listOf(translationsDialogMiddleware),
133             )
134             translationStore.dispatch(TranslationsDialogAction.FetchPageSettings).joinBlocking()
136             translationStore.waitUntilIdle()
138             verify {
139                 browserStore.dispatch(
140                     TranslationsAction.OperationRequestedAction(
141                         tabId = "tab1",
142                         operation = TranslationOperation.FETCH_PAGE_SETTINGS,
143                     ),
144                 )
145             }
146         }
148     @Test
149     fun `GIVEN translationState WHEN UpdatePageSettingsValue with action type AlwaysOfferPopup from TranslationDialogStore is called THEN call UpdatePageSettingAction from BrowserStore`() =
150         runTest {
151             assertTrue(settings.offerTranslation)
152             val translationStore = TranslationsDialogStore(
153                 initialState = TranslationsDialogState(),
154                 middlewares = listOf(translationsDialogMiddleware),
155             )
156             translationStore.dispatch(
157                 TranslationsDialogAction.UpdatePageSettingsValue(
158                     type = TranslationPageSettingsOption.AlwaysOfferPopup(),
159                     checkValue = false,
160                 ),
161             ).joinBlocking()
163             translationStore.waitUntilIdle()
165             verify {
166                 browserStore.dispatch(
167                     TranslationsAction.UpdatePageSettingAction(
168                         tabId = "tab1",
169                         operation = TranslationPageSettingOperation.UPDATE_ALWAYS_OFFER_POPUP,
170                         setting = false,
171                     ),
172                 )
173             }
174             assertFalse(settings.offerTranslation)
175         }
177     @Test
178     fun `GIVEN translationState WHEN UpdatePageSettingsValue with action type AlwaysTranslateLanguage from TranslationDialogStore is called THEN call UpdatePageSettingAction from BrowserStore`() =
179         runTest {
180             val translationStore = TranslationsDialogStore(
181                 initialState = TranslationsDialogState(),
182                 middlewares = listOf(translationsDialogMiddleware),
183             )
184             translationStore.dispatch(
185                 TranslationsDialogAction.UpdatePageSettingsValue(
186                     type = TranslationPageSettingsOption.AlwaysTranslateLanguage(),
187                     checkValue = false,
188                 ),
189             ).joinBlocking()
191             translationStore.waitUntilIdle()
193             verify {
194                 browserStore.dispatch(
195                     TranslationsAction.UpdatePageSettingAction(
196                         tabId = "tab1",
197                         operation = TranslationPageSettingOperation.UPDATE_ALWAYS_TRANSLATE_LANGUAGE,
198                         setting = false,
199                     ),
200                 )
201             }
202         }
204     @Test
205     fun `GIVEN translationState WHEN UpdatePageSettingsValue with action type NeverTranslateLanguage from TranslationDialogStore is called THEN call UpdatePageSettingAction from BrowserStore`() =
206         runTest {
207             val translationStore = TranslationsDialogStore(
208                 initialState = TranslationsDialogState(),
209                 middlewares = listOf(translationsDialogMiddleware),
210             )
211             translationStore.dispatch(
212                 TranslationsDialogAction.UpdatePageSettingsValue(
213                     type = TranslationPageSettingsOption.NeverTranslateLanguage(),
214                     checkValue = true,
215                 ),
216             ).joinBlocking()
218             translationStore.waitUntilIdle()
220             verify {
221                 browserStore.dispatch(
222                     TranslationsAction.UpdatePageSettingAction(
223                         tabId = "tab1",
224                         operation = TranslationPageSettingOperation.UPDATE_NEVER_TRANSLATE_LANGUAGE,
225                         setting = true,
226                     ),
227                 )
228             }
229         }
231     @Test
232     fun `GIVEN translationState WHEN UpdatePageSettingsValue with action type NeverTranslateSite from TranslationDialogStore is called THEN call UpdatePageSettingAction from BrowserStore`() =
233         runTest {
234             val translationStore = TranslationsDialogStore(
235                 initialState = TranslationsDialogState(),
236                 middlewares = listOf(translationsDialogMiddleware),
237             )
238             translationStore.dispatch(
239                 TranslationsDialogAction.UpdatePageSettingsValue(
240                     type = TranslationPageSettingsOption.NeverTranslateSite(),
241                     checkValue = false,
242                 ),
243             ).joinBlocking()
245             translationStore.waitUntilIdle()
247             verify {
248                 browserStore.dispatch(
249                     TranslationsAction.UpdatePageSettingAction(
250                         tabId = "tab1",
251                         operation = TranslationPageSettingOperation.UPDATE_NEVER_TRANSLATE_SITE,
252                         setting = false,
253                     ),
254                 )
255             }
256         }