summaryrefslogtreecommitdiff
path: root/app/src/androidTest/legacy/UserStatusTestController.java
blob: 3b0daf9a77ef12d551a05d9a1e5c6ecd49e5dd84 (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
package se.leap.bitmaskclient.test;

import android.view.*;

import com.robotium.solo.*;

import se.leap.bitmaskclient.R;

import static junit.framework.Assert.assertTrue;

public class UserStatusTestController {
    private final Solo solo;

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

    void clickUserSessionButton() {
        solo.clickOnView(getUserSessionButton());
    }

    View getUserSessionButton() throws IllegalStateException {
        View view = solo.getView(R.id.user_status_button);
        if(view == null)
            throw new IllegalStateException();

        return view;
    }

    void logIn(String username, String password, boolean expectSuccess) {
        solo.enterText(0, username);
        solo.enterText(1, password);
        // https://stackoverflow.com/questions/33560746/how-to-test-alertdialog-item-click-in-robotium-for-android-studio
        solo.clickOnView(solo.getView(android.R.id.button1));
        assertTrue(solo.waitForDialogToClose());

        if (expectSuccess) {
            assertLoggedIn();
        } else {
            solo.waitForText(solo.getString(R.string.cancel));
            assertTrue(solo.waitForText(solo.getString(R.string.login_button)));
        }
    }

    void logIn(String username, String password) {
       logIn(username, password, true);
    }

    private void assertLoggedIn() {
        String log_out = solo.getString(R.string.logout_button);
        assertTrue(solo.waitForText(log_out));
    }

    void assertLoggedOut() {
        String log_in = solo.getString(R.string.login_button);
        assertTrue(solo.waitForText(log_in));
    }

    void logOut() {
        assertLoggedIn();
        clickUserSessionButton();

        solo.clickOnActionBarItem(R.string.logout_button);
        solo.waitForDialogToClose();
        assertLoggedOut();
    }

    boolean assertErrorLogInDialogAppears() {
        assertTrue(solo.waitForDialogToOpen());

        String username_hint = solo.getEditText(0).getHint().toString();
        String correct_username_hint = solo.getString(R.string.username_hint);
        String password_hint = solo.getEditText(1).getHint().toString();
        String correct_password_hint = solo.getString(R.string.password_hint);
        String user_message = solo.getText(0).toString();
        String riseup_user_message = solo.getString(R.string.login_riseup_warning);

        return username_hint.equalsIgnoreCase(correct_username_hint)
                && password_hint.equalsIgnoreCase(correct_password_hint)
                && !user_message.equalsIgnoreCase(riseup_user_message)
                && !user_message.isEmpty();
    }
}