Search in sources :

Example 1 with TestItem

use of io.github.hidroh.materialistic.test.TestItem in project materialistic by hidroh.

the class ItemActivityTest method testScrollToTop.

@Config(shadows = ShadowRecyclerView.class)
@Test
public void testScrollToTop() {
    Intent intent = new Intent();
    intent.putExtra(ItemActivity.EXTRA_ITEM, new TestItem() {

        @NonNull
        @Override
        public String getType() {
            return STORY_TYPE;
        }

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

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

        @Override
        public int getKidCount() {
            return 10;
        }

        @Override
        public String getUrl() {
            return "http://example.com";
        }
    });
    controller.withIntent(intent).create().start().resume();
    // see https://github.com/robolectric/robolectric/issues/1326
    ShadowLooper.pauseMainLooper();
    controller.visible();
    ShadowApplication.getInstance().getForegroundThreadScheduler().advanceToLastPostedRunnable();
    RecyclerView recyclerView = (RecyclerView) activity.findViewById(R.id.recycler_view);
    recyclerView.smoothScrollToPosition(1);
    assertThat(customShadowOf(recyclerView).getScrollPosition()).isEqualTo(1);
    TabLayout tabLayout = (TabLayout) activity.findViewById(R.id.tab_layout);
    assertThat(tabLayout.getTabCount()).isEqualTo(2);
    tabLayout.getTabAt(1).select();
    tabLayout.getTabAt(0).select();
    tabLayout.getTabAt(0).select();
    assertThat(customShadowOf(recyclerView).getScrollPosition()).isEqualTo(0);
}
Also used : TabLayout(android.support.design.widget.TabLayout) NonNull(android.support.annotation.NonNull) Intent(android.content.Intent) RecyclerView(android.support.v7.widget.RecyclerView) ShadowRecyclerView(io.github.hidroh.materialistic.test.shadow.ShadowRecyclerView) TestItem(io.github.hidroh.materialistic.test.TestItem) SlowTest(io.github.hidroh.materialistic.test.suite.SlowTest) Test(org.junit.Test) Config(org.robolectric.annotation.Config)

Example 2 with TestItem

use of io.github.hidroh.materialistic.test.TestItem in project materialistic by hidroh.

the class ItemActivityTest method testOptionExternal.

@SuppressLint("NewApi")
@Test
public void testOptionExternal() {
    RobolectricPackageManager packageManager = (RobolectricPackageManager) RuntimeEnvironment.application.getPackageManager();
    packageManager.addResolveInfoForIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://example.com")), ShadowResolveInfo.newResolveInfo("label", "com.android.chrome", "DefaultActivity"));
    packageManager.addResolveInfoForIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(String.format(HackerNewsClient.WEB_ITEM_PATH, "1"))), ShadowResolveInfo.newResolveInfo("label", "com.android.chrome", "DefaultActivity"));
    Intent intent = new Intent();
    intent.putExtra(ItemActivity.EXTRA_ITEM, new TestItem() {

        @NonNull
        @Override
        public String getType() {
            return STORY_TYPE;
        }

        @Override
        public String getUrl() {
            return "http://example.com";
        }

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

        @Override
        public String getId() {
            return "1";
        }
    });
    controller.withIntent(intent).create().start().resume();
    // inflate menu, see https://github.com/robolectric/robolectric/issues/1326
    ShadowLooper.pauseMainLooper();
    controller.visible();
    ShadowApplication.getInstance().getForegroundThreadScheduler().advanceToLastPostedRunnable();
    // open article
    shadowOf(activity).clickMenuItem(R.id.menu_external);
    shadowOf(ShadowPopupMenu.getLatestPopupMenu()).getOnMenuItemClickListener().onMenuItemClick(new RoboMenuItem(R.id.menu_article));
    ShadowApplication.getInstance().getForegroundThreadScheduler().advanceToLastPostedRunnable();
    assertThat(shadowOf(activity).getNextStartedActivity()).hasAction(Intent.ACTION_VIEW);
    // open item
    shadowOf(activity).clickMenuItem(R.id.menu_external);
    shadowOf(ShadowPopupMenu.getLatestPopupMenu()).getOnMenuItemClickListener().onMenuItemClick(new RoboMenuItem(R.id.menu_comments));
    ShadowApplication.getInstance().getForegroundThreadScheduler().advanceToLastPostedRunnable();
    assertThat(shadowOf(activity).getNextStartedActivity()).hasAction(Intent.ACTION_VIEW);
}
Also used : NonNull(android.support.annotation.NonNull) Intent(android.content.Intent) RobolectricPackageManager(org.robolectric.res.builder.RobolectricPackageManager) RoboMenuItem(org.robolectric.fakes.RoboMenuItem) TestItem(io.github.hidroh.materialistic.test.TestItem) SlowTest(io.github.hidroh.materialistic.test.suite.SlowTest) Test(org.junit.Test) SuppressLint(android.annotation.SuppressLint)

Example 3 with TestItem

use of io.github.hidroh.materialistic.test.TestItem in project materialistic by hidroh.

the class ItemFragmentMultiPageTest method testEmptyView.

@Test
public void testEmptyView() {
    Bundle args = new Bundle();
    args.putParcelable(ItemFragment.EXTRA_ITEM, new TestItem() {
    });
    Fragment fragment = Fragment.instantiate(RuntimeEnvironment.application, ItemFragment.class.getName(), args);
    makeVisible(fragment);
    assertThat(fragment.getView().findViewById(R.id.empty)).isVisible();
}
Also used : Bundle(android.os.Bundle) Fragment(android.support.v4.app.Fragment) TestItem(io.github.hidroh.materialistic.test.TestItem) SlowTest(io.github.hidroh.materialistic.test.suite.SlowTest) Test(org.junit.Test)

Example 4 with TestItem

use of io.github.hidroh.materialistic.test.TestItem 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 5 with TestItem

use of io.github.hidroh.materialistic.test.TestItem 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)

Aggregations

TestItem (io.github.hidroh.materialistic.test.TestItem)40 Test (org.junit.Test)38 SlowTest (io.github.hidroh.materialistic.test.suite.SlowTest)15 Intent (android.content.Intent)12 Bundle (android.os.Bundle)9 NonNull (android.support.annotation.NonNull)8 Fragment (android.support.v4.app.Fragment)7 RoboMenuItem (org.robolectric.fakes.RoboMenuItem)5 TextView (android.widget.TextView)4 TestHnItem (io.github.hidroh.materialistic.data.TestHnItem)4 RecyclerView (android.support.v7.widget.RecyclerView)3 WebView (android.webkit.WebView)3 HackerNewsClient (io.github.hidroh.materialistic.data.HackerNewsClient)3 ShadowWebView (org.robolectric.shadows.ShadowWebView)3 SuppressLint (android.annotation.SuppressLint)2 Parcel (android.os.Parcel)2 TabLayout (android.support.design.widget.TabLayout)2 Item (io.github.hidroh.materialistic.data.Item)2 ResponseListener (io.github.hidroh.materialistic.data.ResponseListener)2 WebItem (io.github.hidroh.materialistic.data.WebItem)2