@Test public void testOnInsertString() { // Given: selectedTU = currentPageRows.get(0); when(editor.getId()).thenReturn(selectedTU.getId()); when(display.getId()).thenReturn(selectedTU.getId()); when(display.getEditors()).thenReturn(Lists.newArrayList(editor)); when(tableEditorMessages.notifyCopied()).thenReturn("copied"); when(sourceContentPresenter.getSourceContent(selectedTU.getId())) .thenReturn(Optional.of("source content")); presenter.setStatesForTesting(selectedTU.getId(), 0, display); // When: presenter.onInsertString(new InsertStringInEditorEvent("", "suggestion")); // Then: verify(editor).insertTextInCursorPosition("suggestion"); verify(eventBus, atLeastOnce()).fireEvent(eventCaptor.capture()); NotificationEvent notificationEvent = TestFixture.extractFromEvents(eventCaptor.getAllValues(), NotificationEvent.class); MatcherAssert.assertThat(notificationEvent.getMessage(), Matchers.equalTo("copied")); RunValidationEvent runValidationEvent = TestFixture.extractFromEvents(eventCaptor.getAllValues(), RunValidationEvent.class); assertThat(runValidationEvent.getSourceContent(), equalTo("source content")); }
@Test public void willNotMoveToNextEntryIfTranslationHasError() { // Given: selected display and focus on first entry ValidationMessages messages = mock(ValidationMessages.class); Map<ValidationAction, List<String>> errorMessage = Maps.newHashMap(); errorMessage.put( new HtmlXmlTagValidation(ValidationId.HTML_XML, messages), new ArrayList<String>()); selectedTU = currentPageRows.get(0); when(display.getNewTargets()).thenReturn(NEW_TARGETS); when(display.getCachedTargets()).thenReturn(CACHED_TARGETS); when(display.getId()).thenReturn(selectedTU.getId()); when(display.getEditors()).thenReturn(Lists.newArrayList(editor)); when(display.getErrorMessages()).thenReturn(errorMessage); presenter.setStatesForTesting(selectedTU.getId(), 0, display); // When: presenter.saveAsApprovedAndMoveNext(selectedTU.getId()); // Then: verify(validationWarning) .center( selectedTU.getId(), userWorkspaceContext.getSelectedDoc(), NEW_TARGETS, errorMessage); }
@Test public void testShowEditorsInReadOnlyMode() { // Given: userWorkspaceContext.setProjectActive(false); userWorkspaceContext.setHasEditTranslationAccess(false); selectedTU = currentPageRows.get(0); ArrayList<ToggleEditor> currentEditors = Lists.newArrayList(editor); when(editor.getId()).thenReturn(selectedTU.getId()); ArrayList<ToggleEditor> previousEditors = Lists.newArrayList(editor2); presenter.setStatesForTesting(null, 0, display); when(display.getId()).thenReturn(selectedTU.getId()); when(display.getEditors()).thenReturn(previousEditors, currentEditors); when(sourceContentPresenter.getSourceContent(selectedTU.getId())) .thenReturn(Optional.of("source")); // When: presenter.setSelected(selectedTU.getId()); // Then: verify(editorTranslators).clearTranslatorList(previousEditors); verify(editor).clearTranslatorList(); verify(display).showButtons(false); verify(display).setToMode(ToggleEditor.ViewMode.VIEW); verify(editorKeyShortcuts).enableNavigationContext(); }
@Test public void willSaveAndMoveToNextRow() { // Given: selected display and there is only one entry(no plural or last entry of plural) selectedTU = currentPageRows.get(0); when(display.getNewTargets()).thenReturn(NEW_TARGETS); when(display.getCachedTargets()).thenReturn(CACHED_TARGETS); when(display.getId()).thenReturn(selectedTU.getId()); when(display.getEditors()).thenReturn(Lists.newArrayList(editor)); presenter.setStatesForTesting(selectedTU.getId(), 0, display); // When: presenter.saveAsApprovedAndMoveNext(selectedTU.getId()); // Then: verify(eventBus, atLeastOnce()).fireEvent(eventCaptor.capture()); TransUnitSaveEvent saveEvent = TestFixture.extractFromEvents(eventCaptor.getAllValues(), TransUnitSaveEvent.class); assertThat(saveEvent.getTransUnitId(), equalTo(selectedTU.getId())); assertThat(saveEvent.getTargets(), Matchers.equalTo(NEW_TARGETS)); assertThat(saveEvent.getStatus(), equalTo(ContentState.Translated)); NavTransUnitEvent navEvent = TestFixture.extractFromEvents(eventCaptor.getAllValues(), NavTransUnitEvent.class); assertThat(navEvent.getRowType(), equalTo(NavTransUnitEvent.NavigationType.NextEntry)); }
@Test public void canGetCurrentTransUnitId() { selectedTU = currentPageRows.get(0); presenter.setStatesForTesting(selectedTU.getId(), 0, display); TransUnitId result = presenter.getCurrentTransUnitIdOrNull(); assertThat(result, Matchers.sameInstance(selectedTU.getId())); }
@Override public void revertEditorContents() { List<String> cachedTargets = cachedValue.getTargets(); for (int i = 0; i < cachedTargets.size(); i++) { String target = cachedTargets.get(i); editors.get(i).setTextAndValidate(target); } editorGrid.setStyleName(resolveStyleName(cachedValue.getStatus())); }
private void prepareTransUnitUpdatedEvent( int previousVersionNum, ContentState previousState, HTextFlowTarget target) { LocaleId localeId = target.getLocaleId(); HTextFlow textFlow = target.getTextFlow(); HDocument document = textFlow.getDocument(); HProjectIteration projectIteration = document.getProjectIteration(); String iterationSlug = projectIteration.getSlug(); String projectSlug = projectIteration.getProject().getSlug(); ProjectType projectType = projectIteration.getProjectType(); WorkspaceId workspaceId = new WorkspaceId(new ProjectIterationId(projectSlug, iterationSlug, projectType), localeId); Optional<TranslationWorkspace> workspaceOptional = translationWorkspaceManager.tryGetWorkspace(workspaceId); if (!workspaceOptional.isPresent()) { return; } TransUnitTransformer transUnitTransformer = serviceLocator.getInstance(TransUnitTransformer.class); TransUnit transUnit = transUnitTransformer.transform(textFlow, target, target.getLocale()); DocumentId documentId = new DocumentId(document.getId(), document.getDocId()); int wordCount = textFlow.getWordCount().intValue(); TransUnitUpdateInfo updateInfo = createTransUnitUpdateInfo( transUnit, documentId, wordCount, previousVersionNum, previousState); CacheValue context = updateContext.getIfPresent(new CacheKey(transUnit.getId(), transUnit.getLocaleId())); TransUnitUpdated updated; if (context != null) { updated = new TransUnitUpdated(updateInfo, context.editorClientId, context.updateType); log.debug("about to publish trans unit updated event {}", updated); } else if (ServletContexts.instance().getRequest() != null) { String sessionId = ServletContexts.instance().getRequest().getSession().getId(); EditorClientId editorClientId = new EditorClientId(sessionId, -1); updated = new TransUnitUpdated( updateInfo, editorClientId, TransUnitUpdated.UpdateType.NonEditorSave); } else { updated = new TransUnitUpdated( updateInfo, new EditorClientId("unknown", -1), TransUnitUpdated.UpdateType.NonEditorSave); } if (Events.exists()) { Events.instance() .raiseTransactionSuccessEvent( TextFlowTargetUpdatedEvent.EVENT_NAME, new TextFlowTargetUpdatedEvent(workspaceOptional.get(), target.getId(), updated)); } }
@Test public void canUpdateRowIfInCurrentDisplays() { selectedTU = currentPageRows.get(2); when(display.getId()).thenReturn(selectedTU.getId()); TransUnit updatedTransUnit = TestFixture.makeTransUnit(selectedTU.getId().getId()); presenter.updateRow(updatedTransUnit); verify(display).setValueAndCreateNewEditors(updatedTransUnit); verify(display).refresh(); }
@Test public void canUpdateToOtherStateIfCachedTargetsAndInEditorTargetsAreNotEqual() { selectedTU = currentPageRows.get(0); when(display.getId()).thenReturn(selectedTU.getId()); when(display.getCachedTargets()).thenReturn(Lists.newArrayList("a")); when(display.getNewTargets()).thenReturn(Lists.newArrayList("b")); presenter.setEditingState(selectedTU.getId(), TargetContentsDisplay.EditingState.SAVING); verify(display).setState(TargetContentsDisplay.EditingState.SAVING); }
@Test public void willIgnoreRejectIfItsAlreadyRejectedState() { selectedTU = currentPageRows.get(1); when(display.getId()).thenReturn(selectedTU.getId()); when(display.getCachedState()).thenReturn(ContentState.Rejected); presenter.setStatesForTesting(selectedTU.getId(), 0, display); presenter.rejectTranslation(selectedTU.getId()); verifyZeroInteractions(eventBus); }
@Test public void canShowHistory() { // Given: selectedTU = currentPageRows.get(0); presenter.setStatesForTesting(selectedTU.getId(), 0, display); // When: presenter.showHistory(selectedTU.getId()); // Then: verify(historyPresenter).showTranslationHistory(selectedTU.getId()); }
@Test public void canUpdateRowIfInCurrentDisplays() { selectedTU = currentPageRows.get(2); when(display.getId()).thenReturn(selectedTU.getId()); TransUnit updatedTransUnit = TestFixture.makeTransUnit(selectedTU.getId().getId()); presenter.updateRow(updatedTransUnit); InOrder inOrder = Mockito.inOrder(display); inOrder.verify(display).setValueAndCreateNewEditors(updatedTransUnit); inOrder.verify(display).setState(TargetContentsDisplay.EditingState.SAVED); inOrder.verify(display).refresh(); }
@Test public void testOnFocus() { selectedTU = currentPageRows.get(0); TransUnitId oldSelection = currentPageRows.get(1).getId(); presenter.setStatesForTesting(oldSelection, 0, display); presenter.onEditorClicked(selectedTU.getId(), 1); verify(eventBus).fireEvent(eventCaptor.capture()); TableRowSelectedEvent tableRowSelectedEvent = TestFixture.extractFromEvents(eventCaptor.getAllValues(), TableRowSelectedEvent.class); assertThat(tableRowSelectedEvent.getSelectedId(), Matchers.equalTo(selectedTU.getId())); }
@Test public void willMoveToNextEntryIfItIsPluralAndNotAtLastEntry() { // Given: selected display and focus on first entry selectedTU = currentPageRows.get(0); when(display.getId()).thenReturn(selectedTU.getId()); when(display.getEditors()).thenReturn(Lists.newArrayList(editor, editor2)); presenter.setStatesForTesting(selectedTU.getId(), 0, display); // When: presenter.saveAsApprovedAndMoveNext(selectedTU.getId()); // Then: verify(display).focusEditor(1); }
@Test public void rejectTranslationWillForceComment() { selectedTU = currentPageRows.get(1); when(display.getId()).thenReturn(selectedTU.getId()); presenter.setStatesForTesting(selectedTU.getId(), 0, display); presenter.rejectTranslation(selectedTU.getId()); verify(eventBus).fireEvent(eventCaptor.capture()); CommentBeforeSaveEvent commentBeforeSaveEvent = extractFromEvents(eventCaptor.getAllValues(), CommentBeforeSaveEvent.class); assertThat( commentBeforeSaveEvent.getSaveEvent().getStatus(), Matchers.equalTo(ContentState.Rejected)); }
@Test public void canConfirmSavedOnSavedState() { selectedTU = currentPageRows.get(1); when(display.getId()).thenReturn(selectedTU.getId()); when(display.getEditingState()).thenReturn(TargetContentsDisplay.EditingState.SAVED); presenter.setStatesForTesting(selectedTU.getId(), 0, display); presenter.confirmSaved(selectedTU); InOrder inOrder = Mockito.inOrder(display); inOrder.verify(display).setValueAndCreateNewEditors(selectedTU); inOrder.verify(display).refresh(); inOrder.verify(display).setState(TargetContentsDisplay.EditingState.SAVED); }
@Test public void canConfirmSavedOnSavingState() { selectedTU = currentPageRows.get(1); when(display.getId()).thenReturn(selectedTU.getId()); when(display.getEditingState()).thenReturn(TargetContentsDisplay.EditingState.SAVING); presenter.setStatesForTesting(selectedTU.getId(), 0, display); presenter.confirmSaved(selectedTU); verify(display) .updateCachedTargetsAndVersion( selectedTU.getTargets(), selectedTU.getVerNum(), selectedTU.getStatus()); verify(display).setState(TargetContentsDisplay.EditingState.SAVED); }
@Test public void canCopySource() { // Given: selected one trans unit selectedTU = currentPageRows.get(0); when(display.getId()).thenReturn(selectedTU.getId()); when(display.getEditors()).thenReturn(Lists.newArrayList(editor)); presenter.setStatesForTesting(selectedTU.getId(), 0, display); when(sourceContentPresenter.getSelectedSource()).thenReturn("source"); presenter.copySource(editor, selectedTU.getId()); verify(editor).setTextAndValidate("source"); verify(editor).setFocus(); verify(eventBus).fireEvent(isA(NotificationEvent.class)); }
@Test public void canCopySourceForActiveRow() { // Given: current editor is focused TargetContentsPresenter spyPresenter = spy(presenter); selectedTU = currentPageRows.get(0); spyPresenter.setStatesForTesting(selectedTU.getId(), 0, display); doNothing().when(spyPresenter).copySource(editor, selectedTU.getId()); when(editor.isFocused()).thenReturn(true); when(display.getEditors()).thenReturn(Lists.newArrayList(editor)); // When: spyPresenter.copySourceForActiveRow(); // Then: verify(spyPresenter).copySource(editor, selectedTU.getId()); }
@Test public void onTransUnitEditEvent() { selectedTU = currentPageRows.get(0); TransUnitEditEvent event = mock(TransUnitEditEvent.class); when(event.getSelectedTransUnitId()).thenReturn(selectedTU.getId()); ArrayList<ToggleEditor> currentEditors = Lists.newArrayList(editor); presenter.setStatesForTesting(selectedTU.getId(), 0, display); when(display.getEditors()).thenReturn(currentEditors); presenter.onTransUnitEdit(event); InOrder inOrder = inOrder(editorTranslators); inOrder.verify(editorTranslators).clearTranslatorList(currentEditors); inOrder.verify(editorTranslators).updateTranslator(currentEditors, selectedTU.getId()); verifyNoMoreInteractions(editorTranslators); }
@Test public void testOnTransMemoryCopy() { when(tableEditorMessages.notifyCopied()).thenReturn("copied"); selectedTU = currentPageRows.get(0); when(display.getId()).thenReturn(selectedTU.getId()); when(display.getEditors()).thenReturn(Lists.newArrayList(editor)); presenter.setStatesForTesting(selectedTU.getId(), 0, display); presenter.onDataCopy(new CopyDataToEditorEvent(Arrays.asList("target"))); verify(editor).setTextAndValidate("target"); verify(eventBus, atLeastOnce()).fireEvent(eventCaptor.capture()); NotificationEvent notificationEvent = TestFixture.extractFromEvents(eventCaptor.getAllValues(), NotificationEvent.class); MatcherAssert.assertThat(notificationEvent.getMessage(), Matchers.equalTo("copied")); }
private void initData() { // @formatter:off data = Lists.newArrayList( TestFixture.makeTransUnit(1, ContentState.Approved), TestFixture.makeTransUnit(2, ContentState.Approved), TestFixture.makeTransUnit(3, ContentState.NeedReview), TestFixture.makeTransUnit(4, ContentState.New), TestFixture.makeTransUnit(5, ContentState.Approved)); // @formatter:on idStateMap = Maps.newHashMap(); idIndexList = Lists.newArrayList(); for (TransUnit transUnit : data) { idStateMap.put(transUnit.getId(), transUnit.getStatus()); idIndexList.add(transUnit.getId()); } }
@Test public void testSetFocus() { presenter.setStatesForTesting(selectedTU.getId(), 99, display); presenter.setFocus(); // current editor index will be corrected if out of bound verify(display).focusEditor(0); }
@Test public void onCancelCanResetTextBack() { // Given: selectedTU = currentPageRows.get(0); when(display.getCachedTargets()).thenReturn(CACHED_TARGETS); when(display.getId()).thenReturn(selectedTU.getId()); ArrayList<ToggleEditor> currentEditors = Lists.newArrayList(editor); when(display.getEditors()).thenReturn(currentEditors); presenter.setStatesForTesting(selectedTU.getId(), 0, display); // When: presenter.onCancel(selectedTU.getId()); // Then: verify(display).revertEditorContents(); verify(display).highlightSearch(anyString()); verify(display).focusEditor(0); }
@Test public void canSavePendingChangesIfContentHasChanged() { // Given: selectedTU = currentPageRows.get(0); presenter.setStatesForTesting(selectedTU.getId(), 0, display); when(display.getCachedTargets()).thenReturn(CACHED_TARGETS); when(display.getNewTargets()).thenReturn(NEW_TARGETS); when(display.getId()).thenReturn(selectedTU.getId()); when(display.getVerNum()).thenReturn(99); // When: presenter.savePendingChangesIfApplicable(); // Then: verify(eventBus) .fireEvent( new TransUnitSaveEvent( NEW_TARGETS, ContentState.Approved, selectedTU.getId(), 99, CACHED_TARGETS)); }
@Test public void canGetNewTargets() { selectedTU = currentPageRows.get(0); when(display.getNewTargets()).thenReturn(NEW_TARGETS); presenter.setStatesForTesting(selectedTU.getId(), 0, display); List<String> result = presenter.getNewTargets(); MatcherAssert.assertThat(result, Matchers.sameInstance(NEW_TARGETS)); }
@Test public void willIgnoreIfUpdateRowWithValueNotInCurrentPage() { selectedTU = currentPageRows.get(2); when(display.getId()).thenReturn(selectedTU.getId()); TransUnit updatedTransUnit = TestFixture.makeTransUnit(99); presenter.updateRow(updatedTransUnit); verify(display, never()).setValueAndCreateNewEditors(updatedTransUnit); }
@Test public void onRequestValidationWillFireRunValidationEvent() { // given current display has one editor and current editor has target content selectedTU = currentPageRows.get(0); presenter.setStatesForTesting(selectedTU.getId(), 0, display); when(display.getId()).thenReturn(selectedTU.getId()); when(editor.getId()).thenReturn(selectedTU.getId()); when(display.getEditors()).thenReturn(Lists.newArrayList(editor)); when(sourceContentPresenter.getCurrentTransUnitIdOrNull()).thenReturn(selectedTU.getId()); when(sourceContentPresenter.getSourceContent(selectedTU.getId())) .thenReturn(Optional.of("source")); when(editor.getText()).thenReturn("target"); presenter.onRequestValidation(RequestValidationEvent.EVENT); verify(eventBus).fireEvent(eventCaptor.capture()); RunValidationEvent event = TestFixture.extractFromEvents(eventCaptor.getAllValues(), RunValidationEvent.class); MatcherAssert.assertThat(event.getTarget(), Matchers.equalTo("target")); }
/** * Display confirmation dialog box if new status of TU has been changed to approved without any * content changes. */ @Override public void onCheckStateHasChanged(CheckStateHasChangedEvent event) { TransUnit transUnit = navigationService.getByIdOrNull(event.getTransUnitId()); if (transUnit == null) { return; } boolean targetChanged = !Objects.equal(transUnit.getTargets(), event.getTargets()); boolean targetUnchangedButCanSaveAsApproved = (event.getAdjustedState() == ContentState.Translated) && !Objects.equal(transUnit.getStatus(), event.getAdjustedState()); if (targetChanged) { targetContentsPresenter.saveAsApprovedAndMoveNext(event.getTransUnitId()); } else if (targetUnchangedButCanSaveAsApproved) { targetContentsPresenter.showSaveAsApprovedConfirmation(event.getTransUnitId()); } else { eventBus.fireEvent(NavTransUnitEvent.NEXT_ENTRY_EVENT); } }
@Override public void updateCachedTargetsAndVersion( List<String> targets, Integer verNum, ContentState status) { cachedValue = TransUnit.Builder.from(cachedValue) .setTargets(targets) .setVerNum(verNum) .setStatus(status) .build(); editorGrid.setStyleName(resolveStyleName(cachedValue.getStatus())); }