Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ComboBoxTest.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package com.vaadin.ui;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Locale;
  5. import java.util.Map;
  6. import org.junit.Assert;
  7. import org.junit.Before;
  8. import org.junit.Test;
  9. import com.vaadin.shared.ui.combobox.FilteringMode;
  10. public class ComboBoxTest {
  11. private ComboBox comboBox;
  12. @Before
  13. public void setup() {
  14. comboBox = new ComboBox();
  15. comboBox.setLocale(Locale.ENGLISH);
  16. }
  17. @Test
  18. public void options_noFilter() {
  19. ComboBox comboBox = new ComboBox();
  20. for (int i = 0; i < 10; i++) {
  21. comboBox.addItem(i);
  22. }
  23. List<?> options = comboBox.getFilteredOptions();
  24. Assert.assertEquals(10, options.size());
  25. for (int i = 0; i < 10; i++) {
  26. Assert.assertEquals(i, options.get(i));
  27. }
  28. }
  29. @Test
  30. public void options_inMemoryFilteringStartsWith() {
  31. for (int i = 0; i < 21; i++) {
  32. comboBox.addItem(i);
  33. }
  34. setFilterAndCurrentPage(comboBox, "1", 0);
  35. List<?> options = comboBox.getFilteredOptions();
  36. Assert.assertEquals(11, options.size());
  37. }
  38. @Test
  39. public void options_inMemoryFilteringContains() {
  40. comboBox.setFilteringMode(FilteringMode.CONTAINS);
  41. for (int i = 0; i < 21; i++) {
  42. comboBox.addItem(i);
  43. }
  44. setFilterAndCurrentPage(comboBox, "2", 0);
  45. List<?> options = comboBox.getFilteredOptions();
  46. Assert.assertEquals(3, options.size());
  47. }
  48. private static void setFilterAndCurrentPage(ComboBox comboBox,
  49. String filterString, int currentPage) {
  50. Map<String, Object> variables = new HashMap<String, Object>();
  51. variables.put("filter", filterString);
  52. variables.put("page", currentPage);
  53. comboBox.changeVariables(null, variables);
  54. }
  55. @Test
  56. public void getOptions_moreThanOnePage_noNullItem() {
  57. int nrOptions = comboBox.getPageLength() * 2;
  58. for (int i = 0; i < nrOptions; i++) {
  59. comboBox.addItem(i);
  60. }
  61. setFilterAndCurrentPage(comboBox, "", 0);
  62. List<?> goingToClient = comboBox
  63. .sanitizeList(comboBox.getFilteredOptions(), false);
  64. Assert.assertEquals(comboBox.getPageLength(), goingToClient.size());
  65. }
  66. @Test
  67. public void getOptions_moreThanOnePage_nullItem() {
  68. int nrOptions = comboBox.getPageLength() * 2;
  69. for (int i = 0; i < nrOptions; i++) {
  70. comboBox.addItem(i);
  71. }
  72. setFilterAndCurrentPage(comboBox, "", 0);
  73. List<?> goingToClient = comboBox
  74. .sanitizeList(comboBox.getFilteredOptions(), true);
  75. // Null item is shown on first page
  76. Assert.assertEquals(comboBox.getPageLength() - 1, goingToClient.size());
  77. setFilterAndCurrentPage(comboBox, "", 1);
  78. goingToClient = comboBox.sanitizeList(comboBox.getFilteredOptions(),
  79. true);
  80. // Null item is not shown on the second page
  81. Assert.assertEquals(comboBox.getPageLength(), goingToClient.size());
  82. }
  83. @Test
  84. public void getOptions_lessThanOnePage_noNullItem() {
  85. int nrOptions = comboBox.getPageLength() / 2;
  86. for (int i = 0; i < nrOptions; i++) {
  87. comboBox.addItem(i);
  88. }
  89. setFilterAndCurrentPage(comboBox, "", 0);
  90. List<?> goingToClient = comboBox
  91. .sanitizeList(comboBox.getFilteredOptions(), false);
  92. Assert.assertEquals(nrOptions, goingToClient.size());
  93. }
  94. @Test
  95. public void getOptions_lessThanOnePage_withNullItem() {
  96. int nrOptions = comboBox.getPageLength() / 2;
  97. for (int i = 0; i < nrOptions; i++) {
  98. comboBox.addItem(i);
  99. }
  100. setFilterAndCurrentPage(comboBox, "", 0);
  101. List<?> goingToClient = comboBox
  102. .sanitizeList(comboBox.getFilteredOptions(), true);
  103. // All items + null still fit on one page
  104. Assert.assertEquals(nrOptions, goingToClient.size());
  105. }
  106. @Test
  107. public void getOptions_exactlyOnePage_withNullItem() {
  108. int nrOptions = comboBox.getPageLength();
  109. for (int i = 0; i < nrOptions; i++) {
  110. comboBox.addItem(i);
  111. }
  112. setFilterAndCurrentPage(comboBox, "", 0);
  113. List<?> goingToClient = comboBox
  114. .sanitizeList(comboBox.getFilteredOptions(), true);
  115. // Null item on first page
  116. Assert.assertEquals(nrOptions - 1, goingToClient.size());
  117. setFilterAndCurrentPage(comboBox, "", 1);
  118. goingToClient = comboBox.sanitizeList(comboBox.getFilteredOptions(),
  119. true);
  120. // All but one was on the first page
  121. Assert.assertEquals(1, goingToClient.size());
  122. }
  123. }