package org.sagebionetworks.web.unitclient.widget.entity;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

import org.junit.Before;
import org.junit.Test;
import org.sagebionetworks.web.client.GlobalApplicationState;
import org.sagebionetworks.web.client.SageImageBundle;
import org.sagebionetworks.web.client.SynapseClientAsync;
import org.sagebionetworks.web.client.security.AuthenticationController;
import org.sagebionetworks.web.client.utils.CallbackP;
import org.sagebionetworks.web.client.widget.search.SynapseSuggestOracle;
import org.sagebionetworks.web.client.widget.search.SynapseSuggestion;
import org.sagebionetworks.web.client.widget.search.SynapseSuggestBox;
import org.sagebionetworks.web.client.widget.search.SynapseSuggestBoxView;
import org.sagebionetworks.web.client.widget.search.UserGroupSuggestionProvider;
import org.sagebionetworks.web.client.widget.search.UserGroupSuggestionProvider.UserGroupSuggestion;

public class SynapseSuggestBoxTest {

	SynapseSuggestBoxView mockView;
	SynapseClientAsync mockSynapseClient;
	SageImageBundle mockSageImageBundle;
	AuthenticationController mockAuthenticationController;
	GlobalApplicationState mockGlobalApplicationState;
	SynapseSuggestOracle mockOracle;
	SynapseSuggestBox suggestBox;
	UserGroupSuggestionProvider mockSuggestionProvider;

	@Before
	public void before() {
		mockView = mock(SynapseSuggestBoxView.class);
		mockSynapseClient = mock(SynapseClientAsync.class);
		mockSageImageBundle = mock(SageImageBundle.class);
		mockAuthenticationController = mock(AuthenticationController.class);
		mockGlobalApplicationState = mock(GlobalApplicationState.class);
		mockOracle = mock(SynapseSuggestOracle.class);
		mockSuggestionProvider = mock(UserGroupSuggestionProvider.class);
		suggestBox = new SynapseSuggestBox(mockView, mockAuthenticationController, mockGlobalApplicationState, mockSynapseClient, mockSageImageBundle, mockOracle);
		suggestBox.setSuggestionProvider(mockSuggestionProvider);
	}
	
	@Test
	public void testSelectedSuggestionCallback() {
		CallbackP<SynapseSuggestion> mockCallback = mock(CallbackP.class);
		suggestBox.addItemSelectedHandler(mockCallback);
		UserGroupSuggestion suggestion = mock(UserGroupSuggestion.class);
		suggestBox.setSelectedSuggestion(suggestion);
		verify(mockCallback).invoke(suggestion);
	}
	
	@Test
	public void testSelectedSuggestionCallbackNullSelection() {
		CallbackP<SynapseSuggestion> mockCallback = mock(CallbackP.class);
		suggestBox.addItemSelectedHandler(mockCallback);
		suggestBox.setSelectedSuggestion(null);
		verify(mockCallback, never()).invoke(any(UserGroupSuggestion.class));
	}
	
	@Test
	public void testSelectedSuggestionNullCallback() {
		suggestBox.addItemSelectedHandler(null);
		UserGroupSuggestion suggestion = mock(UserGroupSuggestion.class);
		suggestBox.setSelectedSuggestion(suggestion);
		//no error
	}
	
}

@@ -48,24 +48,69 @@
 et.entity;%0A%0A
+import static org.junit.Assert.assertEquals;%0A
 import stati
@@ -137,16 +137,16 @@
 rs.any;%0A
-
 import s
@@ -259,16 +259,56 @@
 .verify;
+%0Aimport static org.mockito.Mockito.when;
 %0A%0Aimport
@@ -349,16 +349,44 @@
 t.Test;%0A
+import org.mockito.Mockito;%0A
 import o
@@ -684,153 +684,8 @@
 kP;%0A
-import org.sagebionetworks.web.client.widget.search.SynapseSuggestOracle;%0Aimport org.sagebionetworks.web.client.widget.search.SynapseSuggestion;%0A
 impo
@@ -826,16 +826,161 @@
 oxView;%0A
+import org.sagebionetworks.web.client.widget.search.SynapseSuggestOracle;%0Aimport org.sagebionetworks.web.client.widget.search.SynapseSuggestion;%0A
 import o
@@ -1154,16 +1154,117 @@
 stion;%0A%0A
+import com.google.gwt.event.dom.client.KeyDownHandler;%0Aimport com.google.gwt.event.shared.GwtEvent;%0A%0A
 public c
@@ -3176,12 +3176,819 @@
 rror%0A%09%7D%0A
-
 %09%0A
+%09@Test%0A%09public void testAddKeyDownHandler() %7B%0A%09%09KeyDownHandler handler = Mockito.mock(KeyDownHandler.class);%0A%09%09suggestBox.addKeyDownHandler(handler);%0A%09%09verify(mockView).addKeyDownHandler(handler);%0A%09%7D%0A%09%0A%09@Test%0A%09public void testFireEvent() %7B%0A%09%09GwtEvent event = Mockito.mock(GwtEvent.class);%0A%09%09suggestBox.fireEvent(event);%0A%09%09verify(mockView).fireEvent(event);%0A%09%7D%0A%09%0A%09@Test%0A%09public void testGetTabIndex() %7B%0A%09%09int tabIndex = 4;%0A%09%09when(mockView.getTabIndex()).thenReturn(tabIndex);%0A%09%09assertEquals(tabIndex, suggestBox.getTabIndex());%0A%09%7D%0A%09%0A%09@Test%0A%09public void testSetAccessKey() %7B%0A%09%09char key = 'a';%0A%09%09suggestBox.setAccessKey(key);%0A%09%09verify(mockView).setAccessKey(key);%0A%09%7D%0A%09%0A%09@Test%0A%09public void testSetTabIndex() %7B%0A%09%09int tabIndex = 5;%0A%09%09suggestBox.setTabIndex(tabIndex);%0A%09%09verify(mockView).setTabIndex(tabIndex);%0A%09%7D%0A
 %7D%0A

add tests for synapse suggest box