瀏覽代碼

Merge pull request #400 from greboid/dev3

Implement a JPQ plugin.
pull/407/head
Chris Smith 9 年之前
父節點
當前提交
20151b170e

+ 6
- 2
jpq/plugin.config 查看文件

@@ -7,10 +7,11 @@ keysections:
7 7
   metadata
8 8
   updates
9 9
   version
10
+  defaults
10 11
 
11 12
 metadata:
12 13
   author=Greg <greg@dmdirc.com>
13
-  mainclass=com.dmdirc.addons.jpq.JPQPLugin
14
+  mainclass=com.dmdirc.addons.jpq.JPQPlugin
14 15
   description=Hides Joins, parts and quits from specified channels.
15 16
   name=jpq
16 17
   nicename=JPQ Hiding
@@ -19,4 +20,7 @@ updates:
19 20
   id=79
20 21
 
21 22
 version:
22
-  friendly=0.1
23
+  friendly=0.1
24
+
25
+defaults:
26
+  hidejpq=false

+ 102
- 0
jpq/src/com/dmdirc/addons/jpq/GroupChatHandler.java 查看文件

@@ -0,0 +1,102 @@
1
+/*
2
+ * Copyright (c) 2006-2015 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.addons.jpq;
24
+
25
+import com.dmdirc.config.ConfigBinder;
26
+import com.dmdirc.config.ConfigBinding;
27
+import com.dmdirc.events.ChannelJoinEvent;
28
+import com.dmdirc.events.ChannelPartEvent;
29
+import com.dmdirc.events.ChannelQuitEvent;
30
+import com.dmdirc.events.DisplayProperty;
31
+import com.dmdirc.events.DisplayableEvent;
32
+import com.dmdirc.interfaces.GroupChat;
33
+
34
+import com.google.common.annotations.VisibleForTesting;
35
+
36
+import net.engio.mbassy.listener.Handler;
37
+
38
+/**
39
+ * Handles {@link ChannelJoinEvent}, {@link ChannelPartEvent}, {@link ChannelQuitEvent} events and
40
+ * hides them if the required.
41
+ */
42
+public class GroupChatHandler {
43
+
44
+    private final GroupChat groupChat;
45
+    private final ConfigBinder binder;
46
+    private boolean hideEvents;
47
+
48
+    public GroupChatHandler(final String domain, final GroupChat groupChat) {
49
+        this.groupChat = groupChat;
50
+        binder = groupChat.getWindowModel().getConfigManager().getBinder()
51
+                .withDefaultDomain(domain);
52
+    }
53
+
54
+    /**
55
+     * Loads this handler, adds required listeners and bindings.
56
+     */
57
+    public void load() {
58
+        groupChat.getEventBus().subscribe(this);
59
+        binder.bind(this, GroupChatHandler.class);
60
+    }
61
+
62
+    /**
63
+     * Unloads this handler, removes required listeners and bindings.
64
+     */
65
+    public void unload() {
66
+        groupChat.getEventBus().unsubscribe(this);
67
+        binder.unbind(this);
68
+    }
69
+
70
+    @VisibleForTesting
71
+    @ConfigBinding(key = "hidejpq")
72
+    void handleSettingChange(final boolean value) {
73
+        hideEvents = value;
74
+    }
75
+
76
+    @SuppressWarnings("TypeMayBeWeakened")
77
+    @VisibleForTesting
78
+    @Handler
79
+    void handleJoin(final ChannelJoinEvent event) {
80
+        hideEvent(event);
81
+    }
82
+
83
+    @SuppressWarnings("TypeMayBeWeakened")
84
+    @VisibleForTesting
85
+    @Handler
86
+    void handlePart(final ChannelPartEvent event) {
87
+        hideEvent(event);
88
+    }
89
+
90
+    @SuppressWarnings("TypeMayBeWeakened")
91
+    @VisibleForTesting
92
+    @Handler
93
+    void handleQuit(final ChannelQuitEvent event) {
94
+        hideEvent(event);
95
+    }
96
+
97
+    private void hideEvent(final DisplayableEvent event) {
98
+        if (hideEvents) {
99
+            event.setDisplayProperty(DisplayProperty.DO_NOT_DISPLAY, true);
100
+        }
101
+    }
102
+}

