Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorXavier Coulon2016-01-13 09:28:29 +0000
committerXavier Coulon2016-01-19 19:49:57 +0000
commit71f1fb9f372667fabbf0b97237028482a49cf8e1 (patch)
treee2dcfbe41634e71e944213483d1852deee2d717a
parent014be453aac2ed06ac50c0e1126c76338c0f0e18 (diff)
downloadorg.eclipse.linuxtools-71f1fb9f372667fabbf0b97237028482a49cf8e1.tar.gz
org.eclipse.linuxtools-71f1fb9f372667fabbf0b97237028482a49cf8e1.tar.xz
org.eclipse.linuxtools-71f1fb9f372667fabbf0b97237028482a49cf8e1.zip
Bug 485350 - SWTException: Widget is disposed when DockerExplorer view is closed
Renoving listeners when Docker Explorer view is disposed Added SWTBot test to verify that the listeners were actually removed Removed code duplication in test utility classes. Change-Id: Iba939073614d22dd2368a8d13fa13ff0537d9fd1 Signed-off-by: Xavier Coulon <xcoulon@redhat.com> Reviewed-on: https://git.eclipse.org/r/64215 Tested-by: Hudson CI Tested-by: Roland Grunberg <rgrunber@redhat.com> Reviewed-by: Roland Grunberg <rgrunber@redhat.com> Reviewed-on: https://git.eclipse.org/r/64681
-rw-r--r--containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/DockerConnection.java29
-rw-r--r--containers/org.eclipse.linuxtools.docker.ui.tests/src/org/eclipse/linuxtools/internal/docker/ui/testutils/swt/ClearConnectionManagerRule.java19
-rw-r--r--containers/org.eclipse.linuxtools.docker.ui.tests/src/org/eclipse/linuxtools/internal/docker/ui/testutils/swt/DockerConnectionManagerUtils.java14
-rw-r--r--containers/org.eclipse.linuxtools.docker.ui.tests/src/org/eclipse/linuxtools/internal/docker/ui/views/DockerExplorerViewSWTBotTest.java24
-rw-r--r--containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/views/DockerExplorerView.java21
5 files changed, 88 insertions, 19 deletions
diff --git a/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/DockerConnection.java b/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/DockerConnection.java
index e03cdc02de..ed4a5ed51f 100644
--- a/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/DockerConnection.java
+++ b/containers/org.eclipse.linuxtools.docker.core/src/org/eclipse/linuxtools/internal/docker/core/DockerConnection.java
@@ -18,6 +18,7 @@ import java.nio.ByteBuffer;
import java.nio.channels.WritableByteChannel;
import java.nio.file.FileSystems;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
@@ -379,6 +380,20 @@ public class DockerConnection implements IDockerConnection, Closeable {
}
}
+ /**
+ * @return an fixed-size list of all {@link IDockerContainerListener}
+ */
+ // TODO: include in IDockerConnection API
+ public List<IDockerContainerListener> getContainerListeners() {
+ final IDockerContainerListener[] result = new IDockerContainerListener[this.containerListeners
+ .size()];
+ final Object[] listeners = containerListeners.getListeners();
+ for (int i = 0; i < listeners.length; i++) {
+ result[i] = (IDockerContainerListener) listeners[i];
+ }
+ return Arrays.asList(result);
+ }
+
public Job getActionJob(String id) {
synchronized (actionLock) {
Job j = null;
@@ -680,6 +695,20 @@ public class DockerConnection implements IDockerConnection, Closeable {
}
}
+ /**
+ * @return an fixed-size list of all {@link IDockerImageListener}
+ */
+ // TODO: include in IDockerConnection API
+ public List<IDockerImageListener> getImageListeners() {
+ final IDockerImageListener[] result = new IDockerImageListener[this.imageListeners
+ .size()];
+ final Object[] listeners = imageListeners.getListeners();
+ for (int i = 0; i < listeners.length; i++) {
+ result[i] = (IDockerImageListener) listeners[i];
+ }
+ return Arrays.asList(result);
+ }
+
@Override
public List<IDockerImage> getImages() {
return getImages(false);
diff --git a/containers/org.eclipse.linuxtools.docker.ui.tests/src/org/eclipse/linuxtools/internal/docker/ui/testutils/swt/ClearConnectionManagerRule.java b/containers/org.eclipse.linuxtools.docker.ui.tests/src/org/eclipse/linuxtools/internal/docker/ui/testutils/swt/ClearConnectionManagerRule.java
index b448362689..b2a0697245 100644
--- a/containers/org.eclipse.linuxtools.docker.ui.tests/src/org/eclipse/linuxtools/internal/docker/ui/testutils/swt/ClearConnectionManagerRule.java
+++ b/containers/org.eclipse.linuxtools.docker.ui.tests/src/org/eclipse/linuxtools/internal/docker/ui/testutils/swt/ClearConnectionManagerRule.java
@@ -15,9 +15,6 @@ import java.util.stream.Stream;
import org.eclipse.linuxtools.docker.core.DockerConnectionManager;
import org.eclipse.linuxtools.internal.docker.core.DefaultDockerConnectionSettingsFinder;
-import org.eclipse.linuxtools.internal.docker.ui.views.DockerExplorerView;
-import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
-import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
import org.junit.rules.ExternalResource;
/**
@@ -27,16 +24,10 @@ public class ClearConnectionManagerRule extends ExternalResource {
@Override
protected void after() {
- final SWTWorkbenchBot bot = new SWTWorkbenchBot();
- final SWTBotView dockerExplorerViewBot = bot.viewById("org.eclipse.linuxtools.docker.ui.dockerExplorerView");
- final DockerExplorerView dockerExplorerView = (DockerExplorerView) (dockerExplorerViewBot.getViewReference()
- .getView(true));
- SWTUtils.syncExec(() -> {
- Stream.of(DockerConnectionManager.getInstance().getConnections())
- .forEach(c -> DockerConnectionManager.getInstance().removeConnection(c));
- dockerExplorerView.getCommonViewer().refresh(true);
- dockerExplorerView.showConnectionsOrExplanations();
- });
+ Stream.of(DockerConnectionManager.getInstance().getConnections())
+ .forEach(c -> DockerConnectionManager.getInstance().removeConnection(c));
+ DockerConnectionManagerUtils.configureConnectionManager();
DockerConnectionManager.getInstance().setConnectionSettingsFinder(new DefaultDockerConnectionSettingsFinder());
}
-}
+
+} \ No newline at end of file
diff --git a/containers/org.eclipse.linuxtools.docker.ui.tests/src/org/eclipse/linuxtools/internal/docker/ui/testutils/swt/DockerConnectionManagerUtils.java b/containers/org.eclipse.linuxtools.docker.ui.tests/src/org/eclipse/linuxtools/internal/docker/ui/testutils/swt/DockerConnectionManagerUtils.java
index 954552515b..afd188d853 100644
--- a/containers/org.eclipse.linuxtools.docker.ui.tests/src/org/eclipse/linuxtools/internal/docker/ui/testutils/swt/DockerConnectionManagerUtils.java
+++ b/containers/org.eclipse.linuxtools.docker.ui.tests/src/org/eclipse/linuxtools/internal/docker/ui/testutils/swt/DockerConnectionManagerUtils.java
@@ -19,7 +19,6 @@ import org.eclipse.linuxtools.docker.core.IDockerConnectionStorageManager;
import org.eclipse.linuxtools.internal.docker.ui.testutils.MockDockerConnectionStorageManagerFactory;
import org.eclipse.linuxtools.internal.docker.ui.views.DockerExplorerView;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
-import org.eclipse.swtbot.eclipse.finder.widgets.SWTBotView;
/**
*
@@ -37,10 +36,8 @@ public class DockerConnectionManagerUtils {
final IDockerConnection... connections) {
DockerConnectionManager.getInstance()
.setConnectionStorageManager(MockDockerConnectionStorageManagerFactory.providing(connections));
- final SWTWorkbenchBot bot = new SWTWorkbenchBot();
- final SWTBotView dockerExplorerViewBot = bot.viewById("org.eclipse.linuxtools.docker.ui.dockerExplorerView");
- if(dockerExplorerViewBot != null) {
- final DockerExplorerView dockerExplorerView = (DockerExplorerView) (dockerExplorerViewBot.getViewReference().getView(true));
+ final DockerExplorerView dockerExplorerView = getDockerExplorerView(new SWTWorkbenchBot());
+ if(dockerExplorerView != null) {
SWTUtils.syncExec(() -> {
DockerConnectionManager.getInstance().reloadConnections();
dockerExplorerView.getCommonViewer().refresh();
@@ -49,4 +46,11 @@ public class DockerConnectionManagerUtils {
SWTUtils.wait(1, TimeUnit.SECONDS);
}
}
+
+ private static DockerExplorerView getDockerExplorerView(final SWTWorkbenchBot bot) {
+ return bot.views().stream().filter(v -> v.getReference().getId().equals(DockerExplorerView.VIEW_ID))
+ .map(viewBot -> (DockerExplorerView) (viewBot.getViewReference().getView(true))).findFirst()
+ .orElse(null);
+ }
+
}
diff --git a/containers/org.eclipse.linuxtools.docker.ui.tests/src/org/eclipse/linuxtools/internal/docker/ui/views/DockerExplorerViewSWTBotTest.java b/containers/org.eclipse.linuxtools.docker.ui.tests/src/org/eclipse/linuxtools/internal/docker/ui/views/DockerExplorerViewSWTBotTest.java
index ec26621b1f..14039b04dd 100644
--- a/containers/org.eclipse.linuxtools.docker.ui.tests/src/org/eclipse/linuxtools/internal/docker/ui/views/DockerExplorerViewSWTBotTest.java
+++ b/containers/org.eclipse.linuxtools.docker.ui.tests/src/org/eclipse/linuxtools/internal/docker/ui/views/DockerExplorerViewSWTBotTest.java
@@ -18,6 +18,7 @@ import java.util.concurrent.TimeUnit;
import org.assertj.core.api.Assertions;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.linuxtools.internal.docker.core.DockerConnection;
+import org.eclipse.linuxtools.internal.docker.core.DockerContainerRefreshManager;
import org.eclipse.linuxtools.internal.docker.ui.testutils.MockDockerClientFactory;
import org.eclipse.linuxtools.internal.docker.ui.testutils.MockDockerConnectionFactory;
import org.eclipse.linuxtools.internal.docker.ui.testutils.MockDockerContainerFactory;
@@ -560,4 +561,27 @@ public class DockerExplorerViewSWTBotTest {
// the properties view should be visible
assertThat(this.bot.viewById("org.eclipse.ui.views.PropertySheet").isActive()).isEqualTo(true);
}
+
+ @Test
+ public void shouldRemoveListenersWhenClosingView() {
+ // given
+ final DockerClient client = MockDockerClientFactory
+ .image(MockDockerImageFactory.name("angry_bar").build())
+ .container(MockDockerContainerFactory.name("angry_bar").status("Stopped").build()).build();
+ final DockerConnection dockerConnection = MockDockerConnectionFactory.from("Test", client).get();
+ DockerConnectionManagerUtils.configureConnectionManager(dockerConnection);
+ // remove the DockerContainerRefreshManager
+ dockerConnection.removeContainerListener(DockerContainerRefreshManager
+ .getInstance());
+ // DockerExplorerView inner classes
+ assertThat(dockerConnection.getContainerListeners()).hasSize(1);
+ assertThat(dockerConnection.getImageListeners()).hasSize(1);
+ // close the Docker Explorer View
+ dockerExplorerViewBot.close();
+ // there should be one listener left: DockerConnectionRefreshManager
+ assertThat(dockerConnection.getContainerListeners()).hasSize(0);
+ assertThat(dockerConnection.getImageListeners()).hasSize(0);
+
+
+ }
}
diff --git a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/views/DockerExplorerView.java b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/views/DockerExplorerView.java
index 9715383c94..540086eec2 100644
--- a/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/views/DockerExplorerView.java
+++ b/containers/org.eclipse.linuxtools.docker.ui/src/org/eclipse/linuxtools/internal/docker/ui/views/DockerExplorerView.java
@@ -12,8 +12,10 @@
package org.eclipse.linuxtools.internal.docker.ui.views;
import java.util.HashMap;
+import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import java.util.Map.Entry;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
@@ -106,8 +108,27 @@ public class DockerExplorerView extends CommonNavigator implements
@Override
public void dispose() {
+ // remove all ContainersRefresher instance registered on the Docker
+ // connections
+ for (Iterator<Entry<IDockerConnection, ContainersRefresher>> iterator = containersRefreshers
+ .entrySet().iterator(); iterator.hasNext();) {
+ final Entry<IDockerConnection, ContainersRefresher> entry = iterator
+ .next();
+ entry.getKey().removeContainerListener(entry.getValue());
+ iterator.remove();
+ }
+ // remove all ImagesRefresher instance registered on the Docker
+ // connections
+ for (Iterator<Entry<IDockerConnection, ImagesRefresher>> iterator = imagesRefreshers
+ .entrySet().iterator(); iterator.hasNext();) {
+ final Entry<IDockerConnection, ImagesRefresher> entry = iterator
+ .next();
+ entry.getKey().removeImageListener(entry.getValue());
+ iterator.remove();
+ }
DockerConnectionManager.getInstance().removeConnectionManagerListener(
this);
+
super.dispose();
}

Back to the top