1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs.layer;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 | import static org.junit.Assert.assertFalse;
|
---|
6 | import static org.junit.Assert.assertTrue;
|
---|
7 |
|
---|
8 | import org.junit.BeforeClass;
|
---|
9 | import org.junit.Rule;
|
---|
10 | import org.junit.Test;
|
---|
11 | import org.openstreetmap.josm.JOSMFixture;
|
---|
12 | import org.openstreetmap.josm.Main;
|
---|
13 | import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
|
---|
14 | import org.openstreetmap.josm.gui.dialogs.LayerListDialog.LayerListModel;
|
---|
15 | import org.openstreetmap.josm.gui.layer.TMSLayer;
|
---|
16 | import org.openstreetmap.josm.gui.layer.TMSLayerTest;
|
---|
17 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
18 |
|
---|
19 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Unit tests of {@link LayerVisibilityAction} class.
|
---|
23 | */
|
---|
24 | public class LayerVisibilityActionTest {
|
---|
25 | /**
|
---|
26 | * TMS layer needs prefs. Platform for LayerListDialog shortcuts.
|
---|
27 | */
|
---|
28 | @Rule
|
---|
29 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
30 | public JOSMTestRules test = new JOSMTestRules().preferences().projection().platform();
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Somewhere deep down the dependencies we need shortcuts
|
---|
34 | */
|
---|
35 | @BeforeClass
|
---|
36 | public static void setUp() {
|
---|
37 | JOSMFixture.createUnitTestFixture().init(true);
|
---|
38 | }
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Unit test of {@link LayerVisibilityAction} class.
|
---|
42 | */
|
---|
43 | @Test
|
---|
44 | public void testLayerVisibilityAction() {
|
---|
45 | TMSLayer layer = TMSLayerTest.createTmsLayer();
|
---|
46 | LayerListModel model = new LayerListDialog(Main.getLayerManager()) {
|
---|
47 | @Override
|
---|
48 | protected void registerInWindowMenu() {
|
---|
49 | // ignore
|
---|
50 | }
|
---|
51 | }.getModel();
|
---|
52 | LayerVisibilityAction action = new LayerVisibilityAction(model);
|
---|
53 | action.updateEnabledState();
|
---|
54 | assertFalse(action.isEnabled());
|
---|
55 |
|
---|
56 | Main.getLayerManager().addLayer(layer);
|
---|
57 | model.setSelectedLayer(layer);
|
---|
58 | action.updateEnabledState();
|
---|
59 | assertTrue(action.isEnabled());
|
---|
60 | assertTrue(action.supportLayers(model.getSelectedLayers()));
|
---|
61 |
|
---|
62 | // now check values
|
---|
63 | action.updateValues();
|
---|
64 | assertEquals(1.0, action.opacitySlider.getRealValue(), 1e-15);
|
---|
65 | assertEquals("OpacitySlider [getRealValue()=1.0]", action.opacitySlider.toString());
|
---|
66 |
|
---|
67 | action.opacitySlider.setRealValue(.5);
|
---|
68 | action.updateValues();
|
---|
69 |
|
---|
70 | assertEquals(0.5, action.opacitySlider.getRealValue(), 1e-15);
|
---|
71 | assertEquals("OpacitySlider [getRealValue()=0.5]", action.opacitySlider.toString());
|
---|
72 |
|
---|
73 | action.setVisibleFlag(false);
|
---|
74 | action.updateValues();
|
---|
75 | assertFalse(layer.isVisible());
|
---|
76 |
|
---|
77 | action.setVisibleFlag(true);
|
---|
78 | action.updateValues();
|
---|
79 | assertTrue(layer.isVisible());
|
---|
80 |
|
---|
81 | // layer stays visible during adjust
|
---|
82 | action.opacitySlider.slider.setValueIsAdjusting(true);
|
---|
83 | action.opacitySlider.setRealValue(0);
|
---|
84 | assertEquals(0, layer.getOpacity(), 1e-15);
|
---|
85 | layer.setOpacity(.1); // to make layer.isVisible work
|
---|
86 | assertTrue(layer.isVisible());
|
---|
87 | layer.setOpacity(0);
|
---|
88 |
|
---|
89 | action.opacitySlider.slider.setValueIsAdjusting(false);
|
---|
90 | action.opacitySlider.setRealValue(0);
|
---|
91 | assertEquals(0, layer.getOpacity(), 1e-15);
|
---|
92 | layer.setOpacity(.1); // to make layer.isVisible work
|
---|
93 | assertFalse(layer.isVisible());
|
---|
94 | layer.setOpacity(0);
|
---|
95 | action.updateValues();
|
---|
96 |
|
---|
97 | // Opacity reset when it was 0 and user set layer to visible.
|
---|
98 | action.setVisibleFlag(true);
|
---|
99 | action.updateValues();
|
---|
100 | assertEquals(1.0, action.opacitySlider.getRealValue(), 1e-15);
|
---|
101 | assertEquals(1.0, layer.getOpacity(), 1e-15);
|
---|
102 | }
|
---|
103 | }
|
---|