+ 54
- 0
jpq/src/com/dmdirc/addons/jpq/GroupChatHandlerFactory.java 查看文件

@@ -0,0 +1,54 @@
1
+/*
2
+ * Copyright (c) 2006-2015 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.addons.jpq;
24
+
25
+import com.dmdirc.interfaces.GroupChat;
26
+import com.dmdirc.plugins.PluginDomain;
27
+
28
+import javax.inject.Inject;
29
+
30
+/**
31
+ * Factory for creating {@link GroupChatHandler}s.
32
+ */
33
+public class GroupChatHandlerFactory {
34
+
35
+    private final String domain;
36
+
37
+    @Inject
38
+    public GroupChatHandlerFactory(@PluginDomain(JPQPlugin.class) final String domain) {
39
+        this.domain = domain;
40
+    }
41
+
42
+    /**
43
+     * Creates a new instance for the specified {@link GroupChat}.
44
+     *
45
+     * @param groupChat Group chat to handle
46
+     *
47
+     * @return Loaded instance
48
+     */
49
+    public GroupChatHandler get(final GroupChat groupChat) {
50
+        final GroupChatHandler groupChatHandler = new GroupChatHandler(domain, groupChat);
51
+        groupChatHandler.load();
52
+        return groupChatHandler;
53
+    }
54
+}

+ 137
- 0
jpq/src/com/dmdirc/addons/jpq/JPQManager.java 查看文件

@@ -0,0 +1,137 @@
1
+/*
2
+ * Copyright (c) 2006-2015 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.addons.jpq;
24
+
25
+import com.dmdirc.DMDircMBassador;
26
+import com.dmdirc.config.prefs.PreferencesSetting;
27
+import com.dmdirc.config.prefs.PreferencesType;
28
+import com.dmdirc.events.ChannelSelfJoinEvent;
29
+import com.dmdirc.events.ChannelSelfPartEvent;
30
+import com.dmdirc.events.GroupChatPrefsRequestedEvent;
31
+import com.dmdirc.events.ServerConnectedEvent;
32
+import com.dmdirc.events.ServerDisconnectedEvent;
33
+import com.dmdirc.interfaces.Connection;
34
+import com.dmdirc.interfaces.ConnectionManager;
35
+import com.dmdirc.interfaces.GroupChat;
36
+import com.dmdirc.plugins.PluginDomain;
37
+
38
+import com.google.common.annotations.VisibleForTesting;
39
+
40
+import java.util.HashMap;
41
+import java.util.Map;
42
+
43
+import javax.inject.Inject;
44
+
45
+import net.engio.mbassy.listener.Handler;
46
+
47
+/**
48
+ * Provides the ability to hide joins, parts and quits from a {@link GroupChat}.
49
+ */
50
+public class JPQManager {
51
+
52
+    private final String domain;
53
+    private final ConnectionManager connectionManager;
54
+    private final GroupChatHandlerFactory groupChatHandlerFactory;
55
+    private final DMDircMBassador eventBus;
56
+    private final Map<GroupChat, GroupChatHandler> groupChatHandlers;
57
+
58
+    @Inject
59
+    public JPQManager(
60
+            @PluginDomain(JPQPlugin.class) final String domain,
61
+            final ConnectionManager connectionManager,
62
+            final GroupChatHandlerFactory groupChatHandlerFactory,
63
+            final DMDircMBassador eventBus) {
64
+        this.domain = domain;
65
+        this.connectionManager = connectionManager;
66
+        this.groupChatHandlerFactory = groupChatHandlerFactory;
67
+        this.eventBus = eventBus;
68
+        groupChatHandlers = new HashMap<>();
69
+    }
70
+
71
+    /**
72
+     * Unloads this manager, removing required listeners and bindings.
73
+     */
74
+    public void load() {
75
+        connectionManager.getConnections().forEach(this::addGroupChatHandler);
76
+        eventBus.subscribe(this);
77
+    }
78
+
79
+    /**
80
+     * Loads this manager, adding required listeners and bindings.
81
+     */
82
+    public void unload() {
83
+        connectionManager.getConnections().forEach(this::removeGroupChatHandler);
84
+        eventBus.unsubscribe(this);
85
+    }
86
+
87
+    @VisibleForTesting
88
+    @Handler
89
+    void handleConnectionAdded(final ServerConnectedEvent event) {
90
+        addGroupChatHandler(event.getConnection());
91
+    }
92
+
93
+    @VisibleForTesting
94
+    @Handler
95
+    void handleConnectionRemoved(final ServerDisconnectedEvent event) {
96
+        removeGroupChatHandler(event.getConnection());
97
+    }
98
+    
99
+    @VisibleForTesting
100
+    @Handler
101
+    void handleGroupChatAdded(final ChannelSelfJoinEvent event) {
102
+        addGroupChatHandler(event.getChannel());
103
+    }
104
+    
105
+    @VisibleForTesting
106
+    @Handler
107
+    void handleGroupChatRemoved(final ChannelSelfPartEvent event) {
108
+        removeGroupChatHandler(event.getChannel());
109
+    }
110
+
111
+    @VisibleForTesting
112
+    @Handler
113
+    void handleGroupChatPrefs(final GroupChatPrefsRequestedEvent event) {
114
+        event.getCategory().addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
115
+                domain, "hidejpq", "Hide J/P/Q", "Hides joins, parts and quits in a group chat.",
116
+                event.getConfig(), event.getIdentity()));
117
+    }
118
+
119
+    private void addGroupChatHandler(final Connection connection) {
120
+        connection.getGroupChatManager().getChannels().forEach(this::addGroupChatHandler);
121
+    }
122
+
123
+    private void addGroupChatHandler(final GroupChat groupChat) {
124
+        groupChatHandlers.computeIfAbsent(groupChat, groupChatHandlerFactory::get);
125
+    }
126
+
127
+    private void removeGroupChatHandler(final Connection connection) {
128
+        connection.getGroupChatManager().getChannels().forEach(this::removeGroupChatHandler);
129
+    }
130
+
131
+    private void removeGroupChatHandler(final GroupChat groupChat) {
132
+        final GroupChatHandler groupChatHandler = groupChatHandlers.remove(groupChat);
133
+        if (groupChatHandler != null) {
134
+            groupChatHandler.unload();
135
+        }
136
+    }
137
+}

+ 46
- 0
jpq/src/com/dmdirc/addons/jpq/JPQModule.java 查看文件

@@ -0,0 +1,46 @@
1
+/*
2
+ * Copyright (c) 2006-2015 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.addons.jpq;
24
+
25
+import com.dmdirc.ClientModule;
26
+import com.dmdirc.plugins.PluginDomain;
27
+import com.dmdirc.plugins.PluginInfo;
28
+
29
+import dagger.Module;
30
+import dagger.Provides;
31
+
32
+@Module(injects = {JPQManager.class, GroupChatHandlerFactory.class}, addsTo = ClientModule.class)
33
+public class JPQModule {
34
+
35
+    private final PluginInfo pluginInfo;
36
+
37
+    public JPQModule(final PluginInfo pluginInfo) {
38
+        this.pluginInfo = pluginInfo;
39
+    }
40
+
41
+    @Provides
42
+    @PluginDomain(JPQPlugin.class)
43
+    public String getDomain() {
44
+        return pluginInfo.getDomain();
45
+    }
46
+}

+ 24
- 1
jpq/src/com/dmdirc/addons/jpq/JPQPlugin.java 查看文件

@@ -22,10 +22,33 @@
22 22
 
23 23
 package com.dmdirc.addons.jpq;
24 24
 
25
+import com.dmdirc.interfaces.GroupChat;
26
+import com.dmdirc.plugins.PluginInfo;
25 27
 import com.dmdirc.plugins.implementations.BasePlugin;
26 28
 
29
+import dagger.ObjectGraph;
30
+
27 31
 /**
28
- *
32
+ * Provides the ability to hide joins, parts and quits from a {@link GroupChat}.
29 33
  */
