简体   繁体   中英

AssertJ Swing enterText() writing wrong symbols

I'm using Swing to create a GUI for my application that has a JSpinner named "spinner". Inside the tests, created using AssertJ Swing I have something like this:

import org.assertj.swing.fixture.FrameFixture;
import org.assertj.swing.edt.GuiActionRunner;

FrameFixture window;
MyView view;
GuiActionRunner.execute(() -> {
    view = new MyView();
    return view;
});
window = new FrameFixture(view);
window.spinner("spinner").enterText("-1");

This code runs without any problems on Ubuntu. However while testing the same code on MacOS with an Italian keyboard, I've noticed that instead of entering "-1" the text written is "'1". This happens also for other symbols like "\\" that turns into an "ù".

If I manually enter "-1" inside the spinner on mac it is written ok. So I guess the problem is associated to enterText() itself and mac. Does anyone has an idea of why this is happening? Thanks.

Update:

In order to isolate the problem I've also tried doing:

String minus = java.awt.event.KeyEvent.getKeyText(45);
System.out.println(minus);

From this cheat sheet , I know that 45 key corresponds to - and indeed this code works correctly.

But then I've tried:

window.spinner("spinner").click();
window.robot().pressAndReleaseKeys(45);

Or similarly by creating a new robot:

robot = new Robot();
robot.setAutoDelay(500); 
window.spinner("spinner").click();
robot.keyPress(KeyEvent.VK_MINUS); 

This two fragment of code again instead of writing - write ' . From this old discussion I suspect that there is a bug in the key mapping for the Italian keyboard in a way similar to the bugs mentioned in the discussion for the French and German keyboard.

Here , I've discovered that the tests can be executed using the following VM argument for assertj:

-Dassertj.swing.keyboard.locale=language

Indeed by specifying a different language the keyboard mapping changes, overriding the default one. Of course choosing "it" as language hasn't solved the problem. I did some tests changing different keyboard locale. The one that works the best for my italian MacBook is surprisingly the Finnish keyboard layout.

I've tested a few symbols with:

window.spinner("spinner").enterText("-123456789;.,_èé@)(/&%$£|\"!<>ìàòùabcdefghilmnopqrstuvzwxyjk");

The resulting text is:

-123456789;.,?èé2)(/&%43"!lìàòùabcdefghilmnopqrstuvzwxyjk

Some character are still not correctly mapped, but is way better than before. For the tests that I have in my GUI this is enough. Still I would appreciate a more entirely correct solution.

In the assertj documentation I've found the KeyStrokeMappingProvider class that can be used for mapping. Besides the default one there are currently available keystroke mapping providers for German, English, Finnish, French and French + macOS. My guess is that there should be also a mapping provider for the Italian + macOS layout.

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