-
Notifications
You must be signed in to change notification settings - Fork 228
Remove reflective access from find/replace tests #2060 #2333
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /******************************************************************************* | ||
| * Copyright (c) 2024 Vector Informatik GmbH and others. | ||
| * | ||
| * This program and the accompanying materials | ||
| * are made available under the terms of the Eclipse Public License 2.0 | ||
| * which accompanies this distribution, and is available at | ||
| * https://www.eclipse.org/legal/epl-2.0/ | ||
| * | ||
| * SPDX-License-Identifier: EPL-2.0 | ||
| * | ||
| * Contributors: | ||
| * Vector Informatik GmbH - initial API and implementation | ||
| *******************************************************************************/ | ||
| package org.eclipse.ui.internal.findandreplace; | ||
HeikoKlare marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import static org.junit.Assert.assertFalse; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| 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 rootContainer; | ||
|
|
||
| private final String idDataKey; | ||
|
|
||
| public WidgetExtractor(String idDataKey, Composite container) { | ||
| this.idDataKey= idDataKey; | ||
| this.rootContainer= container; | ||
| } | ||
|
|
||
| public HistoryTextWrapper findHistoryTextWrapper(String id) { | ||
| return findWidget(rootContainer, HistoryTextWrapper.class, id); | ||
| } | ||
|
|
||
| public Combo findCombo(String id) { | ||
| return findWidget(rootContainer, Combo.class, id); | ||
| } | ||
|
|
||
| public Button findButton(String id) { | ||
| return findWidget(rootContainer, Button.class, id); | ||
| } | ||
|
|
||
| public ToolItem findToolItem(String id) { | ||
| return findWidget(rootContainer, ToolItem.class, id); | ||
| } | ||
|
|
||
| private <T extends Widget> T findWidget(Composite container, Class<T> type, String id) { | ||
| List<T> widgets= findWidgets(container, type, id); | ||
| assertFalse("more than one matching widget found for id '" + id + "':" + widgets, widgets.size() > 1); | ||
| return widgets.isEmpty() ? null : widgets.get(0); | ||
| } | ||
|
|
||
| private <T extends Widget> List<T> findWidgets(Composite container, Class<T> type, String id) { | ||
| 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)) { | ||
| if (id.equals(child.getData(idDataKey))) { | ||
| result.add(type.cast(child)); | ||
| } | ||
| } | ||
| if (child instanceof Composite compositeChild) { | ||
| result.addAll(findWidgets(compositeChild, type, id)); | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -45,8 +45,8 @@ public class FindReplaceOverlayTest extends FindReplaceUITest<OverlayAccess> { | |||||
| public OverlayAccess openUIFromTextViewer(TextViewer viewer) { | ||||||
| Accessor actionAccessor= new Accessor(getFindReplaceAction(), FindReplaceAction.class); | ||||||
| actionAccessor.invoke("showOverlayInEditor", null); | ||||||
| Accessor overlayAccessor= new Accessor(actionAccessor.get("overlay"), "org.eclipse.ui.internal.findandreplace.overlay.FindReplaceOverlay", getClass().getClassLoader()); | ||||||
| return new OverlayAccess(getFindReplaceTarget(), overlayAccessor); | ||||||
| FindReplaceOverlay overlay= (FindReplaceOverlay) actionAccessor.get("overlay"); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Just as an example on how one can "transport" an object through the data of the underlying control, of course the If this is needed on multiple places one can even has
that encapsulates the details on how a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That looks like a great idea for further reducing reflective access of the tests (in this case related to the |
||||||
| return new OverlayAccess(getFindReplaceTarget(), overlay); | ||||||
| } | ||||||
|
|
||||||
| @Test | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.