summaryrefslogtreecommitdiff
path: root/app/src/androidTest/java/se/leap/bitmaskclient/test/VpnTestController.java
blob: 9dcb4da17dfd5aedd1d327217f9c2cf5ae55a34f (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
package se.leap.bitmaskclient.test;

import android.view.View;
import android.widget.Button;

import com.robotium.solo.Condition;
import com.robotium.solo.Solo;

import de.blinkt.openvpn.activities.DisconnectVPN;
import mbanje.kurt.fabbutton.FabButton;
import mbanje.kurt.fabbutton.ProgressRingView;
import se.leap.bitmaskclient.R;

import static junit.framework.Assert.assertTrue;

public class VpnTestController {

    private final Solo solo;

    public VpnTestController(Solo solo) {
        this.solo = solo;
    }

    protected void turnVpnOndAndOff() {
        clickVpnButton();
        turningEipOn();
        clickVpnButton();
        turningEipOff();
    }

    protected void clickVpnButton() throws IllegalStateException {
        Button button = getVpnButton();
        if(!isVpnButton(button))
            throw new IllegalStateException();
        solo.clickOnView(button);
    }

    protected Button getVpnButton() {
        View button_view = solo.getView(R.id.vpn_main_button);
        if (button_view != null)
            return (Button) button_view;
        else
            return null;
    }

    private boolean isVpnButton(Button button) {
        return button != null && !button.getText().toString().isEmpty();
    }

    protected FabButton getVpnWholeIcon() {
        View view = solo.getView(R.id.vpn_Status_Image);
        if (view != null)
            return (FabButton) view;
        else
            return null;
    }

    protected void turningEipOn() {
        assertInProgress();
        int max_seconds_until_connected = 120;

        Condition condition = new Condition() {
            @Override
            public boolean isSatisfied() {
                return iconShowsConnected();
            }
        };
        assertTrue("condition iconShowsConnected not fulfilled within " + max_seconds_until_connected * 1000 + " seconds." , solo.waitForCondition(condition, max_seconds_until_connected * 1000));
        sleepSeconds(2);
    }

    private void assertInProgress() {
        FabButton whole_icon = getVpnWholeIcon();
        ProgressRingView a;
        a = whole_icon != null ?
                (ProgressRingView) getVpnWholeIcon().findViewById(R.id.fabbutton_ring) :
                new ProgressRingView(solo.getCurrentActivity());
        BaseTestDashboardFragment.isShownWithinConfinesOfVisibleScreen(a);
    }

    private boolean iconShowsConnected() {
        View vpnIconView = getVpnWholeIcon();
        return vpnIconView.getTag().equals(R.drawable.ic_stat_vpn);
    }

    protected boolean iconShowsDisconnected() {
        View vpnIconView = getVpnWholeIcon();
        return vpnIconView.getTag().equals(R.drawable.ic_stat_vpn_offline);
    }

    protected void turningEipOff() {
        okToBrowserWarning();
        sayOkToDisconnect();

        int max_seconds_until_connected = 1;

        Condition condition = new Condition() {
            @Override
            public boolean isSatisfied() {
                return iconShowsDisconnected();
            }
        };
        assertTrue(solo.waitForCondition(condition, max_seconds_until_connected * 1000));
        sleepSeconds(2);
    }

    private void okToBrowserWarning() {
        assertTrue(solo.waitForDialogToOpen());
        clickYes();
    }

    private void clickYes() {
        String yes = solo.getString(android.R.string.yes);
        solo.clickOnText(yes);
    }

    private void sayOkToDisconnect() throws IllegalStateException {
        boolean disconnect_vpn_appeared = solo.waitForActivity(DisconnectVPN.class);
        if(disconnect_vpn_appeared)
            clickYes();
        else throw new IllegalStateException();
    }

    void sleepSeconds(int seconds) {
        solo.sleep(seconds * 1000);
    }
}