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.

WhoisOnQueryManagerTest.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.whoisonquery;
  18. import com.dmdirc.Query;
  19. import com.dmdirc.config.prefs.PreferencesCategory;
  20. import com.dmdirc.config.prefs.PreferencesDialogModel;
  21. import com.dmdirc.config.prefs.PreferencesSetting;
  22. import com.dmdirc.events.ClientPrefsOpenedEvent;
  23. import com.dmdirc.events.ConnectionPrefsRequestedEvent;
  24. import com.dmdirc.events.QueryOpenedEvent;
  25. import com.dmdirc.interfaces.Connection;
  26. import com.dmdirc.events.eventbus.EventBus;
  27. import com.dmdirc.interfaces.User;
  28. import com.dmdirc.interfaces.WindowModel;
  29. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  30. import com.dmdirc.interfaces.config.ConfigProvider;
  31. import com.dmdirc.plugins.PluginInfo;
  32. import com.dmdirc.plugins.PluginMetaData;
  33. import java.util.Optional;
  34. import org.junit.Before;
  35. import org.junit.Test;
  36. import org.junit.runner.RunWith;
  37. import org.mockito.ArgumentCaptor;
  38. import org.mockito.Captor;
  39. import org.mockito.Mock;
  40. import org.mockito.runners.MockitoJUnitRunner;
  41. import static org.mockito.Matchers.any;
  42. import static org.mockito.Mockito.never;
  43. import static org.mockito.Mockito.verify;
  44. import static org.mockito.Mockito.when;
  45. @RunWith(MockitoJUnitRunner.class)
  46. public class WhoisOnQueryManagerTest {
  47. @Mock private EventBus eventBus;
  48. @Mock private PluginInfo pluginInfo;
  49. @Mock private PluginMetaData pluginMetaData;
  50. @Mock private QueryOpenedEvent queryOpenedEvent;
  51. @Mock private AggregateConfigProvider aggregateConfigProvider;
  52. @Mock private Connection connection;
  53. @Mock private WindowModel windowModel;
  54. @Mock private Query query;
  55. @Mock private User user;
  56. @Mock private ClientPrefsOpenedEvent clientPrefsOpenedEvent;
  57. @Mock private ConnectionPrefsRequestedEvent connectionPrefsRequestedEvent;
  58. @Mock private PreferencesCategory preferencesCategory;
  59. @Captor private ArgumentCaptor<PreferencesSetting> preferencesSetting;
  60. @Mock private ConfigProvider configProvider;
  61. @Mock private PreferencesDialogModel preferencesDialogModel;
  62. @Captor private ArgumentCaptor<PreferencesCategory> preferencesCategoryArgumentCaptor;
  63. private WhoisOnQueryManager instance;
  64. @Before
  65. public void setUp() throws Exception {
  66. when(pluginInfo.getMetaData()).thenReturn(pluginMetaData);
  67. when(pluginMetaData.getFriendlyName()).thenReturn("Plugin");
  68. when(queryOpenedEvent.getQuery()).thenReturn(query);
  69. when(query.getUser()).thenReturn(user);
  70. when(query.getConnection()).thenReturn(Optional.of(connection));
  71. when(connection.getWindowModel()).thenReturn(windowModel);
  72. when(windowModel.getConfigManager()).thenReturn(aggregateConfigProvider);
  73. when(connectionPrefsRequestedEvent.getConfig()).thenReturn(aggregateConfigProvider);
  74. when(connectionPrefsRequestedEvent.getIdentity()).thenReturn(configProvider);
  75. when(connectionPrefsRequestedEvent.getCategory()).thenReturn(preferencesCategory);
  76. when(clientPrefsOpenedEvent.getModel()).thenReturn(preferencesDialogModel);
  77. when(preferencesDialogModel.getIdentity()).thenReturn(configProvider);
  78. when(preferencesDialogModel.getConfigManager()).thenReturn(aggregateConfigProvider);
  79. when(preferencesDialogModel.getCategory("Plugins")).thenReturn(preferencesCategory);
  80. instance = new WhoisOnQueryManager("domain", pluginInfo, eventBus);
  81. }
  82. @Test
  83. public void testLoad() throws Exception {
  84. instance.load();
  85. verify(eventBus).subscribe(instance);
  86. }
  87. @Test
  88. public void testUnload() throws Exception {
  89. instance.unload();
  90. verify(eventBus).unsubscribe(instance);
  91. }
  92. @Test
  93. public void testHandleQueryEvent_Set() throws Exception {
  94. when(aggregateConfigProvider.getOptionBool("domain", "whoisonquery")).thenReturn(true);
  95. instance.handleQueryEvent(queryOpenedEvent);
  96. verify(connection).requestUserInfo(user);
  97. }
  98. @Test
  99. public void testHandleQueryEvent_Unset() throws Exception {
  100. when(aggregateConfigProvider.getOptionBool("domain", "whoisonquery")).thenReturn(false);
  101. instance.handleQueryEvent(queryOpenedEvent);
  102. verify(connection, never()).requestUserInfo(user);
  103. }
  104. @Test
  105. public void testHandlePrefsEvent() throws Exception {
  106. when(aggregateConfigProvider.getOptionBool("domain", "whoisonquery")).thenReturn(true);
  107. instance.handlePrefsEvent(clientPrefsOpenedEvent);
  108. verify(preferencesCategory).addSubCategory(preferencesCategoryArgumentCaptor.capture());
  109. }
  110. @Test
  111. public void testHandleConnectionPrefsEvent() throws Exception {
  112. when(aggregateConfigProvider.getOptionBool("domain", "whoisonquery")).thenReturn(true);
  113. instance.handleConnectionPrefsEvent(connectionPrefsRequestedEvent);
  114. verify(preferencesCategory).addSetting(any());
  115. }
  116. }