|
| 1 | +package io.pslab.activity; |
| 2 | + |
| 3 | + |
| 4 | +import static androidx.test.espresso.Espresso.onView; |
| 5 | +import static androidx.test.espresso.action.ViewActions.*; |
| 6 | +import static androidx.test.espresso.contrib.RecyclerViewActions.actionOnItemAtPosition; |
| 7 | +import static androidx.test.espresso.matcher.ViewMatchers.*; |
| 8 | +import static org.hamcrest.Matchers.allOf; |
| 9 | +import static org.hamcrest.Matchers.is; |
| 10 | + |
| 11 | +import android.view.View; |
| 12 | +import android.view.ViewGroup; |
| 13 | +import android.view.ViewParent; |
| 14 | + |
| 15 | +import androidx.test.espresso.Espresso; |
| 16 | +import androidx.test.ext.junit.rules.ActivityScenarioRule; |
| 17 | + |
| 18 | +import tools.fastlane.screengrab.FalconScreenshotStrategy; |
| 19 | +import tools.fastlane.screengrab.Screengrab; |
| 20 | +import tools.fastlane.screengrab.locale.LocaleTestRule; |
| 21 | + |
| 22 | +import androidx.test.ext.junit.runners.AndroidJUnit4; |
| 23 | +import androidx.test.filters.LargeTest; |
| 24 | +import androidx.test.rule.GrantPermissionRule; |
| 25 | + |
| 26 | +import org.hamcrest.Description; |
| 27 | +import org.hamcrest.Matcher; |
| 28 | +import org.hamcrest.TypeSafeMatcher; |
| 29 | +import org.junit.ClassRule; |
| 30 | +import org.junit.Rule; |
| 31 | +import org.junit.Test; |
| 32 | +import org.junit.runner.RunWith; |
| 33 | + |
| 34 | +import io.pslab.R; |
| 35 | + |
| 36 | +@LargeTest |
| 37 | +@RunWith(AndroidJUnit4.class) |
| 38 | +public class ScreenshotsTest { |
| 39 | + |
| 40 | + @ClassRule |
| 41 | + public static final LocaleTestRule localeTestRule = new LocaleTestRule(); |
| 42 | + |
| 43 | + @Rule |
| 44 | + public ActivityScenarioRule<MainActivity> mActivityScenarioRule = |
| 45 | + new ActivityScenarioRule<>(MainActivity.class); |
| 46 | + |
| 47 | + @Rule |
| 48 | + public GrantPermissionRule mGrantPermissionRule = |
| 49 | + GrantPermissionRule.grant( |
| 50 | + "android.permission.ACCESS_FINE_LOCATION", |
| 51 | + "android.permission.RECORD_AUDIO", |
| 52 | + "android.permission.WRITE_EXTERNAL_STORAGE"); |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testTakeScreenshot() { |
| 56 | + mActivityScenarioRule.getScenario().onActivity(activity -> Screengrab.setDefaultScreenshotStrategy(new FalconScreenshotStrategy(activity))); |
| 57 | + |
| 58 | + Screengrab.screenshot("dashboard"); |
| 59 | + |
| 60 | + onView( |
| 61 | + allOf(withContentDescription("open_drawer"), |
| 62 | + childAtPosition( |
| 63 | + allOf(withId(R.id.toolbar), |
| 64 | + childAtPosition( |
| 65 | + withClassName(is("com.google.android.material.appbar.AppBarLayout")), |
| 66 | + 0)), |
| 67 | + 1), |
| 68 | + isDisplayed())).perform(click()); |
| 69 | + |
| 70 | + Screengrab.screenshot("drawer"); |
| 71 | + |
| 72 | + Espresso.pressBack(); |
| 73 | + |
| 74 | + onView( |
| 75 | + allOf(withId(R.id.applications_recycler_view), |
| 76 | + childAtPosition( |
| 77 | + withClassName(is("android.widget.RelativeLayout")), |
| 78 | + 0))).perform(actionOnItemAtPosition(7, click())); |
| 79 | + |
| 80 | + Screengrab.screenshot("instrument_accelerometer_view"); |
| 81 | + |
| 82 | + Espresso.pressBack(); |
| 83 | + |
| 84 | + onView( |
| 85 | + allOf(withId(R.id.applications_recycler_view), |
| 86 | + childAtPosition( |
| 87 | + withClassName(is("android.widget.RelativeLayout")), |
| 88 | + 0))).perform(actionOnItemAtPosition(8, click())); |
| 89 | + |
| 90 | + Screengrab.screenshot("instrument_barometer_view"); |
| 91 | + |
| 92 | + Espresso.pressBack(); |
| 93 | + |
| 94 | + onView( |
| 95 | + allOf(withId(R.id.applications_recycler_view), |
| 96 | + childAtPosition( |
| 97 | + withClassName(is("android.widget.RelativeLayout")), |
| 98 | + 0))).perform(actionOnItemAtPosition(1, click())); |
| 99 | + |
| 100 | + Screengrab.screenshot("instrument_multimeter_view"); |
| 101 | + |
| 102 | + Espresso.pressBack(); |
| 103 | + |
| 104 | + onView( |
| 105 | + allOf(withId(R.id.applications_recycler_view), |
| 106 | + childAtPosition( |
| 107 | + withClassName(is("android.widget.RelativeLayout")), |
| 108 | + 0))).perform(actionOnItemAtPosition(2, click())); |
| 109 | + |
| 110 | + Screengrab.screenshot("logic_analyzer_view"); |
| 111 | + |
| 112 | + Espresso.pressBack(); |
| 113 | + |
| 114 | + onView( |
| 115 | + allOf(withContentDescription("More options"), |
| 116 | + childAtPosition( |
| 117 | + childAtPosition( |
| 118 | + withId(R.id.toolbar), |
| 119 | + 2), |
| 120 | + 1), |
| 121 | + isDisplayed())).perform(click()); |
| 122 | + |
| 123 | + onView( |
| 124 | + allOf(withId(me.zhanghai.android.materialprogressbar.R.id.title), withText("Pin Layout Front"), |
| 125 | + childAtPosition( |
| 126 | + childAtPosition( |
| 127 | + withId(R.id.content), |
| 128 | + 0), |
| 129 | + 0), |
| 130 | + isDisplayed())).perform(click()); |
| 131 | + |
| 132 | + Screengrab.screenshot("layout_pin_front"); |
| 133 | + |
| 134 | + onView( |
| 135 | + allOf(withContentDescription("open_drawer"), |
| 136 | + childAtPosition( |
| 137 | + allOf(withId(R.id.toolbar), |
| 138 | + childAtPosition( |
| 139 | + withClassName(is("com.google.android.material.appbar.AppBarLayout")), |
| 140 | + 0)), |
| 141 | + 1), |
| 142 | + isDisplayed())).perform(click()); |
| 143 | + |
| 144 | + onView( |
| 145 | + allOf(withId(R.id.nav_instruments), |
| 146 | + childAtPosition( |
| 147 | + allOf(withId(com.google.android.material.R.id.design_navigation_view), |
| 148 | + childAtPosition( |
| 149 | + withId(R.id.nav_view), |
| 150 | + 0)), |
| 151 | + 1), |
| 152 | + isDisplayed())).perform(click()); |
| 153 | + |
| 154 | + onView( |
| 155 | + allOf(withId(R.id.applications_recycler_view), |
| 156 | + childAtPosition( |
| 157 | + withClassName(is("android.widget.RelativeLayout")), |
| 158 | + 0))).perform(actionOnItemAtPosition(0, click())); |
| 159 | + |
| 160 | + Screengrab.screenshot("oscilloscope_channel_view"); |
| 161 | + |
| 162 | + Espresso.pressBack(); |
| 163 | + } |
| 164 | + |
| 165 | + private static Matcher<View> childAtPosition( |
| 166 | + final Matcher<View> parentMatcher, final int position) { |
| 167 | + |
| 168 | + return new TypeSafeMatcher<View>() { |
| 169 | + @Override |
| 170 | + public void describeTo(Description description) { |
| 171 | + description.appendText("Child at position " + position + " in parent "); |
| 172 | + parentMatcher.describeTo(description); |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public boolean matchesSafely(View view) { |
| 177 | + ViewParent parent = view.getParent(); |
| 178 | + return parent instanceof ViewGroup && parentMatcher.matches(parent) |
| 179 | + && view.equals(((ViewGroup) parent).getChildAt(position)); |
| 180 | + } |
| 181 | + }; |
| 182 | + } |
| 183 | +} |
0 commit comments