// Path: app/src/mock/java/com/ironflowers/firebasetest/auth/FakeAuthConnector.java
// public class FakeAuthConnector implements AuthConnector {
// 
//     private static boolean fakeSuccess = false;
// 
//     @Inject
//     public FakeAuthConnector() {
//     }
// 
//     public static void setFakeSuccess(boolean fakeSuccess) {
//         FakeAuthConnector.fakeSuccess = fakeSuccess;
//     }
// 
//     @Override
//     public Task<AuthResult> signInAnonymously() {
//         return new FakeAuthTask(fakeSuccess);
//     }
// 
//     @Override
//     public boolean isSignedIn() {
//         return false;
//     }
// }
// 
// Path: app/src/sharedTest/java/com/ironflowers/firebasetest/util/MyTestUtils.java
// public class MyTestUtils {
// 
//     /**
//      * Signs the user in on the firebase backend, for testing. Only returns when the sign in
//      * finished (failed or success).
//      */
//     public static void signInForTesting() throws InterruptedException {
// 
//         Task task = FirebaseAuth.getInstance().signInAnonymously();
// 
//         /*
//          * Because there are no views involved, we cannot use the IdlingRegistry here. We will wait
//          * on the sign in call to stop this method from returning until the login finished.
//          */
//         while (!task.isComplete()) {
//             Thread.sleep(100);
//         }
//     }
// 
//     /**
//      * Signs the user out from the firebase backend, for testing.
//      */
//     public static void signOutForTesting() {
//         FirebaseAuth.getInstance().signOut();
//     }
// 
//     public static ContentItem createFakeContentItem(int id) {
//         ContentItem item = new ContentItem();
//         item.setId(id);
//         item.setTitle("Title " + id);
//         item.setDescription("Description " + id);
//         return item;
//     }
// }
// 
// Path: app/src/main/java/com/ironflowers/firebasetest/utils/rx/EspressoIdlingResource.java
// public class EspressoIdlingResource {
// 
//     private static final String RESOURCE = "GLOBAL";
// 
//     private static SimpleCountingIdlingResource mCountingIdlingResource =
//             new SimpleCountingIdlingResource(RESOURCE);
// 
//     public static void increment() {
//         mCountingIdlingResource.increment();
//     }
// 
//     public static void decrement() {
//         mCountingIdlingResource.decrement();
//     }
// 
//     public static IdlingResource getIdlingResource() {
//         return mCountingIdlingResource;
//     }
// }


