Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
WorkspaceUsersPresenter unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Huang committed Sep 27, 2012
1 parent dca4b61 commit 2b2afc9
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 36 deletions.
Expand Up @@ -191,12 +191,15 @@ public int hashCode()
public boolean equals(Object obj)
{
if (obj == null)
{
return false;
}
if (!(obj instanceof KeyShortcut))
{
return false;
}
KeyShortcut other = (KeyShortcut) obj;
boolean equal = keys.equals(other.keys) && context == other.context;
return equal;
return keys.equals(other.keys) && context == other.context;
}

/**
Expand Down
Expand Up @@ -18,6 +18,7 @@
import org.zanata.webtrans.client.keys.ShortcutContext;
import org.zanata.webtrans.client.resources.WebTransMessages;
import org.zanata.webtrans.client.rpc.CachingDispatchAsync;
import org.zanata.webtrans.client.rpc.NoOpAsyncCallback;
import org.zanata.webtrans.client.service.UserSessionService;
import org.zanata.webtrans.client.ui.HasManageUserPanel;
import org.zanata.webtrans.client.view.WorkspaceUsersDisplay;
Expand Down Expand Up @@ -61,7 +62,7 @@ public WorkspaceUsersPresenter(WorkspaceUsersDisplay display, EventBus eventBus,
@Override
protected void onBind()
{
keyShortcutPresenter.register(new KeyShortcut(new Keys(Keys.NO_MODIFIER, KeyCodes.KEY_ENTER), ShortcutContext.Chat, messages.searchGlossary(), new KeyShortcutEventHandler()
keyShortcutPresenter.register(new KeyShortcut(new Keys(Keys.NO_MODIFIER, KeyCodes.KEY_ENTER), ShortcutContext.Chat, messages.publishChatContent(), new KeyShortcutEventHandler()
{
@Override
public void onKeyShortcut(KeyShortcutEvent event)
Expand All @@ -87,7 +88,13 @@ public void onEnterWorkspace(EnterWorkspaceEvent event)
@Override
public void onExitWorkspace(ExitWorkspaceEvent event)
{
removeTranslator(event.getEditorClientId(), event.getPerson());
EditorClientId editorClientId = event.getEditorClientId();
UserPanelSessionItem item = sessionService.getUserPanel(editorClientId);
sessionService.removeUser(editorClientId);

display.removeUser(item.getPanel());

dispatchChatAction(null, messages.hasQuitWorkspace(event.getPerson().getId().toString()), MESSAGE_TYPE.SYSTEM_MSG);
}

@Override
Expand All @@ -108,38 +115,16 @@ public void initUserList(Map<EditorClientId, PersonSessionDetails> users)
}
}

public void removeTranslator(EditorClientId editorClientId, Person person)
{
UserPanelSessionItem item = sessionService.getUserPanel(editorClientId);
sessionService.removeUser(editorClientId);

display.removeUser(item.getPanel());

dispatchChatAction(null, messages.hasQuitWorkspace(person.getId().toString()), MESSAGE_TYPE.SYSTEM_MSG);
}

public void dispatchChatAction(String person, String msg, MESSAGE_TYPE messageType)
protected void dispatchChatAction(String person, String msg, MESSAGE_TYPE messageType)
{
if (!Strings.isNullOrEmpty(msg))
{
dispatcher.execute(new PublishWorkspaceChatAction(person, msg, messageType), new AsyncCallback<PublishWorkspaceChatResult>()
{

@Override
public void onFailure(Throwable caught)
{
}

@Override
public void onSuccess(PublishWorkspaceChatResult result)
{
}
});
dispatcher.execute(new PublishWorkspaceChatAction(person, msg, messageType), new NoOpAsyncCallback<PublishWorkspaceChatResult>());
display.setChatInputText("");
}
}

public void addTranslator(EditorClientId editorClientId, Person person, TransUnit selectedTransUnit)
private void addTranslator(EditorClientId editorClientId, Person person, TransUnit selectedTransUnit)
{
String color = sessionService.getColor(editorClientId);

Expand Down
Expand Up @@ -339,6 +339,9 @@ public interface WebTransMessages extends Messages
@DefaultMessage("Chat")
String chatScope();

@DefaultMessage("Publish chat content")
String publishChatContent();

@DefaultMessage("Version")
String versionNumber();

Expand Down
@@ -1,32 +1,50 @@
package org.zanata.webtrans.client.presenter;

import org.hamcrest.Matchers;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.zanata.webtrans.client.events.EnterWorkspaceEvent;
import org.zanata.webtrans.client.events.ExitWorkspaceEvent;
import org.zanata.webtrans.client.events.KeyShortcutEvent;
import org.zanata.webtrans.client.events.PublishWorkspaceChatEvent;
import org.zanata.webtrans.client.keys.KeyShortcut;
import org.zanata.webtrans.client.keys.Keys;
import org.zanata.webtrans.client.keys.ShortcutContext;
import org.zanata.webtrans.client.resources.WebTransMessages;
import org.zanata.webtrans.client.rpc.CachingDispatchAsync;
import org.zanata.webtrans.client.rpc.NoOpAsyncCallback;
import org.zanata.webtrans.client.service.UserSessionService;
import org.zanata.webtrans.client.ui.HasManageUserPanel;
import org.zanata.webtrans.client.view.WorkspaceUsersDisplay;
import org.zanata.webtrans.shared.auth.EditorClientId;
import org.zanata.webtrans.shared.auth.Identity;
import org.zanata.webtrans.shared.model.Person;
import org.zanata.webtrans.shared.model.PersonId;
import org.zanata.webtrans.shared.model.UserPanelSessionItem;
import org.zanata.webtrans.shared.rpc.HasWorkspaceChatData;
import org.zanata.webtrans.shared.rpc.PublishWorkspaceChatAction;
import com.google.gwt.event.dom.client.KeyCodes;

import net.customware.gwt.presenter.client.EventBus;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@Test(groups = "unit-tests")
/**
* @author Patrick Huang <a href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
@Test(groups = "unit-tests")
public class WorkspaceUsersPresenterTest
{
private final static String PUBLIC_CHANNEL_WARN = "Warning! This is a public channel";
private WorkspaceUsersPresenter presenter;
@Mock
private WorkspaceUsersDisplay display;
Expand All @@ -42,27 +60,103 @@ public class WorkspaceUsersPresenterTest
private UserSessionService sessionService;
@Mock
private KeyShortcutPresenter keyShortcutPresenter;
@Captor
private ArgumentCaptor<KeyShortcut> keyShortcutCaptor;
@Mock
private UserPanelSessionItem userPanelSessionItem;
@Mock
private HasManageUserPanel userSessionPanel;

@BeforeMethod
public void beforeMethod()
{
MockitoAnnotations.initMocks(this);
presenter = new WorkspaceUsersPresenter(display, eventBus, identity, dispatcher, messages, sessionService, keyShortcutPresenter);
verify(display).setListener(presenter);
}

@Test
public void onBind()
{
// ArgumentCaptor<KeyShortcutEventHandler> keyShortcutEventHandlerCaptor = ArgumentCaptor.forClass(KeyShortcutEventHandler.class);
when(messages.thisIsAPublicChannel()).thenReturn("Warning! This is a public channel");
presenter.onBind();

verify(keyShortcutPresenter).register(keyShortcutCaptor.capture());
verify(eventBus).addHandler(PublishWorkspaceChatEvent.getType(), presenter);
verify(eventBus).addHandler(ExitWorkspaceEvent.getType(), presenter);
verify(eventBus).addHandler(EnterWorkspaceEvent.getType(), presenter);
verify(display).appendChat(null, null, "Warning! This is a public channel", HasWorkspaceChatData.MESSAGE_TYPE.SYSTEM_WARNING);
}

@Test
public void testKeyShortcut()
{
Person person = person();
WorkspaceUsersPresenter spyPresenter = Mockito.spy(presenter);
doNothing().when(spyPresenter).dispatchChatAction(person.getId().toString(), "hello", HasWorkspaceChatData.MESSAGE_TYPE.USER_MSG);
when(messages.publishChatContent()).thenReturn("publish chat");
spyPresenter.onBind();
verify(keyShortcutPresenter).register(keyShortcutCaptor.capture());

// key is 'enter', context is Chat, description is publish chat
KeyShortcut keyShortcut = keyShortcutCaptor.getValue();
assertThat(keyShortcut.getAllKeys(), Matchers.hasSize(1));
Keys keys = keyShortcut.getAllKeys().iterator().next();
assertThat(keys.getModifiers(), Matchers.equalTo(Keys.NO_MODIFIER));
assertThat(keys.getKeyCode(), Matchers.equalTo(KeyCodes.KEY_ENTER));
assertThat(keyShortcut.getDescription(), Matchers.equalTo("publish chat"));
assertThat(keyShortcut.getContext(), Matchers.equalTo(ShortcutContext.Chat));

// key handler
when(identity.getPerson()).thenReturn(person);
when(display.getChatInputText()).thenReturn("hello");
keyShortcut.getHandler().onKeyShortcut(new KeyShortcutEvent(keys));
verify(spyPresenter).dispatchChatAction(person.getId().toString(), "hello", HasWorkspaceChatData.MESSAGE_TYPE.USER_MSG);
}

@Test
public void onEnterWorkspaceEvent()
{
// Given:
EnterWorkspaceEvent event = mock(EnterWorkspaceEvent.class);
EditorClientId editorClientId = editorClientId();
Person person = person();
when(event.getEditorClientId()).thenReturn(editorClientId);
when(event.getPerson()).thenReturn(person);
when(messages.hasJoinedWorkspace(person.getId().toString())).thenReturn("someone entered");
when(sessionService.getColor(editorClientId)).thenReturn("red");
when(sessionService.getUserPanel(editorClientId)).thenReturn(userPanelSessionItem);
when(userPanelSessionItem.getPanel()).thenReturn(userSessionPanel);

// When:
presenter.onEnterWorkspace(event);

// Then:
verify(userPanelSessionItem).setSelectedTransUnit(null);
verify(userSessionPanel).setColor("red");
verify(sessionService).updateTranslatorStatus(editorClientId, null);
}

@Test
public void onEnterWorkspaceEventWhenUserIsNotThereYet()
{
// Given: session service don't have that user yet
EnterWorkspaceEvent event = mock(EnterWorkspaceEvent.class);
EditorClientId editorClientId = editorClientId();
Person person = person();
when(event.getEditorClientId()).thenReturn(editorClientId);
when(event.getPerson()).thenReturn(person);
when(messages.hasJoinedWorkspace(person.getId().toString())).thenReturn("someone entered");
when(sessionService.getColor(editorClientId)).thenReturn("red");
when(sessionService.getUserPanel(editorClientId)).thenReturn(null); // user is not there
when(display.addUser(person)).thenReturn(userSessionPanel);

// When:
presenter.onEnterWorkspace(event);

// verify(workspaceUsersPresenter).addTranslator(editorClientId, person, null);
// verify(workspaceUsersPresenter).dispatchChatAction(null, "someone entered", HasWorkspaceChatData.MESSAGE_TYPE.SYSTEM_MSG);
// Then:
verify(sessionService).addUser(eq(editorClientId), isA(UserPanelSessionItem.class));
}

private static Person person()
Expand All @@ -78,14 +172,78 @@ private static EditorClientId editorClientId()
@Test
public void onExitWorkspaceEvent()
{
ExitWorkspaceEvent event = mock(ExitWorkspaceEvent.class);
// Given:
EditorClientId editorClientId = editorClientId();
Person person = person();
when(messages.hasQuitWorkspace(person.getId().toString())).thenReturn("someone has quit");
WorkspaceUsersPresenter spyPresenter = Mockito.spy(presenter);
doNothing().when(spyPresenter).dispatchChatAction(null, "someone has quit", HasWorkspaceChatData.MESSAGE_TYPE.SYSTEM_MSG);
ExitWorkspaceEvent event = mock(ExitWorkspaceEvent.class);
when(event.getEditorClientId()).thenReturn(editorClientId);
when(event.getPerson()).thenReturn(person);
when(sessionService.getUserPanel(editorClientId)).thenReturn(userPanelSessionItem);
when(userPanelSessionItem.getPanel()).thenReturn(userSessionPanel);

// When:
spyPresenter.onExitWorkspace(event);

// Then:
verify(sessionService).removeUser(editorClientId);
verify(display).removeUser(userSessionPanel);
verify(spyPresenter).dispatchChatAction(null, "someone has quit", HasWorkspaceChatData.MESSAGE_TYPE.SYSTEM_MSG);
}

@Test
public void onSendButtonClicked()
{
// Given:
when(display.getChatInputText()).thenReturn("hello");
Person person = person();
when(identity.getPerson()).thenReturn(person);

// When:
presenter.onSendButtonClicked();

// Then:
ArgumentCaptor<PublishWorkspaceChatAction> actionCaptor = ArgumentCaptor.forClass(PublishWorkspaceChatAction.class);
verify(dispatcher).execute(actionCaptor.capture(), isA(NoOpAsyncCallback.class));
PublishWorkspaceChatAction chatAction = actionCaptor.getValue();
assertThat(chatAction.getPerson(), Matchers.equalTo(person.getId().toString()));
assertThat(chatAction.getMsg(), Matchers.equalTo("hello"));
assertThat(chatAction.getMessageType(), Matchers.equalTo(HasWorkspaceChatData.MESSAGE_TYPE.USER_MSG));
verify(display).setChatInputText("");
}

@Test
public void onChatInputFocus()
{
presenter.onChatInputFocused();

verify(keyShortcutPresenter).setContextActive(ShortcutContext.Chat, true);
verify(keyShortcutPresenter).setContextActive(ShortcutContext.Navigation, false);
verify(keyShortcutPresenter).setContextActive(ShortcutContext.Edit, false);
}

@Test
public void onChatInputBlur()
{
presenter.onChatInputBlur();

verify(keyShortcutPresenter).setContextActive(ShortcutContext.Chat, false);
verify(keyShortcutPresenter).setContextActive(ShortcutContext.Navigation, true);
}

@Test
public void onPublishWorkspaceChat()
{
PublishWorkspaceChatEvent event = Mockito.mock(PublishWorkspaceChatEvent.class);
when(event.getPersonId()).thenReturn("admin");
when(event.getMsg()).thenReturn("bye");
when(event.getMessageType()).thenReturn(HasWorkspaceChatData.MESSAGE_TYPE.USER_MSG);
when(event.getTimestamp()).thenReturn("a minute ago");

presenter.onExitWorkspace(event);
presenter.onPublishWorkspaceChat(event);

// verify(workspaceUsersPresenter).removeTranslator(editorClientId, person);
verify(display).appendChat(event.getPersonId(), event.getTimestamp(), event.getMsg(), event.getMessageType());
}
}

0 comments on commit 2b2afc9

Please sign in to comment.