MenuUtils { public static MenuBar buildMenuBar() { ClassLoader classLoader = ClassLoader.getSystemClassLoader(); String licenseFileAsString = loadLicenseFileAsString(classLoader); @Nullable Optional<String> versionNumber = VersionInfo.findVersionInfo(VersionInfo.artifactId, false); MenuItem licenseMenuItem = new MenuItem(licenseFileAsString); Menu versionMenu = new Menu(versionNumber.map(value -> "GazePlay " + value).orElse("GazePlay unreleased version")); versionMenu.getItems().add(licenseMenuItem); String username = ActiveConfigurationContext.getInstance().getUserName(); Menu profileMenu = new Menu("Current Profile: " + (username.isEmpty() ? "Default" : username)); MenuBar menuBar = new MenuBar(); menuBar.getMenus().addAll(versionMenu, profileMenu); menuBar.setPrefHeight(40); menuBar.setPrefWidth(80); return menuBar; }  static MenuBar buildMenuBar();  }
@Test void givenVersionIsUnknown_givenProfileIsDefault_shouldBuildMenuBar() { new MockUp<VersionInfo>() { @mockit.Mock public Optional<String> findVersionInfo(String id, boolean time) { return Optional.empty(); } }; MenuBar menuBar = MenuUtils.buildMenuBar(); assertEquals("GazePlay unreleased version", menuBar.getMenus().get(0).getText()); assertEquals("Current Profile: Default", menuBar.getMenus().get(1).getText()); }
@Test void givenVersionIsKnown_givenProfileIsNamed_shouldBuildMenuBar() { when(mockConfiguration.getUserName()).thenReturn("test user name"); new MockUp<ActiveConfigurationContext>() { @mockit.Mock public Configuration getInstance() { return mockConfiguration; } }; new MockUp<VersionInfo>() { @mockit.Mock public Optional<String> findVersionInfo(String id, boolean time) { return Optional.of("v1.2.3"); } }; MenuBar menuBar = MenuUtils.buildMenuBar(); assertEquals("GazePlay v1.2.3", menuBar.getMenus().get(0).getText()); assertEquals("Current Profile: test user name", menuBar.getMenus().get(1).getText()); }
@Test void givenLicenseFileIsFound_shouldCreateLicenseMenu() { MenuBar menuBar = MenuUtils.buildMenuBar(); MenuItem license = menuBar.getMenus().get(0).getItems().get(0); assertTrue(license.getText().contains("test licence")); }