pharo-project / pharo

Pharo is a dynamic reflective pure object-oriented language supporting live programming inspired by Smalltalk.
http://pharo.org
Other
1.22k stars 358 forks source link

Question about Clipboard and FFI calls #14564

Open Nyan11 opened 1 year ago

Nyan11 commented 1 year ago

Hello,

I was working on a project that involve the Clipboard and i have no idea how i can write a unit test with the clipboard.

My problem

I have a method that copy a text in the clipboard and i have to test if the text is present in the clipboard.

How do i know that the clipboard is ready ?

examples

All example are run in a playground.

short text and no delay

empty := 0.
ok := 0.
ko := 0.

text := (1 to: 10) collect: [ :i | $a ] as: String.

1 to: 100 do: [ :i |
                Clipboard clipboardText: ''.
                Clipboard clipboardText: text.

                Clipboard clipboardText ifEmpty: [ empty := empty + 1 ] ifNotEmpty: [
                               Clipboard clipboardText = text
                                               ifTrue: [ ok := ok + 1 ]
                                               ifFalse: [ ko := ko + 1 ]
                ]
].

{ empty . ok . ko }

In this example i create a string "text" with 10 times $a in it.

Then i repeat 100 times the following:

  1. I cleanup the clipboard with an empty string.
  2. I put my "text" in the clipboard
  3. I verify the clipboard: if it is empty, if it is equal to the "text", if it is not equal to the "text".

I obtains the following results :

(The result are not always the same)

short text and a delay

empty := 0.
ok := 0.
ko := 0.

text := (1 to: 10) collect: [ :i | $a ] as: String.

1 to: 100 do: [ :i |
                Clipboard clipboardText: ''.
                Clipboard clipboardText: text.

                (Duration milliSeconds: 10) wait.

                Clipboard clipboardText ifEmpty: [ empty := empty + 1 ] ifNotEmpty: [
                               Clipboard clipboardText = text
                                               ifTrue: [ ok := ok + 1 ]
                                               ifFalse: [ ko := ko + 1 ]
                ]
].

{ empty . ok . ko }

I added a smal delay after setting my clipboard.

The results are :

(The result are not always the same)

long text and a delay

empty := 0.
ok := 0.
ko := 0.

text := (1 to: 4000) collect: [ :i | $a ] as: String.

1 to: 100 do: [ :i |
                Clipboard clipboardText: ''.
                Clipboard clipboardText: text.

                (Duration milliSeconds: 10) wait.

                Clipboard clipboardText ifEmpty: [ empty := empty + 1 ] ifNotEmpty: [
                               Clipboard clipboardText = text
                                               ifTrue: [ ok := ok + 1 ]
                                               ifFalse: [ ko := ko + 1 ]
                ]
].

{ empty . ok . ko }

I added a small delay and a much longer text.

The results are :

(The result are not always the same).

Possible explanation

I use Microsoft Window and i think the "clipboard method" used by Pharo is the SDL2 class >> #clipboardText: (class side).

The method send a ffiCall in SDL2 class >> #primClipboardText:.

My questions ?

How can i know that the FFI call message has been sent and has been processed by my OS ?

If i know that the FFI call is send to the os and if i know that the OS has finish to copy the text, I could write a unit test to ensure the method i write copy the correct text to my clipboard.

other infos

OS: Window Pharo version : Pharo 11.0.0 Build info: Pharo-11.0.0+build.700.sha.53659ed8c20ddb4b644a58135325ff84be4e063e (64 Bit)

welcome[bot] commented 1 year ago

Thanks for opening your first issue! Please check the CONTRIBUTING documents for some tips about which information should be provided. You can find information of how to do a Pull Request here: https://github.com/pharo-project/pharo/wiki/Contribute-a-fix-to-Pharo

GitHub
Contribute a fix to Pharo
Pharo is a dynamic reflective pure object-oriented language supporting live programming inspired by Smalltalk. - pharo-project/pharo