Skip to content

Commit e8c4d88

Browse files
committed
Extract preference memento from UITestCase into TestRule
The preference memento managed in UITestCase is only used in very specific test cases. In addition, the logic is integrated into the UITestCase superclass instead of encapsulating the concern into a dedicated TestRule. This change encapsulates the preference memento into a separate TestRule and only instantiates that rule in those tests that require it.
1 parent 686baa6 commit e8c4d88

File tree

8 files changed

+100
-35
lines changed

8 files changed

+100
-35
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Vector Informatik and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*******************************************************************************/
11+
package org.eclipse.ui.tests.harness.util;
12+
13+
import org.eclipse.jface.preference.IPreferenceStore;
14+
import org.eclipse.jface.preference.PreferenceMemento;
15+
import org.junit.rules.ExternalResource;
16+
17+
/**
18+
* Preference helper to restore changed preference values after test run.
19+
*/
20+
public class PreferenceMementoRule extends ExternalResource {
21+
22+
private PreferenceMemento prefMemento;
23+
24+
@Override
25+
protected void before() {
26+
prefMemento = new PreferenceMemento();
27+
}
28+
29+
@Override
30+
protected void after() {
31+
prefMemento.resetPreferences();
32+
}
33+
34+
/**
35+
* Change a preference value for the associated test run. The preference will
36+
* automatically be reset to the value it had before starting when executing
37+
* {@link #after()}.
38+
*
39+
* @param <T> preference value type. The type must have a corresponding
40+
* {@link IPreferenceStore} setter.
41+
* @param store preference store to manipulate (must not be <code>null</code>)
42+
* @param name preference to change
43+
* @param value new preference value
44+
* @throws IllegalArgumentException when setting a type which is not supported
45+
* by {@link IPreferenceStore}
46+
*
47+
* @see IPreferenceStore#setValue(String, double)
48+
* @see IPreferenceStore#setValue(String, float)
49+
* @see IPreferenceStore#setValue(String, int)
50+
* @see IPreferenceStore#setValue(String, long)
51+
* @see IPreferenceStore#setValue(String, boolean)
52+
* @see IPreferenceStore#setValue(String, String)
53+
*/
54+
public <T> void setPreference(IPreferenceStore store, String name, T value) {
55+
prefMemento.setValue(store, name, value);
56+
}
57+
}

tests/org.eclipse.ui.tests.harness/src/org/eclipse/ui/tests/harness/util/UITestCase.java

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
import java.util.List;
2121
import java.util.Set;
2222

23-
import org.eclipse.jface.preference.IPreferenceStore;
24-
import org.eclipse.jface.preference.PreferenceMemento;
2523
import org.eclipse.swt.widgets.Shell;
2624
import org.eclipse.ui.PlatformUI;
2725
import org.junit.After;
@@ -50,9 +48,6 @@ public abstract class UITestCase extends TestCase {
5048

5149
private Set<Shell> preExistingShells;
5250

53-
/** Preference helper to restore changed preference values after test run. */
54-
private final PreferenceMemento prefMemento = new PreferenceMemento();
55-
5651
/**
5752
* Required to preserve the existing logging output when running tests with
5853
* {@link BlockJUnit4ClassRunner}.
@@ -133,7 +128,6 @@ protected void doSetUp() throws Exception {
133128
public final void tearDown() throws Exception {
134129
String name = runningTest != null ? runningTest : this.getName();
135130
trace(TestRunLogUtil.formatTestFinishedMessage(name));
136-
prefMemento.resetPreferences();
137131
doTearDown();
138132

139133
// Check for shell leak.
@@ -174,26 +168,5 @@ protected void manageWindows(boolean manage) {
174168
closeTestWindows.setEnabled(manage);
175169
}
176170

177-
/**
178-
* Change a preference value for this test run. The preference will be reset to
179-
* its value before test started automatically on {@link #tearDown()}.
180-
*
181-
* @param <T> preference value type. The type must have a corresponding
182-
* {@link IPreferenceStore} setter.
183-
* @param store preference store to manipulate (must not be <code>null</code>)
184-
* @param name preference to change
185-
* @param value new preference value
186-
* @throws IllegalArgumentException when setting a type which is not supported
187-
* by {@link IPreferenceStore}
188-
*
189-
* @see IPreferenceStore#setValue(String, double)
190-
* @see IPreferenceStore#setValue(String, float)
191-
* @see IPreferenceStore#setValue(String, int)
192-
* @see IPreferenceStore#setValue(String, long)
193-
* @see IPreferenceStore#setValue(String, boolean)
194-
* @see IPreferenceStore#setValue(String, String)
195-
*/
196-
protected <T> void setPreference(IPreferenceStore store, String name, T value) {
197-
prefMemento.setValue(store, name, value);
198-
}
171+
199172
}

