You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: gto-support-androidx-lifecycle/src/test/kotlin/org/ccci/gto/android/common/androidx/lifecycle/LiveDataViewModelObserverTest.kt
+35Lines changed: 35 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -9,6 +9,9 @@ import org.junit.Before
9
9
importorg.junit.Test
10
10
importorg.mockito.kotlin.any
11
11
importorg.mockito.kotlin.argumentCaptor
12
+
importorg.mockito.kotlin.eq
13
+
importorg.mockito.kotlin.mock
14
+
importorg.mockito.kotlin.never
12
15
importorg.mockito.kotlin.times
13
16
importorg.mockito.kotlin.verify
14
17
importorg.mockito.kotlin.verifyNoMoreInteractions
@@ -84,4 +87,36 @@ class LiveDataViewModelObserverTest : BaseLiveDataTest() {
84
87
}
85
88
}
86
89
// endregion Multi-observe
90
+
91
+
// region observeOnce
92
+
@Test
93
+
fun`observeOnce() - Only Called One Time`() {
94
+
// observe before LiveData has data
95
+
val observer: (Int) ->Unit= mock()
96
+
liveData.observeOnce(viewModel, observer)
97
+
verify(observer, never()).invoke(any())
98
+
liveData.value =1
99
+
verify(observer).invoke(eq(1))
100
+
liveData.value =2
101
+
verifyNoMoreInteractions(observer)
102
+
103
+
// observe after LiveData has data
104
+
val observer2: (Int) ->Unit= mock()
105
+
liveData.observeOnce(viewModel, observer2)
106
+
verify(observer2).invoke(eq(2))
107
+
liveData.value =3
108
+
verifyNoMoreInteractions(observer2)
109
+
}
110
+
111
+
@Test
112
+
fun`observeOnce() - Respects ViewModel clear`() {
113
+
val observer: (Int) ->Unit= mock()
114
+
liveData.observeOnce(viewModel, observer)
115
+
116
+
// Lifecycle is destroyed so observer is removed before it can be called
0 commit comments