26
26
*/
27
27
package com .salesforce .samples .restexplorer ;
28
28
29
- import static androidx .test .InstrumentationRegistry .getInstrumentation ;
30
29
import static androidx .test .espresso .Espresso .onData ;
31
30
import static androidx .test .espresso .Espresso .onView ;
32
31
import static androidx .test .espresso .Espresso .openActionBarOverflowOrOptionsMenu ;
41
40
import static androidx .test .espresso .matcher .ViewMatchers .withText ;
42
41
import static org .hamcrest .Matchers .allOf ;
43
42
43
+ import android .app .Dialog ;
44
44
import android .app .Fragment ;
45
45
import android .app .FragmentManager ;
46
46
import android .app .FragmentTransaction ;
@@ -90,20 +90,21 @@ public class ServerPickerActivityTest {
90
90
private ServerPickerActivity serverPickerActivity ;
91
91
92
92
public ActivityScenario <ServerPickerActivity > activityScenario ;
93
+ public static UiDevice device = UiDevice .getInstance (InstrumentationRegistry .getInstrumentation ());
93
94
94
95
// Dismissing system dialog if shown
95
96
// See https://stackoverflow.com/questions/39457305/android-testing-waited-for-the-root-of-the-view-hierarchy-to-have-window-focus
96
97
@ BeforeClass
97
98
public static void dismissSystemDialog () throws UiObjectNotFoundException {
98
- UiDevice device = UiDevice .getInstance (InstrumentationRegistry .getInstrumentation ());
99
99
UiObject okButton = device .findObject (new UiSelector ().textContains ("OK" ));
100
100
if (okButton .exists ()) {
101
101
okButton .click ();
102
102
}
103
+ SalesforceSDKManager .getInstance ().setIsTestRun (true );
103
104
}
104
105
105
106
@ Before
106
- public void setUp () throws Exception {
107
+ public void setUp () {
107
108
eq = new EventsListenerQueue ();
108
109
109
110
// Waits for app initialization to complete.
@@ -118,7 +119,7 @@ public void setUp() throws Exception {
118
119
}
119
120
120
121
@ After
121
- public void tearDown () throws Exception {
122
+ public void tearDown () {
122
123
if (eq != null ) {
123
124
eq .tearDown ();
124
125
eq = null ;
@@ -129,11 +130,9 @@ public void tearDown() throws Exception {
129
130
130
131
/**
131
132
* Test that the cancel button can be clicked and the URL not saved.
132
- *
133
- * @throws Throwable
134
133
*/
135
134
@ Test
136
- public void testCancelButton () throws Throwable {
135
+ public void testCancelButton () {
137
136
openCustomEditDialog ();
138
137
clickView (com .salesforce .androidsdk .R .id .sf__cancel_button );
139
138
Assert .assertNull ("Custom URL dialog should be closed" ,
@@ -142,11 +141,9 @@ public void testCancelButton() throws Throwable {
142
141
143
142
/**
144
143
* Test a valid URL can be entered and saved.
145
- *
146
- * @throws Throwable
147
144
*/
148
145
@ Test
149
- public void testAddCustomInstance () throws Throwable {
146
+ public void testAddCustomInstance () {
150
147
String label = "My Custom URL" ;
151
148
String url = "https://valid.url.com" ;
152
149
addCustomUrl (label , url );
@@ -155,16 +152,14 @@ public void testAddCustomInstance() throws Throwable {
155
152
156
153
/**
157
154
* Test an invalid valid URL is not entered or saved.
158
- *
159
- * @throws Throwable
160
155
*/
161
156
@ Test
162
- public void testInvalidUrl () throws Throwable {
157
+ public void testInvalidUrl () {
163
158
String label = "Invalid URL" ;
164
159
String url = "" ;
165
160
addCustomUrl (label , url );
166
- Assert . assertTrue ( "Custom URL dialog should not be closed" ,
167
- serverPickerActivity . getCustomServerUrlEditor (). getDialog () .isShowing ());
161
+ Dialog dialog = serverPickerActivity . getCustomServerUrlEditor (). getDialog ();
162
+ Assert . assertTrue ( "Custom URL dialog should not be closed" , dialog != null && dialog .isShowing ());
168
163
try {
169
164
onView (allOf (withText (label + "\n " + url ), findUiElement ())).check (doesNotExist ());
170
165
} catch (Throwable t ) {
@@ -174,11 +169,9 @@ public void testInvalidUrl() throws Throwable {
174
169
175
170
/**
176
171
* Test that https is used if http is added.
177
- *
178
- * @throws Throwable
179
172
*/
180
173
@ Test
181
- public void testAddHttpUrl () throws Throwable {
174
+ public void testAddHttpUrl () {
182
175
String label = "My http URL" ;
183
176
String httpUrl = "http://invalid.url.com" ;
184
177
String httpsUrl = "https://invalid.url.com" ;
@@ -188,11 +181,9 @@ public void testAddHttpUrl() throws Throwable {
188
181
189
182
/**
190
183
* Test that https is added is added on a url without it.
191
- *
192
- * @throws Throwable
193
184
*/
194
185
@ Test
195
- public void testAddNoProtocolUrl () throws Throwable {
186
+ public void testAddNoProtocolUrl () {
196
187
String label = "No Protocol URL" ;
197
188
String url = "basic.url.com" ;
198
189
String httpsUrl = "https://" + url ;
@@ -202,8 +193,6 @@ public void testAddNoProtocolUrl() throws Throwable {
202
193
203
194
/**
204
195
* Test the reset button works.
205
- *
206
- * @throws Throwable
207
196
*/
208
197
@ Test
209
198
public void testResetButton () throws Throwable {
@@ -239,16 +228,17 @@ private void launchActivityBlocking() {
239
228
}
240
229
}
241
230
242
- private void openCustomEditDialog () throws Throwable {
231
+ private void openCustomEditDialog () {
232
+
243
233
clickView (com .salesforce .androidsdk .R .id .sf__show_custom_url_edit );
244
234
final CustomServerUrlEditor dialog = serverPickerActivity .getCustomServerUrlEditor ();
245
- Thread . sleep (3000 );
235
+ device . waitForIdle (3000 );
246
236
final View rootView = dialog .getRootView ();
247
237
Assert .assertNotNull ("Root view should not be null" , rootView );
248
238
clickView (com .salesforce .androidsdk .R .id .sf__picker_custom_label );
249
239
}
250
240
251
- private void addCustomUrl (String label , String url ) throws Throwable {
241
+ private void addCustomUrl (String label , String url ) {
252
242
if (!serverPickerActivity .getCustomServerUrlEditor ().isVisible ()) {
253
243
openCustomEditDialog ();
254
244
}
@@ -268,6 +258,7 @@ private void removeFragmentIfRequired() {
268
258
}
269
259
270
260
private void setText (final int viewId , final String text ) {
261
+ device .waitForIdle (5000 );
271
262
try {
272
263
onView (withId (viewId )).perform (replaceText (text ), closeSoftKeyboard ());
273
264
} catch (Throwable t ) {
@@ -276,6 +267,7 @@ private void setText(final int viewId, final String text) {
276
267
}
277
268
278
269
private void clickView (final int resId ) {
270
+ device .waitForIdle (5000 );
279
271
try {
280
272
onView (withId (resId )).perform (click ());
281
273
} catch (Throwable t ) {
@@ -320,7 +312,7 @@ public boolean matchesSafely(View view) {
320
312
}
321
313
322
314
private static void tapResetButton () {
323
- openActionBarOverflowOrOptionsMenu (getInstrumentation ().getTargetContext ());
315
+ openActionBarOverflowOrOptionsMenu (InstrumentationRegistry . getInstrumentation ().getTargetContext ());
324
316
try {
325
317
onData (CoreMatchers .anything ())
326
318
.inRoot (RootMatchers .isPlatformPopup ())
0 commit comments