package tools.fastlane.plugin.cordova_screenshots;

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 org.junit.BeforeClass; import org.junit.ClassRule;

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 org.hamcrest.Matchers.allOf;

import tools.fastlane.screengrab.Screengrab; import tools.fastlane.screengrab.UiAutomatorScreenshotStrategy; import tools.fastlane.screengrab.locale.LocaleTestRule;

@LargeTest @RunWith(AndroidJUnit4.class) public class ScreengrabTest {

@ClassRule
public static final LocaleTestRule localeTestRule = new LocaleTestRule();

@Rule
public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);

@BeforeClass
public static void beforeAll() {
  // https://docs.fastlane.tools/getting-started/android/screenshots/#improved-screenshot-capture-with-ui-automator
  Screengrab.setDefaultScreenshotStrategy(new UiAutomatorScreenshotStrategy());
}

@Test
public void screengrabTest() {
  // Added a sleep statement to match the app's execution delay.
  // The recommended way to handle such scenarios is to use Espresso idling resources:
  // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
  try {
    Thread.sleep(10000);
  } catch (InterruptedException e) {
    e.printStackTrace();
  }

  Screengrab.screenshot("0_app-launch");

  /*
  ViewInteraction webView = onView(
    allOf(childAtPosition(
      allOf(withId(android.R.id.content),
        childAtPosition(
          IsInstanceOf.<View>instanceOf(android.widget.LinearLayout.class),
          0)),
      0),
      isDisplayed()));
  webView.check(matches(isDisplayed()));
  */

}

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));
    }
  };
}

}