-
Notifications
You must be signed in to change notification settings - Fork 192
/
NavigationTest.java
648 lines (539 loc) · 28.2 KB
/
NavigationTest.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
/*
* Copyright (C) 2015 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.errai.ui.nav.client.local;
import java.util.Collection;
import org.jboss.errai.common.client.util.CreationalCallback;
import org.jboss.errai.enterprise.client.cdi.AbstractErraiCDITest;
import org.jboss.errai.ioc.client.container.Factory;
import org.jboss.errai.ioc.client.container.IOC;
import org.jboss.errai.ui.nav.client.local.api.MissingPageRoleException;
import org.jboss.errai.ui.nav.client.local.api.PageNotFoundException;
import org.jboss.errai.ui.nav.client.local.pushstate.PushStateUtil;
import org.jboss.errai.ui.nav.client.local.res.TestNavigationErrorHandler;
import org.jboss.errai.ui.nav.client.local.spi.NavigationGraph;
import org.jboss.errai.ui.nav.client.local.spi.PageNode;
import org.jboss.errai.ui.nav.client.local.testpages.AppScopedPageWithNoLifecycleMethods;
import org.jboss.errai.ui.nav.client.local.testpages.CircularRef1;
import org.jboss.errai.ui.nav.client.local.testpages.CircularRef2;
import org.jboss.errai.ui.nav.client.local.testpages.IsElementElemental2PageWithLeadingSlashPath;
import org.jboss.errai.ui.nav.client.local.testpages.IsElementPageWithLeadingSlashPath;
import org.jboss.errai.ui.nav.client.local.testpages.MissingPageRole;
import org.jboss.errai.ui.nav.client.local.testpages.MissingUniquePageRole;
import org.jboss.errai.ui.nav.client.local.testpages.NonCompositePage;
import org.jboss.errai.ui.nav.client.local.testpages.PageA;
import org.jboss.errai.ui.nav.client.local.testpages.PageB;
import org.jboss.errai.ui.nav.client.local.testpages.PageBWithState;
import org.jboss.errai.ui.nav.client.local.testpages.PageIsWidget;
import org.jboss.errai.ui.nav.client.local.testpages.PageWithExtraState;
import org.jboss.errai.ui.nav.client.local.testpages.PageWithLinkToIsWidget;
import org.jboss.errai.ui.nav.client.local.testpages.PageWithNavigationControl;
import org.jboss.errai.ui.nav.client.local.testpages.PageWithRole;
import org.jboss.errai.ui.nav.client.local.testpages.PageWithTransitionToElemental2IsElement;
import org.jboss.errai.ui.nav.client.local.testpages.PageWithTransitionToIsElement;
import org.jboss.errai.ui.nav.client.local.testpages.PageWithTransitionToNonComposite;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableMultimap.Builder;
import com.google.common.collect.Multimap;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.Window.Location;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.web.bindery.event.shared.HandlerRegistration;
import junit.framework.AssertionFailedError;
public class NavigationTest extends AbstractErraiCDITest {
private static final int TIMEOUT = 30000;
private Navigation navigation;
private NavigationGraph navGraph;
private HandlerRegistration historyHandlerRegistration;
private int historyHandlerInvocations;
private HistoryTokenFactory htFactory;
@Override
public String getModuleName() {
return "org.jboss.errai.ui.nav.NavigationTest";
}
@Override
protected void gwtSetUp() throws Exception {
disableBus = true;
super.gwtSetUp();
navigation = IOC.getBeanManager().lookupBean(Navigation.class).getInstance();
navGraph = navigation.getNavGraph();
htFactory = IOC.getBeanManager().lookupBean(HistoryTokenFactory.class).getInstance();
History.newItem("", false);
}
@Override
protected void gwtTearDown() throws Exception {
navigation.cleanUp();
super.gwtTearDown();
}
public void testNavigationToNonCompositePage() throws Exception {
navigation.goTo(NonCompositePage.class.getSimpleName());
assertEquals("Did not navigate to non composite page.", NonCompositePage.class, navigation.currentPage.contentType());
}
public void testNavigationFromNonCompositePage() throws Exception {
try {
testNavigationToNonCompositePage();
} catch (AssertionError ae) {
throw new AssertionError("Precondition failed: Could not navigate to non-composite page.", ae);
}
navigation.goTo(PageB.class, ImmutableMultimap.<String, String>of());
assertEquals("Did not navigate to PageB from composite.", PageB.class, navigation.currentPage.contentType());
}
public void testMissingPage() throws Exception {
try {
navGraph.getPage("page that does not exist");
fail("Did not get an exception for a missing page");
} catch (PageNotFoundException ex) {
assertTrue(ex.getMessage().contains("page that does not exist"));
}
}
public void testDefaultErrorHandlerWithMissingPage() throws Exception {
navigation.goTo("PageB"); // navigate to a known non-default page before
// test because the expected behavior is that it
// navigates back to the default page
navigation.goTo("page that does not exist");
assertEquals("did not navigate to default page", PageA.class, navigation.currentPage.contentType());
}
public void testErrorHandlerWithMissingPageThroughGoTo() throws Exception {
TestNavigationErrorHandler newNavigationHandler = new TestNavigationErrorHandler();
navigation.setErrorHandler(newNavigationHandler);
navigation.goTo("page that does not exist");
assertEquals("did not enter new handler", 1, newNavigationHandler.count);
}
public void testErrorHandlerWithMissingPageThroughHistoryNewItem() throws Exception {
TestNavigationErrorHandler newNavigationHandler = new TestNavigationErrorHandler();
navigation.setErrorHandler(newNavigationHandler);
History.newItem("page that does not exist");
assertEquals("did not enter new handler", 1, newNavigationHandler.count);
}
public void testMissingPageRole() throws Exception {
navigation.goTo("PageB"); // navigate to a known non-default page before
// test because the expected behavior is that it
// navigates back to the default page
navigation.goToWithRole(MissingPageRole.class);
assertEquals("did not navigate to default page", PageA.class, navigation.currentPage.contentType());
}
public void testMissingPageRoleWithTestErrorHandler() throws Exception {
TestNavigationErrorHandler newNavigationHandler = new TestNavigationErrorHandler();
navigation.setErrorHandler(newNavigationHandler);
navigation.goToWithRole(MissingPageRole.class);
assertEquals("Did not go through test error handler", 1, newNavigationHandler.count);
}
public void testPageWithDefaultName() throws Exception {
PageNode<?> pageA = navGraph.getPage("PageA");
assertNotNull(pageA);
assertEquals("PageA", pageA.name());
}
public void testPageWithProvidedName() throws Exception {
PageNode<?> pageB = navGraph.getPage("PageB");
assertNotNull(pageB);
assertEquals("PageB", pageB.name());
}
public void testCircularReferences() throws Exception {
PageNode<CircularRef1> cr1Node = navGraph.getPage(CircularRef1.class);
// now fetch the bean instance
final CircularRef1[] workaround = new CircularRef1[1];
cr1Node.produceContent(new CreationalCallback<CircularRef1>() {
@Override
public void callback(CircularRef1 beanInstance) {
workaround[0] = beanInstance;
}
});
assertNotNull("CreationalCallback should have been invoked before produceContent returned!", workaround[0]);
CircularRef1 cr1 = workaround[0];
TransitionTo<CircularRef2> transitionToCR2 = cr1.getLink();
Class<CircularRef2> cr2Type = transitionToCR2.toPageType();
assertEquals(CircularRef2.class, cr2Type);
}
public void testUrlUpdatesWithPageChange() throws Exception {
navigation.goTo(PageWithExtraState.class, ImmutableMultimap.of("intThing", "42"));
assertEquals("#PageWithExtraState;intThing=42", Window.Location.getHash());
}
public void testUrlUpdateWithPageChangeByRole() {
navigation.goToWithRole(DefaultPage.class);
assertEquals("#PageA", Window.Location.getHash());
}
public void testGetPageByRole() throws Exception {
final Collection<PageNode<?>> pageByRole = navGraph.getPagesByRole(PageWithRole.AdminPage.class);
assertNotNull(pageByRole);
assertFalse(pageByRole.isEmpty());
assertTrue(pageByRole.size() == 2);
for (PageNode<?> pageNode : pageByRole) {
assertTrue(pageNode.name() + " is not a page annotated with the admin role",
pageNode.name().matches("Page.?WithRole"));
}
}
public void testGetMissingPageByRole() throws Exception {
final Collection<PageNode<?>> pagesByRole = navGraph.getPagesByRole(MissingPageRole.class);
assertNotNull(pagesByRole);
assertTrue(pagesByRole.isEmpty());
}
public void testGetMissingPageByUniqueRole() throws Exception {
Throwable thrown = null;
try {
navGraph.getPageByRole(MissingUniquePageRole.class);
} catch (Throwable e) {
thrown = e;
}
assertNotNull("Expected an exception to be thrown.", thrown);
assertEquals(MissingPageRoleException.class, thrown.getClass());
}
public void testGetPageWithDefaultRole() {
final PageNode<?> pageByRole = navGraph.getPageByRole(DefaultPage.class);
assertNotNull(pageByRole);
assertEquals("PageA", pageByRole.name());
}
public void testIsWidgetPage() {
final PageIsWidget page = IOC.getBeanManager().lookupBean(PageIsWidget.class).getInstance();
historyHandlerRegistration = History.addValueChangeHandler(new ValueChangeHandler<String>() {
@Override
public void onValueChange(ValueChangeEvent<String> event) {
historyHandlerRegistration.removeHandler();
assertEquals(PageIsWidget.class, navigation.getCurrentPage().contentType());
assertEquals(page.asWidget(), ((SimplePanel) navigation.getContentPanel().asWidget()).getWidget());
finishTest();
}
});
delayTestFinish(5000);
navigation.goTo(PageIsWidget.class, ImmutableMultimap.<String, String> of());
}
public void testIsWidgetPageTransition() {
final PageWithLinkToIsWidget page = IOC.getBeanManager().lookupBean(PageWithLinkToIsWidget.class).getInstance();
final PageIsWidget targetPage = IOC.getBeanManager().lookupBean(PageIsWidget.class).getInstance();
historyHandlerRegistration = History.addValueChangeHandler(new ValueChangeHandler<String>() {
@Override
public void onValueChange(ValueChangeEvent<String> event) {
historyHandlerRegistration.removeHandler();
assertEquals(PageIsWidget.class, navigation.getCurrentPage().contentType());
assertEquals(targetPage.asWidget(), ((SimplePanel) navigation.getContentPanel().asWidget()).getWidget());
finishTest();
}
});
delayTestFinish(5000);
page.getTransitionToIsWidget().go();
}
public void testTransitionToNonCompositePage() throws Exception {
final PageWithTransitionToNonComposite pageWithTransition = IOC.getBeanManager().lookupBean(PageWithTransitionToNonComposite.class).getInstance();
navigation.goTo("");
assertFalse("Precondition failed: Should not start test on " + NonCompositePage.class.getSimpleName(),
navigation.currentPage.contentType().equals(NonCompositePage.class));
pageWithTransition.transition.go();
assertTrue("Should have navigated to " + NonCompositePage.class.getSimpleName(),
navigation.currentPage.contentType().equals(NonCompositePage.class));
}
public void testTransitionToIsElementPage() throws Exception {
final PageWithTransitionToIsElement pageWithTransition = IOC.getBeanManager().lookupBean(PageWithTransitionToIsElement.class).getInstance();
navigation.goTo("");
assertFalse("Precondition failed: Should not start test on " + IsElementPageWithLeadingSlashPath.class.getSimpleName(),
navigation.currentPage.contentType().equals(IsElementPageWithLeadingSlashPath.class));
pageWithTransition.transition.go();
assertEquals("Should have navigated to " + IsElementPageWithLeadingSlashPath.class.getSimpleName(),
IsElementPageWithLeadingSlashPath.class, navigation.currentPage.contentType());
}
public void testTransitionToPageWithLeadingSlash() throws Exception {
final PageWithTransitionToIsElement pageWithTransition = IOC.getBeanManager().lookupBean(PageWithTransitionToIsElement.class).getInstance();
try {
navigation.goTo("");
assertFalse("Precondition failed: Should not start test on " + IsElementPageWithLeadingSlashPath.class.getSimpleName(),
navigation.currentPage.contentType().equals(IsElementPageWithLeadingSlashPath.class));
pageWithTransition.transition.go();
assertEquals("Should have navigated to " + IsElementPageWithLeadingSlashPath.class.getSimpleName(),
IsElementPageWithLeadingSlashPath.class, navigation.currentPage.contentType());
} catch (Exception e) {
throw new AssertionError("Precondition failed.", e);
}
assertEquals("#" + IsElementPageWithLeadingSlashPath.IS_ELEMENT_PAGE, Location.getHash());
}
public void testTransitionToElemental2IsElementPage() throws Exception {
final PageWithTransitionToElemental2IsElement pageWithTransition = IOC.getBeanManager().lookupBean(PageWithTransitionToElemental2IsElement.class).getInstance();
navigation.goTo("");
assertFalse("Precondition failed: Should not start test on " + IsElementElemental2PageWithLeadingSlashPath.class.getSimpleName(),
navigation.currentPage.contentType().equals(IsElementElemental2PageWithLeadingSlashPath.class));
pageWithTransition.transition.go();
assertEquals("Should have navigated to " + IsElementElemental2PageWithLeadingSlashPath.class.getSimpleName(),
IsElementElemental2PageWithLeadingSlashPath.class, navigation.currentPage.contentType());
}
public void testTransitionToElemental2PageWithLeadingSlash() throws Exception {
final PageWithTransitionToElemental2IsElement pageWithTransition = IOC.getBeanManager().lookupBean(PageWithTransitionToElemental2IsElement.class).getInstance();
try {
navigation.goTo("");
assertFalse("Precondition failed: Should not start test on " + IsElementElemental2PageWithLeadingSlashPath.class.getSimpleName(),
navigation.currentPage.contentType().equals(IsElementElemental2PageWithLeadingSlashPath.class));
pageWithTransition.transition.go();
assertEquals("Should have navigated to " + IsElementElemental2PageWithLeadingSlashPath.class.getSimpleName(),
IsElementElemental2PageWithLeadingSlashPath.class, navigation.currentPage.contentType());
} catch (Exception e) {
throw new AssertionError("Precondition failed.", e);
}
assertEquals("#" + IsElementElemental2PageWithLeadingSlashPath.IS_ELEMENT_PAGE, Location.getHash());
}
public void testIsWidgetAnchorTransition() {
final PageWithLinkToIsWidget page = IOC.getBeanManager().lookupBean(PageWithLinkToIsWidget.class).getInstance();
final PageIsWidget targetPage = IOC.getBeanManager().lookupBean(PageIsWidget.class).getInstance();
historyHandlerRegistration = History.addValueChangeHandler(new ValueChangeHandler<String>() {
@Override
public void onValueChange(ValueChangeEvent<String> event) {
historyHandlerRegistration.removeHandler();
assertEquals(PageIsWidget.class, navigation.getCurrentPage().contentType());
assertEquals(targetPage.asWidget(), ((SimplePanel) navigation.getContentPanel().asWidget()).getWidget());
finishTest();
}
});
delayTestFinish(5000);
page.getLinkToIsWidget().click();
}
public void testNotAttachedToRootPanel() {
assertNull("Navigation Content Panel should not yet have a parent", navigation.getContentPanel().asWidget()
.getParent());
}
public void testAutomaticRootPanelAttachment() {
runPostAttachTests(new Runnable() {
@Override
public void run() {
assertEquals("Navigation Panel was not automatically attached", RootPanel.get(), navigation.getContentPanel()
.asWidget().getParent());
}
}, TIMEOUT, 500);
}
public void testAddNavToRootPanel() {
runPostAttachTests(new Runnable() {
@Override
public void run() {
RootPanel.get().add(navigation.getContentPanel());
assertEquals("Navigation Panel should still be attached to the RootPanel", RootPanel.get(), navigation
.getContentPanel().asWidget().getParent());
}
}, TIMEOUT, 500);
}
public void testAddNavPanelToOtherPanel() {
runPostAttachTests(new Runnable() {
@Override
public void run() {
SimplePanel newParent = new SimplePanel();
newParent.add(navigation.getContentPanel());
assertEquals("Navigation panel should be child of newParent", newParent, navigation.getContentPanel()
.asWidget().getParent());
assertEquals("Navigation panel shoudl not be attached to the RootPanel", -1,
RootPanel.get().getWidgetIndex(navigation.getContentPanel()));
}
}, TIMEOUT, 500);
}
public void testManuallyAttachToRootPanelBefore() {
assertNull("Navigation content panel should not yet be attached", navigation.getContentPanel().asWidget()
.getParent());
RootPanel.get().add(navigation.getContentPanel());
// Force navigation to run Navigation#maybeAttachContentPanel()
navigation.goToWithRole(DefaultPage.class);
assertEquals("RootPanel should still be parent", RootPanel.get(), navigation.getContentPanel().asWidget()
.getParent());
}
public void testManuallyAttachToOtherPanelBefore() {
assertNull("Navigation content panel should not yet be attached", navigation.getContentPanel().asWidget()
.getParent());
SimplePanel panel = new SimplePanel();
panel.add(navigation.getContentPanel());
// Force navigation to run Navigation#maybeAttachContentPanel()
navigation.goToWithRole(DefaultPage.class);
assertEquals(panel, navigation.getContentPanel().asWidget().getParent());
}
/**
* Test if a history event is fired when navigation is done.
*/
public void testHistoryEventFired() {
final PageWithLinkToIsWidget page = IOC.getBeanManager().lookupBean(PageWithLinkToIsWidget.class).getInstance();
historyHandlerInvocations = 0;
historyHandlerRegistration = History.addValueChangeHandler(new ValueChangeHandler<String>() {
@Override
public void onValueChange(ValueChangeEvent<String> event) {
historyHandlerInvocations++;
// the first invocation is triggered by the init vote
if (historyHandlerInvocations == 1) {
page.getLinkToIsWidget().click();
}
else {
historyHandlerRegistration.removeHandler();
finishTest();
}
}
});
delayTestFinish(5000);
}
public void testNavigationControl() throws Exception {
final PageWithNavigationControl page = Factory.maybeUnwrapProxy(IOC.getBeanManager().lookupBean(PageWithNavigationControl.class)
.getInstance());
navigation.goTo(PageWithNavigationControl.class, ArrayListMultimap.<String, String> create());
page.showControl.proceed();
assertEquals(PageWithNavigationControl.class, navigation.getCurrentPage().contentType());
navigation.goTo(PageA.class, ArrayListMultimap.<String, String> create());
page.hideControl.proceed();
assertEquals(PageA.class, navigation.getCurrentPage().contentType());
}
public void testNavigationControlRedirect() throws Exception {
final PageWithNavigationControl page = Factory.maybeUnwrapProxy(IOC.getBeanManager().lookupBean(PageWithNavigationControl.class)
.getInstance());
navigation.goTo(PageWithNavigationControl.class, ArrayListMultimap.<String, String> create());
page.showControl.redirect(PageA.class);
assertEquals(PageA.class, navigation.getCurrentPage().contentType());
navigation.goTo(PageWithNavigationControl.class, ArrayListMultimap.<String, String> create());
page.showControl.proceed();
assertEquals(PageWithNavigationControl.class, navigation.getCurrentPage().contentType());
navigation.goTo(PageA.class, ArrayListMultimap.<String, String> create());
page.hideControl.redirect(PageB.class);
assertEquals(PageB.class, navigation.getCurrentPage().contentType());
}
/**
* Give the bootstrapper time to attach the Navigation content panel to the RootPanel and then run a test.
*
* @param test
* The test code to be executed after the content panel is attached
* @param timeout
* The time in milliseconds to wait for the bootstrapper before giving up
* @param interval
* The interval in milliseconds at which to poll the content panel while waiting for bootstrapping
*/
private void runPostAttachTests(final Runnable test, final int timeout, final int interval) {
delayTestFinish(timeout + 2 * interval);
final long startTime = System.currentTimeMillis();
new Timer() {
@Override
public void run() {
if (System.currentTimeMillis() - startTime >= timeout) {
this.cancel();
}
else {
try {
assertEquals(RootPanel.get(), navigation.getContentPanel().asWidget().getParent());
} catch (AssertionFailedError e) {
return;
}
test.run();
this.cancel();
finishTest();
}
}
}.scheduleRepeating(interval);
}
public void testURLWithExtraKeyValuePairs() throws Exception {
String url = "/page/123/string;var3=4";
HistoryToken encodedToken = htFactory.parseURL(url);
assertEquals("Unexpected state map contents: " + encodedToken.getState(), "123", encodedToken.getState()
.get("var1").iterator().next());
assertEquals("Unexpected state map contents: " + encodedToken.getState(), "string",
encodedToken.getState().get("var2").iterator().next());
assertEquals("Unexpected state map contents: " + encodedToken.getState(), "4", encodedToken.getState().get("var3")
.iterator().next());
}
public void testURLWithNonAsciiCharset() throws Exception {
String url = "/page/123/параметр パラメーター 参数;var3=4";
HistoryToken encodedToken = htFactory.parseURL(url);
assertEquals("Unexpected state map contents: " + encodedToken.getState(), "123", encodedToken.getState()
.get("var1").iterator().next());
assertEquals("Unexpected state map contents: " + encodedToken.getState(), "параметр パラメーター 参数",
encodedToken.getState().get("var2").iterator().next());
assertEquals("Unexpected state map contents: " + encodedToken.getState(), "4", encodedToken.getState().get("var3")
.iterator().next());
}
public void testPageStateWithNonAsciiParam() throws Exception {
String pageName = "PageWithPathParameters";
Builder<String, String> builder = ImmutableMultimap.builder();
builder.put("var1", "123");
builder.put("var2", "параметр パラメーター 参数");
builder.put("var3", "4");
Multimap<String, String> pageStateMap = builder.build();
String decodedToken = URLPattern.decodeParsingCharacters(htFactory.createHistoryToken(pageName, pageStateMap).toString());
assertEquals("Incorrect HistoryToken URL generated: " + decodedToken, "/page/123/параметр パラメーター 参数;var3=4", decodedToken);
}
public void testPageStateWithOneExtraParam() throws Exception {
String pageName = "PageWithPathParameters";
Builder<String, String> builder = ImmutableMultimap.builder();
builder.put("var1", "123");
builder.put("var2", "string");
builder.put("var3", "4");
Multimap<String, String> pageStateMap = builder.build();
String decodedToken = URLPattern.decodeParsingCharacters(htFactory.createHistoryToken(pageName, pageStateMap)
.toString());
assertEquals("Incorrect HistoryToken URL generated: " + decodedToken, "/page/123/string;var3=4", decodedToken);
}
public void testPageStateWithMultipleExtraParams() throws Exception {
String pageName = "PageWithPathParameters";
Builder<String, String> builder = ImmutableMultimap.builder();
builder.put("var1", "123");
builder.put("var2", "string");
builder.put("var3", "4");
builder.put("var4", "thing");
Multimap<String, String> pageStateMap = builder.build();
String decodedToken = URLPattern.decodeParsingCharacters(htFactory.createHistoryToken(pageName, pageStateMap)
.toString());
assertEquals("Incorrect HistoryToken URL generated: " + decodedToken, "/page/123/string;var3=4&var4=thing", decodedToken);
}
public void testPageReloadWithChangedPageStateUsingGoTo() throws Exception {
PageBWithState.hitCount = 0;
Builder<String, String> oldBuilder = ImmutableMultimap.builder();
oldBuilder.put("uuid", "oldstate");
Multimap<String, String> oldState = oldBuilder.build();
navigation.goTo(PageBWithState.class, oldState);
assertEquals("Did not hit @PageShowing method", 1, PageBWithState.hitCount);
Builder<String, String> newBuilder = ImmutableMultimap.builder();
newBuilder.put("uuid", "newstate");
Multimap<String, String> newState = newBuilder.build();
navigation.goTo(PageBWithState.class, newState);
assertEquals("Did not hit @PageShowing method", 2, PageBWithState.hitCount);
}
public void testPageReloadWhenPageStateChangedInURLBar() throws Exception {
PageBWithState.hitCount = 0;
Builder<String, String> oldBuilder = ImmutableMultimap.builder();
oldBuilder.put("uuid", "oldstate");
Multimap<String, String> state = oldBuilder.build();
navigation.goTo(PageBWithState.class, state);
assertEquals("Did not hit @PageShowing method", 1, PageBWithState.hitCount);
History.newItem("page_b_with_state;uuid=newstate", true);
assertEquals("Did not hit @PageShowing method", 2, PageBWithState.hitCount);
}
public void testForNonAsciiPagePath() throws Exception {
String pageName = "PageWithNonAsciiPath";
Builder<String, String> builder = ImmutableMultimap.builder();
builder.put("var1", "параметр パラメーター 参数");
builder.put("var2", "123");
Multimap<String, String> pageStateMap = builder.build();
String decodedToken = URLPattern.decodeParsingCharacters(htFactory.createHistoryToken(pageName, pageStateMap)
.toString());
assertEquals("Incorrect HistoryToken URL generated: " + decodedToken, "pagename/path/some:параметр パラメーター "
+ "参数thing/öther123 thing", decodedToken);
}
public void testForEmptyContextWithoutPushState() throws Exception {
runPostAttachTests(new Runnable() {
@Override
public void run() {
assertFalse(PushStateUtil.isPushStateActivated());
assertEquals("", Navigation.getAppContext());
}
}, TIMEOUT, 500);
}
public void testNavigationPanelInjection() {
NavigationPanelTestApp app =
IOC.getBeanManager().lookupBean(NavigationPanelTestApp.class).getInstance();
assertNotNull(app.getNavigationPanel());
}
public void testAppScopedPageWithNoLifecycleMethodsIsLoadedOnNavigation() throws Exception {
assertEquals("Precondition failed: the page should not have been loaded before navigation.", 0, AppScopedPageWithNoLifecycleMethods.postConstructCount);
navigation.goTo(AppScopedPageWithNoLifecycleMethods.class.getSimpleName());
assertEquals("The page bean should have been loaded when the page was displayed", 1, AppScopedPageWithNoLifecycleMethods.postConstructCount);
}
}