From dc87bccb24c6637c0b7a842a4baa99a4a89253b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9raphin=20Costa?= Date: Fri, 6 Sep 2024 18:37:01 +0200 Subject: [PATCH] [test] improves the restoration of preferences in the test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../tests/support/api/SiriusTestCase.java | 27 +++++---------- .../api/AbstractSiriusSwtBotGefTestCase.java | 34 +++++-------------- 2 files changed, 18 insertions(+), 43 deletions(-) diff --git a/plugins/org.eclipse.sirius.tests.junit.support/src/org/eclipse/sirius/tests/support/api/SiriusTestCase.java b/plugins/org.eclipse.sirius.tests.junit.support/src/org/eclipse/sirius/tests/support/api/SiriusTestCase.java index d444c35538..f55955db8c 100644 --- a/plugins/org.eclipse.sirius.tests.junit.support/src/org/eclipse/sirius/tests/support/api/SiriusTestCase.java +++ b/plugins/org.eclipse.sirius.tests.junit.support/src/org/eclipse/sirius/tests/support/api/SiriusTestCase.java @@ -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 @@ -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); @@ -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 @@ -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); @@ -1698,8 +1694,6 @@ 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 @@ -1707,16 +1701,15 @@ private void resetDiagramPreference(String preferenceKey, IEclipsePreferences di */ 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 @@ -1724,8 +1717,9 @@ protected void changeDiagramUIPreference(String preferenceKey, Integer newValue) */ 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); } @@ -1778,8 +1772,6 @@ 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 @@ -1787,7 +1779,8 @@ protected void changePlatformUIPreference(String preferenceKey, Boolean newValue */ 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); @@ -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 @@ -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); } diff --git a/plugins/org.eclipse.sirius.tests.swtbot.support/src/org/eclipse/sirius/tests/swtbot/support/api/AbstractSiriusSwtBotGefTestCase.java b/plugins/org.eclipse.sirius.tests.swtbot.support/src/org/eclipse/sirius/tests/swtbot/support/api/AbstractSiriusSwtBotGefTestCase.java index fef64d03a0..136cb3ba5c 100644 --- a/plugins/org.eclipse.sirius.tests.swtbot.support/src/org/eclipse/sirius/tests/swtbot/support/api/AbstractSiriusSwtBotGefTestCase.java +++ b/plugins/org.eclipse.sirius.tests.swtbot.support/src/org/eclipse/sirius/tests/swtbot/support/api/AbstractSiriusSwtBotGefTestCase.java @@ -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 @@ -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); @@ -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 @@ -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); @@ -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 @@ -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 @@ -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 @@ -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)); } @@ -748,8 +738,6 @@ 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 @@ -757,7 +745,7 @@ private void resetDiagramUIPreference(String preferenceKey, IPreferenceStore dia */ 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); @@ -769,8 +757,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 @@ -778,7 +764,7 @@ protected void changeSiriusPreference(String preferenceKey, Boolean newValue) { */ 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); @@ -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 @@ -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); } @@ -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); }