You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CoreProfilesDialogModelTest.java 12KB

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