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.

RadioButtonGroupTest.java 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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.ArrayList;
  18. import java.util.Arrays;
  19. import java.util.concurrent.atomic.AtomicInteger;
  20. import org.junit.Assert;
  21. import org.junit.Before;
  22. import org.junit.Test;
  23. import com.vaadin.data.SelectionModel.Multi;
  24. import com.vaadin.data.provider.DataProvider;
  25. import com.vaadin.shared.data.selection.SelectionServerRpc;
  26. public class RadioButtonGroupTest {
  27. private RadioButtonGroup<String> radioButtonGroup;
  28. @Before
  29. public void setUp() {
  30. radioButtonGroup = new RadioButtonGroup<>();
  31. // Intentional deviation from upcoming selection order
  32. radioButtonGroup.setDataProvider(
  33. DataProvider.ofItems("Third", "Second", "First"));
  34. }
  35. @Test
  36. public void apiSelectionChange_notUserOriginated() {
  37. AtomicInteger listenerCount = new AtomicInteger(0);
  38. radioButtonGroup.addSelectionListener(event -> {
  39. listenerCount.incrementAndGet();
  40. Assert.assertFalse(event.isUserOriginated());
  41. });
  42. radioButtonGroup.setValue("First");
  43. radioButtonGroup.setValue("Second");
  44. radioButtonGroup.setValue(null);
  45. radioButtonGroup.setValue(null);
  46. Assert.assertEquals(3, listenerCount.get());
  47. }
  48. @Test
  49. public void rpcSelectionChange_userOriginated() {
  50. AtomicInteger listenerCount = new AtomicInteger(0);
  51. radioButtonGroup.addSelectionListener(event -> {
  52. listenerCount.incrementAndGet();
  53. Assert.assertTrue(event.isUserOriginated());
  54. });
  55. SelectionServerRpc rpc = ComponentTest.getRpcProxy(radioButtonGroup,
  56. SelectionServerRpc.class);
  57. rpc.select(getItemKey("First"));
  58. rpc.select(getItemKey("Second"));
  59. rpc.deselect(getItemKey("Second"));
  60. Assert.assertEquals(3, listenerCount.get());
  61. }
  62. private String getItemKey(String dataObject) {
  63. return radioButtonGroup.getDataCommunicator().getKeyMapper()
  64. .key(dataObject);
  65. }
  66. private static void assertSelectionOrder(Multi<String> selectionModel,
  67. String... selectionOrder) {
  68. Assert.assertEquals(Arrays.asList(selectionOrder),
  69. new ArrayList<>(selectionModel.getSelectedItems()));
  70. }
  71. }