How to check if text is present in clipboard in Ro

2019-06-06 23:29发布

问题:

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?

回答1:

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)