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.

CheckBoxTest.java 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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.ui;
  17. import java.util.concurrent.atomic.AtomicBoolean;
  18. import org.junit.Assert;
  19. import org.junit.Test;
  20. import com.vaadin.shared.MouseEventDetails;
  21. import com.vaadin.shared.ui.checkbox.CheckBoxServerRpc;
  22. import com.vaadin.tests.util.MockUI;
  23. public class CheckBoxTest {
  24. @Test
  25. public void initiallyFalse() {
  26. CheckBox cb = new CheckBox();
  27. Assert.assertFalse(cb.getValue());
  28. }
  29. @Test
  30. public void testSetValue() {
  31. CheckBox cb = new CheckBox();
  32. cb.setValue(true);
  33. Assert.assertTrue(cb.getValue());
  34. cb.setValue(false);
  35. Assert.assertFalse(cb.getValue());
  36. }
  37. @Test
  38. public void setValueChangeFromClientIsUserOriginated() {
  39. UI ui = new MockUI();
  40. CheckBox cb = new CheckBox();
  41. ui.setContent(cb);
  42. AtomicBoolean userOriginated = new AtomicBoolean(false);
  43. cb.addValueChangeListener(e -> {
  44. userOriginated.set(e.isUserOriginated());
  45. });
  46. ComponentTest.syncToClient(cb);
  47. ComponentTest.getRpcProxy(cb, CheckBoxServerRpc.class).setChecked(true,
  48. new MouseEventDetails());
  49. Assert.assertTrue(userOriginated.get());
  50. userOriginated.set(false);
  51. ComponentTest.syncToClient(cb);
  52. ComponentTest.getRpcProxy(cb, CheckBoxServerRpc.class).setChecked(false,
  53. new MouseEventDetails());
  54. Assert.assertTrue(userOriginated.get());
  55. }
  56. @Test
  57. public void setValueChangeFromServerIsNotUserOriginated() {
  58. UI ui = new MockUI();
  59. CheckBox cb = new CheckBox();
  60. ui.setContent(cb);
  61. AtomicBoolean userOriginated = new AtomicBoolean(true);
  62. cb.addValueChangeListener(e -> {
  63. userOriginated.set(e.isUserOriginated());
  64. });
  65. cb.setValue(true);
  66. Assert.assertFalse(userOriginated.get());
  67. userOriginated.set(true);
  68. cb.setValue(false);
  69. Assert.assertFalse(userOriginated.get());
  70. }
  71. @Test(expected = NullPointerException.class)
  72. public void setValue_nullValue_throwsNPE() {
  73. CheckBox cb = new CheckBox();
  74. cb.setValue(null);
  75. }
  76. }