Tuesday, January 1, 2013

How to test Android Long click menus using Robotium?

in Android, Long click menus can be found in List items, texts, views, screen.  

Robotium supports various functions to handle Long click menus,

  • clickLongInList(int line)
  • clickLongOnText(String text)
  • clickLongOnView(android.view.View view)
  • clickLongOnScreen(float x, float y)
Let us see usage of these APIs with an example test case and test code for the project shown on the right side.

project has a list of items and each support a long press functionality. on long press, will open a menu with various options like "Open, Delete, Edit title"

Let us write a test case for this project,

Test case for Android Long click menus :

Steps:
  1. Open notepad application
  2. press the menu item 'Add note' and add note 'Note 1'
  3. long click on the 'Note 1' in the list, which will open a menu, select 'Delete' option.
  4. verify the note is delete or not?
Test Code :
public void testRemoveNote() throws Exception {

        solo.clickOnMenuItem("Add note");

        //Assert that NoteEditor activity is opened

        solo.assertCurrentActivity("Expected NoteEditor activity", "NoteEditor"); 

        //In text field 0, add Note 1

        solo.enterText(0, "Note 1");

        solo.clickOnMenuItem("Save");

 

       solo.clickLongOnText("Note 1");

        //Clicks on Delete in the context menu

        solo.clickOnText("Delete");          

     

        //Note 1 test should not be found

        boolean expected = false;   

        boolean actual = solo.searchText("Note 1");

        //Assert that Note 1 text is not found

        assertEquals("Note 1 Text is found", expected, actual);  

        

    }

Explanation:

  1. Add Note : use 'solo.clickOnMenuItem()' to press on the menu item 'Add Note'. 
  2. Long click : 'solo.clickLongOnText()' is used to perform long click operation on the specified text.

    • use 'solo.clickOnText()', to select the text from long click menu.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.