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.

DateTimeFieldChangeResolutionTest.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package com.vaadin.tests.components.datefield;
  2. import static com.vaadin.tests.components.datefield.DateFieldChangeResolution.BUTTON_BASE_ID;
  3. import static com.vaadin.tests.components.datefield.DateFieldChangeResolution.DATEFIELD_ID;
  4. import static org.junit.Assert.assertFalse;
  5. import static org.junit.Assert.assertTrue;
  6. import org.junit.Test;
  7. import org.openqa.selenium.Keys;
  8. import org.openqa.selenium.WebElement;
  9. import com.vaadin.shared.ui.datefield.DateTimeResolution;
  10. import com.vaadin.testbench.By;
  11. import com.vaadin.tests.tb3.MultiBrowserTest;
  12. public class DateTimeFieldChangeResolutionTest extends MultiBrowserTest {
  13. private WebElement dateFieldButton, textField;
  14. private WebElement resolutionSecond, resolutionMinute, resolutionHour,
  15. resolutionDay, resolutionMonth, resolutionYear;
  16. @Test
  17. public void changeResolutionBetweenYearAndMonth() throws Exception {
  18. initialize();
  19. click(resolutionMonth);
  20. checkHeaderAndBody(DateTimeResolution.MONTH, true);
  21. click(resolutionYear);
  22. checkHeaderAndBody(DateTimeResolution.YEAR, true);
  23. }
  24. @Test
  25. public void changeResolutionBetweenYearAndSecond() throws Exception {
  26. initialize();
  27. click(resolutionSecond);
  28. checkHeaderAndBody(DateTimeResolution.SECOND, true);
  29. click(resolutionYear);
  30. checkHeaderAndBody(DateTimeResolution.YEAR, true);
  31. }
  32. @Test
  33. public void changeResolutionToDayThenMonth() throws Exception {
  34. initialize();
  35. // check the initial state
  36. checkHeaderAndBody(DateTimeResolution.YEAR, true);
  37. click(resolutionDay);
  38. checkHeaderAndBody(DateTimeResolution.DAY, true);
  39. click(resolutionMonth);
  40. checkHeaderAndBody(DateTimeResolution.MONTH, true);
  41. }
  42. @Test
  43. public void setDateAndChangeResolution() throws Exception {
  44. initialize();
  45. // Set the date to previous month.
  46. click(resolutionMonth);
  47. openPopupDateField();
  48. click(driver.findElement(By.className("v-button-prevmonth")));
  49. closePopupDateField();
  50. assertFalse(
  51. "The text field of the calendar should not be empty after selecting a date",
  52. textField.getAttribute("value").isEmpty());
  53. // Change resolutions and check that the selected date is not lost and
  54. // that the calendar has the correct resolution.
  55. click(resolutionHour);
  56. checkHeaderAndBody(DateTimeResolution.HOUR, false);
  57. click(resolutionYear);
  58. checkHeaderAndBody(DateTimeResolution.YEAR, false);
  59. click(resolutionMinute);
  60. checkHeaderAndBody(DateTimeResolution.MINUTE, false);
  61. }
  62. private void initialize() {
  63. openTestURL();
  64. WebElement dateField = driver.findElement(By.id(DATEFIELD_ID));
  65. dateFieldButton = dateField
  66. .findElement(By.className("v-datefield-button"));
  67. textField = dateField
  68. .findElement(By.className("v-datefield-textfield"));
  69. resolutionSecond = driver.findElement(By.id(BUTTON_BASE_ID + "second"));
  70. resolutionMinute = driver.findElement(By.id(BUTTON_BASE_ID + "minute"));
  71. resolutionHour = driver.findElement(By.id(BUTTON_BASE_ID + "hour"));
  72. resolutionDay = driver.findElement(By.id(BUTTON_BASE_ID + "day"));
  73. resolutionMonth = driver.findElement(By.id(BUTTON_BASE_ID + "month"));
  74. resolutionYear = driver.findElement(By.id(BUTTON_BASE_ID + "year"));
  75. }
  76. private void checkHeaderAndBody(DateTimeResolution resolution,
  77. boolean textFieldIsEmpty) throws Exception {
  78. // Popup date field has all kinds of strange timers on the
  79. // client side
  80. sleep(100);
  81. // Open the popup calendar, perform checks and close the popup.
  82. openPopupDateField();
  83. if (resolution.compareTo(DateTimeResolution.MONTH) <= 0) {
  84. checkMonthHeader();
  85. } else {
  86. checkYearHeader();
  87. }
  88. if (resolution.compareTo(DateTimeResolution.DAY) <= 0) {
  89. assertTrue(
  90. "A calendar with the chosen resolution should have a body",
  91. calendarHasBody());
  92. } else {
  93. assertFalse(
  94. "A calendar with the chosen resolution should not have a body",
  95. calendarHasBody());
  96. }
  97. if (textFieldIsEmpty) {
  98. assertTrue("The text field of the calendar should be empty",
  99. textField.getAttribute("value").isEmpty());
  100. } else {
  101. assertFalse("The text field of the calendar should not be empty",
  102. textField.getAttribute("value").isEmpty());
  103. }
  104. closePopupDateField();
  105. }
  106. private void checkMonthHeader() {
  107. checkHeaderForYear();
  108. checkHeaderForMonth(true);
  109. }
  110. private void checkYearHeader() {
  111. checkHeaderForYear();
  112. checkHeaderForMonth(false);
  113. }
  114. private boolean calendarHasBody() {
  115. return isElementPresent(By.className("v-datefield-calendarpanel-body"));
  116. }
  117. private void checkHeaderForMonth(boolean buttonsExpected) {
  118. // If buttonsExpected is true, check that there are buttons for changing
  119. // the month. Otherwise check that there are no such buttons.
  120. if (buttonsExpected) {
  121. assertTrue(
  122. "The calendar should have a button for switching to the previous month",
  123. isElementPresent(By.cssSelector(
  124. ".v-datefield-calendarpanel-header .v-datefield-calendarpanel-prevmonth .v-button-prevmonth")));
  125. assertTrue(
  126. "The calendar should have a button for switching to the next month",
  127. isElementPresent(By.cssSelector(
  128. ".v-datefield-calendarpanel-header .v-datefield-calendarpanel-nextmonth .v-button-nextmonth")));
  129. } else {
  130. assertFalse(
  131. "The calendar should not have a button for switching to the previous month",
  132. isElementPresent(By.cssSelector(
  133. ".v-datefield-calendarpanel-header .v-datefield-calendarpanel-prevmonth .v-button-prevmonth")));
  134. assertFalse(
  135. "The calendar should not have a button for switching to the next month",
  136. isElementPresent(By.cssSelector(
  137. ".v-datefield-calendarpanel-header .v-datefield-calendarpanel-nextmonth .v-button-nextmonth")));
  138. }
  139. }
  140. private void checkHeaderForYear() {
  141. assertTrue(
  142. "The calendar should have a button for switching to the previous year",
  143. isElementPresent(By.cssSelector(
  144. ".v-datefield-calendarpanel-header .v-datefield-calendarpanel-prevyear .v-button-prevyear")));
  145. assertTrue("The calendar header should show the selected year",
  146. isElementPresent(By.cssSelector(
  147. ".v-datefield-calendarpanel-header .v-datefield-calendarpanel-month")));
  148. assertTrue(
  149. "The calendar should have a button for switching to the next year",
  150. isElementPresent(By.cssSelector(
  151. ".v-datefield-calendarpanel-header .v-datefield-calendarpanel-nextyear .v-button-nextyear")));
  152. }
  153. private void click(WebElement element) {
  154. testBenchElement(element).click(5, 5);
  155. }
  156. private void openPopupDateField() {
  157. click(dateFieldButton);
  158. }
  159. private void closePopupDateField() {
  160. WebElement element = driver
  161. .findElement(By.cssSelector(".v-datefield-calendarpanel"));
  162. element.sendKeys(Keys.ESCAPE);
  163. }
  164. }