1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.preferences;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
6 |
|
---|
7 | import java.awt.Color;
|
---|
8 | import java.util.Arrays;
|
---|
9 | import java.util.Collections;
|
---|
10 |
|
---|
11 | import javax.swing.UIManager;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.data.Preferences;
|
---|
14 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
15 | import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
|
---|
16 |
|
---|
17 | import org.junit.jupiter.api.BeforeEach;
|
---|
18 | import org.junit.jupiter.api.Test;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Test {@link NamedColorProperty}
|
---|
22 | * @author Michael Zangl
|
---|
23 | */
|
---|
24 | // This is a preference test
|
---|
25 | @BasicPreferences
|
---|
26 | class NamedColorPropertyTest {
|
---|
27 | private NamedColorProperty base;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Set up test case
|
---|
31 | */
|
---|
32 | @BeforeEach
|
---|
33 | public void createTestProperty() {
|
---|
34 | base = new NamedColorProperty("test", Color.RED);
|
---|
35 | }
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Test {@link NamedColorProperty#get()}
|
---|
39 | */
|
---|
40 | @Test
|
---|
41 | void testGet() {
|
---|
42 | assertEquals(Color.RED, base.get());
|
---|
43 |
|
---|
44 | UIManager.put("JOSM.clr.general.test", Color.GRAY);
|
---|
45 | base = new NamedColorProperty("test", Color.RED);
|
---|
46 | assertEquals(Color.GRAY, base.get());
|
---|
47 |
|
---|
48 | Config.getPref().putList("clr.general.test", Collections.singletonList("#123456"));
|
---|
49 | assertEquals(new Color(0x123456), base.get());
|
---|
50 |
|
---|
51 | Config.getPref().putList("clr.general.test", null);
|
---|
52 | UIManager.put("JOSM.clr.general.test", null);
|
---|
53 | base = new NamedColorProperty("test", Color.RED);
|
---|
54 | assertEquals(Color.RED, base.get());
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Test {@link NamedColorProperty#put}
|
---|
59 | */
|
---|
60 | @Test
|
---|
61 | void testPut() {
|
---|
62 | assertEquals(Color.RED, base.get());
|
---|
63 |
|
---|
64 | base.put(new Color(0xff00af00));
|
---|
65 | assertEquals(new Color(0xff00af00), base.get());
|
---|
66 | assertEquals("#00af00", Config.getPref().getList("clr.general.test").get(0).toLowerCase());
|
---|
67 |
|
---|
68 | base.put(null);
|
---|
69 | assertEquals(Color.RED, base.get());
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Test color alpha.
|
---|
74 | */
|
---|
75 | @Test
|
---|
76 | void testColorAlpha() {
|
---|
77 | assertEquals(0x12, new NamedColorProperty("foo", new Color(0x12345678, true)).get().getAlpha());
|
---|
78 | assertTrue(Preferences.main().putList("clr.general.bar", Arrays.asList("#34567812", "general", "", "bar")));
|
---|
79 | assertEquals(0x12, new NamedColorProperty("bar", Color.RED).get().getAlpha());
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Test color name and alpha.
|
---|
84 | */
|
---|
85 | @Test
|
---|
86 | void testColorNameAlpha() {
|
---|
87 | assertEquals(0x12, new NamedColorProperty("foo", new Color(0x12345678, true)).get().getAlpha());
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Test {@link NamedColorProperty#getChildColor(String)}
|
---|
92 | */
|
---|
93 | @Test
|
---|
94 | void testGetChildColor() {
|
---|
95 | AbstractProperty<Color> child = base.getChildColor("test2");
|
---|
96 |
|
---|
97 | assertEquals(Color.RED, child.get());
|
---|
98 |
|
---|
99 | base.put(Color.GREEN);
|
---|
100 | assertEquals(Color.GREEN, child.get());
|
---|
101 |
|
---|
102 | child.put(Color.YELLOW);
|
---|
103 | assertEquals(Color.YELLOW, child.get());
|
---|
104 | assertEquals(Color.GREEN, base.get());
|
---|
105 |
|
---|
106 | child.put(null);
|
---|
107 | assertEquals(Color.GREEN, child.get());
|
---|
108 | }
|
---|
109 | }
|
---|