summaryrefslogtreecommitdiff
path: root/app/src/androidTest/java/utils/CustomInteractions.java
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/androidTest/java/utils/CustomInteractions.java')
-rw-r--r--app/src/androidTest/java/utils/CustomInteractions.java82
1 files changed, 82 insertions, 0 deletions
diff --git a/app/src/androidTest/java/utils/CustomInteractions.java b/app/src/androidTest/java/utils/CustomInteractions.java
new file mode 100644
index 00000000..9e3a8f9d
--- /dev/null
+++ b/app/src/androidTest/java/utils/CustomInteractions.java
@@ -0,0 +1,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);
+ }
+}