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.

SetNickColourTest.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.commandparser.commands.channel;
  23. import com.dmdirc.Channel;
  24. import com.dmdirc.commandparser.CommandArguments;
  25. import com.dmdirc.commandparser.commands.context.ChannelCommandContext;
  26. import com.dmdirc.events.CommandErrorEvent;
  27. import com.dmdirc.events.DisplayProperty;
  28. import com.dmdirc.interfaces.CommandController;
  29. import com.dmdirc.interfaces.Connection;
  30. import com.dmdirc.events.eventbus.EventBus;
  31. import com.dmdirc.interfaces.GroupChatUser;
  32. import com.dmdirc.interfaces.User;
  33. import com.dmdirc.interfaces.WindowModel;
  34. import com.dmdirc.ui.messages.ColourManager;
  35. import com.dmdirc.ui.messages.ColourManagerFactory;
  36. import com.dmdirc.util.colours.Colour;
  37. import java.util.Optional;
  38. import org.junit.Before;
  39. import org.junit.Test;
  40. import org.junit.runner.RunWith;
  41. import org.mockito.ArgumentCaptor;
  42. import org.mockito.Captor;
  43. import org.mockito.Mock;
  44. import org.mockito.runners.MockitoJUnitRunner;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.mockito.ArgumentMatchers.any;
  47. import static org.mockito.ArgumentMatchers.isA;
  48. import static org.mockito.Mockito.eq;
  49. import static org.mockito.Mockito.never;
  50. import static org.mockito.Mockito.verify;
  51. import static org.mockito.Mockito.when;
  52. @RunWith(MockitoJUnitRunner.class)
  53. public class SetNickColourTest {
  54. @Mock private Connection connection;
  55. @Mock private User user1;
  56. @Mock private User user2;
  57. @Mock private GroupChatUser groupChatUser;
  58. @Mock private Channel channel;
  59. @Mock private ColourManagerFactory colourManagerFactory;
  60. @Mock private ColourManager colourManager;
  61. @Mock private CommandController controller;
  62. @Mock private WindowModel tiw;
  63. @Mock private EventBus eventbus;
  64. @Captor private ArgumentCaptor<CommandErrorEvent> errorEventCaptor;
  65. private SetNickColour command;
  66. @Before
  67. public void setUp() {
  68. command = new SetNickColour(controller, colourManagerFactory);
  69. when(controller.getCommandChar()).thenReturn('/');
  70. when(controller.getSilenceChar()).thenReturn('.');
  71. when(channel.getConnection()).thenReturn(Optional.of(connection));
  72. when(connection.getUser("moo")).thenReturn(user1);
  73. when(connection.getUser("foo")).thenReturn(user2);
  74. when(channel.getUser(user1)).thenReturn(Optional.of(groupChatUser));
  75. when(channel.getUser(user2)).thenReturn(Optional.empty());
  76. when(colourManagerFactory.getColourManager(any())).thenReturn(colourManager);
  77. when(colourManager.getColourFromString(eq("4"), any())).thenReturn(Colour.RED);
  78. when(tiw.getEventBus()).thenReturn(eventbus);
  79. }
  80. @Test
  81. public void testUsageNoArgs() {
  82. command.execute(tiw, new CommandArguments(controller, "/foo"),
  83. new ChannelCommandContext(null, SetNickColour.INFO, channel));
  84. verify(eventbus).publishAsync(isA(CommandErrorEvent.class));
  85. }
  86. @Test
  87. public void testUsageNicknameValid() {
  88. command.execute(tiw, new CommandArguments(controller, "/foo moo"),
  89. new ChannelCommandContext(null, SetNickColour.INFO, channel));
  90. verify(channel).refreshClients();
  91. verify(groupChatUser).removeDisplayProperty(DisplayProperty.FOREGROUND_COLOUR);
  92. }
  93. @Test
  94. public void testUsageNicknameInvalid() {
  95. command.execute(tiw, new CommandArguments(controller, "/foo foo"),
  96. new ChannelCommandContext(null, SetNickColour.INFO, channel));
  97. verify(channel, never()).refreshClients();
  98. verify(groupChatUser, never()).removeDisplayProperty(DisplayProperty.FOREGROUND_COLOUR);
  99. verify(eventbus).publishAsync(errorEventCaptor.capture());
  100. assertEquals("No such nickname (foo)!", errorEventCaptor.getValue().getMessage());
  101. }
  102. @Test
  103. public void testUsageInvalidColour() {
  104. command.execute(tiw, new CommandArguments(controller, "/foo moo omg"),
  105. new ChannelCommandContext(null, SetNickColour.INFO, channel));
  106. verify(eventbus).publishAsync(errorEventCaptor.capture());
  107. assertEquals("Invalid colour specified (omg).", errorEventCaptor.getValue().getMessage());
  108. }
  109. @Test
  110. public void testUsageValidColour() {
  111. command.execute(tiw, new CommandArguments(controller, "/foo moo 4"),
  112. new ChannelCommandContext(null, SetNickColour.INFO, channel));
  113. verify(channel).refreshClients();
  114. verify(groupChatUser).setDisplayProperty(DisplayProperty.FOREGROUND_COLOUR, Colour.RED);
  115. }
  116. }