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.

PreferencesDialogModelTest.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright (c) 2006-2013 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.config.prefs;
  23. import com.dmdirc.actions.ActionManager;
  24. import com.dmdirc.actions.CoreActionType;
  25. import com.dmdirc.config.ConfigManager;
  26. import com.dmdirc.interfaces.ActionListener;
  27. import com.dmdirc.plugins.PluginManager;
  28. import com.dmdirc.plugins.Service;
  29. import java.util.ArrayList;
  30. import java.util.List;
  31. import org.junit.Before;
  32. import org.junit.Test;
  33. import static org.junit.Assert.*;
  34. import static org.mockito.Mockito.*;
  35. public class PreferencesDialogModelTest {
  36. private ActionManager actionManager;
  37. private PluginManager pluginManager;
  38. @Before
  39. public void setup() {
  40. actionManager = mock(ActionManager.class);
  41. pluginManager = mock(PluginManager.class);
  42. final List<Service> services = new ArrayList<Service>();
  43. final Service tabcompleter = mock(Service.class);
  44. when(tabcompleter.getName()).thenReturn("tabber");
  45. services.add(tabcompleter);
  46. when(pluginManager.getServicesByType("tabcompletion")).thenReturn(services);
  47. }
  48. @Test
  49. public void testDefaults() {
  50. ConfigManager cm = mock(ConfigManager.class);
  51. when(cm.getOption("domain", "option")).thenReturn("fallback");
  52. final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
  53. null, null, cm, null, actionManager, pluginManager);
  54. assertNotNull(pm.getCategory("General"));
  55. assertNotNull(pm.getCategory("Connection"));
  56. assertNotNull(pm.getCategory("Messages"));
  57. assertNotNull(pm.getCategory("Advanced"));
  58. assertNotNull(pm.getCategory("GUI"));
  59. assertNotNull(pm.getCategory("Plugins"));
  60. assertNotNull(pm.getCategory("Updates"));
  61. assertNotNull(pm.getCategory("URL Handlers"));
  62. }
  63. @Test
  64. public void testDismiss() {
  65. ConfigManager cm = mock(ConfigManager.class);
  66. when(cm.getOption("domain", "option")).thenReturn("fallback");
  67. final PreferencesCategory category = mock(PreferencesCategory.class);
  68. final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
  69. null, null, cm, null, actionManager, pluginManager);
  70. pm.addCategory(category);
  71. pm.dismiss();
  72. verify(category).dismiss();
  73. }
  74. @Test
  75. public void testSaveNoRestart() {
  76. final PreferencesCategory category = mock(PreferencesCategory.class);
  77. when(category.save()).thenReturn(false);
  78. ConfigManager cm = mock(ConfigManager.class);
  79. when(cm.getOption("domain", "option")).thenReturn("fallback");
  80. final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
  81. null, null, cm, null, actionManager, pluginManager);
  82. pm.addCategory(category);
  83. assertFalse(pm.save());
  84. verify(category).save();
  85. }
  86. @Test
  87. public void testSaveRestart() {
  88. final PreferencesCategory category = mock(PreferencesCategory.class);
  89. when(category.save()).thenReturn(true);
  90. ConfigManager cm = mock(ConfigManager.class);
  91. when(cm.getOption("domain", "option")).thenReturn("fallback");
  92. final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
  93. null, null, cm, null, actionManager, pluginManager);
  94. pm.addCategory(category);
  95. assertTrue(pm.save());
  96. verify(category).save();
  97. }
  98. @Test
  99. public void testGetCategory() {
  100. ConfigManager cm = mock(ConfigManager.class);
  101. when(cm.getOption("domain", "option")).thenReturn("fallback");
  102. final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
  103. null, null, cm, null, actionManager, pluginManager);
  104. assertNull(pm.getCategory("unittest123"));
  105. }
  106. @Test
  107. public void testGetCategories() {
  108. ConfigManager cm = mock(ConfigManager.class);
  109. when(cm.getOption("domain", "option")).thenReturn("fallback");
  110. final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
  111. null, null, cm, null, actionManager, pluginManager);
  112. assertNotNull(pm.getCategories());
  113. assertFalse(pm.getCategories().isEmpty());
  114. for (PreferencesCategory cat : pm.getCategories()) {
  115. assertNotNull(pm.getCategory(cat.getTitle()));
  116. }
  117. }
  118. @Test
  119. public void testSaveListener() {
  120. ConfigManager cm = mock(ConfigManager.class);
  121. when(cm.getOption("domain", "option")).thenReturn("fallback");
  122. final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
  123. null, null, cm, null, actionManager, pluginManager);
  124. final PreferencesInterface tpi = mock(PreferencesInterface.class);
  125. pm.registerSaveListener(tpi);
  126. pm.fireSaveListeners();
  127. verify(tpi).save();
  128. }
  129. @Test
  130. public void testOpenAction() {
  131. ConfigManager cm = mock(ConfigManager.class);
  132. when(cm.getOption("domain", "option")).thenReturn("fallback");
  133. final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
  134. null, null, cm, null, actionManager, pluginManager);
  135. verify(actionManager).triggerEvent(CoreActionType.CLIENT_PREFS_OPENED, null, pm);
  136. }
  137. @Test
  138. public void testCloseAction() {
  139. final ActionListener tal = mock(ActionListener.class);
  140. ConfigManager cm = mock(ConfigManager.class);
  141. when(cm.getOption("domain", "option")).thenReturn("fallback");
  142. final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
  143. null, null, cm, null, actionManager, pluginManager);
  144. pm.close();
  145. verify(actionManager).triggerEvent(CoreActionType.CLIENT_PREFS_CLOSED, null);
  146. }
  147. @Test
  148. public void testCategoryObjectSaveListeners() {
  149. ConfigManager cm = mock(ConfigManager.class);
  150. when(cm.getOption("domain", "option")).thenReturn("fallback");
  151. final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
  152. null, null, cm, null, actionManager, pluginManager);
  153. final PreferencesCategory category = mock(PreferencesCategory.class);
  154. final PreferencesInterface tpi = mock(PreferencesInterface.class);
  155. when(category.hasObject()).thenReturn(true);
  156. when(category.getObject()).thenReturn(tpi);
  157. pm.addCategory(category);
  158. pm.fireSaveListeners();
  159. verify(tpi).save();
  160. }
  161. }