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

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