Skip to content

Commit 2daca41

Browse files
committed
Fix test failures in AddtoHSTest.java - Closes mozilla-mobile#2873
1 parent f9250b5 commit 2daca41

File tree

2 files changed

+53
-12
lines changed

2 files changed

+53
-12
lines changed

app/src/androidTest/java/org/mozilla/focus/activity/AddtoHSTest.java

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
public 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();

app/src/androidTest/java/org/mozilla/focus/helpers/TestHelper.java

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package org.mozilla.focus.helpers;
77

8+
import android.content.res.Resources;
89
import android.support.annotation.NonNull;
910
import android.support.test.InstrumentationRegistry;
1011
import android.support.test.espresso.ViewInteraction;
@@ -13,6 +14,7 @@
1314
import android.support.test.uiautomator.UiObjectNotFoundException;
1415
import android.support.test.uiautomator.UiSelector;
1516
import android.text.format.DateUtils;
17+
import android.util.DisplayMetrics;
1618

1719
import org.mozilla.focus.R;
1820
import org.mozilla.focus.utils.AppConstants;
@@ -149,14 +151,23 @@ public static void waitForWebContent() {
149151
.resourceId(getAppName() + ":id/add_to_homescreen")
150152
.enabled(true));
151153
public static UiObject AddtoHSCancelBtn = TestHelper.mDevice.findObject(new UiSelector()
152-
.className("android.widget.Button")
153-
.instance(0)
154+
.resourceId(getAppName() + ":id/addtohomescreen_dialog_cancel")
154155
.enabled(true));
155156
public static UiObject AddtoHSOKBtn = TestHelper.mDevice.findObject(new UiSelector()
156157
.resourceId(getAppName() + ":id/addtohomescreen_dialog_add")
157158
.enabled(true));
159+
160+
private static String getAddAutoButtonText() {
161+
if (!AppConstants.isGeckoBuild()) {
162+
return "OK"; // Focus (one exception: also on klarGeckoArmDebug w/ 6p, API 26 only)
163+
} else {
164+
return "ADD AUTOMATICALLY"; // Klar
165+
}
166+
}
167+
private static String addAutoButtonText = getAddAutoButtonText();
168+
158169
public static UiObject AddautoBtn = TestHelper.mDevice.findObject(new UiSelector()
159-
.text("ADD AUTOMATICALLY")
170+
.text(addAutoButtonText)
160171
.enabled(true));
161172
public static UiObject shortcutTitle = TestHelper.mDevice.findObject(new UiSelector()
162173
.resourceId(getAppName() + ":id/edit_title")
@@ -228,6 +239,25 @@ public static void expandNotification() throws UiObjectNotFoundException {
228239
}
229240
}
230241

242+
public static final int X_OFFSET = 20;
243+
public static final int Y_OFFSET = 500;
244+
public static final int STEPS = 10;
245+
246+
private static DisplayMetrics devicePixels() {
247+
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
248+
return metrics;
249+
}
250+
251+
public static void swipeScreenLeft() {
252+
DisplayMetrics metrics = devicePixels();
253+
mDevice.swipe(metrics.widthPixels - X_OFFSET, Y_OFFSET, 0, Y_OFFSET, STEPS);
254+
}
255+
256+
public static void swipeScreenRight() {
257+
DisplayMetrics metrics = devicePixels();
258+
mDevice.swipe(X_OFFSET, Y_OFFSET, metrics.widthPixels, Y_OFFSET, STEPS);
259+
}
260+
231261
public static void waitForIdle() {
232262
mDevice.waitForIdle(waitingTime);
233263
}

0 commit comments

Comments
 (0)