@@ -71,8 +71,11 @@ import com.duckduckgo.app.widget.ui.WidgetCapabilities
71
71
import com.nhaarman.mockitokotlin2.*
72
72
import io.reactivex.Observable
73
73
import io.reactivex.Single
74
+ import kotlinx.coroutines.Dispatchers
74
75
import kotlinx.coroutines.ExperimentalCoroutinesApi
75
76
import kotlinx.coroutines.runBlocking
77
+ import kotlinx.coroutines.test.runBlockingTest
78
+ import kotlinx.coroutines.withContext
76
79
import org.junit.After
77
80
import org.junit.Assert.*
78
81
import org.junit.Before
@@ -84,6 +87,7 @@ import org.mockito.Mockito.never
84
87
import org.mockito.Mockito.verify
85
88
import java.util.concurrent.TimeUnit
86
89
90
+ @ExperimentalCoroutinesApi
87
91
class BrowserTabViewModelTest {
88
92
89
93
@get:Rule
@@ -94,7 +98,7 @@ class BrowserTabViewModelTest {
94
98
95
99
@ExperimentalCoroutinesApi
96
100
@get:Rule
97
- var coroutinesTestRule = CoroutineTestRule ()
101
+ var coroutineRule = CoroutineTestRule ()
98
102
99
103
@Mock
100
104
private lateinit var mockPrevalenceStore: PrevalenceStore
@@ -196,7 +200,7 @@ class BrowserTabViewModelTest {
196
200
)
197
201
198
202
val siteFactory = SiteFactory (mockPrivacyPractices, mockTrackerNetworks, prevalenceStore = mockPrevalenceStore)
199
- runBlocking< Unit > {
203
+ runBlockingTest {
200
204
whenever(mockTabsRepository.retrieveSiteData(any())).thenReturn(MutableLiveData ())
201
205
whenever(mockPrivacyPractices.privacyPracticesFor(any())).thenReturn(PrivacyPractices .UNKNOWN )
202
206
whenever(mockAppInstallStore.installTimestamp).thenReturn(System .currentTimeMillis() - TimeUnit .DAYS .toMillis(1 ))
@@ -221,7 +225,8 @@ class BrowserTabViewModelTest {
221
225
ctaViewModel = ctaViewModel,
222
226
searchCountDao = mockSearchCountDao,
223
227
pixel = mockPixel,
224
- variantManager = mockVariantManager
228
+ variantManager = mockVariantManager,
229
+ dispatchers = coroutineRule.testDispatcherProvider
225
230
)
226
231
227
232
testee.loadData(" abc" , null , false )
@@ -263,7 +268,7 @@ class BrowserTabViewModelTest {
263
268
}
264
269
265
270
@Test
266
- fun whenOpenInNewBackgroundRequestedThenTabRepositoryUpdatedAndCommandIssued () = runBlocking< Unit > {
271
+ fun whenOpenInNewBackgroundRequestedThenTabRepositoryUpdatedAndCommandIssued () = runBlockingTest {
267
272
val url = " http://www.example.com"
268
273
testee.openInNewBackgroundTab(url)
269
274
@@ -314,13 +319,13 @@ class BrowserTabViewModelTest {
314
319
}
315
320
316
321
@Test
317
- fun whenBookmarkEditedThenDaoIsUpdated () = runBlocking< Unit > {
322
+ fun whenBookmarkEditedThenDaoIsUpdated () = runBlockingTest {
318
323
testee.editBookmark(0 , " A title" , " www.example.com" )
319
324
verify(mockBookmarksDao).update(BookmarkEntity (title = " A title" , url = " www.example.com" ))
320
325
}
321
326
322
327
@Test
323
- fun whenBookmarkAddedThenDaoIsUpdatedAndUserNotified () = runBlocking< Unit > {
328
+ fun whenBookmarkAddedThenDaoIsUpdatedAndUserNotified () = runBlockingTest {
324
329
loadUrl(" www.example.com" , " A title" )
325
330
326
331
testee.onBookmarkAddRequested()
@@ -356,7 +361,7 @@ class BrowserTabViewModelTest {
356
361
}
357
362
358
363
@Test
359
- fun whenBrowsingAndUrlLoadedThenSiteVisitedEntryAddedToLeaderboardDao () {
364
+ fun whenBrowsingAndUrlLoadedThenSiteVisitedEntryAddedToLeaderboardDao () = runBlockingTest {
360
365
loadUrl(" http://example.com/abc" , isBrowserShowing = true )
361
366
verify(mockNetworkLeaderboardDao).incrementSitesVisited()
362
367
}
@@ -425,9 +430,11 @@ class BrowserTabViewModelTest {
425
430
}
426
431
427
432
@Test
428
- fun whenViewModelNotifiedThatUrlGotFocusThenViewStateIsUpdated () {
429
- testee.onOmnibarInputStateChanged(" " , true , hasQueryChanged = false )
430
- assertTrue(omnibarViewState().isEditing)
433
+ fun whenViewModelNotifiedThatUrlGotFocusThenViewStateIsUpdated () = runBlockingTest {
434
+ withContext(Dispatchers .Main ) {
435
+ testee.onOmnibarInputStateChanged(" " , true , hasQueryChanged = false )
436
+ assertTrue(omnibarViewState().isEditing)
437
+ }
431
438
}
432
439
433
440
@Test
@@ -491,17 +498,21 @@ class BrowserTabViewModelTest {
491
498
}
492
499
493
500
@Test
494
- fun whenUrlClearedThenPrivacyGradeIsCleared () = runBlocking<Unit > {
495
- loadUrl(" https://duckduckgo.com" )
496
- assertNotNull(testee.privacyGrade.value)
497
- loadUrl(null )
498
- assertNull(testee.privacyGrade.value)
501
+ fun whenUrlClearedThenPrivacyGradeIsCleared () = runBlockingTest {
502
+ withContext(Dispatchers .Main ) {
503
+ loadUrl(" https://duckduckgo.com" )
504
+ assertNotNull(testee.privacyGrade.value)
505
+ loadUrl(null )
506
+ assertNull(testee.privacyGrade.value)
507
+ }
499
508
}
500
509
501
510
@Test
502
- fun whenUrlLoadedThenPrivacyGradeIsReset () = runBlocking<Unit > {
503
- loadUrl(" https://duckduckgo.com" )
504
- assertNotNull(testee.privacyGrade.value)
511
+ fun whenUrlLoadedThenPrivacyGradeIsReset () = runBlockingTest {
512
+ withContext(Dispatchers .Main ) {
513
+ loadUrl(" https://duckduckgo.com" )
514
+ assertNotNull(testee.privacyGrade.value)
515
+ }
505
516
}
506
517
507
518
@Test
@@ -924,7 +935,7 @@ class BrowserTabViewModelTest {
924
935
}
925
936
926
937
@Test
927
- fun whenSiteLoadedAndUserSelectsToAddBookmarkThenAddBookmarkCommandSentWithUrlAndTitle () = runBlocking< Unit > {
938
+ fun whenSiteLoadedAndUserSelectsToAddBookmarkThenAddBookmarkCommandSentWithUrlAndTitle () = runBlockingTest {
928
939
loadUrl(" foo.com" )
929
940
testee.titleReceived(" Foo Title" )
930
941
testee.onBookmarkAddRequested()
@@ -934,7 +945,7 @@ class BrowserTabViewModelTest {
934
945
}
935
946
936
947
@Test
937
- fun whenNoSiteAndUserSelectsToAddBookmarkThenBookmarkAddedWithBlankTitleAndUrl () = runBlocking< Unit > {
948
+ fun whenNoSiteAndUserSelectsToAddBookmarkThenBookmarkAddedWithBlankTitleAndUrl () = runBlockingTest {
938
949
whenever(mockBookmarksDao.insert(any())).thenReturn(1 )
939
950
testee.onBookmarkAddRequested()
940
951
verify(mockBookmarksDao).insert(BookmarkEntity (title = " " , url = " " ))
@@ -953,7 +964,7 @@ class BrowserTabViewModelTest {
953
964
}
954
965
955
966
@Test
956
- fun whenOnSiteAndBrokenSiteSelectedThenBrokenSiteFeedbackCommandSentWithUrl () = runBlocking< Unit > {
967
+ fun whenOnSiteAndBrokenSiteSelectedThenBrokenSiteFeedbackCommandSentWithUrl () = runBlockingTest {
957
968
loadUrl(" foo.com" , isBrowserShowing = true )
958
969
testee.onBrokenSiteSelected()
959
970
val command = captureCommands().value as Command .BrokenSiteFeedback
@@ -1012,24 +1023,30 @@ class BrowserTabViewModelTest {
1012
1023
}
1013
1024
1014
1025
@Test
1015
- fun whenUrlNullThenSetBrowserNotShowing () {
1016
- testee.loadData(" id" , null , false )
1017
- testee.determineShowBrowser()
1018
- assertEquals(false , testee.browserViewState.value?.browserShowing)
1026
+ fun whenUrlNullThenSetBrowserNotShowing () = runBlockingTest {
1027
+ withContext(Dispatchers .Main ) {
1028
+ testee.loadData(" id" , null , false )
1029
+ testee.determineShowBrowser()
1030
+ assertEquals(false , testee.browserViewState.value?.browserShowing)
1031
+ }
1019
1032
}
1020
1033
1021
1034
@Test
1022
- fun whenUrlBlankThenSetBrowserNotShowing () {
1023
- testee.loadData(" id" , " " , false )
1024
- testee.determineShowBrowser()
1025
- assertEquals(false , testee.browserViewState.value?.browserShowing)
1035
+ fun whenUrlBlankThenSetBrowserNotShowing () = runBlockingTest {
1036
+ withContext(Dispatchers .Main ) {
1037
+ testee.loadData(" id" , " " , false )
1038
+ testee.determineShowBrowser()
1039
+ assertEquals(false , testee.browserViewState.value?.browserShowing)
1040
+ }
1026
1041
}
1027
1042
1028
1043
@Test
1029
- fun whenUrlPresentThenSetBrowserShowing () {
1030
- testee.loadData(" id" , " https://example.com" , false )
1031
- testee.determineShowBrowser()
1032
- assertEquals(true , testee.browserViewState.value?.browserShowing)
1044
+ fun whenUrlPresentThenSetBrowserShowing () = runBlockingTest {
1045
+ withContext(Dispatchers .Main ) {
1046
+ testee.loadData(" id" , " https://example.com" , false )
1047
+ testee.determineShowBrowser()
1048
+ assertEquals(true , testee.browserViewState.value?.browserShowing)
1049
+ }
1033
1050
}
1034
1051
1035
1052
@Test
@@ -1087,9 +1104,11 @@ class BrowserTabViewModelTest {
1087
1104
}
1088
1105
1089
1106
@Test
1090
- fun whenUserSelectToEditQueryThenMoveCaretToTheEnd () {
1091
- testee.onUserSelectedToEditQuery(" foo" )
1092
- assertTrue(omnibarViewState().shouldMoveCaretToEnd)
1107
+ fun whenUserSelectToEditQueryThenMoveCaretToTheEnd () = runBlockingTest {
1108
+ withContext(Dispatchers .Main ) {
1109
+ testee.onUserSelectedToEditQuery(" foo" )
1110
+ assertTrue(omnibarViewState().shouldMoveCaretToEnd)
1111
+ }
1093
1112
}
1094
1113
1095
1114
@Test
0 commit comments