package net.divlight.retainer.sample;

import android.content.Context;
import android.os.RemoteException;
import android.support.test.filters.LargeTest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.UiDevice;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

@RunWith(AndroidJUnit4.class)
@LargeTest
public class MainActivityTest {
    private Context context;
    private UiDevice device;

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

    @Before
    public void setUp() throws RemoteException {
        context = getInstrumentation().getTargetContext();
        device = UiDevice.getInstance(getInstrumentation());
        device.unfreezeRotation();
    }

    @Test
    public void testRetainByActivity() throws RemoteException {
        onView(withId(R.id.text_view)).check(matches(withText(context.getString(R.string.value_format, 1))));

        device.setOrientationLeft();
        device.waitForWindowUpdate(null, 2000);
        onView(withId(R.id.text_view)).check(matches(withText(context.getString(R.string.value_format, 2))));

        device.setOrientationNatural();
        device.waitForWindowUpdate(null, 2000);
        onView(withId(R.id.text_view)).check(matches(withText(context.getString(R.string.value_format, 3))));

        device.setOrientationRight();
        device.waitForWindowUpdate(null, 2000);
        onView(withId(R.id.text_view)).check(matches(withText(context.getString(R.string.value_format, 4))));

        device.setOrientationNatural();
        device.waitForWindowUpdate(null, 2000);
        onView(withId(R.id.text_view)).check(matches(withText(context.getString(R.string.value_format, 5))));
    }

    @Test
    public void testRetainByDialogFragment() throws RemoteException {
        onView(withId(R.id.button)).perform(click());
        device.waitForWindowUpdate(null, 2000);
        onView(withId(android.R.id.message)).check(matches(withText(context.getString(R.string.value_format, 1))));

        device.setOrientationLeft();
        device.waitForWindowUpdate(null, 2000);
        onView(withId(android.R.id.message)).check(matches(withText(context.getString(R.string.value_format, 2))));

        device.setOrientationNatural();
        device.waitForWindowUpdate(null, 2000);
        onView(withId(android.R.id.message)).check(matches(withText(context.getString(R.string.value_format, 3))));

        device.setOrientationRight();
        device.waitForWindowUpdate(null, 2000);
        onView(withId(android.R.id.message)).check(matches(withText(context.getString(R.string.value_format, 4))));

        device.setOrientationNatural();
        device.waitForWindowUpdate(null, 2000);
        onView(withId(android.R.id.message)).check(matches(withText(context.getString(R.string.value_format, 5))));

        device.pressBack();
        device.waitForWindowUpdate(null, 2000);

        onView(withId(R.id.button)).perform(click());
        device.waitForWindowUpdate(null, 2000);
        onView(withId(android.R.id.message)).check(matches(withText(context.getString(R.string.value_format, 1))));
    }
}


@@ -913,16 +913,86 @@
 yTest %7B%0A
+    private static final long WAIT_FOR_WINDOW_UPDATE_TIMEOUT = 5000;%0A%0A
     priv
@@ -1636,36 +1636,92 @@
 dowUpdate(null, 
-2000
+WAIT_FOR_WINDOW_UPDATE_TIMEOUT);%0A        device.waitForIdle(
 );%0A        onVie
@@ -1891,36 +1891,92 @@
 dowUpdate(null, 
-2000
+WAIT_FOR_WINDOW_UPDATE_TIMEOUT);%0A        device.waitForIdle(
 );%0A        onVie
@@ -2144,36 +2144,92 @@
 dowUpdate(null, 
-2000
+WAIT_FOR_WINDOW_UPDATE_TIMEOUT);%0A        device.waitForIdle(
 );%0A        onVie
@@ -2399,36 +2399,92 @@
 dowUpdate(null, 
-2000
+WAIT_FOR_WINDOW_UPDATE_TIMEOUT);%0A        device.waitForIdle(
 );%0A        onVie
@@ -2754,36 +2754,92 @@
 dowUpdate(null, 
-2000
+WAIT_FOR_WINDOW_UPDATE_TIMEOUT);%0A        device.waitForIdle(
 );%0A        onVie
@@ -3012,36 +3012,92 @@
 dowUpdate(null, 
-2000
+WAIT_FOR_WINDOW_UPDATE_TIMEOUT);%0A        device.waitForIdle(
 );%0A        onVie
@@ -3273,36 +3273,92 @@
 dowUpdate(null, 
-2000
+WAIT_FOR_WINDOW_UPDATE_TIMEOUT);%0A        device.waitForIdle(
 );%0A        onVie
@@ -3532,36 +3532,92 @@
 dowUpdate(null, 
-2000
+WAIT_FOR_WINDOW_UPDATE_TIMEOUT);%0A        device.waitForIdle(
 );%0A        onVie
@@ -3793,36 +3793,92 @@
 dowUpdate(null, 
-2000
+WAIT_FOR_WINDOW_UPDATE_TIMEOUT);%0A        device.waitForIdle(
 );%0A        onVie
@@ -4050,20 +4050,76 @@
 e(null, 
-2000
+WAIT_FOR_WINDOW_UPDATE_TIMEOUT);%0A        device.waitForIdle(
 );%0A%0A    
@@ -4213,12 +4213,68 @@
 ll, 
-2000
+WAIT_FOR_WINDOW_UPDATE_TIMEOUT);%0A        device.waitForIdle(
 );%0A 

Modify MainActivityTest to optimize stability in a poor environment