-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First attempt at testing FavoritesActivity
- Loading branch information
1 parent
e72b3bd
commit 69e809d
Showing
2 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
app/src/androidTest/java/io/catter2/FavoritesActivityTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package io.catter2; | ||
|
||
import android.support.test.rule.ActivityTestRule; | ||
import android.support.test.runner.AndroidJUnit4; | ||
|
||
import org.junit.BeforeClass; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import io.catter2.di.AppDIComponent; | ||
import io.catter2.di.FavoritesRepoDIModule; | ||
import io.catter2.di.UserDIComponent; | ||
import io.catter2.espresso_utils.RecyclerViewItemCountAssertion; | ||
import io.catter2.favorites.FavoriteModel; | ||
import io.catter2.favorites.FavoritesRepository; | ||
|
||
import static android.support.test.espresso.Espresso.onView; | ||
import static android.support.test.espresso.matcher.ViewMatchers.withId; | ||
|
||
@RunWith(AndroidJUnit4.class) | ||
public class FavoritesActivityTest { | ||
@Rule | ||
public ActivityTestRule<FavoritesActivity> activityRule = | ||
new ActivityTestRule<>(FavoritesActivity.class); | ||
|
||
@BeforeClass | ||
public static void beforeClass() { | ||
UserDIComponent.initialize(new FavoritesRepoDIModule() { | ||
@Override | ||
public AppDIComponent getAppDIComponent() { | ||
return AppDIComponent.get(); | ||
} | ||
|
||
@Override | ||
public FavoritesRepository provideFavoritesRepository() { | ||
return new FavoritesRepository() { | ||
@Override | ||
public List<FavoriteModel> getFavorites() { | ||
ArrayList<FavoriteModel> models = new ArrayList<>(); | ||
models.add(new FavoriteModel(10, "url-10")); | ||
models.add(new FavoriteModel(11, "url-11")); | ||
models.add(new FavoriteModel(12, "url-12")); | ||
return models; | ||
} | ||
|
||
@Override | ||
public List<FavoriteModel> addFavorite(FavoriteModel model) { | ||
throw new RuntimeException("Not implemented"); | ||
} | ||
|
||
@Override | ||
public void registerChangeListener(ChangeListener listener) { | ||
// NoOp | ||
} | ||
|
||
@Override | ||
public void clearChangeListener() { | ||
// NoOp | ||
} | ||
}; | ||
} | ||
}); | ||
} | ||
|
||
@Test | ||
public void testThereAreThreeImages() { | ||
onView(withId(R.id.favorites_rv)).check(new RecyclerViewItemCountAssertion(3)); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
app/src/androidTest/java/io/catter2/espresso_utils/RecyclerViewItemCountAssertion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package io.catter2.espresso_utils; | ||
|
||
import android.support.test.espresso.NoMatchingViewException; | ||
import android.support.test.espresso.ViewAssertion; | ||
import android.support.v7.widget.RecyclerView; | ||
import android.view.View; | ||
|
||
import static android.support.test.espresso.matcher.ViewMatchers.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
public class RecyclerViewItemCountAssertion implements ViewAssertion { | ||
private final int expectedCount; | ||
|
||
public RecyclerViewItemCountAssertion(int expectedCount) { | ||
this.expectedCount = expectedCount; | ||
} | ||
|
||
@Override | ||
public void check(View view, NoMatchingViewException noViewFoundException) { | ||
if (noViewFoundException != null) { | ||
throw noViewFoundException; | ||
} | ||
|
||
RecyclerView recyclerView = (RecyclerView) view; | ||
RecyclerView.Adapter adapter = recyclerView.getAdapter(); | ||
assertThat(adapter.getItemCount(), is(expectedCount)); | ||
} | ||
} |