-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
134 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
app/src/androidTest/java/net/osmtracker/util/LogcatHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package net.osmtracker.util; | ||
|
||
import android.util.Log; | ||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.util.regex.Pattern; | ||
|
||
public class LogcatHelper { | ||
|
||
public static boolean checkLogForMessage(String tag, String message) { | ||
try { | ||
Process process = Runtime.getRuntime().exec("logcat -d " + tag + ":I *:S"); | ||
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); | ||
|
||
String line; | ||
Pattern pattern = Pattern.compile(".*\\b" + Pattern.quote(message) + "\\b.*"); | ||
|
||
while ((line = bufferedReader.readLine()) != null) { | ||
if (pattern.matcher(line).matches()) { | ||
return true; | ||
} | ||
} | ||
} catch (Exception e) { | ||
Log.e("LogcatHelper", "Error reading logcat output", e); | ||
} | ||
|
||
return false; | ||
} | ||
} |
63 changes: 62 additions & 1 deletion
63
app/src/androidTest/java/net/osmtracker/util/WaitForView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,65 @@ | ||
package net.osmtracker.util; | ||
|
||
public class WaitForView { | ||
import android.view.View; | ||
import androidx.test.espresso.PerformException; | ||
import androidx.test.espresso.UiController; | ||
import androidx.test.espresso.ViewAction; | ||
import androidx.test.espresso.matcher.ViewMatchers; | ||
import androidx.test.espresso.util.HumanReadables; | ||
import androidx.test.espresso.util.TreeIterables; | ||
|
||
import org.hamcrest.Matcher; | ||
|
||
import java.util.concurrent.TimeoutException; | ||
|
||
public class WaitForView implements ViewAction { | ||
private final int viewId; | ||
private final long timeout; | ||
|
||
/** | ||
* This ViewAction tells espresso to wait till a certain view is found in the view hierarchy. | ||
* @param viewId The id of the view to wait for. | ||
* @param timeout The maximum time which espresso will wait for the view to show up (in milliseconds) | ||
*/ | ||
public WaitForView(int viewId, long timeout) { | ||
this.viewId = viewId; | ||
this.timeout = timeout; | ||
} | ||
|
||
@Override | ||
public Matcher<View> getConstraints() { | ||
return ViewMatchers.isRoot(); | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "wait for a specific view with id " + viewId + " during " + timeout + " millis."; | ||
} | ||
|
||
@Override | ||
public void perform(UiController uiController, View rootView) { | ||
uiController.loopMainThreadUntilIdle(); | ||
long startTime = System.currentTimeMillis(); | ||
long endTime = startTime + timeout; | ||
Matcher<View> viewMatcher = ViewMatchers.withId(viewId); | ||
|
||
do { | ||
for (View child : TreeIterables.breadthFirstViewTraversal(rootView)) { | ||
if (viewMatcher.matches(child)) { | ||
return; | ||
} | ||
} | ||
uiController.loopMainThreadForAtLeast(100); | ||
} while (System.currentTimeMillis() < endTime); | ||
|
||
throw new PerformException.Builder() | ||
.withCause(new TimeoutException()) | ||
.withActionDescription(this.getDescription()) | ||
.withViewDescription(HumanReadables.describe(rootView)) | ||
.build(); | ||
} | ||
|
||
public static ViewAction waitForView(final int viewId, final long timeout) { | ||
return new WaitForView(viewId, timeout); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters