You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractFieldValueChangeTestBase.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package com.vaadin.v7.tests.server.component.abstractfield;
  2. import org.easymock.EasyMock;
  3. import org.junit.Test;
  4. import com.vaadin.v7.data.Property.ValueChangeEvent;
  5. import com.vaadin.v7.data.Property.ValueChangeListener;
  6. import com.vaadin.v7.data.Property.ValueChangeNotifier;
  7. import com.vaadin.v7.data.util.ObjectProperty;
  8. import com.vaadin.v7.ui.AbstractField;
  9. /**
  10. * Base class for tests for checking that value change listeners for fields are
  11. * not called exactly once when they should be, and not at other times.
  12. *
  13. * Does not check all cases (e.g. properties that do not implement
  14. * {@link ValueChangeNotifier}).
  15. *
  16. * Subclasses should implement {@link #setValue()} and call
  17. * <code>super.setValue(LegacyAbstractField)</code>. Also, subclasses should
  18. * typically override {@link #setValue(AbstractField)} to set the field value
  19. * via <code>changeVariables()</code>.
  20. */
  21. public abstract class AbstractFieldValueChangeTestBase<T> {
  22. private AbstractField<T> field;
  23. private ValueChangeListener listener;
  24. protected void setUp(AbstractField<T> field) {
  25. this.field = field;
  26. listener = EasyMock.createStrictMock(ValueChangeListener.class);
  27. }
  28. protected ValueChangeListener getListener() {
  29. return listener;
  30. }
  31. /**
  32. * Test that listeners are not called when they have been unregistered.
  33. */
  34. @Test
  35. public void testRemoveListener() {
  36. getField().setPropertyDataSource(new ObjectProperty<String>(""));
  37. getField().setBuffered(false);
  38. // Expectations and start test
  39. listener.valueChange(EasyMock.isA(ValueChangeEvent.class));
  40. EasyMock.replay(listener);
  41. // Add listener and set the value -> should end up in listener once
  42. getField().addListener(listener);
  43. setValue(getField());
  44. // Ensure listener was called once
  45. EasyMock.verify(listener);
  46. // Remove the listener and set the value -> should not end up in
  47. // listener
  48. getField().removeListener(listener);
  49. setValue(getField());
  50. // Ensure listener still has been called only once
  51. EasyMock.verify(listener);
  52. }
  53. /**
  54. * Common unbuffered case: both writeThrough (auto-commit) and readThrough
  55. * are on. Calling commit() should not cause notifications.
  56. *
  57. * Using the readThrough mode allows changes made to the property value to
  58. * be seen in some cases also when there is no notification of value change
  59. * from the property.
  60. *
  61. * LegacyField value change notifications closely mirror value changes of
  62. * the data source behind the field.
  63. */
  64. @Test
  65. public void testNonBuffered() {
  66. getField().setPropertyDataSource(new ObjectProperty<String>(""));
  67. getField().setBuffered(false);
  68. expectValueChangeFromSetValueNotCommit();
  69. }
  70. /**
  71. * Fully buffered use where the data source is neither read nor modified
  72. * during editing, and is updated at commit().
  73. *
  74. * LegacyField value change notifications reflect the buffered value in the
  75. * field, not the original data source value changes.
  76. */
  77. public void testBuffered() {
  78. getField().setPropertyDataSource(new ObjectProperty<String>(""));
  79. getField().setBuffered(true);
  80. expectValueChangeFromSetValueNotCommit();
  81. }
  82. protected void expectValueChangeFromSetValueNotCommit() {
  83. // Expectations and start test
  84. listener.valueChange(EasyMock.isA(ValueChangeEvent.class));
  85. EasyMock.replay(listener);
  86. // Add listener and set the value -> should end up in listener once
  87. getField().addListener(listener);
  88. setValue(getField());
  89. // Ensure listener was called once
  90. EasyMock.verify(listener);
  91. // commit
  92. getField().commit();
  93. // Ensure listener was not called again
  94. EasyMock.verify(listener);
  95. }
  96. protected AbstractField<T> getField() {
  97. return field;
  98. }
  99. /**
  100. * Override in subclasses to set value with changeVariables().
  101. */
  102. protected void setValue(AbstractField<T> field) {
  103. field.setValue((T) "newValue");
  104. }
  105. }