1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.recyclerview.test; 18 19 import com.example.android.recyclerview.MainActivity; 20 import com.example.android.recyclerview.R; 21 import com.example.android.recyclerview.RecyclerViewFragment; 22 23 import android.support.v4.app.Fragment; 24 import android.support.v7.widget.RecyclerView; 25 import android.test.ActivityInstrumentationTestCase2; 26 27 /** 28 * Tests for RecyclerView sample. 29 */ 30 public class RecyclerViewSampleTests extends ActivityInstrumentationTestCase2<MainActivity> { 31 32 private MainActivity mTestActivity; 33 private RecyclerViewFragment mTestFragment; 34 35 public RecyclerViewSampleTests() { 36 super(MainActivity.class); 37 } 38 39 @Override 40 protected void setUp() throws Exception { 41 super.setUp(); 42 43 // Starts the activity under test using the default Intent with: 44 // action = {@link Intent#ACTION_MAIN} 45 // flags = {@link Intent#FLAG_ACTIVITY_NEW_TASK} 46 // All other fields are null or empty. 47 mTestActivity = getActivity(); 48 mTestFragment = (RecyclerViewFragment) 49 mTestActivity.getSupportFragmentManager().getFragments().get(1); 50 } 51 52 /** 53 * Test if the test fixture has been set up correctly. 54 */ 55 public void testPreconditions() { 56 //Try to add a message to add context to your assertions. These messages will be shown if 57 //a tests fails and make it easy to understand why a test failed 58 assertNotNull("mTestActivity is null", mTestActivity); 59 assertNotNull("mTestFragment is null", mTestFragment); 60 } 61 62 public void testRecyclerViewElementCount() { 63 RecyclerView recyclerView = 64 (RecyclerView) mTestFragment.getView().findViewById(R.id.recyclerView); 65 int elementCount = recyclerView.getAdapter().getItemCount(); 66 // There should be 60 elements in the RecyclerView's adapter. 67 assertEquals(60, elementCount); 68 } 69 70 } 71