This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
/
AppPresenterTest.java
780 lines (643 loc) · 32 KB
/
AppPresenterTest.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
package org.zanata.webtrans.client.presenter;
import static org.easymock.EasyMock.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import net.customware.gwt.presenter.client.EventBus;
import org.easymock.Capture;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.zanata.common.LocaleId;
import org.zanata.common.TransUnitCount;
import org.zanata.common.TransUnitWords;
import org.zanata.common.TranslationStats;
import org.zanata.webtrans.client.events.DocumentSelectionEvent;
import org.zanata.webtrans.client.events.DocumentStatsUpdatedEvent;
import org.zanata.webtrans.client.events.DocumentStatsUpdatedEventHandler;
import org.zanata.webtrans.client.events.NotificationEvent;
import org.zanata.webtrans.client.events.NotificationEvent.Severity;
import org.zanata.webtrans.client.events.NotificationEventHandler;
import org.zanata.webtrans.client.events.ProjectStatsUpdatedEvent;
import org.zanata.webtrans.client.events.ProjectStatsUpdatedEventHandler;
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;
import org.zanata.webtrans.client.history.Window.Location;
import org.zanata.webtrans.client.presenter.AppPresenter.Display;
import org.zanata.webtrans.client.resources.WebTransMessages;
import org.zanata.webtrans.shared.auth.Identity;
import org.zanata.webtrans.shared.model.DocumentId;
import org.zanata.webtrans.shared.model.DocumentInfo;
import org.zanata.webtrans.shared.model.Person;
import org.zanata.webtrans.shared.model.WorkspaceContext;
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.ui.HasVisibility;
@Test(groups = { "unit-tests" })
public class AppPresenterTest
{
private static final String TEST_PERSON_NAME = "Mister Ed";
private static final String TEST_WORKSPACE_NAME = "Test Workspace Name";
private static final String TEST_LOCALE_NAME = "Test Locale Name";
private static final String TEST_WINDOW_TITLE = "Test Window Title";
private static final String WORKSPACE_TITLE_QUERY_PARAMETER_KEY = "title";
private static final String NO_DOCUMENTS_STRING = "No document selected";
private static final String TEST_DOCUMENT_NAME = "test_document_name";
private static final String TEST_DOCUMENT_PATH = "test/document/path/";
private static final String TEST_WORKSPACE_TITLE = "test workspace title";
private AppPresenter appPresenter;
HasClickHandlers mockDismiss;
HasVisibility mockDismissVisibility;
Display mockDisplay;
DocumentListPresenter mockDocumentListPresenter;
HasClickHandlers mockDocumentsLink;
EventBus mockEventBus;
History mockHistory;
Identity mockIdentity;
HasClickHandlers mockLeaveWorkspaceLink;
WebTransMessages mockMessages;
Person mockPerson;
HasClickHandlers mockSearchLink;
SearchResultsPresenter mockSearchResultsPresenter;
HasClickHandlers mockSignoutLink;
TranslationPresenter mockTranslationPresenter;
Window mockWindow;
Location mockWindowLocation;
WorkspaceContext mockWorkspaceContext;
private Capture<ClickHandler> capturedDismissLinkClickHandler;
private Capture<ClickHandler> capturedDocumentLinkClickHandler;
private Capture<DocumentSelectionEvent> capturedDocumentSelectionEvent;
private Capture<DocumentStatsUpdatedEventHandler> capturedDocumentStatsUpdatedEventHandler;
private Capture<String> capturedHistoryTokenString;
private Capture<ValueChangeHandler<String>> capturedHistoryValueChangeHandler;
private Capture<ClickHandler> capturedLeaveWorkspaceLinkClickHandler;
private Capture<NotificationEventHandler> capturedNotificationEventHandler;
private Capture<ProjectStatsUpdatedEventHandler> capturedProjectStatsUpdatedEventHandler;
private Capture<ClickHandler> capturedSearchLinkClickHandler;
private Capture<ClickHandler> capturedSignoutLinkClickHandler;
private Capture<WorkspaceContextUpdateEventHandler> capturedWorkspaceContextUpdatedEventHandler;
private DocumentInfo testDocInfo;
private DocumentId testDocId;
private TranslationStats testDocStats;
private TranslationStats emptyProjectStats;
@BeforeClass
public void createMocks()
{
mockDismiss = createMock(HasClickHandlers.class);
mockDismissVisibility = createMock(HasVisibility.class);
mockDisplay = createMock(AppPresenter.Display.class);
mockDocumentListPresenter = createMock(DocumentListPresenter.class);
mockDocumentsLink = createMock(HasClickHandlers.class);
mockEventBus = createMock(EventBus.class);
mockHistory = createMock(History.class);
mockIdentity = createMock(Identity.class);
mockLeaveWorkspaceLink = createMock(HasClickHandlers.class);
mockMessages = createMock(WebTransMessages.class);
mockPerson = createMock(Person.class);
mockSearchLink = createMock(HasClickHandlers.class);
mockSearchResultsPresenter = createMock(SearchResultsPresenter.class);
mockSignoutLink = createMock(HasClickHandlers.class);
mockTranslationPresenter = createMock(TranslationPresenter.class);
mockWindow = createMock(Window.class);
mockWindowLocation = createMock(Window.Location.class);
mockWorkspaceContext = createMock(WorkspaceContext.class);
capturedDismissLinkClickHandler = new Capture<ClickHandler>();
capturedDocumentLinkClickHandler = new Capture<ClickHandler>();
capturedDocumentSelectionEvent = new Capture<DocumentSelectionEvent>();
capturedDocumentStatsUpdatedEventHandler = new Capture<DocumentStatsUpdatedEventHandler>();
capturedHistoryTokenString = new Capture<String>();
capturedHistoryValueChangeHandler = new Capture<ValueChangeHandler<String>>();
capturedLeaveWorkspaceLinkClickHandler = new Capture<ClickHandler>();
capturedNotificationEventHandler = new Capture<NotificationEventHandler>();
capturedProjectStatsUpdatedEventHandler = new Capture<ProjectStatsUpdatedEventHandler>();
capturedSearchLinkClickHandler = new Capture<ClickHandler>();
capturedSignoutLinkClickHandler = new Capture<ClickHandler>();
capturedWorkspaceContextUpdatedEventHandler = new Capture<WorkspaceContextUpdateEventHandler>();
}
@BeforeMethod
void beforeMethod()
{
resetAllMocks();
resetAllCaptures();
}
private AppPresenter newAppPresenter()
{
return new AppPresenter(mockDisplay, mockEventBus, mockTranslationPresenter, mockDocumentListPresenter, mockSearchResultsPresenter, mockIdentity, mockWorkspaceContext, mockMessages, mockHistory, mockWindow, mockWindowLocation);
}
// Note: unable to test 'sign out' and 'close window' links as these have
// static method calls to Application
// TODO test that initial history state is handled properly
public void testPerformsRequiredActionsOnBind()
{
setupDefaultMockExpectations();
// default mock expectations include:
// - bind doclistpresenter
// - bind translationpresenter
// - show documents view initially
// - set user label
// - set workspace name + title
// - set window title
// - show 'No document selected'
// - show (intitially empty) project stats
replayAllMocks();
appPresenter = newAppPresenter();
appPresenter.bind();
verifyAllMocks();
}
public void testShowsNotificationEvents()
{
setupAndBindAppPresenter();
verifyAllMocks();
reset(mockDisplay);
String testMessage = "test notification message";
mockDisplay.setNotificationMessage(testMessage, Severity.Warning);
expectLastCall().once();
expect(mockDisplay.getDismissVisibility()).andReturn(mockDismissVisibility).anyTimes();
replay(mockDisplay);
reset(mockDismissVisibility);
mockDismissVisibility.setVisible(true);
expectLastCall().once();
replay(mockDismissVisibility);
NotificationEvent notification = new NotificationEvent(Severity.Warning, testMessage);
capturedNotificationEventHandler.getValue().onNotification(notification);
verify(mockDisplay);
verify(mockDismissVisibility);
}
public void testShowsUpdatedProjectStats()
{
setupAndBindAppPresenter();
verifyAllMocks();
reset(mockDisplay);
Capture<TranslationStats> capturedTranslationStats = new Capture<TranslationStats>();
mockDisplay.setStats(and(capture(capturedTranslationStats), isA(TranslationStats.class)));
expectLastCall().once();
replay(mockDisplay);
TranslationStats testStats = new TranslationStats(new TransUnitCount(6, 5, 4), new TransUnitWords(3, 2, 1));
ProjectStatsUpdatedEvent event = new ProjectStatsUpdatedEvent(testStats);
capturedProjectStatsUpdatedEventHandler.getValue().onProjectStatsRetrieved(event);
verify(mockDisplay);
assertThat(capturedTranslationStats.getValue(), is(equalTo(testStats)));
}
public void testUpdateProjectStatsFromEditorView()
{
setupAndBindAppPresenter();
verifyAllMocks();
HistoryToken token = loadDocAndViewEditor();
reset(mockDisplay);
// not expecting stats change yet
replay(mockDisplay);
TranslationStats updatedStats = new TranslationStats(new TransUnitCount(9, 9, 9), new TransUnitWords(9, 9, 9));
capturedProjectStatsUpdatedEventHandler.getValue().onProjectStatsRetrieved(new ProjectStatsUpdatedEvent(updatedStats));
verify(mockDisplay);
returnToDoclistView(token, updatedStats);
}
public void testHistoryTriggersDocumentSelectionEvent()
{
setupAndBindAppPresenter();
verifyAllMocks();
reset(mockDisplay, mockDocumentListPresenter);
// not testing for specific values for the following in this test
mockDisplay.setDocumentLabel(notNull(String.class), notNull(String.class));
expectLastCall().anyTimes();
mockDisplay.setStats(notNull(TranslationStats.class));
expectLastCall().anyTimes();
buildTestDocumentInfo();
expect(mockDocumentListPresenter.getDocumentId(TEST_DOCUMENT_PATH + TEST_DOCUMENT_NAME)).andReturn(testDocId).anyTimes();
expect(mockDocumentListPresenter.getDocumentInfo(testDocId)).andReturn(testDocInfo).anyTimes();
replay(mockDisplay, mockDocumentListPresenter);
HistoryToken docSelectionToken = new HistoryToken();
docSelectionToken.setDocumentPath(TEST_DOCUMENT_PATH + TEST_DOCUMENT_NAME);
capturedHistoryValueChangeHandler.getValue().onValueChange(new ValueChangeEvent<String>(docSelectionToken.toTokenString())
{
});
assertThat("a new document path in history should trigger a document selection event with the correct id", capturedDocumentSelectionEvent.getValue().getDocumentId(), is(testDocId));
}
public void testHistoryTriggersViewChange()
{
setupAndBindAppPresenter();
verifyAllMocks();
reset(mockDisplay, mockDocumentListPresenter);
buildTestDocumentInfo();
expect(mockDocumentListPresenter.getDocumentId(TEST_DOCUMENT_PATH + TEST_DOCUMENT_NAME)).andReturn(testDocId).anyTimes();
expect(mockDocumentListPresenter.getDocumentInfo(testDocId)).andReturn(testDocInfo).anyTimes();
mockDisplay.showInMainView(MainView.Editor);
expectLastCall().once();
// avoid checking name or stats for this test
mockDisplay.setDocumentLabel(notNull(String.class), notNull(String.class));
expectLastCall().anyTimes();
mockDisplay.setStats(notNull(TranslationStats.class));
expectLastCall().anyTimes();
replay(mockDisplay, mockDocumentListPresenter);
HistoryToken docInEditorToken = new HistoryToken();
// requires valid selected document to change to editor view
docInEditorToken.setDocumentPath(TEST_DOCUMENT_PATH + TEST_DOCUMENT_NAME);
docInEditorToken.setView(MainView.Editor);
capturedHistoryValueChangeHandler.getValue().onValueChange(new ValueChangeEvent<String>(docInEditorToken.toTokenString())
{
});
verify(mockDisplay);
}
public void testNoEditorWithoutValidDocument()
{
setupAndBindAppPresenter();
verifyAllMocks();
reset(mockDisplay, mockDocumentListPresenter);
// return invalid document
expect(mockDocumentListPresenter.getDocumentId(notNull(String.class))).andReturn(null).anyTimes();
replay(mockDisplay, mockDocumentListPresenter);
HistoryToken editorWithoutDocToken = new HistoryToken();
editorWithoutDocToken.setView(MainView.Editor);
capturedHistoryValueChangeHandler.getValue().onValueChange(new ValueChangeEvent<String>(editorWithoutDocToken.toTokenString())
{
});
// not expecting show view editor
verify(mockDisplay);
}
public void testHistoryTriggersDocumentNameStatsUpdate()
{
setupAndBindAppPresenter();
verifyAllMocks();
reset(mockDisplay, mockDocumentListPresenter);
buildTestDocumentInfo();
expect(mockDocumentListPresenter.getDocumentId(TEST_DOCUMENT_PATH + TEST_DOCUMENT_NAME)).andReturn(testDocId).anyTimes();
expect(mockDocumentListPresenter.getDocumentInfo(testDocId)).andReturn(testDocInfo).anyTimes();
// avoid checking for view change, tested elsewhere
mockDisplay.showInMainView(isA(MainView.class));
expectLastCall().anyTimes();
mockDisplay.setDocumentLabel(TEST_DOCUMENT_PATH, TEST_DOCUMENT_NAME);
expectLastCall().once();
mockDisplay.setStats(eq(testDocStats));
expectLastCall().once();
replay(mockDisplay, mockDocumentListPresenter);
HistoryToken docInEditorToken = new HistoryToken();
docInEditorToken.setView(MainView.Editor);
docInEditorToken.setDocumentPath(TEST_DOCUMENT_PATH + TEST_DOCUMENT_NAME);
capturedHistoryValueChangeHandler.getValue().onValueChange(new ValueChangeEvent<String>(docInEditorToken.toTokenString())
{
});
verify(mockDisplay);
}
/**
* Note: this also verifies that editor pending change is saved when changing
* from editor to document list
*/
public void testStatsAndNameChangeWithView()
{
setupAndBindAppPresenter();
verifyAllMocks();
HistoryToken token = loadDocAndViewEditor();
token = returnToDoclistView(token, emptyProjectStats);
returnToEditorView(token, testDocStats);
}
public void testShowsUpdatedDocumentStats()
{
setupAndBindAppPresenter();
verifyAllMocks();
// must be in editor to see document stats
loadDocAndViewEditor();
TranslationStats updatedStats = new TranslationStats(new TransUnitCount(9, 9, 9), new TransUnitWords(9, 9, 9));
reset(mockDisplay);
mockDisplay.setStats(eq(updatedStats));
expectLastCall().once();
replay(mockDisplay);
capturedDocumentStatsUpdatedEventHandler.getValue().onDocumentStatsUpdated(new DocumentStatsUpdatedEvent(testDocId, updatedStats));
verify(mockDisplay);
}
public void testDoesNotShowWrongDocumentStats()
{
setupAndBindAppPresenter();
verifyAllMocks();
loadDocAndViewEditor();
TranslationStats updatedStats = new TranslationStats(new TransUnitCount(9, 9, 9), new TransUnitWords(9, 9, 9));
reset(mockDisplay);
// not expecting stats change
replay(mockDisplay);
DocumentId notSelectedDocId = new DocumentId(7777L);
capturedDocumentStatsUpdatedEventHandler.getValue().onDocumentStatsUpdated(new DocumentStatsUpdatedEvent(notSelectedDocId, updatedStats));
verify(mockDisplay);
}
public void testUpdateDocumentStatsFromDoclistView()
{
setupAndBindAppPresenter();
verifyAllMocks();
HistoryToken token = loadDocAndViewEditor();
token = returnToDoclistView(token, emptyProjectStats);
reset(mockDisplay);
// not expecting stats change yet
replay(mockDisplay);
TranslationStats updatedStats = new TranslationStats(new TransUnitCount(9, 9, 9), new TransUnitWords(9, 9, 9));
capturedDocumentStatsUpdatedEventHandler.getValue().onDocumentStatsUpdated(new DocumentStatsUpdatedEvent(testDocId, updatedStats));
verify(mockDisplay);
returnToEditorView(token, updatedStats);
}
public void testDismiss()
{
setupDefaultMockExpectations();
appPresenter = newAppPresenter();
String testMessage = "testing";
mockDisplay.setNotificationMessage(testMessage, Severity.Error);
mockDismissVisibility.setVisible(true); // visible upon message
mockDisplay.setNotificationMessage("", Severity.Info);
mockDismissVisibility.setVisible(false); // invisible upon clear
replayAllMocks();
appPresenter.bind();
NotificationEvent notification = new NotificationEvent(Severity.Error, testMessage);
capturedNotificationEventHandler.getValue().onNotification(notification);
ClickEvent event = new ClickEvent()
{
};
capturedDismissLinkClickHandler.getValue().onClick(event);
verify(mockDismissVisibility, mockDisplay);
}
public void testDocumentsLinkGeneratesHistoryToken()
{
setupAndBindAppPresenter();
verifyAllMocks();
ClickEvent docLinkClickEvent = createMock(ClickEvent.class);
// replay(docLinkClickEvent);
// don't generate MainView.Editor token if a document isn't loaded
capturedDocumentLinkClickHandler.getValue().onClick(docLinkClickEvent);
assertThat(capturedHistoryTokenString.hasCaptured(), is(false));
HistoryToken token = loadDocAndViewEditor();
// make mock history return correct state
reset(mockHistory);
expect(mockHistory.getToken()).andReturn(token.toTokenString()).anyTimes();
mockHistory.newItem(capture(capturedHistoryTokenString));
expectLastCall().anyTimes();
replay(mockHistory);
capturedDocumentLinkClickHandler.getValue().onClick(docLinkClickEvent);
// should have token with doclist view
HistoryToken capturedToken = HistoryToken.fromTokenString(capturedHistoryTokenString.getValue());
assertThat("clicking documents link should always show doclist when editor is visible", capturedToken.getView(), is(MainView.Documents));
assertThat("document path should be maintained when clicking documents link", capturedToken.getDocumentPath(), is(token.getDocumentPath()));
// TODO could check that filter parameters haven't changed as well
token = returnToDoclistView(token, emptyProjectStats);
// make mock history return correct state
reset(mockHistory);
expect(mockHistory.getToken()).andReturn(token.toTokenString()).anyTimes();
mockHistory.newItem(capture(capturedHistoryTokenString));
expectLastCall().anyTimes();
replay(mockHistory);
capturedDocumentLinkClickHandler.getValue().onClick(docLinkClickEvent);
// should have token with editor view
capturedToken = HistoryToken.fromTokenString(capturedHistoryTokenString.getValue());
assertThat("clicking documents link should show editor when doclist is visible and a valid document is selected", capturedToken.getView(), is(MainView.Editor));
assertThat("document path should be maintained when clicking documents link", capturedToken.getDocumentPath(), is(token.getDocumentPath()));
}
public void testSearchLinkGeneratesHistoryToken()
{
setupAndBindAppPresenter();
verifyAllMocks();
//simulate click
ClickEvent searchLinkClickEvent = createMock(ClickEvent.class);
capturedSearchLinkClickHandler.getValue().onClick(searchLinkClickEvent);
HistoryToken capturedToken = HistoryToken.fromTokenString(capturedHistoryTokenString.getValue());
assertThat("clicking search link should set view in history token to search", capturedToken.getView(), is(MainView.Search));
//TODO could check that nothing else has changed in token
}
public void testShowsHidesReadonlyLabel()
{
setupAndBindAppPresenter();
verifyAllMocks();
// display expect show readonly
reset(mockDisplay);
mockDisplay.setReadOnlyVisible(true);
expectLastCall().once();
// simulate workspace readonly event
WorkspaceContextUpdateEvent mockEvent = createMock(WorkspaceContextUpdateEvent.class);
expect(mockEvent.isReadOnly()).andReturn(true).anyTimes();
replay(mockDisplay, mockEvent);
capturedWorkspaceContextUpdatedEventHandler.getValue().onWorkspaceContextUpdated(mockEvent);
verify(mockDisplay);
// display expect hide readonly
reset(mockDisplay, mockEvent);
mockDisplay.setReadOnlyVisible(false);
expectLastCall().once();
// simulate workspace editable event
expect(mockEvent.isReadOnly()).andReturn(false).anyTimes();
replay(mockDisplay, mockEvent);
capturedWorkspaceContextUpdatedEventHandler.getValue().onWorkspaceContextUpdated(mockEvent);
verify(mockDisplay);
}
/**
* generates new test doc id and doc info ready for use in tests
*/
private void buildTestDocumentInfo()
{
testDocId = new DocumentId(2222L);
TransUnitCount unitCount = new TransUnitCount(1, 2, 3);
TransUnitWords wordCount = new TransUnitWords(4, 5, 6);
testDocStats = new TranslationStats(unitCount, wordCount);
testDocInfo = new DocumentInfo(testDocId, TEST_DOCUMENT_NAME, TEST_DOCUMENT_PATH, LocaleId.EN_US, testDocStats);
}
/**
* Simulate selecting test document and viewing it in the editor. The
* application presenter must be bound before calling this method.
*
* This also tests that:
* <ul>
* <li>A history event with a valid document will correctly display the
* editor view.</li>
* <li>The document path, name and stats are displayed in the editor.</li>
* </ul>
*
* @return a {@link HistoryToken} representing the current application state
*/
private HistoryToken loadDocAndViewEditor()
{
reset(mockDisplay, mockDocumentListPresenter);
buildTestDocumentInfo();
expect(mockDocumentListPresenter.getDocumentId(TEST_DOCUMENT_PATH + TEST_DOCUMENT_NAME)).andReturn(testDocId).anyTimes();
expect(mockDocumentListPresenter.getDocumentInfo(testDocId)).andReturn(testDocInfo).anyTimes();
// test document name and stats should be shown
mockDisplay.setDocumentLabel(TEST_DOCUMENT_PATH, TEST_DOCUMENT_NAME);
expectLastCall().anyTimes();
mockDisplay.setStats(eq(testDocStats));
expectLastCall().anyTimes();
mockDisplay.showInMainView(MainView.Editor);
expectLastCall().once();
replay(mockDisplay, mockDocumentListPresenter);
HistoryToken docInEditorToken = new HistoryToken();
docInEditorToken.setDocumentPath(TEST_DOCUMENT_PATH + TEST_DOCUMENT_NAME);
docInEditorToken.setView(MainView.Editor);
capturedHistoryValueChangeHandler.getValue().onValueChange(new ValueChangeEvent<String>(docInEditorToken.toTokenString())
{
});
verify(mockDisplay, mockDocumentListPresenter);
return docInEditorToken;
}
/**
* @param previousToken a token representing the state of the application
* before returning to document list view
*
* @return a {@link HistoryToken} representing the current application state
*/
private HistoryToken returnToDoclistView(HistoryToken previousToken, TranslationStats currentProjectStats)
{
// return to doc list
reset(mockDisplay, mockTranslationPresenter);
mockDisplay.showInMainView(MainView.Documents);
expectLastCall().once();
mockDisplay.setDocumentLabel("", NO_DOCUMENTS_STRING);
expectLastCall().once();
// project stats are empty in default test setup
mockDisplay.setStats(eq(currentProjectStats));
expectLastCall().once();
mockTranslationPresenter.saveEditorPendingChange();
expectLastCall().once();
replay(mockDisplay, mockTranslationPresenter);
previousToken.setView(MainView.Documents);
capturedHistoryValueChangeHandler.getValue().onValueChange(new ValueChangeEvent<String>(previousToken.toTokenString())
{
});
return previousToken; // updated
}
/**
* Return to the editor view with the test document already loaded
*
* @param previousToken a token representing the state of the application
* before returning to the editor view
*/
private void returnToEditorView(HistoryToken previousToken, TranslationStats expectedStats)
{
// return to editor
reset(mockDisplay);
mockDisplay.showInMainView(MainView.Editor);
expectLastCall().once();
mockDisplay.setDocumentLabel(TEST_DOCUMENT_PATH, TEST_DOCUMENT_NAME);
expectLastCall().once();
mockDisplay.setStats(eq(expectedStats));
expectLastCall().once();
replay(mockDisplay);
previousToken.setView(MainView.Editor);
capturedHistoryValueChangeHandler.getValue().onValueChange(new ValueChangeEvent<String>(previousToken.toTokenString())
{
});
}
private void setupAndBindAppPresenter()
{
setupDefaultMockExpectations();
replayAllMocks();
appPresenter = newAppPresenter();
appPresenter.bind();
}
private void setupDefaultMockExpectations()
{
expect(mockDisplay.getSignOutLink()).andReturn(mockSignoutLink).anyTimes();
expect(mockDisplay.getLeaveWorkspaceLink()).andReturn(mockLeaveWorkspaceLink).anyTimes();
expect(mockDisplay.getDocumentsLink()).andReturn(mockDocumentsLink).anyTimes();
expect(mockDisplay.getSearchLink()).andReturn(mockSearchLink).anyTimes();
expect(mockDisplay.getDismiss()).andReturn(mockDismiss).anyTimes();
expect(mockDisplay.getDismissVisibility()).andReturn(mockDismissVisibility).anyTimes();
mockDismissVisibility.setVisible(false); // starts invisible
expectLastCall().once();
mockDisplay.showInMainView(MainView.Documents);
expectLastCall().anyTimes();
mockDisplay.setDocumentLabel("", NO_DOCUMENTS_STRING);
expectLastCall().once();
mockDisplay.setUserLabel(TEST_PERSON_NAME);
expectLastCall().anyTimes();
mockDisplay.setWorkspaceNameLabel(TEST_WORKSPACE_NAME, TEST_WORKSPACE_TITLE);
expectLastCall().anyTimes();
mockDisplay.setReadOnlyVisible(false);
expectLastCall().once();
// initially empty project stats
emptyProjectStats = new TranslationStats();
mockDisplay.setStats(eq(emptyProjectStats));
expectLastCall().once();
mockDocumentListPresenter.bind();
expectLastCall().once();
expectClickHandlerRegistrationOnce(mockDocumentsLink, capturedDocumentLinkClickHandler);
expectClickHandlerRegistrationOnce(mockSearchLink, capturedSearchLinkClickHandler);
expectClickHandlerRegistrationOnce(mockDismiss, capturedDismissLinkClickHandler);
expectClickHandlerRegistrationOnce(mockLeaveWorkspaceLink, capturedLeaveWorkspaceLinkClickHandler);
expectClickHandlerRegistrationOnce(mockSignoutLink, capturedSignoutLinkClickHandler);
expect(mockEventBus.addHandler(eq(NotificationEvent.getType()), and(capture(capturedNotificationEventHandler), isA(NotificationEventHandler.class)))).andReturn(createMock(HandlerRegistration.class)).once();
expect(mockEventBus.addHandler(eq(DocumentStatsUpdatedEvent.getType()), and(capture(capturedDocumentStatsUpdatedEventHandler), isA(DocumentStatsUpdatedEventHandler.class)))).andReturn(createMock(HandlerRegistration.class)).once();
expect(mockEventBus.addHandler(eq(ProjectStatsUpdatedEvent.getType()), and(capture(capturedProjectStatsUpdatedEventHandler), isA(ProjectStatsUpdatedEventHandler.class)))).andReturn(createMock(HandlerRegistration.class)).once();
expect(mockEventBus.addHandler(eq(WorkspaceContextUpdateEvent.getType()), and(capture(capturedWorkspaceContextUpdatedEventHandler), isA(WorkspaceContextUpdateEventHandler.class)))).andReturn(createMock(HandlerRegistration.class)).once();
mockEventBus.fireEvent(and(capture(capturedDocumentSelectionEvent), isA(DocumentSelectionEvent.class)));
expectLastCall().anyTimes();
setupMockHistory("");
expect(mockIdentity.getPerson()).andReturn(mockPerson).anyTimes();
expect(mockMessages.windowTitle(TEST_WORKSPACE_NAME, TEST_LOCALE_NAME)).andReturn(TEST_WINDOW_TITLE).anyTimes();
expect(mockMessages.noDocumentSelected()).andReturn(NO_DOCUMENTS_STRING).anyTimes();
expect(mockPerson.getName()).andReturn(TEST_PERSON_NAME).anyTimes();
mockSearchResultsPresenter.bind();
expectLastCall().once();
mockTranslationPresenter.bind();
expectLastCall().once();
mockWindow.setTitle(TEST_WINDOW_TITLE);
expectLastCall().once();
expect(mockWindowLocation.getParameter(WORKSPACE_TITLE_QUERY_PARAMETER_KEY)).andReturn(TEST_WORKSPACE_TITLE).anyTimes();
expect(mockWorkspaceContext.getWorkspaceName()).andReturn(TEST_WORKSPACE_NAME).anyTimes();
expect(mockWorkspaceContext.getLocaleName()).andReturn(TEST_LOCALE_NAME).anyTimes();
expect(mockWorkspaceContext.isReadOnly()).andReturn(false).anyTimes();
}
/**
* Expect a single handler registration on a mock object, and capture the
* click handler in the given {@link Capture}
*
* @param mockObjectToClick
* @param captureForHandler
*/
private void expectClickHandlerRegistrationOnce(HasClickHandlers mockObjectToClick, Capture<ClickHandler> captureForHandler)
{
expect(mockObjectToClick.addClickHandler(and(capture(captureForHandler), isA(ClickHandler.class)))).andReturn(createMock(HandlerRegistration.class)).once();
}
@SuppressWarnings("unchecked")
private void setupMockHistory(String tokenToReturn)
{
expect(mockHistory.addValueChangeHandler(and(capture(capturedHistoryValueChangeHandler), isA(ValueChangeHandler.class)))).andReturn(createMock(HandlerRegistration.class)).anyTimes();
expect(mockHistory.getToken()).andReturn(tokenToReturn).anyTimes();
mockHistory.fireCurrentHistoryState();
expectLastCall().anyTimes();
mockHistory.newItem(capture(capturedHistoryTokenString));
expectLastCall().anyTimes();
}
private void resetAllMocks()
{
reset(mockDisplay, mockDocumentListPresenter, mockDocumentsLink);
reset(mockEventBus, mockHistory, mockIdentity, mockLeaveWorkspaceLink);
reset(mockMessages, mockPerson, mockSearchResultsPresenter, mockSignoutLink);
reset(mockTranslationPresenter, mockWindow, mockWindowLocation, mockWorkspaceContext);
reset(mockDismiss, mockDismissVisibility, mockSearchLink);
}
private void resetAllCaptures()
{
capturedDismissLinkClickHandler.reset();
capturedDocumentLinkClickHandler.reset();
capturedDocumentSelectionEvent.reset();
capturedDocumentStatsUpdatedEventHandler.reset();
capturedHistoryTokenString.reset();
capturedHistoryValueChangeHandler.reset();
capturedLeaveWorkspaceLinkClickHandler.reset();
capturedNotificationEventHandler.reset();
capturedProjectStatsUpdatedEventHandler.reset();
capturedSearchLinkClickHandler.reset();
capturedSignoutLinkClickHandler.reset();
capturedWorkspaceContextUpdatedEventHandler.reset();
}
private void replayAllMocks()
{
replay(mockDisplay, mockDocumentListPresenter, mockDocumentsLink);
replay(mockEventBus, mockHistory, mockIdentity, mockLeaveWorkspaceLink);
replay(mockMessages, mockPerson, mockSearchResultsPresenter, mockSignoutLink);
replay(mockTranslationPresenter, mockWindow, mockWindowLocation, mockWorkspaceContext);
replay(mockDismiss, mockDismissVisibility, mockSearchLink);
}
private void verifyAllMocks()
{
verify(mockDisplay, mockDocumentListPresenter, mockDocumentsLink);
verify(mockEventBus, mockHistory, mockIdentity, mockLeaveWorkspaceLink);
verify(mockMessages, mockPerson, mockSearchResultsPresenter, mockSignoutLink);
verify(mockTranslationPresenter, mockWindow, mockWindowLocation, mockWorkspaceContext);
verify(mockDismiss, mockDismissVisibility, mockSearchLink);
}
}