您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

CoreProfilesDialogModelTest.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. package com.dmdirc.ui.core.profiles;
  23. import com.dmdirc.config.profiles.Profile;
  24. import com.dmdirc.config.profiles.ProfileManager;
  25. import com.dmdirc.interfaces.ui.ProfilesDialogModelListener;
  26. import com.google.common.collect.Lists;
  27. import java.util.Collections;
  28. import java.util.List;
  29. import java.util.Optional;
  30. import org.junit.Before;
  31. import org.junit.Test;
  32. import org.junit.runner.RunWith;
  33. import org.mockito.Mock;
  34. import org.mockito.runners.MockitoJUnitRunner;
  35. import static java.util.Comparator.comparing;
  36. import static org.junit.Assert.assertEquals;
  37. import static org.junit.Assert.assertFalse;
  38. import static org.junit.Assert.assertTrue;
  39. import static org.mockito.Matchers.any;
  40. import static org.mockito.Mockito.never;
  41. import static org.mockito.Mockito.times;
  42. import static org.mockito.Mockito.verify;
  43. import static org.mockito.Mockito.when;
  44. @RunWith(MockitoJUnitRunner.class)
  45. public class CoreProfilesDialogModelTest {
  46. @Mock private ProfilesDialogModelListener listener;
  47. @Mock private ProfileManager profileManager;
  48. private MutableProfile mutableProfile1;
  49. private MutableProfile mutableProfile2;
  50. private MutableProfile mutableProfile3;
  51. private MutableProfile mutableProfile4;
  52. private List<MutableProfile> mutableProfiles;
  53. private CoreProfilesDialogModel instance;
  54. @Before
  55. public void setupMocks() {
  56. final Profile profile1 = getProfile(1);
  57. final Profile profile2 = getProfile(2);
  58. final Profile profile3 = getProfile(3);
  59. final Profile profile4 = getProfile(4);
  60. final List<Profile> profiles = Lists.newArrayList(profile3, profile1, profile2);
  61. Collections.sort(profiles, comparing(Profile::getName));
  62. mutableProfile1 = new MutableProfile(profile1);
  63. mutableProfile2 = new MutableProfile(profile2);
  64. mutableProfile3 = new MutableProfile(profile3);
  65. mutableProfile4 = new MutableProfile(profile4);
  66. mutableProfiles = Lists.newArrayList(mutableProfile1, mutableProfile2, mutableProfile3);
  67. Collections.sort(mutableProfiles, comparing(MutableProfile::getName));
  68. when(profileManager.getProfiles()).thenReturn(profiles);
  69. instance = new CoreProfilesDialogModel(profileManager);
  70. instance.loadModel();
  71. instance.addListener(listener);
  72. }
  73. private Profile getProfile(final int name) {
  74. return Profile.create("profile" + name, "realname" + name, Optional.of("ident" + name),
  75. Lists.newArrayList("nickname" + name + '1', "nickname" + name + '2',
  76. "nickname" + name + '3'));
  77. }
  78. @Test
  79. public void testGetProfileList() {
  80. assertEquals(mutableProfiles, instance.getProfileList());
  81. }
  82. @Test
  83. public void testGetProfileNotExist() {
  84. assertFalse("testGetProfileNotExist", instance.getProfile("").isPresent());
  85. }
  86. @Test
  87. public void testGetProfileExist() {
  88. assertTrue("testGetProfileExist", instance.getProfile("profile1").isPresent());
  89. }
  90. @Test
  91. public void testIsProfileListValidEmptyList() {
  92. when(profileManager.getProfiles()).thenReturn(Lists.newArrayList());
  93. instance.loadModel();
  94. assertFalse("testIsProfileListValidEmptyList", instance.isProfileListValid());
  95. }
  96. @Test
  97. public void testIsProfileListValid() {
  98. assertTrue("testIsProfileListValid", instance.isProfileListValid());
  99. }
  100. @Test
  101. public void testAddProfile() {
  102. when(profileManager.getProfiles()).thenReturn(Lists.newArrayList());
  103. instance.loadModel();
  104. verify(listener).profileSelectionChanged(Optional.empty());
  105. assertTrue("testAddProfile", !instance.getSelectedProfile().isPresent());
  106. assertFalse("testAddProfile", instance.getProfile("profile4").isPresent());
  107. instance.addProfile(mutableProfile1.getName(), mutableProfile1.getRealname(),
  108. mutableProfile1.getIdent().get(), mutableProfile1.getNicknames());
  109. assertTrue("testAddProfile", instance.getProfile(mutableProfile1.getName()).isPresent());
  110. verify(listener).profileAdded(mutableProfile1);
  111. verify(listener).profileSelectionChanged(Optional.of(mutableProfile1));
  112. }
  113. @Test
  114. public void testEditProfile() {
  115. final MutableProfile preEdit = instance.getProfile("profile1").get();
  116. assertEquals("testEditProfile", "profile1", mutableProfile1.getName());
  117. assertEquals("testEditProfile", "realname1", mutableProfile1.getRealname());
  118. assertEquals("testEditProfile", "ident1", mutableProfile1.getIdent().get());
  119. assertEquals("testEditProfile",
  120. Lists.newArrayList("nickname11", "nickname12", "nickname13"),
  121. mutableProfile1.getNicknames());
  122. instance.editProfile(preEdit, mutableProfile4.getName(), mutableProfile4.getRealname(),
  123. mutableProfile4.getIdent().get(), mutableProfile4.getNicknames());
  124. verify(listener).profileEdited(mutableProfile4);
  125. assertEquals("testEditProfile", "profile4", mutableProfile4.getName());
  126. assertEquals("testEditProfile", "realname4", mutableProfile4.getRealname());
  127. assertEquals("testEditProfile", "ident4", mutableProfile4.getIdent().get());
  128. assertEquals("testEditProfile",
  129. Lists.newArrayList("nickname41", "nickname42", "nickname43"),
  130. mutableProfile4.getNicknames());
  131. }
  132. @Test
  133. public void testRenameProfile() {
  134. assertTrue("testRenameProfile", instance.getProfile("profile1").isPresent());
  135. assertFalse("testRenameProfile", instance.getProfile("profile4").isPresent());
  136. final MutableProfile preEdit = instance.getProfile("profile1").get();
  137. instance.editProfile(preEdit, "profile4", preEdit.getRealname(),
  138. preEdit.getIdent().get(), preEdit.getNicknames());
  139. assertFalse("testRenameProfile", instance.getProfile("profile1").isPresent());
  140. assertTrue("testRenameProfile", instance.getProfile("profile4").isPresent());
  141. }
  142. @Test
  143. public void testRemoveProfile() {
  144. assertEquals("testRemoveProfile", 3, instance.getProfileList().size());
  145. assertTrue("testRemoveProfile", instance.getProfile("profile3").isPresent());
  146. instance.removeProfile("profile3");
  147. verify(listener).profileRemoved(mutableProfile3);
  148. assertEquals("testRemoveProfile", 2, instance.getProfileList().size());
  149. assertFalse("testRemoveProfile", instance.getProfile("profile3").isPresent());
  150. }
  151. @Test
  152. public void testGetSelectedProfile() {
  153. assertTrue("testGetSelectedProfile", instance.getSelectedProfile().isPresent());
  154. instance.setSelectedProfile(instance.getProfile("profile2"));
  155. verify(listener).profileSelectionChanged(Optional.of(mutableProfile2));
  156. assertEquals("testGetSelectedProfile", "profile2",
  157. instance.getSelectedProfile().get().getName());
  158. }
  159. @Test
  160. public void testGetSelectedProfileDetails() {
  161. instance.setSelectedProfile(instance.getProfile("profile1"));
  162. verify(listener, times(0)).profileSelectionChanged(Optional.of(mutableProfile1));
  163. assertEquals("testGetSelectedProfileDetails", Optional.of(mutableProfile1),
  164. instance.getSelectedProfile());
  165. assertEquals("testGetSelectedProfileDetails", "profile1",
  166. instance.getSelectedProfileName().get());
  167. assertEquals("testGetSelectedProfileDetails", "ident1",
  168. instance.getSelectedProfileIdent().get());
  169. assertEquals("testGetSelectedProfileDetails", "realname1",
  170. instance.getSelectedProfileRealname().get());
  171. assertEquals("testGetSelectedProfileDetails",
  172. Lists.newArrayList("nickname11", "nickname12", "nickname13"),
  173. instance.getSelectedProfileNicknames().get());
  174. }
  175. @Test
  176. public void testSetSelectedProfileSelectedNickname() {
  177. instance.setSelectedProfile(instance.getProfile("profile1"));
  178. assertEquals("testSetSelectedProfileSelectedNickname", Optional.<String>empty(),
  179. instance.getSelectedProfileSelectedNickname());
  180. instance.setSelectedProfileSelectedNickname(Optional.ofNullable("nickname12"));
  181. verify(listener).selectedNicknameChanged(Optional.of("nickname12"));
  182. assertEquals("testSetSelectedProfileSelectedNickname", Optional.ofNullable("nickname12"),
  183. instance.getSelectedProfileSelectedNickname());
  184. }
  185. @Test
  186. public void testSetSelectedProfileDetails() {
  187. instance.setSelectedProfile(instance.getProfile("profile2"));
  188. verify(listener).profileSelectionChanged(Optional.of(mutableProfile2));
  189. instance.setSelectedProfileName(Optional.ofNullable("testName"));
  190. instance.setSelectedProfileIdent(Optional.ofNullable("testIdent"));
  191. instance.setSelectedProfileRealname(Optional.ofNullable("testRealname"));
  192. instance.setSelectedProfileNicknames(Optional.ofNullable((List<String>) Lists.
  193. newArrayList("testNickname")));
  194. assertEquals("testGetSelectedProfileDetails", "testName",
  195. instance.getSelectedProfileName().get());
  196. assertEquals("testGetSelectedProfileDetails", "testIdent",
  197. instance.getSelectedProfileIdent().get());
  198. assertEquals("testGetSelectedProfileDetails", "testRealname",
  199. instance.getSelectedProfileRealname().get());
  200. assertEquals("testGetSelectedProfileDetails",
  201. Lists.newArrayList("testNickname"),
  202. instance.getSelectedProfileNicknames().get());
  203. }
  204. @Test
  205. public void testAddSelectedProfileNickname() {
  206. instance.setSelectedProfile(instance.getProfile("profile1"));
  207. assertEquals("testAddSelectedProfileNickname",
  208. Lists.newArrayList("nickname11", "nickname12", "nickname13"),
  209. instance.getSelectedProfileNicknames().get());
  210. instance.addSelectedProfileNickname("nickname4");
  211. verify(listener).selectedProfileNicknameAdded("nickname4");
  212. assertEquals("testAddSelectedProfileNickname",
  213. Lists.newArrayList("nickname11", "nickname12", "nickname13", "nickname4"),
  214. instance.getSelectedProfileNicknames().get());
  215. }
  216. @Test
  217. public void testRemoveSelectedProfileNickname() {
  218. instance.setSelectedProfile(instance.getProfile("profile1"));
  219. assertEquals("testRemoveSelectedProfileNickname",
  220. Lists.newArrayList("nickname11", "nickname12", "nickname13"),
  221. instance.getSelectedProfileNicknames().get());
  222. instance.removeSelectedProfileNickname("nickname13");
  223. verify(listener).selectedProfileNicknameRemoved("nickname13");
  224. assertEquals("testRemoveSelectedProfileNickname",
  225. Lists.newArrayList("nickname11", "nickname12"),
  226. instance.getSelectedProfileNicknames().get());
  227. }
  228. @Test
  229. public void testEditSelectedProfileSelectedNickname() {
  230. instance.setSelectedProfile(instance.getProfile("profile1"));
  231. instance.setSelectedProfileSelectedNickname(Optional.ofNullable("nickname12"));
  232. assertEquals("testSetSelectedProfileSelectedNickname",
  233. Optional.ofNullable("nickname12"),
  234. instance.getSelectedProfileSelectedNickname());
  235. instance.editSelectedProfileNickname("nickname12", "nickname4");
  236. verify(listener).selectedProfileNicknameEdited("nickname12", "nickname4");
  237. assertEquals("testAddSelectedProfileNickname",
  238. Lists.newArrayList("nickname11", "nickname4", "nickname13"),
  239. instance.getSelectedProfileNicknames().get());
  240. }
  241. @Test
  242. public void testAddListener() {
  243. instance.addProfile("profile4", "realname", "ident", Lists.newArrayList("nickname"));
  244. verify(listener).profileAdded(any(MutableProfile.class));
  245. }
  246. @Test
  247. public void testRemoveListener() {
  248. instance.addProfile("profile4", "realname", "ident", Lists.newArrayList("nickname"));
  249. assertTrue(instance.getProfile("profile4").isPresent());
  250. final MutableProfile profile4 = instance.getProfile("profile4").get();
  251. verify(listener).profileAdded(profile4);
  252. instance.removeListener(listener);
  253. instance.setSelectedProfile(Optional.ofNullable(profile4));
  254. verify(listener, never()).profileRemoved(profile4);
  255. }
  256. @Test
  257. public void testSave() {
  258. final MutableProfile editedProfile = new MutableProfile("newName", "realName",
  259. Optional.of("newIdent"), Lists.newArrayList("newNickname"));
  260. instance.editProfile(mutableProfile1, editedProfile.getName(), editedProfile.getRealname(),
  261. editedProfile.getIdent().get(), editedProfile.getNicknames());
  262. final List<MutableProfile> profiles = Lists.newArrayList(editedProfile, mutableProfile2,
  263. mutableProfile3);
  264. instance.save();
  265. assertEquals(profiles, instance.getProfileList());
  266. }
  267. }