SettingsDialogController { public void setRootCategories(Collection<Category> rootCategories) { rootItem.getChildren().clear(); for (Category rootCategory : rootCategories) { TreeItem<Category> item = new TreeItem<>(rootCategory); addSubcategories(item); rootItem.getChildren().add(item); } }  void setRootCategories(Collection<Category> rootCategories); void applySettings();  }
@Test public void testSingleCategory() { Category category = Category.of("Category", Group.of("Group 1", Setting.of("Setting 1", new SimpleBooleanProperty()), Setting.of("Setting 2", new SimpleBooleanProperty()) ), Group.of("Group 2", Setting.of("Setting 3", new SimpleDoubleProperty()), Setting.of("Setting 4", new SimpleDoubleProperty()) ) ); FxUtils.runOnFxThread(() -> controller.setRootCategories(Collections.singleton(category))); waitForFxEvents(); TreeView<Category> tree = lookup(".settings-categories").query(); ObservableList<TreeItem<Category>> children = tree.getRoot().getChildren(); assertEquals(1, children.size(), "Only one category should be present"); assertEquals(category, children.get(0).getValue(), "Root category was wrong"); PropertySheet propertySheet = (PropertySheet) root.lookup(".property-sheet"); assertNotNull(propertySheet, "Property sheet for category was not added"); Set<Label> headers = lookup(".h5").queryAll(); assertAll("Check header text", () -> assertEquals("Group 1", Iterables.get(headers, 0).getText(), "Different first header text"), () -> assertEquals("Group 2", Iterables.get(headers, 1).getText(), "Different second header text") ); assertAll("Check presence of editors for the settings", () -> assertNotNull(lookup(TextMatchers.hasText("Setting 1")).query()), () -> assertNotNull(lookup(TextMatchers.hasText("Setting 2")).query()), () -> assertNotNull(lookup(TextMatchers.hasText("Setting 3")).query()), () -> assertNotNull(lookup(TextMatchers.hasText("Setting 4")).query()) ); }
@Test public void testMultipleCategories() { Category categoryA = Category.of("A"); Category categoryB = Category.of("B"); Category categoryC = Category.of("C"); FxUtils.runOnFxThread(() -> controller.setRootCategories(ImmutableList.of(categoryA, categoryB, categoryC))); waitForFxEvents(); TreeView<Category> tree = lookup(".settings-categories").query(); ObservableList<TreeItem<Category>> children = tree.getRoot().getChildren(); assertEquals(3, children.size(), "Three children should be present"); assertEquals(categoryA, children.get(0).getValue()); assertEquals(categoryB, children.get(1).getValue()); assertEquals(categoryC, children.get(2).getValue()); assertEquals(children.get(0), tree.getSelectionModel().getSelectedItem(), "The first child should be selected"); }
@Test public void testSubCategories() { Category subA1 = Category.of("Sub A1"); Category subA2 = Category.of("Sub A2", ImmutableList.of(subA1), ImmutableList.of()); Category rootA = Category.of("Root A", ImmutableList.of(subA2), ImmutableList.of()); Category rootB = Category.of("Root B", ImmutableList.of(subA1), ImmutableList.of()); FxUtils.runOnFxThread(() -> controller.setRootCategories(ImmutableList.of(rootA, rootB))); waitForFxEvents(); TreeView<Category> tree = lookup(".settings-categories").query(); ObservableList<TreeItem<Category>> children = tree.getRoot().getChildren(); assertEquals(2, children.size(), "Two children should be present"); assertEquals(rootA, children.get(0).getValue()); assertEquals(rootB, children.get(1).getValue()); TreeItem<Category> rootAItem = children.get(0); assertEquals(1, rootAItem.getChildren().size()); TreeItem<Category> subA2Item = rootAItem.getChildren().get(0); assertEquals(subA2, subA2Item.getValue()); assertEquals(1, subA2Item.getChildren().size()); assertEquals(subA1, subA2Item.getChildren().get(0).getValue()); TreeItem<Category> rootBItem = children.get(1); assertEquals(1, rootBItem.getChildren().size()); assertEquals(subA1, rootBItem.getChildren().get(0).getValue()); }