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