1
1
/*
2
- * Copyright (c) 2010, 2022 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2010, 2024 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
26
26
package test .javafx .scene .control ;
27
27
28
28
import javafx .css .CssMetaData ;
29
+
29
30
import static test .com .sun .javafx .scene .control .infrastructure .ControlTestUtils .*;
30
31
32
+ import javafx .scene .input .MouseEvent ;
33
+ import javafx .util .Duration ;
31
34
import test .com .sun .javafx .pgstub .StubToolkit ;
32
35
import com .sun .javafx .tk .Toolkit ;
33
36
import javafx .beans .property .BooleanProperty ;
47
50
import javafx .scene .shape .Rectangle ;
48
51
import javafx .scene .text .Font ;
49
52
import javafx .scene .text .TextAlignment ;
50
- import static org .junit .Assert .*;
51
53
54
+ import static org .junit .Assert .*;
52
55
53
56
import org .junit .Before ;
57
+ import org .junit .After ;
54
58
import org .junit .Ignore ;
55
59
import org .junit .Test ;
60
+ import test .com .sun .javafx .scene .control .infrastructure .MouseEventGenerator ;
61
+ import test .com .sun .javafx .scene .control .infrastructure .StageLoader ;
62
+
63
+ import java .nio .charset .StandardCharsets ;
64
+ import java .util .Base64 ;
56
65
57
66
public class TooltipTest {
58
67
private TooltipShim toolTip ;//Empty string
59
68
private TooltipShim dummyToolTip ;//Empty string
60
69
61
- @ Before public void setup () {
62
- assertTrue ( Toolkit . getToolkit () instanceof StubToolkit ); // Ensure StubToolkit is loaded
70
+ private StageLoader stageLoader ;
71
+ private StubToolkit toolkit ;
63
72
73
+ @ Before
74
+ public void setup () {
64
75
toolTip = new TooltipShim ();
65
76
dummyToolTip = new TooltipShim ("dummy" );
66
- }
67
77
78
+ toolkit = (StubToolkit ) Toolkit .getToolkit ();
79
+ toolkit .setAnimationTime (0 );
80
+ }
68
81
82
+ @ After
83
+ public void tearDown () {
84
+ if (stageLoader != null ) {
85
+ stageLoader .dispose ();
86
+ }
87
+ }
69
88
70
89
/*********************************************************************
71
90
* Tests for the constructors *
@@ -318,7 +337,7 @@ public class TooltipTest {
318
337
319
338
@ Test public void whenWrapTextIsSpecifiedViaCSSAndIsNotBound_CssMetaData_isSettable_ReturnsTrue () {
320
339
CssMetaData styleable = ((StyleableProperty )toolTip .wrapTextProperty ()).getCssMetaData ();
321
- assertTrue (styleable .isSettable (toolTip .get_bridge ()));
340
+ assertTrue (styleable .isSettable (toolTip .get_bridge ()));
322
341
}
323
342
324
343
@ Test public void canSpecifyWrapTextViaCSS () {
@@ -328,15 +347,15 @@ public class TooltipTest {
328
347
329
348
@ Test public void whenFontIsBound_CssMetaData_isSettable_ReturnsFalse () {
330
349
CssMetaData styleable = ((StyleableProperty )toolTip .fontProperty ()).getCssMetaData ();
331
- assertTrue (styleable .isSettable (toolTip .get_bridge ()));
350
+ assertTrue (styleable .isSettable (toolTip .get_bridge ()));
332
351
ObjectProperty <Font > other = new SimpleObjectProperty <>();
333
352
toolTip .fontProperty ().bind (other );
334
- assertFalse (styleable .isSettable (toolTip .get_bridge ()));
353
+ assertFalse (styleable .isSettable (toolTip .get_bridge ()));
335
354
}
336
355
337
356
@ Test public void whenFontIsSpecifiedViaCSSAndIsNotBound_CssMetaData_isSettable_ReturnsTrue () {
338
357
CssMetaData styleable = ((StyleableProperty )toolTip .fontProperty ()).getCssMetaData ();
339
- assertTrue (styleable .isSettable (toolTip .get_bridge ()));
358
+ assertTrue (styleable .isSettable (toolTip .get_bridge ()));
340
359
}
341
360
342
361
@ Test public void canSpecifyFontViaCSS () {
@@ -349,12 +368,12 @@ public class TooltipTest {
349
368
assertTrue (styleable .isSettable (toolTip .get_bridge ()));
350
369
ObjectProperty <Node > other = new SimpleObjectProperty <>();
351
370
toolTip .graphicProperty ().bind (other );
352
- assertFalse (styleable .isSettable (toolTip .get_bridge ()));
371
+ assertFalse (styleable .isSettable (toolTip .get_bridge ()));
353
372
}
354
373
355
374
@ Test public void whenGraphicIsSpecifiedViaCSSAndIsNotBound_CssMetaData_isSettable_ReturnsTrue () {
356
375
CssMetaData styleable = ((StyleableProperty )toolTip .graphicProperty ()).getCssMetaData ();
357
- assertTrue (styleable .isSettable (toolTip .get_bridge ()));
376
+ assertTrue (styleable .isSettable (toolTip .get_bridge ()));
358
377
}
359
378
360
379
@ Ignore ("CSS sets graphicProperty indirectly" )
@@ -366,14 +385,14 @@ public class TooltipTest {
366
385
367
386
@ Test public void whenContentDisplayIsBound_CssMetaData_isSettable_ReturnsFalse () {
368
387
CssMetaData styleable = ((StyleableProperty )toolTip .contentDisplayProperty ()).getCssMetaData ();
369
- assertTrue (styleable .isSettable (toolTip .get_bridge ()));
388
+ assertTrue (styleable .isSettable (toolTip .get_bridge ()));
370
389
ObjectProperty <ContentDisplay > other = new SimpleObjectProperty <>();
371
390
toolTip .contentDisplayProperty ().bind (other );
372
- assertFalse (styleable .isSettable (toolTip .get_bridge ()));
391
+ assertFalse (styleable .isSettable (toolTip .get_bridge ()));
373
392
}
374
- @ Test public void whenContentDisplayIsSpecifiedViaCSSAndIsNotBound_CssMetaData_isSettable_ReturnsTrue () {
393
+ @ Test public void whenContentDisplayIsSpecifiedViaCSSAndIsNotBound_CssMetaData_isSettable_ReturnsTrue () {
375
394
CssMetaData styleable = ((StyleableProperty )toolTip .contentDisplayProperty ()).getCssMetaData ();
376
- assertTrue (styleable .isSettable (toolTip .get_bridge ()));
395
+ assertTrue (styleable .isSettable (toolTip .get_bridge ()));
377
396
}
378
397
379
398
@ Test public void canSpecifyContentDisplayViaCSS () {
@@ -383,15 +402,15 @@ public class TooltipTest {
383
402
384
403
@ Test public void whenGraphicTextGapIsBound_CssMetaData_isSettable_ReturnsFalse () {
385
404
CssMetaData styleable = ((StyleableProperty )toolTip .graphicTextGapProperty ()).getCssMetaData ();
386
- assertTrue (styleable .isSettable (toolTip .get_bridge ()));
405
+ assertTrue (styleable .isSettable (toolTip .get_bridge ()));
387
406
DoubleProperty other = new SimpleDoubleProperty ();
388
407
toolTip .graphicTextGapProperty ().bind (other );
389
- assertFalse (styleable .isSettable (toolTip .get_bridge ()));
408
+ assertFalse (styleable .isSettable (toolTip .get_bridge ()));
390
409
}
391
410
392
411
@ Test public void whenGraphicTextGapIsSpecifiedViaCSSAndIsNotBound_CssMetaData_isSettable_ReturnsTrue () {
393
412
CssMetaData styleable = ((StyleableProperty )toolTip .graphicTextGapProperty ()).getCssMetaData ();
394
- assertTrue (styleable .isSettable (toolTip .get_bridge ()));
413
+ assertTrue (styleable .isSettable (toolTip .get_bridge ()));
395
414
}
396
415
397
416
@ Test public void canSpecifyGraphicTextGapViaCSS () {
@@ -531,5 +550,161 @@ public class TooltipTest {
531
550
}
532
551
}
533
552
553
+ /**
554
+ * A {@link Tooltip} once was showing and quickly hiding itself in order to process the CSS.
555
+ * This was changed in <a href="https://bugs.openjdk.org/browse/JDK-8296387">JDK-8296387</a>
556
+ * and this test ensure that this is the case.
557
+ */
558
+ @ Test
559
+ public void testTooltipShouldNotBeShownBeforeDelayIsUp () {
560
+ toolTip .showingProperty ().addListener (inv -> fail ());
561
+ Rectangle rect = new Rectangle (0 , 0 , 100 , 100 );
562
+
563
+ stageLoader = new StageLoader (rect );
564
+
565
+ Tooltip .install (rect , toolTip );
566
+
567
+ MouseEvent mouseEvent = MouseEventGenerator .generateMouseEvent (MouseEvent .MOUSE_MOVED , 1 , 1 );
568
+ rect .fireEvent (mouseEvent );
569
+ }
570
+
571
+ @ Test
572
+ public void testTooltipShouldNotBeShownBeforeDefaultDelay () {
573
+ Rectangle rect = new Rectangle (0 , 0 , 100 , 100 );
574
+
575
+ stageLoader = new StageLoader (rect );
576
+
577
+ Tooltip .install (rect , toolTip );
578
+
579
+ MouseEvent mouseEvent = MouseEventGenerator .generateMouseEvent (MouseEvent .MOUSE_MOVED , 1 , 1 );
580
+ rect .fireEvent (mouseEvent );
581
+
582
+ assertFalse (toolTip .isShowing ());
583
+
584
+ toolkit .setAnimationTime (999 );
585
+
586
+ assertFalse (toolTip .isShowing ());
587
+ }
588
+
589
+ @ Test
590
+ public void testTooltipShouldBeShownAfterDefaultDelay () {
591
+ Rectangle rect = new Rectangle (0 , 0 , 100 , 100 );
592
+
593
+ stageLoader = new StageLoader (rect );
594
+
595
+ Tooltip .install (rect , toolTip );
596
+
597
+ assertFalse (toolTip .isShowing ());
598
+
599
+ assertTooltipShownAfter (rect , 1000 );
600
+ assertTooltipHiddenAfter (rect , 200 );
601
+ }
602
+
603
+ @ Test
604
+ public void testTooltipShouldBeHiddenAfterHideDelay () {
605
+ int delay = 50 ;
606
+ toolTip .setHideDelay (Duration .millis (delay ));
607
+
608
+ Rectangle rect = new Rectangle (0 , 0 , 100 , 100 );
609
+
610
+ stageLoader = new StageLoader (rect );
611
+
612
+ Tooltip .install (rect , toolTip );
613
+
614
+ assertFalse (toolTip .isShowing ());
615
+
616
+ assertTooltipShownAfter (rect , 1000 );
617
+ assertTooltipHiddenAfter (rect , delay );
618
+ }
619
+
620
+ @ Test
621
+ public void testTooltipShouldBeShownAfterSetShowDelay () {
622
+ int delay = 200 ;
623
+ toolTip .setShowDelay (Duration .millis (delay ));
624
+
625
+ Rectangle rect = new Rectangle (0 , 0 , 100 , 100 );
626
+
627
+ stageLoader = new StageLoader (rect );
628
+
629
+ Tooltip .install (rect , toolTip );
630
+
631
+ assertFalse (toolTip .isShowing ());
632
+
633
+ assertTooltipShownAfter (rect , delay );
634
+ assertTooltipHiddenAfter (rect , 200 );
635
+ }
636
+
637
+ @ Test
638
+ public void testTooltipShouldBeShownAfterSetStyleShowDelay () {
639
+ toolTip .setStyle ("-fx-show-delay: 200ms;" );
640
+
641
+ Rectangle rect = new Rectangle (0 , 0 , 100 , 100 );
642
+
643
+ stageLoader = new StageLoader (rect );
644
+
645
+ Tooltip .install (rect , toolTip );
646
+
647
+ assertFalse (toolTip .isShowing ());
648
+
649
+ assertTooltipShownAfter (rect , 200 );
650
+ assertTooltipHiddenAfter (rect , 200 );
651
+ }
652
+
653
+ @ Test
654
+ public void testTooltipShouldBeShownAfterSetCssShowDelay () {
655
+ Rectangle rect = new Rectangle (0 , 0 , 100 , 100 );
656
+
657
+ stageLoader = new StageLoader (rect );
658
+ stageLoader .getStage ().getScene ().getStylesheets ().setAll (toBase64 (".tooltip { -fx-show-delay: 200ms; }" ));
659
+
660
+ Tooltip .install (rect , toolTip );
661
+
662
+ assertFalse (toolTip .isShowing ());
663
+
664
+ assertTooltipShownAfter (rect , 200 );
665
+ assertTooltipHiddenAfter (rect , 200 );
666
+ }
667
+
668
+ @ Test
669
+ public void testTooltipChangeShowDelayCss () {
670
+ Rectangle rect = new Rectangle (0 , 0 , 100 , 100 );
671
+
672
+ stageLoader = new StageLoader (rect );
673
+ stageLoader .getStage ().getScene ().getStylesheets ().setAll (toBase64 (".tooltip { -fx-show-delay: 200ms; }" ));
674
+
675
+ Tooltip .install (rect , toolTip );
676
+
677
+ assertFalse (toolTip .isShowing ());
678
+
679
+ assertTooltipShownAfter (rect , 200 );
680
+ assertTooltipHiddenAfter (rect , 200 );
681
+
682
+ stageLoader .getStage ().getScene ().getStylesheets ().setAll (toBase64 (".tooltip { -fx-show-delay: 450ms; }" ));
683
+
684
+ assertTooltipShownAfter (rect , 450 );
685
+ assertTooltipHiddenAfter (rect , 200 );
686
+ }
687
+
688
+ private void assertTooltipShownAfter (Rectangle rect , int millis ) {
689
+ MouseEvent mouseEvent = MouseEventGenerator .generateMouseEvent (MouseEvent .MOUSE_MOVED , 1 , 1 );
690
+ rect .fireEvent (mouseEvent );
691
+
692
+ toolkit .setAnimationTime (toolkit .getCurrentTime () + millis );
693
+
694
+ assertTrue (toolTip .isShowing ());
695
+ }
696
+
697
+ private void assertTooltipHiddenAfter (Rectangle rect , int millis ) {
698
+ MouseEvent mouseEvent = MouseEventGenerator .generateMouseEvent (MouseEvent .MOUSE_EXITED , -1 , -1 );
699
+ rect .fireEvent (mouseEvent );
700
+
701
+ toolkit .setAnimationTime (toolkit .getCurrentTime () + millis );
702
+
703
+ assertFalse (toolTip .isShowing ());
704
+ }
705
+
706
+ private String toBase64 (String css ) {
707
+ return "data:base64," + Base64 .getUrlEncoder ().encodeToString (css .getBytes (StandardCharsets .UTF_8 ));
708
+ }
534
709
535
710
}
0 commit comments