Issue
I just try to test a simple app which gets the device's location and prints it on a TextView, I use Record Espresso Test option on Android Studio, I tested the runtime permission request dialogs and got a bunch of code, but I haven't been able to resolve com.android.packageinstaller
symbol, I noticed that is used for the request permissions dialog buttons, how can I fix this error?
This is the code generated by the recorder:
import android.support.test.espresso.ViewInteraction;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.hamcrest.core.IsInstanceOf;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf;
@LargeTest
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {
@Rule
public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);
@Test
public void mainActivityTest() {
ViewInteraction button = onView(
allOf(withId(com.android.packageinstaller.R.id.permission_allow_button),
childAtPosition(
allOf(withId(com.android.packageinstaller.R.id.button_group),
childAtPosition(
IsInstanceOf.<View>instanceOf(android.widget.LinearLayout.class),
0)),
1),
isDisplayed()));
button.check(matches(isDisplayed()));
ViewInteraction button2 = onView(
allOf(withId(com.android.packageinstaller.R.id.permission_deny_button),
childAtPosition(
allOf(withId(com.android.packageinstaller.R.id.button_group),
childAtPosition(
IsInstanceOf.<View>instanceOf(android.widget.LinearLayout.class),
0)),
0),
isDisplayed()));
button2.check(matches(isDisplayed()));
ViewInteraction textView = onView(
allOf(withId(R.id.text_view), withText("Your location: 10.910198 / -74.777500"),
childAtPosition(
childAtPosition(
withId(android.R.id.content),
0),
0),
isDisplayed()));
textView.check(matches(withText("Your location: 10.910198 / -74.777500")));
}
private static Matcher<View> childAtPosition(
final Matcher<View> parentMatcher, final int position) {
return new TypeSafeMatcher<View>() {
@Override
public void describeTo(Description description) {
description.appendText("Child at position " + position + " in parent ");
parentMatcher.describeTo(description);
}
@Override
public boolean matchesSafely(View view) {
ViewParent parent = view.getParent();
return parent instanceof ViewGroup && parentMatcher.matches(parent)
&& view.equals(((ViewGroup) parent).getChildAt(position));
}
};
}
}
Solution
After having a poke online, I found that Espresso is not suitable for testing runtime permissions, this is mainly because permission dialogs are from other package and Expresso works only on the current one, however we can use UiAutomator api to get things done, like it is shown in this post: https://blog.egorand.me/testing-runtime-permissions-lessons-learned/
Answered By - glisu
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.