-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove reflective access from find/replace tests #2060
The tests for the FindReplaceDialog and FindReplaceOverlay currently use reflection to access specific UI elements. This ties the test implementations to implementation details of the production classes (i.e., specific hidden field to be present) and particularly requires the production code to contain (hidden) fields even if they would not be required just to provide according tests. This change replaces the reflective access (at the code level) with widget extraction functionality based on unique information about the widget to be searched for. This may also be considered reflective (at the configuration level), but at least it gets rid of requiring the code to contain specific fields just in order to write tests that need to extract them. Fixes #2060
- Loading branch information
1 parent
f35ef3a
commit aaed0d2
Showing
5 changed files
with
168 additions
and
81 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
...orkbench.texteditor.tests/src/org/eclipse/ui/internal/findandreplace/WidgetExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package org.eclipse.ui.internal.findandreplace; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.function.Function; | ||
|
||
import org.eclipse.swt.widgets.Button; | ||
import org.eclipse.swt.widgets.Combo; | ||
import org.eclipse.swt.widgets.Composite; | ||
import org.eclipse.swt.widgets.ToolBar; | ||
import org.eclipse.swt.widgets.ToolItem; | ||
import org.eclipse.swt.widgets.Widget; | ||
|
||
import org.eclipse.ui.internal.findandreplace.overlay.HistoryTextWrapper; | ||
|
||
public final class WidgetExtractor { | ||
|
||
private final Composite container; | ||
|
||
public WidgetExtractor(Composite container) { | ||
this.container= container; | ||
} | ||
|
||
public HistoryTextWrapper findHistoryTextWrapperWithMessage(String includedMessageText) { | ||
List<HistoryTextWrapper> widgets= findWidgets(container, HistoryTextWrapper.class, candidate -> { | ||
String message= removeMnemonicsAndMakeLowercase(candidate.getTextBar().getMessage()); | ||
return message.contains(includedMessageText); | ||
}); | ||
return widgets.isEmpty() ? null : widgets.get(0); | ||
} | ||
|
||
public List<Combo> findCombos() { | ||
return findWidgets(container, Combo.class, candidate -> true); | ||
} | ||
|
||
public Button findButtonWithText(String includedText, String... excludedTexts) { | ||
List<Button> widgets= findWidgets(container, Button.class, candidate -> { | ||
String text= removeMnemonicsAndMakeLowercase(candidate.getText()); | ||
boolean containsIncludeString= text.contains(includedText); | ||
boolean containsExcludeStrings= Arrays.stream(excludedTexts).anyMatch(it -> text.contains(it)); | ||
return containsIncludeString && !containsExcludeStrings; | ||
}); | ||
return widgets.isEmpty() ? null : widgets.get(0); | ||
} | ||
|
||
public Button findButtonWithTooltipText(String includedToolTipText) { | ||
List<Button> widgets= findWidgets(container, Button.class, candidate -> { | ||
String toolTipText= candidate.getToolTipText(); | ||
return toolTipText != null && removeMnemonicsAndMakeLowercase(toolTipText).contains(includedToolTipText); | ||
}); | ||
return widgets.isEmpty() ? null : widgets.get(0); | ||
} | ||
|
||
public ToolItem findToolItemWithTooltipText(String includedToolTipText, String... excludedToolTipTexts) { | ||
List<ToolItem> widgets= findWidgets(container, ToolItem.class, candidate -> { | ||
String toolTipText= removeMnemonicsAndMakeLowercase(candidate.getToolTipText()); | ||
boolean containsIncludeString= toolTipText.contains(includedToolTipText); | ||
boolean containsExcludeStrings= Arrays.stream(excludedToolTipTexts).anyMatch(it -> toolTipText.contains(it)); | ||
return containsIncludeString && !containsExcludeStrings; | ||
}); | ||
return widgets.isEmpty() ? null : widgets.get(0); | ||
} | ||
|
||
private static <T extends Widget> List<T> findWidgets(Composite container, Class<T> type, Function<T, Boolean> matcher) { | ||
List<Widget> children= new ArrayList<>(); | ||
children.addAll(List.of(container.getChildren())); | ||
if (container instanceof ToolBar toolbar) { | ||
children.addAll(List.of(toolbar.getItems())); | ||
} | ||
List<T> result= new ArrayList<>(); | ||
for (Widget child : children) { | ||
if (type.isInstance(child)) { | ||
@SuppressWarnings("unchecked") | ||
T typedChild= (T) child; | ||
if (matcher.apply(typedChild)) { | ||
result.add(typedChild); | ||
} | ||
} | ||
if (child instanceof Composite compositeChild) { | ||
result.addAll(findWidgets(compositeChild, type, matcher)); | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
private static String removeMnemonicsAndMakeLowercase(String string) { | ||
return string == null ? "" : string.replaceAll("&", "").toLowerCase(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.