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.

AbstractMultiSelectUsingIdTest.java 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 static org.junit.Assert.assertEquals;
  18. import java.util.ArrayList;
  19. import java.util.Arrays;
  20. import java.util.List;
  21. import java.util.Set;
  22. import java.util.stream.Collectors;
  23. import org.junit.Before;
  24. import org.junit.Test;
  25. import com.vaadin.data.provider.ListDataProvider;
  26. public class AbstractMultiSelectUsingIdTest {
  27. public TwinColSelect<ItemWithId> selectToTest;
  28. public static class ItemWithId {
  29. private int id;
  30. public ItemWithId() {
  31. }
  32. public ItemWithId(int id) {
  33. this.id = id;
  34. }
  35. public int getId() {
  36. return id;
  37. }
  38. public void setId(int id) {
  39. this.id = id;
  40. }
  41. }
  42. @Before
  43. public void setUp() {
  44. selectToTest = new TwinColSelect<>();
  45. List<ItemWithId> items = new ArrayList<>();
  46. items.add(new ItemWithId(3));
  47. items.add(new ItemWithId(2));
  48. items.add(new ItemWithId(1));
  49. items.add(new ItemWithId(8));
  50. items.add(new ItemWithId(7));
  51. items.add(new ItemWithId(4));
  52. items.add(new ItemWithId(6));
  53. ListDataProvider<ItemWithId> dataProvider = new ListDataProvider<ItemWithId>(
  54. items) {
  55. @Override
  56. public Object getId(ItemWithId item) {
  57. return item.getId();
  58. }
  59. };
  60. selectToTest.setDataProvider(dataProvider);
  61. }
  62. @Test
  63. public void selectTwiceSelectsOnce() {
  64. selectToTest.select(new ItemWithId(1));
  65. assertSelectionOrder(1);
  66. selectToTest.select(new ItemWithId(1));
  67. assertSelectionOrder(1);
  68. }
  69. @Test
  70. public void deselectWorks() {
  71. selectToTest.select(new ItemWithId(1));
  72. selectToTest.deselect(new ItemWithId(1));
  73. assertSelectionOrder();
  74. }
  75. private void assertSelectionOrder(Integer... selectionOrder) {
  76. List<Integer> asList = Arrays.asList(selectionOrder);
  77. assertEquals(asList, selectToTest.getSelectedItems().stream()
  78. .map(ItemWithId::getId).collect(Collectors.toList()));
  79. }
  80. }