30 34
 public class JPQPlugin extends BasePlugin {
35
+
36
+    private JPQManager manager;
37
+
38
+    @Override
39
+    public void load(final PluginInfo pluginInfo, final ObjectGraph graph) {
40
+        super.load(pluginInfo, graph);
41
+        setObjectGraph(graph.plus(new JPQModule(pluginInfo)));
42
+        manager = getObjectGraph().get(JPQManager.class);
43
+    }
44
+
45
+    @Override
46
+    public void onLoad() {
47
+        manager.load();
48
+    }
49
+
50
+    @Override
51
+    public void onUnload() {
52
+        manager.unload();
53
+    }
31 54
 }

+ 129
- 0
jpq/test/com/dmdirc/addons/jpq/GroupChatHandlerTest.java 查看文件

@@ -0,0 +1,129 @@
1
+/*
2
+ * Copyright (c) 2006-2015 DMDirc Developers
3
+ *
4
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
5
+ * of this software and associated documentation files (the "Software"), to deal
6
+ * in the Software without restriction, including without limitation the rights
7
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8
+ * copies of the Software, and to permit persons to whom the Software is
9
+ * furnished to do so, subject to the following conditions:
10
+ *
11
+ * The above copyright notice and this permission notice shall be included in
12
+ * all copies or substantial portions of the Software.
13
+ *
14
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20
+ * SOFTWARE.
21
+ */
22
+
23
+package com.dmdirc.addons.jpq;
24
+
25
+import com.dmdirc.DMDircMBassador;
26
+import com.dmdirc.config.ConfigBinder;
27
+import com.dmdirc.events.ChannelJoinEvent;
28
+import com.dmdirc.events.ChannelPartEvent;
29
+import com.dmdirc.events.ChannelQuitEvent;
30
+import com.dmdirc.events.DisplayProperty;
31
+import com.dmdirc.interfaces.GroupChat;
32
+import com.dmdirc.interfaces.WindowModel;
33
+import com.dmdirc.interfaces.config.AggregateConfigProvider;
34
+
35
+import org.junit.Before;
36
+import org.junit.Test;
37
+import org.junit.runner.RunWith;
38
+import org.mockito.Mock;
39
+import org.mockito.runners.MockitoJUnitRunner;
40
+
41
+import static org.mockito.Mockito.never;
42
+import static org.mockito.Mockito.verify;
43
+import static org.mockito.Mockito.when;
44
+
45
+@RunWith(MockitoJUnitRunner.class)
46
+public class GroupChatHandlerTest {
47
+
48
+    @Mock private GroupChat groupChat;
49
+    @Mock private WindowModel windowModel;
50
+    @Mock private AggregateConfigProvider configProvider;
51
+    @Mock private ConfigBinder configBinder;
52
+    @Mock private DMDircMBassador eventBus;
53
+    @Mock private ChannelJoinEvent channelJoinEvent;
54
+    @Mock private ChannelPartEvent channelPartEvent;
55
+    @Mock private ChannelQuitEvent channelQuitEvent;
56
+    private GroupChatHandler instance;
57
+
58
+    @Before
59
+    public void setUp() throws Exception {
60
+        when(groupChat.getEventBus()).thenReturn(eventBus);
61
+        when(groupChat.getWindowModel()).thenReturn(windowModel);
62
+        when(windowModel.getConfigManager()).thenReturn(configProvider);
63
+        when(configProvider.getBinder()).thenReturn(configBinder);
64
+        when(configBinder.withDefaultDomain("domain")).thenReturn(configBinder);
65
+        instance = new GroupChatHandler("domain", groupChat);
66
+    }
67
+
68
+    @Test
69
+    public void testLoad() throws Exception {
70
+        instance.load();
71
+        verify(configBinder).bind(instance, GroupChatHandler.class);
72
+        verify(eventBus).subscribe(instance);
73
+    }
74
+
75
+    @Test
76
+    public void testUnload() throws Exception {
77
+        instance.unload();
78
+        verify(configBinder).unbind(instance);
79
+        verify(eventBus).unsubscribe(instance);
80
+    }
81
+
82
+    @Test
83
+    public void testHandleJoin_On() throws Exception {
84
+        instance.load();
85
+        instance.handleSettingChange(true);
86
+        instance.handleJoin(channelJoinEvent);
87
+        verify(channelJoinEvent).setDisplayProperty(DisplayProperty.DO_NOT_DISPLAY, true);
88
+    }
89
+
90
+    @Test
91
+    public void testHandlePart_On() throws Exception {
92
+        instance.load();
93
+        instance.handleSettingChange(true);
94
+        instance.handlePart(channelPartEvent);
95
+        verify(channelPartEvent).setDisplayProperty(DisplayProperty.DO_NOT_DISPLAY, true);
96
+    }
97
+
98
+    @Test
99
+    public void testHandleQuit_On() throws Exception {
100
+        instance.load();
101
+        instance.handleSettingChange(true);
102
+        instance.handleQuit(channelQuitEvent);
103
+        verify(channelQuitEvent).setDisplayProperty(DisplayProperty.DO_NOT_DISPLAY, true);
104
+    }
105
+
106
+    @Test
107
+    public void testHandleJoin_Off() throws Exception {
108
+        instance.load();
109
+        instance.handleSettingChange(false);
110
+        instance.handleJoin(channelJoinEvent);
111
+        verify(channelJoinEvent, never()).setDisplayProperty(DisplayProperty.DO_NOT_DISPLAY, true);
112
+    }
113
+
114
+    @Test
115
+    public void testHandlePart_Off() throws Exception {
116
+        instance.load();
117
+        instance.handleSettingChange(false);
118
+        instance.handlePart(channelPartEvent);
119
+        verify(channelPartEvent, never()).setDisplayProperty(DisplayProperty.DO_NOT_DISPLAY, true);
120
+    }
121
+
122
+    @Test
123
+    public void testHandleQuit_Off() throws Exception {
124
+        instance.load();
125
+        instance.handleSettingChange(false);
126
+        instance.handleJoin(channelJoinEvent);
127
+        verify(channelJoinEvent, never()).setDisplayProperty(DisplayProperty.DO_NOT_DISPLAY, true);
128
+    }
129
+}

