Skip to content

Commit de12a4b

Browse files
authored
Merge pull request #21 from The-Nefarious-Developer/windows-fix
Windows fix
2 parents 66fa259 + 7341082 commit de12a4b

File tree

5 files changed

+134
-5
lines changed

5 files changed

+134
-5
lines changed

com.developer.nefarious.zjoule.plugin/META-INF/MANIFEST.MF

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ Require-Bundle:
1111
org.eclipse.core.resources;bundle-version="3.21.0",
1212
org.eclipse.ui.ide;bundle-version="3.22.300",
1313
org.eclipse.jface,
14-
org.eclipse.swt
14+
org.eclipse.swt,
15+
org.eclipse.wst.wsdl.ui;bundle-version="1.3.101"
1516
Bundle-RequiredExecutionEnvironment: JavaSE-21
1617
Automatic-Module-Name: com.developer.nefarious.zjoule.plugin
1718
Import-Package:
@@ -30,6 +31,7 @@ Export-Package:
3031
com.developer.nefarious.zjoule.plugin.core.events,
3132
com.developer.nefarious.zjoule.plugin.core.functions,
3233
com.developer.nefarious.zjoule.plugin.core.ui,
34+
com.developer.nefarious.zjoule.plugin.core.utils,
3335
com.developer.nefarious.zjoule.plugin.login,
3436
com.developer.nefarious.zjoule.plugin.login.api,
3537
com.developer.nefarious.zjoule.plugin.login.events,

com.developer.nefarious.zjoule.plugin/src/com/developer/nefarious/zjoule/plugin/core/ui/ViewListener.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.developer.nefarious.zjoule.plugin.core.functions.LoginHandler;
2020
import com.developer.nefarious.zjoule.plugin.core.functions.LogoutHandler;
2121
import com.developer.nefarious.zjoule.plugin.core.functions.PromptHandler;
22+
import com.developer.nefarious.zjoule.plugin.core.utils.SystemProvider;
2223

2324
import jakarta.inject.Inject;
2425

