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.

AwayColoursManagerTest.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2006-2014 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.awaycolours;
  23. import com.dmdirc.ChannelClientProperty;
  24. import com.dmdirc.DMDircMBassador;
  25. import com.dmdirc.config.ConfigBinder;
  26. import com.dmdirc.events.ChannelUserAwayEvent;
  27. import com.dmdirc.events.ChannelUserBackEvent;
  28. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  29. import com.dmdirc.parser.interfaces.ChannelClientInfo;
  30. import com.dmdirc.ui.messages.ColourManager;
  31. import com.dmdirc.util.colours.Colour;
  32. import java.util.Map;
  33. import org.junit.Before;
  34. import org.junit.Test;
  35. import org.junit.runner.RunWith;
  36. import org.mockito.Mock;
  37. import org.mockito.runners.MockitoJUnitRunner;
  38. import static org.mockito.Matchers.anyString;
  39. import static org.mockito.Mockito.never;
  40. import static org.mockito.Mockito.verify;
  41. import static org.mockito.Mockito.when;
  42. @RunWith(MockitoJUnitRunner.class)
  43. public class AwayColoursManagerTest {
  44. @Mock private DMDircMBassador eventBus;
  45. @Mock private AggregateConfigProvider config;
  46. @Mock private ConfigBinder binder;
  47. @Mock private ChannelUserAwayEvent awayEvent;
  48. @Mock private ChannelUserBackEvent backEvent;
  49. @Mock private ChannelClientInfo user;
  50. @Mock private Map<Object, Object> map;
  51. @Mock private ColourManager colourManager;
  52. private AwayColoursManager instance;
  53. private String red;
  54. private Colour redColour;
  55. private String black;
  56. private Colour blackColour;
  57. @Before
  58. public void setUp() throws Exception {
  59. red = "red";
  60. black = "black";
  61. redColour = Colour.RED;
  62. blackColour = Colour.BLACK;
  63. when(awayEvent.getUser()).thenReturn(user);
  64. when(backEvent.getUser()).thenReturn(user);
  65. when(user.getMap()).thenReturn(map);
  66. when(config.getBinder()).thenReturn(binder);
  67. when(binder.withDefaultDomain(anyString())).thenReturn(binder);
  68. when(colourManager.getColourFromString(red, Colour.GRAY)).thenReturn(redColour);
  69. when(colourManager.getColourFromString(black, Colour.GRAY)).thenReturn(blackColour);
  70. instance = new AwayColoursManager(eventBus, config, "test", colourManager);
  71. }
  72. @Test
  73. public void testLoad() throws Exception {
  74. instance.load();
  75. verify(eventBus).subscribe(instance);
  76. }
  77. @Test
  78. public void testUnload() throws Exception {
  79. instance.unload();
  80. verify(eventBus).unsubscribe(instance);
  81. }
  82. @Test
  83. public void testHandleColourChange() throws Exception {
  84. instance.handleColourChange(red);
  85. instance.handleNicklistChange(true);
  86. instance.handleTextChange(true);
  87. instance.handleAwayEvent(awayEvent);
  88. verify(map).put(ChannelClientProperty.NICKLIST_FOREGROUND, redColour);
  89. verify(map).put(ChannelClientProperty.TEXT_FOREGROUND, redColour);
  90. instance.handleColourChange(black);
  91. instance.handleNicklistChange(true);
  92. instance.handleTextChange(true);
  93. instance.handleAwayEvent(awayEvent);
  94. verify(map).put(ChannelClientProperty.NICKLIST_FOREGROUND, blackColour);
  95. verify(map).put(ChannelClientProperty.TEXT_FOREGROUND, blackColour);
  96. }
  97. @Test
  98. public void testHandleNicklistChange() throws Exception {
  99. instance.handleColourChange(red);
  100. instance.handleNicklistChange(true);
  101. instance.handleTextChange(true);
  102. instance.handleAwayEvent(awayEvent);
  103. verify(map).put(ChannelClientProperty.NICKLIST_FOREGROUND, redColour);
  104. verify(map).put(ChannelClientProperty.TEXT_FOREGROUND, redColour);
  105. instance.handleColourChange(black);
  106. instance.handleNicklistChange(false);
  107. instance.handleTextChange(true);
  108. instance.handleAwayEvent(awayEvent);
  109. verify(map, never()).put(ChannelClientProperty.NICKLIST_FOREGROUND, blackColour);
  110. verify(map).put(ChannelClientProperty.TEXT_FOREGROUND, blackColour);
  111. }
  112. @Test
  113. public void testHandleTextChange() throws Exception {
  114. instance.handleColourChange(red);
  115. instance.handleNicklistChange(true);
  116. instance.handleTextChange(true);
  117. instance.handleAwayEvent(awayEvent);
  118. verify(map).put(ChannelClientProperty.NICKLIST_FOREGROUND, redColour);
  119. verify(map).put(ChannelClientProperty.TEXT_FOREGROUND, redColour);
  120. instance.handleColourChange(black);
  121. instance.handleNicklistChange(true);
  122. instance.handleTextChange(false);
  123. instance.handleAwayEvent(awayEvent);
  124. verify(map).put(ChannelClientProperty.NICKLIST_FOREGROUND, blackColour);
  125. verify(map, never()).put(ChannelClientProperty.TEXT_FOREGROUND, blackColour);
  126. }
  127. @Test
  128. public void testHandleAwayEvent() throws Exception {
  129. instance.handleNicklistChange(true);
  130. instance.handleTextChange(true);
  131. instance.handleBackEvent(backEvent);
  132. verify(map).remove(ChannelClientProperty.NICKLIST_FOREGROUND);
  133. verify(map).remove(ChannelClientProperty.TEXT_FOREGROUND);
  134. }
  135. @Test
  136. public void testHandleBackEvent() throws Exception {
  137. instance.handleColourChange(red);
  138. instance.handleNicklistChange(true);
  139. instance.handleTextChange(true);
  140. instance.handleAwayEvent(awayEvent);
  141. verify(map).put(ChannelClientProperty.NICKLIST_FOREGROUND, redColour);
  142. verify(map).put(ChannelClientProperty.TEXT_FOREGROUND, redColour);
  143. }
  144. }