|
@@ -0,0 +1,135 @@
|
|
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.whoisonquery;
|
|
24
|
+
|
|
25
|
+import com.dmdirc.DMDircMBassador;
|
|
26
|
+import com.dmdirc.Query;
|
|
27
|
+import com.dmdirc.config.prefs.PreferencesCategory;
|
|
28
|
+import com.dmdirc.config.prefs.PreferencesDialogModel;
|
|
29
|
+import com.dmdirc.config.prefs.PreferencesSetting;
|
|
30
|
+import com.dmdirc.events.ClientPrefsOpenedEvent;
|
|
31
|
+import com.dmdirc.events.ConnectionPrefsRequestedEvent;
|
|
32
|
+import com.dmdirc.events.QueryOpenedEvent;
|
|
33
|
+import com.dmdirc.interfaces.Connection;
|
|
34
|
+import com.dmdirc.interfaces.User;
|
|
35
|
+import com.dmdirc.interfaces.WindowModel;
|
|
36
|
+import com.dmdirc.interfaces.config.AggregateConfigProvider;
|
|
37
|
+import com.dmdirc.interfaces.config.ConfigProvider;
|
|
38
|
+import com.dmdirc.plugins.PluginInfo;
|
|
39
|
+import com.dmdirc.plugins.PluginMetaData;
|
|
40
|
+
|
|
41
|
+import java.util.Optional;
|
|
42
|
+
|
|
43
|
+import org.junit.Before;
|
|
44
|
+import org.junit.Test;
|
|
45
|
+import org.junit.runner.RunWith;
|
|
46
|
+import org.mockito.ArgumentCaptor;
|
|
47
|
+import org.mockito.Captor;
|
|
48
|
+import org.mockito.Mock;
|
|
49
|
+import org.mockito.runners.MockitoJUnitRunner;
|
|
50
|
+
|
|
51
|
+import static org.mockito.Matchers.any;
|
|
52
|
+import static org.mockito.Mockito.never;
|
|
53
|
+import static org.mockito.Mockito.verify;
|
|
54
|
+import static org.mockito.Mockito.when;
|
|
55
|
+
|
|
56
|
+@RunWith(MockitoJUnitRunner.class)
|
|
57
|
+public class WhoisOnQueryManagerTest {
|
|
58
|
+
|
|
59
|
+ @Mock private DMDircMBassador eventBus;
|
|
60
|
+ @Mock private PluginInfo pluginInfo;
|
|
61
|
+ @Mock private PluginMetaData pluginMetaData;
|
|
62
|
+ @Mock private QueryOpenedEvent queryOpenedEvent;
|
|
63
|
+ @Mock private AggregateConfigProvider aggregateConfigProvider;
|
|
64
|
+ @Mock private Connection connection;
|
|
65
|
+ @Mock private WindowModel windowModel;
|
|
66
|
+ @Mock private Query query;
|
|
67
|
+ @Mock private User user;
|
|
68
|
+ @Mock private ClientPrefsOpenedEvent clientPrefsOpenedEvent;
|
|
69
|
+ @Mock private ConnectionPrefsRequestedEvent connectionPrefsRequestedEvent;
|
|
70
|
+ @Mock private PreferencesCategory preferencesCategory;
|
|
71
|
+ @Captor private ArgumentCaptor<PreferencesSetting> preferencesSetting;
|
|
72
|
+ @Mock private ConfigProvider configProvider;
|
|
73
|
+ @Mock private PreferencesDialogModel preferencesDialogModel;
|
|
74
|
+ @Captor private ArgumentCaptor<PreferencesCategory> preferencesCategoryArgumentCaptor;
|
|
75
|
+ private WhoisOnQueryManager instance;
|
|
76
|
+
|
|
77
|
+ @Before
|
|
78
|
+ public void setUp() throws Exception {
|
|
79
|
+ when(pluginInfo.getMetaData()).thenReturn(pluginMetaData);
|
|
80
|
+ when(pluginMetaData.getFriendlyName()).thenReturn("Plugin");
|
|
81
|
+ when(queryOpenedEvent.getQuery()).thenReturn(query);
|
|
82
|
+ when(query.getUser()).thenReturn(user);
|
|
83
|
+ when(query.getConnection()).thenReturn(Optional.of(connection));
|
|
84
|
+ when(connection.getWindowModel()).thenReturn(windowModel);
|
|
85
|
+ when(windowModel.getConfigManager()).thenReturn(aggregateConfigProvider);
|
|
86
|
+ when(connectionPrefsRequestedEvent.getConfig()).thenReturn(aggregateConfigProvider);
|
|
87
|
+ when(connectionPrefsRequestedEvent.getIdentity()).thenReturn(configProvider);
|
|
88
|
+ when(connectionPrefsRequestedEvent.getCategory()).thenReturn(preferencesCategory);
|
|
89
|
+ when(clientPrefsOpenedEvent.getModel()).thenReturn(preferencesDialogModel);
|
|
90
|
+ when(preferencesDialogModel.getIdentity()).thenReturn(configProvider);
|
|
91
|
+ when(preferencesDialogModel.getConfigManager()).thenReturn(aggregateConfigProvider);
|
|
92
|
+ when(preferencesDialogModel.getCategory("Plugins")).thenReturn(preferencesCategory);
|
|
93
|
+ instance = new WhoisOnQueryManager("domain", pluginInfo, eventBus);
|
|
94
|
+ }
|
|
95
|
+
|
|
96
|
+ @Test
|
|
97
|
+ public void testLoad() throws Exception {
|
|
98
|
+ instance.load();
|
|
99
|
+ verify(eventBus).subscribe(instance);
|
|
100
|
+ }
|
|
101
|
+
|
|
102
|
+ @Test
|
|
103
|
+ public void testUnload() throws Exception {
|
|
104
|
+ instance.unload();
|
|
105
|
+ verify(eventBus).unsubscribe(instance);
|
|
106
|
+ }
|
|
107
|
+
|
|
108
|
+ @Test
|
|
109
|
+ public void testHandleQueryEvent_Set() throws Exception {
|
|
110
|
+ when(aggregateConfigProvider.getOptionBool("domain", "whoisonquery")).thenReturn(true);
|
|
111
|
+ instance.handleQueryEvent(queryOpenedEvent);
|
|
112
|
+ verify(connection).requestUserInfo(user);
|
|
113
|
+ }
|
|
114
|
+
|
|
115
|
+ @Test
|
|
116
|
+ public void testHandleQueryEvent_Unset() throws Exception {
|
|
117
|
+ when(aggregateConfigProvider.getOptionBool("domain", "whoisonquery")).thenReturn(false);
|
|
118
|
+ instance.handleQueryEvent(queryOpenedEvent);
|
|
119
|
+ verify(connection, never()).requestUserInfo(user);
|
|
120
|
+ }
|
|
121
|
+
|
|
122
|
+ @Test
|
|
123
|
+ public void testHandlePrefsEvent() throws Exception {
|
|
124
|
+ when(aggregateConfigProvider.getOptionBool("domain", "whoisonquery")).thenReturn(true);
|
|
125
|
+ instance.handlePrefsEvent(clientPrefsOpenedEvent);
|
|
126
|
+ verify(preferencesCategory).addSubCategory(preferencesCategoryArgumentCaptor.capture());
|
|
127
|
+ }
|
|
128
|
+
|
|
129
|
+ @Test
|
|
130
|
+ public void testHandleConnectionPrefsEvent() throws Exception {
|
|
131
|
+ when(aggregateConfigProvider.getOptionBool("domain", "whoisonquery")).thenReturn(true);
|
|
132
|
+ instance.handleConnectionPrefsEvent(connectionPrefsRequestedEvent);
|
|
133
|
+ verify(preferencesCategory).addSetting(any());
|
|
134
|
+ }
|
|
135
|
+}
|