Skip to content

Commit

Permalink
[test] improves the restoration of preferences in the test
Browse files Browse the repository at this point in the history
This commit improves the preference change methods, so that it can be
called several times without any problem to restore the initial value.

Signed-off-by: Séraphin Costa <[email protected]>
  • Loading branch information
scosta-obeo committed Sep 12, 2024
1 parent b833d35 commit dc87bcc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1620,8 +1620,6 @@ protected void closeAndReloadSession() throws Exception {
/**
* Change a preference and store the old value. It will be automatically reset during tear down.
*
* TO CALL ONLY ONCE PER TEST (set up + test)
*
* @param preferenceKey
* The key of the preference.
* @param newValue
Expand All @@ -1631,7 +1629,7 @@ protected void changeDiagramPreference(String preferenceKey, Integer newValue) {
SiriusAssert.assertNoDiagramUIPreferenceChangedinDiagramCoreStore(preferenceKey);

int oldValue = Platform.getPreferencesService().getInt(DiagramPlugin.ID, preferenceKey, 0, null);
oldValueDiagramPreferences.put(preferenceKey, oldValue);
oldValueDiagramPreferences.putIfAbsent(preferenceKey, oldValue);

IEclipsePreferences diagramCorePreferences = InstanceScope.INSTANCE.getNode(DiagramPlugin.ID);
diagramCorePreferences.putInt(preferenceKey, newValue);
Expand All @@ -1643,8 +1641,6 @@ protected void changeDiagramPreference(String preferenceKey, Integer newValue) {
/**
* Change a boolean preference and store the old value. It will be automatically reset during tear down.
*
* TO CALL ONLY ONCE PER TEST (set up + test)
*
* @param preferenceKey
* The key of the preference.
* @param newValue
Expand All @@ -1654,7 +1650,7 @@ protected void changeDiagramPreference(String preferenceKey, Boolean newValue) {
SiriusAssert.assertNoDiagramUIPreferenceChangedinDiagramCoreStore(preferenceKey);

boolean oldValue = Platform.getPreferencesService().getBoolean(DiagramPlugin.ID, preferenceKey, false, null);
oldValueDiagramPreferences.put(preferenceKey, oldValue);
oldValueDiagramPreferences.putIfAbsent(preferenceKey, oldValue);

IEclipsePreferences diagramCorePreferences = InstanceScope.INSTANCE.getNode(DiagramPlugin.ID);
diagramCorePreferences.putBoolean(preferenceKey, newValue);
Expand Down Expand Up @@ -1698,34 +1694,32 @@ private void resetDiagramPreference(String preferenceKey, IEclipsePreferences di
/**
* Change a preference and store the old value. It will be automatically reset during tear down.
*
* TO CALL ONLY ONCE PER TEST (set up + test)
*
* @param preferenceKey
* The key of the preference.
* @param newValue
* The new value.
*/
protected void changeDiagramUIPreference(String preferenceKey, Integer newValue) {
SiriusAssert.assertNoDiagramCorePreferenceChangedinDiagramUIStore(preferenceKey);

final IPreferenceStore prefs = DiagramUIPlugin.getPlugin().getPreferenceStore();
oldValueDiagramUiPreferences.put(preferenceKey, prefs.getInt(preferenceKey));
oldValueDiagramUiPreferences.putIfAbsent(preferenceKey, prefs.getInt(preferenceKey));
prefs.setValue(preferenceKey, newValue);
}

/**
* Change a boolean preference and store the old value. It will be automatically reset during tear down.
*
* TO CALL ONLY ONCE PER TEST (set up + test)
*
* @param preferenceKey
* The key of the preference.
* @param newValue
* The new value.
*/
protected void changeDiagramUIPreference(String preferenceKey, Boolean newValue) {
SiriusAssert.assertNoDiagramCorePreferenceChangedinDiagramUIStore(preferenceKey);

final IPreferenceStore prefs = DiagramUIPlugin.getPlugin().getPreferenceStore();
oldValueDiagramUiPreferences.put(preferenceKey, prefs.getBoolean(preferenceKey));
oldValueDiagramUiPreferences.putIfAbsent(preferenceKey, prefs.getBoolean(preferenceKey));
prefs.setValue(preferenceKey, newValue);
}

Expand Down Expand Up @@ -1778,16 +1772,15 @@ protected void changePlatformUIPreference(String preferenceKey, Boolean newValue
/**
* Change a boolean preference and store the old value. It will be automatically reset during tear down.
*
* TO CALL ONLY ONCE PER TEST (set up + test)
*
* @param preferenceKey
* The key of the preference.
* @param newValue
* The new value.
*/
protected void changeSiriusPreference(String preferenceKey, Boolean newValue) {
boolean oldValue = Platform.getPreferencesService().getBoolean(SiriusPlugin.ID, preferenceKey, false, null);
oldValueSiriusPreferences.put(preferenceKey, oldValue);

oldValueSiriusPreferences.putIfAbsent(preferenceKey, oldValue);

IEclipsePreferences corePreferences = InstanceScope.INSTANCE.getNode(SiriusPlugin.ID);
corePreferences.putBoolean(preferenceKey, newValue);
Expand All @@ -1799,8 +1792,6 @@ protected void changeSiriusPreference(String preferenceKey, Boolean newValue) {
/**
* Change a boolean preference and store the old value. It will be automatically reset during tear down.
*
* TO CALL ONLY ONCE PER TEST (set up + test)
*
* @param preferenceKey
* The key of the preference.
* @param newValue
Expand All @@ -1810,7 +1801,7 @@ protected void changeSiriusUIPreference(String preferenceKey, Boolean newValue)
SiriusAssert.assertNoSiriusCorePreferenceChangedinSiriusUIStore(preferenceKey);

IPreferenceStore viewpointUIPrefs = SiriusEditPlugin.getPlugin().getPreferenceStore();
oldValueSiriusUIPreferences.put(preferenceKey, viewpointUIPrefs.getBoolean(preferenceKey));
oldValueSiriusUIPreferences.putIfAbsent(preferenceKey, viewpointUIPrefs.getBoolean(preferenceKey));
viewpointUIPrefs.setValue(preferenceKey, newValue);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -588,8 +588,6 @@ protected void launchCancelCustomStyle() {
/**
* Change a preference and store the old value. It will be automatically reset during tear down.
*
* TO CALL ONLY ONCE PER TEST (set up + test)
*
* @param preferenceKey
* The key of the preference.
* @param newValue
Expand All @@ -599,7 +597,7 @@ protected void changeDiagramPreference(String preferenceKey, Integer newValue) {
assertNoDiagramUIPreferenceChangedinDiagramCoreStore(preferenceKey);

int oldValue = Platform.getPreferencesService().getInt(DiagramPlugin.ID, preferenceKey, 0, null);
oldValueDiagramPreferences.put(preferenceKey, oldValue);
oldValueDiagramPreferences.putIfAbsent(preferenceKey, oldValue);

IEclipsePreferences diagramCorePreferences = InstanceScope.INSTANCE.getNode(DiagramPlugin.ID);
diagramCorePreferences.putInt(preferenceKey, newValue);
Expand All @@ -611,8 +609,6 @@ protected void changeDiagramPreference(String preferenceKey, Integer newValue) {
/**
* Change a boolean preference and store the old value. It will be automatically reset during tear down.
*
* TO CALL ONLY ONCE PER TEST (set up + test)
*
* @param preferenceKey
* The key of the preference.
* @param newValue
Expand All @@ -622,7 +618,7 @@ protected void changeDiagramPreference(String preferenceKey, Boolean newValue) {
assertNoDiagramUIPreferenceChangedinDiagramCoreStore(preferenceKey);

boolean oldValue = Platform.getPreferencesService().getBoolean(DiagramPlugin.ID, preferenceKey, false, null);
oldValueDiagramPreferences.put(preferenceKey, oldValue);
oldValueDiagramPreferences.putIfAbsent(preferenceKey, oldValue);

IEclipsePreferences diagramCorePreferences = InstanceScope.INSTANCE.getNode(DiagramPlugin.ID);
diagramCorePreferences.putBoolean(preferenceKey, newValue);
Expand Down Expand Up @@ -664,8 +660,6 @@ private void resetDiagramPreference(String preferenceKey, IEclipsePreferences di
/**
* Change a boolean preference and store the old value. It will be automatically reset during tear down.
*
* TO CALL ONLY ONCE PER TEST (set up + test)
*
* @param preferenceKey
* The key of the preference.
* @param newValue
Expand All @@ -675,15 +669,13 @@ protected void changeDiagramUIPreference(final String preferenceKey, final Boole
assertNoDiagramCorePreferenceChangedinDiagramUIStore(preferenceKey);

final IPreferenceStore prefs = DiagramUIPlugin.getPlugin().getPreferenceStore();
oldValueDiagramUIPreferences.put(preferenceKey, prefs.getBoolean(preferenceKey));
oldValueDiagramUIPreferences.putIfAbsent(preferenceKey, prefs.getBoolean(preferenceKey));
UIThreadRunnable.syncExec(() -> prefs.setValue(preferenceKey, newValue));
}

/**
* Change an int preference and store the old value. It will be automatically reset during tear down.
*
* TO CALL ONLY ONCE PER TEST (set up + test)
*
* @param preferenceKey
* The key of the preference.
* @param newValue
Expand All @@ -693,15 +685,13 @@ protected void changeDiagramUIPreference(final String preferenceKey, final int n
assertNoDiagramCorePreferenceChangedinDiagramUIStore(preferenceKey);

final IPreferenceStore prefs = DiagramUIPlugin.getPlugin().getPreferenceStore();
oldValueDiagramUIPreferences.put(preferenceKey, prefs.getInt(preferenceKey));
oldValueDiagramUIPreferences.putIfAbsent(preferenceKey, prefs.getInt(preferenceKey));
UIThreadRunnable.syncExec(() -> prefs.setValue(preferenceKey, newValue));
}

/**
* Change a double preference and store the old value. It will be automatically reset during tear down.
*
* TO CALL ONLY ONCE PER TEST (set up + test)
*
* @param preferenceKey
* The key of the preference.
* @param newValue
Expand All @@ -711,7 +701,7 @@ protected void changeDiagramUIPreference(final String preferenceKey, final doubl
assertNoDiagramCorePreferenceChangedinDiagramUIStore(preferenceKey);

final IPreferenceStore prefs = DiagramUIPlugin.getPlugin().getPreferenceStore();
oldValueDiagramUIPreferences.put(preferenceKey, prefs.getDouble(preferenceKey));
oldValueDiagramUIPreferences.putIfAbsent(preferenceKey, prefs.getDouble(preferenceKey));
UIThreadRunnable.syncExec(() -> prefs.setValue(preferenceKey, newValue));
}

Expand Down Expand Up @@ -748,16 +738,14 @@ private void resetDiagramUIPreference(String preferenceKey, IPreferenceStore dia
/**
* Change a boolean preference and store the old value. It will be automatically reset during tear down.
*
* TO CALL ONLY ONCE PER TEST (set up + test)
*
* @param preferenceKey
* The key of the preference.
* @param newValue
* The new value.
*/
protected void changeSiriusPreference(String preferenceKey, Boolean newValue) {
boolean oldValue = Platform.getPreferencesService().getBoolean(SiriusPlugin.ID, preferenceKey, false, null);
oldValueSiriusPreferences.put(preferenceKey, oldValue);
oldValueSiriusPreferences.putIfAbsent(preferenceKey, oldValue);

IEclipsePreferences corePreferences = InstanceScope.INSTANCE.getNode(SiriusPlugin.ID);
corePreferences.putBoolean(preferenceKey, newValue);
Expand All @@ -769,16 +757,14 @@ protected void changeSiriusPreference(String preferenceKey, Boolean newValue) {
/**
* Change a boolean preference and store the old value. It will be automatically reset during tear down.
*
* TO CALL ONLY ONCE PER TEST (set up + test)
*
* @param preferenceKey
* The key of the preference.
* @param newValue
* The new value.
*/
protected void changeSiriusCommonPreference(String preferenceKey, Boolean newValue) {
boolean oldValue = Platform.getPreferencesService().getBoolean(SiriusPlugin.ID, preferenceKey, false, null);
oldValueCommonPreferences.put(preferenceKey, oldValue);
oldValueCommonPreferences.putIfAbsent(preferenceKey, oldValue);

IEclipsePreferences corePreferences = InstanceScope.INSTANCE.getNode(SiriusTransPlugin.PLUGIN_ID);
corePreferences.putBoolean(preferenceKey, newValue);
Expand All @@ -790,8 +776,6 @@ protected void changeSiriusCommonPreference(String preferenceKey, Boolean newVal
/**
* Change a boolean preference and store the old value. It will be automatically reset during tear down.
*
* TO CALL ONLY ONCE PER TEST (set up + test)
*
* @param preferenceKey
* The key of the preference.
* @param newValue
Expand All @@ -801,7 +785,7 @@ protected void changeSiriusUIPreference(String preferenceKey, Boolean newValue)
assertNoSiriusCorePreferenceChangedinSiriusUIStore(preferenceKey);

IPreferenceStore viewpointUIPrefs = SiriusEditPlugin.getPlugin().getPreferenceStore();
oldValueSiriusUIPreferences.put(preferenceKey, viewpointUIPrefs.getBoolean(preferenceKey));
oldValueSiriusUIPreferences.putIfAbsent(preferenceKey, viewpointUIPrefs.getBoolean(preferenceKey));
viewpointUIPrefs.setValue(preferenceKey, newValue);
}

Expand All @@ -815,7 +799,7 @@ protected void changeSiriusUIPreference(String preferenceKey, Boolean newValue)
*/
protected void changePlatformUIPreference(String preferenceKey, Boolean newValue) {
IPreferenceStore viewpointUIPrefs = PlatformUI.getPreferenceStore();
oldPlatformUIPreferences.put(preferenceKey, viewpointUIPrefs.getBoolean(preferenceKey));
oldPlatformUIPreferences.putIfAbsent(preferenceKey, viewpointUIPrefs.getBoolean(preferenceKey));
viewpointUIPrefs.setValue(preferenceKey, newValue);
}

Expand Down

0 comments on commit dc87bcc

Please sign in to comment.