I write a testing to the Grid View to count the number of items in the grid. i want to perform onclick to the grid view
here is a my mainActivity class
public class MainActivity extends AppCompatActivity {
GridView gridView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridview1);
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Send intent to SingleViewActivity
Intent i = new Intent(getApplicationContext(), SingleViewActivity.class);
// Pass image index
i.putExtra("id", position);
startActivity(i);
}
});
}
}
SingleViewActivity.Java
public class SingleViewActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_view);
// Get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.SingleView);
imageView.setImageResource(imageAdapter.mThumbIds[position]);
}
}
ImageAdapter.java
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Constructor
public ImageAdapter(Context c) {
mContext = c;
}
@Override
public int getCount() {
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
}
else
{
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
public Integer[] mThumbIds = {
R.drawable.images, R.drawable.asdawe,
R.drawable.bokey, R.drawable.assd,
R.drawable.download, R.drawable.ew,
R.drawable.lilly, R.drawable.jh,
R.drawable.rose, R.drawable.we,
R.drawable.lilly1, R.drawable.images,
R.drawable.bokey, R.drawable.asdawe,
R.drawable.download, R.drawable.ew,
R.drawable.jh, R.drawable.lilly,
R.drawable.rose, R.drawable.we,
R.drawable.lilly1, R.drawable.images
};
}
MainActivityTest.java
public class MainActivityTest extends ActivityInstrumentationTestCase<MainActivity> {
private MainActivity mActivity;
private GridView mGridView;
private Instrumentation mInstrumentation;
public MainActivityTest() {
super("com.android.frameworks.coretests", MainActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
mActivity = getActivity();
mGridView = (GridView) mActivity.findViewById(R.id.gridview1);
mInstrumentation = getInstrumentation();
}
@MediumTest
public void testPreconditions() {
assertNotNull(mActivity);
assertNotNull(mGridView);
}
@LargeTest
public void testSetSelection() {
mGridView = new GridView(mActivity);
mGridView.setSelection(0);
assertEquals(0, mGridView.getSelectedItemPosition());
mGridView.setSelection(-1);
assertEquals(-1, mGridView.getSelectedItemPosition());
mGridView.setSelection(mGridView.getCount());
assertEquals(mGridView.getCount(), mGridView.getSelectedItemPosition());
}
@Test
public void testSetSelectionone() {
TouchUtils.dragQuarterScreenDown(this);
TouchUtils.dragQuarterScreenUp(this);
// Nothing should be selected
assertEquals("Selection still available after touch", -1,
mGridView.getSelectedItemPosition());
final int targetPosition = mGridView.getAdapter().getCount() ;
assertEquals(22,targetPosition);
}
}
i want a test for the on click on the Grid View
Aucun commentaire:
Enregistrer un commentaire