Skip to content

Commit ef718b0

Browse files
committed
Migrate to PreferenceMementoExtension in PR 3612
1 parent 6ff09fb commit ef718b0

File tree

3 files changed

+72
-8
lines changed

3 files changed

+72
-8
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.jupiter.api.extension.AfterEachCallback;
16+
import org.junit.jupiter.api.extension.BeforeEachCallback;
17+
import org.junit.jupiter.api.extension.ExtensionContext;
18+
19+
/**
20+
* Preference helper to restore changed preference values after test run.
21+
*/
22+
public class PreferenceMementoExtension implements BeforeEachCallback, AfterEachCallback {
23+
24+
private PreferenceMemento prefMemento;
25+
26+
@Override
27+
public void beforeEach(ExtensionContext context) {
28+
prefMemento = new PreferenceMemento();
29+
}
30+
31+
@Override
32+
public void afterEach(ExtensionContext context) {
33+
if (prefMemento != null) {
34+
prefMemento.resetPreferences();
35+
}
36+
}
37+
38+
/**
39+
* Change a preference value for the associated test run. The preference will
40+
* automatically be reset to the value it had before starting when executing
41+
* {@link #afterEach(ExtensionContext)}.
42+
*
43+
* @param <T> preference value type. The type must have a corresponding
44+
* {@link IPreferenceStore} setter.
45+
* @param store preference store to manipulate (must not be <code>null</code>)
46+
* @param name preference to change
47+
* @param value new preference value
48+
* @throws IllegalArgumentException when setting a type which is not supported
49+
* by {@link IPreferenceStore}
50+
*
51+
* @see IPreferenceStore#setValue(String, double)
52+
* @see IPreferenceStore#setValue(String, float)
53+
* @see IPreferenceStore#setValue(String, int)
54+
* @see IPreferenceStore#setValue(String, long)
55+
* @see IPreferenceStore#setValue(String, boolean)
56+
* @see IPreferenceStore#setValue(String, String)
57+
*/
58+
public <T> void setPreference(IPreferenceStore store, String name, T value) {
59+
if (prefMemento == null) {
60+
prefMemento = new PreferenceMemento();
61+
}
62+
prefMemento.setValue(store, name, value);
63+
}
64+
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@
2323
import org.eclipse.ui.internal.WorkbenchWindow;
2424
import org.eclipse.ui.internal.util.PrefUtil;
2525
import org.eclipse.ui.tests.harness.util.CloseTestWindowsExtension;
26-
import org.eclipse.ui.tests.harness.util.PreferenceMementoRule;
27-
import org.junit.Rule;
26+
import org.eclipse.ui.tests.harness.util.PreferenceMementoExtension;
2827
import org.junit.jupiter.api.Test;
2928
import org.junit.jupiter.api.extension.ExtendWith;
29+
import org.junit.jupiter.api.extension.RegisterExtension;
3030

3131
@ExtendWith(CloseTestWindowsExtension.class)
3232
public class PerspectiveSwitcherTest {
3333

34-
@Rule
35-
public final PreferenceMementoRule preferenceMemento = new PreferenceMementoRule();
34+
@RegisterExtension
35+
public final PreferenceMementoExtension preferenceMemento = new PreferenceMementoExtension();
3636

3737
/**
3838
* This test ensures that our workbench window's perspective bar can opened if

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727
import org.eclipse.ui.IWorkbenchPreferenceConstants;
2828
import org.eclipse.ui.PlatformUI;
2929
import org.eclipse.ui.tests.harness.util.CloseTestWindowsExtension;
30-
import org.eclipse.ui.tests.harness.util.PreferenceMementoRule;
31-
import org.junit.Rule;
30+
import org.eclipse.ui.tests.harness.util.PreferenceMementoExtension;
3231
import org.junit.jupiter.api.BeforeEach;
3332
import org.junit.jupiter.api.Disabled;
3433
import org.junit.jupiter.api.Test;
3534
import org.junit.jupiter.api.extension.ExtendWith;
35+
import org.junit.jupiter.api.extension.RegisterExtension;
3636

3737
/**
3838
* @since 3.6
@@ -41,8 +41,8 @@
4141
@ExtendWith(CloseTestWindowsExtension.class)
4242
public class StickyViewManagerTest {
4343

44-
@Rule
45-
public final PreferenceMementoRule preferenceMemento = new PreferenceMementoRule();
44+
@RegisterExtension
45+
public final PreferenceMementoExtension preferenceMemento = new PreferenceMementoExtension();
4646

4747

4848
@BeforeEach

0 commit comments

Comments
 (0)