@@ -54,7 +55,8 @@ public class ViewListener extends ViewPart {
5455
*/
5556
@Override
5657
public void createPartControl(final Composite parent) {
57-
browser = BrowserFactory.create(parent, SWT.WEBKIT);
58+
int style = (isWindows()) ? SWT.EDGE : SWT.WEBKIT;
59+
browser = BrowserFactory.create(parent, style);
5860
selectionListener = SelectionListener.create(browser);
5961
IViewRender viewRender = ViewRender.create();
6062
partListener = PartListener.create(browser);
@@ -74,8 +76,24 @@ public void createPartControl(final Composite parent) {
7476

7577
setUpToolbar();
7678
}
77-
79+
7880
/**
81+
* Checks if the current operating system is a version of Windows.
82+
* <p>
83+
* This method determines the operating system by checking if the string
84+
* returned by {@link SystemProvider#getCurrentSystem()} contains the substring "win"
85+
* (case insensitive).
86+
* </p>
87+
*
88+
* @return {@code true} if the operating system name contains "win", indicating a Windows OS;
89+
* {@code false} otherwise.
90+
* @see SystemProvider#getCurrentSystem()
91+
*/
92+
private boolean isWindows() {
93+
return SystemProvider.getCurrentSystem().toLowerCase().contains("win");
94+
}
95+
96+
/**
7997
* Disposes of the view and its associated resources, including listeners and the browser.
8098
*/
8199
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.developer.nefarious.zjoule.plugin.core.utils;
2+
3+
/**
4+
* Utility class for providing information about the current operating system.
5+
* <p>
6+
* This class is designed to be used as a utility and cannot be instantiated.
7+
* It provides a static method to retrieve the name of the operating system
8+
* as reported by the Java Virtual Machine (JVM).
9+
* </p>
10+
*/
11+
public abstract class SystemProvider {
12+
13+
/**
14+
* Retrieves the name of the operating system on which the JVM is currently running.
15+
* <p>
16+
* This method wraps the {@code System.getProperty("os.name")} call, which
17+
* provides the operating system name as a {@code String}.
18+
* </p>
19+
*
20+
* @return the name of the operating system, such as "Windows 10", "Linux", or "Mac OS X".
21+
* The exact value is determined by the underlying JVM implementation.
22+
* @see System#getProperty(String)
23+
*/
24+
public static String getCurrentSystem() {
25+
return System.getProperty("os.name");
26+
}
27+
28+
/**
29+
* Private constructor to prevent instantiation of this utility class.
30+
*/
31+
private SystemProvider() { }
32+
33+
}

com.developer.nefarious.zjoule.test/src/com/developer/nefarious/zjoule/test/core/ui/ViewListenerTest.java

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import com.developer.nefarious.zjoule.plugin.core.ui.IViewRender;
4343
import com.developer.nefarious.zjoule.plugin.core.ui.ViewListener;
4444
import com.developer.nefarious.zjoule.plugin.core.ui.ViewRender;
45+
import com.developer.nefarious.zjoule.plugin.core.utils.SystemProvider;
4546

4647
public class ViewListenerTest {
4748

@@ -64,6 +65,8 @@ public class ViewListenerTest {
6465
private MockedStatic<ClearHandler> mockedStaticClearHandler;
6566

6667
private MockedStatic<LogoutHandler> mockedStaticLogoutHandler;
68+
69+
private MockedStatic<SystemProvider> mockedSystemProvider;
6770

6871
@Mock
6972
private Shell mockShell;
@@ -111,8 +114,8 @@ public void setUp() {
111114
mockedStaticLoginHandler = mockStatic(LoginHandler.class);
112115
mockedStaticClearHandler = mockStatic(ClearHandler.class);
113116
mockedStaticLogoutHandler = mockStatic(LogoutHandler.class);
117+
mockedSystemProvider = mockStatic(SystemProvider.class);
114118

115-
mockedStaticBrowserFactory.when(() -> BrowserFactory.create(mockParent, SWT.WEBKIT)).thenReturn(mockBrowser);
116119
mockedStaticSelectionListener.when(() -> SelectionListener.create(mockBrowser)).thenReturn(mockSelectionListener);
117120
mockedStaticViewRender.when(ViewRender::create).thenReturn(mockViewRender);
118121
mockedStaticPartListener.when(() -> PartListener.create(mockBrowser)).thenReturn(mockPartListener);
@@ -125,10 +128,59 @@ public void setUp() {
125128
cut = spy(new ViewListener());
126129
cut.setShell(mockShell);
127130
}
131+
132+
@Test
133+
public void shouldPlumbPartControlForWindows() {
134+
// Arrange
135+
mockedSystemProvider.when(SystemProvider::getCurrentSystem).thenReturn("Windows 95");
136+
mockedStaticBrowserFactory.when(() -> BrowserFactory.create(mockParent, SWT.EDGE)).thenReturn(mockBrowser);
137+
138+
String mockBuildResult = "html-text";
139+
when(mockViewRender.build()).thenReturn(mockBuildResult);
140+
141+
IWorkbenchPartSite mockSite = mock(IWorkbenchPartSite.class);
142+
doReturn(mockSite).when(cut).getSite();
143+
144+
IWorkbenchPage mockPage = mock(IWorkbenchPage.class);
145+
when(mockSite.getPage()).thenReturn(mockPage);
146+
147+
IViewSite mockViewSite = mock(IViewSite.class);
148+
doReturn(mockViewSite).when(cut).getViewSite();
149+
IActionBars mockActionBars = mock(IActionBars.class);
150+
when(mockViewSite.getActionBars()).thenReturn(mockActionBars);
151+
152+
IToolBarManager mockToolBarManager = mock(IToolBarManager.class);
153+
when(mockActionBars.getToolBarManager()).thenReturn(mockToolBarManager);
154+
155+
IMenuManager mockMenuManager = mock(IMenuManager.class);
156+
when(mockActionBars.getMenuManager()).thenReturn(mockMenuManager);
157+
158+
// Act
159+
cut.createPartControl(mockParent);
160+
161+
// Assert
162+
verify(mockBrowser).setText(mockBuildResult);
163+
verify(mockBrowser).addDisposeListener(any(DisposeListener.class));
164+
verify(mockPage).addPartListener(mockPartListener);
165+
verify(mockPage).addSelectionListener(mockSelectionListener);
166+
167+
ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class);
168+
verify(mockDisplay).asyncExec(captor.capture());
169+
Runnable actualRunnable = captor.getValue();
170+
assertTrue(actualRunnable instanceof Initialization);
171+
172+
verify(mockToolBarManager).add(mockLoginHandler);
173+
verify(mockMenuManager).add(mockClearHandler);
174+
verify(mockMenuManager).add(any(Separator.class));
175+
verify(mockMenuManager).add(mockLogoutHandler);
176+
}
128177

129178
@Test
130-
public void shouldPlumbPartControl() {
179+
public void shouldPlumbPartControlForOtherSystems() {
131180
// Arrange
181+
mockedSystemProvider.when(SystemProvider::getCurrentSystem).thenReturn("Mac OS");
182+
mockedStaticBrowserFactory.when(() -> BrowserFactory.create(mockParent, SWT.WEBKIT)).thenReturn(mockBrowser);
183+
132184
String mockBuildResult = "html-text";
133185
when(mockViewRender.build()).thenReturn(mockBuildResult);
134186

@@ -169,6 +221,7 @@ public void shouldPlumbPartControl() {
169221
verify(mockMenuManager).add(mockLogoutHandler);
170222
}
171223

224+
//CHECKSTYLE:OFF CyclomaticComplexity
172225
@AfterEach
173226
public void tearDown() {
174227
if (mockedStaticBrowserFactory != null) {
@@ -198,7 +251,11 @@ public void tearDown() {
198251
if (mockedStaticLogoutHandler != null) {
199252
mockedStaticLogoutHandler.close();
200253
}
254+
if (mockedSystemProvider != null) {
255+
mockedSystemProvider.close();
256+
}
201257
}
258+
//CHECKSTYLE:ON CyclomaticComplexity
202259

203260
@Test
204261
public void testDispose() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.developer.nefarious.zjoule.test.core.utils;
2+
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
6+
import com.developer.nefarious.zjoule.plugin.core.utils.SystemProvider;
7+
8+
public class SystemProviderTest {
9+
10+
public void shouldReturnTheCurrentSystem() {
11+
// Arrange
12+
// Act
13+
String returnValue = SystemProvider.getCurrentSystem();
14+
// Assert
15+
assertNotNull(returnValue);
16+
assertFalse(returnValue.isBlank());
17+
}
18+
19+
}

0 commit comments

Comments
 (0)