1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
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 | import static org.junit.Assert.fail;
|
---|
10 |
|
---|
11 | import java.awt.Desktop;
|
---|
12 | import java.io.File;
|
---|
13 | import java.io.IOException;
|
---|
14 | import java.security.KeyStoreException;
|
---|
15 | import java.util.Collection;
|
---|
16 |
|
---|
17 | import org.junit.BeforeClass;
|
---|
18 | import org.junit.Test;
|
---|
19 | import org.openstreetmap.josm.JOSMFixture;
|
---|
20 | import org.openstreetmap.josm.TestUtils;
|
---|
21 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
22 |
|
---|
23 | import mockit.Expectations;
|
---|
24 | import mockit.Injectable;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Unit tests of {@link PlatformHookWindows} class.
|
---|
28 | */
|
---|
29 | public class PlatformHookWindowsTest {
|
---|
30 |
|
---|
31 | static PlatformHookWindows hook;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Setup test.
|
---|
35 | */
|
---|
36 | @BeforeClass
|
---|
37 | public static void setUp() {
|
---|
38 | JOSMFixture.createUnitTestFixture().init();
|
---|
39 | hook = new PlatformHookWindows();
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Test method for {@code PlatformHookWindows#startupHook}
|
---|
44 | */
|
---|
45 | @Test
|
---|
46 | public void testStartupHook() {
|
---|
47 | hook.startupHook((a, b, c, d) -> System.out.println("callback"));
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Test method for {@code PlatformHookWindows#getRootKeystore}
|
---|
52 | * @throws Exception if an error occurs
|
---|
53 | */
|
---|
54 | @Test
|
---|
55 | public void testGetRootKeystore() throws Exception {
|
---|
56 | if (PlatformManager.isPlatformWindows()) {
|
---|
57 | assertNotNull(PlatformHookWindows.getRootKeystore());
|
---|
58 | } else {
|
---|
59 | try {
|
---|
60 | PlatformHookWindows.getRootKeystore();
|
---|
61 | fail("Expected KeyStoreException");
|
---|
62 | } catch (KeyStoreException e) {
|
---|
63 | Logging.info(e.getMessage());
|
---|
64 | }
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Test method for {@code PlatformHookWindows#afterPrefStartupHook}
|
---|
70 | */
|
---|
71 | @Test
|
---|
72 | public void testAfterPrefStartupHook() {
|
---|
73 | hook.afterPrefStartupHook();
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Test method for {@code PlatformHookWindows#openUrl} when Desktop works as expected
|
---|
78 | * @param mockDesktop desktop mock
|
---|
79 | * @throws IOException if an error occurs
|
---|
80 | */
|
---|
81 | @Test
|
---|
82 | public void testOpenUrlSuccess(@Injectable final Desktop mockDesktop) throws IOException {
|
---|
83 | TestUtils.assumeWorkingJMockit();
|
---|
84 | new Expectations(Desktop.class) {{
|
---|
85 | // real implementation would raise HeadlessException
|
---|
86 | Desktop.getDesktop(); result = mockDesktop; times = 1;
|
---|
87 | }};
|
---|
88 | new Expectations() {{
|
---|
89 | mockDesktop.browse(withNotNull()); times = 1;
|
---|
90 | }};
|
---|
91 |
|
---|
92 | hook.openUrl(Config.getUrls().getJOSMWebsite());
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Test method for {@code PlatformHookWindows#openUrl} when Desktop fails
|
---|
97 | * @param mockDesktop desktop mock
|
---|
98 | * @throws IOException if an error occurs
|
---|
99 | */
|
---|
100 | @Test
|
---|
101 | public void testOpenUrlFallback(@Injectable final Desktop mockDesktop) throws IOException {
|
---|
102 | TestUtils.assumeWorkingJMockit();
|
---|
103 | new Expectations(Desktop.class) {{
|
---|
104 | // real implementation would raise HeadlessException
|
---|
105 | Desktop.getDesktop(); result = mockDesktop; times = 1;
|
---|
106 | }};
|
---|
107 | new Expectations() {{
|
---|
108 | mockDesktop.browse(withNotNull()); result = new IOException(); times = 1;
|
---|
109 | }};
|
---|
110 | final Runtime anyRuntime = Runtime.getRuntime();
|
---|
111 | new Expectations(Runtime.class) {{
|
---|
112 | anyRuntime.exec(new String[] {"rundll32", "url.dll,FileProtocolHandler", Config.getUrls().getJOSMWebsite()});
|
---|
113 | result = null;
|
---|
114 | times = 1;
|
---|
115 | // prevent a non-matching invocation being executed
|
---|
116 | anyRuntime.exec((String[]) withNotNull()); result = null; times = 0;
|
---|
117 | }};
|
---|
118 |
|
---|
119 | hook.openUrl(Config.getUrls().getJOSMWebsite());
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Test method for {@code PlatformHookWindows#getAdditionalFonts}
|
---|
124 | */
|
---|
125 | @Test
|
---|
126 | public void testGetAdditionalFonts() {
|
---|
127 | assertFalse(hook.getAdditionalFonts().isEmpty());
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Test method for {@code PlatformHookWindows#getDefaultCacheDirectory}
|
---|
132 | */
|
---|
133 | @Test
|
---|
134 | public void testGetDefaultCacheDirectory() {
|
---|
135 | File cache = hook.getDefaultCacheDirectory();
|
---|
136 | assertNotNull(cache);
|
---|
137 | if (PlatformManager.isPlatformWindows()) {
|
---|
138 | assertTrue(cache.toString().contains(":"));
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Test method for {@code PlatformHookWindows#getDefaultPrefDirectory}
|
---|
144 | */
|
---|
145 | @Test
|
---|
146 | public void testGetDefaultPrefDirectory() {
|
---|
147 | File cache = hook.getDefaultPrefDirectory();
|
---|
148 | assertNotNull(cache);
|
---|
149 | if (PlatformManager.isPlatformWindows()) {
|
---|
150 | assertTrue(cache.toString().contains(":"));
|
---|
151 | }
|
---|
152 | }
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Test method for {@code PlatformHookWindows#getDefaultStyle}
|
---|
156 | */
|
---|
157 | @Test
|
---|
158 | public void testGetDefaultStyle() {
|
---|
159 | assertEquals("com.sun.java.swing.plaf.windows.WindowsLookAndFeel", hook.getDefaultStyle());
|
---|
160 | }
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * Test method for {@code PlatformHookWindows#getInstalledFonts}
|
---|
164 | */
|
---|
165 | @Test
|
---|
166 | public void testGetInstalledFonts() {
|
---|
167 | Collection<String> fonts = hook.getInstalledFonts();
|
---|
168 | if (PlatformManager.isPlatformWindows()) {
|
---|
169 | assertFalse(fonts.isEmpty());
|
---|
170 | } else {
|
---|
171 | assertNull(fonts);
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Test method for {@code PlatformHookWindows#getOSDescription}
|
---|
177 | */
|
---|
178 | @Test
|
---|
179 | public void testGetOSDescription() {
|
---|
180 | String os = hook.getOSDescription();
|
---|
181 | if (PlatformManager.isPlatformWindows()) {
|
---|
182 | assertTrue(os.contains("Windows"));
|
---|
183 | } else {
|
---|
184 | assertFalse(os.contains("Windows"));
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Test method for {@code PlatformHookWindows#initSystemShortcuts}
|
---|
190 | */
|
---|
191 | @Test
|
---|
192 | public void testInitSystemShortcuts() {
|
---|
193 | hook.initSystemShortcuts();
|
---|
194 | }
|
---|
195 | }
|
---|