|
| 1 | +package com.gimranov.zandy.app; |
| 2 | + |
| 3 | + |
| 4 | +import android.content.SharedPreferences; |
| 5 | +import android.preference.PreferenceManager; |
| 6 | +import android.support.test.espresso.ViewInteraction; |
| 7 | +import android.support.test.rule.ActivityTestRule; |
| 8 | +import android.support.test.runner.AndroidJUnit4; |
| 9 | +import android.test.suitebuilder.annotation.LargeTest; |
| 10 | +import android.view.View; |
| 11 | +import android.view.ViewGroup; |
| 12 | +import android.view.ViewParent; |
| 13 | + |
| 14 | +import com.gimranov.zandy.app.data.Database; |
| 15 | + |
| 16 | +import org.hamcrest.Description; |
| 17 | +import org.hamcrest.Matcher; |
| 18 | +import org.hamcrest.TypeSafeMatcher; |
| 19 | +import org.junit.After; |
| 20 | +import org.junit.Before; |
| 21 | +import org.junit.Rule; |
| 22 | +import org.junit.Test; |
| 23 | +import org.junit.runner.RunWith; |
| 24 | + |
| 25 | +import static android.support.test.InstrumentationRegistry.getTargetContext; |
| 26 | +import static android.support.test.espresso.Espresso.onView; |
| 27 | +import static android.support.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu; |
| 28 | +import static android.support.test.espresso.action.ViewActions.click; |
| 29 | +import static android.support.test.espresso.assertion.ViewAssertions.matches; |
| 30 | +import static android.support.test.espresso.matcher.RootMatchers.withDecorView; |
| 31 | +import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; |
| 32 | +import static android.support.test.espresso.matcher.ViewMatchers.withClassName; |
| 33 | +import static android.support.test.espresso.matcher.ViewMatchers.withId; |
| 34 | +import static android.support.test.espresso.matcher.ViewMatchers.withText; |
| 35 | +import static org.hamcrest.Matchers.allOf; |
| 36 | +import static org.hamcrest.Matchers.is; |
| 37 | +import static org.hamcrest.Matchers.not; |
| 38 | + |
| 39 | +@LargeTest |
| 40 | +@RunWith(AndroidJUnit4.class) |
| 41 | +public class SyncTest { |
| 42 | + |
| 43 | + @Rule |
| 44 | + public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class); |
| 45 | + |
| 46 | + @Before |
| 47 | + public void setUpCredentials() { |
| 48 | + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getTargetContext()); |
| 49 | + preferences.edit() |
| 50 | + .putString("user_id", BuildConfig.TEST_USER_ID) |
| 51 | + .putString("user_key", BuildConfig.TEST_USER_KEY_READONLY) |
| 52 | + .commit(); |
| 53 | + } |
| 54 | + |
| 55 | + @After |
| 56 | + public void clearCredentials() { |
| 57 | + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getTargetContext()); |
| 58 | + preferences.edit().clear().commit(); |
| 59 | + } |
| 60 | + |
| 61 | + @Before |
| 62 | + @After |
| 63 | + public void clearDatabase() { |
| 64 | + Database database = new Database(getTargetContext()); |
| 65 | + database.resetAllData(); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void syncTest() { |
| 70 | + openActionBarOverflowOrOptionsMenu(getTargetContext()); |
| 71 | + |
| 72 | + ViewInteraction textView = onView( |
| 73 | + allOf(withId(android.R.id.title), withText(R.string.menu_sync), |
| 74 | + childAtPosition( |
| 75 | + childAtPosition( |
| 76 | + withClassName(is("com.android.internal.view.menu.ListMenuItemView")), |
| 77 | + 0), |
| 78 | + 0), |
| 79 | + isDisplayed())); |
| 80 | + textView.perform(click()); |
| 81 | + |
| 82 | + onView(withText(R.string.sync_started)) |
| 83 | + .inRoot(withDecorView(not(is(mActivityTestRule.getActivity().getWindow().getDecorView())))) |
| 84 | + .check(matches(isDisplayed())); |
| 85 | + |
| 86 | + ViewInteraction button2 = onView( |
| 87 | + allOf(withId(R.id.itemButton), withText(R.string.view_items), |
| 88 | + childAtPosition( |
| 89 | + allOf(withId(R.id.main), |
| 90 | + childAtPosition( |
| 91 | + withId(android.R.id.content), |
| 92 | + 0)), |
| 93 | + 1), |
| 94 | + isDisplayed())); |
| 95 | + button2.perform(click()); |
| 96 | + } |
| 97 | + |
| 98 | + private static Matcher<View> childAtPosition( |
| 99 | + final Matcher<View> parentMatcher, final int position) { |
| 100 | + |
| 101 | + return new TypeSafeMatcher<View>() { |
| 102 | + @Override |
| 103 | + public void describeTo(Description description) { |
| 104 | + description.appendText("Child at position " + position + " in parent "); |
| 105 | + parentMatcher.describeTo(description); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public boolean matchesSafely(View view) { |
| 110 | + ViewParent parent = view.getParent(); |
| 111 | + return parent instanceof ViewGroup && parentMatcher.matches(parent) |
| 112 | + && view.equals(((ViewGroup) parent).getChildAt(position)); |
| 113 | + } |
| 114 | + }; |
| 115 | + } |
| 116 | +} |
0 commit comments