From 166218f74aac50569a1d565728b03e3f28b59480 Mon Sep 17 00:00:00 2001 From: kglazko Date: Mon, 8 Jul 2019 09:00:29 -0700 Subject: [PATCH] Adding l10n screenshot tests for Fenix (#3562) * Adding l10n screenshot tests for Fenix * fixing comments * fix klint and detekt error and adding all tests * better test name and doc added explaining tests --- Gemfile | 3 + app/build.gradle | 2 + .../ui/screenshots/DefaultHomeScreenTest.kt | 71 +++++++++ .../fenix/ui/screenshots/ScreenshotTest.java | 75 +++++++++ .../screenshots/ThreeDotMenuScreenShotTest.kt | 150 ++++++++++++++++++ app/src/debug/AndroidManifest.xml | 10 ++ architecture/build.gradle | 2 +- docs/l10nScreenshotsTests.md | 15 ++ fastlane/Appfile | 2 + fastlane/Screengrabfile | 28 ++++ 10 files changed, 357 insertions(+), 1 deletion(-) create mode 100644 Gemfile create mode 100644 app/src/androidTest/java/org/mozilla/fenix/ui/screenshots/DefaultHomeScreenTest.kt create mode 100644 app/src/androidTest/java/org/mozilla/fenix/ui/screenshots/ScreenshotTest.java create mode 100644 app/src/androidTest/java/org/mozilla/fenix/ui/screenshots/ThreeDotMenuScreenShotTest.kt create mode 100644 docs/l10nScreenshotsTests.md create mode 100644 fastlane/Appfile create mode 100644 fastlane/Screengrabfile diff --git a/Gemfile b/Gemfile new file mode 100644 index 000000000..7a118b49b --- /dev/null +++ b/Gemfile @@ -0,0 +1,3 @@ +source "https://rubygems.org" + +gem "fastlane" diff --git a/app/build.gradle b/app/build.gradle index 6d9c11597..b4499adf6 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -376,6 +376,8 @@ dependencies { // androidTestImplementation Deps.tools_espresso_core androidTestImplementation Deps.uiautomator + androidTestImplementation "tools.fastlane:screengrab:1.2.0" + androidTestImplementation "br.com.concretesolutions:kappuccino:1.2.1" androidTestImplementation Deps.espresso_core, { exclude group: 'com.android.support', module: 'support-annotations' diff --git a/app/src/androidTest/java/org/mozilla/fenix/ui/screenshots/DefaultHomeScreenTest.kt b/app/src/androidTest/java/org/mozilla/fenix/ui/screenshots/DefaultHomeScreenTest.kt new file mode 100644 index 000000000..f22dba7d2 --- /dev/null +++ b/app/src/androidTest/java/org/mozilla/fenix/ui/screenshots/DefaultHomeScreenTest.kt @@ -0,0 +1,71 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package org.mozilla.fenix.ui.screenshots + +import androidx.test.rule.ActivityTestRule +import org.junit.After +import org.junit.Rule +import org.junit.Test +import org.mozilla.fenix.helpers.HomeActivityTestRule +import org.mozilla.fenix.HomeActivity +import org.mozilla.fenix.R +import android.os.SystemClock +import tools.fastlane.screengrab.Screengrab +import tools.fastlane.screengrab.locale.LocaleTestRule +import androidx.test.espresso.Espresso.onView +import androidx.test.espresso.action.ViewActions.click +import androidx.test.espresso.action.ViewActions.swipeUp +import androidx.test.espresso.action.ViewActions.swipeDown +import androidx.test.espresso.matcher.ViewMatchers.hasFocus +import androidx.test.espresso.matcher.ViewMatchers.isDisplayed +import androidx.test.espresso.matcher.ViewMatchers.withId +import org.hamcrest.Matchers.allOf + +class DefaultHomeScreenTest : ScreenshotTest() { + @Rule @JvmField + val localeTestRule = LocaleTestRule() + @get:Rule + var mActivityTestRule: ActivityTestRule = HomeActivityTestRule() + + @After + fun tearDown() { + mActivityTestRule.getActivity().finishAndRemoveTask() + } + + @Test + fun showDefaultHomeScreen() { + onView(allOf(withId(R.id.homeLayout), isDisplayed(), hasFocus())) + onView(allOf(withId(R.id.toolbar), isDisplayed())) + SystemClock.sleep(5000) + Screengrab.screenshot("home-screen") + + onView(allOf(withId(R.id.privateBrowsingButton))).perform(click()) + Screengrab.screenshot("private-browsing-menu") + onView(allOf(withId(R.id.privateBrowsingButton))).perform(click()) + } + + @Test + fun scrollHomeScreen() { + onView(withId(R.id.home_component)).perform(swipeUp()) + Screengrab.screenshot("home-screen2") + SystemClock.sleep(3000) + + onView(withId(R.id.home_component)).perform(swipeUp()) + Screengrab.screenshot("home-screen3") + SystemClock.sleep(3000) + + onView(withId(R.id.finish_button)).perform(click()) + SystemClock.sleep(3000) + + Screengrab.screenshot("finish-button") + SystemClock.sleep(3000) + + onView(withId(R.id.home_component)).perform(swipeDown()) + SystemClock.sleep(3000) + + onView(withId(R.id.add_tab_button)).perform(click()) + Screengrab.screenshot("add_tab_button") + } +} diff --git a/app/src/androidTest/java/org/mozilla/fenix/ui/screenshots/ScreenshotTest.java b/app/src/androidTest/java/org/mozilla/fenix/ui/screenshots/ScreenshotTest.java new file mode 100644 index 000000000..ba13340f6 --- /dev/null +++ b/app/src/androidTest/java/org/mozilla/fenix/ui/screenshots/ScreenshotTest.java @@ -0,0 +1,75 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package org.mozilla.fenix.ui.screenshots; + +import android.Manifest; +import android.app.Instrumentation; +import android.content.Context; +import androidx.annotation.StringRes; +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.rule.GrantPermissionRule; +import androidx.test.uiautomator.UiDevice; +import android.text.format.DateUtils; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.rules.TestRule; +import org.junit.rules.TestWatcher; +import org.junit.runner.Description; + +import tools.fastlane.screengrab.Screengrab; +import tools.fastlane.screengrab.UiAutomatorScreenshotStrategy; + +/** + * Base class for tests that take screenshots. + */ +public abstract class ScreenshotTest { + final long waitingTime = DateUtils.SECOND_IN_MILLIS * 10; + + private Context targetContext; + + UiDevice device; + + @Rule + public GrantPermissionRule permissionRule = GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE); + + @Rule + public TestRule screenshotOnFailureRule = new TestWatcher() { + @Override + protected void failed(Throwable e, Description description) { + // On error take a screenshot so that we can debug it easily + Screengrab.screenshot("FAILURE-" + getScreenshotName(description)); + } + + private String getScreenshotName(Description description) { + return description.getClassName().replace(".", "-") + + "_" + + description.getMethodName().replace(".", "-"); + } + }; + + @Before + public void setUpScreenshots() { + Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); + targetContext = instrumentation.getTargetContext(); + device = UiDevice.getInstance(instrumentation); + + // Use this to switch between default strategy and HostScreencap strategy + Screengrab.setDefaultScreenshotStrategy(new UiAutomatorScreenshotStrategy()); + } + + String getString(@StringRes int resourceId) { + return targetContext.getString(resourceId).trim(); + } + + String getString(@StringRes int resourceId, Object... formatArgs) { + return targetContext.getString(resourceId, formatArgs).trim(); + } + + public void takeScreenshotsAfterWait(String filename, int waitingTime) throws InterruptedException { + Thread.sleep(waitingTime); + Screengrab.screenshot(filename); + } +} diff --git a/app/src/androidTest/java/org/mozilla/fenix/ui/screenshots/ThreeDotMenuScreenShotTest.kt b/app/src/androidTest/java/org/mozilla/fenix/ui/screenshots/ThreeDotMenuScreenShotTest.kt new file mode 100644 index 000000000..9bc985572 --- /dev/null +++ b/app/src/androidTest/java/org/mozilla/fenix/ui/screenshots/ThreeDotMenuScreenShotTest.kt @@ -0,0 +1,150 @@ +package org.mozilla.fenix.ui.screenshots + +import android.os.SystemClock +import androidx.test.rule.ActivityTestRule + +import org.junit.After +import org.junit.Rule +import org.junit.Test +import org.mozilla.fenix.HomeActivity +import org.mozilla.fenix.R +import org.mozilla.fenix.helpers.HomeActivityTestRule + +import tools.fastlane.screengrab.Screengrab +import tools.fastlane.screengrab.locale.LocaleTestRule + +import androidx.test.espresso.Espresso.onView +import androidx.test.espresso.action.ViewActions +import androidx.test.espresso.action.ViewActions.click +import androidx.test.espresso.action.ViewActions.swipeUp +import androidx.test.espresso.matcher.ViewMatchers.withId + +import br.com.concretesolutions.kappuccino.actions.ClickActions +import org.hamcrest.Matchers.allOf +import org.mozilla.fenix.ui.robots.homeScreen + +class ThreeDotMenuScreenShotTest : ScreenshotTest() { + @Rule @JvmField + val localeTestRule = LocaleTestRule() + + @get:Rule + var mActivityTestRule: ActivityTestRule = HomeActivityTestRule() + + @After + fun tearDown() { + mActivityTestRule.getActivity().finishAndRemoveTask() + } + + @Test + fun threeDotMenu() { + onView(allOf(withId(R.id.menuButton))).perform(click()) + Screengrab.screenshot("three-dot-menu") + device.pressBack() + } + + @Test + fun settingsTest() { + homeScreen { + }.openThreeDotMenu { + } + settingsButton2() + Screengrab.screenshot("settings") + + SystemClock.sleep(1000) + settingsAccount() + Screengrab.screenshot("settings-sync") + device.pressBack() + + settingsTheme() + Screengrab.screenshot("settings-theme") + device.pressBack() + + settingsSearch() + Screengrab.screenshot("settings-search") + device.pressBack() + + settingsAccessibility() + Screengrab.screenshot("settings-accessibility") + device.pressBack() + + settingsTp() + Screengrab.screenshot("settings-tp") + device.pressBack() + } + + @Test + fun settingsAfterScrollMenusTest() { + homeScreen { + }.openThreeDotMenu { + } + settingsButton2() + SystemClock.sleep(1000) + onView(withId(R.id.switch_widget)).perform(ViewActions.swipeUp()) + onView(withId(R.id.switch_widget)).perform(ViewActions.swipeUp()) + Screengrab.screenshot("settings-scroll") + SystemClock.sleep(2000) + + settingsRemoveData() + Screengrab.screenshot("settings-delete-browsing-data") + device.pressBack() + + SystemClock.sleep(1000) + onView(withId(R.id.switch_widget)).perform(ViewActions.swipeUp()) + onView(withId(R.id.switch_widget)).perform(ViewActions.swipeUp()) + Screengrab.screenshot("settings-scroll") + SystemClock.sleep(3000) + + settingsTelemetry() + Screengrab.screenshot("settings-telemetry") + device.pressBack() + } + + @Test + fun settingsScrollToBottomTest() { + homeScreen { + }.openThreeDotMenu { + } + settingsButton2() + SystemClock.sleep(1000) + onView(withId(R.id.switch_widget)).perform(ViewActions.swipeUp()) + onView(withId(R.id.switch_widget)).perform(ViewActions.swipeUp()) + onView(withId(R.id.switch_widget)).perform(ViewActions.swipeUp()) + Screengrab.screenshot("settings-scroll2") + SystemClock.sleep(1000) + + onView(withId(R.id.switch_widget)).perform(ViewActions.swipeUp()) + onView(withId(R.id.switch_widget)).perform(ViewActions.swipeUp()) + Screengrab.screenshot("settings-scroll3") + SystemClock.sleep(1000) + + onView(withId(R.id.recycler_view)).perform(swipeUp()) + Screengrab.screenshot("settings-scroll4") + } + + @Test + fun libraryTest() { + homeScreen { + }.openThreeDotMenu { + } + libraryButton() + Screengrab.screenshot("library") + bookmarksButton() + Screengrab.screenshot("library-bookmarks") + device.pressBack() + historyButton() + Screengrab.screenshot("library-history") + } +} + +fun settingsButton2() = ClickActions.click { text(R.string.settings) } +fun libraryButton() = ClickActions.click { text(R.string.browser_menu_your_library) } +fun bookmarksButton() = ClickActions.click { text(R.string.library_bookmarks) } +fun historyButton() = ClickActions.click { text(R.string.library_history) } +fun settingsAccount() = ClickActions.click { text(R.string.preferences_sync) } + +fun settingsSearch() = ClickActions.click { text(R.string.preferences_search_engine) } +fun settingsTheme() = ClickActions.click { text(R.string.preferences_theme) } +fun settingsAccessibility() = ClickActions.click { text(R.string.preferences_accessibility) } +fun settingsTp() = ClickActions.click { text(R.string.preferences_tracking_protection) } +fun settingsRemoveData() = ClickActions.click { text(R.string.preferences_delete_browsing_data) } +fun settingsTelemetry() = ClickActions.click { text(R.string.preferences_data_choices) } diff --git a/app/src/debug/AndroidManifest.xml b/app/src/debug/AndroidManifest.xml index 27db316cd..45846ce14 100644 --- a/app/src/debug/AndroidManifest.xml +++ b/app/src/debug/AndroidManifest.xml @@ -4,6 +4,16 @@ xmlns:tools="http://schemas.android.com/tools"> + + + + + + + + + +