Search in sources :

Example 1 with TestHnItem

use of io.github.hidroh.materialistic.data.TestHnItem in project materialistic by hidroh.

the class ItemActivityTest method testVoteError.

@Test
public void testVoteError() {
    Intent intent = new Intent();
    intent.putExtra(ItemActivity.EXTRA_ITEM, new TestHnItem(1));
    controller.withIntent(intent).create().start().resume();
    activity.findViewById(R.id.vote_button).performClick();
    verify(userServices).voteUp(any(Context.class), eq("1"), userServicesCallback.capture());
    userServicesCallback.getValue().onError(new IOException());
    assertEquals(activity.getString(R.string.vote_failed), ShadowToast.getTextOfLatestToast());
}
Also used : Context(android.content.Context) Intent(android.content.Intent) TestHnItem(io.github.hidroh.materialistic.data.TestHnItem) IOException(java.io.IOException) SlowTest(io.github.hidroh.materialistic.test.suite.SlowTest) Test(org.junit.Test)

Example 2 with TestHnItem

use of io.github.hidroh.materialistic.data.TestHnItem in project materialistic by hidroh.

the class ItemFragmentMultiPageTest method testBindLocalKidData.

@Test
public void testBindLocalKidData() {
    Item story = new TestHnItem(0L);
    story.populate(new TestItem() {

        @Override
        public int getDescendants() {
            return 1;
        }

        @Override
        public long[] getKids() {
            return new long[] { 1L };
        }
    });
    story.getKidItems()[0].populate(new TestItem() {

        @Override
        public String getText() {
            return "text";
        }

        @Override
        public long[] getKids() {
            return new long[] { 2L };
        }

        @Override
        public int getDescendants() {
            return 1;
        }
    });
    Bundle args = new Bundle();
    args.putParcelable(ItemFragment.EXTRA_ITEM, story);
    Fragment fragment = Fragment.instantiate(RuntimeEnvironment.application, ItemFragment.class.getName(), args);
    makeVisible(fragment);
    assertThat(fragment.getView().findViewById(R.id.empty)).isNotVisible();
    RecyclerView recyclerView = (RecyclerView) fragment.getView().findViewById(R.id.recycler_view);
    RecyclerView.ViewHolder viewHolder = CustomShadows.customShadowOf(recyclerView.getAdapter()).getViewHolder(0);
    assertThat((TextView) viewHolder.itemView.findViewById(R.id.text)).hasTextString("text");
    assertThat(viewHolder.itemView.findViewById(R.id.comment)).isVisible();
    viewHolder.itemView.findViewById(R.id.comment).performClick();
    Intent actual = shadowOf(fragment.getActivity()).getNextStartedActivity();
    assertEquals(ItemActivity.class.getName(), actual.getComponent().getClassName());
    assertThat(actual).hasExtra(ItemActivity.EXTRA_OPEN_COMMENTS, true);
}
Also used : Bundle(android.os.Bundle) Intent(android.content.Intent) TestHnItem(io.github.hidroh.materialistic.data.TestHnItem) Fragment(android.support.v4.app.Fragment) TestItem(io.github.hidroh.materialistic.test.TestItem) WebItem(io.github.hidroh.materialistic.data.WebItem) Item(io.github.hidroh.materialistic.data.Item) TestHnItem(io.github.hidroh.materialistic.data.TestHnItem) RoboMenuItem(org.robolectric.fakes.RoboMenuItem) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) TestItem(io.github.hidroh.materialistic.test.TestItem) SlowTest(io.github.hidroh.materialistic.test.suite.SlowTest) Test(org.junit.Test)

Example 3 with TestHnItem

use of io.github.hidroh.materialistic.data.TestHnItem in project materialistic by hidroh.

