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.

ButtonClickTest.java 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package com.vaadin.tests.server.component.button;
  2. import static org.junit.Assert.assertEquals;
  3. import static org.junit.Assert.assertFalse;
  4. import static org.junit.Assert.assertTrue;
  5. import java.util.concurrent.atomic.AtomicInteger;
  6. import org.junit.Test;
  7. import com.vaadin.server.VaadinRequest;
  8. import com.vaadin.ui.Button;
  9. import com.vaadin.ui.UI;
  10. /**
  11. * Tests the public click() method.
  12. */
  13. public class ButtonClickTest {
  14. private boolean clicked = false;
  15. @Test
  16. public void clickDetachedButton() {
  17. Button b = new Button();
  18. AtomicInteger counter = new AtomicInteger(0);
  19. b.addClickListener(event -> counter.incrementAndGet());
  20. b.click();
  21. assertEquals(1, counter.get());
  22. }
  23. @Test
  24. public void testClick() {
  25. getButton().click();
  26. assertTrue("Button doesn't fire clicks", clicked);
  27. }
  28. @Test
  29. public void testClickDisabled() {
  30. Button b = getButton();
  31. b.setEnabled(false);
  32. b.click();
  33. assertFalse("Disabled button fires click events", clicked);
  34. }
  35. private Button getButton() {
  36. Button b = new Button();
  37. UI ui = createUI();
  38. b.setParent(ui);
  39. addClickListener(b);
  40. return b;
  41. }
  42. private UI createUI() {
  43. UI ui = new UI() {
  44. @Override
  45. protected void init(VaadinRequest request) {
  46. }
  47. };
  48. return ui;
  49. }
  50. private void addClickListener(Button b) {
  51. clicked = false;
  52. b.addClickListener(event -> clicked = true);
  53. }
  54. }