Search in sources :

Example 1 with ShadowContentObserver

use of org.robolectric.shadows.ShadowContentObserver in project materialistic by hidroh.

the class ListFragmentViewHolderTest method testSaveItem.

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Test
public void testSaveItem() {
    ShadowContentObserver observer = shadowOf(shadowOf(activity.getContentResolver()).getContentObservers(MaterialisticProvider.URI_FAVORITE).iterator().next());
    verify(itemManager).getItem(any(), eq(ItemManager.MODE_DEFAULT), itemListener.capture());
    itemListener.getValue().onResponse(item);
    adapter.getViewHolder(0).itemView.performLongClick();
    PopupMenu popupMenu = ShadowPopupMenu.getLatestPopupMenu();
    assertNotNull(popupMenu);
    assertThat(popupMenu.getMenu().findItem(R.id.menu_contextual_save).isVisible()).isFalse();
    shadowOf(popupMenu).getOnMenuItemClickListener().onMenuItemClick(new RoboMenuItem(R.id.menu_contextual_save));
    verify(favoriteManager).add(any(Context.class), eq(item));
    observer.dispatchChange(false, MaterialisticProvider.URI_FAVORITE.buildUpon().appendPath("add").appendPath("1").build());
    assertTrue(item.isFavorite());
    View snackbarView = ShadowSnackbar.getLatestView();
    assertThat((TextView) snackbarView.findViewById(R.id.snackbar_text)).isNotNull().containsText(R.string.toast_saved);
    snackbarView.findViewById(R.id.snackbar_action).performClick();
    verify(favoriteManager).remove(any(Context.class), eq("1"));
    observer.dispatchChange(false, MaterialisticProvider.URI_FAVORITE.buildUpon().appendPath("remove").appendPath("1").build());
    assertFalse(item.isFavorite());
}
Also used : Context(android.content.Context) ShadowContentObserver(org.robolectric.shadows.ShadowContentObserver) View(android.view.View) TextView(android.widget.TextView) RecyclerView(android.support.v7.widget.RecyclerView) ShadowRecyclerView(io.github.hidroh.materialistic.test.shadow.ShadowRecyclerView) RoboMenuItem(org.robolectric.fakes.RoboMenuItem) PopupMenu(android.widget.PopupMenu) ShadowPopupMenu(org.robolectric.shadows.ShadowPopupMenu) SlowTest(io.github.hidroh.materialistic.test.suite.SlowTest) Test(org.junit.Test) TargetApi(android.annotation.TargetApi)

Example 2 with ShadowContentObserver

use of org.robolectric.shadows.ShadowContentObserver in project materialistic by hidroh.

the class ItemActivityTest method testFavoriteStory.

@Test
public void testFavoriteStory() {
    Intent intent = new Intent();
    TestHnItem item = new TestHnItem(1L) {

        @NonNull
        @Override
        public String getType() {
            return STORY_TYPE;
        }
    };
    item.setFavorite(true);
    intent.putExtra(ItemActivity.EXTRA_ITEM, item);
    controller.withIntent(intent).create().start().resume();
    assertTrue(item.isFavorite());
    ShadowContentObserver observer = shadowOf(shadowOf(activity.getContentResolver()).getContentObservers(MaterialisticProvider.URI_FAVORITE).iterator().next());
    activity.findViewById(R.id.bookmarked).performClick();
    verify(favoriteManager).remove(any(Context.class), eq("1"));
    observer.dispatchChange(false, MaterialisticProvider.URI_FAVORITE.buildUpon().appendPath("remove").appendPath("1").build());
    assertFalse(item.isFavorite());
    assertThat((TextView) activity.findViewById(R.id.snackbar_text)).isNotNull().containsText(R.string.toast_removed);
    activity.findViewById(R.id.snackbar_action).performClick();
    observer.dispatchChange(false, MaterialisticProvider.URI_FAVORITE.buildUpon().appendPath("add").appendPath("1").build());
    assertTrue(item.isFavorite());
}
Also used : Context(android.content.Context) ShadowContentObserver(org.robolectric.shadows.ShadowContentObserver) Intent(android.content.Intent) TestHnItem(io.github.hidroh.materialistic.data.TestHnItem) SlowTest(io.github.hidroh.materialistic.test.suite.SlowTest) Test(org.junit.Test)

Example 3 with ShadowContentObserver

use of org.robolectric.shadows.ShadowContentObserver in project materialistic by hidroh.

the class ItemActivityTest method testNonFavoriteStory.

