package com.asksven.betterbatterystats; 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.WindowManager; import org.junit.Before; 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.action.ViewActions.*; import static android.support.test.espresso.assertion.ViewAssertions.*; import static android.support.test.espresso.matcher.ViewMatchers.*; import static org.hamcrest.Matchers.allOf; @RunWith(AndroidJUnit4.class) public class ShareActivityTestCancel { @Rule public ActivityTestRule<StatsActivity> mActivityTestRule = new ActivityTestRule<>(StatsActivity.class); @Before public void setUp() { final StatsActivity activity = mActivityTestRule.getActivity(); Runnable wakeUpDevice = new Runnable() { public void run() { activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); } }; activity.runOnUiThread(wakeUpDevice); } @Test public void shareActivityTest() { // 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(2000); } catch (InterruptedException e) { e.printStackTrace(); } ViewInteraction actionMenuItemView = onView( allOf(withId(R.id.share), withContentDescription("Share"), isDisplayed())); actionMenuItemView.perform(click()); // 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(2000); } catch (InterruptedException e) { e.printStackTrace(); } ViewInteraction appCompatButton2 = onView(withText("Cancel")); appCompatButton2.perform(click()); } }