44
55package mozilla.components.feature.session.middleware.undo
66
7+ import kotlinx.coroutines.test.StandardTestDispatcher
78import kotlinx.coroutines.test.TestDispatcher
9+ import kotlinx.coroutines.test.runTest
810import kotlinx.coroutines.withContext
911import mozilla.components.browser.state.action.TabListAction
1012import mozilla.components.browser.state.action.UndoAction
1113import mozilla.components.browser.state.selector.selectedTab
1214import mozilla.components.browser.state.state.BrowserState
1315import mozilla.components.browser.state.state.createTab
1416import mozilla.components.browser.state.store.BrowserStore
15- import mozilla.components.support.test.rule.MainCoroutineRule
16- import mozilla.components.support.test.rule.runTestOnMain
1717import org.junit.Assert.assertEquals
1818import org.junit.Assert.assertNull
1919import org.junit.Assert.assertTrue
20- import org.junit.Rule
2120import org.junit.Test
2221
2322class UndoMiddlewareTest {
24- @get:Rule
25- val coroutinesTestRule = MainCoroutineRule ()
26- private val dispatcher = coroutinesTestRule.testDispatcher
23+ private val testDispatcher = StandardTestDispatcher ()
2724
2825 @Test
29- fun `Undo scenario - Removing single tab` () = runTestOnMain {
26+ fun `Undo scenario - Removing single tab` () = runTest(testDispatcher) {
3027 val store = BrowserStore (
3128 middleware = listOf (
32- UndoMiddleware (clearAfterMillis = 60000 ),
29+ UndoMiddleware (clearAfterMillis = 60000 , this , this ),
3330 ),
3431 initialState = BrowserState (
3532 tabs = listOf (
@@ -51,17 +48,17 @@ class UndoMiddlewareTest {
5148 assertEquals(1 , store.state.tabs.size)
5249 assertEquals(" https://getpocket.com" , store.state.selectedTab!! .content.url)
5350
54- restoreRecoverableTabs(dispatcher , store)
51+ restoreRecoverableTabs(testDispatcher , store)
5552
5653 assertEquals(2 , store.state.tabs.size)
5754 assertEquals(" https://www.mozilla.org" , store.state.selectedTab!! .content.url)
5855 }
5956
6057 @Test
61- fun `Undo scenario - Removing list of tabs` () = runTestOnMain {
58+ fun `Undo scenario - Removing list of tabs` () = runTest(testDispatcher) {
6259 val store = BrowserStore (
6360 middleware = listOf (
64- UndoMiddleware (clearAfterMillis = 60000 ),
61+ UndoMiddleware (clearAfterMillis = 60000 , this , this ),
6562 ),
6663 initialState = BrowserState (
6764 tabs = listOf (
@@ -83,17 +80,17 @@ class UndoMiddlewareTest {
8380 assertEquals(1 , store.state.tabs.size)
8481 assertEquals(" https://firefox.com" , store.state.selectedTab!! .content.url)
8582
86- restoreRecoverableTabs(dispatcher , store)
83+ restoreRecoverableTabs(testDispatcher , store)
8784
8885 assertEquals(3 , store.state.tabs.size)
8986 assertEquals(" https://www.mozilla.org" , store.state.selectedTab!! .content.url)
9087 }
9188
9289 @Test
93- fun `Undo scenario - Removing all normal tabs` () = runTestOnMain {
90+ fun `Undo scenario - Removing all normal tabs` () = runTest(testDispatcher) {
9491 val store = BrowserStore (
9592 middleware = listOf (
96- UndoMiddleware (clearAfterMillis = 60000 ),
93+ UndoMiddleware (clearAfterMillis = 60000 , this , this ),
9794 ),
9895 initialState = BrowserState (
9996 tabs = listOf (
@@ -115,17 +112,17 @@ class UndoMiddlewareTest {
115112 assertEquals(1 , store.state.tabs.size)
116113 assertNull(store.state.selectedTab)
117114
118- restoreRecoverableTabs(dispatcher , store)
115+ restoreRecoverableTabs(testDispatcher , store)
119116
120117 assertEquals(3 , store.state.tabs.size)
121118 assertEquals(" https://getpocket.com" , store.state.selectedTab!! .content.url)
122119 }
123120
124121 @Test
125- fun `Undo scenario - Removing all tabs` () = runTestOnMain {
122+ fun `Undo scenario - Removing all tabs` () = runTest(testDispatcher) {
126123 val store = BrowserStore (
127124 middleware = listOf (
128- UndoMiddleware (clearAfterMillis = 60000 ),
125+ UndoMiddleware (clearAfterMillis = 60000 , this , this ),
129126 ),
130127 initialState = BrowserState (
131128 tabs = listOf (
@@ -147,17 +144,17 @@ class UndoMiddlewareTest {
147144 assertEquals(0 , store.state.tabs.size)
148145 assertNull(store.state.selectedTab)
149146
150- restoreRecoverableTabs(dispatcher , store)
147+ restoreRecoverableTabs(testDispatcher , store)
151148
152149 assertEquals(3 , store.state.tabs.size)
153150 assertEquals(" https://getpocket.com" , store.state.selectedTab!! .content.url)
154151 }
155152
156153 @Test
157- fun `Undo scenario - Removing all tabs non-recoverable` () = runTestOnMain {
154+ fun `Undo scenario - Removing all tabs non-recoverable` () = runTest(testDispatcher) {
158155 val store = BrowserStore (
159156 middleware = listOf (
160- UndoMiddleware (clearAfterMillis = 60000 ),
157+ UndoMiddleware (clearAfterMillis = 60000 , this , this ),
161158 ),
162159 initialState = BrowserState (
163160 tabs = listOf (
@@ -179,16 +176,16 @@ class UndoMiddlewareTest {
179176 assertEquals(0 , store.state.tabs.size)
180177 assertNull(store.state.selectedTab)
181178
182- restoreRecoverableTabs(dispatcher , store)
179+ restoreRecoverableTabs(testDispatcher , store)
183180
184181 assertEquals(0 , store.state.tabs.size)
185182 }
186183
187184 @Test
188- fun `Undo History in State is written` () = runTestOnMain {
185+ fun `Undo History in State is written` () = runTest(testDispatcher) {
189186 val store = BrowserStore (
190187 middleware = listOf (
191- UndoMiddleware (clearAfterMillis = 60000 ),
188+ UndoMiddleware (clearAfterMillis = 60000 , this , this ),
192189 ),
193190 initialState = BrowserState (
194191 tabs = listOf (
@@ -223,7 +220,7 @@ class UndoMiddlewareTest {
223220 assertEquals(" https://getpocket.com" , store.state.undoHistory.tabs[1 ].state.url)
224221 assertEquals(0 , store.state.tabs.size)
225222
226- restoreRecoverableTabs(dispatcher , store)
223+ restoreRecoverableTabs(testDispatcher , store)
227224
228225 assertNull(store.state.undoHistory.selectedTabId)
229226 assertTrue(store.state.undoHistory.tabs.isEmpty())
@@ -234,10 +231,10 @@ class UndoMiddlewareTest {
234231 }
235232
236233 @Test
237- fun `Undo History gets cleared after time` () = runTestOnMain {
234+ fun `Undo History gets cleared after time` () = runTest(testDispatcher) {
238235 val store = BrowserStore (
239236 middleware = listOf (
240- UndoMiddleware (clearAfterMillis = 60000 , waitScope = coroutinesTestRule.scope ),
237+ UndoMiddleware (clearAfterMillis = 60000 , waitScope = this , mainScope = this ),
241238 ),
242239 initialState = BrowserState (
243240 tabs = listOf (
@@ -262,14 +259,14 @@ class UndoMiddlewareTest {
262259 assertEquals(" https://www.mozilla.org" , store.state.undoHistory.tabs[0 ].state.url)
263260 assertEquals(" https://getpocket.com" , store.state.undoHistory.tabs[1 ].state.url)
264261
265- dispatcher .scheduler.advanceUntilIdle()
262+ testDispatcher .scheduler.advanceUntilIdle()
266263
267264 assertNull(store.state.undoHistory.selectedTabId)
268265 assertTrue(store.state.undoHistory.tabs.isEmpty())
269266 assertEquals(1 , store.state.tabs.size)
270267 assertEquals(" https://reddit.com/r/firefox" , store.state.tabs[0 ].content.url)
271268
272- restoreRecoverableTabs(dispatcher , store)
269+ restoreRecoverableTabs(testDispatcher , store)
273270
274271 assertEquals(1 , store.state.tabs.size)
275272 assertEquals(" https://reddit.com/r/firefox" , store.state.tabs[0 ].content.url)
0 commit comments