tests/org.eclipse.ui.tests.performance/src/org/eclipse/ui/tests/performance/ProgressReportingTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import org.eclipse.ui.PlatformUI;
2828
import org.eclipse.ui.internal.IPreferenceConstants;
2929
import org.eclipse.ui.internal.WorkbenchPlugin;
30+
import org.eclipse.ui.tests.harness.util.PreferenceMementoRule;
31+
import org.junit.Rule;
3032

3133
/**
3234
* Verifies the performance of progress reporting APIs in various contexts which
@@ -64,6 +66,10 @@ public class ProgressReportingTest extends BasicPerformanceTest {
6466
* results during profiling.
6567
*/
6668
public static final int MAX_ITERATIONS = 100;
69+
70+
@Rule
71+
public final PreferenceMementoRule preferenceMemento = new PreferenceMementoRule();
72+
6773
private volatile boolean isDone;
6874
private Display display;
6975

@@ -81,7 +87,8 @@ protected void doSetUp() throws Exception {
8187
}
8288

8389
private void setRunInBackground(boolean newRunInBackgroundSetting) {
84-
setPreference(WorkbenchPlugin.getDefault().getPreferenceStore(), IPreferenceConstants.RUN_IN_BACKGROUND,
90+
preferenceMemento.setPreference(WorkbenchPlugin.getDefault().getPreferenceStore(),
91+
IPreferenceConstants.RUN_IN_BACKGROUND,
8592
newRunInBackgroundSetting);
8693
}
8794

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/api/StickyViewTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@
3535
import org.eclipse.ui.internal.util.PrefUtil;
3636
import org.eclipse.ui.part.FileEditorInput;
3737
import org.eclipse.ui.tests.harness.util.FileUtil;
38+
import org.eclipse.ui.tests.harness.util.PreferenceMementoRule;
3839
import org.eclipse.ui.tests.harness.util.UITestCase;
3940
import org.eclipse.ui.views.IStickyViewDescriptor;
4041
import org.junit.Ignore;
42+
import org.junit.Rule;
4143
import org.junit.Test;
4244
import org.junit.runner.RunWith;
4345
import org.junit.runners.JUnit4;
@@ -48,6 +50,9 @@
4850
@RunWith(JUnit4.class)
4951
public class StickyViewTest extends UITestCase {
5052

53+
@Rule
54+
public final PreferenceMementoRule preferenceMemento = new PreferenceMementoRule();
55+
5156
private IWorkbenchWindow window;
5257

5358
private IWorkbenchPage page;
@@ -277,7 +282,8 @@ public void testPerspectiveCloseStandaloneView() throws Throwable {
277282
public void XXXtestPerspectiveViewToolBarVisible() throws Throwable {
278283
// These tests are hard-wired to the pre-3.3 zoom behaviour
279284
// Run them anyway to ensure that we preserve the 3.0 mechanism
280-
setPreference(PrefUtil.getAPIPreferenceStore(), IWorkbenchPreferenceConstants.ENABLE_NEW_MIN_MAX, false);
285+
preferenceMemento.setPreference(PrefUtil.getAPIPreferenceStore(),
286+
IWorkbenchPreferenceConstants.ENABLE_NEW_MIN_MAX, false);
281287

282288
IPerspectiveDescriptor perspective = WorkbenchPlugin.getDefault()
283289
.getPerspectiveRegistry().findPerspectiveWithId(

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/internal/PerspectiveSwitcherTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,19 @@
2121
import org.eclipse.ui.IWorkbenchPreferenceConstants;
2222
import org.eclipse.ui.internal.WorkbenchWindow;
2323
import org.eclipse.ui.internal.util.PrefUtil;
24+
import org.eclipse.ui.tests.harness.util.PreferenceMementoRule;
2425
import org.eclipse.ui.tests.harness.util.UITestCase;
26+
import org.junit.Rule;
2527
import org.junit.Test;
2628
import org.junit.runner.RunWith;
2729
import org.junit.runners.JUnit4;
2830

2931
@RunWith(JUnit4.class)
3032
public class PerspectiveSwitcherTest extends UITestCase {
3133

34+
@Rule
35+
public final PreferenceMementoRule preferenceMemento = new PreferenceMementoRule();
36+
3237
public PerspectiveSwitcherTest() {
3338
super(PerspectiveSwitcherTest.class.getSimpleName());
3439
}
@@ -45,7 +50,8 @@ public void testCreatePerspectiveSwithcerInToolbar() {
4550
assertNotNull("We should have a perspective bar in the beginning", getPerspectiveSwitcher(window)); //$NON-NLS-1$
4651

4752
// turn off the 'Open Perspective' item
48-
setPreference(apiPreferenceStore, IWorkbenchPreferenceConstants.SHOW_OPEN_ON_PERSPECTIVE_BAR, false);
53+
preferenceMemento.setPreference(apiPreferenceStore, IWorkbenchPreferenceConstants.SHOW_OPEN_ON_PERSPECTIVE_BAR,
54+
false);
4955

5056
// check that we still have a perspective bar
5157
assertNotNull("The perspective bar should have been created successfully", getPerspectiveSwitcher(window)); //$NON-NLS-1$

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/internal/StickyViewManagerTest.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
import org.eclipse.ui.IWorkbenchPage;
2525
import org.eclipse.ui.IWorkbenchPreferenceConstants;
2626
import org.eclipse.ui.PlatformUI;
27+
import org.eclipse.ui.tests.harness.util.PreferenceMementoRule;
2728
import org.eclipse.ui.tests.harness.util.UITestCase;
2829
import org.junit.Ignore;
30+
import org.junit.Rule;
2931
import org.junit.Test;
3032
import org.junit.runner.RunWith;
3133
import org.junit.runners.JUnit4;
@@ -37,14 +39,17 @@
3739
@Ignore
3840
public class StickyViewManagerTest extends UITestCase {
3941

42+
@Rule
43+
public final PreferenceMementoRule preferenceMemento = new PreferenceMementoRule();
44+
4045
public StickyViewManagerTest() {
4146
super(StickyViewManagerTest.class.getSimpleName());
4247
}
4348

4449
@Override
4550
protected void doSetUp() throws Exception {
46-
setPreference(PlatformUI.getPreferenceStore(), IWorkbenchPreferenceConstants.ENABLE_32_STICKY_CLOSE_BEHAVIOR,
47-
false);
51+
preferenceMemento.setPreference(PlatformUI.getPreferenceStore(),
52+
IWorkbenchPreferenceConstants.ENABLE_32_STICKY_CLOSE_BEHAVIOR, false);
4853
super.doSetUp();
4954
}
5055

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/intro/IntroTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
import org.eclipse.ui.intro.IIntroManager;
3434
import org.eclipse.ui.intro.IIntroPart;
3535
import org.eclipse.ui.tests.harness.util.EmptyPerspective;
36+
import org.eclipse.ui.tests.harness.util.PreferenceMementoRule;
3637
import org.eclipse.ui.tests.harness.util.UITestCase;
38+
import org.junit.Rule;
3739
import org.junit.Test;
3840
import org.junit.runner.RunWith;
3941
import org.junit.runners.JUnit4;
@@ -44,6 +46,9 @@
4446
@RunWith(JUnit4.class)
4547
public class IntroTest extends UITestCase {
4648

49+
@Rule
50+
public final PreferenceMementoRule preferenceMemento = new PreferenceMementoRule();
51+
4752
IWorkbenchWindow window = null;
4853

4954
private IntroDescriptor oldDesc;
@@ -149,7 +154,8 @@ public void testStandby() {
149154
public void testPerspectiveChange() {
150155
// These tests are hard-wired to the pre-3.3 zoom behaviour
151156
// Run them anyway to ensure that we preserve the 3.0 mechanism
152-
setPreference(PrefUtil.getAPIPreferenceStore(), IWorkbenchPreferenceConstants.ENABLE_NEW_MIN_MAX, false);
157+
preferenceMemento.setPreference(PrefUtil.getAPIPreferenceStore(),
158+
IWorkbenchPreferenceConstants.ENABLE_NEW_MIN_MAX, false);
153159

154160
IWorkbench workbench = window.getWorkbench();
155161
IIntroPart part = workbench.getIntroManager().showIntro(window, false);

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/zoom/ZoomTestCase.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,17 @@
3636
import org.eclipse.ui.part.FileEditorInput;
3737
import org.eclipse.ui.tests.api.MockEditorPart;
3838
import org.eclipse.ui.tests.harness.util.FileUtil;
39+
import org.eclipse.ui.tests.harness.util.PreferenceMementoRule;
3940
import org.eclipse.ui.tests.harness.util.UITestCase;
4041
import org.junit.Assert;
42+
import org.junit.Rule;
4143

4244
public class ZoomTestCase extends UITestCase {
4345
// protected static final String view2Id = IPageLayout.ID_OUTLINE;
4446

47+
@Rule
48+
public final PreferenceMementoRule preferenceMemento = new PreferenceMementoRule();
49+
4550
protected WorkbenchWindow window;
4651

4752
protected WorkbenchPage page;
@@ -74,7 +79,7 @@ protected void doSetUp() throws Exception {
7479

7580
// These tests are hard-wired to the pre-3.3 zoom behaviour
7681
// Run them anyway to ensure that we preserve the 3.0 mechanism
77-
setPreference(apiStore, IWorkbenchPreferenceConstants.ENABLE_NEW_MIN_MAX, false);
82+
preferenceMemento.setPreference(apiStore, IWorkbenchPreferenceConstants.ENABLE_NEW_MIN_MAX, false);
7883

7984
try {
8085
project = FileUtil.createProject("IEditorPartTest"); //$NON-NLS-1$

0 commit comments

Comments
 (0)