gt:         HistoryToken newToken = capturedHistoryToken.getValue();
context: /*
   * Copyright 2012, Red Hat, Inc. and individual contributors as indicated by the
   * @author tags. See the copyright.txt file in the distribution for a full
   * listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it under the
   * terms of the GNU Lesser General Public License as published by the Free
   * Software Foundation; either version 2.1 of the License, or (at your option)
   * any later version.
   *
   * This software is distributed in the hope that it will be useful, but WITHOUT
   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
   * details.
   *
   * You should have received a copy of the GNU Lesser General Public License
   * along with this software; if not, write to the Free Software Foundation,
   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
   * site: http://www.fsf.org.
   */
  package org.zanata.webtrans.client.presenter;
  
  import static org.hamcrest.MatcherAssert.assertThat;
  import static org.hamcrest.Matchers.is;
  import static org.mockito.Matchers.eq;
  import static org.mockito.Mockito.doAnswer;
  import static org.mockito.Mockito.mock;
  import static org.mockito.Mockito.verify;
  import static org.mockito.Mockito.when;
  
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  
  import com.google.gwtmockito.GwtMock;
  import com.google.gwtmockito.GwtMockito;
  import net.customware.gwt.presenter.client.EventBus;
  
  import org.junit.After;
  import org.junit.Before;
  import org.junit.Ignore;
  import org.junit.Test;
  import org.mockito.ArgumentCaptor;
  import org.mockito.Captor;
  import org.mockito.Mock;
  import org.mockito.MockitoAnnotations;
  import org.mockito.invocation.InvocationOnMock;
  import org.mockito.stubbing.Answer;
  import org.zanata.webtrans.client.events.TransUnitUpdatedEvent;
  import org.zanata.webtrans.client.events.TransUnitUpdatedEventHandler;
  import org.zanata.webtrans.client.events.WorkspaceContextUpdateEvent;
  import org.zanata.webtrans.client.events.WorkspaceContextUpdateEventHandler;
  import org.zanata.webtrans.client.history.History;
  import org.zanata.webtrans.client.history.HistoryToken;
  import org.zanata.webtrans.client.history.Window.Location;
  import org.zanata.webtrans.client.keys.KeyShortcut;
  import org.zanata.webtrans.client.presenter.SearchResultsPresenter.Display;
  import org.zanata.webtrans.client.resources.WebTransMessages;
  import org.zanata.webtrans.client.rpc.CachingDispatchAsync;
  import org.zanata.webtrans.client.service.GetTransUnitActionContextHolder;
  import org.zanata.webtrans.client.ui.UndoLink;
  import org.zanata.webtrans.shared.model.TransUnit;
  import org.zanata.webtrans.shared.model.UserWorkspaceContext;
  import org.zanata.webtrans.shared.rpc.GetProjectTransUnitLists;
  import org.zanata.webtrans.shared.rpc.GetProjectTransUnitListsResult;
  
  import com.google.gwt.cell.client.ActionCell;
  import com.google.gwt.event.dom.client.ClickEvent;
  import com.google.gwt.event.dom.client.ClickHandler;
  import com.google.gwt.event.dom.client.HasClickHandlers;
  import com.google.gwt.event.logical.shared.ValueChangeEvent;
  import com.google.gwt.event.logical.shared.ValueChangeHandler;
  import com.google.gwt.event.shared.HandlerRegistration;
  import com.google.gwt.user.client.rpc.AsyncCallback;
  import com.google.gwt.user.client.ui.HasText;
  import com.google.gwt.user.client.ui.HasValue;
  import com.google.gwt.view.client.ListDataProvider;
  import com.google.gwt.view.client.MultiSelectionModel;
  import com.google.gwt.view.client.SelectionChangeEvent;
  import com.google.inject.Provider;
  
  /**
   * @author David Mason, <a
   *         href="mailto:damason@redhat.com">damason@redhat.com</a>
   *
   */
  public class SearchResultsPresenterTest {
  
      private static final String TEST_LOCALE_ID = "de";
  
      private static final long TEST_DOC_ID_1 = 5L;
      private static final String TEST_DOC_PATH_1 = "doc1";
  
      private static final String TEST_TARGET_STRING_1 = "target 1";
      private static final String TEST_SOURCE_STRING_1 = "source";
      private static final int TEST_VERNUM_1 = 0;
      private static final long TEST_TU_ID_1 = 7L;
      private static final String TEST_RES_ID_1 = "resId1";
  
      private static final int TOTAL_KEY_SHORTCUTS = 5;
  
      private static final String TEST_MESSAGE_FOCUS_SEARCH_PHRASE_KEY_SHORTCUT =
              "Focus search phrase";
      private static final String TEST_MESSAGE_FOCUS_REPLACEMENT_PHRASE_KEY_SHORTCUT =
              "Focus replacement phrase";
      private static final String TEST_MESSAGE_REPLACE_SELECTED_KEY_SHORTCUT =
              "Replace selected text flows";
      private static final String TEST_MESSAGE_SELECT_ALL_TEXT_FLOWS_KEY_SHORTCUT =
              "Select all text flows";
      private static final String TEST_MESSAGE_SHOWING_RESULTS_FOR_SEARCH =
              "Results for search XXX (X text flows in X documents)";
      private static final String TEST_MESSAGE_TOGGLE_ROW_ACTION_BUTTONS =
              "Toggle row action buttons";
  
      private static final String TEST_SEARCH_PHRASE = "search phrase";
      private static final String TEST_REPLACE_PHRASE = "replace phrase";
  
      private static final String TEST_MESSAGE_NO_RESULTS_FOR_SEARCH =
              "No results for search XXX";
  
      // object under test
      SearchResultsPresenter searchResultsPresenter;
  
      @Mock
      CachingDispatchAsync mockDispatcher;
      @Mock
      Display mockDisplay;
      @Mock
      EventBus mockEventBus;
      @Mock
      History mockHistory;
      @Mock
      KeyShortcutPresenter mockKeyShortcutPresenter;
      @Mock
      WebTransMessages mockMessages;
      @Mock
      Location mockWindowLocation;
      @Mock
      UserWorkspaceContext mockUserWorkspaceContext;
      @Mock
      Provider<UndoLink> mockUndoLinkProvider;
  
      @Mock
      HasValue<Boolean> mockCaseSensitiveChk;
      @Mock
      HasValue<String> mockFilterTextBox;
      @Mock
      HasClickHandlers mockReplaceAllButton;
      @Mock
      HasValue<String> mockReplacementTextBox;
      @Mock
      HasValue<Boolean> mockRequirePreviewChk;
      @Mock
      HasValue<Boolean> mockSelectAllHeader;
      @Mock
      HasClickHandlers mockSearchButton;
      @Mock
      HasText mockSearchResponseLabel;
      @Mock
      HasValue<Boolean> mockSelectAllChk;
  
      @Captor
      ArgumentCaptor<ClickHandler> capturedReplaceAllButtonClickHandler;
      @Captor
      ArgumentCaptor<ValueChangeHandler<String>> capturedReplacementTextBoxValueChangeHandler;
      @Captor
      ArgumentCaptor<ValueChangeHandler<Boolean>> capturedRequirePreviewChkValueChangeHandler;
      @Captor
      ArgumentCaptor<ClickHandler> capturedSearchButtonClickHandler;
      @Captor
      ArgumentCaptor<ClickHandler> capturedSelectAllButtonClickHandler;
      @Captor
      ArgumentCaptor<ValueChangeHandler<Boolean>> capturedSelectAllChkValueChangeHandler;
  
      @Captor
      ArgumentCaptor<TransUnitUpdatedEventHandler> capturedTransUnitUpdatedEventHandler;
      @Captor
      ArgumentCaptor<WorkspaceContextUpdateEventHandler> capturedWorkspaceContextUpdatedEventHandler;
      @Captor
      ArgumentCaptor<KeyShortcut> capturedKeyShortcuts;
      @Captor
      ArgumentCaptor<HistoryToken> capturedHistoryToken;
  
      @Captor
      ArgumentCaptor<GetProjectTransUnitLists> capturedDispatchedSearch;
      @Captor
      ArgumentCaptor<AsyncCallback<GetProjectTransUnitListsResult>> capturedDispatchedSearchCallback;
  
      // arguments to Display.addDocument method
      @Captor
      ArgumentCaptor<ClickHandler> capturedViewDocClickHandlers;
      @Captor
      ArgumentCaptor<ClickHandler> capturedSearchDocClickHandlers;
      @Captor
      ArgumentCaptor<ClickHandler> capturedInfoClickHandlers;
      @Captor
      ArgumentCaptor<MultiSelectionModel<TransUnitReplaceInfo>> capturedSelectionModels;
      @Captor
      ArgumentCaptor<ValueChangeHandler<Boolean>> capturedSelectDocChangeHandlers;
      @Captor
      ArgumentCaptor<ActionCell.Delegate<TransUnitReplaceInfo>> capturedGoToEditorDelegate;
  
      // first test document
      @Mock
      ListDataProvider<TransUnitReplaceInfo> mockDataProviderDoc1;
      List<TransUnitReplaceInfo> dataProviderDoc1List;
      @Mock
      MultiSelectionModel<TransUnitReplaceInfo> mockSelectionModelDoc1;
      @Captor
      ArgumentCaptor<SelectionChangeEvent.Handler> capturedSelectionChangeHandlerDoc1;
      @Captor
      ArgumentCaptor<SelectionChangeEvent.Handler> capturedSelectionChangeDeselectHandlerDoc1;
      @GwtMock
      private ClickEvent clickEvent;
      @Mock
      private HandlerRegistration handlerRegistration;
      private GetTransUnitActionContextHolder contextHolder =
              new GetTransUnitActionContextHolder(new UserConfigHolder());
  
      @Before
      public void beforeMethod() {
          MockitoAnnotations.initMocks(this);
          GwtMockito.initMocks(this);
  
          searchResultsPresenter =
                  new SearchResultsPresenter(mockDisplay, mockEventBus,
                          mockDispatcher, mockHistory, mockMessages,
                          mockUserWorkspaceContext, mockKeyShortcutPresenter,
                          mockUndoLinkProvider, mockWindowLocation, contextHolder);
  
          when(mockMessages.selectAllTextFlowsKeyShortcut()).thenReturn(
                  TEST_MESSAGE_SELECT_ALL_TEXT_FLOWS_KEY_SHORTCUT);
          when(mockMessages.focusSearchPhraseKeyShortcut()).thenReturn(
                  TEST_MESSAGE_FOCUS_SEARCH_PHRASE_KEY_SHORTCUT);
          when(mockMessages.focusReplacementPhraseKeyShortcut()).thenReturn(
                  TEST_MESSAGE_FOCUS_REPLACEMENT_PHRASE_KEY_SHORTCUT);
          when(mockMessages.replaceSelectedKeyShortcut()).thenReturn(
                  TEST_MESSAGE_REPLACE_SELECTED_KEY_SHORTCUT);
          when(mockMessages.toggleRowActionButtons()).thenReturn(
                  TEST_MESSAGE_TOGGLE_ROW_ACTION_BUTTONS);
  
          // getters used during bind
          when(mockDisplay.getSearchButton()).thenReturn(mockSearchButton);
          when(mockDisplay.getReplacementTextBox()).thenReturn(
                  mockReplacementTextBox);
          when(mockDisplay.getSelectAllChk()).thenReturn(mockSelectAllChk);
          when(mockDisplay.getRequirePreviewChk()).thenReturn(
                  mockRequirePreviewChk);
          when(mockDisplay.getReplaceAllButton())
                  .thenReturn(mockReplaceAllButton);
  
          // getters used after bind
          when(mockDisplay.getCaseSensitiveChk())
                  .thenReturn(mockCaseSensitiveChk);
          when(mockDisplay.getFilterTextBox()).thenReturn(mockFilterTextBox);
          when(mockDisplay.getSearchResponseLabel()).thenReturn(
                  mockSearchResponseLabel);
  
      }
  
      @After
      public void tearDown() {
          GwtMockito.tearDown();
      }
  
      @Test
      public void testExpectedActionsOnBind() {
          dataProviderDoc1List = new ArrayList<TransUnitReplaceInfo>();
  
          when(mockKeyShortcutPresenter.register(capturedKeyShortcuts.capture()))
                  .thenReturn(handlerRegistration);
          when(mockDataProviderDoc1.getList()).thenReturn(dataProviderDoc1List);
  
          boolean hasModifyTranslationAccess = true;
          when(mockUserWorkspaceContext.hasEditTranslationAccess()).thenReturn(
                  hasModifyTranslationAccess);
  
          searchResultsPresenter.bind();
  
          verify(mockSearchButton).addClickHandler(
                  capturedSearchButtonClickHandler.capture());
          verify(mockReplacementTextBox).addValueChangeHandler(
                  capturedReplacementTextBoxValueChangeHandler.capture());
          verify(mockSelectAllChk).addValueChangeHandler(
                  capturedSelectAllChkValueChangeHandler.capture());
          verify(mockRequirePreviewChk).addValueChangeHandler(
                  capturedRequirePreviewChkValueChangeHandler.capture());
          verify(mockReplaceAllButton).addClickHandler(
                  capturedReplaceAllButtonClickHandler.capture());
  
          verify(mockEventBus).addHandler(eq(TransUnitUpdatedEvent.getType()),
                  capturedTransUnitUpdatedEventHandler.capture());
          verify(mockEventBus).addHandler(
                  eq(WorkspaceContextUpdateEvent.getType()),
                  capturedWorkspaceContextUpdatedEventHandler.capture());
  
          verify(mockDisplay).setReplaceAllButtonVisible(
                  hasModifyTranslationAccess);
          verify(mockDisplay).setReplaceAllButtonEnabled(false);
  
          verify(mockDisplay).addSearchFieldsSelect("search target", "target");
          verify(mockDisplay).addSearchFieldsSelect("search source", "source");
          verify(mockDisplay).addSearchFieldsSelect("search both", "both");
  
      }
  
      @Test
      public void searchButtonClickUpdatesHistory() {
          boolean caseSensitiveFalseValue = false;
          expectRunSearch(searchPageHistoryToken(), caseSensitiveFalseValue,
                  TEST_SEARCH_PHRASE,
                  SearchResultsPresenter.Display.SEARCH_FIELD_TARGET);
  
          searchResultsPresenter.bind();
  
          verify(mockSearchButton).addClickHandler(
                  capturedSearchButtonClickHandler.capture());
  
          capturedSearchButtonClickHandler.getValue().onClick(clickEvent);
  
          verify(mockHistory).newItem(capturedHistoryToken.capture());
  
          HistoryToken newToken = capturedHistoryToken.getValue();
          assertThat(
                  "new history token should be updated with current search phrase in search text box",
                  newToken.getProjectSearchText(), is(TEST_SEARCH_PHRASE));
          assertThat(
                  "new history token project search case sensitivity should match checkbox value",
                  newToken.getProjectSearchCaseSensitive(),
                  is(caseSensitiveFalseValue));
          assertThat(
                  "new history token should reflect search in target when selected search field is target",
                  newToken.isProjectSearchInTarget(), is(true));
          assertThat(
                  "new history token should reflect not to search in source when selected search field is target",
                  newToken.isProjectSearchInSource(), is(false));
      }
  
      @Test
      public void searchWithCaseSensitive() {
          boolean caseSensitiveTrueValue = true;
          expectRunSearch(searchPageHistoryToken(), caseSensitiveTrueValue,
                  TEST_SEARCH_PHRASE,
                  SearchResultsPresenter.Display.SEARCH_FIELD_TARGET);
  
          searchResultsPresenter.bind();
  
          verify(mockSearchButton).addClickHandler(
                  capturedSearchButtonClickHandler.capture());
  
          capturedSearchButtonClickHandler.getValue().onClick(clickEvent);
  
          verify(mockHistory).newItem(capturedHistoryToken.capture());
          HistoryToken newToken = capturedHistoryToken.getValue();
          assertThat(
                  "new history token project search case sensitivity should match checkbox value",
                  newToken.getProjectSearchCaseSensitive(),
                  is(caseSensitiveTrueValue));
      }
  
      @Test
      public void searchInSource() {
          expectRunSearch(searchPageHistoryToken(), false, TEST_SEARCH_PHRASE,
                  SearchResultsPresenter.Display.SEARCH_FIELD_SOURCE);
  
          searchResultsPresenter.bind();
          verify(mockSearchButton).addClickHandler(
                  capturedSearchButtonClickHandler.capture());
          capturedSearchButtonClickHandler.getValue().onClick(clickEvent);
  
          verify(mockHistory).newItem(capturedHistoryToken.capture());
  
          HistoryToken newToken = capturedHistoryToken.getValue();
          assertThat(
                  "new history token should reflect search in source when selected search field is source",
                  newToken.isProjectSearchInSource(), is(true));
          assertThat(
                  "new history token should reflect not to search in target when selected search field is source",
                  newToken.isProjectSearchInTarget(), is(false));
      }
  
      @Test
      public void searchInSourceAndTarget() {
          expectRunSearch(searchPageHistoryToken(), false, TEST_SEARCH_PHRASE,
                  SearchResultsPresenter.Display.SEARCH_FIELD_BOTH);
  
          searchResultsPresenter.bind();
          verify(mockSearchButton).addClickHandler(
                  capturedSearchButtonClickHandler.capture());
  
          capturedSearchButtonClickHandler.getValue().onClick(clickEvent);
  
          verify(mockHistory).newItem(capturedHistoryToken.capture());
  
          HistoryToken newToken = capturedHistoryToken.getValue();
          assertThat(
                  "new history token should reflect search in source when selected search field is both",
                  newToken.isProjectSearchInSource(), is(true));
          assertThat(
                  "new history token should reflect search in target when selected search field is both",
                  newToken.isProjectSearchInTarget(), is(true));
      }
  
      @Test
      public void replacementValueChanged() {
          when(mockHistory.getHistoryToken())
                  .thenReturn(searchPageHistoryToken());
  
          searchResultsPresenter.bind();
          verify(mockReplacementTextBox).addValueChangeHandler(
                  capturedReplacementTextBoxValueChangeHandler.capture());
  
          ValueChangeEvent<String> valueChangeEvent =
                  mock(ValueChangeEvent.class);
          when(valueChangeEvent.getValue()).thenReturn(TEST_REPLACE_PHRASE);
          capturedReplacementTextBoxValueChangeHandler.getValue().onValueChange(
                  valueChangeEvent);
  
          verify(mockHistory).newItem(capturedHistoryToken.capture());
  
  