1515
1616import static org .eclipse .ui .tests .harness .util .UITestUtil .processEvents ;
1717import static org .eclipse .ui .tests .harness .util .UITestUtil .processEventsUntil ;
18- import static org .junit .Assert .assertTrue ;
18+ import static org .junit .jupiter .api .Assertions .assertEquals ;
19+ import static org .junit .jupiter .api .Assertions .assertTrue ;
1920
2021import java .io .IOException ;
2122import java .nio .file .Files ;
2223import java .nio .file .Paths ;
24+ import java .util .ArrayList ;
25+ import java .util .List ;
26+ import java .util .Set ;
2327
2428import org .eclipse .core .resources .IFile ;
2529import org .eclipse .core .resources .IProject ;
2630import org .eclipse .core .resources .IResource ;
2731import org .eclipse .core .runtime .CoreException ;
32+ import org .eclipse .swt .widgets .Shell ;
2833import org .eclipse .ui .IEditorInput ;
2934import org .eclipse .ui .IEditorPart ;
3035import org .eclipse .ui .IEditorReference ;
3136import org .eclipse .ui .INavigationLocation ;
37+ import org .eclipse .ui .IWindowListener ;
3238import org .eclipse .ui .IWorkbenchPage ;
3339import org .eclipse .ui .IWorkbenchWindow ;
3440import org .eclipse .ui .PartInitException ;
3541import org .eclipse .ui .PlatformUI ;
3642import org .eclipse .ui .internal .NavigationHistoryAction ;
3743import org .eclipse .ui .intro .IIntroPart ;
3844import org .eclipse .ui .part .FileEditorInput ;
39- import org .eclipse .ui .tests .harness .util .CloseTestWindowsRule ;
4045import org .eclipse .ui .tests .harness .util .EditorTestHelper ;
4146import org .eclipse .ui .tests .harness .util .FileUtil ;
4247import org .eclipse .ui .tests .harness .util .UITestUtil .Condition ;
4348import org .eclipse .ui .texteditor .AbstractTextEditor ;
4449import org .eclipse .ui .texteditor .TextSelectionNavigationLocation ;
45- import org .junit .Assert ;
46- import org .junit .Before ;
47- import org .junit .Rule ;
48- import org .junit .Test ;
50+ import org .junit .jupiter .api .AfterEach ;
51+ import org .junit .jupiter .api .BeforeEach ;
52+ import org .junit .jupiter .api .Test ;
4953
5054/**
5155 * @since 3.3
@@ -64,19 +68,84 @@ public class GoBackForwardsTest {
6468
6569 private IProject project ;
6670 private IFile file ;
71+ private final List <IWorkbenchWindow > testWindows = new ArrayList <>();
72+ private IWindowListener windowListener ;
73+ private Set <Shell > initialShells ;
6774
68- @ Rule
69- public final CloseTestWindowsRule closeTestWindows = new CloseTestWindowsRule ();
70-
71- @ Before
75+ @ BeforeEach
7276 public void setUp () throws CoreException , IOException {
77+ addWindowListener ();
78+ storeInitialShells ();
79+
7380 project = FileUtil .createProject (PROJECT_NAME );
7481 file = FileUtil .createFile (FILE_NAME , project );
7582 StringBuilder stringBuilder = new StringBuilder ();
7683 stringBuilder .append (FILE_CONTENTS );
7784 Files .writeString (Paths .get (file .getLocation ().toOSString ()), stringBuilder );
7885 project .refreshLocal (IResource .DEPTH_INFINITE , null );
86+ }
87+
88+ @ AfterEach
89+ public void tearDown () {
90+ removeWindowListener ();
91+ processEvents ();
92+ closeAllTestWindows ();
93+ processEvents ();
94+ checkForLeakedShells ();
95+ }
7996
97+ private void addWindowListener () {
98+ windowListener = new IWindowListener () {
99+ @ Override
100+ public void windowActivated (IWorkbenchWindow window ) {
101+ }
102+
103+ @ Override
104+ public void windowDeactivated (IWorkbenchWindow window ) {
105+ }
106+
107+ @ Override
108+ public void windowClosed (IWorkbenchWindow window ) {
109+ testWindows .remove (window );
110+ }
111+
112+ @ Override
113+ public void windowOpened (IWorkbenchWindow window ) {
114+ testWindows .add (window );
115+ }
116+ };
117+ PlatformUI .getWorkbench ().addWindowListener (windowListener );
118+ }
119+
120+ private void removeWindowListener () {
121+ if (windowListener != null ) {
122+ PlatformUI .getWorkbench ().removeWindowListener (windowListener );
123+ }
124+ }
125+
126+ private void closeAllTestWindows () {
127+ List <IWorkbenchWindow > testWindowsCopy = new ArrayList <>(testWindows );
128+ for (IWorkbenchWindow testWindow : testWindowsCopy ) {
129+ testWindow .close ();
130+ }
131+ testWindows .clear ();
132+ }
133+
134+ private void storeInitialShells () {
135+ this .initialShells = Set .of (PlatformUI .getWorkbench ().getDisplay ().getShells ());
136+ }
137+
138+ private void checkForLeakedShells () {
139+ List <String > leakedModalShellTitles = new ArrayList <>();
140+ Shell [] shells = PlatformUI .getWorkbench ().getDisplay ().getShells ();
141+ for (Shell shell : shells ) {
142+ if (!shell .isDisposed () && !initialShells .contains (shell )) {
143+ leakedModalShellTitles .add (shell .getText ());
144+ shell .close ();
145+ }
146+ }
147+ assertEquals (0 , leakedModalShellTitles .size (),
148+ "Test leaked modal shell: [" + String .join (", " , leakedModalShellTitles ) + "]" );
80149 }
81150
82151 @ Test
@@ -95,79 +164,75 @@ public void testNavigationHistoryNavigation() throws PartInitException {
95164
96165 openGenericEditor (editorInput );
97166
98- assertTrue ("Timeout during navigation." + getStateDetails ( ),
99- processEventsUntil ( genericEditorNoSelection , 1000 ));
167+ assertTrue (processEventsUntil ( genericEditorNoSelection , 1000 ),
168+ "Timeout during navigation." + getStateDetails ( ));
100169
101170 selectInGenericEditor (editorInput );
102171
103- assertTrue ("Timeout during navigation." + getStateDetails ( ),
104- processEventsUntil ( genericEditorSelection , 1000 ));
172+ assertTrue (processEventsUntil ( genericEditorSelection , 1000 ),
173+ "Timeout during navigation." + getStateDetails ( ));
105174
106175 openTextEditor (editorInput );
107176
108- assertTrue ("Timeout during navigation." + getStateDetails ( ),
109- processEventsUntil ( textEditorNoSelection , 1000 ));
177+ assertTrue (processEventsUntil ( textEditorNoSelection , 1000 ),
178+ "Timeout during navigation." + getStateDetails ( ));
110179
111180 selectInTextEditor (editorInput );
112181
113- assertTrue ("Timeout during navigation." + getStateDetails ( ),
114- processEventsUntil ( textEditorSelection , 1000 ));
182+ assertTrue (processEventsUntil ( textEditorSelection , 1000 ),
183+ "Timeout during navigation." + getStateDetails ( ));
115184
116185 openGenericEditor (editorInput );
117186
118- assertTrue ("Timeout during navigation." + getStateDetails ( ),
119- processEventsUntil ( genericEditorSelection , 1000 ));
187+ assertTrue (processEventsUntil ( genericEditorSelection , 1000 ),
188+ "Timeout during navigation." + getStateDetails ( ));
120189
121190 openTextEditor (editorInput );
122191
123- assertTrue ("Timeout during navigation." + getStateDetails ( ),
124- processEventsUntil ( textEditorSelection , 1000 ));
192+ assertTrue (processEventsUntil ( textEditorSelection , 1000 ),
193+ "Timeout during navigation." + getStateDetails ( ));
125194
126195 // Navigate backward from text editor to editor
127196 goBackward (EditorTestHelper .getActiveWorkbenchWindow (), genericEditorSelection );
128- Assert .assertEquals (
129- "Failed to correctly navigate backward from text editor to java editor." + getStateDetails (),
130- GENERIC_EDITOR_ID , getActiveEditorId ());
197+ assertEquals (GENERIC_EDITOR_ID , getActiveEditorId (),
198+ "Failed to correctly navigate backward from text editor to java editor." + getStateDetails ());
131199
132200 // Navigate backward from java editor to text editor
133201 goBackward (EditorTestHelper .getActiveWorkbenchWindow (), textEditorSelection );
134- Assert .assertEquals (
135- "Failed to correctly navigate backward from java editor to test editor." + getStateDetails (),
136- TEXT_EDITOR_ID , getActiveEditorId ());
202+ assertEquals (TEXT_EDITOR_ID , getActiveEditorId (),
203+ "Failed to correctly navigate backward from java editor to test editor." + getStateDetails ());
137204
138205 // Navigate backward from text editor to text editor
139206 goBackward (EditorTestHelper .getActiveWorkbenchWindow (), textEditorNoSelection );
140- Assert .assertEquals (
141- "Failed to correctly navigate backward from text editor to text editor." + getStateDetails (),
142- TEXT_EDITOR_ID , getActiveEditorId ());
207+ assertEquals (TEXT_EDITOR_ID , getActiveEditorId (),
208+ "Failed to correctly navigate backward from text editor to text editor." + getStateDetails ());
143209
144210 // Navigate backward from java editor to java editor
145211 goBackward (EditorTestHelper .getActiveWorkbenchWindow (), genericEditorSelection );
146212 goBackward (EditorTestHelper .getActiveWorkbenchWindow (), genericEditorNoSelection );
147- Assert .assertEquals (
148- "Failed to correctly navigate backward from java editor to java editor." + getStateDetails (),
149- GENERIC_EDITOR_ID , getActiveEditorId ());
213+ assertEquals (GENERIC_EDITOR_ID , getActiveEditorId (),
214+ "Failed to correctly navigate backward from java editor to java editor." + getStateDetails ());
150215
151216 // Navigate forward from java editor to java editor
152217 goForward (EditorTestHelper .getActiveWorkbenchWindow (), genericEditorSelection );
153- Assert . assertEquals ("Failed to correctly navigate forward from java editor to java editor." + getStateDetails (),
154- GENERIC_EDITOR_ID , getActiveEditorId ());
218+ assertEquals (GENERIC_EDITOR_ID , getActiveEditorId (),
219+ "Failed to correctly navigate forward from java editor to java editor." + getStateDetails ());
155220
156221 // Navigate forward from text editor to text editor
157222 goForward (EditorTestHelper .getActiveWorkbenchWindow (), textEditorNoSelection );
158223 goForward (EditorTestHelper .getActiveWorkbenchWindow (), textEditorSelection );
159- Assert . assertEquals ("Failed to correctly navigate forward from java editor to java editor." + getStateDetails (),
160- TEXT_EDITOR_ID , getActiveEditorId ());
224+ assertEquals (TEXT_EDITOR_ID , getActiveEditorId (),
225+ "Failed to correctly navigate forward from java editor to java editor." + getStateDetails ());
161226
162227 // Navigate forward from text editor to java editor
163228 goForward (EditorTestHelper .getActiveWorkbenchWindow (), genericEditorSelection );
164- Assert . assertEquals ("Failed to correctly navigate forward from text editor to java editor." + getStateDetails (),
165- GENERIC_EDITOR_ID , getActiveEditorId ());
229+ assertEquals (GENERIC_EDITOR_ID , getActiveEditorId (),
230+ "Failed to correctly navigate forward from text editor to java editor." + getStateDetails ());
166231
167232 // Navigate forward from java editor to text editor
168233 goForward (EditorTestHelper .getActiveWorkbenchWindow (), textEditorSelection );
169- Assert . assertEquals ("Failed to correctly navigate forward from java editor to text editor." + getStateDetails (),
170- TEXT_EDITOR_ID , getActiveEditorId ());
234+ assertEquals (TEXT_EDITOR_ID , getActiveEditorId (),
235+ "Failed to correctly navigate forward from java editor to text editor." + getStateDetails ());
171236 }
172237
173238 private Condition currentNavigationHistoryLocationCondition (String editorId , boolean selection ) {
@@ -207,13 +272,13 @@ private void openTextEditor(IEditorInput editorInput) throws PartInitException {
207272 private void goForward (IWorkbenchWindow window , Condition condition ) {
208273 NavigationHistoryAction action = new NavigationHistoryAction (window , true );
209274 action .run ();
210- assertTrue ("Timeout during navigation." , processEventsUntil ( condition , 1000 ) );
275+ assertTrue (processEventsUntil ( condition , 1000 ), "Timeout during navigation." );
211276 }
212277
213278 private void goBackward (IWorkbenchWindow window , Condition condition ) {
214279 NavigationHistoryAction action = new NavigationHistoryAction (window , false );
215280 action .run ();
216- assertTrue ("Timeout during navigation." , processEventsUntil ( condition , 1000 ) );
281+ assertTrue (processEventsUntil ( condition , 1000 ), "Timeout during navigation." );
217282 }
218283
219284 private String getActiveEditorId () {
0 commit comments