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.

ContextClickListenerTest.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright 2000-2016 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests.server;
  17. import java.util.EventObject;
  18. import org.easymock.EasyMock;
  19. import org.junit.Assert;
  20. import org.junit.Test;
  21. import com.vaadin.event.ContextClickEvent;
  22. import com.vaadin.event.ContextClickEvent.ContextClickListener;
  23. import com.vaadin.ui.AbstractComponent;
  24. import com.vaadin.ui.LegacyGrid.GridContextClickEvent;
  25. import com.vaadin.ui.Table.TableContextClickEvent;
  26. /**
  27. * Server-side unit tests to see that context click events are sent to listeners
  28. * correctly.
  29. *
  30. * If a listener is listening to a super type of an event, it should get the
  31. * event. i.e. Listening to ContextClickEvent, it should get the specialized
  32. * GridContextClickEvent as well.
  33. *
  34. * If a listener is listening to a sub-type of an event, it should not get the
  35. * super version. i.e. Listening to GridContextClickEvent, it should not get a
  36. * plain ContextClickEvent.
  37. */
  38. public class ContextClickListenerTest extends AbstractComponent {
  39. private final static ContextClickEvent contextClickEvent = EasyMock
  40. .createMock(ContextClickEvent.class);
  41. private final static GridContextClickEvent gridContextClickEvent = EasyMock
  42. .createMock(GridContextClickEvent.class);
  43. private final static TableContextClickEvent tableContextClickEvent = EasyMock
  44. .createMock(TableContextClickEvent.class);
  45. private final AssertListener contextListener = new AssertListener();
  46. private final AssertListener ctxtListener2 = new AssertListener();
  47. public static class AssertListener implements ContextClickListener {
  48. private Class<?> expected = null;
  49. private String error = null;
  50. @Override
  51. public void contextClick(ContextClickEvent event) {
  52. if (expected == null) {
  53. error = "Unexpected context click event.";
  54. return;
  55. }
  56. if (!expected.isAssignableFrom(event.getClass())) {
  57. error = "Expected event type did not match the actual event.";
  58. }
  59. expected = null;
  60. }
  61. public <T extends ContextClickEvent> void expect(Class<T> clazz) {
  62. validate();
  63. expected = clazz;
  64. }
  65. public void validate() {
  66. if (expected != null) {
  67. Assert.fail("Expected context click never happened.");
  68. } else if (error != null) {
  69. Assert.fail(error);
  70. }
  71. }
  72. }
  73. @Test
  74. public void testListenerGetsASubClass() {
  75. addContextClickListener(contextListener);
  76. contextListener.expect(GridContextClickEvent.class);
  77. fireEvent(gridContextClickEvent);
  78. }
  79. @Test
  80. public void testListenerGetsExactClass() {
  81. addContextClickListener(contextListener);
  82. contextListener.expect(ContextClickEvent.class);
  83. fireEvent(contextClickEvent);
  84. }
  85. /**
  86. * Multiple listeners should get fitting events.
  87. */
  88. @Test
  89. public void testMultipleListenerGetEvents() {
  90. addContextClickListener(ctxtListener2);
  91. addContextClickListener(contextListener);
  92. ctxtListener2.expect(GridContextClickEvent.class);
  93. contextListener.expect(GridContextClickEvent.class);
  94. fireEvent(gridContextClickEvent);
  95. }
  96. @Test
  97. public void testAddAndRemoveListener() {
  98. addContextClickListener(contextListener);
  99. contextListener.expect(ContextClickEvent.class);
  100. fireEvent(contextClickEvent);
  101. removeContextClickListener(contextListener);
  102. fireEvent(contextClickEvent);
  103. }
  104. @Test
  105. public void testAddAndRemoveMultipleListeners() {
  106. addContextClickListener(ctxtListener2);
  107. addContextClickListener(contextListener);
  108. ctxtListener2.expect(GridContextClickEvent.class);
  109. contextListener.expect(GridContextClickEvent.class);
  110. fireEvent(gridContextClickEvent);
  111. removeContextClickListener(ctxtListener2);
  112. contextListener.expect(GridContextClickEvent.class);
  113. fireEvent(gridContextClickEvent);
  114. }
  115. @Test(expected = AssertionError.class)
  116. public void testExpectedEventNotReceived() {
  117. addContextClickListener(contextListener);
  118. contextListener.expect(GridContextClickEvent.class);
  119. fireEvent(contextClickEvent);
  120. }
  121. @Test(expected = AssertionError.class)
  122. public void testUnexpectedEventReceived() {
  123. addContextClickListener(contextListener);
  124. fireEvent(gridContextClickEvent);
  125. }
  126. @Override
  127. protected void fireEvent(EventObject event) {
  128. super.fireEvent(event);
  129. // Validate listeners automatically.
  130. ctxtListener2.validate();
  131. contextListener.validate();
  132. }
  133. }