summaryrefslogtreecommitdiffstats
path: root/tests/server-side
diff options
context:
space:
mode:
authorHenri Sara <hesara@vaadin.com>2012-05-24 13:07:19 +0300
committerHenri Sara <hesara@vaadin.com>2012-05-24 13:07:33 +0300
commit338563e4d772c395da863d8764bca4a4b759cbe3 (patch)
tree98e7c3e0176bacdfa3321ef2df261b74d8e665ee /tests/server-side
parente1d2b8f1797e9e373161cfc7658b1e5386f98125 (diff)
downloadvaadin-framework-338563e4d772c395da863d8764bca4a4b759cbe3.tar.gz
vaadin-framework-338563e4d772c395da863d8764bca4a4b759cbe3.zip
Implement first version of view and navigation APIs (#8859).
Diffstat (limited to 'tests/server-side')
-rw-r--r--tests/server-side/com/vaadin/tests/server/navigator/ClassBasedViewProviderTest.java208
-rw-r--r--tests/server-side/com/vaadin/tests/server/navigator/NavigatorTest.java252
-rw-r--r--tests/server-side/com/vaadin/tests/server/navigator/UriFragmentManagerTest.java51
3 files changed, 511 insertions, 0 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
new file mode 100644
index 0000000000..818229ab2f
--- /dev/null
+++ b/tests/server-side/com/vaadin/tests/server/navigator/ClassBasedViewProviderTest.java
@@ -0,0 +1,208 @@
+/*
+@VaadinApache2LicenseForJavaFiles@
+ */
+
+package com.vaadin.tests.server.navigator;
+
+import junit.framework.TestCase;
+
+import com.vaadin.navigator.Navigator.ClassBasedViewProvider;
+import com.vaadin.navigator.View;
+import com.vaadin.ui.Label;
+
+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) {
+ this.parameters = parameters;
+ }
+
+ }
+
+ public static class TestView2 extends TestView {
+
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ provider = new ClassBasedViewProvider();
+ }
+
+ public void testAddViewWithNullName() throws Exception {
+ try {
+ provider.addView(null, TestView.class);
+ fail("Should not be able to add view with null name");
+ } catch (IllegalArgumentException e) {
+ }
+ }
+
+ public void testAddViewWithEmptyStringName() throws Exception {
+ try {
+ provider.addView("", TestView.class);
+ fail("Should not be able to add view with empty name");
+ } catch (IllegalArgumentException e) {
+ }
+ }
+
+ public void testAddViewNull() throws Exception {
+ try {
+ provider.addView("test", null);
+ fail("Should not be able to add null view");
+ } catch (IllegalArgumentException e) {
+ }
+ }
+
+ public void testAddViewSameName() throws Exception {
+ try {
+ provider.addView("test", TestView.class);
+ provider.addView("test", TestView2.class);
+ fail("Should not be able to add two views with same name");
+ } catch (IllegalArgumentException e) {
+ }
+ }
+
+ public void testAddViewSameClass() throws Exception {
+ try {
+ provider.addView("test", TestView.class);
+ provider.addView("test2", TestView.class);
+ fail("Should not be able to add same view class with two different names");
+ } catch (IllegalArgumentException e) {
+ }
+ }
+
+ public void testGetViewNameForNullString() throws Exception {
+ assertNull(
+ "Found view name for null view string in an empty view provider",
+ provider.getViewName((String) null));
+
+ provider.addView("test", TestView.class);
+ assertNull("Found view name for null view string",
+ provider.getViewName((String) null));
+ }
+
+ public void testGetViewNameForNullClass() throws Exception {
+ assertNull("Found view name for null class",
+ provider.getViewName((Class<View>) null));
+ }
+
+ public void testGetViewNameForEmptyString() throws Exception {
+ assertNull(
+ "Found view name for empty view string in an empty provider",
+ provider.getViewName(""));
+ provider.addView("test", TestView.class);
+ assertNull("Found view name for empty view string",
+ provider.getViewName(""));
+ }
+
+ public void testGetViewNameForClass() throws Exception {
+ provider.addView("test", TestView.class);
+ assertEquals("No view name found for view class", "test",
+ provider.getViewName(TestView.class));
+ }
+
+ public void testGetViewNameWithParameters() throws Exception {
+ provider.addView("test", TestView.class);
+ assertEquals("Incorrect view name found for view string", "test",
+ provider.getViewName("test"));
+ assertEquals(
+ "Incorrect view name found for view string ending with slash",
+ "test", provider.getViewName("test/"));
+ assertEquals(
+ "Incorrect view name found for view string with parameters",
+ "test", provider.getViewName("test/params/are/here"));
+ }
+
+ public void testGetViewNameMultipleRegisteredWithParameters()
+ throws Exception {
+ provider.addView("test", TestView.class);
+ provider.addView("test2", TestView2.class);
+ assertEquals("Incorrect view name found for view string", "test",
+ provider.getViewName("test/test2/params"));
+ }
+
+ public void testGetViewNameNestedNames() throws Exception {
+ provider.addView("test/subview", TestView2.class);
+ provider.addView("test", TestView.class);
+ assertEquals("Incorrect view name found for subview string",
+ "test/subview", provider.getViewName("test/subview"));
+ assertEquals(
+ "Incorrect view name found for subview string with empty parameters",
+ "test/subview", provider.getViewName("test/subview/"));
+ assertEquals(
+ "Incorrect view name found for subview string with parameters",
+ "test/subview", provider.getViewName("test/subview/parameters"));
+ assertEquals("Incorrect view name found for top level view string",
+ "test", provider.getViewName("test"));
+ assertEquals(
+ "Incorrect view name found for top level view string with empty parameters",
+ "test", provider.getViewName("test/"));
+ assertEquals(
+ "Incorrect view name found for top level view string with parameters starting like subview name",
+ "test", provider.getViewName("test/subviewnothere"));
+ }
+
+ public void testGetViewClass() throws Exception {
+ assertNull("View class found for empty view provider",
+ provider.getViewClass("test"));
+ provider.addView("test", TestView.class);
+ assertEquals("View class not found", TestView.class,
+ provider.getViewClass("test"));
+ assertNull("View class found for unregistered view name",
+ provider.getViewClass("test2"));
+ }
+
+ public void testGetViewSimple() throws Exception {
+ assertNull("Found view in an empty view provider",
+ provider.getViewName("test"));
+
+ provider.addView("test", TestView.class);
+ 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 {
+ provider.addView("test", TestView.class);
+ provider.addView("test2", TestView2.class);
+ assertEquals("Incorrect view type", TestView.class,
+ provider.getView("test").getClass());
+ assertEquals("Incorrect view type", TestView2.class,
+ provider.getView("test2").getClass());
+ assertEquals("Incorrect view type", TestView.class,
+ provider.getView("test").getClass());
+ }
+
+ public void testRemoveView() throws Exception {
+ provider.addView("test", TestView.class);
+ assertNotNull("Did not get view from a provider",
+ provider.getView("test"));
+ provider.removeView("test");
+ assertNull("View class found for removed view name",
+ provider.getViewClass("test"));
+ assertNull("View name found for removed view",
+ provider.getViewName(TestView.class));
+ // cached view?
+ assertNull(
+ "Received view instance from a provider after removing view type",
+ provider.getView("test"));
+ }
+
+ public void testGetViewCached() throws Exception {
+ provider.addView("test", TestView.class);
+ View view1 = provider.getView("test");
+ View view2 = provider.getView("test");
+ assertSame("View instance not cached", view1, view2);
+ }
+
+}
diff --git a/tests/server-side/com/vaadin/tests/server/navigator/NavigatorTest.java b/tests/server-side/com/vaadin/tests/server/navigator/NavigatorTest.java
new file mode 100644
index 0000000000..d6a7cc68c7
--- /dev/null
+++ b/tests/server-side/com/vaadin/tests/server/navigator/NavigatorTest.java
@@ -0,0 +1,252 @@
+/*
+@VaadinApache2LicenseForJavaFiles@
+ */
+
+package com.vaadin.tests.server.navigator;
+
+import junit.framework.TestCase;
+
+import org.easymock.EasyMock;
+import org.easymock.IMocksControl;
+
+import com.vaadin.navigator.FragmentManager;
+import com.vaadin.navigator.Navigator;
+import com.vaadin.navigator.View;
+import com.vaadin.navigator.ViewChangeListener;
+import com.vaadin.navigator.ViewDisplay;
+import com.vaadin.navigator.ViewProvider;
+
+public class NavigatorTest extends TestCase {
+
+ // TODO test internal parameters (and absence of them)
+ // TODO test listeners blocking navigation, multiple listeners
+
+ public void testBasicNavigation() {
+ IMocksControl control = EasyMock.createControl();
+ FragmentManager manager = control.createMock(FragmentManager.class);
+ ViewDisplay display = control.createMock(ViewDisplay.class);
+ ViewProvider provider = control.createMock(ViewProvider.class);
+ View view1 = control.createMock(View.class);
+ 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/params"))
+ .andReturn("test1");
+ EasyMock.expect(provider.getView("test1")).andReturn(view1);
+ view1.init();
+ EasyMock.expect(manager.getFragment()).andReturn("view2");
+ view1.navigateTo("params");
+ display.showView(view1);
+ manager.setFragment("test1/params");
+
+ control.replay();
+
+ // create and test navigator
+ Navigator navigator = new Navigator(manager, display);
+ navigator.registerProvider(provider);
+
+ navigator.navigateTo("test1");
+ navigator.navigateTo("test2/");
+ navigator.navigateTo("test1/params");
+ }
+
+ public void testMainView() {
+ IMocksControl control = EasyMock.createControl();
+ FragmentManager manager = control.createMock(FragmentManager.class);
+ ViewDisplay display = control.createMock(ViewDisplay.class);
+ ViewProvider provider = control.createMock(ViewProvider.class);
+ View view1 = control.createMock(View.class);
+ 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.getView("test1")).andReturn(view1);
+ view1.init();
+ EasyMock.expect(manager.getFragment()).andReturn("");
+ view1.navigateTo(null);
+ display.showView(view1);
+ manager.setFragment("test1");
+
+ 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);
+ manager.setFragment("test1/params");
+
+ control.replay();
+
+ // 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("");
+ navigator.navigateTo("test1/params");
+ }
+
+ public void testListeners() {
+ IMocksControl control = EasyMock.createControl();
+ FragmentManager manager = control.createMock(FragmentManager.class);
+ ViewDisplay display = control.createMock(ViewDisplay.class);
+ ViewProvider provider = control.createMock(ViewProvider.class);
+ View view1 = control.createMock(View.class);
+ View view2 = control.createMock(View.class);
+ ViewChangeListener listener = control
+ .createMock(ViewChangeListener.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("");
+ 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);
+
+ EasyMock.expect(provider.getViewName("test2")).andReturn("test2");
+ EasyMock.expect(provider.getView("test2")).andReturn(view2);
+ view2.init();
+ 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);
+
+ control.replay();
+
+ // create and test navigator
+ Navigator navigator = new Navigator(manager, display);
+ navigator.registerProvider(provider);
+ navigator.addListener(listener);
+
+ navigator.navigateTo("test1");
+ navigator.navigateTo("test2");
+ }
+
+ public void testBlockNavigation() {
+ IMocksControl control = EasyMock.createControl();
+ FragmentManager manager = control.createMock(FragmentManager.class);
+ ViewDisplay display = control.createMock(ViewDisplay.class);
+ 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);
+
+ // 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);
+
+ // 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);
+
+ // 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);
+ view1.navigateTo("bar");
+ display.showView(view1);
+ manager.setFragment("test1/bar");
+ listener1.navigatorViewChanged(null, view1);
+ listener2.navigatorViewChanged(null, view1);
+
+ // 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);
+ view2.navigateTo(null);
+ display.showView(view2);
+ manager.setFragment("test2");
+ listener1.navigatorViewChanged(view1, view2);
+ listener2.navigatorViewChanged(view1, view2);
+
+ control.replay();
+
+ // create and test navigator
+ Navigator navigator = new Navigator(manager, display);
+ navigator.registerProvider(provider);
+ navigator.addListener(listener1);
+ navigator.addListener(listener2);
+
+ navigator.navigateTo("test1");
+ navigator.navigateTo("test1/test");
+ navigator.navigateTo("test1/bar");
+ navigator.navigateTo("test2");
+ }
+
+}
diff --git a/tests/server-side/com/vaadin/tests/server/navigator/UriFragmentManagerTest.java b/tests/server-side/com/vaadin/tests/server/navigator/UriFragmentManagerTest.java
new file mode 100644
index 0000000000..cfbf50f256
--- /dev/null
+++ b/tests/server-side/com/vaadin/tests/server/navigator/UriFragmentManagerTest.java
@@ -0,0 +1,51 @@
+/*
+@VaadinApache2LicenseForJavaFiles@
+ */
+
+package com.vaadin.tests.server.navigator;
+
+import junit.framework.TestCase;
+
+import org.easymock.EasyMock;
+import org.easymock.IMocksControl;
+
+import com.vaadin.navigator.Navigator;
+import com.vaadin.navigator.Navigator.UriFragmentManager;
+import com.vaadin.ui.Root;
+import com.vaadin.ui.Root.FragmentChangedEvent;
+
+public class UriFragmentManagerTest extends TestCase {
+
+ public void testGetSetFragment() {
+ Root root = EasyMock.createMock(Root.class);
+ UriFragmentManager manager = new UriFragmentManager(root, null);
+
+ // prepare mock
+ EasyMock.expect(root.getFragment()).andReturn("");
+ root.setFragment("test");
+ EasyMock.expect(root.getFragment()).andReturn("test");
+ EasyMock.replay(root);
+
+ // test manager using the mock
+ assertEquals("Incorrect fragment value", "", manager.getFragment());
+ manager.setFragment("test");
+ assertEquals("Incorrect fragment value", "test", manager.getFragment());
+ }
+
+ public void testListener() {
+ // create mocks
+ IMocksControl control = EasyMock.createControl();
+ Navigator navigator = control.createMock(Navigator.class);
+ Root root = control.createMock(Root.class);
+
+ UriFragmentManager manager = new UriFragmentManager(root, navigator);
+
+ EasyMock.expect(root.getFragment()).andReturn("test");
+ navigator.navigateTo("test");
+ control.replay();
+
+ FragmentChangedEvent event = root.new FragmentChangedEvent(root,
+ "oldtest");
+ manager.fragmentChanged(event);
+ }
+}