可以在recyclerView上执行什么样的浓咖啡测试

时间:2018-03-08 15:34:50

标签: android android-recyclerview

我一直在进行UI测试,到目前为止一直很好,我目前的挑战是在recyclerView适配器类上执行测试。我不知道可以做什么样的测试。我的代码如下。

public class HomeAdapter extends RecyclerView.Adapter <HomeAdapter.ProductViewHolder> {

   private Context mCtx;

   private List <HomeProduct> homeProductList;

   public HomeAdapter(Context context, List <HomeProduct> homeProductList) {
    this.mCtx = context;
    this.homeProductList = homeProductList;
}

@Override
public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View view = inflater.inflate(R.layout.home_item_layout, null);
    return new ProductViewHolder(view);
}

@Override
public void onBindViewHolder(final ProductViewHolder holder, final int position) {

    final HomeProduct product = homeProductList.get(position);
    final String title = product.getTitle();
    final String subtitle = product.getSubtitle();

    holder.homeImageView.setImageDrawable(ContextCompat.getDrawable(mCtx, product.getImage()));
    holder.homeTitleTextView.setText(product.getTitle());
    holder.homeSubtitleTextView.setText(product.getSubtitle());

    holder.homeLinearlayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intentRoomActivity = new Intent(mCtx, RoomActivity.class);
            intentRoomActivity.putExtra("room_title", title);
            intentRoomActivity.putExtra("room_state", subtitle);
            mCtx.startActivity(intentRoomActivity);
        }
    });
}

@Override
public int getItemCount() {
    return homeProductList.size();
}

class ProductViewHolder extends RecyclerView.ViewHolder {
    TextView homeTitleTextView, homeSubtitleTextView;
    ImageView homeImageView;
    LinearLayout homeLinearlayout;

    public ProductViewHolder(View itemView) {
        super(itemView);

        homeTitleTextView = itemView.findViewById(R.id.home_title_textView);
        homeSubtitleTextView = itemView.findViewById(R.id.home_subtitle_textView);
        homeImageView = itemView.findViewById(R.id.home_imageView);
        homeLinearlayout = itemView.findViewById(R.id.homeLinearLayout);
    }

}

3 个答案:

答案 0 :(得分:0)

在Gradle中导入espresso-contrib,然后在you have access to中导入

  • actionOnHolderItem
  • actionOnItem
  • actionOnItemAtPosition
  • scrollToHolder
  • scrollToPosition

答案 1 :(得分:0)

如果您想测试HomeAdapter,可以使用http://www.stevetrefethen.com/blog/debugging-a-python-scrapy-project-in-vscode模拟RecyclerView,然后对适配器方法进行单元测试。 Roboelectric

package com.codepath.testingdemo.adapters;

    import android.content.Context;
    import android.os.Build;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.view.View;

    import com.codepath.testingdemo.BuildConfig;
    import com.codepath.testingdemo.models.Post;

    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.robolectric.RobolectricGradleTestRunner;
    import org.robolectric.RuntimeEnvironment;
    import org.robolectric.annotation.Config;

    import java.util.Arrays;
    import java.util.List;

    import static org.assertj.android.api.Assertions.assertThat;
    import static org.assertj.android.recyclerview.v7.api.Assertions.assertThat;
    import static org.junit.Assert.assertEquals;


    @Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP)
    @RunWith(RobolectricGradleTestRunner.class)
    public class PostsAdapterTest {

        private Context context;

        @Before
        public void setup() {
            context = RuntimeEnvironment.application;
        }

        @Test
        public void postsAdapterViewRecyclingCaption() {
            // Set up input
            List<Post> posts = Arrays.asList(
                    new Post("Lebron", null),
                    new Post("Steph", "We Won!!!")
            );


            PostsRecyclerViewAdapter adapter = new PostsRecyclerViewAdapter(posts);

            RecyclerView rvParent = new RecyclerView(context);
            rvParent.setLayoutManager(new LinearLayoutManager(context));

            // Run test
            PostsRecyclerViewAdapter.PostItemViewHolder viewHolder =
                    adapter.onCreateViewHolder(rvParent, 0);

            adapter.onBindViewHolder(viewHolder, 0);

            // JUnit Assertion
            assertEquals(View.GONE, viewHolder.tvCaption.getVisibility());

            // AssertJ-Android Assertion
            assertThat(viewHolder.tvCaption).isGone();

            adapter.onBindViewHolder(viewHolder, 1);

            // JUnit Assertion
            assertEquals("Steph: We Won!!!", viewHolder.tvCaption.getText().toString());

            // AssertJ-Android Assertion
            assertThat(viewHolder.tvCaption).isVisible().containsText("Won");

            assertThat(adapter).hasItemCount(2);
        }
    }

答案 2 :(得分:0)

您需要使用Robolectric进行make测试,该类必须放在Android资源目录的测试目录文件夹中。

所以根据我的情况,某些测试我可以在我的RoomAdapter类中使用robolectric运行,它是recyclerView适配器,当涉及drawable时,我使用的shadowDrawable完全在下面。

希望它可以帮助某人,保持幸福。

import static junit.framework.Assert.assertEquals;


@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class)

public class RoomAdapterTest {
    private Context context;

    @Before
    public void setUp() throws Exception
    {
        context = RuntimeEnvironment.application;

//        Intent intent = new Intent();
//        intent.putExtra("roomTitle", "Title");
//        intent.putExtra("roomState","Subtitle");

        }

    @Test
    public void postsRecycleViewHomeAdapter() {
        // Set up dummy data
        List<RoomProduct> roomProductList = Arrays.asList(
                new RoomProduct(0, R.drawable.ic_launcher_background,"Tap", "On" , 05f),
                new RoomProduct(1, R.drawable.ic_launcher_background, "Shower", "Running", 0.5f),
                new RoomProduct(2, R.drawable.ic_launcher_background, "TV Channel", "53", 0.5f)
        );

        RoomAdapter adapter = new RoomAdapter(context,roomProductList);

        RecyclerView rvParent = new RecyclerView(context);
        rvParent.setLayoutManager(new LinearLayoutManager(context));

        // Run test On onBindViewHolder and ViewHolder on dummy data
        RoomAdapter.ItemViewAdapterHolder viewHolder = adapter.onCreateViewHolder(rvParent, 0);

        //Test number 1
        adapter.onBindViewHolder(viewHolder, 0);
        assertEquals("Tap", viewHolder.roomItem.getText());
        assertEquals("On", viewHolder.roomState.getText());
        ShadowDrawable shadowDrawable = Shadows.shadowOf(viewHolder.roomImageItem.getDrawable());
        assertEquals(R.drawable.ic_launcher_background, shadowDrawable.getCreatedFromResId());


        //Test number 2
        adapter.onBindViewHolder(viewHolder,1);
        assertEquals("Shower", viewHolder.roomItem.getText());
        assertEquals("Running", viewHolder.roomState.getText());
        ShadowDrawable shadowDrawable1 = Shadows.shadowOf(viewHolder.roomImageItem.getDrawable());
        assertEquals(R.drawable.ic_launcher_background, shadowDrawable1.getCreatedFromResId());

        //Test number 3
        adapter.onBindViewHolder(viewHolder, 2);
        assertEquals("TV Channel", viewHolder.roomItem.getText());
        assertEquals("53", viewHolder.roomState.getText());
        ShadowDrawable shadowDrawable2 = Shadows.shadowOf(viewHolder.roomImageItem.getDrawable());
        assertEquals(R.drawable.ic_launcher_background, shadowDrawable2.getCreatedFromResId());

    }
}