|
@@ -0,0 +1,307 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (c) 2006-2014 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.ui.core.profiles;
|
|
24
|
+
|
|
25
|
+import com.dmdirc.actions.wrappers.Profile;
|
|
26
|
+import com.dmdirc.interfaces.config.ConfigProvider;
|
|
27
|
+import com.dmdirc.interfaces.config.IdentityController;
|
|
28
|
+import com.dmdirc.interfaces.config.IdentityFactory;
|
|
29
|
+import com.dmdirc.interfaces.ui.ProfilesDialogModelListener;
|
|
30
|
+
|
|
31
|
+import com.google.common.base.Optional;
|
|
32
|
+
|
|
33
|
+import java.util.List;
|
|
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 clover.com.google.common.collect.Lists;
|
|
42
|
+
|
|
43
|
+import static org.junit.Assert.assertEquals;
|
|
44
|
+import static org.junit.Assert.assertFalse;
|
|
45
|
+import static org.junit.Assert.assertTrue;
|
|
46
|
+import static org.mockito.Matchers.any;
|
|
47
|
+import static org.mockito.Matchers.anyString;
|
|
48
|
+import static org.mockito.Mockito.never;
|
|
49
|
+import static org.mockito.Mockito.verify;
|
|
50
|
+import static org.mockito.Mockito.when;
|
|
51
|
+
|
|
52
|
+@RunWith(MockitoJUnitRunner.class)
|
|
53
|
+public class CoreProfilesDialogModelTest {
|
|
54
|
+
|
|
55
|
+ @Mock private IdentityController identityController;
|
|
56
|
+ @Mock private ProfilesDialogModelListener listener;
|
|
57
|
+ @Mock private IdentityFactory identityFactory;
|
|
58
|
+ @Mock private ConfigProvider profileProvider1;
|
|
59
|
+ @Mock private ConfigProvider profileProvider2;
|
|
60
|
+ @Mock private ConfigProvider profileProvider3;
|
|
61
|
+ private List<ConfigProvider> profiles;
|
|
62
|
+
|
|
63
|
+ @Before
|
|
64
|
+ public void setupMocks() {
|
|
65
|
+ mockProfile("profile1", profileProvider1);
|
|
66
|
+ mockProfile("profile2", profileProvider2);
|
|
67
|
+ mockProfile("profile3", profileProvider3);
|
|
68
|
+ profiles = Lists.newArrayList(profileProvider1, profileProvider2, profileProvider3);
|
|
69
|
+ when(identityController.getProvidersByType(anyString()))
|
|
70
|
+ .thenReturn(profiles);
|
|
71
|
+ }
|
|
72
|
+
|
|
73
|
+ private void mockProfile(final String name, final ConfigProvider mock) {
|
|
74
|
+ when(mock.getName()).thenReturn(name);
|
|
75
|
+ when(mock.getOption("identity", "name")).thenReturn(name);
|
|
76
|
+ when(mock.getOptionList("profile", "nicknames"))
|
|
77
|
+ .thenReturn(Lists.newArrayList("nickname1", "nickname2", "nickname3"));
|
|
78
|
+ when(mock.getOption("profile", "realname")).thenReturn("realname");
|
|
79
|
+ when(mock.getOption("profile", "ident")).thenReturn("ident");
|
|
80
|
+ when(mock.toString()).thenReturn("Identity: " + name);
|
|
81
|
+ }
|
|
82
|
+
|
|
83
|
+ @Test
|
|
84
|
+ public void testGetProfileList() {
|
|
85
|
+ final CoreProfilesDialogModel instance = new CoreProfilesDialogModel(identityController,
|
|
86
|
+ identityFactory);
|
|
87
|
+ assertEquals("testGetProfileList", profiles.size(), instance.getProfileList().size());
|
|
88
|
+ }
|
|
89
|
+
|
|
90
|
+ @Test
|
|
91
|
+ public void testGetProfileNotExist() {
|
|
92
|
+ final CoreProfilesDialogModel instance = new CoreProfilesDialogModel(identityController,
|
|
93
|
+ identityFactory);
|
|
94
|
+ assertFalse("testGetProfileNotExist", instance.getProfile("").isPresent());
|
|
95
|
+ }
|
|
96
|
+
|
|
97
|
+ @Test
|
|
98
|
+ public void testGetProfileExist() {
|
|
99
|
+ final CoreProfilesDialogModel instance = new CoreProfilesDialogModel(identityController,
|
|
100
|
+ identityFactory);
|
|
101
|
+ assertTrue("testGetProfileExist", instance.getProfile("profile1").isPresent());
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+ @Test
|
|
105
|
+ public void testIsProfileListValidEmptyList() {
|
|
106
|
+ when(identityController.getProvidersByType(anyString()))
|
|
107
|
+ .thenReturn(Lists.<ConfigProvider>newArrayList());
|
|
108
|
+ final CoreProfilesDialogModel instance = new CoreProfilesDialogModel(identityController,
|
|
109
|
+ identityFactory);
|
|
110
|
+ assertFalse("testIsProfileListValidEmptyList", instance.isProfileListValid());
|
|
111
|
+ }
|
|
112
|
+
|
|
113
|
+ @Test
|
|
114
|
+ public void testIsProfileListValid() {
|
|
115
|
+ final CoreProfilesDialogModel instance = new CoreProfilesDialogModel(identityController,
|
|
116
|
+ identityFactory);
|
|
117
|
+ assertTrue("testIsProfileListValid", instance.isProfileListValid());
|
|
118
|
+ }
|
|
119
|
+
|
|
120
|
+ @Test
|
|
121
|
+ public void testAddProfile() {
|
|
122
|
+ when(identityController.getProvidersByType(anyString()))
|
|
123
|
+ .thenReturn(Lists.<ConfigProvider>newArrayList());
|
|
124
|
+ final CoreProfilesDialogModel instance = new CoreProfilesDialogModel(identityController,
|
|
125
|
+ identityFactory);
|
|
126
|
+ assertFalse("testAddProfile", instance.getProfile("profile4").isPresent());
|
|
127
|
+ instance.addProfile("profile4", "realname", "ident", Lists.newArrayList("nickname"));
|
|
128
|
+ assertTrue("testAddProfile", instance.getProfile("profile4").isPresent());
|
|
129
|
+ }
|
|
130
|
+
|
|
131
|
+ @Test
|
|
132
|
+ public void testEditProfile() {
|
|
133
|
+ final CoreProfilesDialogModel instance = new CoreProfilesDialogModel(identityController,
|
|
134
|
+ identityFactory);
|
|
135
|
+ final Profile preEdit = instance.getProfile("profile1").get();
|
|
136
|
+ assertEquals("testEditProfile", "profile1", preEdit.getName());
|
|
137
|
+ assertEquals("testEditProfile", "realname", preEdit.getRealname());
|
|
138
|
+ assertEquals("testEditProfile", "ident", preEdit.getIdent());
|
|
139
|
+ assertEquals("testEditProfile", Lists.newArrayList("nickname1", "nickname2", "nickname3"),
|
|
140
|
+ preEdit.getNicknames());
|
|
141
|
+ instance.editProfile("profile1", "newRealname", "newIdent", Lists.newArrayList("nickname"));
|
|
142
|
+ final Profile postEdit = instance.getProfile("profile1").get();
|
|
143
|
+ assertEquals("testEditProfile", "profile1", postEdit.getName());
|
|
144
|
+ assertEquals("testEditProfile", "newRealname", postEdit.getRealname());
|
|
145
|
+ assertEquals("testEditProfile", "newIdent", postEdit.getIdent());
|
|
146
|
+ assertEquals("testEditProfile", Lists.newArrayList("nickname"),
|
|
147
|
+ postEdit.getNicknames());
|
|
148
|
+ }
|
|
149
|
+
|
|
150
|
+ @Test
|
|
151
|
+ public void testRenameProfile() {
|
|
152
|
+ final CoreProfilesDialogModel instance = new CoreProfilesDialogModel(identityController,
|
|
153
|
+ identityFactory);
|
|
154
|
+ assertTrue("testRenameProfile", instance.getProfile("profile1").isPresent());
|
|
155
|
+ assertFalse("testRenameProfile", instance.getProfile("profile4").isPresent());
|
|
156
|
+ instance.renameProfile("profile1", "profile4");
|
|
157
|
+ assertFalse("testRenameProfile", instance.getProfile("profile1").isPresent());
|
|
158
|
+ assertTrue("testRenameProfile", instance.getProfile("profile4").isPresent());
|
|
159
|
+ }
|
|
160
|
+
|
|
161
|
+ @Test
|
|
162
|
+ public void testRemoveProfile() {
|
|
163
|
+ final CoreProfilesDialogModel instance = new CoreProfilesDialogModel(identityController,
|
|
164
|
+ identityFactory);
|
|
165
|
+ assertEquals("testRemoveProfile", 3, instance.getProfileList().size());
|
|
166
|
+ assertTrue("testRemoveProfile", instance.getProfile("profile3").isPresent());
|
|
167
|
+ instance.removeProfile("profile3");
|
|
168
|
+ assertEquals("testRemoveProfile", 2, instance.getProfileList().size());
|
|
169
|
+ assertFalse("testRemoveProfile", instance.getProfile("profile3").isPresent());
|
|
170
|
+ }
|
|
171
|
+
|
|
172
|
+ @Test
|
|
173
|
+ public void testSave() {
|
|
174
|
+
|
|
175
|
+ }
|
|
176
|
+
|
|
177
|
+ @Test
|
|
178
|
+ public void testSetSelectedProfile() {
|
|
179
|
+ }
|
|
180
|
+
|
|
181
|
+ @Test
|
|
182
|
+ public void testGetSelectedProfile() {
|
|
183
|
+ final CoreProfilesDialogModel instance = new CoreProfilesDialogModel(identityController,
|
|
184
|
+ identityFactory);
|
|
185
|
+ assertFalse("testGetSelectedProfile", instance.getSelectedProfile().isPresent());
|
|
186
|
+ instance.setSelectedProfile(instance.getProfile("profile2"));
|
|
187
|
+ assertEquals("testGetSelectedProfile", "profile2",
|
|
188
|
+ instance.getSelectedProfile().get().getName());
|
|
189
|
+ }
|
|
190
|
+
|
|
191
|
+ @Test
|
|
192
|
+ public void testGetSelectedProfileDetails() {
|
|
193
|
+ final CoreProfilesDialogModel instance = new CoreProfilesDialogModel(identityController,
|
|
194
|
+ identityFactory);
|
|
195
|
+ instance.setSelectedProfile(instance.getProfile("profile1"));
|
|
196
|
+ assertEquals("testGetSelectedProfileDetails", "profile1",
|
|
197
|
+ instance.getSelectedProfileName().get());
|
|
198
|
+ assertEquals("testGetSelectedProfileDetails", "ident",
|
|
199
|
+ instance.getSelectedProfileIdent().get());
|
|
200
|
+ assertEquals("testGetSelectedProfileDetails", "realname",
|
|
201
|
+ instance.getSelectedProfileRealname().get());
|
|
202
|
+ assertEquals("testGetSelectedProfileDetails",
|
|
203
|
+ Lists.newArrayList("nickname1", "nickname2", "nickname3"),
|
|
204
|
+ instance.getSelectedProfileNicknames().get());
|
|
205
|
+ }
|
|
206
|
+
|
|
207
|
+ @Test
|
|
208
|
+ public void testSetSelectedProfileSelectedNickname() {
|
|
209
|
+ final CoreProfilesDialogModel instance = new CoreProfilesDialogModel(identityController,
|
|
210
|
+ identityFactory);
|
|
211
|
+ instance.setSelectedProfile(instance.getProfile("profile1"));
|
|
212
|
+ assertEquals("testSetSelectedProfileSelectedNickname",
|
|
213
|
+ Optional.absent(), instance.getSelectedProfileSelectedNickname());
|
|
214
|
+ instance.setSelectedProfileSelectedNickname(Optional.fromNullable("nickname2"));
|
|
215
|
+ assertEquals("testSetSelectedProfileSelectedNickname",
|
|
216
|
+ Optional.fromNullable("nickname2"),
|
|
217
|
+ instance.getSelectedProfileSelectedNickname());
|
|
218
|
+ }
|
|
219
|
+
|
|
220
|
+ @Test
|
|
221
|
+ public void testSetSelectedProfileDetails() {
|
|
222
|
+ final CoreProfilesDialogModel instance = new CoreProfilesDialogModel(identityController,
|
|
223
|
+ identityFactory);
|
|
224
|
+ instance.setSelectedProfile(instance.getProfile("profile1"));
|
|
225
|
+ instance.setSelectedProfileName(Optional.fromNullable("testName"));
|
|
226
|
+ instance.setSelectedProfileIdent(Optional.fromNullable("testIdent"));
|
|
227
|
+ instance.setSelectedProfileRealname(Optional.fromNullable("testRealname"));
|
|
228
|
+ instance.setSelectedProfileNicknames(Optional.fromNullable((List<String>) Lists.
|
|
229
|
+ newArrayList("testNickname")));
|
|
230
|
+ assertEquals("testGetSelectedProfileDetails", "testName",
|
|
231
|
+ instance.getSelectedProfileName().get());
|
|
232
|
+ assertEquals("testGetSelectedProfileDetails", "testIdent",
|
|
233
|
+ instance.getSelectedProfileIdent().get());
|
|
234
|
+ assertEquals("testGetSelectedProfileDetails", "testRealname",
|
|
235
|
+ instance.getSelectedProfileRealname().get());
|
|
236
|
+ assertEquals("testGetSelectedProfileDetails",
|
|
237
|
+ Lists.newArrayList("testNickname"),
|
|
238
|
+ instance.getSelectedProfileNicknames().get());
|
|
239
|
+ }
|
|
240
|
+
|
|
241
|
+ @Test
|
|
242
|
+ public void testAddSelectedProfileNickname() {
|
|
243
|
+ final CoreProfilesDialogModel instance = new CoreProfilesDialogModel(identityController,
|
|
244
|
+ identityFactory);
|
|
245
|
+ instance.setSelectedProfile(instance.getProfile("profile1"));
|
|
246
|
+ assertEquals("testAddSelectedProfileNickname",
|
|
247
|
+ Lists.newArrayList("nickname1", "nickname2", "nickname3"),
|
|
248
|
+ instance.getSelectedProfileNicknames().get());
|
|
249
|
+ instance.addSelectedProfileNickname("nickname4");
|
|
250
|
+ assertEquals("testAddSelectedProfileNickname",
|
|
251
|
+ Lists.newArrayList("nickname1", "nickname2", "nickname3", "nickname4"),
|
|
252
|
+ instance.getSelectedProfileNicknames().get());
|
|
253
|
+ }
|
|
254
|
+
|
|
255
|
+ @Test
|
|
256
|
+ public void testRemoveSelectedProfileNickname() {
|
|
257
|
+ final CoreProfilesDialogModel instance = new CoreProfilesDialogModel(identityController,
|
|
258
|
+ identityFactory);
|
|
259
|
+ instance.setSelectedProfile(instance.getProfile("profile1"));
|
|
260
|
+ assertEquals("testRemoveSelectedProfileNickname",
|
|
261
|
+ Lists.newArrayList("nickname1", "nickname2", "nickname3"),
|
|
262
|
+ instance.getSelectedProfileNicknames().get());
|
|
263
|
+ instance.removeSelectedProfileNickname("nickname3");
|
|
264
|
+ assertEquals("testRemoveSelectedProfileNickname",
|
|
265
|
+ Lists.newArrayList("nickname1", "nickname2"),
|
|
266
|
+ instance.getSelectedProfileNicknames().get());
|
|
267
|
+ }
|
|
268
|
+
|
|
269
|
+ @Test
|
|
270
|
+ public void testEditSelectedProfileSelectedNickname() {
|
|
271
|
+ final CoreProfilesDialogModel instance = new CoreProfilesDialogModel(identityController,
|
|
272
|
+ identityFactory);
|
|
273
|
+ instance.setSelectedProfile(instance.getProfile("profile1"));
|
|
274
|
+ instance.setSelectedProfileSelectedNickname(Optional.fromNullable("nickname2"));
|
|
275
|
+ assertEquals("testSetSelectedProfileSelectedNickname",
|
|
276
|
+ Optional.fromNullable("nickname2"),
|
|
277
|
+ instance.getSelectedProfileSelectedNickname());
|
|
278
|
+ instance.editSelectedProfileNickname("nickname2", "nickname4");
|
|
279
|
+ assertEquals("testAddSelectedProfileNickname",
|
|
280
|
+ Lists.newArrayList("nickname1", "nickname4", "nickname3"),
|
|
281
|
+ instance.getSelectedProfileNicknames().get());
|
|
282
|
+ }
|
|
283
|
+
|
|
284
|
+ @Test
|
|
285
|
+ public void testAddListener() {
|
|
286
|
+ final CoreProfilesDialogModel instance = new CoreProfilesDialogModel(identityController,
|
|
287
|
+ identityFactory);
|
|
288
|
+ instance.addListener(listener);
|
|
289
|
+ instance.addProfile("profile4", "realname", "ident", Lists.newArrayList("nickname"));
|
|
290
|
+ verify(listener).profileAdded(any(Profile.class));
|
|
291
|
+ }
|
|
292
|
+
|
|
293
|
+ @Test
|
|
294
|
+ public void testRemoveListener() {
|
|
295
|
+ final CoreProfilesDialogModel instance = new CoreProfilesDialogModel(identityController,
|
|
296
|
+ identityFactory);
|
|
297
|
+ instance.addListener(listener);
|
|
298
|
+ instance.addProfile("profile4", "realname", "ident", Lists.newArrayList("nickname"));
|
|
299
|
+ assertTrue(instance.getProfile("profile4").isPresent());
|
|
300
|
+ final Profile profile4 = instance.getProfile("profile4").get();
|
|
301
|
+ verify(listener).profileAdded(profile4);
|
|
302
|
+ instance.removeListener(listener);
|
|
303
|
+ instance.setSelectedProfile(Optional.fromNullable(profile4));
|
|
304
|
+ verify(listener, never()).profileRemoved(profile4);
|
|
305
|
+ }
|
|
306
|
+
|
|
307
|
+}
|