From bf797d855bb55f325e2fb647ac0690aabc62772f Mon Sep 17 00:00:00 2001
From: cyBerta <cyberta@riseup.net>
Date: Tue, 11 Apr 2023 02:40:15 +0200
Subject: [PATCH] handle correctly vpn permission dialog

---
 app/build.gradle                              |  1 +
 .../bitmaskclient/base/ProviderBaseTest.java  | 21 ++++++++++++++-----
 .../base/CustomProviderTest.java              | 15 ++++++++++++-
 3 files changed, 31 insertions(+), 6 deletions(-)

diff --git a/app/build.gradle b/app/build.gradle
index a79c97f11..6db759b48 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -410,6 +410,7 @@ dependencies {
   androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
   androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.5.0'
   androidTestImplementation 'androidx.test.espresso:espresso-intents:3.5.0'
+  androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
 
   androidTestImplementation 'tools.fastlane:screengrab:2.1.1'
   testImplementation 'tools.fastlane:screengrab:2.1.1'
diff --git a/app/src/androidTest/java/se/leap/bitmaskclient/base/ProviderBaseTest.java b/app/src/androidTest/java/se/leap/bitmaskclient/base/ProviderBaseTest.java
index bbfcdc8b6..26cd8699a 100644
--- a/app/src/androidTest/java/se/leap/bitmaskclient/base/ProviderBaseTest.java
+++ b/app/src/androidTest/java/se/leap/bitmaskclient/base/ProviderBaseTest.java
@@ -14,22 +14,25 @@ import static androidx.test.espresso.matcher.ViewMatchers.withTagValue;
 import static androidx.test.espresso.matcher.ViewMatchers.withText;
 import static org.hamcrest.Matchers.allOf;
 import static org.hamcrest.Matchers.anything;
-import static org.hamcrest.Matchers.containsString;
-import static org.hamcrest.Matchers.hasToString;
 import static org.hamcrest.Matchers.is;
 import static se.leap.bitmaskclient.base.models.Constants.SHARED_PREFERENCES;
 import static utils.CustomInteractions.tryResolve;
 
+import android.app.Instrumentation;
 import android.content.SharedPreferences;
+import android.net.VpnService;
 import android.view.Gravity;
 
-import androidx.test.espresso.DataInteraction;
-import androidx.test.espresso.NoMatchingViewException;
 import androidx.test.espresso.ViewInteraction;
 import androidx.test.espresso.contrib.DrawerActions;
 import androidx.test.ext.junit.rules.ActivityScenarioRule;
 import androidx.test.ext.junit.runners.AndroidJUnit4;
 import androidx.test.filters.LargeTest;
+import androidx.test.platform.app.InstrumentationRegistry;
+import androidx.test.uiautomator.UiDevice;
+import androidx.test.uiautomator.UiObject;
+import androidx.test.uiautomator.UiObjectNotFoundException;
+import androidx.test.uiautomator.UiSelector;
 
 import org.junit.Before;
 import org.junit.ClassRule;
@@ -56,15 +59,18 @@ public abstract class ProviderBaseTest {
     public ActivityScenarioRule<StartActivity> mActivityScenarioRule =
             new ActivityScenarioRule<>(StartActivity.class);
 
+    UiDevice device;
     @Before
     public void setup() {
         Screengrab.setDefaultScreenshotStrategy(new UiAutomatorScreenshotStrategy());
         SharedPreferences preferences = getApplicationContext().getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE);
         preferences.edit().clear().commit();
+        Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
+        device = UiDevice.getInstance(instrumentation);
     }
 
     @Test
-    public void test01_vpnStartTest() throws InterruptedException {
+    public void test01_vpnStartTest() throws InterruptedException, UiObjectNotFoundException {
         boolean configurationNeeded = configureProviderIfNeeded();
 
         ViewInteraction mainButtonStop;
@@ -87,6 +93,11 @@ public abstract class ProviderBaseTest {
                     20);
             Screengrab.screenshot("VPN_connected");
         } else {
+            // handle VPN permission dialog
+            if (VpnService.prepare(getApplicationContext()) != null) {
+                UiObject okButton = device.findObject(new UiSelector().packageName("com.android.vpndialogs").resourceId("android:id/button1"));
+                okButton.click();
+            }
             // on new configurations the VPN is automatically started
             Screengrab.screenshot("VPN_connecting");
             mainButtonStop = tryResolve(
diff --git a/app/src/androidTestCustom/java/se/leap/bitmaskclient/base/CustomProviderTest.java b/app/src/androidTestCustom/java/se/leap/bitmaskclient/base/CustomProviderTest.java
index 1a0814b6f..92416af46 100644
--- a/app/src/androidTestCustom/java/se/leap/bitmaskclient/base/CustomProviderTest.java
+++ b/app/src/androidTestCustom/java/se/leap/bitmaskclient/base/CustomProviderTest.java
@@ -1,5 +1,6 @@
 package se.leap.bitmaskclient.base;
 
+import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
 import static androidx.test.espresso.Espresso.onView;
 import static androidx.test.espresso.action.ViewActions.click;
 import static androidx.test.espresso.assertion.ViewAssertions.matches;
@@ -9,7 +10,12 @@ import static androidx.test.espresso.matcher.ViewMatchers.withId;
 import static androidx.test.espresso.matcher.ViewMatchers.withText;
 import static utils.CustomInteractions.tryResolve;
 
+import android.net.VpnService;
+
 import androidx.test.espresso.ViewInteraction;
+import androidx.test.uiautomator.UiObject;
+import androidx.test.uiautomator.UiObjectNotFoundException;
+import androidx.test.uiautomator.UiSelector;
 
 import org.junit.Test;
 
@@ -20,7 +26,14 @@ public class CustomProviderTest extends ProviderBaseTest {
 
     @Test
     @Override
-    public void test01_vpnStartTest() throws InterruptedException {
+    public void test01_vpnStartTest() throws InterruptedException, UiObjectNotFoundException {
+        // handle VPN permission dialog
+        if (VpnService.prepare(getApplicationContext()) != null) {
+            UiObject okButton = device.findObject(new UiSelector().packageName("com.android.vpndialogs").resourceId("android:id/button1"));
+            okButton.waitForExists(30000);
+            okButton.click();
+        }
+
         ViewInteraction mainButtonStop;
         mainButtonStop = tryResolve(
                 onView(withId(R.id.main_button)),
-- 
GitLab