-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathBaseTest.kt
More file actions
196 lines (166 loc) · 7.03 KB
/
BaseTest.kt
File metadata and controls
196 lines (166 loc) · 7.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package com.reactnativenavigation
import android.app.Activity
import android.content.Context
import android.content.res.Configuration
import android.content.res.Resources
import android.os.Handler
import android.os.Looper
import android.view.View
import android.view.ViewGroup
import androidx.annotation.CallSuper
import androidx.appcompat.app.AppCompatActivity
import androidx.coordinatorlayout.widget.CoordinatorLayout
import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags
import com.reactnativenavigation.options.params.Bool
import com.reactnativenavigation.utils.CollectionUtils
import com.reactnativenavigation.utils.Functions
import com.reactnativenavigation.utils.SystemUiUtils
import com.reactnativenavigation.utils.SystemUiUtils.getStatusBarHeight
import com.reactnativenavigation.utils.SystemUiUtils.getStatusBarHeightDp
import com.reactnativenavigation.utils.ViewUtils
import com.reactnativenavigation.viewcontrollers.statusbar.StatusBarPresenter
import com.reactnativenavigation.viewcontrollers.viewcontroller.ViewController
import org.assertj.core.api.Java6Assertions
import org.junit.After
import org.junit.Before
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers
import org.mockito.MockedStatic
import org.mockito.Mockito
import org.mockito.Mockito.mockStatic
import org.robolectric.Robolectric
import org.robolectric.RobolectricTestRunner
import org.robolectric.Shadows
import org.robolectric.android.controller.ActivityController
import org.robolectric.annotation.Config
import org.robolectric.shadows.ShadowLooper
import java.util.Arrays
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [28], application = TestApplication::class, shadows = [ShadowSoLoader::class, ShadowReactView::class])
abstract class BaseTest {
private val handler = Handler(Looper.getMainLooper())
private val shadowMainLooper: ShadowLooper = Shadows.shadowOf(Looper.getMainLooper())
protected lateinit var mockConfiguration: Configuration
private var mockReactNativeFeatureFlags: MockedStatic<ReactNativeFeatureFlags>? = null
@Before
open fun beforeEach() {
mockReactNativeFeatureFlags = mockStatic(ReactNativeFeatureFlags::class.java)
NavigationApplication.instance = Mockito.mock(NavigationApplication::class.java)
mockConfiguration = Mockito.mock(Configuration::class.java)
val res = Mockito.mock(Resources::class.java)
mockConfiguration.uiMode = Configuration.UI_MODE_NIGHT_NO
Mockito.`when`(res.configuration).thenReturn(mockConfiguration)
Mockito.`when`(NavigationApplication.instance.resources).thenReturn(res)
Mockito.`when`(res.getColor(ArgumentMatchers.anyInt())).thenReturn(0x00000)
Mockito.`when`(res.getColor(ArgumentMatchers.anyInt(), ArgumentMatchers.any()))
.thenReturn(0x00000)
RNNFeatureToggles.init()
StatusBarPresenter.init(newActivity())
}
fun mockSystemUiUtils(
statusBarHeight: Int,
statusBarHeightDp: Int,
mockedBlock: Functions.Func1<MockedStatic<SystemUiUtils?>?>
) {
Mockito.mockStatic(SystemUiUtils::class.java).use { theMock ->
theMock.`when`<Any> {
getStatusBarHeight(ArgumentMatchers.any())
}.thenReturn(statusBarHeight)
theMock.`when`<Any> {
getStatusBarHeightDp(ArgumentMatchers.any())
}.thenReturn(statusBarHeightDp)
mockedBlock.run(theMock)
}
}
@After
@CallSuper
fun afterEach() {
idleMainLooper()
mockReactNativeFeatureFlags?.close()
RNNFeatureToggles.clear();
}
fun newActivity(): Activity {
return Robolectric.setupActivity(AppCompatActivity::class.java)
}
fun <T : AppCompatActivity?> newActivityController(clazz: Class<T>): ActivityController<T> {
return Robolectric.buildActivity(clazz)
}
fun assertIsChild(parent: ViewGroup?, vararg children: ViewController<*>?) {
CollectionUtils.forEach(
Arrays.asList(*children)
) { c: ViewController<*> -> assertIsChild(parent, c.view) }
}
fun assertIsChild(parent: ViewGroup?, child: View) {
Java6Assertions.assertThat(parent).isNotNull()
Java6Assertions.assertThat(child).isNotNull()
Java6Assertions.assertThat(ViewUtils.isChildOf(parent, child)).isTrue()
}
fun assertNotChildOf(parent: ViewGroup?, vararg children: ViewController<*>?) {
CollectionUtils.forEach(
Arrays.asList(*children)
) { c: ViewController<*> -> assertNotChildOf(parent, c.view) }
}
fun assertNotChildOf(parent: ViewGroup?, child: View) {
Java6Assertions.assertThat(parent).isNotNull()
Java6Assertions.assertThat(child).isNotNull()
Java6Assertions.assertThat(ViewUtils.isChildOf(parent, child)).isFalse()
}
fun assertMatchParent(view: View) {
Java6Assertions.assertThat(view.layoutParams.width)
.isEqualTo(ViewGroup.LayoutParams.MATCH_PARENT)
Java6Assertions.assertThat(view.layoutParams.height)
.isEqualTo(ViewGroup.LayoutParams.MATCH_PARENT)
}
protected fun disablePushAnimation(vararg controllers: ViewController<*>) {
for (controller in controllers) {
controller.options.animations.push.enabled = Bool(false)
}
}
protected fun disablePopAnimation(vararg controllers: ViewController<*>) {
for (controller in controllers) {
controller.options.animations.pop.enabled = Bool(false)
}
}
protected fun disableModalAnimations(vararg modals: ViewController<*>) {
disableShowModalAnimation(*modals)
disableDismissModalAnimation(*modals)
}
protected fun disableShowModalAnimation(vararg modals: ViewController<*>) {
for (modal in modals) {
modal.options.animations.showModal.toggle(Bool(false))
}
}
protected fun disableDismissModalAnimation(vararg modals: ViewController<*>) {
for (modal in modals) {
modal.options.animations.dismissModal.toggle(Bool(false))
}
}
protected fun dispatchPreDraw(view: View) {
view.viewTreeObserver.dispatchOnPreDraw()
}
protected fun dispatchOnGlobalLayout(view: View) {
view.viewTreeObserver.dispatchOnGlobalLayout()
}
protected fun addToParent(context: Context, vararg controllers: ViewController<*>) {
for (controller in controllers) {
CoordinatorLayout(context).addView(controller.view)
}
}
protected fun mockView(activity: Activity): View {
val mock = Mockito.mock(View::class.java)
Mockito.`when`(mock.context).thenReturn(activity)
return mock
}
protected fun assertVisible(view: View) {
Java6Assertions.assertThat(view.visibility).isEqualTo(View.VISIBLE)
}
protected fun assertGone(view: View) {
Java6Assertions.assertThat(view.visibility).isEqualTo(View.GONE)
}
protected fun post(runnable: Runnable) {
handler.post(runnable)
}
protected fun idleMainLooper() {
shadowMainLooper.idle()
}
}