Fokusanforderung für Komponenten in sichtbarem Rahmen verweigert [geschlossen]Java

Java-Forum
Anonymous
 Fokusanforderung für Komponenten in sichtbarem Rahmen verweigert [geschlossen]

Post by Anonymous »

Warum fällt der Test aus und wie kann ich es übergeben? < /p>
Aus irgendeinem Grund wird die Fokusanforderung abgelehnt.import org.junit.jupiter.api.Test;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicReference;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeFalse;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

class FocusTest {

@Test
void ifFocusRequested_andWindowAncestorVisibleAndShowing_thenFocusGranted() {
JTextField field = new JTextField(10);
JFrame frame = callAndWait(() -> {
JFrame fr = new JFrame();
fr.add(field);
fr.setVisible(true);
return fr;
});
assumeFalse(callAndWait(field::hasFocus), "Field has focus");
assumeTrue(callAndWait(frame::isVisible));
assumeTrue(callAndWait(frame::isShowing));

// fails here
assertTrue(callAndWait(field::requestFocusInWindow), "Focus request denied");
}

static T callAndWait(Callable edtCallable) {
try {
AtomicReference value = new AtomicReference();
SwingUtilities.invokeAndWait(() -> {
try {
value.set(edtCallable.call());
} catch (Throwable e) {
throw new RuntimeException(e);
}
});
return value.get();
} catch (InterruptedException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
}
< /code>
org.opentest4j.AssertionFailedError: Focus request denied ==>
Expected :true
Actual :false
< /code>

org.junit.jupiter
junit-jupiter-engine
5.9.3
test

< /code>
Java 8.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post