@@ -47,38 +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) {
75
- dialogModule.showAlert(options, null , null )
76
- shadowOf(getMainLooper()).idle()
77
- }
78
-
79
- activity.setTheme(APP_COMPAT_THEME )
80
- }
81
-
82
58
@Test
83
59
fun testAllOptions () {
84
60
val options =
@@ -96,8 +72,7 @@ class DialogModuleTest {
96
72
97
73
val fragment = getFragment()
98
74
99
- assertNotNull(" Fragment was not displayed" , fragment)
100
- assertFalse(fragment!! .isCancelable)
75
+ assertFalse(fragment.isCancelable)
101
76
102
77
val dialog = fragment.dialog as AlertDialog
103
78
assertEquals(" OK" , dialog.getButton(DialogInterface .BUTTON_POSITIVE ).text.toString())
@@ -113,13 +88,13 @@ class DialogModuleTest {
113
88
dialogModule.showAlert(options, null , actionCallback)
114
89
shadowOf(getMainLooper()).idle()
115
90
116
- val dialog = getFragment()!! .dialog as AlertDialog
91
+ val dialog = getFragment().dialog as AlertDialog
117
92
dialog.getButton(DialogInterface .BUTTON_POSITIVE ).performClick()
118
93
shadowOf(getMainLooper()).idle()
119
94
120
95
assertEquals(1 , actionCallback.calls)
121
- assertEquals(DialogModule .ACTION_BUTTON_CLICKED , actionCallback.args!! [ 0 ] )
122
- 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 ) )
123
98
}
124
99
125
100
@Test
@@ -130,13 +105,13 @@ class DialogModuleTest {
130
105
dialogModule.showAlert(options, null , actionCallback)
131
106
shadowOf(getMainLooper()).idle()
132
107
133
- val dialog = getFragment()!! .dialog as AlertDialog
108
+ val dialog = getFragment().dialog as AlertDialog
134
109
dialog.getButton(DialogInterface .BUTTON_NEGATIVE ).performClick()
135
110
shadowOf(getMainLooper()).idle()
136
111
137
112
assertEquals(1 , actionCallback.calls)
138
- assertEquals(DialogModule .ACTION_BUTTON_CLICKED , actionCallback.args!! [ 0 ] )
139
- 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 ) )
140
115
}
141
116
142
117
@Test
@@ -147,13 +122,13 @@ class DialogModuleTest {
147
122
dialogModule.showAlert(options, null , actionCallback)
148
123
shadowOf(getMainLooper()).idle()
149
124
150
- val dialog = getFragment()!! .dialog as AlertDialog
125
+ val dialog = getFragment().dialog as AlertDialog
151
126
dialog.getButton(DialogInterface .BUTTON_NEUTRAL ).performClick()
152
127
shadowOf(getMainLooper()).idle()
153
128
154
129
assertEquals(1 , actionCallback.calls)
155
- assertEquals(DialogModule .ACTION_BUTTON_CLICKED , actionCallback.args!! [ 0 ] )
156
- 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 ) )
157
132
}
158
133
159
134
@Test
@@ -164,16 +139,52 @@ class DialogModuleTest {
164
139
dialogModule.showAlert(options, null , actionCallback)
165
140
shadowOf(getMainLooper()).idle()
166
141
167
- getFragment()!! .dialog!! .dismiss()
142
+ getFragment().dialog?.dismiss()
143
+ shadowOf(getMainLooper()).idle()
144
+
145
+ assertEquals(1 , actionCallback.calls)
146
+ assertEquals(DialogModule .ACTION_DISMISSED , actionCallback.args?.get(0 ))
147
+ }
148
+
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()
168
160
shadowOf(getMainLooper()).idle()
169
161
170
162
assertEquals(1 , actionCallback.calls)
171
- assertEquals(DialogModule .ACTION_DISMISSED , actionCallback.args!! [ 0 ] )
163
+ assertEquals(DialogModule .ACTION_DISMISSED , actionCallback.args?.get( 0 ) )
172
164
}
173
165
174
- private fun getFragment (): AlertFragment ? {
175
- return activity.supportFragmentManager.findFragmentByTag(DialogModule .FRAGMENT_TAG )
176
- as ? AlertFragment
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
177
188
}
178
189
179
190
companion object {
0 commit comments