summaryrefslogtreecommitdiffstats
path: root/tests/server-side/com
diff options
context:
space:
mode:
authorJohannes Dahlström <johannesd@vaadin.com>2012-06-13 17:46:09 +0300
committerJohannes Dahlström <johannesd@vaadin.com>2012-06-13 17:46:09 +0300
commit52d06a13b9e20cb752a21413e15adc250daa5e1c (patch)
treeac0e5878490d802960d9463354721ce1820b4aa8 /tests/server-side/com
parentde5fd8e0aefa0c43e2b0fe724102bca92eeaaf96 (diff)
parentadcf03c1b5f05b38cda25819b732745c1a4859fc (diff)
downloadvaadin-framework-52d06a13b9e20cb752a21413e15adc250daa5e1c.tar.gz
vaadin-framework-52d06a13b9e20cb752a21413e15adc250daa5e1c.zip
Merge remote-tracking branch 'origin/master'
Conflicts: src/com/vaadin/terminal/gwt/server/DragAndDropService.java
Diffstat (limited to 'tests/server-side/com')
-rw-r--r--tests/server-side/com/vaadin/tests/server/navigator/ClassBasedViewProviderTest.java183
-rw-r--r--tests/server-side/com/vaadin/tests/server/navigator/NavigatorTest.java449
2 files changed, 429 insertions, 203 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..fc0d1b60a0 100644
--- a/tests/server-side/com/vaadin/tests/server/navigator/ClassBasedViewProviderTest.java
+++ b/tests/server-side/com/vaadin/tests/server/navigator/ClassBasedViewProviderTest.java
@@ -12,17 +12,10 @@ 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) {
+ public void navigateTo(String parameters) {
this.parameters = parameters;
}
@@ -32,86 +25,68 @@ public class ClassBasedViewProviderTest extends TestCase {
}
- @Override
- protected void setUp() throws Exception {
- super.setUp();
- provider = new ClassBasedViewProvider();
- }
-
- public void testAddViewWithNullName() throws Exception {
+ public void testCreateProviderWithNullName() throws Exception {
try {
- provider.addView(null, TestView.class);
- fail("Should not be able to add view with null name");
+ new ClassBasedViewProvider(null, TestView.class);
+ fail("Should not be able to create view provider 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 testCreateProviderWithEmptyStringName() throws Exception {
+ new ClassBasedViewProvider("", TestView.class);
}
- public void testAddViewNull() throws Exception {
+ public void testCreateProviderNullViewClass() throws Exception {
try {
- provider.addView("test", null);
- fail("Should not be able to add null view");
+ new ClassBasedViewProvider("test", null);
+ fail("Should not be able to create view provider with null view class");
} 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 testViewNameGetter() throws Exception {
+ ClassBasedViewProvider provider1 = new ClassBasedViewProvider("",
+ TestView.class);
+ assertEquals("View name should be empty", "", provider1.getViewName());
+
+ ClassBasedViewProvider provider2 = new ClassBasedViewProvider("test",
+ TestView.class);
+ assertEquals("View name does not match", "test",
+ provider2.getViewName());
}
- 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 testViewClassGetter() throws Exception {
+ ClassBasedViewProvider provider = new ClassBasedViewProvider("test",
+ TestView.class);
+ assertEquals("Incorrect view class returned by getter", TestView.class,
+ provider.getViewClass());
}
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",
+ ClassBasedViewProvider provider = new ClassBasedViewProvider("test",
+ TestView.class);
+ assertNull("Received 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(""));
- }
+ ClassBasedViewProvider provider1 = new ClassBasedViewProvider("",
+ TestView.class);
+ assertEquals(
+ "Did not find view name for empty view string in a provider with empty string registered",
+ "", provider1.getViewName(""));
- public void testGetViewNameForClass() throws Exception {
- provider.addView("test", TestView.class);
- assertEquals("No view name found for view class", "test",
- provider.getViewName(TestView.class));
+ ClassBasedViewProvider provider2 = new ClassBasedViewProvider("test",
+ TestView.class);
+ assertNull(
+ "Found view name for empty view string when none registered",
+ provider2.getViewName(""));
}
public void testGetViewNameWithParameters() throws Exception {
- provider.addView("test", TestView.class);
+ ClassBasedViewProvider provider = new ClassBasedViewProvider("test",
+ TestView.class);
assertEquals("Incorrect view name found for view string", "test",
provider.getViewName("test"));
assertEquals(
@@ -122,87 +97,21 @@ public class ClassBasedViewProviderTest extends TestCase {
"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"));
+ public void testGetView() throws Exception {
+ ClassBasedViewProvider provider = new ClassBasedViewProvider("test",
+ TestView.class);
- 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 testGetViewIncorrectViewName() throws Exception {
+ ClassBasedViewProvider provider = new ClassBasedViewProvider("test",
+ TestView.class);
- 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);
+ View view = provider.getView("test2");
+ assertNull("Got view from a provider for incorrect view name", view);
}
}
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..ab983b4022 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;
@@ -11,16 +13,146 @@ import org.easymock.IMocksControl;
import com.vaadin.navigator.FragmentManager;
import com.vaadin.navigator.Navigator;
+import com.vaadin.navigator.Navigator.SimpleViewDisplay;
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;
+import com.vaadin.tests.server.navigator.ClassBasedViewProviderTest.TestView;
+import com.vaadin.tests.server.navigator.ClassBasedViewProviderTest.TestView2;
+import com.vaadin.ui.Root;
public class NavigatorTest extends TestCase {
// TODO test internal parameters (and absence of them)
// TODO test listeners blocking navigation, multiple listeners
+ public static class NullDisplay implements ViewDisplay {
+ public void showView(View view) {
+ // do nothing
+ }
+ }
+
+ public static class NullFragmentManager implements FragmentManager {
+ public String getFragment() {
+ return null;
+ }
+
+ public void setFragment(String fragment) {
+ // do nothing
+ }
+ }
+
+ public static class TestDisplay implements ViewDisplay {
+ private View currentView;
+
+ public void showView(View view) {
+ currentView = view;
+ }
+
+ public View getCurrentView() {
+ return currentView;
+ }
+ }
+
+ public static class TestNavigator extends Navigator {
+ public TestNavigator() {
+ super(new NullFragmentManager(), new TestDisplay());
+ }
+
+ public View getView(String viewAndParameters) {
+ navigateTo(viewAndParameters);
+ return ((TestDisplay) getDisplay()).getCurrentView();
+ }
+ }
+
+ 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 +164,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 +171,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 +179,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 +204,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 +221,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 +231,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 +244,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 +293,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 +355,215 @@ 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");
+ }
+ }
+
+ public void testDefaultDisplayType() {
+ IMocksControl control = EasyMock.createControl();
+ Root root = control.createMock(Root.class);
+
+ Navigator navigator = new Navigator(root);
+
+ assertEquals("Default display should be a SimpleViewDisplay",
+ SimpleViewDisplay.class, navigator.getDisplay().getClass());
+ }
+
+ public void testAddViewInstance() throws Exception {
+ View view = new TestView();
+
+ TestNavigator navigator = new TestNavigator();
+
+ navigator.addView("test", view);
+
+ assertEquals("Registered view instance not returned by navigator",
+ view, navigator.getView("test"));
}
+ public void testAddViewInstanceSameName() throws Exception {
+ View view1 = new TestView();
+ View view2 = new TestView2();
+
+ TestNavigator navigator = new TestNavigator();
+
+ navigator.addView("test", view1);
+ navigator.addView("test", view2);
+
+ assertEquals(
+ "Adding second view with same name should override previous view",
+ view2, navigator.getView("test"));
+ }
+
+ public void testAddViewClass() throws Exception {
+ TestNavigator navigator = new TestNavigator();
+
+ navigator.addView("test", TestView.class);
+
+ View view = navigator.getView("test");
+ assertNotNull("Received null view", view);
+ assertEquals("Received incorrect type of view", TestView.class,
+ view.getClass());
+ }
+
+ public void testAddViewClassSameName() throws Exception {
+ TestNavigator navigator = new TestNavigator();
+
+ navigator.addView("test", TestView.class);
+ navigator.addView("test", TestView2.class);
+
+ assertEquals(
+ "Adding second view class with same name should override previous view",
+ TestView2.class, navigator.getView("test").getClass());
+ }
+
+ public void testAddViewInstanceAndClassSameName() throws Exception {
+ TestNavigator navigator = new TestNavigator();
+
+ navigator.addView("test", TestView.class);
+ TestView2 view2 = new TestView2();
+ navigator.addView("test", view2);
+
+ assertEquals(
+ "Adding second view class with same name should override previous view",
+ view2, navigator.getView("test"));
+
+ navigator.addView("test", TestView.class);
+
+ assertEquals(
+ "Adding second view class with same name should override previous view",
+ TestView.class, navigator.getView("test").getClass());
+ }
+
+ public void testAddViewWithNullName() throws Exception {
+ Navigator navigator = new Navigator(new NullFragmentManager(),
+ new NullDisplay());
+
+ try {
+ navigator.addView(null, new TestView());
+ fail("addView() accepted null view name");
+ } catch (IllegalArgumentException e) {
+ }
+ try {
+ navigator.addView(null, TestView.class);
+ fail("addView() accepted null view name");
+ } catch (IllegalArgumentException e) {
+ }
+ }
+
+ public void testAddViewWithNullInstance() throws Exception {
+ Navigator navigator = new Navigator(new NullFragmentManager(),
+ new NullDisplay());
+
+ try {
+ navigator.addView("test", (View) null);
+ fail("addView() accepted null view instance");
+ } catch (IllegalArgumentException e) {
+ }
+ }
+
+ public void testAddViewWithNullClass() throws Exception {
+ Navigator navigator = new Navigator(new NullFragmentManager(),
+ new NullDisplay());
+
+ try {
+ navigator.addView("test", (Class<View>) null);
+ fail("addView() accepted null view class");
+ } catch (IllegalArgumentException e) {
+ }
+ }
+
+ public void testRemoveViewInstance() throws Exception {
+ View view = new TestView();
+
+ TestNavigator navigator = new TestNavigator();
+
+ navigator.addView("test", view);
+ navigator.removeView("test");
+
+ assertNull("View not removed", navigator.getView("test"));
+ }
+
+ public void testRemoveViewInstanceNothingElse() throws Exception {
+ View view = new TestView();
+ View view2 = new TestView2();
+
+ TestNavigator navigator = new TestNavigator();
+
+ navigator.addView("test", view);
+ navigator.addView("test2", view2);
+ navigator.removeView("test");
+
+ assertEquals("Removed extra views", view2, navigator.getView("test2"));
+ }
+
+ public void testRemoveViewClass() throws Exception {
+ TestNavigator navigator = new TestNavigator();
+
+ navigator.addView("test", TestView.class);
+ navigator.removeView("test");
+
+ assertNull("View not removed", navigator.getView("test"));
+ }
+
+ public void testRemoveViewClassNothingElse() throws Exception {
+ TestNavigator navigator = new TestNavigator();
+
+ navigator.addView("test", TestView.class);
+ navigator.addView("test2", TestView2.class);
+ navigator.removeView("test");
+
+ assertEquals("Removed extra views", TestView2.class,
+ navigator.getView("test2").getClass());
+ }
+
+ public void testGetViewNestedNames() throws Exception {
+ TestNavigator navigator = new TestNavigator();
+
+ navigator.addView("test/subview", TestView2.class);
+ navigator.addView("test", TestView.class);
+
+ assertEquals("Incorrect view name found for subview string",
+ TestView2.class, navigator.getView("test/subview").getClass());
+ assertEquals(
+ "Incorrect view name found for subview string with empty parameters",
+ TestView2.class, navigator.getView("test/subview/").getClass());
+ assertEquals(
+ "Incorrect view name found for subview string with parameters",
+ TestView2.class, navigator.getView("test/subview/parameters")
+ .getClass());
+ assertEquals("Incorrect view name found for top level view string",
+ TestView.class, navigator.getView("test").getClass());
+ assertEquals(
+ "Incorrect view name found for top level view string with empty parameters",
+ TestView.class, navigator.getView("test/").getClass());
+ assertEquals(
+ "Incorrect view name found for top level view string with parameters starting like subview name",
+ TestView.class, navigator.getView("test/subviewnothere")
+ .getClass());
+ }
+
+ public void testGetViewLongestPrefixOrder() throws Exception {
+ TestNavigator navigator = new TestNavigator();
+
+ navigator.addView("test/subview", TestView2.class);
+ navigator.addView("test", TestView.class);
+
+ assertEquals("Incorrect view name found", TestView.class, navigator
+ .getView("test").getClass());
+
+ // other order
+
+ TestNavigator navigator2 = new TestNavigator();
+
+ navigator2.addView("test", TestView.class);
+ navigator2.addView("test/subview", TestView2.class);
+
+ assertEquals("Incorrect view name found", TestView.class, navigator2
+ .getView("test").getClass());
+ }
}