| 1 | // License: GPL. For details, see LICENSE file. |
| 2 | package org.openstreetmap.josm.gui.layer; |
| 3 | |
| 4 | import static org.junit.Assert.assertEquals; |
| 5 | import static org.junit.Assert.assertFalse; |
| 6 | import static org.junit.Assert.assertNotNull; |
| 7 | import static org.junit.Assert.assertNull; |
| 8 | import static org.junit.Assert.assertTrue; |
| 9 | |
| 10 | import java.awt.Color; |
| 11 | import java.io.File; |
| 12 | |
| 13 | import org.junit.Before; |
| 14 | import org.junit.Rule; |
| 15 | import org.junit.Test; |
| 16 | import org.openstreetmap.josm.Main; |
| 17 | import org.openstreetmap.josm.data.preferences.AbstractProperty; |
| 18 | import org.openstreetmap.josm.data.preferences.ColorProperty; |
| 19 | import org.openstreetmap.josm.testutils.JOSMTestRules; |
| 20 | |
| 21 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
| 22 | |
| 23 | /** |
| 24 | * Test of the base {@link Layer} class |
| 25 | * @author Michael Zangl |
| 26 | * @since xxx |
| 27 | */ |
| 28 | public class LayerTest { |
| 29 | /** |
| 30 | * We need projection |
| 31 | */ |
| 32 | @Rule |
| 33 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") |
| 34 | public JOSMTestRules test = new JOSMTestRules().preferences().projection(); |
| 35 | |
| 36 | private Layer testLayer; |
| 37 | |
| 38 | /** |
| 39 | * Create test layer |
| 40 | */ |
| 41 | @Before |
| 42 | public void setUp() { |
| 43 | testLayer = new LayerManagerTest.TestLayer(); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Test {@link Layer#getColorProperty()} |
| 48 | */ |
| 49 | @Test |
| 50 | public void testGetColorProperty() { |
| 51 | assertEquals(null, testLayer.getColorProperty()); |
| 52 | |
| 53 | AbstractProperty<Color> color = new LayerManagerTest.TestLayer() { |
| 54 | @Override |
| 55 | protected ColorProperty getBaseColorProperty() { |
| 56 | return new ColorProperty("x", Color.BLACK); |
| 57 | }; |
| 58 | }.getColorProperty(); |
| 59 | |
| 60 | assertEquals(Color.BLACK, color.get()); |
| 61 | assertEquals(Color.BLACK, color.getDefaultValue()); |
| 62 | assertEquals("color.layer.test.layer", color.getKey()); |
| 63 | } |
| 64 | |
| 65 | @Test |
| 66 | public void testIsInfoResizable() { |
| 67 | assertFalse(testLayer.isInfoResizable()); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Test of {@link Layer#getAssociatedFile()} and {@link Layer#setAssociatedFile(java.io.File)} |
| 72 | */ |
| 73 | @Test |
| 74 | public void testAssociatedFile() { |
| 75 | assertNull(testLayer.getAssociatedFile()); |
| 76 | |
| 77 | File file = new File("test"); |
| 78 | testLayer.setAssociatedFile(file); |
| 79 | assertEquals(file, testLayer.getAssociatedFile()); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Test {@link Layer#getName()} |
| 84 | */ |
| 85 | @Test |
| 86 | public void testGetName() { |
| 87 | assertEquals("Test Layer", testLayer.getName()); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Test {@link Layer#setName(String)} |
| 92 | */ |
| 93 | @Test |
| 94 | public void testSetName() { |
| 95 | testLayer.setName("Test Layer2"); |
| 96 | assertEquals("Test Layer2", testLayer.getName()); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Test {@link Layer#rename(String)} and {@link Layer#isRenamed()} |
| 101 | */ |
| 102 | @Test |
| 103 | public void testRename() { |
| 104 | assertFalse(testLayer.isRenamed()); |
| 105 | testLayer.rename("Test Layer2"); |
| 106 | assertEquals("Test Layer2", testLayer.getName()); |
| 107 | assertTrue(testLayer.isRenamed()); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Test {@link Layer#isBackgroundLayer()} and {@link Layer#setBackgroundLayer(boolean)} |
| 112 | */ |
| 113 | @Test |
| 114 | public void testBackgroundLayer() { |
| 115 | assertFalse(testLayer.isBackgroundLayer()); |
| 116 | testLayer.setBackgroundLayer(true); |
| 117 | assertTrue(testLayer.isBackgroundLayer()); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Test {@link Layer#isVisible()} and {@link Layer#setVisible(boolean)} |
| 122 | */ |
| 123 | @Test |
| 124 | public void testVisible() { |
| 125 | assertTrue(testLayer.isVisible()); |
| 126 | testLayer.setVisible(false); |
| 127 | assertFalse(testLayer.isVisible()); |
| 128 | testLayer.setVisible(true); |
| 129 | assertTrue(testLayer.isVisible()); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Test {@link Layer#toggleVisible()} |
| 134 | */ |
| 135 | @Test |
| 136 | public void testToggleVisible() { |
| 137 | assertTrue(testLayer.isVisible()); |
| 138 | testLayer.toggleVisible(); |
| 139 | assertFalse(testLayer.isVisible()); |
| 140 | testLayer.toggleVisible(); |
| 141 | assertTrue(testLayer.isVisible()); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Test {@link Layer#setOpacity(double)} and {@link Layer#getOpacity()} |
| 146 | */ |
| 147 | @Test |
| 148 | public void testOpacity() { |
| 149 | assertEquals(1, testLayer.getOpacity(), 1e-3); |
| 150 | |
| 151 | testLayer.setOpacity(0.5); |
| 152 | assertEquals(0.5, testLayer.getOpacity(), 1e-3); |
| 153 | |
| 154 | testLayer.setOpacity(0); |
| 155 | assertFalse(testLayer.isVisible()); |
| 156 | |
| 157 | testLayer.setVisible(true); |
| 158 | assertTrue(testLayer.isVisible()); |
| 159 | assertEquals(1, testLayer.getOpacity(), 1e-3); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Test {@link Layer#isProjectionSupported(org.openstreetmap.josm.data.projection.Projection)} |
| 164 | */ |
| 165 | @Test |
| 166 | public void testIsProjectionSupported() { |
| 167 | assertFalse(testLayer.isProjectionSupported(null)); |
| 168 | assertTrue(testLayer.isProjectionSupported(Main.getProjection())); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Test {@link Layer#nameSupportedProjections()} |
| 173 | */ |
| 174 | @Test |
| 175 | public void testNameSupportedProjections() { |
| 176 | assertNotNull(testLayer.nameSupportedProjections()); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Test {@link Layer#isSavable()} |
| 181 | */ |
| 182 | @Test |
| 183 | public void testIsSavable() { |
| 184 | assertFalse(testLayer.isSavable()); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Test {@link Layer#checkSaveConditions()} |
| 189 | */ |
| 190 | @Test |
| 191 | public void testCheckSaveConditions() { |
| 192 | assertTrue(testLayer.checkSaveConditions()); |
| 193 | } |
| 194 | |
| 195 | } |