import android.content.Intent;
import android.support.test.espresso.Espresso;
import android.support.test.espresso.IdlingRegistry;
import android.support.test.filters.LargeTest;
import android.support.test.rule.ActivityTestRule;
import com.google.firebase.auth.FirebaseAuth;
import com.ironflowers.firebasetest.R;
import com.ironflowers.firebasetest.auth.FakeAuthConnector;
import com.ironflowers.firebasetest.util.MyTestUtils;
import com.ironflowers.firebasetest.utils.rx.EspressoIdlingResource;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist;
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.not;
        Espresso.onView(withId(R.id.buttonContinue)).check(doesNotExist());
        // home screen should be opened:
        Espresso.onView(withId(R.id.recyclerContent)).check(matches(isDisplayed()));
    }

    @Test
    @SuppressWarnings("ConstantConditions")
    public void signInFailure_ShowsErrorMessage() throws Exception {

        FirebaseAuth.getInstance().signOut();
        FakeAuthConnector.setFakeSuccess(false);

        createActivity();

        // error message should be showing:
        Espresso.onView(withId(R.id.progressBar)).check(matches(not(isDisplayed())));
        Espresso.onView(withId(R.id.textMessage)).check(matches(isDisplayed()));
        Espresso.onView(withId(R.id.buttonContinue)).check(matches(isDisplayed()));

        // double check that the home screen is not shown:
        Espresso.onView(withId(R.id.recyclerContent)).check(doesNotExist());
    }

    @After
    public void unregisterIdlingResource() {
        IdlingRegistry.getInstance().unregister(EspressoIdlingResource.getIdlingResource());
    }

    @After
    public void signOut() {
// Path: app/src/mock/java/com/ironflowers/firebasetest/auth/FakeAuthConnector.java
// public class FakeAuthConnector implements AuthConnector {
// 
//     private static boolean fakeSuccess = false;
// 
//     @Inject
//     public FakeAuthConnector() {
//     }
// 
//     public static void setFakeSuccess(boolean fakeSuccess) {
//         FakeAuthConnector.fakeSuccess = fakeSuccess;
//     }
// 
//     @Override
//     public Task<AuthResult> signInAnonymously() {
//         return new FakeAuthTask(fakeSuccess);
//     }
// 
//     @Override
//     public boolean isSignedIn() {
//         return false;
//     }
// }
// 
// Path: app/src/sharedTest/java/com/ironflowers/firebasetest/util/MyTestUtils.java
// public class MyTestUtils {
// 
//     /**
//      * Signs the user in on the firebase backend, for testing. Only returns when the sign in
//      * finished (failed or success).
//      */
//     public static void signInForTesting() throws InterruptedException {
// 
//         Task task = FirebaseAuth.getInstance().signInAnonymously();
// 
//         /*
//          * Because there are no views involved, we cannot use the IdlingRegistry here. We will wait
//          * on the sign in call to stop this method from returning until the login finished.
//          */
//         while (!task.isComplete()) {
//             Thread.sleep(100);
//         }
//     }
// 
//     /**
//      * Signs the user out from the firebase backend, for testing.
//      */
//     public static void signOutForTesting() {
//         FirebaseAuth.getInstance().signOut();
//     }
// 
//     public static ContentItem createFakeContentItem(int id) {
//         ContentItem item = new ContentItem();
//         item.setId(id);
//         item.setTitle("Title " + id);
//         item.setDescription("Description " + id);
//         return item;
//     }
// }
// 
// Path: app/src/main/java/com/ironflowers/firebasetest/utils/rx/EspressoIdlingResource.java
// public class EspressoIdlingResource {
// 
//     private static final String RESOURCE = "GLOBAL";
// 
//     private static SimpleCountingIdlingResource mCountingIdlingResource =
//             new SimpleCountingIdlingResource(RESOURCE);
// 
//     public static void increment() {
//         mCountingIdlingResource.increment();
//     }
// 
//     public static void decrement() {
//         mCountingIdlingResource.decrement();
//     }
// 
//     public static IdlingResource getIdlingResource() {
//         return mCountingIdlingResource;
//     }
// }


// Path: app/src/androidTestMock/java/com/ironflowers/firebasetest/ui/signin/SignInScreenTest.java
import android.content.Intent;
import android.support.test.espresso.Espresso;
import android.support.test.espresso.IdlingRegistry;
import android.support.test.filters.LargeTest;
import android.support.test.rule.ActivityTestRule;
import com.google.firebase.auth.FirebaseAuth;
import com.ironflowers.firebasetest.R;
import com.ironflowers.firebasetest.auth.FakeAuthConnector;
import com.ironflowers.firebasetest.util.MyTestUtils;
import com.ironflowers.firebasetest.utils.rx.EspressoIdlingResource;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist;
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.not;

        Espresso.onView(withId(R.id.buttonContinue)).check(doesNotExist());
        // home screen should be opened:
        Espresso.onView(withId(R.id.recyclerContent)).check(matches(isDisplayed()));
    }

    @Test
    @SuppressWarnings("ConstantConditions")
    public void signInFailure_ShowsErrorMessage() throws Exception {

        FirebaseAuth.getInstance().signOut();
        FakeAuthConnector.setFakeSuccess(false);

        createActivity();

        // error message should be showing:
        Espresso.onView(withId(R.id.progressBar)).check(matches(not(isDisplayed())));
        Espresso.onView(withId(R.id.textMessage)).check(matches(isDisplayed()));
        Espresso.onView(withId(R.id.buttonContinue)).check(matches(isDisplayed()));

        // double check that the home screen is not shown:
        Espresso.onView(withId(R.id.recyclerContent)).check(doesNotExist());
    }

    @After
    public void unregisterIdlingResource() {
        IdlingRegistry.getInstance().unregister(EspressoIdlingResource.getIdlingResource());
    }

    @After
    public void signOut() {
        MyTestUtils.signOutForTesting();