package org.zanata.webtrans.client.presenter;

import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.expectLastCall;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.reset;
import static org.easymock.EasyMock.verify;

import java.util.HashMap;
import java.util.Map;

import net.customware.gwt.presenter.client.EventBus;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.zanata.webtrans.client.presenter.WorkspaceUsersPresenter.Display;
import org.zanata.webtrans.client.ui.HasManageUserSession;
import org.zanata.webtrans.shared.auth.SessionId;
import org.zanata.webtrans.shared.model.Person;
import org.zanata.webtrans.shared.model.PersonId;


@Test(groups = { "unit-tests" })
public class WorkspaceUsersPresenterTest
{
   // object under test
   WorkspaceUsersPresenter workspaceUsersPresenter;

   //injected mocks
   Display mockDisplay = createMock(Display.class);
   EventBus mockEventBus = createMock(EventBus.class);

   @BeforeMethod
   public void resetMocks()
   {
      reset(mockDisplay, mockEventBus);
      workspaceUsersPresenter = new WorkspaceUsersPresenter(mockDisplay, mockEventBus);
   }

   public void setEmptyUserList()
   {
      replay(mockDisplay, mockEventBus);

      workspaceUsersPresenter.bind();
      workspaceUsersPresenter.initUserList(new HashMap<SessionId, Person>());

      verify(mockDisplay, mockEventBus);
   }

   public void setNonEmptyUserList()
   {
      HasManageUserSession mockHasManageUserSession = createMock(HasManageUserSession.class);

      expect(mockDisplay.addUser(new Person(new PersonId("person1"), "John Smith", "http://www.gravatar.com/avatar/john@zanata.org?d=mm&s=16"))).andReturn(mockHasManageUserSession);
      expect(mockDisplay.addUser(new Person(new PersonId("person2"), "Smith John", "http://www.gravatar.com/avatar/smith@zanata.org?d=mm&s=16"))).andReturn(mockHasManageUserSession);
      expect(mockDisplay.addUser(new Person(new PersonId("person3"), "Smohn Jith", "http://www.gravatar.com/avatar/smohn@zanata.org?d=mm&s=16"))).andReturn(mockHasManageUserSession);

      replay(mockDisplay, mockEventBus);

      workspaceUsersPresenter.bind();

      Map<SessionId, Person> people = new HashMap<SessionId, Person>();
      people.put(new SessionId("sessionId1"), new Person(new PersonId("person1"), "John Smith", "http://www.gravatar.com/avatar/john@zanata.org?d=mm&s=16"));
      people.put(new SessionId("sessionId2"), new Person(new PersonId("person2"), "Smith John", "http://www.gravatar.com/avatar/smith@zanata.org?d=mm&s=16"));
      people.put(new SessionId("sessionId3"), new Person(new PersonId("person3"), "Smohn Jith", "http://www.gravatar.com/avatar/smohn@zanata.org?d=mm&s=16"));
      workspaceUsersPresenter.initUserList(people);

      verify(mockDisplay, mockEventBus);
   }
}

@@ -76,26 +76,152 @@
 asyMock.
-createMock
+and;%0Aimport static org.easymock.EasyMock.capture;%0Aimport static org.easymock.EasyMock.createMock;%0Aimport static org.easymock.EasyMock.eq
 ;%0Aimport
@@ -310,16 +310,57 @@
 stCall;%0A
+import static org.easymock.EasyMock.isA;%0A
 import s
@@ -666,16 +666,298 @@
 s.Test;%0A
+import org.zanata.webtrans.client.events.TransUnitUpdatedEvent;%0Aimport org.zanata.webtrans.client.events.TransUnitUpdatedEventHandler;%0Aimport org.zanata.webtrans.client.events.TranslatorStatusUpdateEvent;%0Aimport org.zanata.webtrans.client.events.TranslatorStatusUpdateEventHandler;%0A
 import o
@@ -1232,16 +1232,72 @@
 rsonId;%0A
+import com.google.gwt.event.shared.HandlerRegistration;%0A
 %0A%0A@Test(
@@ -1786,32 +1786,211 @@
 UserList()%0A   %7B%0A
+      expect(mockEventBus.addHandler(eq(TranslatorStatusUpdateEvent.getType()), isA(TranslatorStatusUpdateEventHandler.class)) ).andReturn(createMock(HandlerRegistration.class));%0A
       replay(moc
@@ -2861,24 +2861,203 @@
 rSession);%0A%0A
+      expect(mockEventBus.addHandler(eq(TranslatorStatusUpdateEvent.getType()), isA(TranslatorStatusUpdateEventHandler.class)) ).andReturn(createMock(HandlerRegistration.class));%0A
       replay

Fix failed test for workspaceuserpresenter