This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
/
TranslationHistoryPresenterTest.java
266 lines (225 loc) · 10.6 KB
/
TranslationHistoryPresenterTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package org.zanata.webtrans.client.presenter;
import java.util.Date;
import java.util.List;
import org.hamcrest.Matchers;
import org.mockito.Answers;
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.common.ContentState;
import org.zanata.webtrans.client.events.CopyDataToEditorEvent;
import org.zanata.webtrans.client.events.KeyShortcutEvent;
import org.zanata.webtrans.client.events.NotificationEvent;
import org.zanata.webtrans.client.events.ReviewCommentEvent;
import org.zanata.webtrans.client.keys.KeyShortcut;
import org.zanata.webtrans.client.keys.Keys;
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.TranslationHistoryDisplay;
import org.zanata.webtrans.shared.model.ComparableByDate;
import org.zanata.webtrans.shared.model.DocumentId;
import org.zanata.webtrans.shared.model.ReviewComment;
import org.zanata.webtrans.shared.model.ReviewCommentId;
import org.zanata.webtrans.shared.model.TransHistoryItem;
import org.zanata.webtrans.shared.model.TransUnitId;
import org.zanata.webtrans.shared.rpc.AddReviewCommentAction;
import org.zanata.webtrans.shared.rpc.AddReviewCommentResult;
import org.zanata.webtrans.shared.rpc.GetTranslationHistoryAction;
import org.zanata.webtrans.shared.rpc.GetTranslationHistoryResult;
import com.google.common.collect.Lists;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.user.cellview.client.ColumnSortEvent;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.view.client.SelectionChangeEvent;
import net.customware.gwt.presenter.client.EventBus;
import static org.hamcrest.MatcherAssert.assertThat;
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;
/**
* @author Patrick Huang <a href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
@Test(groups = "unit-tests")
public class TranslationHistoryPresenterTest
{
private TranslationHistoryPresenter presenter;
@Mock
private TranslationHistoryDisplay display;
@Mock
private EventBus eventBus;
@Mock
private CachingDispatchAsync dispatcher;
@Mock
private WebTransMessages messages;
@Mock
private TargetContentsPresenter targetContentsPresenter;
@Mock
private SelectionChangeEvent selectionChangeEvent;
@Captor
private ArgumentCaptor<ColumnSortEvent.ListHandler<TransHistoryItem>> sortHandlerCaptor;
@Captor
private ArgumentCaptor<GetTranslationHistoryAction> actionCaptor;
@Captor
private ArgumentCaptor<AsyncCallback<GetTranslationHistoryResult>> resultCaptor;
private final TransUnitId transUnitId = new TransUnitId(1L);
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private GetTransUnitActionContextHolder contextHolder;
@Mock
private KeyShortcutPresenter keyShortcutPresenter;
@Captor
private ArgumentCaptor<KeyShortcut> keyShortcutCapture;
@BeforeMethod
public void beforeMethod()
{
MockitoAnnotations.initMocks(this);
presenter = new TranslationHistoryPresenter(display, eventBus, dispatcher, messages, contextHolder, keyShortcutPresenter);
presenter.setCurrentValueHolder(targetContentsPresenter);
doNothing().when(dispatcher).execute(actionCaptor.capture(), resultCaptor.capture());
verify(eventBus).addHandler(ReviewCommentEvent.TYPE, presenter);
verify(keyShortcutPresenter).register(keyShortcutCapture.capture());
}
private static TransHistoryItem historyItem(String versionNum)
{
return new TransHistoryItem(versionNum, Lists.newArrayList("a"), ContentState.Approved, "admin", new Date());
}
@Test
public void willNotifyErrorAndHideTranslationHistoryOnFailure()
{
// Given:
when(messages.translationHistory()).thenReturn("translation history");
// When: request history for trans unit id 1
presenter.showTranslationHistory(transUnitId);
// Then:
verify(display).setTitle("translation history");
verify(display).resetView();
verify(display).center();
assertThat(actionCaptor.getValue().getTransUnitId(), Matchers.equalTo(transUnitId));
// And on failure
AsyncCallback<GetTranslationHistoryResult> result = resultCaptor.getValue();
result.onFailure(new RuntimeException());
verify(eventBus).fireEvent(isA(NotificationEvent.class));
verify(display).hide();
}
@Test
public void willShowTranslationHistoryOnSuccess()
{
// Given: text flow has one history item and one latest translation
when(messages.translationHistory()).thenReturn("translation history");
TransHistoryItem historyItem = historyItem("1");
String latestVersion = "2";
TransHistoryItem latest = historyItem(latestVersion);
// latest contents and current contents are equal
when(targetContentsPresenter.getNewTargets()).thenReturn(Lists.newArrayList(latest.getContents()));
when(messages.latest()).thenReturn("latest");
// When: request history for trans unit id 1
presenter.showTranslationHistory(transUnitId);
// Then:on success
verify(display).setTitle("translation history");
verify(display).resetView();
verify(display).center();
AsyncCallback<GetTranslationHistoryResult> result = resultCaptor.getValue();
result.onSuccess(createTranslationHistory(latest, historyItem));
verify(display).setData(Lists.<ComparableByDate>newArrayList(latest, historyItem));
}
@Test
public void willShowTranslationHistoryWithUnsavedValueOnSuccess()
{
// Given: text flow has one history item and one latest translation
TransHistoryItem historyItem = historyItem("1");
String latestVersion = "2";
TransHistoryItem latest = historyItem(latestVersion);
// latest contents and current contents are NOT equal
when(targetContentsPresenter.getNewTargets()).thenReturn(Lists.newArrayList("b"));
when(messages.latest()).thenReturn("latest");
when(messages.unsaved()).thenReturn("unsaved");
// When: request history for trans unit id 1
presenter.showTranslationHistory(transUnitId);
// Then: on success
AsyncCallback<GetTranslationHistoryResult> result = resultCaptor.getValue();
result.onSuccess(createTranslationHistory(latest, historyItem));
ArgumentCaptor<List> listArgumentCaptor = ArgumentCaptor.forClass(List.class);
verify(display).setData(listArgumentCaptor.capture());
assertThat((List<ComparableByDate>) listArgumentCaptor.getValue(), Matchers.<ComparableByDate>hasSize(3));
}
private static GetTranslationHistoryResult createTranslationHistory(TransHistoryItem latest, TransHistoryItem... historyItems)
{
return new GetTranslationHistoryResult(Lists.newArrayList(historyItems), latest, Lists.<ReviewComment>newArrayList());
}
@Test
public void canCopyIntoEditor()
{
List<String> contents = Lists.newArrayList("a");
presenter.copyIntoEditor(contents);
ArgumentCaptor<CopyDataToEditorEvent> eventCaptor = ArgumentCaptor.forClass(CopyDataToEditorEvent.class);
verify(eventBus).fireEvent(eventCaptor.capture());
assertThat(eventCaptor.getValue().getTargetResult(), Matchers.equalTo(contents));
}
@Test
public void testAddComment() throws Exception
{
when(contextHolder.getContext().getDocument().getId()).thenReturn(new DocumentId(1L, "doc"));
ArgumentCaptor<AddReviewCommentAction> actionCaptor = ArgumentCaptor.forClass(AddReviewCommentAction.class);
ArgumentCaptor<AsyncCallback> resultCaptor = ArgumentCaptor.forClass(AsyncCallback.class);
presenter.addComment("some comment");
verify(dispatcher).execute(actionCaptor.capture(), resultCaptor.capture());
assertThat(actionCaptor.getValue().getContent(), Matchers.equalTo("some comment"));
AsyncCallback<AddReviewCommentResult> callback = resultCaptor.getValue();
AddReviewCommentResult result = new AddReviewCommentResult(new ReviewComment());
callback.onSuccess(result);
verify(display).addCommentToList(result.getComment());
verify(display).clearInput();
}
@Test
public void canDisplayEntriesInOrder()
{
// no unsaved content
when(targetContentsPresenter.getNewTargets()).thenReturn(Lists.newArrayList("a"));
long now = new Date().getTime();
// items in time order
TransHistoryItem latest = new TransHistoryItem("5", Lists.newArrayList("a"), ContentState.Approved, "admin",
new Date(now - 1000));
TransHistoryItem item = new TransHistoryItem("4", Lists.newArrayList("a"), ContentState.Approved, "admin",
new Date(now - 2000));
ReviewComment comment = new ReviewComment(new ReviewCommentId(1L), "comment", "admin", new Date(now), 5);
presenter.displayEntries(latest, Lists.newArrayList(item), Lists.newArrayList(comment));
ArgumentCaptor<List> listArgumentCaptor = ArgumentCaptor.forClass(List.class);
verify(display).setData(listArgumentCaptor.capture());
List<ComparableByDate> result = (List<ComparableByDate>) listArgumentCaptor.getValue();
assertThat(result, Matchers.<ComparableByDate>contains(comment, latest, item));
}
@Test
public void onCompareClickedWhenThePairIsNotFull()
{
// the pair is empty initially
presenter.compareClicked(historyItem("5"));
verify(display).disableComparison();
}
@Test
public void onCompareClickedWhichMakesTwoItems()
{
when(messages.translationHistoryComparison("5", "4")).thenReturn("comparison of 5 and 4");
TransHistoryItem one = historyItem("5");
presenter.compareClicked(one);
verify(display).disableComparison();
TransHistoryItem two = historyItem("4");
presenter.compareClicked(two);
verify(display).showDiff(one, two, "comparison of 5 and 4");
}
@Test
public void testKeyShortcutForAddComment()
{
when(display.getComment()).thenReturn("blah");
KeyShortcut keyShortcut = keyShortcutCapture.getValue();
assertThat(keyShortcut.getAllKeys().iterator().next(), Matchers.equalTo(new Keys(Keys.CTRL_KEY, KeyCodes.KEY_ENTER)));
keyShortcut.getHandler().onKeyShortcut(mock(KeyShortcutEvent.class));
verify(display).getComment();
verify(dispatcher).execute(Mockito.isA(AddReviewCommentAction.class), Mockito.isA(AsyncCallback.class));
}
}