1616
1717import java .io .ByteArrayInputStream ;
1818import java .lang .reflect .Method ;
19+ import java .util .HashSet ;
20+ import java .util .Set ;
1921import java .util .concurrent .atomic .AtomicBoolean ;
2022
2123import org .eclipse .core .commands .Command ;
4951
5052@ ExtendWith (AllCleanRule .class )
5153public class TestJsTs {
54+ private static final String WIZARD_CLASSNAME_TEMPLATE = "org.eclipse.ltk.internal.ui.refactoring.Refactoring" ;
55+ private static final String WIZARD_RENAME = "Rename" ;
56+ private static final String WIZARD_REFACTORING = "Refactoring" ;
57+ private static final String BUTTON_OK = "OK" ;
58+ private static final String BUTTON_CANCEL = "Cancel" ;
59+ private static final String BUTTON_CONTINUE = "Con&tinue" ;
60+ private static final String BUTTON_BACK = "< &Back" ;
61+
5262 private IProject project ;
5363
5464 @ BeforeEach
@@ -76,8 +86,9 @@ public void testRefactoringRename() throws Exception {
7686 "org.eclipse.ui.genericeditor.GenericEditor" );
7787 editor .getSelectionProvider ().setSelection (new TextSelection (offset , 0 ));
7888 editor .setFocus ();
79- DisplayHelper .sleep (2000 ); // Give time for LS to initialize enough before making edit and sending a
89+ DisplayHelper .sleep (5000 ); // Give time for LS to initialize enough before making edit and sending a
8090 // didChange
91+
8192 IDocument document = editor .getDocumentProvider ().getDocument (editor .getEditorInput ());
8293
8394 ICommandService commandService = PlatformUI .getWorkbench ().getService (ICommandService .class );
@@ -97,26 +108,27 @@ public void testRefactoringRename() throws Exception {
97108 if (event .widget instanceof Composite ) {
98109 Composite c = (Composite ) event .widget ;
99110 Shell shell = c .getShell ();
100- if (shell != ideShell ) {
101- if ("Rename" .equals (shell .getText ())) {
111+ if (shell != ideShell && shell .getData ().getClass ().getName ().startsWith (WIZARD_CLASSNAME_TEMPLATE )) {
112+ Set <String > buttons = getButtons (c );
113+ if (WIZARD_RENAME .equals (shell .getText ())) {
102114 if (!renameDialogOkPressed .get ()) {
103- if (hasButton ( c , "OK" )) {
115+ if (buttons . contains ( BUTTON_OK )) {
104116 event .widget .getDisplay ().asyncExec (() -> pressOk (shell ));
105117 renameDialogOkPressed .set (true );
106118 }
107119 } else if (!renameDialogContinuePressed .get ()) {
108- if (hasButton ( c , "Con&tinue" )) {
120+ if (buttons . contains ( BUTTON_CONTINUE )) {
109121 event .widget .getDisplay ().asyncExec (() -> pressOk (shell ));
110122 renameDialogContinuePressed .set (true );
111- } else if (!renameDialogCancelPressed .get () && hasButton ( c , "Cancel" )
112- && hasButton ( c , "< &Back" )) {
123+ } else if (!renameDialogCancelPressed .get () && buttons . contains ( BUTTON_CANCEL )
124+ && buttons . contains ( BUTTON_BACK )) {
113125 event .widget .getDisplay ().asyncExec (() -> pressCancel (shell ));
114126 renameDialogCancelPressed .set (true );
115127 }
116128 }
117- } else if ("Refactoring" .equals (shell .getText ())) {
129+ } else if (WIZARD_REFACTORING .equals (shell .getText ())) {
118130 if (!errorDialogOkPressed .get ()) {
119- if (hasButton ( c , "OK" )) {
131+ if (buttons . contains ( BUTTON_OK )) {
120132 event .widget .getDisplay ().asyncExec (() -> pressOk (shell ));
121133 errorDialogOkPressed .set (true );
122134 }
@@ -125,10 +137,12 @@ && hasButton(c, "< &Back")) {
125137 }
126138 }
127139 };
140+
128141 try {
129142 display .addFilter (SWT .Paint , pressOKonRenameDialogPaint );
130143 ExecutionEvent executionEvent = handlerService .createExecutionEvent (command , e );
131144 command .executeWithChecks (executionEvent );
145+
132146 assertTrue (new DisplayHelper () {
133147 @ Override
134148 protected boolean condition () {
@@ -147,22 +161,19 @@ protected boolean condition() {
147161 }
148162 }
149163
150- private boolean hasButton (Widget w , String requiredText ) {
164+ static private Set <String > getButtons (Widget w ) {
165+ Set <String > result = new HashSet <>();
151166 if (w instanceof Button ) {
152- Button b = (Button ) w ;
153- return requiredText .equals (b .getText ());
154- }
155- if (w instanceof Composite ) {
167+ result .add (((Button ) w ).getText ());
168+ } else if (w instanceof Composite ) {
156169 for (Control child : ((Composite ) w ).getChildren ()) {
157- if (hasButton (child , requiredText )) {
158- return true ;
159- }
170+ result .addAll (getButtons (child ));
160171 }
161172 }
162- return false ;
173+ return result ;
163174 }
164175
165- private void pressOk (Shell dialogShell ) {
176+ static private void pressOk (Shell dialogShell ) {
166177 try {
167178 Dialog dialog = (Dialog ) dialogShell .getData ();
168179 Method okPressedMethod = Dialog .class .getDeclaredMethod ("okPressed" );
@@ -174,7 +185,7 @@ private void pressOk(Shell dialogShell) {
174185 }
175186 }
176187
177- private void pressCancel (Shell dialogShell ) {
188+ static private void pressCancel (Shell dialogShell ) {
178189 try {
179190 Dialog dialog = (Dialog ) dialogShell .getData ();
180191 Method cancelPressedMethod = Dialog .class .getDeclaredMethod ("cancelPressed" );
0 commit comments