+ 143
- 0
jpq/test/com/dmdirc/addons/jpq/JPQManagerTest.java 查看文件

@@ -0,0 +1,143 @@
1
+package com.dmdirc.addons.jpq;
2
+
3
+import com.dmdirc.DMDircMBassador;
4
+import com.dmdirc.config.prefs.PreferencesCategory;
5
+import com.dmdirc.config.prefs.PreferencesSetting;
6
+import com.dmdirc.config.prefs.PreferencesType;
7
+import com.dmdirc.events.ChannelSelfJoinEvent;
8
+import com.dmdirc.events.ChannelSelfPartEvent;
9
+import com.dmdirc.events.GroupChatPrefsRequestedEvent;
10
+import com.dmdirc.events.ServerConnectedEvent;
11
+import com.dmdirc.events.ServerDisconnectedEvent;
12
+import com.dmdirc.interfaces.Connection;
13
+import com.dmdirc.interfaces.ConnectionManager;
14
+import com.dmdirc.interfaces.GroupChat;
15
+import com.dmdirc.interfaces.GroupChatManager;
16
+import com.dmdirc.interfaces.config.AggregateConfigProvider;
17
+
18
+import com.google.common.collect.Lists;
19
+
20
+import org.junit.Before;
21
+import org.junit.Test;
22
+import org.junit.runner.RunWith;
23
+import org.mockito.ArgumentCaptor;
24
+import org.mockito.Captor;
25
+import org.mockito.Mock;
26
+import org.mockito.runners.MockitoJUnitRunner;
27
+
28
+import static org.junit.Assert.assertEquals;
29
+import static org.mockito.Matchers.anyString;
30
+import static org.mockito.Mockito.verify;
31
+import static org.mockito.Mockito.when;
32
+
33
+@RunWith(MockitoJUnitRunner.class)
34
+public class JPQManagerTest {
35
+
36
+    @Mock private DMDircMBassador eventBus;
37
+    @Mock private ConnectionManager connectionManager;
38
+    @Mock private GroupChatHandlerFactory groupChatHandlerFactory;
39
+    @Mock private ServerConnectedEvent serverConnectedEvent;
40
+    @Mock private ServerDisconnectedEvent serverDisconnectedEvent;
41
+    @Mock private GroupChatPrefsRequestedEvent groupChatPrefsRequestedEvent;
42
+    @Mock private ChannelSelfJoinEvent channelSelfJoinEvent;
43
+    @Mock private ChannelSelfPartEvent channelSelfPartEvent;
44
+    @Mock private PreferencesCategory prefsCategory;
45
+    @Mock private AggregateConfigProvider configProvider;
46
+    @Captor private ArgumentCaptor<PreferencesSetting> preferencesSetting;
47
+    @Mock private Connection connection1;
48
+    @Mock private Connection connection2;
49
+    @Mock private GroupChat groupChat1;
50
+    @Mock private GroupChat groupChat2;
51
+    @Mock private GroupChat groupChat3;
52
+    @Mock private GroupChat groupChat4;
53
+    @Mock private GroupChatHandler groupChatHandler1;
54
+    @Mock private GroupChatHandler groupChatHandler2;
55
+    @Mock private GroupChatHandler groupChatHandler3;
56
+    @Mock private GroupChatHandler groupChatHandler4;
57
+    @Mock private GroupChatManager groupChatManager1;
58
+    @Mock private GroupChatManager groupChatManager2;
59
+    private JPQManager instance;
60
+
61
+    @Before
62
+    public void setUp() throws Exception {
63
+        when(groupChatHandlerFactory.get(groupChat1)).thenReturn(groupChatHandler1);
64
+        when(groupChatHandlerFactory.get(groupChat2)).thenReturn(groupChatHandler2);
65
+        when(groupChatHandlerFactory.get(groupChat3)).thenReturn(groupChatHandler3);
66
+        when(groupChatHandlerFactory.get(groupChat4)).thenReturn(groupChatHandler4);
67
+        when(serverConnectedEvent.getConnection()).thenReturn(connection1);
68
+        when(serverDisconnectedEvent.getConnection()).thenReturn(connection1);
69
+        when(connection1.getGroupChatManager()).thenReturn(groupChatManager1);
70
+        when(connection2.getGroupChatManager()).thenReturn(groupChatManager2);
71
+        when(groupChatManager1.getChannels())
72
+                .thenReturn(Lists.newArrayList(groupChat1, groupChat2));
73
+        when(groupChatManager2.getChannels())
74
+                .thenReturn(Lists.newArrayList(groupChat3, groupChat4));
75
+        when(connectionManager.getConnections())
76
+                .thenReturn(Lists.newArrayList(connection1, connection2));
77
+        when(groupChatPrefsRequestedEvent.getCategory()).thenReturn(prefsCategory);
78
+        when(groupChatPrefsRequestedEvent.getConfig()).thenReturn(configProvider);
79
+        when(channelSelfJoinEvent.getChannel()).thenReturn(groupChat3);
80
+        when(channelSelfPartEvent.getChannel()).thenReturn(groupChat3);
81
+        when(configProvider.getOption(anyString(), anyString())).thenReturn("true");
82
+        instance = new JPQManager("domain", connectionManager, groupChatHandlerFactory, eventBus);
83
+    }
84
+
85
+    @Test
86
+    public void testLoad() throws Exception {
87
+        instance.load();
88
+        verify(groupChatHandlerFactory).get(groupChat1);
89
+        verify(groupChatHandlerFactory).get(groupChat2);
90
+        verify(groupChatHandlerFactory).get(groupChat3);
91
+        verify(groupChatHandlerFactory).get(groupChat4);
92
+        verify(eventBus).subscribe(instance);
93
+    }
94
+
95
+    @Test
96
+    public void testUnload() throws Exception {
97
+        instance.load();
98
+        instance.unload();
99
+        verify(groupChatHandler1).unload();
100
+        verify(groupChatHandler2).unload();
101
+        verify(groupChatHandler3).unload();
102
+        verify(groupChatHandler4).unload();
103
+        verify(eventBus).unsubscribe(instance);
104
+    }
105
+
106
+    @Test
107
+    public void testHandlePrefsEvent() throws Exception {
108
+        instance.handleGroupChatPrefs(groupChatPrefsRequestedEvent);
109
+        verify(prefsCategory).addSetting(preferencesSetting.capture());
110
+        assertEquals(PreferencesType.BOOLEAN, preferencesSetting.getValue().getType());
111
+        assertEquals("true", preferencesSetting.getValue().getValue());
112
+    }
113
+
114
+    @Test
115
+    public void testHandleConnectionAdded() throws Exception {
116
+        instance.handleConnectionAdded(serverConnectedEvent);
117
+        verify(groupChatHandlerFactory).get(groupChat1);
118
+        verify(groupChatHandlerFactory).get(groupChat2);
119
+    }
120
+
121
+    @Test
122
+    public void testHandleConnectionRemoved() throws Exception {
123
+        instance.handleConnectionAdded(serverConnectedEvent);
124
+        instance.handleConnectionRemoved(serverDisconnectedEvent);
125
+        verify(groupChatHandler1).unload();
126
+        verify(groupChatHandler2).unload();
127
+    }
128
+
129
+    @Test
130
+    public void testGroupChatAdded() throws Exception {
131
+        instance.handleConnectionAdded(serverConnectedEvent);
132
+        instance.handleGroupChatAdded(channelSelfJoinEvent);
133
+        verify(groupChatHandlerFactory).get(groupChat3);
134
+    }
135
+
136
+    @Test
137
+    public void testGroupChatRemoved() throws Exception {
138
+        instance.handleGroupChatAdded(channelSelfJoinEvent);
139
+        verify(groupChatHandlerFactory).get(groupChat3);
140
+        instance.handleGroupChatRemoved(channelSelfPartEvent);
141
+        verify(groupChatHandler3).unload();
142
+    }
143
+}

