Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImportExistingProjectsWizardTest: fix leaked Shells #2379 #2380

Merged
merged 3 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;

import org.eclipse.core.resources.ResourcesPlugin;
Expand All @@ -30,7 +31,6 @@
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.preference.PreferenceMemento;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.events.ShellListener;
import org.eclipse.swt.widgets.Display;
Expand Down Expand Up @@ -75,6 +75,7 @@ public static IAdaptable getPageInput() {
private final CloseTestWindowsRule closeTestWindows = new CloseTestWindowsRule();

protected IWorkbench fWorkbench;
private Set<Shell> preExistingShells;

/** Preference helper to restore changed preference values after test run. */
private final PreferenceMemento prefMemento = new PreferenceMemento();
Expand Down Expand Up @@ -180,6 +181,7 @@ public final void setUp() throws Exception {
super.setUp();
closeTestWindows.before();
fWorkbench = PlatformUI.getWorkbench();
this.preExistingShells = Set.of(fWorkbench.getDisplay().getShells());
String name = runningTest != null ? runningTest : this.getName();
trace(TestRunLogUtil.formatTestStartMessage(name));
doSetUp();
Expand Down Expand Up @@ -212,18 +214,17 @@ public final void tearDown() throws Exception {
trace(TestRunLogUtil.formatTestFinishedMessage(name));
prefMemento.resetPreferences();
doTearDown();
fWorkbench = null;

// Check for modal shell leak.
// Check for shell leak.
List<String> leakedModalShellTitles = new ArrayList<>();
Shell[] shells = PlatformUI.getWorkbench().getDisplay().getShells();
Shell[] shells = fWorkbench.getDisplay().getShells();
for (Shell shell : shells) {
if (!shell.isDisposed() && shell.isVisible()
&& (shell.getStyle() & (SWT.APPLICATION_MODAL | SWT.PRIMARY_MODAL | SWT.SYSTEM_MODAL)) != 0) {
if (!shell.isDisposed() && !preExistingShells.contains(shell)) {
leakedModalShellTitles.add(shell.getText());
shell.close();
}
}
fWorkbench = null;
assertEquals("Test leaked modal shell: [" + String.join(", ", leakedModalShellTitles) + "]", 0,
leakedModalShellTitles.size());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@
import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.WorkbenchPlugin;
import org.eclipse.ui.internal.dialogs.ImportExportWizard;
import org.eclipse.ui.internal.wizards.datatransfer.WizardProjectsImportPage;
import org.eclipse.ui.internal.wizards.datatransfer.WizardProjectsImportPage.ProjectRecord;
import org.eclipse.ui.navigator.resources.ProjectExplorer;
import org.eclipse.ui.tests.TestPlugin;
import org.eclipse.ui.tests.harness.util.DialogCheck;
import org.eclipse.ui.tests.harness.util.EmptyPerspective;
import org.eclipse.ui.tests.harness.util.FileUtil;
import org.eclipse.ui.tests.harness.util.UITestCase;
Expand All @@ -66,6 +66,10 @@ public ImportExistingArchiveProjectFilterTest() {

@Override
protected void doTearDown() throws Exception {
if (dialog != null) {
dialog.close();
dialog = null;
}
IWorkspaceRoot wsRoot = ResourcesPlugin.getWorkspace().getRoot();
IProject[] projects = wsRoot.getProjects();
for (int i = projects.length - 1; i >= 0; i--) {
Expand Down Expand Up @@ -158,6 +162,7 @@ private void processElementAndChildren(Object element, ITreeContentProvider cont
}
}

private WizardDialog dialog;
public WizardProjectsImportPage getNewWizard() {
ImportExportWizard wizard = new ImportExportWizard(ImportExportWizard.IMPORT);
wizard.init(getWorkbench(), null);
Expand All @@ -173,7 +178,10 @@ public WizardProjectsImportPage getNewWizard() {

Shell shell = getShell();

WizardDialog dialog = new WizardDialog(shell, wizard);
if (dialog != null) {
dialog.close();
}
dialog = new WizardDialog(shell, wizard);
dialog.create();
dialog.getShell().setSize(Math.max(100, dialog.getShell().getSize().x), 100);

Expand All @@ -185,6 +193,6 @@ public WizardProjectsImportPage getNewWizard() {
}

private Shell getShell() {
return DialogCheck.getShell();
return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
import org.eclipse.ui.internal.wizards.datatransfer.WizardProjectsImportPage.ProjectRecord;
import org.eclipse.ui.tests.TestPlugin;
import org.eclipse.ui.tests.datatransfer.ImportTestUtils.TestBuilder;
import org.eclipse.ui.tests.harness.util.DialogCheck;
import org.eclipse.ui.tests.harness.util.FileUtil;
import org.eclipse.ui.tests.harness.util.UITestCase;
import org.eclipse.ui.wizards.datatransfer.ExternalProjectImportWizard;
Expand Down Expand Up @@ -86,14 +85,16 @@ public class ImportExistingProjectsWizardTest extends UITestCase {

private String zipLocation = null;

private WizardDialog dialog;

private boolean originalRefreshSetting;

public ImportExistingProjectsWizardTest() {
super(ImportExistingProjectsWizardTest.class.getName());
}

private Shell getShell() {
return DialogCheck.getShell();
return PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
}

@Override
Expand All @@ -109,6 +110,11 @@ protected void doSetUp() throws Exception {

@Override
protected void doTearDown() throws Exception {
if (dialog != null) {
dialog.close();
dialog = null;
}

IWorkspaceRoot wsRoot = ResourcesPlugin.getWorkspace().getRoot();
IProject[] projects = wsRoot.getProjects();
for (int i = projects.length - 1; i >= 0; i--) {
Expand Down Expand Up @@ -799,8 +805,6 @@ public void test14InitialValue() throws IOException, CoreException {
wpip = getExternalImportWizard(null);
selectedProjects = wpip.getProjectRecords();
assertEquals(0, selectedProjects.length);


}

@Test
Expand Down Expand Up @@ -900,7 +904,6 @@ private String copyZipLocation(String zipLocation) throws IOException {
return ImportTestUtils.copyZipLocation(zipLocation, ARCHIVE_HELLOWORLD);
}


private WizardProjectsImportPage getNewWizard() {
ImportExportWizard wizard = new ImportExportWizard(
ImportExportWizard.IMPORT);
Expand All @@ -920,7 +923,10 @@ private WizardProjectsImportPage getNewWizard() {

Shell shell = getShell();

WizardDialog dialog = new WizardDialog(shell, wizard);
if (dialog != null) {
dialog.close();
}
dialog = new WizardDialog(shell, wizard);
dialog.create();
dialog.getShell().setSize(Math.max(100, dialog.getShell().getSize().x),
100);
Expand Down Expand Up @@ -1176,7 +1182,10 @@ private WizardProjectsImportPage getExternalImportWizard(String initialPath) {
ExternalProjectImportWizard wizard = new ExternalProjectImportWizard(
initialPath);
wizard.init(getWorkbench(), null);
WizardDialog dialog = new WizardDialog(getShell(), wizard);
if (dialog != null) {
dialog.close();
}
dialog = new WizardDialog(getShell(), wizard);
dialog.create();

dialog.getShell().setSize(Math.max(100, dialog.getShell().getSize().x),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public void testOpenView() {
IWorkbenchPage page = window.getActivePage();

try {
page.showView(IPageLayout.ID_BOOKMARKS);
page.showView(IPageLayout.ID_PROBLEM_VIEW);
page.showView(IPageLayout.ID_TASK_LIST);
page.hideView(page.showView(IPageLayout.ID_BOOKMARKS));
page.hideView(page.showView(IPageLayout.ID_PROBLEM_VIEW));
page.hideView(page.showView(IPageLayout.ID_TASK_LIST));
} catch (PartInitException e) {
assertTrue(e.getLocalizedMessage(), false);
return;
Expand Down
Loading