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

PreferencesDialogModelTest.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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.events.eventbus.EventBus;
  26. import com.dmdirc.config.provider.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.ArgumentMatchers.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. final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
  60. null, null, cm, null, serviceManager, eventBus);
  61. assertNotNull(pm.getCategory("General"));
  62. assertNotNull(pm.getCategory("Connection"));
  63. assertNotNull(pm.getCategory("Messages"));
  64. assertNotNull(pm.getCategory("Advanced"));
  65. assertNotNull(pm.getCategory("GUI"));
  66. assertNotNull(pm.getCategory("Plugins"));
  67. assertNotNull(pm.getCategory("Updates"));
  68. assertNotNull(pm.getCategory("URL Handlers"));
  69. }
  70. @Test
  71. public void testDismiss() {
  72. final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
  73. final PreferencesCategory category = mock(PreferencesCategory.class);
  74. final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
  75. null, null, cm, null, serviceManager, eventBus);
  76. pm.addCategory(category);
  77. pm.dismiss();
  78. verify(category).dismiss();
  79. }
  80. @Test
  81. public void testSaveNoRestart() {
  82. final PreferencesCategory category = mock(PreferencesCategory.class);
  83. when(category.save()).thenReturn(false);
  84. final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
  85. final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
  86. null, null, cm, null, serviceManager, eventBus);
  87. pm.addCategory(category);
  88. assertFalse(pm.save());
  89. verify(category).save();
  90. }
  91. @Test
  92. public void testSaveRestart() {
  93. final PreferencesCategory category = mock(PreferencesCategory.class);
  94. when(category.save()).thenReturn(true);
  95. final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
  96. final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
  97. null, null, cm, null, serviceManager, eventBus);
  98. pm.addCategory(category);
  99. assertTrue(pm.save());
  100. verify(category).save();
  101. }
  102. @Test
  103. public void testGetCategory() {
  104. final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
  105. final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
  106. null, null, cm, null, serviceManager, eventBus);
  107. assertNull(pm.getCategory("unittest123"));
  108. }
  109. @Test
  110. public void testGetCategories() {
  111. final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
  112. final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
  113. null, null, cm, null, serviceManager, eventBus);
  114. assertNotNull(pm.getCategories());
  115. assertFalse(pm.getCategories().isEmpty());
  116. for (PreferencesCategory cat : pm.getCategories()) {
  117. assertNotNull(pm.getCategory(cat.getTitle()));
  118. }
  119. }
  120. @Test
  121. public void testSaveListener() {
  122. final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
  123. final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
  124. null, null, cm, null, serviceManager, eventBus);
  125. final PreferencesInterface tpi = mock(PreferencesInterface.class);
  126. pm.registerSaveListener(tpi);
  127. pm.fireSaveListeners();
  128. verify(tpi).save();
  129. }
  130. @Test
  131. public void testOpenAction() {
  132. final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
  133. new PreferencesDialogModel(null, null, null, null, cm, null, serviceManager, eventBus);
  134. verify(eventBus).publish(isA(ClientPrefsOpenedEvent.class));
  135. }
  136. @Test
  137. public void testCloseAction() {
  138. final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
  139. final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
  140. null, null, cm, null, serviceManager, eventBus);
  141. pm.close();
  142. verify(eventBus).publishAsync(isA(ClientPrefsClosedEvent.class));
  143. }
  144. @Test
  145. public void testCategoryObjectSaveListeners() {
  146. final AggregateConfigProvider cm = mock(AggregateConfigProvider.class);
  147. final PreferencesDialogModel pm = new PreferencesDialogModel(null, null,
  148. null, null, cm, null, serviceManager, eventBus);
  149. final PreferencesCategory category = mock(PreferencesCategory.class);
  150. final PreferencesInterface tpi = mock(PreferencesInterface.class);
  151. when(category.hasObject()).thenReturn(true);
  152. when(category.getObject()).thenReturn(tpi);
  153. pm.addCategory(category);
  154. pm.fireSaveListeners();
  155. verify(tpi).save();
  156. }
  157. }