From 7afa3ece23d0bacb44f0970c580dc18f7ff634e8 Mon Sep 17 00:00:00 2001 From: Felix Ding Date: Thu, 21 Nov 2024 14:59:14 -0800 Subject: [PATCH] Adds display static mock for test (#261) --- .../util/AutoTriggerTopLevelListener.java | 25 +++++++++++++++++++ .../util/AutoTriggerTopLevelListenerTest.java | 12 ++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/AutoTriggerTopLevelListener.java b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/AutoTriggerTopLevelListener.java index 74d5a382..43917687 100644 --- a/plugin/src/software/aws/toolkits/eclipse/amazonq/util/AutoTriggerTopLevelListener.java +++ b/plugin/src/software/aws/toolkits/eclipse/amazonq/util/AutoTriggerTopLevelListener.java @@ -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; @@ -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. diff --git a/plugin/tst/software/aws/toolkits/eclipse/amazonq/util/AutoTriggerTopLevelListenerTest.java b/plugin/tst/software/aws/toolkits/eclipse/amazonq/util/AutoTriggerTopLevelListenerTest.java index 7c0dc85d..8724b327 100644 --- a/plugin/tst/software/aws/toolkits/eclipse/amazonq/util/AutoTriggerTopLevelListenerTest.java +++ b/plugin/tst/software/aws/toolkits/eclipse/amazonq/util/AutoTriggerTopLevelListenerTest.java @@ -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; @@ -22,6 +23,7 @@ public class AutoTriggerTopLevelListenerTest { private static MockedStatic platformUIMockStatic; + private static MockedStatic displayMockStatic; private static IWorkbench workbenchMock; private static IWorkbenchWindow windowMock; private static IAutoTriggerPartListener partListenerMock; @@ -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" })