+ 34
- 1
jpq/test/com/dmdirc/addons/jpq/JPQPluginTest.java 查看文件

@@ -22,14 +22,47 @@
22 22
 
23 23
 package com.dmdirc.addons.jpq;
24 24
 
25
+import com.dmdirc.plugins.PluginInfo;
26
+
27
+import org.junit.Before;
25 28
 import org.junit.Test;
26 29
 import org.junit.runner.RunWith;
30
+import org.mockito.Mock;
27 31
 import org.mockito.runners.MockitoJUnitRunner;
28 32
 
33
+import dagger.ObjectGraph;
34
+
35
+import static org.mockito.Matchers.any;
36
+import static org.mockito.Mockito.verify;
37
+import static org.mockito.Mockito.when;
38
+
29 39
 @RunWith(MockitoJUnitRunner.class)
30 40
 public class JPQPluginTest {
31 41
 
42
+    @Mock private ObjectGraph objectGraph;
43
+    @Mock private PluginInfo pluginInfo;
44
+    @Mock private JPQManager manager;
45
+    private JPQPlugin instance;
46
+
47
+    @Before
48
+    public void setup() {
49
+        when(objectGraph.plus(any(JPQModule.class))).thenReturn(objectGraph);
50
+        when(objectGraph.get(JPQManager.class)).thenReturn(manager);
51
+        instance = new JPQPlugin();
52
+    }
53
+
54
+    @Test
55
+    public void testLoad() {
56
+        instance.load(pluginInfo, objectGraph);
57
+        instance.onLoad();
58
+        verify(manager).load();
59
+    }
60
+
32 61
     @Test
33
-    public void testSomething() {}
62
+    public void testUnload() {
63
+        instance.load(pluginInfo, objectGraph);
64
+        instance.onUnload();
65
+        verify(manager).unload();
66
+    }
34 67
 
35 68
 }

Loading…
取消
儲存