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.9KB

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