2626 */
2727package com .salesforce .samples .restexplorer ;
2828
29- import static androidx .test .InstrumentationRegistry .getInstrumentation ;
3029import static androidx .test .espresso .Espresso .onData ;
3130import static androidx .test .espresso .Espresso .onView ;
3231import static androidx .test .espresso .Espresso .openActionBarOverflowOrOptionsMenu ;
4140import static androidx .test .espresso .matcher .ViewMatchers .withText ;
4241import static org .hamcrest .Matchers .allOf ;
4342
43+ import android .app .Dialog ;
4444import android .app .Fragment ;
4545import android .app .FragmentManager ;
4646import android .app .FragmentTransaction ;
@@ -90,20 +90,21 @@ public class ServerPickerActivityTest {
9090 private ServerPickerActivity serverPickerActivity ;
9191
9292 public ActivityScenario <ServerPickerActivity > activityScenario ;
93+ public static UiDevice device = UiDevice .getInstance (InstrumentationRegistry .getInstrumentation ());
9394
9495 // Dismissing system dialog if shown
9596 // See https://stackoverflow.com/questions/39457305/android-testing-waited-for-the-root-of-the-view-hierarchy-to-have-window-focus
9697 @ BeforeClass
9798 public static void dismissSystemDialog () throws UiObjectNotFoundException {
98- UiDevice device = UiDevice .getInstance (InstrumentationRegistry .getInstrumentation ());
9999 UiObject okButton = device .findObject (new UiSelector ().textContains ("OK" ));
100100 if (okButton .exists ()) {
101101 okButton .click ();
102102 }
103+ SalesforceSDKManager .getInstance ().setIsTestRun (true );
103104 }
104105
105106 @ Before
106- public void setUp () throws Exception {
107+ public void setUp () {
107108 eq = new EventsListenerQueue ();
108109
109110 // Waits for app initialization to complete.
@@ -118,7 +119,7 @@ public void setUp() throws Exception {
118119 }
119120
120121 @ After
121- public void tearDown () throws Exception {
122+ public void tearDown () {
122123 if (eq != null ) {
123124 eq .tearDown ();
124125 eq = null ;
@@ -129,11 +130,9 @@ public void tearDown() throws Exception {
129130
130131 /**
131132 * Test that the cancel button can be clicked and the URL not saved.
132- *
133- * @throws Throwable
134133 */
135134 @ Test
136- public void testCancelButton () throws Throwable {
135+ public void testCancelButton () {
137136 openCustomEditDialog ();
138137 clickView (com .salesforce .androidsdk .R .id .sf__cancel_button );
139138 Assert .assertNull ("Custom URL dialog should be closed" ,
@@ -142,11 +141,9 @@ public void testCancelButton() throws Throwable {
142141
143142 /**
144143 * Test a valid URL can be entered and saved.
145- *
146- * @throws Throwable
147144 */
148145 @ Test
149- public void testAddCustomInstance () throws Throwable {
146+ public void testAddCustomInstance () {
150147 String label = "My Custom URL" ;
151148 String url = "https://valid.url.com" ;
152149 addCustomUrl (label , url );
@@ -155,16 +152,14 @@ public void testAddCustomInstance() throws Throwable {
155152
156153 /**
157154 * Test an invalid valid URL is not entered or saved.
158- *
159- * @throws Throwable
160155 */
161156 @ Test
162- public void testInvalidUrl () throws Throwable {
157+ public void testInvalidUrl () {
163158 String label = "Invalid URL" ;
164159 String url = "" ;
165160 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 ());
168163 try {
169164 onView (allOf (withText (label + "\n " + url ), findUiElement ())).check (doesNotExist ());
170165 } catch (Throwable t ) {
@@ -174,11 +169,9 @@ public void testInvalidUrl() throws Throwable {
174169
175170 /**
176171 * Test that https is used if http is added.
177- *
178- * @throws Throwable
179172 */
180173 @ Test
181- public void testAddHttpUrl () throws Throwable {
174+ public void testAddHttpUrl () {
182175 String label = "My http URL" ;
183176 String httpUrl = "http://invalid.url.com" ;
184177 String httpsUrl = "https://invalid.url.com" ;
@@ -188,11 +181,9 @@ public void testAddHttpUrl() throws Throwable {
188181
189182 /**
190183 * Test that https is added is added on a url without it.
191- *
192- * @throws Throwable
193184 */
194185 @ Test
195- public void testAddNoProtocolUrl () throws Throwable {
186+ public void testAddNoProtocolUrl () {
196187 String label = "No Protocol URL" ;
197188 String url = "basic.url.com" ;
198189 String httpsUrl = "https://" + url ;
@@ -202,8 +193,6 @@ public void testAddNoProtocolUrl() throws Throwable {
202193
203194 /**
204195 * Test the reset button works.
205- *
206- * @throws Throwable
207196 */
208197 @ Test
209198 public void testResetButton () throws Throwable {
@@ -239,16 +228,17 @@ private void launchActivityBlocking() {
239228 }
240229 }
241230
242- private void openCustomEditDialog () throws Throwable {
231+ private void openCustomEditDialog () {
232+
243233 clickView (com .salesforce .androidsdk .R .id .sf__show_custom_url_edit );
244234 final CustomServerUrlEditor dialog = serverPickerActivity .getCustomServerUrlEditor ();
245- Thread . sleep (3000 );
235+ device . waitForIdle (3000 );
246236 final View rootView = dialog .getRootView ();
247237 Assert .assertNotNull ("Root view should not be null" , rootView );
248238 clickView (com .salesforce .androidsdk .R .id .sf__picker_custom_label );
249239 }
250240
251- private void addCustomUrl (String label , String url ) throws Throwable {
241+ private void addCustomUrl (String label , String url ) {
252242 if (!serverPickerActivity .getCustomServerUrlEditor ().isVisible ()) {
253243 openCustomEditDialog ();
254244 }
@@ -268,6 +258,7 @@ private void removeFragmentIfRequired() {
268258 }
269259
270260 private void setText (final int viewId , final String text ) {
261+ device .waitForIdle (5000 );
271262 try {
272263 onView (withId (viewId )).perform (replaceText (text ), closeSoftKeyboard ());
273264 } catch (Throwable t ) {
@@ -276,6 +267,7 @@ private void setText(final int viewId, final String text) {
276267 }
277268
278269 private void clickView (final int resId ) {
270+ device .waitForIdle (5000 );
279271 try {
280272 onView (withId (resId )).perform (click ());
281273 } catch (Throwable t ) {
@@ -320,7 +312,7 @@ public boolean matchesSafely(View view) {
320312 }
321313
322314 private static void tapResetButton () {
323- openActionBarOverflowOrOptionsMenu (getInstrumentation ().getTargetContext ());
315+ openActionBarOverflowOrOptionsMenu (InstrumentationRegistry . getInstrumentation ().getTargetContext ());
324316 try {
325317 onData (CoreMatchers .anything ())
326318 .inRoot (RootMatchers .isPlatformPopup ())
0 commit comments