From 54f8b95fc47b75991853891e2b9039921f411ded Mon Sep 17 00:00:00 2001 From: Darin Wright Date: Wed, 26 May 2010 14:36:27 +0000 Subject: Bug 257202 - [launching] Launch history no longer works properly --- .../debug/tests/launching/LaunchFavoriteTests.java | 259 +++++++++++++++++++++ .../org/eclipse/debug/tests/AutomatedSuite.java | 4 +- .../ui/launchConfigurations/LaunchHistory.java | 47 ++-- 3 files changed, 279 insertions(+), 31 deletions(-) create mode 100644 org.eclipse.debug.tests/src/org/eclipe/debug/tests/launching/LaunchFavoriteTests.java diff --git a/org.eclipse.debug.tests/src/org/eclipe/debug/tests/launching/LaunchFavoriteTests.java b/org.eclipse.debug.tests/src/org/eclipe/debug/tests/launching/LaunchFavoriteTests.java new file mode 100644 index 000000000..1e5a7827e --- /dev/null +++ b/org.eclipse.debug.tests/src/org/eclipe/debug/tests/launching/LaunchFavoriteTests.java @@ -0,0 +1,259 @@ +/******************************************************************************* + * Copyright (c) 2010 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipe.debug.tests.launching; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.debug.core.ILaunchConfiguration; +import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; +import org.eclipse.debug.internal.ui.launchConfigurations.LaunchHistory; +import org.eclipse.debug.ui.IDebugUIConstants; + +/** + * Test the launch history favorites get updated properly as configurations as + * modified. + * + * @since 3.6 + */ +public class LaunchFavoriteTests extends AbstractLaunchTest { + + /** + * Configuration to use for the test. One is created for each test during + * setup and deleted during tear down. + */ + private ILaunchConfiguration fConfig; + + /** + * Constructor + * @param name + */ + public LaunchFavoriteTests(String name) { + super(name); + } + + /** + * Returns the run launch history + * @return + */ + private LaunchHistory getRunLaunchHistory() { + return getLaunchConfigurationManager().getLaunchHistory(IDebugUIConstants.ID_RUN_LAUNCH_GROUP); + } + + /** + * Returns the debug launch history + * @return + */ + private LaunchHistory getDebugLaunchHistory() { + return getLaunchConfigurationManager().getLaunchHistory(IDebugUIConstants.ID_DEBUG_LAUNCH_GROUP); + } + + /* (non-Javadoc) + * @see junit.framework.TestCase#setUp() + */ + protected void setUp() throws Exception { + super.setUp(); + // clear the favorites + getRunLaunchHistory().setFavorites(new ILaunchConfiguration[0]); + getDebugLaunchHistory().setFavorites(new ILaunchConfiguration[0]); + fConfig = getLaunchConfiguration(getName()); + } + + /* (non-Javadoc) + * @see junit.framework.TestCase#tearDown() + */ + protected void tearDown() throws Exception { + super.tearDown(); + // delete the configuration used during this test + ILaunchConfiguration configuration = getLaunchConfiguration(); + if (configuration.exists()) { + configuration.delete(); + } + } + + /** + * Returns the new/initial launch configuration to use for this test. + * + * @return + */ + private ILaunchConfiguration getLaunchConfiguration() { + return fConfig; + } + + /** + * Makes the configuration a favorite in the specified group. + * + * @param config configuration to make a favorite - may be a working copy already + * @param groupId group to add it to + * @return working copy after making it a favorite + * + * @throws CoreException + */ + private ILaunchConfigurationWorkingCopy addFavorite(ILaunchConfiguration config, String groupId) throws CoreException { + ILaunchConfigurationWorkingCopy wc = getWorkingCopy(config); + List list = config.getAttribute(IDebugUIConstants.ATTR_FAVORITE_GROUPS, (List)null); + if (list == null) { + list = new ArrayList(); + } + list.add(groupId); + wc.setAttribute(IDebugUIConstants.ATTR_FAVORITE_GROUPS, list); + return wc; + } + + /** + * Removes the favorite group from the configuration's favorites attribute. Returns + * the resulting working copy. + * + * @param config configuration to remove favorite attribute from, may already be a working copy + * @param groupId group to remove + * @return working copy after removing favorite + * @throws CoreException + */ + private ILaunchConfigurationWorkingCopy removeFavorite(ILaunchConfiguration config, String groupId) throws CoreException { + ILaunchConfigurationWorkingCopy wc = getWorkingCopy(config); + List list = config.getAttribute(IDebugUIConstants.ATTR_FAVORITE_GROUPS, (List)null); + if (list != null) { + if (list.remove(groupId)) { + if (list.isEmpty()) { + list = null; + } + wc.setAttribute(IDebugUIConstants.ATTR_FAVORITE_GROUPS, list); + } + } + return wc; + } + + private ILaunchConfigurationWorkingCopy getWorkingCopy(ILaunchConfiguration config) throws CoreException { + ILaunchConfigurationWorkingCopy wc = null; + if (config.isWorkingCopy()) { + wc = (ILaunchConfigurationWorkingCopy) config; + } else { + wc = config.getWorkingCopy(); + } + return wc; + } + + /** + * Returns whether the launch history contains the given configuration as a favorite and is + * the expected size. + * + * @param history launch history + * @param configuration launch configuration + * @param size expected size of favorites or -1 if unknown + * @return whether the launch history contains the given configuration as a favorite and is + * the expected size + */ + private boolean containsFavorite(LaunchHistory history, ILaunchConfiguration configuration, int size) { + ILaunchConfiguration[] favorites = history.getFavorites(); + assertNotNull("No favorites", favorites); + if (size != -1) { + assertEquals("Favorites wrong size", size, favorites.length); + } + for (int i = 0; i < favorites.length; i++) { + if (configuration.equals(favorites[i])) { + return true; + } + } + return false; + } + + /** + * Tests that a configuration becoming a favorite appears in the favorites. + * + * @throws CoreException + */ + public void testBecomeFavorite() throws CoreException { + ILaunchConfigurationWorkingCopy wc = addFavorite(getLaunchConfiguration(), IDebugUIConstants.ID_DEBUG_LAUNCH_GROUP); + addFavorite(wc, IDebugUIConstants.ID_RUN_LAUNCH_GROUP); + ILaunchConfiguration saved = wc.doSave(); + assertTrue("Missing from debug favorites", containsFavorite(getDebugLaunchHistory(), saved, 1)); + assertTrue("Missing from run favorites", containsFavorite(getRunLaunchHistory(), saved, 1)); + } + + /** + * Tests that a when a favorite is no longer a favorite, it gets removed from the favorites. + * + * @throws CoreException + */ + public void testUnFavorite() throws CoreException { + testBecomeFavorite(); // create favorite in two histories + ILaunchConfigurationWorkingCopy wc = removeFavorite(getLaunchConfiguration(), IDebugUIConstants.ID_DEBUG_LAUNCH_GROUP); + removeFavorite(wc, IDebugUIConstants.ID_RUN_LAUNCH_GROUP); + ILaunchConfiguration saved = wc.doSave(); + assertFalse("Should not be a debug favorite", containsFavorite(getDebugLaunchHistory(), saved, 0)); + assertFalse("Should not be a run favorite", containsFavorite(getRunLaunchHistory(), saved, 0)); + } + + /** + * When a configuration is deleted, it should no longer be in the list. + * + * @throws CoreException + */ + public void testDeleteConfiguration() throws CoreException { + testBecomeFavorite(); // create a favorite in two histories + ILaunchConfiguration configuration = getLaunchConfiguration(); + configuration.delete(); + assertFalse("Should not be a debug favorite", containsFavorite(getDebugLaunchHistory(), configuration, 0)); + assertFalse("Should not be a run favorite", containsFavorite(getRunLaunchHistory(), configuration, 0)); + } + + /** + * When a favorite is renamed, it should still be in the list, with the new name. + * + * @throws CoreException + */ + public void testRenameFavorite() throws CoreException { + testBecomeFavorite(); + ILaunchConfiguration original = getLaunchConfiguration(); + ILaunchConfigurationWorkingCopy copy = original.getWorkingCopy(); + copy.rename("rename"+original.getName()); + ILaunchConfiguration saved = copy.doSave(); + assertTrue("Missing from debug favorites", containsFavorite(getDebugLaunchHistory(), saved, 1)); + assertTrue("Missing from run favorites", containsFavorite(getRunLaunchHistory(), saved, 1)); + saved.delete(); + } + + /** + * Renaming a configuration and making it a favorite at the same time - should appear in the list. + * + * @throws CoreException + */ + public void testRenameBecomeFavorite() throws CoreException { + ILaunchConfiguration original = getLaunchConfiguration(); + ILaunchConfigurationWorkingCopy copy = original.getWorkingCopy(); + copy.rename("rename"+original.getName()); + addFavorite(copy, IDebugUIConstants.ID_DEBUG_LAUNCH_GROUP); + addFavorite(copy, IDebugUIConstants.ID_RUN_LAUNCH_GROUP); + ILaunchConfiguration saved = copy.doSave(); + assertTrue("Missing from debug favorites", containsFavorite(getDebugLaunchHistory(), saved, 1)); + assertTrue("Missing from run favorites", containsFavorite(getRunLaunchHistory(), saved, 1)); + saved.delete(); + } + + /** + * Renaming a configuration and removing its favorite status should remove it from the list. + * + * @throws CoreException + */ + public void testRenameUnFavorite() throws CoreException { + testBecomeFavorite(); + ILaunchConfiguration original = getLaunchConfiguration(); + ILaunchConfigurationWorkingCopy copy = original.getWorkingCopy(); + copy.rename("rename"+original.getName()); + removeFavorite(copy, IDebugUIConstants.ID_DEBUG_LAUNCH_GROUP); + removeFavorite(copy, IDebugUIConstants.ID_RUN_LAUNCH_GROUP); + ILaunchConfiguration saved = copy.doSave(); + assertFalse("Should not be a debug favorite", containsFavorite(getDebugLaunchHistory(), saved, 0)); + assertFalse("Should not be a run favorite", containsFavorite(getRunLaunchHistory(), saved, 0)); + saved.delete(); + } +} diff --git a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/AutomatedSuite.java b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/AutomatedSuite.java index 9ae9b39f8..4d9b0b45b 100644 --- a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/AutomatedSuite.java +++ b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/AutomatedSuite.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009 IBM Corporation and others. + * Copyright (c) 2009, 2010 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -15,6 +15,7 @@ import junit.framework.TestSuite; import org.eclipe.debug.tests.launching.AcceleratorSubstitutionTests; import org.eclipe.debug.tests.launching.LaunchConfigurationTests; +import org.eclipe.debug.tests.launching.LaunchFavoriteTests; import org.eclipe.debug.tests.launching.LaunchHistoryTests; import org.eclipe.debug.tests.launching.LaunchManagerTests; import org.eclipe.debug.tests.launching.RefreshTabTests; @@ -84,6 +85,7 @@ public class AutomatedSuite extends TestSuite { addTest(new TestSuite(LaunchConfigurationTests.class)); addTest(new TestSuite(AcceleratorSubstitutionTests.class)); addTest(new TestSuite(LaunchHistoryTests.class)); + addTest(new TestSuite(LaunchFavoriteTests.class)); addTest(new TestSuite(LaunchManagerTests.class)); addTest(new TestSuite(RefreshTabTests.class)); diff --git a/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/launchConfigurations/LaunchHistory.java b/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/launchConfigurations/LaunchHistory.java index 4657cdc93..307b09126 100644 --- a/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/launchConfigurations/LaunchHistory.java +++ b/org.eclipse.debug.ui/ui/org/eclipse/debug/internal/ui/launchConfigurations/LaunchHistory.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2008 IBM Corporation and others. + * Copyright (c) 2000, 2010 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -344,26 +344,31 @@ public class LaunchHistory implements ILaunchListener, ILaunchConfigurationListe * @see org.eclipse.debug.core.ILaunchConfigurationListener#launchConfigurationAdded(org.eclipse.debug.core.ILaunchConfiguration) */ public void launchConfigurationAdded(ILaunchConfiguration configuration) { - checkFavorites(configuration); + ILaunchConfiguration movedFrom = DebugPlugin.getDefault().getLaunchManager().getMovedFrom(configuration); + // if this is a move, the launchConfigurationRemoved(...) method will handle updates + if (movedFrom == null) { + checkFavorites(configuration); + } } /** - * This method checks if the specified ILaunchConfiguration is a favorite of the launch group - * with the specified id + * This method checks if the specified ILaunchConfiguration is a favorite in this + * history's launch group. + * * @param configuration - * @param launchgroup - * @return true if the configuration is a favorite of the launch group with the given id, false otherwise + * @return true if the configuration is a favorite in this history's launch group, false otherwise * @throws CoreException * * @since 3.4 */ - protected boolean isFavorite(ILaunchConfiguration configuration, String launchgroup) throws CoreException { + protected boolean isFavorite(ILaunchConfiguration configuration) throws CoreException { + String groupId = getLaunchGroup().getIdentifier(); List favoriteGroups = configuration.getAttribute(IDebugUIConstants.ATTR_FAVORITE_GROUPS, (List)null); if (favoriteGroups == null) { // check deprecated attributes for backwards compatibility - if (launchgroup.equals(IDebugUIConstants.ID_DEBUG_LAUNCH_GROUP)) { + if (groupId.equals(IDebugUIConstants.ID_DEBUG_LAUNCH_GROUP)) { return configuration.getAttribute(IDebugUIConstants.ATTR_DEBUG_FAVORITE, false); - } else if (launchgroup.equals(IDebugUIConstants.ID_RUN_LAUNCH_GROUP)) { + } else if (groupId.equals(IDebugUIConstants.ID_RUN_LAUNCH_GROUP)) { return configuration.getAttribute(IDebugUIConstants.ATTR_RUN_FAVORITE, false); } } @@ -386,28 +391,10 @@ public class LaunchHistory implements ILaunchListener, ILaunchConfigurationListe return false; } try { - List favoriteGroups = configuration.getAttribute(IDebugUIConstants.ATTR_FAVORITE_GROUPS, (List)null); - if (favoriteGroups == null) { - // check deprecated attributes for backwards compatibility - String groupId = getLaunchGroup().getIdentifier(); - boolean fav = false; - if (groupId.equals(IDebugUIConstants.ID_DEBUG_LAUNCH_GROUP)) { - fav = configuration.getAttribute(IDebugUIConstants.ATTR_DEBUG_FAVORITE, false); - } else if (groupId.equals(IDebugUIConstants.ID_RUN_LAUNCH_GROUP)) { - fav = configuration.getAttribute(IDebugUIConstants.ATTR_RUN_FAVORITE, false); - } - if (fav) { - addFavorite(configuration); - return true; - } - removeFavorite(configuration); - return false; - } - else if (favoriteGroups.contains(getLaunchGroup().getIdentifier())) { + if (isFavorite(configuration)) { addFavorite(configuration); return true; - } - else { + } else { removeFavorite(configuration); return false; } @@ -441,7 +428,7 @@ public class LaunchHistory implements ILaunchListener, ILaunchConfigurationListe public synchronized void removeFromHistory(ILaunchConfiguration configuration) { try { boolean removed = fCompleteHistory.remove(configuration); - if(isFavorite(configuration, getLaunchGroup().getIdentifier())) { + if(isFavorite(configuration)) { removed |= fFavorites.remove(configuration); } if(removed) { -- cgit v1.2.3