Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

WhoisOnQueryManagerTest.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.addons.whoisonquery;
  23. import com.dmdirc.DMDircMBassador;
  24. import com.dmdirc.Query;
  25. import com.dmdirc.config.prefs.PreferencesCategory;
  26. import com.dmdirc.config.prefs.PreferencesDialogModel;
  27. import com.dmdirc.config.prefs.PreferencesSetting;
  28. import com.dmdirc.events.ClientPrefsOpenedEvent;
  29. import com.dmdirc.events.ConnectionPrefsRequestedEvent;
  30. import com.dmdirc.events.QueryOpenedEvent;
  31. import com.dmdirc.interfaces.Connection;
  32. import com.dmdirc.interfaces.User;
  33. import com.dmdirc.interfaces.WindowModel;
  34. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  35. import com.dmdirc.interfaces.config.ConfigProvider;
  36. import com.dmdirc.plugins.PluginInfo;
  37. import com.dmdirc.plugins.PluginMetaData;
  38. import java.util.Optional;
  39. import org.junit.Before;
  40. import org.junit.Test;
  41. import org.junit.runner.RunWith;
  42. import org.mockito.ArgumentCaptor;
  43. import org.mockito.Captor;
  44. import org.mockito.Mock;
  45. import org.mockito.runners.MockitoJUnitRunner;
  46. import static org.mockito.Matchers.any;
  47. import static org.mockito.Mockito.never;
  48. import static org.mockito.Mockito.verify;
  49. import static org.mockito.Mockito.when;
  50. @RunWith(MockitoJUnitRunner.class)
  51. public class WhoisOnQueryManagerTest {
  52. @Mock private DMDircMBassador eventBus;
  53. @Mock private PluginInfo pluginInfo;
  54. @Mock private PluginMetaData pluginMetaData;
  55. @Mock private QueryOpenedEvent queryOpenedEvent;
  56. @Mock private AggregateConfigProvider aggregateConfigProvider;
  57. @Mock private Connection connection;
  58. @Mock private WindowModel windowModel;
  59. @Mock private Query query;
  60. @Mock private User user;
  61. @Mock private ClientPrefsOpenedEvent clientPrefsOpenedEvent;
  62. @Mock private ConnectionPrefsRequestedEvent connectionPrefsRequestedEvent;
  63. @Mock private PreferencesCategory preferencesCategory;
  64. @Captor private ArgumentCaptor<PreferencesSetting> preferencesSetting;
  65. @Mock private ConfigProvider configProvider;
  66. @Mock private PreferencesDialogModel preferencesDialogModel;
  67. @Captor private ArgumentCaptor<PreferencesCategory> preferencesCategoryArgumentCaptor;
  68. private WhoisOnQueryManager instance;
  69. @Before
  70. public void setUp() throws Exception {
  71. when(pluginInfo.getMetaData()).thenReturn(pluginMetaData);
  72. when(pluginMetaData.getFriendlyName()).thenReturn("Plugin");
  73. when(queryOpenedEvent.getQuery()).thenReturn(query);
  74. when(query.getUser()).thenReturn(user);
  75. when(query.getConnection()).thenReturn(Optional.of(connection));
  76. when(connection.getWindowModel()).thenReturn(windowModel);
  77. when(windowModel.getConfigManager()).thenReturn(aggregateConfigProvider);
  78. when(connectionPrefsRequestedEvent.getConfig()).thenReturn(aggregateConfigProvider);
  79. when(connectionPrefsRequestedEvent.getIdentity()).thenReturn(configProvider);
  80. when(connectionPrefsRequestedEvent.getCategory()).thenReturn(preferencesCategory);
  81. when(clientPrefsOpenedEvent.getModel()).thenReturn(preferencesDialogModel);
  82. when(preferencesDialogModel.getIdentity()).thenReturn(configProvider);
  83. when(preferencesDialogModel.getConfigManager()).thenReturn(aggregateConfigProvider);
  84. when(preferencesDialogModel.getCategory("Plugins")).thenReturn(preferencesCategory);
  85. instance = new WhoisOnQueryManager("domain", pluginInfo, eventBus);
  86. }
  87. @Test
  88. public void testLoad() throws Exception {
  89. instance.load();
  90. verify(eventBus).subscribe(instance);
  91. }
  92. @Test
  93. public void testUnload() throws Exception {
  94. instance.unload();
  95. verify(eventBus).unsubscribe(instance);
  96. }
  97. @Test
  98. public void testHandleQueryEvent_Set() throws Exception {
  99. when(aggregateConfigProvider.getOptionBool("domain", "whoisonquery")).thenReturn(true);
  100. instance.handleQueryEvent(queryOpenedEvent);
  101. verify(connection).requestUserInfo(user);
  102. }
  103. @Test
  104. public void testHandleQueryEvent_Unset() throws Exception {
  105. when(aggregateConfigProvider.getOptionBool("domain", "whoisonquery")).thenReturn(false);
  106. instance.handleQueryEvent(queryOpenedEvent);
  107. verify(connection, never()).requestUserInfo(user);
  108. }
  109. @Test
  110. public void testHandlePrefsEvent() throws Exception {
  111. when(aggregateConfigProvider.getOptionBool("domain", "whoisonquery")).thenReturn(true);
  112. instance.handlePrefsEvent(clientPrefsOpenedEvent);
  113. verify(preferencesCategory).addSubCategory(preferencesCategoryArgumentCaptor.capture());
  114. }
  115. @Test
  116. public void testHandleConnectionPrefsEvent() throws Exception {
  117. when(aggregateConfigProvider.getOptionBool("domain", "whoisonquery")).thenReturn(true);
  118. instance.handleConnectionPrefsEvent(connectionPrefsRequestedEvent);
  119. verify(preferencesCategory).addSetting(any());
  120. }
  121. }