the class ItemFragmentSinglePageTest method setUp.

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    TestApplication.applicationGraph.inject(this);
    reset(hackerNewsClient);
    reset(userServices);
    shadowOf(((WindowManager) RuntimeEnvironment.application.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay()).setHeight(// no preload
    0);
    shadowOf((ConnectivityManager) RuntimeEnvironment.application.getSystemService(Context.CONNECTIVITY_SERVICE)).setActiveNetworkInfo(ShadowNetworkInfo.newInstance(null, ConnectivityManager.TYPE_WIFI, 0, true, true));
    final TestHnItem item0 = new TestHnItem(1, 1) {

        @Override
        public long getNeighbour(int direction) {
            if (direction == Navigable.DIRECTION_DOWN) {
                return 4L;
            }
            return super.getNeighbour(direction);
        }
    };
    item0.populate(new // level 0
    TestItem() {

        @Override
        public String getId() {
            return "1";
        }

        @Override
        public String getText() {
            return "text";
        }

        @Override
        public int getDescendants() {
            return 1;
        }

        @Override
        public long[] getKids() {
            return new long[] { 2L };
        }
    });
    final TestHnItem item1 = new TestHnItem(2, 2);
    item1.populate(new // level 1
    TestItem() {

        @Override
        public String getId() {
            return "2";
        }

        @Override
        public String getParent() {
            return "1";
        }

        @Override
        public boolean isDeleted() {
            return true;
        }

        @Override
        public String getText() {
            return "text";
        }

        @Override
        public int getDescendants() {
            return 1;
        }

        @Override
        public long[] getKids() {
            return new long[] { 3L };
        }
    });
    item0.getKidItems()[0] = item1;
    final TestHnItem item2 = new TestHnItem(3, 3);
    item2.populate(new // level 2
    TestItem() {

        @Override
        public String getId() {
            return "3";
        }

        @Override
        public long[] getKids() {
            return new long[0];
        }

        @Override
        public int getDescendants() {
            return 0;
        }

        @Override
        public String getParent() {
            return "2";
        }

        @Override
        public boolean isDead() {
            return true;
        }
    });
    item1.getKidItems()[0] = item2;
    TestHnItem item3 = new TestHnItem(4, 4) {

        @Override
        public long getNeighbour(int direction) {
            if (direction == Navigable.DIRECTION_UP) {
                return 1L;
            }
            return super.getNeighbour(direction);
        }
    };
    item3.populate(new // level 0
    TestItem() {

        @Override
        public String getId() {
            return "4";
        }

        @Override
        public String getText() {
            return "text";
        }

        @Override
        public int getDescendants() {
            return 0;
        }

        @Override
        public long[] getKids() {
            return new long[0];
        }
    });
    TestHnItem story = new TestHnItem(0);
    story.populate(new TestItem() {

        @Override
        public long[] getKids() {
            return new long[] { 1L, 4L };
        }

        @Override
        public int getDescendants() {
            return 4;
        }
    });
    story.getKidItems()[0] = item0;
    story.getKidItems()[1] = item3;
    Bundle args = new Bundle();
    args.putParcelable(ItemFragment.EXTRA_ITEM, story);
    fragment = (ItemFragment) Fragment.instantiate(RuntimeEnvironment.application, ItemFragment.class.getName(), args);
    controller = Robolectric.buildActivity(ItemFragmentMultiPageTest.TestItemActivity.class);
    activity = controller.create().start().resume().visible().get();
    PreferenceManager.getDefaultSharedPreferences(activity).edit().putBoolean(activity.getString(R.string.pref_lazy_load), false).apply();
    activity.getSupportFragmentManager().beginTransaction().add(R.id.content_frame, fragment, ItemFragment.class.getName()).commit();
    recyclerView = (RecyclerView) fragment.getView().findViewById(R.id.recycler_view);
    adapter = (SinglePageItemRecyclerViewAdapter) recyclerView.getAdapter();
    // auto expand all
    viewHolder = (ToggleItemViewHolder) customShadowOf(adapter).getViewHolder(0);
    viewHolder1 = (ToggleItemViewHolder) customShadowOf(adapter).getViewHolder(1);
    viewHolder2 = (ToggleItemViewHolder) customShadowOf(adapter).getViewHolder(2);
}
Also used : ConnectivityManager(android.net.ConnectivityManager) Bundle(android.os.Bundle) TestHnItem(io.github.hidroh.materialistic.data.TestHnItem) WindowManager(android.view.WindowManager) TestItem(io.github.hidroh.materialistic.test.TestItem) Before(org.junit.Before)

Example 4 with TestHnItem

use of io.github.hidroh.materialistic.data.TestHnItem in project materialistic by hidroh.

the class ListFragmentTest method testHighlightNewItems.

