Skip to content

Commit

Permalink
Fix test failures in AddtoHSTest.java - Closes mozilla-mobile#2873
Browse files Browse the repository at this point in the history
  • Loading branch information
rpappalax committed Jul 14, 2018
1 parent f9250b5 commit 2daca41
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
public class AddtoHSTest {
private static final String TEST_PATH = "/";
private MockWebServer webServer;
private int webServerPort;
private String webServerBookmarkName;

@Rule
public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<MainActivity>(MainActivity.class) {
Expand All @@ -53,14 +55,16 @@ protected void beforeActivityLaunched() {
.apply();

webServer = new MockWebServer();
// note: requesting getPort() will automatically start the mock server,
// so if you use the 2 lines, do not try to start server or it will choke.
webServerPort = webServer.getPort();
webServerBookmarkName = "localhost_" + Integer.toString(webServerPort);

try {
webServer.enqueue(new MockResponse()
.setBody(TestHelper.readTestAsset("plain_test.html")));
webServer.enqueue(new MockResponse()
.setBody(TestHelper.readTestAsset("plain_test.html")));

webServer.start();
} catch (IOException e) {
throw new AssertionError("Could not start web server", e);
}
Expand Down Expand Up @@ -112,7 +116,7 @@ public void AddToHomeScreenTest() throws UiObjectNotFoundException {

UiObject shortcutIcon = TestHelper.mDevice.findObject(new UiSelector()
.className("android.widget.TextView")
.description("For Testing Purpose")
.description(webServerBookmarkName)
.enabled(true));

// Open website, and click 'Add to homescreen'
Expand All @@ -138,7 +142,7 @@ public void AddToHomeScreenTest() throws UiObjectNotFoundException {

//Edit shortcut text
TestHelper.shortcutTitle.click();
TestHelper.shortcutTitle.setText("For Testing Purpose");
TestHelper.shortcutTitle.setText(webServerBookmarkName);
TestHelper.AddtoHSOKBtn.click();

// For Android O, we need additional steps
Expand All @@ -163,7 +167,7 @@ public void AddToHomeScreenTest() throws UiObjectNotFoundException {
public void NonameTest() throws UiObjectNotFoundException {
UiObject shortcutIcon = TestHelper.mDevice.findObject(new UiSelector()
.className("android.widget.TextView")
.description("localhost")
.description(webServerBookmarkName)
.enabled(true));

// Open website, and click 'Add to homescreen'
Expand All @@ -180,17 +184,17 @@ public void NonameTest() throws UiObjectNotFoundException {

openAddtoHSDialog();

// Add to Home screen dialog is now shown
// "Add to Home screen" dialog is now shown
TestHelper.shortcutTitle.waitForExists(waitingTime);

Assert.assertTrue(TestHelper.shortcutTitle.isEnabled());
Assert.assertEquals(TestHelper.shortcutTitle.getText(), "gigantic experience");
Assert.assertTrue(TestHelper.AddtoHSOKBtn.isEnabled());
Assert.assertTrue(TestHelper.AddtoHSCancelBtn.isEnabled());

//remove shortcut text
//replace shortcut text
TestHelper.shortcutTitle.click();
TestHelper.shortcutTitle.setText("");
TestHelper.shortcutTitle.setText(webServerBookmarkName);
TestHelper.AddtoHSOKBtn.click();

// For Android O, we need additional steps
Expand All @@ -201,6 +205,9 @@ public void NonameTest() throws UiObjectNotFoundException {
welcomeBtn.click();
}
//App is sent to background, in launcher now
//Start from home and then swipe, to ensure we land where we want to search for shortcut
TestHelper.mDevice.pressHome();
TestHelper.swipeScreenLeft();
shortcutIcon.waitForExists(waitingTime);
Assert.assertTrue(shortcutIcon.isEnabled());
shortcutIcon.click();
Expand Down Expand Up @@ -231,7 +238,7 @@ public void SearchTermShortcutTest() throws UiObjectNotFoundException {

openAddtoHSDialog();

// Add to Home screen dialog is now shown
// "Add to Home screen" dialog is now shown
TestHelper.shortcutTitle.waitForExists(waitingTime);
TestHelper.AddtoHSOKBtn.click();

Expand All @@ -243,7 +250,11 @@ public void SearchTermShortcutTest() throws UiObjectNotFoundException {
if (welcomeBtn.exists()) {
welcomeBtn.click();
}

//App is sent to background, in launcher now
//Start from home and then swipe, to ensure we land where we want to search for shortcut
TestHelper.mDevice.pressHome();
TestHelper.swipeScreenLeft();
shortcutIcon.waitForExists(waitingTime);
Assert.assertTrue(shortcutIcon.isEnabled());
shortcutIcon.click();
Expand Down
36 changes: 33 additions & 3 deletions app/src/androidTest/java/org/mozilla/focus/helpers/TestHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package org.mozilla.focus.helpers;

import android.content.res.Resources;
import android.support.annotation.NonNull;
import android.support.test.InstrumentationRegistry;
import android.support.test.espresso.ViewInteraction;
Expand All @@ -13,6 +14,7 @@
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;
import android.text.format.DateUtils;
import android.util.DisplayMetrics;

import org.mozilla.focus.R;
import org.mozilla.focus.utils.AppConstants;
Expand Down Expand Up @@ -149,14 +151,23 @@ public static void waitForWebContent() {
.resourceId(getAppName() + ":id/add_to_homescreen")
.enabled(true));
public static UiObject AddtoHSCancelBtn = TestHelper.mDevice.findObject(new UiSelector()
.className("android.widget.Button")
.instance(0)
.resourceId(getAppName() + ":id/addtohomescreen_dialog_cancel")
.enabled(true));
public static UiObject AddtoHSOKBtn = TestHelper.mDevice.findObject(new UiSelector()
.resourceId(getAppName() + ":id/addtohomescreen_dialog_add")
.enabled(true));

private static String getAddAutoButtonText() {
if (!AppConstants.isGeckoBuild()) {
return "OK"; // Focus (one exception: also on klarGeckoArmDebug w/ 6p, API 26 only)
} else {
return "ADD AUTOMATICALLY"; // Klar
}
}
private static String addAutoButtonText = getAddAutoButtonText();

public static UiObject AddautoBtn = TestHelper.mDevice.findObject(new UiSelector()
.text("ADD AUTOMATICALLY")
.text(addAutoButtonText)
.enabled(true));
public static UiObject shortcutTitle = TestHelper.mDevice.findObject(new UiSelector()
.resourceId(getAppName() + ":id/edit_title")
Expand Down Expand Up @@ -228,6 +239,25 @@ public static void expandNotification() throws UiObjectNotFoundException {
}
}

public static final int X_OFFSET = 20;
public static final int Y_OFFSET = 500;
public static final int STEPS = 10;

private static DisplayMetrics devicePixels() {
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
return metrics;
}

public static void swipeScreenLeft() {
DisplayMetrics metrics = devicePixels();
mDevice.swipe(metrics.widthPixels - X_OFFSET, Y_OFFSET, 0, Y_OFFSET, STEPS);
}

public static void swipeScreenRight() {
DisplayMetrics metrics = devicePixels();
mDevice.swipe(X_OFFSET, Y_OFFSET, metrics.widthPixels, Y_OFFSET, STEPS);
}

public static void waitForIdle() {
mDevice.waitForIdle(waitingTime);
}
Expand Down

0 comments on commit 2daca41

Please sign in to comment.