summaryrefslogtreecommitdiff
path: root/app/src/androidTest/java/utils/ProviderSetupUtils.java
blob: 9b76ffbd306b261bd6cb52252b938162de02a45e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package utils;

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;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withTagValue;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static utils.CustomInteractions.tryResolve;

import android.content.Context;
import android.net.VpnService;

import androidx.test.espresso.NoMatchingViewException;
import androidx.test.espresso.ViewInteraction;
import androidx.test.espresso.matcher.ViewMatchers;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.UiObject;
import androidx.test.uiautomator.UiObjectNotFoundException;
import androidx.test.uiautomator.UiSelector;

import org.hamcrest.Matchers;

import se.leap.bitmaskclient.BuildConfig;
import se.leap.bitmaskclient.R;
import tools.fastlane.screengrab.Screengrab;

public class ProviderSetupUtils {

    public static void runProviderSetup(UiDevice device, boolean takeConfigurationScreenshots, boolean useCircumvention, Context targetContext) {
        try {
            // ------- PROVIDER SELECTION --------------
            if ("normal".equals(BuildConfig.FLAVOR_branding)) {
                System.out.println("next: provider selection");
                ViewInteraction radioButtonSelection = tryResolve(onView(withText("Riseup")), matches(isDisplayed()));
                if (takeConfigurationScreenshots) Screengrab.screenshot("provider_selection");
                radioButtonSelection.perform(click());
                // next button click
                onView(withText(R.string.next)).perform(click());
            }

            // ------- CIRCUMVENTION SELECTION --------------
            System.out.println("next: standard/circumvention selection");
            if (useCircumvention) {
                onView(withText(targetContext.getString(R.string.use_circumvention_tech))).perform(click());
            } else {
                onView(withText(targetContext.getString(R.string.use_standard_vpn, targetContext.getString(R.string.app_name)))).perform(click());
            }
            if (takeConfigurationScreenshots) Screengrab.screenshot("circumvention_selection");

            // ------- CONFIGURATION PROGRESS --------------
            System.out.println("next: configuring");
            onView(withText(R.string.next)).perform(click());
            tryResolve(
                    onView(
                            Matchers.allOf(
                                    withText(R.string.configuring_provider),
                                    withId(R.id.tv_title)
                            )
                    ),
                    matches(isDisplayed())
            );
            if (takeConfigurationScreenshots) Screengrab.screenshot("configuring_provider");

            // ------- VPN PERMISSON DIALOG --------------
            boolean showPermissionDialog = false;
            if (VpnService.prepare(getApplicationContext()) != null) {
                showPermissionDialog = true;
                tryResolve(onView(withText(R.string.upcoming_connection_request_description)), matches(isDisplayed()), useCircumvention ? 180 : 20);
                System.out.println("next: next permission request");
                if (takeConfigurationScreenshots) Screengrab.screenshot("vpn_permission_rationale");
                onView(withText(R.string.next)).perform(click());
                UiObject okButton = device.findObject(new UiSelector().packageName("com.android.vpndialogs").resourceId("android:id/button1"));
                okButton.waitForExists(30000);
                okButton.click();
                device.waitForWindowUpdate("com.android.vpndialogs", 1000);
            }

            // ------- START VPN --------------
            System.out.println("next: perform click on VPN button");
            ViewInteraction interaction = tryResolve(onView(withTagValue(Matchers.is("button_setup_circle_custom"))), matches(isDisplayed()), useCircumvention && !showPermissionDialog ? 180 : 20);
            if (takeConfigurationScreenshots) {
                Screengrab.screenshot("all_set_start_vpn");
            } else {
                // we only want to start the VPN in case we're not running the ProviderSetupTest
                interaction.perform(click());
            }
        } 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();
        } catch (UiObjectNotFoundException | NullPointerException e) {
            e.printStackTrace();
        }
    }
}