Skip to content

Commit

Permalink
Adds display static mock for test (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
dingfeli authored Nov 21, 2024
1 parent 53c340f commit 7afa3ec
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

package software.aws.toolkits.eclipse.amazonq.util;

import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IPartListener2;
import org.eclipse.ui.IPartService;
import org.eclipse.ui.IWindowListener;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
Expand Down Expand Up @@ -60,6 +62,29 @@ public void windowOpened(final IWorkbenchWindow window) {
PlatformUI.getWorkbench().addWindowListener(windowListener);
activeWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();

// Similar to how part listener needs to tell its child listener (i.e. document
// listener), the part listener needs to actively be attached to the part that
// contains the editors.
// Without this, because the window listener is attached after the subscribed
// events has already happened, the part listener will not be attached unless
// you trigger one of the subscribed events
Display.getDefault().timerExec(1000, new Runnable() {
@Override
public void run() {
IWorkbenchWindow activeWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (activeWindow == null) {
Display.getDefault().timerExec(1000, this);
return;
}
IPartService partService = activeWindow.getPartService();
if (partService == null) {
Display.getDefault().timerExec(1000, this);
return;
}
partService.addPartListener(partListener);
}
});

// Aside from adding the listeners to the window, we would also need to add the
// listener actively for the first time
// Because all of the subscribed events has already happened.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IPartListener2;
import org.eclipse.ui.IPartService;
import org.eclipse.ui.IWindowListener;
Expand All @@ -22,6 +23,7 @@

public class AutoTriggerTopLevelListenerTest {
private static MockedStatic<PlatformUI> platformUIMockStatic;
private static MockedStatic<Display> displayMockStatic;
private static IWorkbench workbenchMock;
private static IWorkbenchWindow windowMock;
private static IAutoTriggerPartListener partListenerMock;
Expand All @@ -37,11 +39,19 @@ public static void setUp() {
when(windowMock.getPartService()).thenReturn(partServiceMock);
when(workbenchMock.getActiveWorkbenchWindow()).thenReturn(windowMock);
partListenerMock = mock(IAutoTriggerPartListener.class);
displayMockStatic = mockStatic(Display.class);
Display displayMock = mock(Display.class);
displayMockStatic.when(Display::getDefault).thenReturn(displayMock);
}

@AfterAll
public static void tearDown() {
platformUIMockStatic.close();
if (platformUIMockStatic != null) {
platformUIMockStatic.close();
}
if (displayMockStatic != null) {
displayMockStatic.close();
}
}

@SuppressWarnings({ "rawtypes", "unchecked" })
Expand Down

0 comments on commit 7afa3ec

Please sign in to comment.