summaryrefslogtreecommitdiff
path: root/app/src/androidTest/java/se/leap/bitmaskclient/base/ProviderBaseTest.java
blob: 68676bee00e993e8a0c7573a5d850e56429125f1 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package se.leap.bitmaskclient.base;

import static androidx.test.core.app.ApplicationProvider.getApplicationContext;
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.assertion.ViewAssertions.matches;
import static androidx.test.espresso.contrib.DrawerMatchers.isClosed;
import static androidx.test.espresso.matcher.RootMatchers.isDialog;
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 org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.anything;
import static org.hamcrest.Matchers.is;
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.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;
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.utils.PreferenceHelper;
import tools.fastlane.screengrab.Screengrab;
import tools.fastlane.screengrab.UiAutomatorScreenshotStrategy;
import tools.fastlane.screengrab.locale.LocaleTestRule;

@LargeTest
@RunWith(AndroidJUnit4.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public abstract class ProviderBaseTest {

    @ClassRule
    public static final LocaleTestRule localeTestRule = new LocaleTestRule();

    @Rule
    public ActivityScenarioRule<StartActivity> mActivityScenarioRule =
            new ActivityScenarioRule<>(StartActivity.class);

    UiDevice device;
    @Before
    public void setup() {
        Screengrab.setDefaultScreenshotStrategy(new UiAutomatorScreenshotStrategy());
        SharedPreferences preferences = PreferenceHelper.getSharedPreferences(getApplicationContext());
        preferences.edit().clear().commit();
        Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
        device = UiDevice.getInstance(instrumentation);
    }

    @Test
    public void test01_vpnStartTest() throws InterruptedException, UiObjectNotFoundException {
        boolean configurationNeeded = configureProviderIfNeeded();

        ViewInteraction mainButtonStop;
        if (!configurationNeeded) {
            // click on Main on/off button and start VPN
            ViewInteraction mainButton = tryResolve(
                    onView(withId(R.id.main_button)),
                    matches(isDisplayed())
            );

            mainButton.perform(click());
            Thread.sleep(50);
            Screengrab.screenshot("VPN_connecting");

            mainButtonStop = tryResolve(
                    onView(allOf(
                            withId(R.id.button),
                            withTagValue(is("button_circle_stop")))),
                    matches(isDisplayed()),
                    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(
                    onView(allOf(
                            withId(R.id.button),
                            withTagValue(is("button_circle_stop")))),
                    matches(isDisplayed()),
                    20);
            Screengrab.screenshot("VPN_connected");
        }

        mainButtonStop.perform(click());
        Screengrab.screenshot("VPN_ask_disconnect");

        onView(withText(android.R.string.yes))
                .inRoot(isDialog())
                .check(matches(isDisplayed()))
                .perform(click());
        Screengrab.screenshot("VPN_disconnected");
    }

    @Test
    public void test02_SettingsFragmentScreenshots() {
        onView(withId(R.id.drawer_layout))
                .check(matches(isClosed(Gravity.LEFT))) // Left Drawer should be closed.
                .perform(DrawerActions.open()); // Open Drawer

        Screengrab.screenshot("navigationDrawer");

        // Start the screen of your activity.
        onView(withId(R.id.advancedSettings))
                .perform(click());

        Screengrab.screenshot("settingsFragment");
    }

    @Test
    public void test03_LocationSelectionFragmentScreenshots() {
        onView(withId(R.id.drawer_layout))
                .check(matches(isClosed(Gravity.LEFT))) // Left Drawer should be closed.
                .perform(DrawerActions.open()); // Open Drawer

        onView(withId(R.id.manualGatewaySelection))
                .perform(click());

        Screengrab.screenshot("GatewaySelectionFragment");
    }

    @Test
    public void test04_AppExclusionFragmentScreenshots() {
        onView(withId(R.id.drawer_layout))
                .check(matches(isClosed(Gravity.LEFT))) // Left Drawer should be closed.
                .perform(DrawerActions.open()); // Open Drawer

        onView(withId(R.id.advancedSettings)).perform(click());

        onView(withId(R.id.exclude_apps)).perform(click());

        tryResolve(
                onData(anything()).inAdapterView(withId(android.R.id.list)).atPosition(2),
                matches(isDisplayed()),
                5);

        Screengrab.screenshot("App_Exclusion_Fragment");
    }

    public abstract boolean configureProviderIfNeeded();
}