Bug 1417255 - Add test when PAW badge should be displayed or hidden. r?maliu draft
authorNevin Chen <cnevinchen@gmail.com>
Tue, 09 Jan 2018 18:02:38 +0800
changeset 719460 d6b08643c06bb7482e529f18e1b9da328ebcb4db
parent 719459 1b9b136d8ecca52b60aa2d44f6837525b1443dbd
child 745813 07ccbddd99daefa75889e1384d1fffd643327341
push id95274
push userbmo:cnevinchen@gmail.com
push dateFri, 12 Jan 2018 08:44:43 +0000
reviewersmaliu
bugs1417255
milestone59.0a1
Bug 1417255 - Add test when PAW badge should be displayed or hidden. r?maliu MozReview-Commit-ID: CZFym4atKV5
mobile/android/app/src/androidTest/java/org/mozilla/gecko/PwaBadgeTest.java
--- a/mobile/android/app/src/androidTest/java/org/mozilla/gecko/PwaBadgeTest.java
+++ b/mobile/android/app/src/androidTest/java/org/mozilla/gecko/PwaBadgeTest.java
@@ -1,31 +1,110 @@
 package org.mozilla.gecko;
 
 import android.support.annotation.Keep;
+import android.support.test.espresso.IdlingRegistry;
+import android.support.test.espresso.idling.CountingIdlingResource;
+import android.support.test.espresso.matcher.ViewMatchers;
 import android.support.test.rule.ActivityTestRule;
 import android.support.test.runner.AndroidJUnit4;
 
+import org.junit.After;
+import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
 import static android.support.test.espresso.Espresso.onView;
+import static android.support.test.espresso.action.ViewActions.click;
+import static android.support.test.espresso.action.ViewActions.pressImeActionButton;
+import static android.support.test.espresso.action.ViewActions.replaceText;
+import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist;
 import static android.support.test.espresso.assertion.ViewAssertions.matches;
-import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
+import static android.support.test.espresso.matcher.ViewMatchers.withEffectiveVisibility;
 import static android.support.test.espresso.matcher.ViewMatchers.withId;
+import static android.support.test.espresso.matcher.ViewMatchers.withParent;
+import static android.support.test.espresso.matcher.ViewMatchers.withTagValue;
+import static android.support.test.espresso.matcher.ViewMatchers.withText;
+import static org.hamcrest.Matchers.allOf;
+import static org.hamcrest.Matchers.is;
+import static org.mozilla.gecko.toolbar.PageActionLayout.PageAction.UUID_PAGE_ACTION_PWA;
 
 @EspressoOnly
 @Keep
 @RunWith(AndroidJUnit4.class)
 public class PwaBadgeTest {
 
+    private static final String PAGE_WEB = "https://pwa.rocks";
+    private static final String PAGE_BADGE_DISPLAY = "badge_display.html";
+    private static final String PAGE_BADGE_HIDE = "badge_hide.html";
+    private static final String IDLE_RESOURCE_PWA = "IDLE_RESOURCE_PWA";
     @Rule
     public ActivityTestRule<BrowserApp> activityRule = new ActivityTestRule<>(BrowserApp.class);
 
+    private CountingIdlingResource countingResource;
+    private String url = null;
+
+    @Before
+    public void setup() {
+        countingResource = new CountingIdlingResource("IDLE_RESOURCE_PWA");
+
+        // To prove that the test fails, omit this call:
+        IdlingRegistry.getInstance().register(countingResource);
+    }
+
+    private String adjustUrl(String path) {
+        return "resource://android/assets/pwa_test_site/" + path;
+
+    }
+
     @Test
-    public void simpleTest() {
+    public void shouldHidePwaBadge() {
+        url = adjustUrl(PAGE_BADGE_HIDE);
+
+        onView(withId(R.id.url_bar_title_scroll_view)).perform(click());
+        onView(withId(R.id.url_edit_text)).perform(click(), replaceText(url), pressImeActionButton());
 
-        onView(withId(R.id.url_bar_title_scroll_view)).check(matches(isDisplayed()));
+        onView(withId(R.id.page_action_layout)).check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)));
+
+        onView(allOf(
+                withParent(withId(R.id.page_action_layout)),
+                withTagValue(is((Object) UUID_PAGE_ACTION_PWA))))
+                .check(doesNotExist());
+
 
     }
 
+    @Test
+    public void shouldDisplayPwaBadge() {
+        // Should display PWA badge
+        url = adjustUrl(PAGE_BADGE_DISPLAY);
+
+        onView(withId(R.id.url_bar_title_scroll_view)).perform(click());
+        onView(withId(R.id.url_edit_text)).perform(click(), replaceText(url), pressImeActionButton());
+
+        countingResource.increment();
+        activityRule.getActivity().setPwaBadgeListener(new BrowserApp.PwaBadgeListener() {
+            @Override
+            public void onPwaBadgeChange() {
+                countingResource.decrement();
+            }
+        });
+        onView(withId(R.id.page_action_layout)).check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)));
+
+        onView(allOf(
+                withParent(withId(R.id.page_action_layout)),
+                withTagValue(is((Object) UUID_PAGE_ACTION_PWA))))
+                .check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)));
+
+
+    }
+
+    @After
+    public void tearDown() {
+        onView(withId(R.id.counter_box)).perform(click());
+        onView(withId(R.id.tabs_menu)).perform(click());
+        onView(withText(R.string.close_all_tabs)).perform(click());
+
+        IdlingRegistry.getInstance().unregister(countingResource);
+
+    }
 }