|
1 | 1 | package com.thecoderscorner.menu.editorint.uitests.uimenuitem; |
2 | 2 |
|
| 3 | +import com.thecoderscorner.menu.domain.RuntimeListMenuItem; |
| 4 | +import com.thecoderscorner.menu.domain.RuntimeListMenuItemBuilder; |
| 5 | +import com.thecoderscorner.menu.domain.util.MenuItemHelper; |
3 | 6 | import com.thecoderscorner.menu.editorui.generator.core.VariableNameGenerator; |
| 7 | +import com.thecoderscorner.menu.persist.LocaleMappingHandler; |
| 8 | +import com.thecoderscorner.menu.persist.PropertiesLocaleEnabledHandler; |
| 9 | +import com.thecoderscorner.menu.persist.SafeBundleLoader; |
4 | 10 | import javafx.application.Platform; |
| 11 | +import javafx.scene.Node; |
| 12 | +import javafx.scene.control.Label; |
| 13 | +import javafx.scene.control.Spinner; |
5 | 14 | import javafx.stage.Stage; |
6 | 15 | import org.junit.jupiter.api.AfterEach; |
7 | 16 | import org.junit.jupiter.api.Test; |
8 | 17 | import org.junit.jupiter.api.extension.ExtendWith; |
| 18 | +import org.testfx.api.FxAssert; |
| 19 | +import org.testfx.api.FxRobot; |
9 | 20 | import org.testfx.framework.junit5.ApplicationExtension; |
10 | 21 | import org.testfx.framework.junit5.Start; |
| 22 | +import org.testfx.matcher.control.ListViewMatchers; |
11 | 23 |
|
12 | | -import static org.junit.jupiter.api.Assertions.fail; |
13 | | -import static org.mockito.ArgumentMatchers.any; |
| 24 | +import java.io.File; |
| 25 | +import java.io.IOException; |
| 26 | +import java.nio.file.Files; |
| 27 | +import java.nio.file.Path; |
| 28 | +import java.util.Comparator; |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +import static org.assertj.core.api.Assertions.assertThat; |
| 32 | +import static org.testfx.api.FxAssert.verifyThat; |
14 | 33 |
|
15 | 34 | @ExtendWith(ApplicationExtension.class) |
16 | 35 | public class UIRuntimeListMenuItemTest extends UIMenuItemTestBase { |
| 36 | + private Path tempPath; |
| 37 | + |
17 | 38 | @Start |
18 | | - public void setup(Stage stage) { |
| 39 | + public void setup(Stage stage) throws IOException { |
| 40 | + tempPath = Files.createTempDirectory("i18ntest"); |
19 | 41 | init(stage); |
20 | 42 | } |
21 | 43 |
|
22 | 44 | @AfterEach |
23 | | - protected void closeWindow() { |
| 45 | + protected void closeWindow() throws IOException { |
24 | 46 | Platform.runLater(() -> stage.close()); |
| 47 | + Files.walk(tempPath) |
| 48 | + .sorted(Comparator.reverseOrder()) |
| 49 | + .map(Path::toFile) |
| 50 | + .forEach(File::delete); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testListInCustomMode(FxRobot robot) throws Exception { |
| 55 | + VariableNameGenerator vng = new VariableNameGenerator(menuTree, false); |
| 56 | + var uiList = editorUI.createPanelForMenuItem(new RuntimeListMenuItemBuilder() |
| 57 | + .withExisting((RuntimeListMenuItem) menuTree.getMenuById(10).orElseThrow()) |
| 58 | + .withInitialRows(10).withCreationMode(RuntimeListMenuItem.ListCreationMode.CUSTOM_RTCALL) |
| 59 | + .menuItem(), menuTree, vng, mockedConsumer); |
| 60 | + if(uiList.isEmpty()) throw new IllegalArgumentException("No menu item found"); |
| 61 | + createMainPanel(uiList); |
| 62 | + |
| 63 | + performAllCommonChecks(uiList.get().getMenuItem(), false, false); |
| 64 | + |
| 65 | + verifyThat("#initialRowsSpinner", (Spinner<Integer> spinner) -> spinner.getValue() == 10); |
25 | 66 | } |
26 | 67 |
|
27 | 68 | @Test |
28 | | - public void testListCanConstruct() throws Exception { |
| 69 | + public void testListInArrayMode(FxRobot robot) throws Exception { |
29 | 70 | VariableNameGenerator vng = new VariableNameGenerator(menuTree, false); |
30 | | - var uiList = editorUI.createPanelForMenuItem(menuTree.getMenuById(10).get(), menuTree, vng, mockedConsumer); |
| 71 | + RuntimeListMenuItem menuItem = new RuntimeListMenuItemBuilder() |
| 72 | + .withExisting((RuntimeListMenuItem) menuTree.getMenuById(10).orElseThrow()) |
| 73 | + .withInitialRows(10).withCreationMode(RuntimeListMenuItem.ListCreationMode.FLASH_ARRAY) |
| 74 | + .menuItem(); |
| 75 | + MenuItemHelper.setMenuState(menuItem, List.of("%list1", "%list2"), menuTree); |
| 76 | + |
| 77 | + var uiList = editorUI.createPanelForMenuItem(menuItem, menuTree, vng, mockedConsumer); |
31 | 78 | if(uiList.isEmpty()) throw new IllegalArgumentException("No menu item found"); |
32 | 79 | createMainPanel(uiList); |
33 | 80 |
|
34 | 81 | performAllCommonChecks(uiList.get().getMenuItem(), false, false); |
| 82 | + verifyThat("#initialRowsSpinner", Node::isDisable); |
| 83 | + FxAssert.verifyThat("#enumList", ListViewMatchers.hasListCell("%list1")); |
| 84 | + FxAssert.verifyThat("#enumList", ListViewMatchers.hasListCell("%list2")); |
| 85 | + |
| 86 | + robot.clickOn("#addEnumEntry"); |
| 87 | + FxAssert.verifyThat("#enumList", ListViewMatchers.hasItems(3)); |
| 88 | + FxAssert.verifyThat("#enumList", ListViewMatchers.hasListCell("ChangeMe")); |
| 89 | + |
| 90 | + var item = menuTree.getMenuById(10).orElseThrow(); |
| 91 | + assertThat((List<String>)MenuItemHelper.getValueFor(item, menuTree)).containsExactly("%list1", "%list2", "ChangeMe"); |
| 92 | + |
| 93 | + var errorText = "WARNING List values: no locale entry in bundle"; |
| 94 | + verifyThat("#uiItemErrors", (Label l)-> l.getText().contains(errorText) && l.isVisible()); |
| 95 | + verifyThat("#initialRowsSpinner", (Spinner<Integer> s)-> s.getValue() == 3); |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + protected LocaleMappingHandler getTestLocaleHandler() { |
| 101 | + try { |
| 102 | + var coreFile = tempPath.resolve("temp.properties"); |
| 103 | + Files.writeString(coreFile, """ |
| 104 | + menu.item.name=hello world |
| 105 | + list1=abc |
| 106 | + """); |
| 107 | + return new PropertiesLocaleEnabledHandler(new SafeBundleLoader(tempPath, "temp")); |
| 108 | + } catch (IOException e) { |
| 109 | + throw new RuntimeException(e); |
| 110 | + } |
35 | 111 | } |
36 | 112 | } |
0 commit comments