简体   繁体   中英

How to check if text is present in clipboard in Robotium test?

I am testing my Android application using Robotium. In a dialog, I have a button that will copy the text in the dialog to the clipboard. Is it possible to access the clipboard in my test to see if the text has been copied after the button has been pressed? If so, how?

You can use the clipboard manager service in the same way you do in your application and then use its getText() method to retrieve the value. It should look something like (untested, from memory and I have had a few drinks...):

public String getClipboardText(){
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB){
         android.content.ClipboardManager clipboard =  (android.content.ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 
            return clipboard.getText(); 
    } else{
        android.text.ClipboardManager clipboard = (android.text.ClipboardManager)getSystemService(CLIPBOARD_SERVICE); 
        return clipboard.getText(); 
    }
}

You will then need to assert that this matches the expected result (Whatever you set in the dialog)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM