From 85e5ed816451b6e78574a40980f6b30876836e4d Mon Sep 17 00:00:00 2001 From: cyBerta Date: Tue, 21 Nov 2023 15:05:38 +0100 Subject: fixing Screenshot tests for Bitmask, provider setup screenshot tests are also fixed for custom flavored builds. --- .../se/leap/bitmaskclient/base/BitmaskTest.java | 166 ++++++++++++++++++--- .../leap/bitmaskclient/base/ProviderSetupTest.java | 69 --------- .../leap/bitmaskclient/suite/ScreenshotTest.java | 2 +- 3 files changed, 148 insertions(+), 89 deletions(-) delete mode 100644 app/src/androidTestNormal/java/se/leap/bitmaskclient/base/ProviderSetupTest.java (limited to 'app/src/androidTestNormal/java/se/leap/bitmaskclient') diff --git a/app/src/androidTestNormal/java/se/leap/bitmaskclient/base/BitmaskTest.java b/app/src/androidTestNormal/java/se/leap/bitmaskclient/base/BitmaskTest.java index aa437c74..1d340f61 100644 --- a/app/src/androidTestNormal/java/se/leap/bitmaskclient/base/BitmaskTest.java +++ b/app/src/androidTestNormal/java/se/leap/bitmaskclient/base/BitmaskTest.java @@ -1,42 +1,170 @@ package se.leap.bitmaskclient.base; -import static androidx.test.espresso.Espresso.onData; +import static androidx.test.espresso.Espresso.onView; import static androidx.test.espresso.action.ViewActions.click; import static androidx.test.espresso.matcher.ViewMatchers.withId; -import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.hasToString; -import static utils.CustomInteractions.tryResolve; +import static androidx.test.espresso.matcher.ViewMatchers.withText; +import static org.junit.Assert.assertNotNull; +import static se.leap.bitmaskclient.base.MainActivity.ACTION_SHOW_VPN_FRAGMENT; +import static utils.ProviderSetupUtils.runProviderSetup; -import androidx.test.espresso.DataInteraction; -import androidx.test.espresso.NoMatchingViewException; +import android.Manifest; +import android.app.Activity; +import android.app.Instrumentation; +import android.content.Context; +import android.content.Intent; +import android.os.Build; +import android.view.Gravity; + +import androidx.test.espresso.Espresso; +import androidx.test.espresso.PerformException; +import androidx.test.espresso.ViewInteraction; +import androidx.test.espresso.action.ViewActions; +import androidx.test.espresso.assertion.ViewAssertions; +import androidx.test.espresso.contrib.DrawerActions; +import androidx.test.espresso.contrib.DrawerMatchers; +import androidx.test.espresso.matcher.RootMatchers; +import androidx.test.espresso.matcher.ViewMatchers; import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.LargeTest; +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.rule.GrantPermissionRule; +import androidx.test.uiautomator.UiDevice; +import androidx.test.uiautomator.UiObjectNotFoundException; +import org.hamcrest.Matchers; +import org.junit.Before; +import org.junit.ClassRule; import org.junit.FixMethodOrder; +import org.junit.Rule; +import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.MethodSorters; import se.leap.bitmaskclient.R; +import se.leap.bitmaskclient.base.models.ProviderObservable; +import tools.fastlane.screengrab.Screengrab; +import tools.fastlane.screengrab.UiAutomatorScreenshotStrategy; +import tools.fastlane.screengrab.locale.LocaleTestRule; +import utils.CustomInteractions; @LargeTest @RunWith(AndroidJUnit4.class) @FixMethodOrder(MethodSorters.NAME_ASCENDING) -public class BitmaskTest extends ProviderBaseTest { +public class BitmaskTest { + + @ClassRule + public static final LocaleTestRule localeTestRule = new LocaleTestRule(); + + @Rule + public GrantPermissionRule notificationPermissionRule = (Build.VERSION.SDK_INT >= 33) ? GrantPermissionRule.grant(Manifest.permission.POST_NOTIFICATIONS) : null; + + UiDevice device; + @Before + public void setup() { + Screengrab.setDefaultScreenshotStrategy(new UiAutomatorScreenshotStrategy()); + Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); + device = UiDevice.getInstance(instrumentation); + } + + @Test + public void test03_vpnStartTest() throws InterruptedException, UiObjectNotFoundException { + startMainActivity(); + + Screengrab.screenshot("VPN_connecting"); + ViewInteraction mainButtonStop = CustomInteractions.tryResolve( + Espresso.onView(Matchers.allOf( + ViewMatchers.withId(R.id.button), + ViewMatchers.withTagValue(Matchers.is("button_circle_stop")))), + ViewAssertions.matches(ViewMatchers.isDisplayed()), + 20); + Screengrab.screenshot("VPN_connected"); + + mainButtonStop.perform(ViewActions.click()); + Screengrab.screenshot("VPN_ask_disconnect"); + + Espresso.onView(ViewMatchers.withText(android.R.string.yes)) + .inRoot(RootMatchers.isDialog()) + .check(ViewAssertions.matches(ViewMatchers.isDisplayed())) + .perform(ViewActions.click()); + Screengrab.screenshot("VPN_disconnected"); + } + + @Test + public void test04_SettingsFragmentScreenshots() { + startMainActivity(); + Espresso.onView(ViewMatchers.withId(R.id.drawer_layout)) + .check(ViewAssertions.matches(DrawerMatchers.isClosed(Gravity.LEFT))) // Left Drawer should be closed. + .perform(DrawerActions.open()); // Open Drawer + + Screengrab.screenshot("navigationDrawer"); + + // Start the screen of your activity. + Espresso.onView(ViewMatchers.withId(R.id.advancedSettings)) + .perform(ViewActions.click()); + + Screengrab.screenshot("settingsFragment"); + } - @Override - public boolean configureProviderIfNeeded() { + @Test + public void test05_LocationSelectionFragmentScreenshots() { + startMainActivity(); + Espresso.onView(ViewMatchers.withId(R.id.drawer_layout)) + .check(ViewAssertions.matches(DrawerMatchers.isClosed(Gravity.LEFT))) // Left Drawer should be closed. + .perform(DrawerActions.open()); // Open Drawer + + Espresso.onView(ViewMatchers.withId(R.id.manualGatewaySelection)) + .perform(ViewActions.click()); + + Screengrab.screenshot("GatewaySelectionFragment"); + } + + @Test + public void test06_AppExclusionFragmentScreenshots() { + startMainActivity(); + Espresso.onView(ViewMatchers.withId(R.id.drawer_layout)) + .check(ViewAssertions.matches(DrawerMatchers.isClosed(Gravity.LEFT))) // Left Drawer should be closed. + .perform(DrawerActions.open()); // Open Drawer + + Espresso.onView(ViewMatchers.withId(R.id.advancedSettings)).perform(ViewActions.click()); + + Espresso.onView(ViewMatchers.withId(R.id.exclude_apps)).perform(ViewActions.click()); + + CustomInteractions.tryResolve( + Espresso.onData(Matchers.anything()).inAdapterView(ViewMatchers.withId(android.R.id.list)).atPosition(2), + ViewAssertions.matches(ViewMatchers.isDisplayed()), + 5); + + Screengrab.screenshot("App_Exclusion_Fragment"); + } + + private void startMainActivity() { + Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); + Instrumentation.ActivityMonitor monitor = new Instrumentation.ActivityMonitor(MainActivity.class.getName(), null, false); + instrumentation.addMonitor(monitor); + Intent intent = new Intent(instrumentation.getTargetContext(), MainActivity.class); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + intent.setAction(ACTION_SHOW_VPN_FRAGMENT); + instrumentation.startActivitySync(intent); + Activity activity = instrumentation.waitForMonitor(monitor); + assertNotNull(activity); + configureIfNeeded(); + } + + private void configureIfNeeded() { + if (ProviderObservable.getInstance().getCurrentProvider().isConfigured()) { + return; + } + final Context targetContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); try { - DataInteraction linearLayout = tryResolve(onData(hasToString(containsString("riseup.net"))) - .inAdapterView(withId(R.id.provider_list)), - 2); - linearLayout.perform(click()); - return true; - } catch (NoMatchingViewException e) { - // it might be that the provider was already configured, so we print the stack - // trace here and try to continue - e.printStackTrace(); + onView(withId(R.id.drawer_layout)).perform(DrawerActions.open()); + } catch (PerformException performException) { + System.out.println("navigation drawer already opened"); } - return false; + onView(withText(R.string.switch_provider_menu_option)).perform(click()); + System.out.println("configure Provider... starting SetupActivity"); + + runProviderSetup(device, false, false, targetContext); } } diff --git a/app/src/androidTestNormal/java/se/leap/bitmaskclient/base/ProviderSetupTest.java b/app/src/androidTestNormal/java/se/leap/bitmaskclient/base/ProviderSetupTest.java deleted file mode 100644 index 460a30ad..00000000 --- a/app/src/androidTestNormal/java/se/leap/bitmaskclient/base/ProviderSetupTest.java +++ /dev/null @@ -1,69 +0,0 @@ -package se.leap.bitmaskclient.base; - - -import static android.content.Context.MODE_PRIVATE; -import static androidx.test.core.app.ApplicationProvider.getApplicationContext; -import static androidx.test.espresso.Espresso.onData; -import static androidx.test.espresso.action.ViewActions.click; -import static androidx.test.espresso.matcher.ViewMatchers.withId; -import static org.hamcrest.Matchers.anything; -import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.hasToString; -import static se.leap.bitmaskclient.base.models.Constants.SHARED_PREFERENCES; -import static utils.CustomInteractions.tryResolve; - -import android.content.SharedPreferences; - -import androidx.test.espresso.DataInteraction; -import androidx.test.espresso.NoMatchingViewException; -import androidx.test.ext.junit.rules.ActivityScenarioRule; -import androidx.test.ext.junit.runners.AndroidJUnit4; -import androidx.test.filters.LargeTest; - -import org.junit.Before; -import org.junit.ClassRule; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; - -import se.leap.bitmaskclient.R; -import se.leap.bitmaskclient.base.utils.PreferenceHelper; -import se.leap.bitmaskclient.providersetup.ProviderListActivity; -import tools.fastlane.screengrab.Screengrab; -import tools.fastlane.screengrab.UiAutomatorScreenshotStrategy; -import tools.fastlane.screengrab.locale.LocaleTestRule; - -@LargeTest -@RunWith(AndroidJUnit4.class) -public class ProviderSetupTest { - - @ClassRule - public static final LocaleTestRule localeTestRule = new LocaleTestRule(); - - @Rule - public ActivityScenarioRule mActivityScenarioRule = - new ActivityScenarioRule<>(ProviderListActivity.class); - - @Before - public void setup() { - Screengrab.setDefaultScreenshotStrategy(new UiAutomatorScreenshotStrategy()); - SharedPreferences preferences = PreferenceHelper.getSharedPreferences(getApplicationContext()); - preferences.edit().clear().commit(); - } - - @Test - public void testConfigureRiseupVPNScreenshot() { - DataInteraction linearLayout = tryResolve(onData(hasToString(containsString("riseup.net"))) - .inAdapterView(withId(R.id.provider_list)), - 2); - Screengrab.screenshot("ProviderListActivity"); - linearLayout.perform(click()); - Screengrab.screenshot("ProviderListActivity_configureRiseup"); - } - - @Test - public void testaddManuallyNewProviderScreenshot() { - onData(anything()).inAdapterView(withId(R.id.provider_list)).atPosition(3).perform(click()); - Screengrab.screenshot("ProviderListActivity_addManuallyNewProvider"); - } -} diff --git a/app/src/androidTestNormal/java/se/leap/bitmaskclient/suite/ScreenshotTest.java b/app/src/androidTestNormal/java/se/leap/bitmaskclient/suite/ScreenshotTest.java index 5fa45a95..ec2e7b5c 100644 --- a/app/src/androidTestNormal/java/se/leap/bitmaskclient/suite/ScreenshotTest.java +++ b/app/src/androidTestNormal/java/se/leap/bitmaskclient/suite/ScreenshotTest.java @@ -6,7 +6,7 @@ import androidx.test.filters.LargeTest; import org.junit.runner.RunWith; import org.junit.runners.Suite; -import se.leap.bitmaskclient.base.ProviderSetupTest; +import se.leap.bitmaskclient.ProviderSetupTest; import se.leap.bitmaskclient.base.BitmaskTest; @LargeTest -- cgit v1.2.3