|
| 1 | +package org.fxmisc.richtext.model; |
| 2 | + |
| 3 | +import com.nitorcreations.junit.runners.NestedRunner; |
| 4 | +import javafx.scene.Scene; |
| 5 | +import javafx.scene.control.ContextMenu; |
| 6 | +import javafx.scene.control.MenuItem; |
| 7 | +import javafx.scene.input.KeyCode; |
| 8 | +import javafx.scene.input.MouseButton; |
| 9 | +import javafx.stage.Stage; |
| 10 | +import org.fxmisc.flowless.VirtualizedScrollPane; |
| 11 | +import org.fxmisc.richtext.InlineCssTextArea; |
| 12 | +import org.junit.Test; |
| 13 | +import org.junit.runner.RunWith; |
| 14 | +import org.testfx.framework.junit.ApplicationTest; |
| 15 | + |
| 16 | +@RunWith(NestedRunner.class) |
| 17 | +public class StyledTextAreaBehaviorTest { |
| 18 | + |
| 19 | + static { |
| 20 | + String osName = System.getProperty("os.name").toLowerCase(); |
| 21 | + |
| 22 | + WINDOWS_OS = osName.contains("win"); |
| 23 | + } |
| 24 | + |
| 25 | + private static final boolean WINDOWS_OS; |
| 26 | + |
| 27 | + public class ContextMenuTests extends ApplicationTest { |
| 28 | + |
| 29 | + public InlineCssTextArea area = new InlineCssTextArea(); |
| 30 | + |
| 31 | + @Override |
| 32 | + public void start(Stage stage) throws Exception { |
| 33 | + area.setContextMenu(new ContextMenu(new MenuItem("A Menu Item"))); |
| 34 | + // offset needs to be 5 to prevent test failures |
| 35 | + area.setContextMenuXOffset(5); |
| 36 | + area.setContextMenuYOffset(5); |
| 37 | + |
| 38 | + VirtualizedScrollPane<InlineCssTextArea> pane = new VirtualizedScrollPane<>(area); |
| 39 | + stage.setScene(new Scene(pane, 500, 500)); |
| 40 | + stage.show(); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + public void clickingSecondaryShowsContextMenu() { |
| 45 | + // when |
| 46 | + rightClickOn(area); |
| 47 | + |
| 48 | + // then |
| 49 | + area.getContextMenu().isShowing(); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void pressingSecondaryShowsContextMenu() { |
| 54 | + // when |
| 55 | + moveTo(area); |
| 56 | + press(MouseButton.SECONDARY); |
| 57 | + |
| 58 | + // then |
| 59 | + area.getContextMenu().isShowing(); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void pressingPrimaryMouseButtonHidesContextMenu() { |
| 64 | + // given menu is showing |
| 65 | + rightClickOn(area); |
| 66 | + |
| 67 | + press(MouseButton.PRIMARY); |
| 68 | + assert !area.getContextMenu().isShowing(); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void pressingMiddleMouseButtonHidesContextMenu() { |
| 73 | + // given menu is showing |
| 74 | + rightClickOn(area); |
| 75 | + |
| 76 | + press(MouseButton.MIDDLE); |
| 77 | + assert !area.getContextMenu().isShowing(); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void requestingContextMenuViaKeyboardWorksOnWindows() { |
| 82 | + if (WINDOWS_OS) { |
| 83 | + clickOn(area); |
| 84 | + press(KeyCode.CONTEXT_MENU); |
| 85 | + |
| 86 | + assert area.getContextMenu().isShowing(); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | +} |
0 commit comments