@@ -47,35 +47,14 @@ class DialogModuleTest {
47
47
48
48
@Before
49
49
fun setUp () {
50
- activityController = Robolectric .buildActivity(FragmentActivity ::class .java)
51
- activity = activityController.create().start().resume().get()
52
- // We must set the theme to a descendant of AppCompat for the AlertDialog to show without
53
- // raising an exception
54
- activity.setTheme(APP_COMPAT_THEME )
55
-
56
- val context: ReactApplicationContext = mock(ReactApplicationContext ::class .java)
57
- whenever(context.hasActiveReactInstance()).thenReturn(true )
58
- whenever(context.currentActivity).thenReturn(activity)
59
-
60
- dialogModule = DialogModule (context)
61
- dialogModule.onHostResume()
50
+ setupActivity()
62
51
}
63
52
64
53
@After
65
54
fun tearDown () {
66
55
activityController.pause().stop().destroy()
67
56
}
68
57
69
- @Test
70
- fun testIllegalActivityTheme () {
71
- val options = JavaOnlyMap ()
72
- activity.setTheme(NON_APP_COMPAT_THEME )
73
-
74
- assertThrows(NullPointerException ::class .java) { dialogModule.showAlert(options, null , null ) }
75
-
76
- activity.setTheme(APP_COMPAT_THEME )
77
- }
78
-
79
58
@Test
80
59
fun testAllOptions () {
81
60
val options =
@@ -93,8 +72,7 @@ class DialogModuleTest {
93
72
94
73
val fragment = getFragment()
95
74
96
- assertNotNull(" Fragment was not displayed" , fragment)
97
- assertFalse(fragment!! .isCancelable)
75
+ assertFalse(fragment.isCancelable)
98
76
99
77
val dialog = fragment.dialog as AlertDialog
100
78
assertEquals(" OK" , dialog.getButton(DialogInterface .BUTTON_POSITIVE ).text.toString())
@@ -110,13 +88,13 @@ class DialogModuleTest {
110
88
dialogModule.showAlert(options, null , actionCallback)
111
89
shadowOf(getMainLooper()).idle()
112
90
113
- val dialog = getFragment()!! .dialog as AlertDialog
91
+ val dialog = getFragment().dialog as AlertDialog
114
92
dialog.getButton(DialogInterface .BUTTON_POSITIVE ).performClick()
115
93
shadowOf(getMainLooper()).idle()
116
94
117
95
assertEquals(1 , actionCallback.calls)
118
- assertEquals(DialogModule .ACTION_BUTTON_CLICKED , actionCallback.args!! [ 0 ] )
119
- assertEquals(DialogInterface .BUTTON_POSITIVE , actionCallback.args!! [ 1 ] )
96
+ assertEquals(DialogModule .ACTION_BUTTON_CLICKED , actionCallback.args?.get( 0 ) )
97
+ assertEquals(DialogInterface .BUTTON_POSITIVE , actionCallback.args?.get( 1 ) )
120
98
}
121
99
122
100
@Test
@@ -127,13 +105,13 @@ class DialogModuleTest {
127
105
dialogModule.showAlert(options, null , actionCallback)
128
106
shadowOf(getMainLooper()).idle()
129
107
130
- val dialog = getFragment()!! .dialog as AlertDialog
108
+ val dialog = getFragment().dialog as AlertDialog
131
109
dialog.getButton(DialogInterface .BUTTON_NEGATIVE ).performClick()
132
110
shadowOf(getMainLooper()).idle()
133
111
134
112
assertEquals(1 , actionCallback.calls)
135
- assertEquals(DialogModule .ACTION_BUTTON_CLICKED , actionCallback.args!! [ 0 ] )
136
- assertEquals(DialogInterface .BUTTON_NEGATIVE , actionCallback.args!! [ 1 ] )
113
+ assertEquals(DialogModule .ACTION_BUTTON_CLICKED , actionCallback.args?.get( 0 ) )
114
+ assertEquals(DialogInterface .BUTTON_NEGATIVE , actionCallback.args?.get( 1 ) )
137
115
}
138
116
139
117
@Test
@@ -144,13 +122,13 @@ class DialogModuleTest {
144
122
dialogModule.showAlert(options, null , actionCallback)
145
123
shadowOf(getMainLooper()).idle()
146
124
147
- val dialog = getFragment()!! .dialog as AlertDialog
125
+ val dialog = getFragment().dialog as AlertDialog
148
126
dialog.getButton(DialogInterface .BUTTON_NEUTRAL ).performClick()
149
127
shadowOf(getMainLooper()).idle()
150
128
151
129
assertEquals(1 , actionCallback.calls)
152
- assertEquals(DialogModule .ACTION_BUTTON_CLICKED , actionCallback.args!! [ 0 ] )
153
- assertEquals(DialogInterface .BUTTON_NEUTRAL , actionCallback.args!! [ 1 ] )
130
+ assertEquals(DialogModule .ACTION_BUTTON_CLICKED , actionCallback.args?.get( 0 ) )
131
+ assertEquals(DialogInterface .BUTTON_NEUTRAL , actionCallback.args?.get( 1 ) )
154
132
}
155
133
156
134
@Test
@@ -161,16 +139,52 @@ class DialogModuleTest {
161
139
dialogModule.showAlert(options, null , actionCallback)
162
140
shadowOf(getMainLooper()).idle()
163
141
164
- getFragment()!! .dialog!! .dismiss()
142
+ getFragment().dialog? .dismiss()
165
143
shadowOf(getMainLooper()).idle()
166
144
167
145
assertEquals(1 , actionCallback.calls)
168
- assertEquals(DialogModule .ACTION_DISMISSED , actionCallback.args!! [ 0 ] )
146
+ assertEquals(DialogModule .ACTION_DISMISSED , actionCallback.args?.get( 0 ) )
169
147
}
170
148
171
- private fun getFragment (): AlertFragment ? {
172
- return activity.supportFragmentManager.findFragmentByTag(DialogModule .FRAGMENT_TAG )
173
- as ? AlertFragment
149
+ @Test
150
+ fun testNonAppCompatActivityTheme () {
151
+ setupActivity(NON_APP_COMPAT_THEME )
152
+
153
+ val options = JavaOnlyMap ()
154
+
155
+ val actionCallback = SimpleCallback ()
156
+ dialogModule.showAlert(options, null , actionCallback)
157
+ shadowOf(getMainLooper()).idle()
158
+
159
+ getFragment().dialog?.dismiss()
160
+ shadowOf(getMainLooper()).idle()
161
+
162
+ assertEquals(1 , actionCallback.calls)
163
+ assertEquals(DialogModule .ACTION_DISMISSED , actionCallback.args?.get(0 ))
164
+ }
165
+
166
+ private fun setupActivity (theme : Int = APP_COMPAT_THEME ) {
167
+ activityController = Robolectric .buildActivity(FragmentActivity ::class .java)
168
+ activity = activityController.create().start().resume().get()
169
+
170
+ // We must set the theme to a descendant of AppCompat for the AlertDialog to show without
171
+ // raising an exception
172
+ activity.setTheme(theme)
173
+
174
+ val context: ReactApplicationContext = mock(ReactApplicationContext ::class .java)
175
+ whenever(context.hasActiveReactInstance()).thenReturn(true )
176
+ whenever(context.currentActivity).thenReturn(activity)
177
+
178
+ dialogModule = DialogModule (context)
179
+ dialogModule.onHostResume()
180
+ }
181
+
182
+ private fun getFragment (): AlertFragment {
183
+ val maybeFragment = activity.supportFragmentManager.findFragmentByTag(DialogModule .FRAGMENT_TAG )
184
+ if (maybeFragment == null || ! (maybeFragment is AlertFragment )) {
185
+ error(" Fragment was not displayed" )
186
+ }
187
+ return maybeFragment
174
188
}
175
189
176
190
companion object {
0 commit comments