Skip to content

Commit

Permalink
androidTest: test for TOAST in logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Binnette committed Nov 25, 2024
1 parent cbb4fb5 commit aca3524
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
package net.osmtracker.layouts;

import static androidx.test.espresso.Espresso.onData;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.scrollTo;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.PreferenceMatchers.withTitleText;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.isRoot;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
import static junit.framework.TestCase.fail;
import static net.osmtracker.util.LogcatHelper.checkLogForMessage;
import static net.osmtracker.util.WaitForView.waitForView;

import android.Manifest;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import androidx.test.espresso.Espresso;
import androidx.test.espresso.assertion.ViewAssertions;
import androidx.test.rule.ActivityTestRule;
import androidx.test.rule.GrantPermissionRule;

import net.osmtracker.OSMTracker;
import net.osmtracker.R;
Expand All @@ -18,19 +35,19 @@

import java.util.Locale;

import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
import static androidx.test.espresso.Espresso.onData;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.scrollTo;
import static androidx.test.espresso.matcher.PreferenceMatchers.withTitleText;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static junit.framework.TestCase.fail;

public class DownloadLayoutTest {

private final int WAIT_VIEW_TIMEOUT = 5000;

@Rule
public GrantPermissionRule fineLocationPermission = GrantPermissionRule.grant(Manifest.permission.ACCESS_FINE_LOCATION);
@Rule
public GrantPermissionRule coarseLocationPermission = GrantPermissionRule.grant(Manifest.permission.ACCESS_COARSE_LOCATION);
@Rule
public GrantPermissionRule backgroundLocationPermission = GrantPermissionRule.grant(Manifest.permission.ACCESS_BACKGROUND_LOCATION);
@Rule
public GrantPermissionRule serviceLocationPermission = GrantPermissionRule.grant(Manifest.permission.FOREGROUND_SERVICE_LOCATION);

@Rule
public ActivityTestRule<TrackManager> mRule = new ActivityTestRule(TrackManager.class) {
@Override
Expand Down Expand Up @@ -71,13 +88,16 @@ public void deleteLayoutsDirectory(){
/**
* Assuming being in TrackManager
*/
public void navigateToAvailableLayouts(){
public void navigateToAvailableLayouts() {
// Open options menu in the Action Bar
openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext());

// Click on "Settings" in this menu
onView(withText(TestUtils.getStringResource(R.string.menu_settings))).perform(click());

// Click on "Buttons presets" settings
onData(withTitleText(TestUtils.getStringResource(R.string.prefs_ui_buttons_layout))).perform(scrollTo(), click());

// Wait for "+" to be visible
onView(isRoot()).perform(waitForView(R.id.launch_available, WAIT_VIEW_TIMEOUT));
// Perform a click action on the "+" button
onView(withId(R.id.launch_available)).perform(click());
}

Expand All @@ -91,7 +111,7 @@ private void makePostDownloadAssertions(String layoutName) {
Espresso.pressBack();

// Check the layout appears as a new option in AvailableLayouts
onView(withText(layoutName.toLowerCase())).check(ViewAssertions.matches(isDisplayed()));
onView(withText(layoutName.toLowerCase())).check(matches(isDisplayed()));

// Select the layout
onView(withText(layoutName.toLowerCase())).perform(click());
Expand All @@ -104,7 +124,7 @@ private void makePostDownloadAssertions(String layoutName) {
// Check the buttons are loaded correctly
String expectedButtonsLabels[] = new String[]{"A", "B", "C"};
for(String label : expectedButtonsLabels)
onView(withText(label)).check(ViewAssertions.matches(isDisplayed()));
onView(withText(label)).check(matches(isDisplayed()));

}

Expand All @@ -120,6 +140,8 @@ private void clickButtonsToDownloadLayout(String layoutName) {
onView(withText(TestUtils.getStringResource(R.string.available_layouts_description_dialog_positive_confirmation))).
perform(click());

checkLogForMessage("TOAST", TestUtils.getStringResource(R.string.available_layouts_successful_download));

TestUtils.checkToastIsShownWith(TestUtils.getStringResource(R.string.available_layouts_successful_download));
}
}
29 changes: 29 additions & 0 deletions app/src/androidTest/java/net/osmtracker/util/LogcatHelper.java
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 app/src/androidTest/java/net/osmtracker/util/WaitForView.java
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -492,9 +492,11 @@ protected void onPostExecute(Boolean status){
String message="";
if (status) {
message = getResources().getString(R.string.available_layouts_successful_download);
Log.i("TOAST", message);
}
else {
message = getResources().getString(R.string.available_layouts_unsuccessful_download);
Log.e("TOAST", message);
}
Toast.makeText(getApplicationContext(),message,Toast.LENGTH_LONG).show();
dialog.dismiss();
Expand Down

0 comments on commit aca3524

Please sign in to comment.