@Test
public void testHighlightNewItems() {
    Bundle args = new Bundle();
    args.putString(ListFragment.EXTRA_ITEM_MANAGER, HackerNewsClient.class.getName());
    args.putString(ListFragment.EXTRA_FILTER, ItemManager.TOP_FETCH_MODE);
    activity.getSupportFragmentManager().beginTransaction().add(android.R.id.list, Fragment.instantiate(activity, ListFragment.class.getName(), args)).commit();
    verify(itemManager).getStories(any(), eq(ItemManager.MODE_DEFAULT), listener.capture());
    listener.getValue().onResponse(new Item[] { new TestItem() {

        @Override
        public String getId() {
            return "1";
        }
    } });
    reset(itemManager);
    ShadowSwipeRefreshLayout shadowSwipeRefreshLayout = (ShadowSwipeRefreshLayout) ShadowExtractor.extract(activity.findViewById(R.id.swipe_layout));
    shadowSwipeRefreshLayout.getOnRefreshListener().onRefresh();
    // should trigger another data request
    verify(itemManager).getStories(any(String.class), eq(ItemManager.MODE_NETWORK), listener.capture());
    listener.getValue().onResponse(new Item[] { new TestHnItem(1L), new TestHnItem(2L) });
    assertEquals(2, ((RecyclerView) activity.findViewById(R.id.recycler_view)).getAdapter().getItemCount());
    View snackbarView = ShadowSnackbar.getLatestView();
    Assertions.assertThat((TextView) snackbarView.findViewById(R.id.snackbar_text)).isNotNull().containsText(activity.getResources().getQuantityString(R.plurals.new_stories_count, 1, 1));
    snackbarView.findViewById(R.id.snackbar_action).performClick();
    assertEquals(1, ((RecyclerView) activity.findViewById(R.id.recycler_view)).getAdapter().getItemCount());
    snackbarView = ShadowSnackbar.getLatestView();
    Assertions.assertThat((TextView) snackbarView.findViewById(R.id.snackbar_text)).isNotNull().containsText(activity.getResources().getQuantityString(R.plurals.showing_new_stories, 1, 1));
    snackbarView.findViewById(R.id.snackbar_action).performClick();
    assertEquals(2, ((RecyclerView) activity.findViewById(R.id.recycler_view)).getAdapter().getItemCount());
    controller.pause().stop().destroy();
}
Also used : ShadowSwipeRefreshLayout(io.github.hidroh.materialistic.test.shadow.ShadowSwipeRefreshLayout) Bundle(android.os.Bundle) RecyclerView(android.support.v7.widget.RecyclerView) TestHnItem(io.github.hidroh.materialistic.data.TestHnItem) HackerNewsClient(io.github.hidroh.materialistic.data.HackerNewsClient) View(android.view.View) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) TestItem(io.github.hidroh.materialistic.test.TestItem) Test(org.junit.Test) SlowTest(io.github.hidroh.materialistic.test.suite.SlowTest)

Example 5 with TestHnItem

use of io.github.hidroh.materialistic.data.TestHnItem in project materialistic by hidroh.

the class ListFragmentViewHolderTest method testNewStory.

@Test
public void testNewStory() {
    reset(itemManager);
    ShadowSwipeRefreshLayout shadowSwipeRefreshLayout = (ShadowSwipeRefreshLayout) ShadowExtractor.extract(activity.findViewById(R.id.swipe_layout));
    shadowSwipeRefreshLayout.getOnRefreshListener().onRefresh();
    verify(itemManager).getStories(any(), eq(ItemManager.MODE_NETWORK), storiesListener.capture());
    storiesListener.getValue().onResponse(new Item[] { new TestHnItem(2) {

        @Override
        public int getRank() {
            return 46;
        }
    } });
    ShadowSnackbar.getLatestView().findViewById(R.id.snackbar_action).performClick();
    verify(itemManager, atLeastOnce()).getItem(any(), eq(ItemManager.MODE_NETWORK), itemListener.capture());
    itemListener.getValue().onResponse(new PopulatedStory(2));
    RecyclerView.ViewHolder holder = adapter.getViewHolder(0);
    assertThat((TextView) holder.itemView.findViewById(R.id.rank)).hasTextString("46*");
}
Also used : ShadowSwipeRefreshLayout(io.github.hidroh.materialistic.test.shadow.ShadowSwipeRefreshLayout) RecyclerView(android.support.v7.widget.RecyclerView) ShadowRecyclerView(io.github.hidroh.materialistic.test.shadow.ShadowRecyclerView) TextView(android.widget.TextView) TestHnItem(io.github.hidroh.materialistic.data.TestHnItem) SlowTest(io.github.hidroh.materialistic.test.suite.SlowTest) Test(org.junit.Test)

Aggregations

TestHnItem (io.github.hidroh.materialistic.data.TestHnItem)35 Test (org.junit.Test)31 SlowTest (io.github.hidroh.materialistic.test.suite.SlowTest)23 RecyclerView (android.support.v7.widget.RecyclerView)12 TextView (android.widget.TextView)9 Intent (android.content.Intent)8 Bundle (android.os.Bundle)8 NonNull (android.support.annotation.NonNull)8 ShadowRecyclerView (io.github.hidroh.materialistic.test.shadow.ShadowRecyclerView)8 ShadowSwipeRefreshLayout (io.github.hidroh.materialistic.test.shadow.ShadowSwipeRefreshLayout)6 Context (android.content.Context)5 TestItem (io.github.hidroh.materialistic.test.TestItem)5 Before (org.junit.Before)4 Fragment (android.support.v4.app.Fragment)3 View (android.view.View)3 HackerNewsClient (io.github.hidroh.materialistic.data.HackerNewsClient)3 Item (io.github.hidroh.materialistic.data.Item)3 WebItem (io.github.hidroh.materialistic.data.WebItem)3 RoboMenuItem (org.robolectric.fakes.RoboMenuItem)3 ShadowContentObserver (org.robolectric.shadows.ShadowContentObserver)3