Reputation: 87
I'm using the clipboard in my RCP application for various functions. I have written some unit tests to test the functionality of these functions. They are very simple as they only copy and paste some data into and from the clipboard. The problem is, that these tests are failing occasionally. I think it is because there is some data left in the system clipboard which causes my tests to fail.
So I tried to write a method to clear all contents of the system clipboard before executing a test:
protected void clearClipboard() {
Display display = Display.getCurrent();
Clipboard clipboard = new Clipboard(display);
clipboard.clearContents();
clipboard.dispose();
}
But apparently this doesn't change anything and the tests keep failing. Is this the right approach to clear the system clipboard?
Upvotes: 2
Views: 783
Reputation: 2357
Clearing the clipboard is only possible if the data to be cleared is owned by the Clipboard through which you wish to clear said content. If this is not the case (eg. because some other application put it there), nothing is done, according to the corresponding official JavaDoc. In short, if you didn't put it there, you may not clear it.
Maybe try to set (=overwrite) the contents of the clipboard first and clear it afterwards, so it's more likely that you own the content (but beware the caveat about Clipboard tools grabbing ownership, as purported in above JavaDoc).
Upvotes: 4