3737public class AddtoHSTest {
3838 private static final String TEST_PATH = "/" ;
3939 private MockWebServer webServer ;
40+ private int webServerPort ;
41+ private String webServerBookmarkName ;
4042
4143 @ Rule
4244 public ActivityTestRule <MainActivity > mActivityTestRule = new ActivityTestRule <MainActivity >(MainActivity .class ) {
@@ -53,14 +55,16 @@ protected void beforeActivityLaunched() {
5355 .apply ();
5456
5557 webServer = new MockWebServer ();
58+ // note: requesting getPort() will automatically start the mock server,
59+ // so if you use the 2 lines, do not try to start server or it will choke.
60+ webServerPort = webServer .getPort ();
61+ webServerBookmarkName = "localhost_" + Integer .toString (webServerPort );
5662
5763 try {
5864 webServer .enqueue (new MockResponse ()
5965 .setBody (TestHelper .readTestAsset ("plain_test.html" )));
6066 webServer .enqueue (new MockResponse ()
6167 .setBody (TestHelper .readTestAsset ("plain_test.html" )));
62-
63- webServer .start ();
6468 } catch (IOException e ) {
6569 throw new AssertionError ("Could not start web server" , e );
6670 }
@@ -112,7 +116,7 @@ public void AddToHomeScreenTest() throws UiObjectNotFoundException {
112116
113117 UiObject shortcutIcon = TestHelper .mDevice .findObject (new UiSelector ()
114118 .className ("android.widget.TextView" )
115- .description ("For Testing Purpose" )
119+ .description (webServerBookmarkName )
116120 .enabled (true ));
117121
118122 // Open website, and click 'Add to homescreen'
@@ -138,7 +142,7 @@ public void AddToHomeScreenTest() throws UiObjectNotFoundException {
138142
139143 //Edit shortcut text
140144 TestHelper .shortcutTitle .click ();
141- TestHelper .shortcutTitle .setText ("For Testing Purpose" );
145+ TestHelper .shortcutTitle .setText (webServerBookmarkName );
142146 TestHelper .AddtoHSOKBtn .click ();
143147
144148 // For Android O, we need additional steps
@@ -163,7 +167,7 @@ public void AddToHomeScreenTest() throws UiObjectNotFoundException {
163167 public void NonameTest () throws UiObjectNotFoundException {
164168 UiObject shortcutIcon = TestHelper .mDevice .findObject (new UiSelector ()
165169 .className ("android.widget.TextView" )
166- .description ("localhost" )
170+ .description (webServerBookmarkName )
167171 .enabled (true ));
168172
169173 // Open website, and click 'Add to homescreen'
@@ -180,17 +184,17 @@ public void NonameTest() throws UiObjectNotFoundException {
180184
181185 openAddtoHSDialog ();
182186
183- // Add to Home screen dialog is now shown
187+ // " Add to Home screen" dialog is now shown
184188 TestHelper .shortcutTitle .waitForExists (waitingTime );
185189
186190 Assert .assertTrue (TestHelper .shortcutTitle .isEnabled ());
187191 Assert .assertEquals (TestHelper .shortcutTitle .getText (), "gigantic experience" );
188192 Assert .assertTrue (TestHelper .AddtoHSOKBtn .isEnabled ());
189193 Assert .assertTrue (TestHelper .AddtoHSCancelBtn .isEnabled ());
190194
191- //remove shortcut text
195+ //replace shortcut text
192196 TestHelper .shortcutTitle .click ();
193- TestHelper .shortcutTitle .setText ("" );
197+ TestHelper .shortcutTitle .setText (webServerBookmarkName );
194198 TestHelper .AddtoHSOKBtn .click ();
195199
196200 // For Android O, we need additional steps
@@ -201,6 +205,9 @@ public void NonameTest() throws UiObjectNotFoundException {
201205 welcomeBtn .click ();
202206 }
203207 //App is sent to background, in launcher now
208+ //Start from home and then swipe, to ensure we land where we want to search for shortcut
209+ TestHelper .mDevice .pressHome ();
210+ TestHelper .swipeScreenLeft ();
204211 shortcutIcon .waitForExists (waitingTime );
205212 Assert .assertTrue (shortcutIcon .isEnabled ());
206213 shortcutIcon .click ();
@@ -231,7 +238,7 @@ public void SearchTermShortcutTest() throws UiObjectNotFoundException {
231238
232239 openAddtoHSDialog ();
233240
234- // Add to Home screen dialog is now shown
241+ // " Add to Home screen" dialog is now shown
235242 TestHelper .shortcutTitle .waitForExists (waitingTime );
236243 TestHelper .AddtoHSOKBtn .click ();
237244
@@ -243,7 +250,11 @@ public void SearchTermShortcutTest() throws UiObjectNotFoundException {
243250 if (welcomeBtn .exists ()) {
244251 welcomeBtn .click ();
245252 }
253+
246254 //App is sent to background, in launcher now
255+ //Start from home and then swipe, to ensure we land where we want to search for shortcut
256+ TestHelper .mDevice .pressHome ();
257+ TestHelper .swipeScreenLeft ();
247258 shortcutIcon .waitForExists (waitingTime );
248259 Assert .assertTrue (shortcutIcon .isEnabled ());
249260 shortcutIcon .click ();
0 commit comments