< prev index next >

modules/controls/src/test/java/test/javafx/scene/control/AlertTest.java

Print this page
rev 9280 : 8088397: [Dialog] ButtonType text not updated when Locale changes


  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 test.javafx.scene.control;
  26 
  27 import javafx.application.Platform;
  28 import javafx.scene.control.Alert;
  29 import javafx.scene.control.ButtonType;
  30 import javafx.scene.control.Dialog;
  31 import javafx.scene.control.DialogShim;
  32 import javafx.scene.control.HeavyweightDialogShim;
  33 import javafx.stage.Stage;
  34 import javafx.stage.WindowEvent;

  35 import org.junit.After;
  36 import org.junit.Test;
  37 
  38 import static org.junit.Assert.*;
  39 
  40 /**
  41  * Note that this class tests non-blocking alerts only. For blocking alerts,
  42  * there exists an exact copy of this code in
  43  * rt\tests\system\src\test\java\javafx\scene\control
  44  *
  45  * Whenever this file changes, the contents of the class should be copied over
  46  * to the system test class __with no changes__.
  47  */
  48 public class AlertTest {
  49 
  50     private static final String DUMMY_RESULT = "dummy";
  51     static boolean blocking = false;
  52 
  53     private Dialog<ButtonType> dialog;
  54 
  55     private boolean closeWasForcedButStageWasShowing = false;
  56     private boolean closeVetoed = false;
  57     private Object result = DUMMY_RESULT;
  58 
  59     @After public void cleanup() {

  60         getStage(dialog).close();
  61         dialog = null;

  62         result = DUMMY_RESULT;
  63         closeVetoed = false;
  64         closeWasForcedButStageWasShowing = false;
  65     }
  66 
  67     private static Stage getStage(Dialog<?> dialog) {
  68         return HeavyweightDialogShim.get_stage(DialogShim.get_dialog(dialog));
  69     }
  70 
  71     private void showAndHideDialog(Dialog<?> dialog, boolean normalClose) {
  72         if (dialog.isShowing()) return;
  73 
  74         if (blocking) {
  75             new Thread(() -> {
  76                 try {
  77                     // wait a short while after showing the dialog and try to
  78                     // close it.
  79                     Thread.sleep(750);
  80                     Platform.runLater(() -> hideDialog(dialog, normalClose));
  81 


 292 
 293     @Test public void alert_twoButtons_noCancel_normalClose() {
 294         dialog = new Alert(Alert.AlertType.NONE, "Hello World!");
 295         dialog.getDialogPane().getButtonTypes().addAll(ButtonType.APPLY, ButtonType.YES);
 296         assertResultValue(null, dialog, true);
 297         assertCloseRequestVetoed(dialog, true);
 298     }
 299 
 300     @Test public void alert_twoButtons_withCancel_abnormalClose() {
 301         dialog = new Alert(Alert.AlertType.NONE, "Hello World!");
 302         dialog.getDialogPane().getButtonTypes().addAll(ButtonType.APPLY, ButtonType.CANCEL);
 303         assertResultValue(ButtonType.CANCEL, dialog, false);
 304         assertCloseRequestAccepted(dialog, false);
 305     }
 306 
 307     @Test public void alert_twoButtons_withCancel_normalClose() {
 308         dialog = new Alert(Alert.AlertType.NONE, "Hello World!");
 309         dialog.getDialogPane().getButtonTypes().addAll(ButtonType.APPLY, ButtonType.CANCEL);
 310         assertResultValue(ButtonType.CANCEL, dialog, true);
 311         assertCloseRequestAccepted(dialog, true);











 312     }
 313 }


  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 test.javafx.scene.control;
  26 
  27 import javafx.application.Platform;
  28 import javafx.scene.control.Alert;
  29 import javafx.scene.control.ButtonType;
  30 import javafx.scene.control.Dialog;
  31 import javafx.scene.control.DialogShim;
  32 import javafx.scene.control.HeavyweightDialogShim;
  33 import javafx.stage.Stage;
  34 import javafx.stage.WindowEvent;
  35 import java.util.Locale;
  36 import org.junit.After;
  37 import org.junit.Test;
  38 
  39 import static org.junit.Assert.*;
  40 
  41 /**
  42  * Note that this class tests non-blocking alerts only. For blocking alerts,
  43  * there exists an exact copy of this code in
  44  * rt\tests\system\src\test\java\javafx\scene\control
  45  *
  46  * Whenever this file changes, the contents of the class should be copied over
  47  * to the system test class __with no changes__.
  48  */
  49 public class AlertTest {
  50 
  51     private static final String DUMMY_RESULT = "dummy";
  52     static boolean blocking = false;
  53 
  54     private Dialog<ButtonType> dialog;
  55 
  56     private boolean closeWasForcedButStageWasShowing = false;
  57     private boolean closeVetoed = false;
  58     private Object result = DUMMY_RESULT;
  59 
  60     @After public void cleanup() {
  61         if (dialog != null) {
  62             getStage(dialog).close();
  63             dialog = null;
  64         }
  65         result = DUMMY_RESULT;
  66         closeVetoed = false;
  67         closeWasForcedButStageWasShowing = false;
  68     }
  69 
  70     private static Stage getStage(Dialog<?> dialog) {
  71         return HeavyweightDialogShim.get_stage(DialogShim.get_dialog(dialog));
  72     }
  73 
  74     private void showAndHideDialog(Dialog<?> dialog, boolean normalClose) {
  75         if (dialog.isShowing()) return;
  76 
  77         if (blocking) {
  78             new Thread(() -> {
  79                 try {
  80                     // wait a short while after showing the dialog and try to
  81                     // close it.
  82                     Thread.sleep(750);
  83                     Platform.runLater(() -> hideDialog(dialog, normalClose));
  84 


 295 
 296     @Test public void alert_twoButtons_noCancel_normalClose() {
 297         dialog = new Alert(Alert.AlertType.NONE, "Hello World!");
 298         dialog.getDialogPane().getButtonTypes().addAll(ButtonType.APPLY, ButtonType.YES);
 299         assertResultValue(null, dialog, true);
 300         assertCloseRequestVetoed(dialog, true);
 301     }
 302 
 303     @Test public void alert_twoButtons_withCancel_abnormalClose() {
 304         dialog = new Alert(Alert.AlertType.NONE, "Hello World!");
 305         dialog.getDialogPane().getButtonTypes().addAll(ButtonType.APPLY, ButtonType.CANCEL);
 306         assertResultValue(ButtonType.CANCEL, dialog, false);
 307         assertCloseRequestAccepted(dialog, false);
 308     }
 309 
 310     @Test public void alert_twoButtons_withCancel_normalClose() {
 311         dialog = new Alert(Alert.AlertType.NONE, "Hello World!");
 312         dialog.getDialogPane().getButtonTypes().addAll(ButtonType.APPLY, ButtonType.CANCEL);
 313         assertResultValue(ButtonType.CANCEL, dialog, true);
 314         assertCloseRequestAccepted(dialog, true);
 315     }
 316 
 317     @Test public void jdk8088397_buttontype_strings_change_with_locale() {
 318         Locale defaultLocale = Locale.getDefault();
 319         Locale.setDefault(new Locale("en"));
 320         String englishStr = ButtonType.CANCEL.getText();
 321         Locale.setDefault(new Locale("sv"));
 322         String swedishStr = ButtonType.CANCEL.getText();
 323         Locale.setDefault(defaultLocale);
 324         assertEquals("Cancel", englishStr);
 325         assertEquals("Avbryt", swedishStr);
 326     }
 327 }
< prev index next >