summaryrefslogtreecommitdiff
path: root/app/src/androidTest/java/utils/CustomInteractions.java
blob: 9e3a8f9d2a245c7dee7ebd0265af97563a5abfa7 (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
package utils;

import androidx.annotation.Nullable;
import androidx.test.espresso.DataInteraction;
import androidx.test.espresso.NoMatchingViewException;
import androidx.test.espresso.ViewAssertion;
import androidx.test.espresso.ViewInteraction;

public class CustomInteractions {

    public static @Nullable
    ViewInteraction tryResolve(ViewInteraction viewInteraction, int maxTries) {
        return tryResolve(viewInteraction, null, maxTries);
    }

    public static @Nullable
    ViewInteraction tryResolve(ViewInteraction viewInteraction, ViewAssertion assertion) {
        return tryResolve(viewInteraction, assertion, 10);
    }

    public static @Nullable ViewInteraction tryResolve(ViewInteraction viewInteraction, ViewAssertion assertion, int maxTries) {
        ViewInteraction resolvedViewInteraction = null;
        int attempt = 0;
        boolean hasFound = false;
        while (!hasFound && attempt < maxTries) {
            try {
                resolvedViewInteraction = viewInteraction;
                if (assertion != null) {
                    resolvedViewInteraction.check(assertion);
                }
                hasFound = true;
            } catch (NoMatchingViewException exception) {
                System.out.println("NoMatchingViewException attempt: " + attempt);
                exception.printStackTrace();
                attempt++;
                if (attempt == maxTries) {
                    throw exception;
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    break;
                }
            }
        }
        return resolvedViewInteraction;
    }

    public static @Nullable
    DataInteraction tryResolve(DataInteraction dataInteraction, ViewAssertion assertion, int maxTries) {
        DataInteraction resolvedDataInteraction = null;
        int attempt = 0;
        boolean hasFound = false;
        while (!hasFound && attempt < maxTries) {
            try {
                resolvedDataInteraction = dataInteraction;
                if (assertion != null) {
                    resolvedDataInteraction.check(assertion);
                }
                hasFound = true;
            } catch (Exception exception) {
                // TODO: specify expected exception
                attempt++;
                if (attempt == maxTries) {
                    throw exception;
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    break;
                }
            }
        }
        return resolvedDataInteraction;
    }
    public static @Nullable
    DataInteraction tryResolve(DataInteraction dataInteraction, int maxTries) {
        return tryResolve(dataInteraction, null, maxTries);
    }
}