@Test
public void testNonFavoriteStory() {
    TestHnItem item = new TestHnItem(1L) {

        @NonNull
        @Override
        public String getType() {
            return STORY_TYPE;
        }
    };
    Intent intent = new Intent();
    intent.putExtra(ItemActivity.EXTRA_ITEM, item);
    controller.withIntent(intent).create().start().resume();
    assertFalse(item.isFavorite());
    activity.findViewById(R.id.bookmarked).performClick();
    ShadowContentObserver observer = shadowOf(shadowOf(activity.getContentResolver()).getContentObservers(MaterialisticProvider.URI_FAVORITE).iterator().next());
    observer.dispatchChange(false, MaterialisticProvider.URI_FAVORITE.buildUpon().appendPath("add").appendPath("1").build());
    assertTrue(item.isFavorite());
}
Also used : ShadowContentObserver(org.robolectric.shadows.ShadowContentObserver) Intent(android.content.Intent) TestHnItem(io.github.hidroh.materialistic.data.TestHnItem) SlowTest(io.github.hidroh.materialistic.test.suite.SlowTest) Test(org.junit.Test)

Example 4 with ShadowContentObserver

use of org.robolectric.shadows.ShadowContentObserver in project materialistic by hidroh.

the class ListFragmentViewHolderTest method testFavoriteObserver.

@Test
public void testFavoriteObserver() {
    verify(itemManager).getItem(any(), eq(ItemManager.MODE_DEFAULT), itemListener.capture());
    item.setFavorite(true);
    itemListener.getValue().onResponse(item);
    assertTrue(item.isFavorite());
    controller.pause();
    ShadowContentObserver observer = shadowOf(shadowOf(activity.getContentResolver()).getContentObservers(MaterialisticProvider.URI_FAVORITE).iterator().next());
    // observed clear
    observer.dispatchChange(false, MaterialisticProvider.URI_FAVORITE.buildUpon().appendPath("clear").build());
    RecyclerView.ViewHolder viewHolder = adapter.getViewHolder(0);
    assertFalse(item.isFavorite());
    assertThat(viewHolder.itemView.findViewById(R.id.bookmarked)).isNotVisible();
    // observed add
    observer.dispatchChange(false, MaterialisticProvider.URI_FAVORITE.buildUpon().appendPath("add").appendPath("1").build());
    assertTrue(item.isFavorite());
    // observed remove
    observer.dispatchChange(false, MaterialisticProvider.URI_FAVORITE.buildUpon().appendPath("remove").appendPath("1").build());
    assertFalse(item.isFavorite());
    controller.resume();
}
Also used : ShadowContentObserver(org.robolectric.shadows.ShadowContentObserver) RecyclerView(android.support.v7.widget.RecyclerView) ShadowRecyclerView(io.github.hidroh.materialistic.test.shadow.ShadowRecyclerView) SlowTest(io.github.hidroh.materialistic.test.suite.SlowTest) Test(org.junit.Test)

Example 5 with ShadowContentObserver

use of org.robolectric.shadows.ShadowContentObserver in project materialistic by hidroh.

the class ListFragmentViewHolderTest method testViewedObserver.

@Test
public void testViewedObserver() {
    verify(itemManager).getItem(any(), eq(ItemManager.MODE_DEFAULT), itemListener.capture());
    itemListener.getValue().onResponse(item);
    assertNotViewed();
    controller.pause();
    ShadowContentObserver observer = shadowOf(shadowOf(activity.getContentResolver()).getContentObservers(MaterialisticProvider.URI_VIEWED).iterator().next());
    observer.dispatchChange(false, MaterialisticProvider.URI_VIEWED.buildUpon().appendPath("2").build());
    observer.dispatchChange(false, MaterialisticProvider.URI_VIEWED.buildUpon().appendPath("1").build());
    controller.resume();
    assertViewed();
}
Also used : ShadowContentObserver(org.robolectric.shadows.ShadowContentObserver) SlowTest(io.github.hidroh.materialistic.test.suite.SlowTest) Test(org.junit.Test)

Aggregations

SlowTest (io.github.hidroh.materialistic.test.suite.SlowTest)6 Test (org.junit.Test)6 ShadowContentObserver (org.robolectric.shadows.ShadowContentObserver)6 TestHnItem (io.github.hidroh.materialistic.data.TestHnItem)3 Context (android.content.Context)2 Intent (android.content.Intent)2 RecyclerView (android.support.v7.widget.RecyclerView)2 ShadowRecyclerView (io.github.hidroh.materialistic.test.shadow.ShadowRecyclerView)2 TargetApi (android.annotation.TargetApi)1 View (android.view.View)1 PopupMenu (android.widget.PopupMenu)1 TextView (android.widget.TextView)1 RoboMenuItem (org.robolectric.fakes.RoboMenuItem)1 ShadowPopupMenu (org.robolectric.shadows.ShadowPopupMenu)1