-
Notifications
You must be signed in to change notification settings - Fork 189
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 with widget extraction functionality based on explicit IDs assigned to the UI elements of interest. Fixes #2060
- Loading branch information
1 parent
9b21618
commit 70a2d11
Showing
7 changed files
with
218 additions
and
91 deletions.
There are no files selected for viewing
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
83 changes: 83 additions & 0 deletions
83
...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,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; | ||
|
||
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; | ||
} | ||
|
||
} |
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.