123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- /*
- * Copyright (c) 2006-2015 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.ui.messages;
-
- import com.dmdirc.Channel;
- import com.dmdirc.DMDircMBassador;
- import com.dmdirc.config.profiles.Profile;
- import com.dmdirc.events.ChannelHighlightEvent;
- import com.dmdirc.events.ChannelMessageEvent;
- import com.dmdirc.events.ServerConnectedEvent;
- import com.dmdirc.events.ServerNickChangeEvent;
- import com.dmdirc.interfaces.Connection;
- import com.dmdirc.interfaces.GroupChatUser;
- import com.dmdirc.interfaces.User;
- import com.dmdirc.interfaces.WindowModel;
-
- import com.google.common.collect.Lists;
-
- import java.util.Collections;
- import java.util.Optional;
-
- import org.junit.Before;
- import org.junit.Test;
- import org.junit.runner.RunWith;
- import org.mockito.ArgumentCaptor;
- import org.mockito.Mock;
- import org.mockito.runners.MockitoJUnitRunner;
-
- import static org.junit.Assert.assertEquals;
- import static org.mockito.Matchers.any;
- import static org.mockito.Mockito.never;
- import static org.mockito.Mockito.only;
- import static org.mockito.Mockito.verify;
- import static org.mockito.Mockito.when;
-
- @RunWith(MockitoJUnitRunner.class)
- public class HighlightManagerTest {
-
- @Mock private Connection connection;
- @Mock private WindowModel windowModel;
- @Mock private User user;
- @Mock private Profile profile;
-
- @Mock private Channel channel;
- @Mock private GroupChatUser channelUser;
- @Mock private DMDircMBassador eventBus;
-
- private HighlightManager manager;
-
- @Before
- public void setup() {
- when(connection.getLocalUser()).thenReturn(Optional.of(user));
- when(connection.getProfile()).thenReturn(profile);
- when(connection.getWindowModel()).thenReturn(windowModel);
-
- when(channel.getEventBus()).thenReturn(eventBus);
- when(channel.getConnection()).thenReturn(Optional.of(connection));
-
- manager = new HighlightManager(windowModel);
- }
-
- @Test
- public void testNicknameInChannel() {
- when(user.getNickname()).thenReturn("nickName");
- when(profile.getHighlights()).thenReturn(Collections.<String>emptyList());
-
- final ChannelMessageEvent event = new ChannelMessageEvent(channel, channelUser,
- "Hi, nickName!");
-
- manager.handleConnected(new ServerConnectedEvent(connection));
- manager.handleChannelMessage(event);
-
- final ArgumentCaptor<ChannelHighlightEvent> captor =
- ArgumentCaptor.forClass(ChannelHighlightEvent.class);
-
- verify(eventBus).publishAsync(captor.capture());
- assertEquals(channel, captor.getValue().getChannel());
- assertEquals(channelUser, captor.getValue().getClient());
- assertEquals("Hi, nickName!", captor.getValue().getMessage());
- }
-
- @Test
- public void testNewNicknameInChannelAfterNickChange() {
- when(user.getNickname()).thenReturn("nickName");
- when(profile.getHighlights()).thenReturn(Collections.<String>emptyList());
-
- final ChannelMessageEvent event = new ChannelMessageEvent(channel, channelUser,
- "Hi, newName!");
-
- manager.handleConnected(new ServerConnectedEvent(connection));
- manager.handleNickChange(new ServerNickChangeEvent(connection, "nickName", "newName"));
- manager.handleChannelMessage(event);
-
- final ArgumentCaptor<ChannelHighlightEvent> captor =
- ArgumentCaptor.forClass(ChannelHighlightEvent.class);
-
- verify(eventBus).publishAsync(captor.capture());
- assertEquals(channel, captor.getValue().getChannel());
- assertEquals(channelUser, captor.getValue().getClient());
- assertEquals("Hi, newName!", captor.getValue().getMessage());
- }
-
- @Test
- public void testOldNicknameInChannelAfterNickChange() {
- when(user.getNickname()).thenReturn("nickName");
- when(profile.getHighlights()).thenReturn(Collections.<String>emptyList());
-
- final ChannelMessageEvent event = new ChannelMessageEvent(channel, channelUser,
- "Hi, nickName!");
-
- manager.handleConnected(new ServerConnectedEvent(connection));
- manager.handleNickChange(new ServerNickChangeEvent(connection, "nickName", "newName"));
- manager.handleChannelMessage(event);
-
- verify(eventBus, never()).publishAsync(any());
- }
-
- @Test
- public void testCustomHighlightInChannel() {
- when(user.getNickname()).thenReturn("nickName");
- when(profile.getHighlights()).thenReturn(Lists.newArrayList("dmdirc"));
-
- final ChannelMessageEvent event = new ChannelMessageEvent(channel, channelUser,
- "DMDirc is great.");
-
- manager.handleConnected(new ServerConnectedEvent(connection));
- manager.handleChannelMessage(event);
-
- final ArgumentCaptor<ChannelHighlightEvent> captor =
- ArgumentCaptor.forClass(ChannelHighlightEvent.class);
-
- verify(eventBus).publishAsync(captor.capture());
- assertEquals(channel, captor.getValue().getChannel());
- assertEquals(channelUser, captor.getValue().getClient());
- assertEquals("DMDirc is great.", captor.getValue().getMessage());
- }
-
- @Test
- public void testMultipleCustomHighlightInChannel() {
- when(user.getNickname()).thenReturn("nickName");
- when(profile.getHighlights()).thenReturn(
- Lists.newArrayList("dmdirc", "is", "great", "e"));
-
- final ChannelMessageEvent event = new ChannelMessageEvent(channel, channelUser,
- "DMDirc is great.");
-
- manager.handleConnected(new ServerConnectedEvent(connection));
- manager.handleChannelMessage(event);
-
- final ArgumentCaptor<ChannelHighlightEvent> captor =
- ArgumentCaptor.forClass(ChannelHighlightEvent.class);
-
- verify(eventBus, only()).publishAsync(captor.capture());
- assertEquals(channel, captor.getValue().getChannel());
- assertEquals(channelUser, captor.getValue().getClient());
- assertEquals("DMDirc is great.", captor.getValue().getMessage());
- }
-
- }
|