diff options
author | Henri Sara <hesara@vaadin.com> | 2012-06-11 14:57:19 +0300 |
---|---|---|
committer | Henri Sara <hesara@vaadin.com> | 2012-06-12 14:17:51 +0300 |
commit | a1a87b0ce6bf96f868f18177bebb1a226a5a2753 (patch) | |
tree | d45da652d9dc8f70c9a8755d39541d55f185e79a /tests/server-side | |
parent | 8c60fbdc78d9ce79750b59db4875ec0c3c53c821 (diff) | |
download | vaadin-framework-a1a87b0ce6bf96f868f18177bebb1a226a5a2753.tar.gz vaadin-framework-a1a87b0ce6bf96f868f18177bebb1a226a5a2753.zip |
Some Navigator API changes based on review (#8859).
The changes in this changeset include
- remove the concept of main view (use empty view name)
- remove View.init()
- use ViewChangeEvent in ViewChangeListener
- remove internal parameters
- add getDisplay() and a constructor that creates a SimpleViewDisplay by
default
Diffstat (limited to 'tests/server-side')
-rw-r--r-- | tests/server-side/com/vaadin/tests/server/navigator/ClassBasedViewProviderTest.java | 8 | ||||
-rw-r--r-- | tests/server-side/com/vaadin/tests/server/navigator/NavigatorTest.java | 204 |
2 files changed, 139 insertions, 73 deletions
diff --git a/tests/server-side/com/vaadin/tests/server/navigator/ClassBasedViewProviderTest.java b/tests/server-side/com/vaadin/tests/server/navigator/ClassBasedViewProviderTest.java index 818229ab2f..72d595bc7d 100644 --- a/tests/server-side/com/vaadin/tests/server/navigator/ClassBasedViewProviderTest.java +++ b/tests/server-side/com/vaadin/tests/server/navigator/ClassBasedViewProviderTest.java @@ -15,14 +15,9 @@ public class ClassBasedViewProviderTest extends TestCase { private ClassBasedViewProvider provider; public static class TestView extends Label implements View { - public boolean initialized = false; public String parameters = null; - public void init() { - initialized = true; - } - - public void navigateTo(String parameters, Object... internalParameters) { + public void navigateTo(String parameters) { this.parameters = parameters; } @@ -169,7 +164,6 @@ public class ClassBasedViewProviderTest extends TestCase { View view = provider.getView("test"); assertNotNull("Did not get view from a provider", view); assertEquals("Incorrect view type", TestView.class, view.getClass()); - assertTrue("View not initialized", ((TestView) view).initialized); } public void testGetViewMultipleRegistered() throws Exception { diff --git a/tests/server-side/com/vaadin/tests/server/navigator/NavigatorTest.java b/tests/server-side/com/vaadin/tests/server/navigator/NavigatorTest.java index d6a7cc68c7..49cd82c2e3 100644 --- a/tests/server-side/com/vaadin/tests/server/navigator/NavigatorTest.java +++ b/tests/server-side/com/vaadin/tests/server/navigator/NavigatorTest.java @@ -4,6 +4,8 @@ package com.vaadin.tests.server.navigator; +import java.util.LinkedList; + import junit.framework.TestCase; import org.easymock.EasyMock; @@ -13,6 +15,7 @@ import com.vaadin.navigator.FragmentManager; import com.vaadin.navigator.Navigator; import com.vaadin.navigator.View; import com.vaadin.navigator.ViewChangeListener; +import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent; import com.vaadin.navigator.ViewDisplay; import com.vaadin.navigator.ViewProvider; @@ -21,6 +24,92 @@ public class NavigatorTest extends TestCase { // TODO test internal parameters (and absence of them) // TODO test listeners blocking navigation, multiple listeners + public static class ViewChangeTestListener implements ViewChangeListener { + private final LinkedList<ViewChangeEvent> referenceEvents = new LinkedList<ViewChangeListener.ViewChangeEvent>(); + private final LinkedList<Boolean> referenceIsCheck = new LinkedList<Boolean>(); + private final LinkedList<Boolean> checkReturnValues = new LinkedList<Boolean>(); + + public void addExpectedIsViewChangeAllowed(ViewChangeEvent event, + boolean returnValue) { + referenceIsCheck.add(true); + referenceEvents.add(event); + checkReturnValues.add(returnValue); + } + + public void addExpectedNavigatorViewChange(ViewChangeEvent event) { + referenceIsCheck.add(false); + referenceEvents.add(event); + } + + public boolean isReady() { + return referenceEvents.isEmpty(); + } + + public boolean equalsReferenceEvent(ViewChangeEvent event, + ViewChangeEvent reference) { + if (event == null) { + return false; + } + if (reference.getNavigator() != event.getNavigator()) { + return false; + } + if (reference.getOldView() != event.getOldView()) { + return false; + } + if (reference.getNewView() != event.getNewView()) { + return false; + } + if (!stringEquals(reference.getViewName(), event.getViewName())) { + return false; + } + if (!stringEquals(reference.getFragmentParameters(), + event.getFragmentParameters())) { + return false; + } + return true; + } + + private static boolean stringEquals(String string1, String string2) { + if (string1 == null) { + return string2 == null; + } else { + return string1.equals(string2); + } + } + + public boolean isViewChangeAllowed(ViewChangeEvent event) { + if (referenceEvents.isEmpty()) { + fail("Unexpected call to isViewChangeAllowed()"); + } + ViewChangeEvent reference = referenceEvents.remove(); + Boolean isCheck = referenceIsCheck.remove(); + if (!isCheck) { + fail("Expected navigatorViewChanged(), received isViewChangeAllowed()"); + } + // here to make sure exactly the correct values are removed from + // each queue + Boolean returnValue = checkReturnValues.remove(); + if (!equalsReferenceEvent(event, reference)) { + fail("View change event does not match reference event"); + } + return returnValue; + } + + public void navigatorViewChanged(ViewChangeEvent event) { + if (referenceEvents.isEmpty()) { + fail("Unexpected call to navigatorViewChanged()"); + } + ViewChangeEvent reference = referenceEvents.remove(); + Boolean isCheck = referenceIsCheck.remove(); + if (isCheck) { + fail("Expected isViewChangeAllowed(), received navigatorViewChanged()"); + } + if (!equalsReferenceEvent(event, reference)) { + fail("View change event does not match reference event"); + } + } + } + public void testBasicNavigation() { IMocksControl control = EasyMock.createControl(); FragmentManager manager = control.createMock(FragmentManager.class); @@ -32,7 +121,6 @@ public class NavigatorTest extends TestCase { // prepare mocks: what to expect EasyMock.expect(provider.getViewName("test1")).andReturn("test1"); EasyMock.expect(provider.getView("test1")).andReturn(view1); - view1.init(); EasyMock.expect(manager.getFragment()).andReturn(""); view1.navigateTo(null); display.showView(view1); @@ -40,7 +128,6 @@ public class NavigatorTest extends TestCase { EasyMock.expect(provider.getViewName("test2/")).andReturn("test2"); EasyMock.expect(provider.getView("test2")).andReturn(view2); - view2.init(); EasyMock.expect(manager.getFragment()).andReturn("view1"); view2.navigateTo(null); display.showView(view2); @@ -49,7 +136,6 @@ public class NavigatorTest extends TestCase { EasyMock.expect(provider.getViewName("test1/params")) .andReturn("test1"); EasyMock.expect(provider.getView("test1")).andReturn(view1); - view1.init(); EasyMock.expect(manager.getFragment()).andReturn("view2"); view1.navigateTo("params"); display.showView(view1); @@ -75,25 +161,15 @@ public class NavigatorTest extends TestCase { View view2 = control.createMock(View.class); // prepare mocks: what to expect - EasyMock.expect(provider.getViewName("test1")).andReturn("test1"); - EasyMock.expect(provider.getView("test1")).andReturn(view1); - view1.init(); - EasyMock.expect(manager.getFragment()).andReturn(""); - view1.navigateTo(null); - display.showView(view1); - manager.setFragment("test1"); - EasyMock.expect(provider.getViewName("test2")).andReturn("test2"); EasyMock.expect(provider.getView("test2")).andReturn(view2); - view2.init(); EasyMock.expect(manager.getFragment()).andReturn("view1"); view2.navigateTo(null); display.showView(view2); manager.setFragment("test2"); - EasyMock.expect(provider.getViewName("test1")).andReturn("test1"); + EasyMock.expect(provider.getViewName("")).andReturn("test1"); EasyMock.expect(provider.getView("test1")).andReturn(view1); - view1.init(); EasyMock.expect(manager.getFragment()).andReturn(""); view1.navigateTo(null); display.showView(view1); @@ -102,7 +178,6 @@ public class NavigatorTest extends TestCase { EasyMock.expect(provider.getViewName("test1/params")) .andReturn("test1"); EasyMock.expect(provider.getView("test1")).andReturn(view1); - view1.init(); EasyMock.expect(manager.getFragment()).andReturn("view2"); view1.navigateTo("params"); display.showView(view1); @@ -113,8 +188,6 @@ public class NavigatorTest extends TestCase { // create and test navigator Navigator navigator = new Navigator(manager, display); navigator.registerProvider(provider); - // this also triggers navigation - navigator.setMainView("test1"); navigator.navigateTo("test2"); navigator.navigateTo(""); @@ -128,43 +201,46 @@ public class NavigatorTest extends TestCase { ViewProvider provider = control.createMock(ViewProvider.class); View view1 = control.createMock(View.class); View view2 = control.createMock(View.class); - ViewChangeListener listener = control - .createMock(ViewChangeListener.class); + ViewChangeTestListener listener = new ViewChangeTestListener(); + + // create navigator to test + Navigator navigator = new Navigator(manager, display); // prepare mocks: what to expect EasyMock.expect(provider.getViewName("test1")).andReturn("test1"); EasyMock.expect(provider.getView("test1")).andReturn(view1); - view1.init(); + ViewChangeEvent event1 = new ViewChangeEvent(navigator, null, view1, + "test1", null); + listener.addExpectedIsViewChangeAllowed(event1, true); EasyMock.expect(manager.getFragment()).andReturn(""); - EasyMock.expect( - listener.isViewChangeAllowed(null, view1, "test1", null, - new Object[0])).andReturn(true); view1.navigateTo(null); display.showView(view1); manager.setFragment("test1"); - listener.navigatorViewChanged(null, view1); + listener.addExpectedNavigatorViewChange(event1); EasyMock.expect(provider.getViewName("test2")).andReturn("test2"); EasyMock.expect(provider.getView("test2")).andReturn(view2); - view2.init(); + ViewChangeEvent event2 = new ViewChangeEvent(navigator, view1, view2, + "test2", null); + listener.addExpectedIsViewChangeAllowed(event2, true); EasyMock.expect(manager.getFragment()).andReturn("view1"); - EasyMock.expect( - listener.isViewChangeAllowed(view1, view2, "test2", null, - new Object[0])).andReturn(true); view2.navigateTo(null); display.showView(view2); manager.setFragment("test2"); - listener.navigatorViewChanged(view1, view2); + listener.addExpectedNavigatorViewChange(event2); control.replay(); - // create and test navigator - Navigator navigator = new Navigator(manager, display); + // test navigator navigator.registerProvider(provider); navigator.addListener(listener); navigator.navigateTo("test1"); navigator.navigateTo("test2"); + + if (!listener.isReady()) { + fail("Missing listener calls"); + } } public void testBlockNavigation() { @@ -174,71 +250,60 @@ public class NavigatorTest extends TestCase { ViewProvider provider = control.createMock(ViewProvider.class); View view1 = control.createMock(View.class); View view2 = control.createMock(View.class); - ViewChangeListener listener1 = control - .createMock(ViewChangeListener.class); - ViewChangeListener listener2 = control - .createMock(ViewChangeListener.class); + ViewChangeTestListener listener1 = new ViewChangeTestListener(); + ViewChangeTestListener listener2 = new ViewChangeTestListener(); + + Navigator navigator = new Navigator(manager, display); // prepare mocks: what to expect // first listener blocks first view change EasyMock.expect(provider.getViewName("test1")).andReturn("test1"); EasyMock.expect(provider.getView("test1")).andReturn(view1); - view1.init(); EasyMock.expect(manager.getFragment()).andReturn(""); - EasyMock.expect( - listener1.isViewChangeAllowed(null, view1, "test1", null, - new Object[0])).andReturn(false); + ViewChangeEvent event1 = new ViewChangeEvent(navigator, null, view1, + "test1", null); + listener1.addExpectedIsViewChangeAllowed(event1, false); // second listener blocks second view change EasyMock.expect(provider.getViewName("test1/test")).andReturn("test1"); EasyMock.expect(provider.getView("test1")).andReturn(view1); - view1.init(); EasyMock.expect(manager.getFragment()).andReturn(""); - EasyMock.expect( - listener1.isViewChangeAllowed(null, view1, "test1", "test", - new Object[0])).andReturn(true); - EasyMock.expect( - listener2.isViewChangeAllowed(null, view1, "test1", "test", - new Object[0])).andReturn(false); + ViewChangeEvent event2 = new ViewChangeEvent(navigator, null, view1, + "test1", "test"); + listener1.addExpectedIsViewChangeAllowed(event2, true); + listener2.addExpectedIsViewChangeAllowed(event2, false); // both listeners allow view change EasyMock.expect(provider.getViewName("test1/bar")).andReturn("test1"); EasyMock.expect(provider.getView("test1")).andReturn(view1); - view1.init(); EasyMock.expect(manager.getFragment()).andReturn(""); - EasyMock.expect( - listener1.isViewChangeAllowed(null, view1, "test1", "bar", - new Object[0])).andReturn(true); - EasyMock.expect( - listener2.isViewChangeAllowed(null, view1, "test1", "bar", - new Object[0])).andReturn(true); + ViewChangeEvent event3 = new ViewChangeEvent(navigator, null, view1, + "test1", "bar"); + listener1.addExpectedIsViewChangeAllowed(event3, true); + listener2.addExpectedIsViewChangeAllowed(event3, true); view1.navigateTo("bar"); display.showView(view1); manager.setFragment("test1/bar"); - listener1.navigatorViewChanged(null, view1); - listener2.navigatorViewChanged(null, view1); + listener1.addExpectedNavigatorViewChange(event3); + listener2.addExpectedNavigatorViewChange(event3); // both listeners allow view change from non-null view EasyMock.expect(provider.getViewName("test2")).andReturn("test2"); EasyMock.expect(provider.getView("test2")).andReturn(view2); - view2.init(); EasyMock.expect(manager.getFragment()).andReturn("view1"); - EasyMock.expect( - listener1.isViewChangeAllowed(view1, view2, "test2", null, - new Object[0])).andReturn(true); - EasyMock.expect( - listener2.isViewChangeAllowed(view1, view2, "test2", null, - new Object[0])).andReturn(true); + ViewChangeEvent event4 = new ViewChangeEvent(navigator, view1, view2, + "test2", null); + listener1.addExpectedIsViewChangeAllowed(event4, true); + listener2.addExpectedIsViewChangeAllowed(event4, true); view2.navigateTo(null); display.showView(view2); manager.setFragment("test2"); - listener1.navigatorViewChanged(view1, view2); - listener2.navigatorViewChanged(view1, view2); + listener1.addExpectedNavigatorViewChange(event4); + listener2.addExpectedNavigatorViewChange(event4); control.replay(); - // create and test navigator - Navigator navigator = new Navigator(manager, display); + // test navigator navigator.registerProvider(provider); navigator.addListener(listener1); navigator.addListener(listener2); @@ -247,6 +312,13 @@ public class NavigatorTest extends TestCase { navigator.navigateTo("test1/test"); navigator.navigateTo("test1/bar"); navigator.navigateTo("test2"); + + if (!listener1.isReady()) { + fail("Missing listener calls for listener1"); + } + if (!listener2.isReady()) { + fail("Missing listener calls for listener2"); + } } } |