Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to test listview click event using TDD in android ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 214
    Comment on it

    We know that test driven development is basically for functional testing , in this blog we will test listview using instrumentation method runOnMainSync() that executes a call on application main ui thread.

    here is the layout file :

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            tools:context=".ListActivity" >
    
    <ListView
    android:id="@+id/lvListviewTest"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    </ListView>
    
    </LinearLayout>

    Main activity to run Listview and setting adapter :

    public class MainActivity extends Activity {
        // Array of strings...
        String[] mobileArray = {"Test1","Test2","Test3","Test4","Test5","Test6","Test7","Test8"};
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            ArrayAdapter myAdapter = new ArrayAdapter<String>(this, R.layout.activity_listview, mobileArray);
    
            ListView lvListviewTest = (ListView) findViewById(R.id.lvListviewTest);
            lvListviewTest.setAdapter(myAdapter);
        }
    }

    now its turn to write test case so this is the class to test click on listview :

    public class ActivityTests extends
            ActivityInstrumentationTestCase2<Activity>
    {
    final ListView lvListviewTest = (ListView) mActivity.findViewById(R.id.lvListviewTest);
            assertNotNull("The list was not loaded", lvListviewTest);
    
            getInstrumentation().runOnMainSync(new Runnable() {
    @Override
    public void run() {
    
            lvListviewTest.performItemClick(list.getAdapter().getView(position, null, null),
            position, list.getAdapter().getItemId(position));
            Log.e("click list click"," yes ");
            }
    
            });
    
            getInstrumentation().waitForIdleSync();
            }

    Here performItemClick method do the click event on listview.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: