Repository navigation
Add headless JUnit tests for docking, layout persistence and themes #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
27fe447
Add tests for theme configuration
danilovesky b178302
Add tests for docking ports and siblings
danilovesky 4857fe8
Add tests for layout and restoration
danilovesky d28766f
Add tests for perspective persistence
danilovesky fe7d8ce
Add tests for split proportion on import
danilovesky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
...erspective/src/test/java/org/flexdock/perspective/persist/FilePersistenceHandlerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| package org.flexdock.perspective.persist; | ||
|
|
||
| import org.flexdock.perspective.Perspective; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.io.TempDir; | ||
|
|
||
| import java.io.File; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| class FilePersistenceHandlerTest { | ||
|
|
||
| private static PerspectiveModel createModel() { | ||
| Perspective perspective = new Perspective("one", "First perspective"); | ||
|
|
||
| return new PerspectiveModel("one", "one", new Perspective[] {perspective}); | ||
| } | ||
|
|
||
| @Test | ||
| void testNullKeyUsesDefaultFile(@TempDir File dir) throws Exception { | ||
| File defaultFile = new File(dir, "default.xml"); | ||
| FilePersistenceHandler handler = new FilePersistenceHandler(defaultFile); | ||
|
|
||
| assertEquals(defaultFile, handler.getPerspectiveFile(null)); | ||
|
|
||
| handler.store(null, createModel()); | ||
|
|
||
| assertTrue(defaultFile.isFile()); | ||
| assertTrue(defaultFile.length() > 0); | ||
| } | ||
|
|
||
| @Test | ||
| void testKeyWithSeparatorIsUsedAsPath(@TempDir File dir) throws Exception { | ||
| FilePersistenceHandler handler = new FilePersistenceHandler(new File(dir, "default.xml")); | ||
| File keyFile = new File(dir, "sub/key.xml"); | ||
|
|
||
| assertEquals(keyFile, handler.getPerspectiveFile(keyFile.getPath())); | ||
|
|
||
| handler.store(keyFile.getPath(), createModel()); | ||
|
|
||
| // The missing directory is created and the default file is left alone | ||
| assertTrue(keyFile.isFile()); | ||
| assertFalse(new File(dir, "default.xml").exists()); | ||
| } | ||
|
|
||
| @Test | ||
| void testKeyWithoutSeparatorIsFileInDefaultDirectory(@TempDir File dir) { | ||
| FilePersistenceHandler handler = new FilePersistenceHandler(new File(dir, "default.xml")); | ||
|
|
||
| File file = handler.getPerspectiveFile("name.xml"); | ||
|
|
||
| // Only the path is checked: storing would write to the user's home directory | ||
| assertEquals("name.xml", file.getName()); | ||
| assertEquals(FilePersistenceHandler.DEFAULT_PERSPECTIVE_DIR.getAbsoluteFile(), | ||
| file.getAbsoluteFile().getParentFile()); | ||
| } | ||
|
|
||
| @Test | ||
| void testLoadOfMissingFileGivesNull(@TempDir File dir) throws Exception { | ||
| FilePersistenceHandler handler = new FilePersistenceHandler(new File(dir, "default.xml")); | ||
|
|
||
| assertNull(handler.load(null)); | ||
| assertNull(handler.load(new File(dir, "missing.xml").getPath())); | ||
| assertFalse(new File(dir, "default.xml").exists()); | ||
| } | ||
|
|
||
| @Test | ||
| void testStoredModelIsLoadedBack(@TempDir File dir) throws Exception { | ||
| FilePersistenceHandler handler = new FilePersistenceHandler(new File(dir, "default.xml")); | ||
|
|
||
| handler.store(null, createModel()); | ||
| PerspectiveModel result = handler.load(null); | ||
|
|
||
| assertNotNull(result); | ||
| assertEquals("one", result.getDefaultPerspective()); | ||
| assertEquals("one", result.getCurrentPerspective()); | ||
| assertEquals(1, result.getPerspectives().length); | ||
| assertEquals("First perspective", result.getPerspectives()[0].getName()); | ||
| } | ||
|
|
||
| } |
127 changes: 127 additions & 0 deletions
127
...ective/src/test/java/org/flexdock/perspective/persist/xml/DockingStateSerializerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| package org.flexdock.perspective.persist.xml; | ||
|
|
||
| import org.flexdock.docking.DockingConstants; | ||
| import org.flexdock.docking.state.DockingPath; | ||
| import org.flexdock.docking.state.DockingState; | ||
| import org.flexdock.docking.state.MinimizationManager; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.w3c.dom.Element; | ||
|
|
||
| import java.awt.Point; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| class DockingStateSerializerTest { | ||
|
|
||
| private final DockingStateSerializer serializer = new DockingStateSerializer(); | ||
|
|
||
| @BeforeAll | ||
| static void registerSerializers() { | ||
| XMLPersister.newDefaultInstance(); | ||
| } | ||
|
|
||
| private DockingState roundTrip(DockingState state) throws Exception { | ||
| return (DockingState) XmlRoundTrip.roundTrip(serializer, state); | ||
| } | ||
|
|
||
| @Test | ||
| void testOpenedStateKeepsParentRegionAndRatio() throws Exception { | ||
| DockingState state = new DockingState("view"); | ||
| state.setRelativeParentId("parent"); | ||
| state.setRegion(DockingConstants.WEST_REGION); | ||
| state.setSplitRatio(0.25f); | ||
|
|
||
| DockingState result = roundTrip(state); | ||
|
|
||
| assertEquals("view", result.getDockableId()); | ||
| assertEquals("parent", result.getRelativeParentId()); | ||
| assertEquals(DockingConstants.WEST_REGION, result.getRegion()); | ||
| assertEquals(0.25f, result.getSplitRatio()); | ||
| assertFalse(result.isMinimized()); | ||
| assertFalse(result.isFloating()); | ||
| } | ||
|
|
||
| @Test | ||
| void testDefaultsAreNotWritten() throws Exception { | ||
| DockingState state = new DockingState("view"); | ||
| state.setRegion(DockingConstants.CENTER_REGION); | ||
|
|
||
| Element element = XmlRoundTrip.serialize(serializer, state); | ||
|
|
||
| assertFalse(element.hasAttribute(PersistenceConstants.DOCKING_STATE_ATTRIBUTE_RELATIVE_PARENT_ID)); | ||
| assertFalse(element.hasAttribute(PersistenceConstants.DOCKING_STATE_ATTRIBUTE_SPLIT_RATIO)); | ||
| assertFalse(element.hasAttribute(PersistenceConstants.DOCKING_STATE_ATTRIBUTE_FLOATING_GROUP_NAME)); | ||
| assertFalse(element.hasAttribute(PersistenceConstants.DOCKING_STATE_ATTRIBUTE_MINIMIZE_CONSTRAINT)); | ||
|
|
||
| DockingState result = (DockingState) serializer.deserialize(element); | ||
|
|
||
| assertNull(result.getRelativeParentId()); | ||
| assertEquals(DockingConstants.UNINITIALIZED_RATIO, result.getSplitRatio()); | ||
| assertFalse(result.hasCenterPoint()); | ||
| assertFalse(result.hasDockingPath()); | ||
| } | ||
|
|
||
| @Test | ||
| void testMinimizedConstraintsSurvive() throws Exception { | ||
| int[] constraints = { | ||
| MinimizationManager.LEFT, | ||
| MinimizationManager.RIGHT, | ||
| MinimizationManager.TOP, | ||
| MinimizationManager.BOTTOM, | ||
| MinimizationManager.CENTER | ||
| }; | ||
|
|
||
| for (int constraint : constraints) { | ||
| DockingState state = new DockingState("view"); | ||
| state.setRegion(DockingConstants.CENTER_REGION); | ||
| state.setMinimizedConstraint(constraint); | ||
|
|
||
| DockingState result = roundTrip(state); | ||
|
|
||
| assertTrue(result.isMinimized(), "constraint " + constraint); | ||
| assertEquals(constraint, result.getMinimizedConstraint()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void testFloatingGroupSurvives() throws Exception { | ||
| DockingState state = new DockingState("view"); | ||
| state.setRegion(DockingConstants.CENTER_REGION); | ||
| state.setFloatingGroup("group"); | ||
|
|
||
| DockingState result = roundTrip(state); | ||
|
|
||
| assertTrue(result.isFloating()); | ||
| assertEquals("group", result.getFloatingGroup()); | ||
| assertFalse(result.isMinimized()); | ||
| } | ||
|
|
||
| @Test | ||
| void testCenterPointAndPathSurvive() throws Exception { | ||
| DockingPath path = new DockingPath(); | ||
| path.setRootPortId("root"); | ||
| DockingState state = new DockingState("view"); | ||
| state.setRegion(DockingConstants.CENTER_REGION); | ||
| state.setCenter(new Point(30, 70)); | ||
| state.setPath(path); | ||
|
|
||
| DockingState result = roundTrip(state); | ||
|
|
||
| assertTrue(result.hasCenterPoint()); | ||
| assertEquals(new Point(30, 70), result.getCenterPoint()); | ||
| assertTrue(result.hasDockingPath()); | ||
| assertEquals("root", result.getPath().getRootPortId()); | ||
| } | ||
|
|
||
| @Test | ||
| void testRegionIsWrittenInLowerCase() throws Exception { | ||
| DockingState state = new DockingState("view"); | ||
| state.setRegion(DockingConstants.SOUTH_REGION); | ||
|
|
||
| Element element = XmlRoundTrip.serialize(serializer, state); | ||
|
|
||
| assertEquals("south", element.getAttribute(PersistenceConstants.DOCKING_STATE_ATTRIBUTE_REGION)); | ||
| } | ||
|
|
||
| } |
70 changes: 70 additions & 0 deletions
70
...ctive/src/test/java/org/flexdock/perspective/persist/xml/FloatingGroupSerializerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package org.flexdock.perspective.persist.xml; | ||
|
|
||
| import org.flexdock.docking.state.FloatingGroup; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.awt.Rectangle; | ||
| import java.util.HashSet; | ||
| import java.util.Iterator; | ||
| import java.util.Set; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| class FloatingGroupSerializerTest { | ||
|
|
||
| private final FloatingGroupSerializer serializer = new FloatingGroupSerializer(); | ||
|
|
||
| @BeforeAll | ||
| static void registerSerializers() { | ||
| XMLPersister.newDefaultInstance(); | ||
| } | ||
|
|
||
| private static Set<String> getDockableIds(FloatingGroup group) { | ||
| Set<String> ids = new HashSet<>(); | ||
| for (Iterator<?> it = group.getDockableIterator(); it.hasNext();) { | ||
| ids.add((String) it.next()); | ||
| } | ||
|
|
||
| return ids; | ||
| } | ||
|
|
||
| @Test | ||
| void testNameBoundsAndDockablesSurvive() throws Exception { | ||
| FloatingGroup group = new FloatingGroup("group"); | ||
| group.setBounds(new Rectangle(10, 20, 300, 200)); | ||
| group.addDockable("first"); | ||
| group.addDockable("second"); | ||
|
|
||
| FloatingGroup result = (FloatingGroup) XmlRoundTrip.roundTrip(serializer, group); | ||
|
|
||
| assertEquals("group", result.getName()); | ||
| assertEquals(new Rectangle(10, 20, 300, 200), result.getBounds()); | ||
| assertEquals(2, result.getDockableCount()); | ||
| assertEquals(getDockableIds(group), getDockableIds(result)); | ||
| } | ||
|
|
||
| @Test | ||
| void testGroupWithoutDockablesSurvives() throws Exception { | ||
| FloatingGroup group = new FloatingGroup("empty"); | ||
| group.setBounds(new Rectangle(0, 0, 100, 50)); | ||
|
|
||
| FloatingGroup result = (FloatingGroup) XmlRoundTrip.roundTrip(serializer, group); | ||
|
|
||
| assertEquals("empty", result.getName()); | ||
| assertEquals(new Rectangle(0, 0, 100, 50), result.getBounds()); | ||
| assertEquals(0, result.getDockableCount()); | ||
| } | ||
|
|
||
| // Known bug (https://github.com/opencollab/flexdock/issues/32): | ||
| // a group whose bounds were never set cannot be serialized (NPE in | ||
| // RectangleSerializer). Once fixed, assertThrows fails: round-trip the group instead and | ||
| // check that its bounds stay null. | ||
| @Test | ||
| void testGroupWithoutBoundsKnownBug() { | ||
| FloatingGroup group = new FloatingGroup("unbounded"); | ||
|
|
||
| assertThrows(NullPointerException.class, () -> XmlRoundTrip.roundTrip(serializer, group)); | ||
| } | ||
|
|
||
| } | ||
56 changes: 56 additions & 0 deletions
56
...tive/src/test/java/org/flexdock/perspective/persist/xml/LayoutSequenceSerializerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package org.flexdock.perspective.persist.xml; | ||
|
|
||
| import org.flexdock.docking.DockingConstants; | ||
| import org.flexdock.docking.state.DockingState; | ||
| import org.flexdock.perspective.LayoutSequence; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| class LayoutSequenceSerializerTest { | ||
|
|
||
| private final LayoutSequenceSerializer serializer = new LayoutSequenceSerializer(); | ||
|
|
||
| @BeforeAll | ||
| static void registerSerializers() { | ||
| XMLPersister.newDefaultInstance(); | ||
| } | ||
|
|
||
| @Test | ||
| void testOrderParentsRegionsAndRatiosSurvive() throws Exception { | ||
| LayoutSequence sequence = new LayoutSequence(); | ||
| sequence.add("first"); | ||
| sequence.add("second", "first", DockingConstants.WEST_REGION, 0.3f); | ||
| sequence.add("third", "second", DockingConstants.SOUTH_REGION, 0.6f); | ||
|
|
||
| LayoutSequence result = (LayoutSequence) XmlRoundTrip.roundTrip(serializer, sequence); | ||
|
|
||
| List<?> states = result.getDockingStates(); | ||
| assertEquals(3, states.size()); | ||
| DockingState first = (DockingState) states.get(0); | ||
| DockingState second = (DockingState) states.get(1); | ||
| DockingState third = (DockingState) states.get(2); | ||
|
|
||
| assertEquals("first", first.getDockableId()); | ||
| assertNull(first.getRelativeParentId()); | ||
| assertEquals("second", second.getDockableId()); | ||
| assertEquals("first", second.getRelativeParentId()); | ||
| assertEquals(DockingConstants.WEST_REGION, second.getRegion()); | ||
| assertEquals(0.3f, second.getSplitRatio()); | ||
| assertEquals("third", third.getDockableId()); | ||
| assertEquals("second", third.getRelativeParentId()); | ||
| assertEquals(DockingConstants.SOUTH_REGION, third.getRegion()); | ||
| assertEquals(0.6f, third.getSplitRatio()); | ||
| } | ||
|
|
||
| @Test | ||
| void testEmptySequenceSurvives() throws Exception { | ||
| LayoutSequence result = (LayoutSequence) XmlRoundTrip.roundTrip(serializer, new LayoutSequence()); | ||
|
|
||
| assertTrue(result.getDockingStates().isEmpty()); | ||
| } | ||
|
|
||
| } |
53 changes: 53 additions & 0 deletions
53
...ve/src/test/java/org/flexdock/perspective/persist/xml/PerspectiveModelSerializerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package org.flexdock.perspective.persist.xml; | ||
|
|
||
| import org.flexdock.perspective.LayoutSequence; | ||
| import org.flexdock.perspective.Perspective; | ||
| import org.flexdock.perspective.persist.PerspectiveModel; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| class PerspectiveModelSerializerTest { | ||
|
|
||
| private final PerspectiveModelSerializer serializer = new PerspectiveModelSerializer(); | ||
|
|
||
| @BeforeAll | ||
| static void registerSerializers() { | ||
| XMLPersister.newDefaultInstance(); | ||
| } | ||
|
|
||
| @Test | ||
| void testIdsAndPerspectivesSurvive() throws Exception { | ||
| LayoutSequence sequence = new LayoutSequence(); | ||
| sequence.add("first"); | ||
| Perspective withSequence = new Perspective("one", "First perspective"); | ||
| withSequence.setInitialSequence(sequence); | ||
| Perspective withoutSequence = new Perspective("two", "Second perspective"); | ||
| PerspectiveModel model = new PerspectiveModel("one", "two", new Perspective[] {withSequence, withoutSequence}); | ||
|
|
||
| PerspectiveModel result = (PerspectiveModel) XmlRoundTrip.roundTrip(serializer, model); | ||
|
|
||
| assertEquals("one", result.getDefaultPerspective()); | ||
| assertEquals("two", result.getCurrentPerspective()); | ||
| Perspective[] perspectives = result.getPerspectives(); | ||
| assertEquals(2, perspectives.length); | ||
| assertEquals("one", perspectives[0].getPersistentId()); | ||
| assertEquals("First perspective", perspectives[0].getName()); | ||
| assertEquals(1, perspectives[0].getInitialSequence().getDockingStates().size()); | ||
| assertEquals("two", perspectives[1].getPersistentId()); | ||
| assertNull(perspectives[1].getInitialSequence()); | ||
| } | ||
|
|
||
| @Test | ||
| void testModelWithoutPerspectivesSurvives() throws Exception { | ||
| PerspectiveModel model = new PerspectiveModel("default", "current", new Perspective[0]); | ||
|
|
||
| PerspectiveModel result = (PerspectiveModel) XmlRoundTrip.roundTrip(serializer, model); | ||
|
|
||
| assertEquals("default", result.getDefaultPerspective()); | ||
| assertEquals("current", result.getCurrentPerspective()); | ||
| assertEquals(0, result.getPerspectives().length); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.