1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package javafx.scene.control; 26 27 import static junit.framework.Assert.*; 28 29 import com.sun.javafx.scene.control.skin.SpinnerSkin; 30 import javafx.collections.FXCollections; 31 import javafx.collections.ObservableList; 32 import org.junit.Before; 33 import org.junit.Ignore; 34 import org.junit.Test; 35 36 import java.time.LocalDate; 37 import java.time.LocalTime; 38 import java.time.temporal.ChronoUnit; 39 40 import static javafx.scene.control.SpinnerValueFactory.*; 41 42 public class SpinnerTest { 43 44 private Spinner<?> spinner; 45 46 // --- int spinner 47 private Spinner<Integer> intSpinner; 48 private IntegerSpinnerValueFactory intValueFactory; 49 50 // --- double spinner 51 private Spinner<Double> dblSpinner; 52 private DoubleSpinnerValueFactory dblValueFactory; 53 54 // --- list spinner 55 private ObservableList<String> strings; 56 private Spinner<String> listSpinner; 57 private ListSpinnerValueFactory listValueFactory; 58 59 // --- LocalDate spinner 60 private Spinner<LocalDate> localDateSpinner; 61 private LocalDateSpinnerValueFactory localDateValueFactory; 62 63 // --- LocalTime spinner 64 private Spinner<LocalTime> localTimeSpinner; 65 private LocalTimeSpinnerValueFactory localTimeValueFactory; 66 67 // used in tests for counting events, reset to zero in setup() 68 private int eventCount; 69 70 71 @Before public void setup() { 72 eventCount = 0; 73 spinner = new Spinner(); 74 75 intSpinner = new Spinner<>(0, 10, 5, 1); 76 intValueFactory = (IntegerSpinnerValueFactory) intSpinner.getValueFactory(); 77 78 dblSpinner = new Spinner<>(0.0, 1.0, 0.5, 0.05); 79 dblValueFactory = (DoubleSpinnerValueFactory) dblSpinner.getValueFactory(); 80 81 strings = FXCollections.observableArrayList("string1", "string2", "string3"); 82 listSpinner = new Spinner<>(strings); 83 listValueFactory = (ListSpinnerValueFactory<String>) listSpinner.getValueFactory(); 84 85 // minimum is today minus 10 days, maximum is today plus 10 days 86 localDateSpinner = new Spinner<>(nowPlusDays(-10), nowPlusDays(10), LocalDate.now(), 1, ChronoUnit.DAYS); 87 localDateValueFactory = (LocalDateSpinnerValueFactory) localDateSpinner.getValueFactory(); 88 89 localTimeSpinner = new Spinner<>(LocalTime.MIN, LocalTime.MAX, LocalTime.now(), 1, ChronoUnit.HOURS); 90 localTimeValueFactory = (LocalTimeSpinnerValueFactory) localTimeSpinner.getValueFactory(); 91 } 92 93 94 /*************************************************************************** 95 * * 96 * Basic tests * 97 * * 98 **************************************************************************/ 99 100 @Test public void createDefaultSpinner_hasSpinnerStyleClass() { 101 assertEquals(1, spinner.getStyleClass().size()); 102 assertTrue(spinner.getStyleClass().contains("spinner")); 103 } 104 105 @Test public void createDefaultSpinner_editorIsNotNull() { 106 assertNotNull(spinner.getEditor()); 107 } 108 109 @Test public void createDefaultSpinner_valueFactoryIsNull() { 110 assertNull(spinner.getValueFactory()); 111 } 112 113 @Test public void createDefaultSpinner_valueIsNull() { 114 assertNull(spinner.getValue()); 115 } 116 117 @Test public void createDefaultSpinner_editableIsFalse() { 118 assertFalse(spinner.isEditable()); 119 } 120 121 @Ignore("Waiting for StageLoader") 122 @Test public void createDefaultSpinner_defaultSkinIsInstalled() { 123 assertTrue(spinner.getSkin() instanceof SpinnerSkin); 124 } 125 126 127 /*************************************************************************** 128 * * 129 * Alternative constructor tests * 130 * * 131 **************************************************************************/ 132 133 @Test public void createIntSpinner_createValidValueFactory() { 134 Spinner<Integer> intSpinner = new Spinner<Integer>(0, 10, 5, 1); 135 assertTrue(intSpinner.getValueFactory() instanceof IntegerSpinnerValueFactory); 136 IntegerSpinnerValueFactory valueFactory = (IntegerSpinnerValueFactory) intSpinner.getValueFactory(); 137 assertEquals(5, (int) valueFactory.getValue()); 138 } 139 140 @Test public void createIntSpinner_setInitialValueOutsideMaxBounds() { 141 Spinner<Integer> intSpinner = new Spinner<Integer>(0, 10, 100, 1); 142 assertTrue(intSpinner.getValueFactory() instanceof IntegerSpinnerValueFactory); 143 IntegerSpinnerValueFactory valueFactory = (IntegerSpinnerValueFactory) intSpinner.getValueFactory(); 144 assertEquals(0, (int) valueFactory.getValue()); 145 } 146 147 @Test public void createIntSpinner_setInitialValueOutsideMinBounds() { 148 Spinner<Integer> intSpinner = new Spinner<Integer>(0, 10, -100, 1); 149 assertTrue(intSpinner.getValueFactory() instanceof IntegerSpinnerValueFactory); 150 IntegerSpinnerValueFactory valueFactory = (IntegerSpinnerValueFactory) intSpinner.getValueFactory(); 151 assertEquals(0, (int) valueFactory.getValue()); 152 } 153 154 @Test public void createListSpinner_createValidValueFactory() { 155 Spinner<String> stringSpinner = new Spinner<>(FXCollections.observableArrayList("item 1", "item 2")); 156 assertTrue(stringSpinner.getValueFactory() instanceof ListSpinnerValueFactory); 157 ListSpinnerValueFactory valueFactory = (ListSpinnerValueFactory) stringSpinner.getValueFactory(); 158 assertEquals("item 1", valueFactory.getValue()); 159 } 160 161 @Test public void createListSpinner_emptyListResultsInNullValue() { 162 Spinner<String> stringSpinner = new Spinner<String>(FXCollections.observableArrayList()); 163 assertTrue(stringSpinner.getValueFactory() instanceof ListSpinnerValueFactory); 164 ListSpinnerValueFactory valueFactory = (ListSpinnerValueFactory) stringSpinner.getValueFactory(); 165 assertNull(valueFactory.getValue()); 166 } 167 168 @Test public void createListSpinner_nullListResultsInNullValue() { 169 Spinner<String> stringSpinner = new Spinner<>((ObservableList<String>)null); 170 assertTrue(stringSpinner.getValueFactory() instanceof ListSpinnerValueFactory); 171 ListSpinnerValueFactory valueFactory = (ListSpinnerValueFactory) stringSpinner.getValueFactory(); 172 assertNull(valueFactory.getValue()); 173 } 174 175 @Test public void createSpinner_customSpinnerValueFactory() { 176 SpinnerValueFactory<String> valueFactory = new ListSpinnerValueFactory<>(FXCollections.observableArrayList("item 1", "item 2")); 177 Spinner<String> stringSpinner = new Spinner<>(valueFactory); 178 assertEquals(valueFactory, stringSpinner.getValueFactory()); 179 ListSpinnerValueFactory valueFactory1 = (ListSpinnerValueFactory) stringSpinner.getValueFactory(); 180 assertEquals("item 1", valueFactory.getValue()); 181 assertEquals("item 1", valueFactory1.getValue()); 182 } 183 184 185 186 /*************************************************************************** 187 * * 188 * increment / decrement tests * 189 * (we test the actual inc / dec in the value factory impl tests) * 190 * * 191 **************************************************************************/ 192 193 @Test(expected = IllegalStateException.class) 194 public void expectExceptionWhenNoArgsIncrementCalled_noValueFactory() { 195 spinner.increment(); 196 } 197 198 @Test(expected = IllegalStateException.class) 199 public void expectExceptionWhenOneArgsIncrementCalled_noValueFactory() { 200 spinner.increment(2); 201 } 202 203 @Test(expected = IllegalStateException.class) 204 public void expectExceptionWhenNoArgsDecrementCalled_noValueFactory() { 205 spinner.decrement(); 206 } 207 208 @Test(expected = IllegalStateException.class) 209 public void expectExceptionWhenOneArgsDecrementCalled_noValueFactory() { 210 spinner.decrement(2); 211 } 212 213 214 /*************************************************************************** 215 * * 216 * changing value factory tests * 217 * * 218 **************************************************************************/ 219 220 @Test public void valueFactory_valueIsNulledWhenValueFactoryisNull() { 221 assertEquals(5, (int) intSpinner.getValue()); 222 intSpinner.setValueFactory(null); 223 assertNull(spinner.getValue()); 224 } 225 226 @Test public void valueFactory_valueIsUpdatedWhenValueFactoryChanged() { 227 assertEquals(5, (int) intSpinner.getValue()); 228 intSpinner.setValueFactory(new IntegerSpinnerValueFactory(0, 10, 8)); 229 assertEquals(8, (int) intSpinner.getValue()); 230 } 231 232 // @Test public void valueFactory_spinnerPropertyIsNullWhenRemovedFromSpinner() { 233 // SpinnerValueFactory initialValueFactory = intSpinner.getValueFactory(); 234 // assertEquals(intSpinner, initialValueFactory.getSpinner()); 235 // 236 // intSpinner.setValueFactory(null); 237 // assertNull(intSpinner.getValueFactory()); 238 // } 239 // 240 // @Test public void valueFactory_spinnerPropertyIsSetOnNewSpinner() { 241 // SpinnerValueFactory initialValueFactory = intSpinner.getValueFactory(); 242 // assertEquals(intSpinner, initialValueFactory.getSpinner()); 243 // 244 // SpinnerValueFactory newValueFactory = new IntSpinnerValueFactory(0, 10, 8); 245 // intSpinner.setValueFactory(newValueFactory); 246 // 247 // assertNull(initialValueFactory.getSpinner()); 248 // assertEquals(intSpinner, newValueFactory.getSpinner()); 249 // } 250 251 252 /*************************************************************************** 253 * * 254 * value property events * 255 * * * 256 **************************************************************************/ 257 258 @Test public void value_notifyWhenChanged_validValue() { 259 assertEquals(5, (int) intSpinner.getValue()); 260 intSpinner.valueProperty().addListener(o -> eventCount++); 261 intSpinner.getValueFactory().setValue(3); 262 assertEquals(1, eventCount); 263 } 264 265 @Test public void value_notifyWhenChanged_invalidValue() { 266 assertEquals(5, (int) intSpinner.getValue()); 267 intSpinner.valueProperty().addListener(o -> eventCount++); 268 intSpinner.getValueFactory().setValue(1000); 269 270 // we expect two events: firstly, one for the invalid value, and another 271 // for the valid value 272 assertEquals(2, eventCount); 273 } 274 275 @Test public void value_notifyWhenChanged_existingValue() { 276 assertEquals(5, (int) intSpinner.getValue()); 277 intSpinner.valueProperty().addListener(o -> eventCount++); 278 intSpinner.getValueFactory().setValue(5); 279 assertEquals(0, eventCount); 280 } 281 282 283 /*************************************************************************** 284 * * 285 * editing tests * 286 * * 287 **************************************************************************/ 288 289 @Ignore("Need KeyboardEventFirer") 290 @Test public void editing_commitValidInput() { 291 intSpinner.valueProperty().addListener(o -> eventCount++); 292 intSpinner.getEditor().setText("3"); 293 // TODO press enter 294 295 assertEquals(1, eventCount); 296 assertEquals(3, (int) intSpinner.getValue()); 297 assertEquals("3", intSpinner.getEditor().getText()); 298 } 299 300 @Ignore("Need KeyboardEventFirer") 301 @Test public void editing_commitInvalidInput() { 302 intSpinner.valueProperty().addListener(o -> eventCount++); 303 intSpinner.getEditor().setText("300"); 304 // TODO press enter 305 306 assertEquals(2, eventCount); 307 assertEquals(5, (int) intSpinner.getValue()); 308 assertEquals("5", intSpinner.getEditor().getText()); 309 } 310 311 312 /*************************************************************************** 313 * * 314 * IntegerSpinnerValueFactory tests * 315 * * 316 **************************************************************************/ 317 318 @Test public void intSpinner_testIncrement_oneStep() { 319 intValueFactory.increment(1); 320 assertEquals(6, (int) intValueFactory.getValue()); 321 } 322 323 @Test public void intSpinner_testIncrement_twoSteps() { 324 intValueFactory.increment(2); 325 assertEquals(7, (int) intValueFactory.getValue()); 326 } 327 328 @Test public void intSpinner_testIncrement_manyCalls() { 329 for (int i = 0; i < 100; i++) { 330 intValueFactory.increment(1); 331 } 332 assertEquals(10, (int) intValueFactory.getValue()); 333 } 334 335 @Test public void intSpinner_testIncrement_bigStepPastMaximum() { 336 intValueFactory.increment(1000); 337 assertEquals(10, (int) intValueFactory.getValue()); 338 } 339 340 @Test public void intSpinner_testDecrement_oneStep() { 341 intValueFactory.decrement(1); 342 assertEquals(4, (int) intValueFactory.getValue()); 343 } 344 345 @Test public void intSpinner_testDecrement_twoSteps() { 346 intValueFactory.decrement(2); 347 assertEquals(3, (int) intValueFactory.getValue()); 348 } 349 350 @Test public void intSpinner_testDecrement_manyCalls() { 351 for (int i = 0; i < 100; i++) { 352 intValueFactory.decrement(1); 353 } 354 assertEquals(0, (int) intValueFactory.getValue()); 355 } 356 357 @Test public void intSpinner_testDecrement_bigStepPastMinimum() { 358 intValueFactory.decrement(1000); 359 assertEquals(0, (int) intValueFactory.getValue()); 360 } 361 362 @Test public void intSpinner_testWrapAround_increment_oneStep() { 363 intValueFactory.setWrapAround(true); 364 intValueFactory.increment(1); // 6 365 intValueFactory.increment(1); // 7 366 intValueFactory.increment(1); // 8 367 intValueFactory.increment(1); // 9 368 intValueFactory.increment(1); // 10 369 intValueFactory.increment(1); // 0 370 intValueFactory.increment(1); // 1 371 assertEquals(1, (int) intValueFactory.getValue()); 372 } 373 374 @Test public void intSpinner_testWrapAround_increment_twoSteps() { 375 intValueFactory.setWrapAround(true); 376 intValueFactory.increment(2); // 7 377 intValueFactory.increment(2); // 9 378 intValueFactory.increment(2); // 0 379 intValueFactory.increment(2); // 2 380 assertEquals(2, (int) intValueFactory.getValue()); 381 } 382 383 @Test public void intSpinner_testWrapAround_decrement_oneStep() { 384 intValueFactory.setWrapAround(true); 385 intValueFactory.decrement(1); // 4 386 intValueFactory.decrement(1); // 3 387 intValueFactory.decrement(1); // 2 388 intValueFactory.decrement(1); // 1 389 intValueFactory.decrement(1); // 0 390 intValueFactory.decrement(1); // 10 391 intValueFactory.decrement(1); // 9 392 assertEquals(9, (int) intValueFactory.getValue()); 393 } 394 395 @Test public void intSpinner_testWrapAround_decrement_twoSteps() { 396 intValueFactory.setWrapAround(true); 397 intValueFactory.decrement(2); // 3 398 intValueFactory.decrement(2); // 1 399 intValueFactory.decrement(2); // 10 400 intValueFactory.decrement(2); // 8 401 assertEquals(8, (int) intValueFactory.getValue()); 402 } 403 404 @Test public void intSpinner_assertDefaultConverterIsNonNull() { 405 assertNotNull(intValueFactory.getConverter()); 406 } 407 408 @Test public void intSpinner_testToString_valueInRange() { 409 assertEquals("3", intValueFactory.getConverter().toString(3)); 410 } 411 412 @Test public void intSpinner_testToString_valueOutOfRange() { 413 assertEquals("300", intValueFactory.getConverter().toString(300)); 414 } 415 416 @Test public void intSpinner_testFromString_valueInRange() { 417 assertEquals(3, (int) intValueFactory.getConverter().fromString("3")); 418 } 419 420 @Test public void intSpinner_testFromString_valueOutOfRange() { 421 assertEquals(300, (int) intValueFactory.getConverter().fromString("300")); 422 } 423 424 @Test public void intSpinner_testSetMin_doesNotChangeSpinnerValueWhenMinIsLessThanCurrentValue() { 425 intValueFactory.setValue(5); 426 assertEquals(5, (int) intSpinner.getValue()); 427 intValueFactory.setMin(3); 428 assertEquals(5, (int) intSpinner.getValue()); 429 } 430 431 @Test public void intSpinner_testSetMin_changesSpinnerValueWhenMinIsGreaterThanCurrentValue() { 432 intValueFactory.setValue(0); 433 assertEquals(0, (int) intSpinner.getValue()); 434 intValueFactory.setMin(5); 435 assertEquals(5, (int) intSpinner.getValue()); 436 } 437 438 @Test public void intSpinner_testSetMin_ensureThatMinCanNotExceedMax() { 439 assertEquals(0, intValueFactory.getMin()); 440 assertEquals(10, intValueFactory.getMax()); 441 intValueFactory.setMin(20); 442 assertEquals(10, intValueFactory.getMin()); 443 } 444 445 @Test public void intSpinner_testSetMin_ensureThatMinCanEqualMax() { 446 assertEquals(0, intValueFactory.getMin()); 447 assertEquals(10, intValueFactory.getMax()); 448 intValueFactory.setMin(10); 449 assertEquals(10, intValueFactory.getMin()); 450 } 451 452 @Test public void intSpinner_testSetMax_doesNotChangeSpinnerValueWhenMaxIsGreaterThanCurrentValue() { 453 intValueFactory.setValue(5); 454 assertEquals(5, (int) intSpinner.getValue()); 455 intValueFactory.setMax(8); 456 assertEquals(5, (int) intSpinner.getValue()); 457 } 458 459 @Test public void intSpinner_testSetMax_changesSpinnerValueWhenMaxIsGreaterThanCurrentValue() { 460 intValueFactory.setValue(5); 461 assertEquals(5, (int) intSpinner.getValue()); 462 intValueFactory.setMax(3); 463 assertEquals(3, (int) intSpinner.getValue()); 464 } 465 466 @Test public void intSpinner_testSetMax_ensureThatMaxCanNotGoLessThanMin() { 467 intValueFactory.setMin(5); 468 assertEquals(5, intValueFactory.getMin()); 469 assertEquals(10, intValueFactory.getMax()); 470 intValueFactory.setMax(3); 471 assertEquals(5, intValueFactory.getMin()); 472 } 473 474 @Test public void intSpinner_testSetMax_ensureThatMaxCanEqualMin() { 475 intValueFactory.setMin(5); 476 assertEquals(5, intValueFactory.getMin()); 477 assertEquals(10, intValueFactory.getMax()); 478 intValueFactory.setMax(5); 479 assertEquals(5, intValueFactory.getMin()); 480 } 481 482 @Test public void intSpinner_testSetValue_canNotExceedMax() { 483 assertEquals(0, intValueFactory.getMin()); 484 assertEquals(10, intValueFactory.getMax()); 485 intValueFactory.setValue(50); 486 assertEquals(10, (int) intSpinner.getValue()); 487 } 488 489 @Test public void intSpinner_testSetValue_canNotExceedMin() { 490 assertEquals(0, intValueFactory.getMin()); 491 assertEquals(10, intValueFactory.getMax()); 492 intValueFactory.setValue(-50); 493 assertEquals(0, (int) intSpinner.getValue()); 494 } 495 496 497 498 /*************************************************************************** 499 * * 500 * DoubleSpinnerValueFactory tests * 501 * * * 502 **************************************************************************/ 503 504 @Test public void dblSpinner_testIncrement_oneStep() { 505 dblValueFactory.increment(1); 506 assertEquals(0.55, dblValueFactory.getValue(), 0); 507 } 508 509 @Test public void dblSpinner_testIncrement_twoSteps() { 510 dblValueFactory.increment(2); 511 assertEquals(0.6, dblValueFactory.getValue(), 0); 512 } 513 514 @Test public void dblSpinner_testIncrement_manyCalls() { 515 for (int i = 0; i < 100; i++) { 516 dblValueFactory.increment(1); 517 } 518 assertEquals(1.0, dblValueFactory.getValue(), 0); 519 } 520 521 @Test public void dblSpinner_testIncrement_bigStepPastMaximum() { 522 dblValueFactory.increment(1000); 523 assertEquals(1.0, dblValueFactory.getValue(), 0); 524 } 525 526 @Test public void dblSpinner_testDecrement_oneStep() { 527 dblValueFactory.decrement(1); 528 assertEquals(0.45, dblValueFactory.getValue()); 529 } 530 531 @Test public void dblSpinner_testDecrement_twoSteps() { 532 dblValueFactory.decrement(2); 533 assertEquals(0.4, dblValueFactory.getValue()); 534 } 535 536 @Test public void dblSpinner_testDecrement_manyCalls() { 537 for (int i = 0; i < 100; i++) { 538 dblValueFactory.decrement(1); 539 } 540 assertEquals(0, dblValueFactory.getValue(), 0); 541 } 542 543 @Test public void dblSpinner_testDecrement_bigStepPastMinimum() { 544 dblValueFactory.decrement(1000); 545 assertEquals(0, dblValueFactory.getValue(), 0); 546 } 547 548 @Test public void dblSpinner_testWrapAround_increment_oneStep() { 549 dblValueFactory.setWrapAround(true); 550 dblValueFactory.setValue(0.80); 551 dblValueFactory.increment(1); // 0.85 552 dblValueFactory.increment(1); // 0.90 553 dblValueFactory.increment(1); // 0.95 554 dblValueFactory.increment(1); // 1.00 555 dblValueFactory.increment(1); // 0.00 556 dblValueFactory.increment(1); // 0.05 557 dblValueFactory.increment(1); // 0.10 558 assertEquals(0.10, dblValueFactory.getValue(), 0); 559 } 560 561 @Test public void dblSpinner_testWrapAround_increment_twoSteps() { 562 dblValueFactory.setWrapAround(true); 563 dblValueFactory.setValue(0.80); 564 dblValueFactory.increment(2); // 0.90 565 dblValueFactory.increment(2); // 1.00 566 dblValueFactory.increment(2); // 0.00 567 dblValueFactory.increment(2); // 0.10 568 assertEquals(0.10, dblValueFactory.getValue(), 0); 569 } 570 571 @Test public void dblSpinner_testWrapAround_decrement_oneStep() { 572 dblValueFactory.setWrapAround(true); 573 dblValueFactory.setValue(0.20); 574 dblValueFactory.decrement(1); // 0.15 575 dblValueFactory.decrement(1); // 0.10 576 dblValueFactory.decrement(1); // 0.05 577 dblValueFactory.decrement(1); // 0.00 578 dblValueFactory.decrement(1); // 1.00 579 dblValueFactory.decrement(1); // 0.95 580 dblValueFactory.decrement(1); // 0.90 581 assertEquals(0.90, dblValueFactory.getValue(), 0); 582 } 583 584 @Test public void dblSpinner_testWrapAround_decrement_twoSteps() { 585 dblValueFactory.setWrapAround(true); 586 dblValueFactory.setValue(0.20); 587 dblValueFactory.decrement(2); // 0.10 588 dblValueFactory.decrement(2); // 0.00 589 dblValueFactory.decrement(2); // 1.00 590 dblValueFactory.decrement(2); // 0.90 591 assertEquals(0.90, dblValueFactory.getValue()); 592 } 593 594 @Test public void dblSpinner_assertDefaultConverterIsNonNull() { 595 assertNotNull(dblValueFactory.getConverter()); 596 } 597 598 @Test public void dblSpinner_testToString_valueInRange() { 599 assertEquals("0.3", dblValueFactory.getConverter().toString(0.3)); 600 } 601 602 @Test public void dblSpinner_testToString_valueOutOfRange() { 603 assertEquals("300", dblValueFactory.getConverter().toString(300D)); 604 } 605 606 @Test public void dblSpinner_testFromString_valueInRange() { 607 assertEquals(0.3, dblValueFactory.getConverter().fromString("0.3")); 608 } 609 610 @Test public void dblSpinner_testFromString_valueOutOfRange() { 611 assertEquals(300.0, dblValueFactory.getConverter().fromString("300"), 0); 612 } 613 614 @Test public void dblSpinner_testSetMin_doesNotChangeSpinnerValueWhenMinIsLessThanCurrentValue() { 615 dblValueFactory.setValue(0.5); 616 assertEquals(0.5, dblSpinner.getValue()); 617 dblValueFactory.setMin(0.3); 618 assertEquals(0.5, dblSpinner.getValue()); 619 } 620 621 @Test public void dblSpinner_testSetMin_changesSpinnerValueWhenMinIsGreaterThanCurrentValue() { 622 dblValueFactory.setValue(0.0); 623 assertEquals(0.0, dblSpinner.getValue()); 624 dblValueFactory.setMin(0.5); 625 assertEquals(0.5, dblSpinner.getValue()); 626 } 627 628 @Test public void dblSpinner_testSetMin_ensureThatMinCanNotExceedMax() { 629 assertEquals(0, dblValueFactory.getMin(), 0); 630 assertEquals(1.0, dblValueFactory.getMax()); 631 dblValueFactory.setMin(20); 632 assertEquals(1.0, dblValueFactory.getMin()); 633 } 634 635 @Test public void dblSpinner_testSetMin_ensureThatMinCanEqualMax() { 636 assertEquals(0, dblValueFactory.getMin(), 0); 637 assertEquals(1.0, dblValueFactory.getMax()); 638 dblValueFactory.setMin(1.0); 639 assertEquals(1.0, dblValueFactory.getMin()); 640 } 641 642 @Test public void dblSpinner_testSetMax_doesNotChangeSpinnerValueWhenMaxIsGreaterThanCurrentValue() { 643 dblValueFactory.setValue(0.5); 644 assertEquals(0.5, dblSpinner.getValue()); 645 dblValueFactory.setMax(0.8); 646 assertEquals(0.5, dblSpinner.getValue()); 647 } 648 649 @Test public void dblSpinner_testSetMax_changesSpinnerValueWhenMaxIsGreaterThanCurrentValue() { 650 dblValueFactory.setValue(0.5); 651 assertEquals(0.5, dblSpinner.getValue()); 652 dblValueFactory.setMax(0.3); 653 assertEquals(0.3, dblSpinner.getValue()); 654 } 655 656 @Test public void dblSpinner_testSetMax_ensureThatMaxCanNotGoLessThanMin() { 657 dblValueFactory.setMin(0.5); 658 assertEquals(0.5, dblValueFactory.getMin()); 659 assertEquals(1.0, dblValueFactory.getMax()); 660 dblValueFactory.setMax(0.3); 661 assertEquals(0.5, dblValueFactory.getMin()); 662 } 663 664 @Test public void dblSpinner_testSetMax_ensureThatMaxCanEqualMin() { 665 dblValueFactory.setMin(0.5); 666 assertEquals(0.5, dblValueFactory.getMin()); 667 assertEquals(1.0, dblValueFactory.getMax()); 668 dblValueFactory.setMax(0.5); 669 assertEquals(0.5, dblValueFactory.getMin()); 670 } 671 672 @Test public void dblSpinner_testSetValue_canNotExceedMax() { 673 assertEquals(0, dblValueFactory.getMin(), 0); 674 assertEquals(1.0, dblValueFactory.getMax()); 675 dblValueFactory.setValue(5.0); 676 assertEquals(1.0, dblSpinner.getValue()); 677 } 678 679 @Test public void dblSpinner_testSetValue_canNotExceedMin() { 680 assertEquals(0, dblValueFactory.getMin(), 0); 681 assertEquals(1.0, dblValueFactory.getMax(), 0); 682 dblValueFactory.setValue(-5.0); 683 assertEquals(0, dblSpinner.getValue(), 0); 684 } 685 686 687 /*************************************************************************** 688 * * 689 * ListSpinnerValueFactory tests * 690 * * 691 **************************************************************************/ 692 693 @Test public void listSpinner_testIncrement_oneStep() { 694 listValueFactory.increment(1); 695 assertEquals("string2", listValueFactory.getValue()); 696 } 697 698 @Test public void listSpinner_testIncrement_twoSteps() { 699 listValueFactory.increment(2); 700 assertEquals("string3", listValueFactory.getValue()); 701 } 702 703 @Test public void listSpinner_testIncrement_manyCalls() { 704 for (int i = 0; i < 100; i++) { 705 listValueFactory.increment(1); 706 } 707 assertEquals("string3", listValueFactory.getValue()); 708 } 709 710 @Test public void listSpinner_testIncrement_bigStepPastMaximum() { 711 listValueFactory.increment(1000); 712 assertEquals("string3", listValueFactory.getValue()); 713 } 714 715 @Test public void listSpinner_testDecrement_oneStep() { 716 listValueFactory.decrement(1); 717 assertEquals("string1", listValueFactory.getValue()); 718 } 719 720 @Test public void listSpinner_testDecrement_twoSteps() { 721 listValueFactory.decrement(2); 722 assertEquals("string1", listValueFactory.getValue()); 723 } 724 725 @Test public void listSpinner_testDecrement_manyCalls() { 726 for (int i = 0; i < 100; i++) { 727 listValueFactory.decrement(1); 728 } 729 assertEquals("string1", listValueFactory.getValue()); 730 } 731 732 @Test public void listSpinner_testDecrement_bigStepPastMinimum() { 733 listValueFactory.decrement(1000); 734 assertEquals("string1", listValueFactory.getValue()); 735 } 736 737 @Test public void listSpinner_testWrapAround_increment_oneStep() { 738 listValueFactory.setWrapAround(true); 739 listValueFactory.increment(1); // string2 740 listValueFactory.increment(1); // string3 741 listValueFactory.increment(1); // string1 742 listValueFactory.increment(1); // string2 743 listValueFactory.increment(1); // string3 744 listValueFactory.increment(1); // string1 745 listValueFactory.increment(1); // string2 746 assertEquals("string2", listValueFactory.getValue()); 747 } 748 749 @Test public void listSpinner_testWrapAround_increment_twoSteps() { 750 listValueFactory.setWrapAround(true); 751 listValueFactory.increment(2); // string1 -> string3 752 listValueFactory.increment(2); // string3 -> string2 753 listValueFactory.increment(2); // string2 -> string1 754 listValueFactory.increment(2); // string1 -> string3 755 assertEquals("string3", listValueFactory.getValue()); 756 } 757 758 @Test public void listSpinner_testWrapAround_decrement_oneStep() { 759 listValueFactory.setWrapAround(true); 760 listValueFactory.decrement(1); // string3 761 listValueFactory.decrement(1); // string2 762 listValueFactory.decrement(1); // string1 763 listValueFactory.decrement(1); // string3 764 listValueFactory.decrement(1); // string2 765 listValueFactory.decrement(1); // string1 766 listValueFactory.decrement(1); // string3 767 assertEquals("string3", listValueFactory.getValue()); 768 } 769 770 @Test public void listSpinner_testWrapAround_decrement_twoSteps() { 771 listValueFactory.setWrapAround(true); 772 listValueFactory.decrement(2); // string1 -> string2 773 listValueFactory.decrement(2); // string2 -> string3 774 listValueFactory.decrement(2); // string3 -> string1 775 listValueFactory.decrement(2); // string1 -> string2 776 assertEquals("string2", listValueFactory.getValue()); 777 } 778 779 @Test public void listSpinner_assertDefaultConverterIsNonNull() { 780 assertNotNull(listValueFactory.getConverter()); 781 } 782 783 @Test public void listSpinner_testToString_valueInRange() { 784 assertEquals("string2", listValueFactory.getConverter().toString("string2")); 785 } 786 787 @Test public void listSpinner_testToString_valueOutOfRange() { 788 assertEquals("string300", listValueFactory.getConverter().toString("string300")); 789 } 790 791 @Test public void listSpinner_testFromString_valueInRange() { 792 assertEquals("string3", listValueFactory.getConverter().fromString("string3")); 793 } 794 795 @Test public void listSpinner_testFromString_valueOutOfRange() { 796 assertEquals("string300", listValueFactory.getConverter().fromString("string300")); 797 } 798 799 @Test public void listSpinner_testListChange_changeNonSelectedItem() { 800 assertEquals("string1", listSpinner.getValue()); 801 802 strings.set(1, "string200"); // change 'string2' to 'string200' 803 804 // there should be no change 805 assertEquals("string1", listSpinner.getValue()); 806 } 807 808 @Test public void listSpinner_testListChange_changeSelectedItem() { 809 assertEquals("string1", listSpinner.getValue()); 810 811 strings.set(0, "string100"); // change 'string1' to 'string100' 812 813 // the selected value should change 814 assertEquals("string100", listSpinner.getValue()); 815 } 816 817 @Test public void listSpinner_testListChange_changeEntireList_directly() { 818 assertEquals("string1", listSpinner.getValue()); 819 820 listValueFactory.getItems().setAll("newString1", "newString2", "newString3"); 821 822 // the selected value should change 823 assertEquals("newString1", listSpinner.getValue()); 824 } 825 826 @Test public void listSpinner_testListChange_changeEntireList_usingSetter() { 827 assertEquals("string1", listSpinner.getValue()); 828 829 listValueFactory.setItems(FXCollections.observableArrayList("newString1", "newString2", "newString3")); 830 assertEquals("newString1", listSpinner.getValue()); 831 } 832 833 @Test public void listSpinner_testListChange_setItemsToNull() { 834 assertEquals("string1", listSpinner.getValue()); 835 listValueFactory.setItems(null); 836 assertNull(listSpinner.getValue()); 837 } 838 839 @Test public void listSpinner_testListChange_setItemsToNonNull() { 840 assertEquals("string1", listSpinner.getValue()); 841 listValueFactory.setItems(null); 842 assertNull(listSpinner.getValue()); 843 844 listValueFactory.setItems(FXCollections.observableArrayList("newString1", "newString2", "newString3")); 845 assertEquals("newString1", listSpinner.getValue()); 846 } 847 848 @Test public void listSpinner_testListChange_setNewEmptyListOverOldEmptyList() { 849 // this tests the issue where we replace an empty list with another. As 850 // both empty lists are equal, we are ensuring that the listeners update 851 // to the new list. 852 ObservableList<String> firstEmptyList = FXCollections.observableArrayList(); 853 ObservableList<String> newEmptyList = FXCollections.observableArrayList(); 854 855 ListSpinnerValueFactory valueFactory = new ListSpinnerValueFactory(firstEmptyList); 856 Spinner listSpinner = new Spinner(valueFactory); 857 858 valueFactory.setItems(newEmptyList); 859 assertNull(listSpinner.getValue()); 860 861 newEmptyList.addAll("newString1", "newString2", "newString3"); 862 assertEquals("newString1", listSpinner.getValue()); 863 } 864 865 866 867 /*************************************************************************** 868 * * 869 * LocalDateSpinnerValueFactory tests * 870 * * 871 **************************************************************************/ 872 873 private LocalDate nowPlusDays(int days) { 874 return LocalDate.now().plus(days, ChronoUnit.DAYS); 875 } 876 877 @Test public void localDateSpinner_testIncrement_oneStep() { 878 localDateValueFactory.increment(1); 879 assertEquals(nowPlusDays(1), localDateValueFactory.getValue()); 880 } 881 882 @Test public void localDateSpinner_testIncrement_twoSteps() { 883 localDateValueFactory.increment(2); 884 assertEquals(nowPlusDays(2), localDateValueFactory.getValue()); 885 } 886 887 @Test public void localDateSpinner_testIncrement_manyCalls() { 888 for (int i = 0; i < 100; i++) { 889 localDateValueFactory.increment(1); 890 } 891 assertEquals(nowPlusDays(10), localDateValueFactory.getValue()); 892 } 893 894 @Test public void localDateSpinner_testIncrement_bigStepPastMaximum() { 895 localDateValueFactory.increment(1000); 896 assertEquals(nowPlusDays(10), localDateValueFactory.getValue()); 897 } 898 899 @Test public void localDateSpinner_testDecrement_oneStep() { 900 localDateValueFactory.decrement(1); 901 assertEquals(nowPlusDays(-1), localDateValueFactory.getValue()); 902 } 903 904 @Test public void localDateSpinner_testDecrement_twoSteps() { 905 localDateValueFactory.decrement(2); 906 assertEquals(nowPlusDays(-2), localDateValueFactory.getValue()); 907 } 908 909 @Test public void localDateSpinner_testDecrement_manyCalls() { 910 for (int i = 0; i < 100; i++) { 911 localDateValueFactory.decrement(1); 912 } 913 assertEquals(nowPlusDays(-10), localDateValueFactory.getValue()); 914 } 915 916 @Test public void localDateSpinner_testDecrement_bigStepPastMinimum() { 917 localDateValueFactory.decrement(1000); 918 assertEquals(nowPlusDays(-10), localDateValueFactory.getValue()); 919 } 920 921 @Test public void localDateSpinner_testWrapAround_increment_oneStep() { 922 localDateValueFactory.setWrapAround(true); 923 localDateValueFactory.setValue(nowPlusDays(7)); 924 localDateValueFactory.increment(1); // nowPlusDays(8) 925 localDateValueFactory.increment(1); // nowPlusDays(9) 926 localDateValueFactory.increment(1); // nowPlusDays(10) 927 localDateValueFactory.increment(1); // nowPlusDays(-10) 928 localDateValueFactory.increment(1); // nowPlusDays(-9) 929 localDateValueFactory.increment(1); // nowPlusDays(-8) 930 localDateValueFactory.increment(1); // nowPlusDays(-7) 931 assertEquals(nowPlusDays(-7), localDateValueFactory.getValue()); 932 } 933 934 @Test public void localDateSpinner_testWrapAround_increment_twoSteps() { 935 localDateValueFactory.setWrapAround(true); 936 localDateValueFactory.setValue(nowPlusDays(7)); 937 localDateValueFactory.increment(2); // nowPlusDays(9) 938 localDateValueFactory.increment(2); // nowPlusDays(-10) 939 localDateValueFactory.increment(2); // nowPlusDays(-8) 940 localDateValueFactory.increment(2); // nowPlusDays(-6) 941 assertEquals(nowPlusDays(-6), localDateValueFactory.getValue()); 942 } 943 944 @Test public void localDateSpinner_testWrapAround_decrement_oneStep() { 945 localDateValueFactory.setWrapAround(true); 946 localDateValueFactory.setValue(nowPlusDays(-8)); 947 localDateValueFactory.decrement(1); // nowPlusDays(-9) 948 localDateValueFactory.decrement(1); // nowPlusDays(-10) 949 localDateValueFactory.decrement(1); // nowPlusDays(10) 950 localDateValueFactory.decrement(1); // nowPlusDays(9) 951 localDateValueFactory.decrement(1); // nowPlusDays(8) 952 localDateValueFactory.decrement(1); // nowPlusDays(7) 953 localDateValueFactory.decrement(1); // nowPlusDays(6) 954 assertEquals(nowPlusDays(6), localDateValueFactory.getValue()); 955 } 956 957 @Test public void localDateSpinner_testWrapAround_decrement_twoSteps() { 958 localDateValueFactory.setWrapAround(true); 959 localDateValueFactory.setValue(nowPlusDays(-8)); 960 localDateValueFactory.decrement(2); // nowPlusDays(-10) 961 localDateValueFactory.decrement(2); // nowPlusDays(9) 962 localDateValueFactory.decrement(2); // nowPlusDays(7) 963 localDateValueFactory.decrement(2); // nowPlusDays(6) 964 assertEquals(nowPlusDays(6), localDateValueFactory.getValue()); 965 } 966 967 @Test public void localDateSpinner_assertDefaultConverterIsNonNull() { 968 assertNotNull(localDateValueFactory.getConverter()); 969 } 970 971 @Test public void localDateSpinner_testToString_valueInRange() { 972 assertEquals("2014-06-27", localDateValueFactory.getConverter().toString(LocalDate.of(2014, 6, 27))); 973 } 974 975 @Test public void localDateSpinner_testToString_valueOutOfRange() { 976 assertEquals("2024-06-27", localDateValueFactory.getConverter().toString(LocalDate.of(2024, 6, 27))); 977 } 978 979 @Test public void localDateSpinner_testFromString_valueInRange() { 980 assertEquals(LocalDate.of(2014, 6, 27), localDateValueFactory.getConverter().fromString("2014-06-27")); 981 } 982 983 @Test public void localDateSpinner_testFromString_valueOutOfRange() { 984 assertEquals(LocalDate.of(2024, 6, 27), localDateValueFactory.getConverter().fromString("2024-06-27")); 985 } 986 987 @Test public void localDateSpinner_testSetMin_doesNotChangeSpinnerValueWhenMinIsLessThanCurrentValue() { 988 LocalDate newValue = LocalDate.now(); 989 localDateValueFactory.setValue(newValue); 990 assertEquals(newValue, localDateSpinner.getValue()); 991 localDateValueFactory.setMin(nowPlusDays(-3)); 992 assertEquals(newValue, localDateSpinner.getValue()); 993 } 994 995 @Test public void localDateSpinner_testSetMin_changesSpinnerValueWhenMinIsGreaterThanCurrentValue() { 996 LocalDate newValue = LocalDate.now(); 997 localDateValueFactory.setValue(newValue); 998 assertEquals(newValue, localDateSpinner.getValue()); 999 1000 LocalDate twoDaysFromNow = nowPlusDays(2); 1001 localDateValueFactory.setMin(twoDaysFromNow); 1002 assertEquals(twoDaysFromNow, localDateSpinner.getValue()); 1003 } 1004 1005 @Test public void localDateSpinner_testSetMin_ensureThatMinCanNotExceedMax() { 1006 assertEquals(nowPlusDays(-10), localDateValueFactory.getMin()); 1007 assertEquals(nowPlusDays(10), localDateValueFactory.getMax()); 1008 localDateValueFactory.setMin(nowPlusDays(20)); 1009 assertEquals(nowPlusDays(10), localDateValueFactory.getMin()); 1010 } 1011 1012 @Test public void localDateSpinner_testSetMin_ensureThatMinCanEqualMax() { 1013 assertEquals(nowPlusDays(-10), localDateValueFactory.getMin()); 1014 assertEquals(nowPlusDays(10), localDateValueFactory.getMax()); 1015 localDateValueFactory.setMin(nowPlusDays(10)); 1016 assertEquals(nowPlusDays(10), localDateValueFactory.getMin()); 1017 } 1018 1019 @Test public void localDateSpinner_testSetMax_doesNotChangeSpinnerValueWhenMaxIsGreaterThanCurrentValue() { 1020 LocalDate newValue = LocalDate.now(); 1021 localDateValueFactory.setValue(newValue); 1022 assertEquals(newValue, localDateSpinner.getValue()); 1023 localDateValueFactory.setMax(nowPlusDays(2)); 1024 assertEquals(newValue, localDateSpinner.getValue()); 1025 } 1026 1027 @Test public void localDateSpinner_testSetMax_changesSpinnerValueWhenMaxIsLessThanCurrentValue() { 1028 LocalDate newValue = nowPlusDays(4); 1029 localDateValueFactory.setValue(newValue); 1030 assertEquals(newValue, localDateSpinner.getValue()); 1031 1032 LocalDate twoDays = nowPlusDays(2); 1033 localDateValueFactory.setMax(twoDays); 1034 assertEquals(twoDays, localDateSpinner.getValue()); 1035 } 1036 1037 @Test public void localDateSpinner_testSetMax_ensureThatMaxCanNotGoLessThanMin() { 1038 localDateValueFactory.setMin(nowPlusDays(5)); 1039 assertEquals(nowPlusDays(5), localDateValueFactory.getMin()); 1040 assertEquals(nowPlusDays(10), localDateValueFactory.getMax()); 1041 localDateValueFactory.setMax(nowPlusDays(2)); 1042 assertEquals(nowPlusDays(5), localDateValueFactory.getMin()); 1043 } 1044 1045 @Test public void localDateSpinner_testSetMax_ensureThatMaxCanEqualMin() { 1046 LocalDate twoDays = nowPlusDays(2); 1047 localDateValueFactory.setMin(twoDays); 1048 assertEquals(twoDays, localDateValueFactory.getMin()); 1049 assertEquals(nowPlusDays(10), localDateValueFactory.getMax()); 1050 localDateValueFactory.setMax(twoDays); 1051 assertEquals(twoDays, localDateValueFactory.getMin()); 1052 } 1053 1054 @Test public void localDateSpinner_testSetValue_canNotExceedMax() { 1055 assertEquals(nowPlusDays(-10), localDateValueFactory.getMin()); 1056 assertEquals(nowPlusDays(10), localDateValueFactory.getMax()); 1057 localDateValueFactory.setValue(nowPlusDays(50)); 1058 assertEquals(nowPlusDays(10), localDateSpinner.getValue()); 1059 } 1060 1061 @Test public void localDateSpinner_testSetValue_canNotExceedMin() { 1062 assertEquals(nowPlusDays(-10), localDateValueFactory.getMin()); 1063 assertEquals(nowPlusDays(10), localDateValueFactory.getMax()); 1064 localDateValueFactory.setValue(nowPlusDays(-50)); 1065 assertEquals(nowPlusDays(-10), localDateSpinner.getValue()); 1066 } 1067 1068 1069 1070 /*************************************************************************** 1071 * * 1072 * LocalTimeSpinnerValueFactory tests * 1073 * * 1074 **************************************************************************/ 1075 1076 private LocalTime nowPlusHours(int hours) { 1077 return LocalTime.now().plus(hours, ChronoUnit.HOURS); 1078 } 1079 1080 private void assertTimeEquals(LocalTime expected, LocalTime actual) { 1081 // just compare hours, minutes and seconds 1082 assertEquals(expected.truncatedTo(ChronoUnit.MINUTES), actual.truncatedTo(ChronoUnit.MINUTES)); 1083 } 1084 1085 @Ignore 1086 @Test public void localTimeSpinner_testIncrement_oneStep() { 1087 localTimeValueFactory.increment(1); 1088 assertTimeEquals(nowPlusHours(1), localTimeValueFactory.getValue()); 1089 } 1090 1091 @Ignore 1092 @Test public void localTimeSpinner_testIncrement_twoSteps() { 1093 localTimeValueFactory.increment(2); 1094 assertTimeEquals(nowPlusHours(2), localTimeValueFactory.getValue()); 1095 } 1096 1097 @Ignore 1098 @Test public void localTimeSpinner_testIncrement_manyCalls() { 1099 for (int i = 0; i < 100; i++) { 1100 localTimeValueFactory.increment(1); 1101 } 1102 assertTimeEquals(LocalTime.MAX, localTimeValueFactory.getValue()); 1103 } 1104 1105 @Ignore 1106 @Test public void localTimeSpinner_testIncrement_bigStepPastMaximum() { 1107 localTimeValueFactory.increment(100000); 1108 assertTimeEquals(LocalTime.MAX, localTimeValueFactory.getValue()); 1109 } 1110 1111 @Ignore 1112 @Test public void localTimeSpinner_testDecrement_oneStep() { 1113 localTimeValueFactory.decrement(1); 1114 assertTimeEquals(nowPlusHours(-1), localTimeValueFactory.getValue()); 1115 } 1116 1117 @Ignore 1118 @Test public void localTimeSpinner_testDecrement_twoSteps() { 1119 localTimeValueFactory.decrement(2); 1120 assertTimeEquals(nowPlusHours(-2), localTimeValueFactory.getValue()); 1121 } 1122 1123 @Ignore 1124 @Test public void localTimeSpinner_testDecrement_manyCalls() { 1125 for (int i = 0; i < 100; i++) { 1126 localTimeValueFactory.decrement(1); 1127 } 1128 assertTimeEquals(LocalTime.MIN, localTimeValueFactory.getValue()); 1129 } 1130 1131 @Ignore 1132 @Test public void localTimeSpinner_testDecrement_bigStepPastMinimum() { 1133 localTimeValueFactory.decrement(100000); 1134 assertTimeEquals(LocalTime.MIN, localTimeValueFactory.getValue()); 1135 } 1136 1137 @Ignore 1138 @Test public void localTimeSpinner_testWrapAround_increment_oneStep() { 1139 localTimeValueFactory.setWrapAround(true); 1140 1141 LocalTime six_pm = LocalTime.of(18,32); 1142 localTimeValueFactory.setValue(six_pm); 1143 localTimeValueFactory.increment(1); // 19:32 1144 localTimeValueFactory.increment(1); // 20:32 1145 localTimeValueFactory.increment(1); // 21:32 1146 localTimeValueFactory.increment(1); // 22:32 1147 localTimeValueFactory.increment(1); // 23:32 1148 localTimeValueFactory.increment(1); // 00:32 1149 localTimeValueFactory.increment(1); // 01:32 1150 assertTimeEquals(LocalTime.of(01,32), localTimeValueFactory.getValue()); 1151 } 1152 1153 @Ignore 1154 @Test public void localTimeSpinner_testWrapAround_increment_twoSteps() { 1155 localTimeValueFactory.setWrapAround(true); 1156 1157 LocalTime six_pm = LocalTime.of(18,32); 1158 localTimeValueFactory.setValue(six_pm); 1159 localTimeValueFactory.increment(2); // 20:32 1160 localTimeValueFactory.increment(2); // 22:32 1161 localTimeValueFactory.increment(2); // 00:32 1162 localTimeValueFactory.increment(2); // 02:32 1163 assertTimeEquals(LocalTime.of(02,32), localTimeValueFactory.getValue()); 1164 } 1165 1166 @Test public void localTimeSpinner_testWrapAround_decrement_oneStep() { 1167 localTimeValueFactory.setWrapAround(true); 1168 1169 LocalTime six_am = LocalTime.of(06,32); 1170 localTimeValueFactory.setValue(six_am); 1171 localTimeValueFactory.decrement(1); // 05:32 1172 localTimeValueFactory.decrement(1); // 04:32 1173 localTimeValueFactory.decrement(1); // 03:32 1174 localTimeValueFactory.decrement(1); // 02:32 1175 localTimeValueFactory.decrement(1); // 01:32 1176 localTimeValueFactory.decrement(1); // 00:32 1177 localTimeValueFactory.decrement(1); // 23:32 1178 assertTimeEquals(LocalTime.of(23,32), localTimeValueFactory.getValue()); 1179 } 1180 1181 @Ignore 1182 @Test public void localTimeSpinner_testWrapAround_decrement_twoSteps() { 1183 localTimeValueFactory.setWrapAround(true); 1184 1185 LocalTime six_am = LocalTime.of(06,32); 1186 localTimeValueFactory.setValue(six_am); 1187 localTimeValueFactory.decrement(2); // 04:32 1188 localTimeValueFactory.decrement(2); // 02:32 1189 localTimeValueFactory.decrement(2); // 00:32 1190 localTimeValueFactory.decrement(2); // 22:32 1191 assertTimeEquals(LocalTime.of(22,32), localTimeValueFactory.getValue()); 1192 } 1193 1194 @Ignore 1195 @Test public void localTimeSpinner_assertDefaultConverterIsNonNull() { 1196 assertNotNull(localTimeValueFactory.getConverter()); 1197 } 1198 1199 @Ignore("Not safe when run early in the morning - needs refining when time permits") 1200 @Test public void localTimeSpinner_testSetMin_doesNotChangeSpinnerValueWhenMinIsLessThanCurrentValue() { 1201 LocalTime newValue = LocalTime.now(); 1202 localTimeValueFactory.setValue(newValue); 1203 assertTimeEquals(newValue, localTimeSpinner.getValue()); 1204 localTimeValueFactory.setMin(nowPlusHours(-3)); 1205 assertTimeEquals(newValue, localTimeSpinner.getValue()); 1206 } 1207 1208 @Ignore("Not safe when late at night - needs refining when time permits") 1209 @Test public void localTimeSpinner_testSetMin_changesSpinnerValueWhenMinIsGreaterThanCurrentValue() { 1210 LocalTime newValue = LocalTime.now(); 1211 localTimeValueFactory.setValue(newValue); 1212 assertTimeEquals(newValue, localTimeSpinner.getValue()); 1213 1214 LocalTime twoDaysFromNow = nowPlusHours(2); 1215 localTimeValueFactory.setMin(twoDaysFromNow); 1216 assertTimeEquals(twoDaysFromNow, localTimeSpinner.getValue()); 1217 } 1218 1219 @Test public void localTimeSpinner_testSetMin_ensureThatMinCanEqualMax() { 1220 assertTimeEquals(LocalTime.MIN, localTimeValueFactory.getMin()); 1221 assertTimeEquals(LocalTime.MAX, localTimeValueFactory.getMax()); 1222 localTimeValueFactory.setMin(LocalTime.MAX); 1223 assertTimeEquals(LocalTime.MAX, localTimeValueFactory.getMin()); 1224 } 1225 1226 @Ignore 1227 @Test public void localTimeSpinner_testSetMax_doesNotChangeSpinnerValueWhenMaxIsGreaterThanCurrentValue() { 1228 LocalTime newValue = LocalTime.now(); 1229 localTimeValueFactory.setValue(newValue); 1230 assertTimeEquals(newValue, localTimeSpinner.getValue()); 1231 localTimeValueFactory.setMax(nowPlusHours(2)); 1232 assertTimeEquals(newValue, localTimeSpinner.getValue()); 1233 } 1234 1235 @Ignore 1236 @Test public void localTimeSpinner_testSetMax_changesSpinnerValueWhenMaxIsLessThanCurrentValue() { 1237 LocalTime newValue = nowPlusHours(4); 1238 localTimeValueFactory.setValue(newValue); 1239 assertTimeEquals(newValue, localTimeSpinner.getValue()); 1240 1241 LocalTime twoDays = nowPlusHours(2); 1242 localTimeValueFactory.setMax(twoDays); 1243 assertTimeEquals(twoDays, localTimeSpinner.getValue()); 1244 } 1245 1246 @Ignore 1247 @Test public void localTimeSpinner_testSetMax_ensureThatMaxCanNotGoLessThanMin() { 1248 localTimeValueFactory.setMin(nowPlusHours(5)); 1249 assertTimeEquals(nowPlusHours(5), localTimeValueFactory.getMin()); 1250 assertTimeEquals(LocalTime.MAX, localTimeValueFactory.getMax()); 1251 localTimeValueFactory.setMax(nowPlusHours(2)); 1252 assertTimeEquals(nowPlusHours(5), localTimeValueFactory.getMin()); 1253 } 1254 1255 @Ignore 1256 @Test public void localTimeSpinner_testSetMax_ensureThatMaxCanEqualMin() { 1257 LocalTime twoDays = nowPlusHours(2); 1258 localTimeValueFactory.setMin(twoDays); 1259 assertTimeEquals(twoDays, localTimeValueFactory.getMin()); 1260 assertTimeEquals(LocalTime.MAX, localTimeValueFactory.getMax()); 1261 localTimeValueFactory.setMax(twoDays); 1262 assertTimeEquals(twoDays, localTimeValueFactory.getMin()); 1263 } 1264 1265 1266 /*************************************************************************** 1267 * * 1268 * Tests for bugs * 1269 * * * 1270 **************************************************************************/ 1271 1272 @Test public void test_rt_39655_decrement() { 1273 assertEquals(5, (int) intSpinner.getValue()); 1274 intSpinner.setEditable(true); 1275 intSpinner.getEditor().setText("7"); 1276 intSpinner.decrement(); 1277 assertEquals(6, (int) intSpinner.getValue()); 1278 } 1279 1280 @Test public void test_rt_39655_increment() { 1281 assertEquals(5, (int) intSpinner.getValue()); 1282 intSpinner.setEditable(true); 1283 intSpinner.getEditor().setText("7"); 1284 intSpinner.increment(); 1285 assertEquals(8, (int) intSpinner.getValue()); 1286 } 1287 }