Skip to content

Commit

Permalink
Considered OS dependency in test.
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver-Loeffler committed Jan 5, 2022
1 parent 8184303 commit b532602
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,17 @@
*/
package com.oracle.javafx.scenebuilder.kit.fxom;

import java.util.Locale;
import java.util.Objects;

import com.oracle.javafx.scenebuilder.kit.editor.EditorPlatform;

class FXMLPropertiesDisabler {
private final OperatingSystem os;
public FXMLPropertiesDisabler() {
this(OperatingSystem.get());
}
public FXMLPropertiesDisabler(OperatingSystem os) {
this.os = Objects.requireNonNull(os);
}
/**
* In some cases, during FXML Loading, certain properties must be disabled.
* This method modifies the FXML source accordingly.
Expand Down Expand Up @@ -71,10 +77,29 @@ public String disableProperties(String fxmlText) {
*/
private String disableUseSystemMenuBarProperty(String fxmlText) {
Objects.requireNonNull(fxmlText, "fxmlText must not be null");
if (EditorPlatform.IS_MAC) {
if (OperatingSystem.MACOS.equals(os)) {
return fxmlText.replace("useSystemMenuBar=\"true\"",
"useSystemMenuBar=\"false\"");
}
return fxmlText;
}

enum OperatingSystem {
MACOS,
WINDOWS,
LINUX;
public static OperatingSystem get() {
String osName = System.getProperty("os.name").toLowerCase(Locale.ROOT);
if (osName.contains("linux")) {
return OperatingSystem.LINUX;
}
if (osName.contains("mac")) {
return OperatingSystem.MACOS;
}
if (osName.contains("windows")) {
return OperatingSystem.WINDOWS;
}
throw new UnsupportedOperationException("Unknown operating system platform!");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,67 @@
*/
package com.oracle.javafx.scenebuilder.kit.fxom;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.nio.file.Files;

import org.junit.Test;

import com.oracle.javafx.scenebuilder.kit.editor.EditorPlatform;
import com.oracle.javafx.scenebuilder.kit.fxom.FXMLPropertiesDisabler.OperatingSystem;

public class FXMLPropertiesDisablerTest {

private FXMLPropertiesDisabler classUnderTest = new FXMLPropertiesDisabler();

@Test
public void that_property_value_is_set_to_false() throws Exception {
public void that_property_value_is_set_to_false_on_MacOS() throws Exception {
classUnderTest = new FXMLPropertiesDisabler(OperatingSystem.MACOS);
String fxmlText = readResourceText("ContainerWithMenu_SystemMenuBarEnabled.fxml");
assertTrue("ensures that test resource is correct",
fxmlText.contains("<MenuBar useSystemMenuBar=\"true\" VBox.vgrow=\"NEVER\" fx:id=\"theMenuBar\">"));
String modfiedFxmlText = classUnderTest.disableProperties(fxmlText);
assertTrue(modfiedFxmlText.contains("<MenuBar useSystemMenuBar=\"false\" VBox.vgrow=\"NEVER\" fx:id=\"theMenuBar\">"));
}

@Test
public void that_property_value_is_not_modified_on_Windows() throws Exception {
classUnderTest = new FXMLPropertiesDisabler(OperatingSystem.WINDOWS);
String fxmlText = readResourceText("ContainerWithMenu_SystemMenuBarEnabled.fxml");
assertTrue("ensures that test resource is correct",
fxmlText.contains("<MenuBar useSystemMenuBar=\"true\" VBox.vgrow=\"NEVER\" fx:id=\"theMenuBar\">"));
String modfiedFxmlText = classUnderTest.disableProperties(fxmlText);
assertTrue(modfiedFxmlText.contains("<MenuBar useSystemMenuBar=\"true\" VBox.vgrow=\"NEVER\" fx:id=\"theMenuBar\">"));
}

private String readResourceText(String resourceName) throws Exception {
File fxmlFileName = new File(getClass().getResource(resourceName).toURI());
return Files.readString(fxmlFileName.toPath());
}

@Test
public void that_MacOS_is_detected_properly() {
if (EditorPlatform.IS_MAC) {
OperatingSystem os = OperatingSystem.get();
assertEquals(OperatingSystem.MACOS, os);
}
}

@Test
public void that_Windows_is_detected_properly() {
if (EditorPlatform.IS_WINDOWS) {
OperatingSystem os = OperatingSystem.get();
assertEquals(OperatingSystem.WINDOWS, os);
}
}

@Test
public void that_Linux_is_detected_properly() {
if (EditorPlatform.IS_LINUX) {
OperatingSystem os = OperatingSystem.get();
assertEquals(OperatingSystem.LINUX, os);
}
}
}

0 comments on commit b532602

Please sign in to comment.