3232import  java .util .List ;
3333import  java .util .Queue ;
3434import  java .util .Set ;
35- import  java .util .function .BooleanSupplier ;
3635import  java .util .stream .Collectors ;
3736
3837import  org .junit .jupiter .api .AfterEach ;
4645
4746import  org .eclipse .swt .SWT ;
4847import  org .eclipse .swt .custom .ST ;
49- import  org .eclipse .swt .custom .StyledText ;
5048import  org .eclipse .swt .widgets .Composite ;
5149import  org .eclipse .swt .widgets .Control ;
5250import  org .eclipse .swt .widgets .Display ;
@@ -87,7 +85,7 @@ public class CompletionTest extends AbstratGenericEditorTest {
8785	@ DisabledOnOs (value  = OS .MAC , disabledReason  = "test fails on Mac, see https://github.com/eclipse-platform/eclipse.platform.ui/issues/906" )
8886	public  void  testCompletion () throws  Exception  {
8987		editor .selectAndReveal (3 , 0 );
90- 		this .completionShell  = openConentAssist ( );
88+ 		this .completionShell  = openContentAssist ( true );
9189		final  Table  completionProposalList  = findCompletionSelectionControl (completionShell );
9290		checkCompletionContent (completionProposalList );
9391		// TODO find a way to actually trigger completion and verify result against 
@@ -102,7 +100,7 @@ public void testDefaultContentAssistBug570488() throws Exception {
102100		TestLogListener  listener = new  TestLogListener ();
103101		log .addLogListener (listener );
104102		createAndOpenFile ("Bug570488.txt" , "bar 'bar'" );
105- 		openConentAssist (false );
103+ 		openContentAssist (false );
106104		DisplayHelper .runEventLoop (Display .getCurrent (), 0 );
107105		assertFalse (listener .messages .stream ().anyMatch (s  -> s .matches (IStatus .ERROR )), "There are errors in the log" );
108106		log .removeLogListener (listener );
@@ -120,10 +118,10 @@ public void testCompletionService() throws Exception {
120118				new  Hashtable <>(Collections .singletonMap ("contentType" , "org.eclipse.ui.genericeditor.tests.content-type" )));
121119		DisplayHelper .runEventLoop (Display .getCurrent (), 0 );
122120		editor .selectAndReveal (3 , 0 );
123- 		this .completionShell = openConentAssist ( );
121+ 		this .completionShell = openContentAssist ( true );
124122		final  Table  completionProposalList = findCompletionSelectionControl (completionShell );
125- 		checkCompletionContent (completionProposalList );
126123		assertTrue (service .called , "Service was not called!" );
124+ 		checkCompletionContent (completionProposalList );
127125		registration .unregister ();
128126	}
129127
@@ -132,17 +130,12 @@ public void testCompletionService() throws Exception {
132130	public  void  testCompletionUsingViewerSelection () throws  Exception  {
133131		editor .getDocumentProvider ().getDocument (editor .getEditorInput ()).set ("abc" );
134132		editor .selectAndReveal (0 , 3 );
135- 		this .completionShell = openConentAssist ( );
133+ 		this .completionShell = openContentAssist ( true );
136134		final  Table  completionProposalList  = findCompletionSelectionControl (completionShell );
137- 		waitForProposalRelatedCondition ("Proposal list did not contain expected item: ABC" , completionProposalList ,
138- 				() -> Arrays .stream (completionProposalList .getItems ()).map (TableItem ::getText ).anyMatch ("ABC" ::equals ), 5_000 );
139- 	}
140- 	
141- 	private  static  void  waitForProposalRelatedCondition (String  errorMessage , Table  completionProposalList , BooleanSupplier  condition , int  timeoutInMsec ) {
142- 		assertTrue (DisplayHelper .waitForCondition (completionProposalList .getDisplay (), timeoutInMsec , () -> {
135+ 		assertTrue (DisplayHelper .waitForCondition (completionProposalList .getDisplay (), 5000 , () -> {
143136			assertFalse (completionProposalList .isDisposed (), "Completion proposal list was unexpectedly disposed" );
144- 			return  condition . getAsBoolean ( );
145- 		}), errorMessage );
137+ 			return  Arrays . stream ( completionProposalList . getItems ()). map ( TableItem :: getText ). anyMatch ( "ABC" :: equals );
138+ 		}), "Proposal list did not contain expected item: ABC" );
146139	}
147140
148141	@ Test 
@@ -151,20 +144,17 @@ public void testEnabledWhenCompletion() throws Exception {
151144		EnabledPropertyTester .setEnabled (false );
152145		createAndOpenFile ("enabledWhen.txt" , "bar 'bar'" );
153146		editor .selectAndReveal (3 , 0 );
154- 		assertNull (openConentAssist (false ), "A new shell was found" );
147+ 		assertNull (openContentAssist (false ), "A new shell was found" );
155148		cleanFileAndEditor ();
156149
157150		// Confirm that when enabled, a completion shell is present 
158151		EnabledPropertyTester .setEnabled (true );
159152		createAndOpenFile ("enabledWhen.txt" , "bar 'bar'" );
160153		editor .selectAndReveal (3 , 0 );		
161- 		assertNotNull (openConentAssist ( ));
154+ 		assertNotNull (openContentAssist ( true ));
162155	}
163156
164- 	private  Shell  openConentAssist () {
165- 		return  openConentAssist (true );
166- 	}
167- 	private  Shell  openConentAssist (boolean  expectShell ) {
157+ 	private  Shell  openContentAssist (boolean  expectShell ) {
168158		ContentAssistAction  action  = (ContentAssistAction ) editor .getAction (ITextEditorActionConstants .CONTENT_ASSIST );
169159		action .update ();
170160		final  Set <Shell > beforeShells  = Arrays .stream (editor .getSite ().getShell ().getDisplay ().getShells ()).filter (Shell ::isVisible ).collect (Collectors .toSet ());
@@ -183,19 +173,22 @@ private Shell openConentAssist(boolean expectShell) {
183173	 */ 
184174	private  void  checkCompletionContent (final  Table  completionProposalList ) {
185175		// should be instantaneous, but happens to go asynchronous on CI so let's allow a wait 
186- 		waitForProposalRelatedCondition ("Proposal list did not show two initial items" , completionProposalList , 
187- 				() -> completionProposalList .getItemCount () == 2 , 200 );
188- 		assertTrue (isComputingInfoEntry (completionProposalList .getItem (0 )), "Missing computing info entry" );
176+ 		assertTrue (DisplayHelper .waitForCondition (completionProposalList .getDisplay (), 200 , () -> {
177+ 			assertFalse (completionProposalList .isDisposed (), "Completion proposal list was unexpectedly disposed" );
178+ 			return  completionProposalList .getItemCount () == 2  && completionProposalList .getItem (1 ).getData () != null ;
179+ 		}), "Proposal list did not show two initial items" );
189180		assertTrue (isComputingInfoEntry (completionProposalList .getItem (0 )), "Missing computing info entry in proposal list" );
190181		final  TableItem  initialProposalItem  = completionProposalList .getItem (1 );
182+ 		System .out .println (initialProposalItem .toString ());
191183		final  String  initialProposalString  = ((ICompletionProposal )initialProposalItem .getData ()).getDisplayString ();
192184		assertThat ("Unexpected initial proposal item" , 
193185				BAR_CONTENT_ASSIST_PROPOSAL , endsWith (initialProposalString ));
194186		completionProposalList .setSelection (initialProposalItem );
195187		// asynchronous 
196- 		waitForProposalRelatedCondition ("Proposal list did not show two items after finishing computing" , completionProposalList , 
197- 				() -> !isComputingInfoEntry (completionProposalList .getItem (0 )) && completionProposalList .getItemCount () == 2 ,
198- 				LongRunningBarContentAssistProcessor .DELAY  + 200 );
188+ 		assertTrue (DisplayHelper .waitForCondition (completionProposalList .getDisplay (), LongRunningBarContentAssistProcessor .DELAY  * 2 , () -> {
189+ 			assertFalse (completionProposalList .isDisposed (), "Completion proposal list was unexpectedly disposed" );
190+ 			return  !isComputingInfoEntry (completionProposalList .getItem (0 )) && completionProposalList .getItemCount () == 2 ;
191+ 		}), "Proposal list did not show two items after finishing computing" );
199192		final  TableItem  firstCompletionProposalItem  = completionProposalList .getItem (0 );
200193		final  TableItem  secondCompletionProposalItem  = completionProposalList .getItem (1 );
201194		String  firstCompletionProposalText  = ((ICompletionProposal )firstCompletionProposalItem .getData ()).getDisplayString ();
@@ -211,25 +204,27 @@ private static boolean isComputingInfoEntry(TableItem item) {
211204	}
212205
213206	public  static  Shell  findNewShell (Set <Shell > beforeShells , Display  display , boolean  expectShell ) {
214- 		Shell []  afterShells  = Arrays .stream (display .getShells ())
207+ 		List < Shell >  afterShells  = Arrays .stream (display .getShells ())
215208				.filter (Shell ::isVisible )
216209				.filter (shell  -> !beforeShells .contains (shell ))
217- 				.toArray ( Shell []:: new );
210+ 				.toList ( );
218211		if  (expectShell ) {
219- 			assertEquals (1 , afterShells .length , "No new shell found" );
212+ 			assertEquals (1 , afterShells .size () , "No new shell found" );
220213		}
221- 		return  afterShells .length  >  0  ?  afterShells [ 0 ]  : null ;
214+ 		return  afterShells .isEmpty () ?  null  : afterShells . get ( 0 ) ;
222215	}
223216
224217	@ Test 
225218	@ DisabledOnOs (value  = OS .MAC , disabledReason  = "test fails on Mac, see https://github.com/eclipse-platform/eclipse.platform.ui/issues/906" )
226219	public  void  testCompletionFreeze_bug521484 () throws  Exception  {
227220		editor .selectAndReveal (3 , 0 );
228- 		this .completionShell =openConentAssist ( );
221+ 		this .completionShell =openContentAssist ( true );
229222		final  Table  completionProposalList  = findCompletionSelectionControl (this .completionShell );
230223		// should be instantaneous, but happens to go asynchronous on CI so let's allow a wait 
231- 		waitForProposalRelatedCondition ("Proposal list did not show two items" , completionProposalList , 
232- 				() -> completionProposalList .getItemCount () == 2 , 200 );
224+ 		assertTrue (DisplayHelper .waitForCondition (completionProposalList .getDisplay (), 200 , () -> {
225+ 			assertFalse (completionProposalList .isDisposed (), "Completion proposal list was unexpectedly disposed" );
226+ 			return  completionProposalList .getItemCount () == 2 ;
227+ 		}), "Proposal list did not show two items" );
233228		assertTrue (isComputingInfoEntry (completionProposalList .getItem (0 )), "Missing computing info entry" );
234229		// Some processors are long running, moving cursor can cause freeze (bug 521484) 
235230		// asynchronous 
@@ -254,7 +249,7 @@ public void testMoveCaretBackUsesAllProcessors_bug522255() throws Exception {
254249
255250	private  void  emulatePressLeftArrowKey () {
256251		editor .selectAndReveal (((ITextSelection )editor .getSelectionProvider ().getSelection ()).getOffset () - 1 , 0 );
257- 		StyledText  styledText  = ( StyledText )  editor .getAdapter (Control .class );
252+ 		Control  styledText  = editor .getAdapter (Control .class );
258253		Event  e  = new  Event ();
259254		e .type  = ST .VerifyKey ;
260255		e .widget  = styledText ;
0 commit comments