modules/graphics/src/test/java/test/javafx/scene/FocusTest.java

Print this page


   1 /*
   2  * Copyright (c) 2010, 2015, 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 
  26 package test.javafx.scene;
  27 
  28 

  29 import test.com.sun.javafx.pgstub.StubScene;
  30 import test.com.sun.javafx.pgstub.StubToolkit;
  31 import com.sun.javafx.tk.Toolkit;
  32 import static org.junit.Assert.assertEquals;
  33 import static org.junit.Assert.assertFalse;
  34 import static org.junit.Assert.assertNull;
  35 import static org.junit.Assert.assertSame;
  36 import static org.junit.Assert.assertTrue;
  37 import static org.junit.Assert.fail;
  38 
  39 import java.util.ArrayList;
  40 import java.util.List;
  41 import javafx.beans.InvalidationListener;
  42 import javafx.beans.Observable;
  43 import javafx.beans.value.ChangeListener;
  44 import javafx.beans.value.ObservableValue;
  45 import javafx.scene.Group;
  46 import javafx.scene.Node;
  47 import javafx.scene.ParentShim;
  48 import javafx.scene.Scene;


 698         assertFalse(n2.isFocused());
 699 
 700         n2.requestFocus();
 701         assertTrue(n1.isFocused());
 702         assertFalse(n2.isFocused());
 703     }
 704 
 705     @Test public void shouldCancelInputMethodWhenLoosingFocus() {
 706         final Node n1 = n();
 707         final Node n2 = n();
 708         scene.setRoot(new Group(n1, n2));
 709 
 710         stage.show();
 711 
 712         Toolkit.getToolkit().firePulse();
 713 
 714         n1.requestFocus();
 715         assertSame(n1, scene.getFocusOwner());
 716         actionTaken = false;
 717 
 718         ((StubScene) scene.impl_getPeer()).setInputMethodCompositionFinishDelegate(
 719                 () -> {
 720                     assertSame(n1, scene.getFocusOwner());
 721                     actionTaken = true;
 722                 }
 723         );
 724 
 725         n2.requestFocus();
 726 
 727         ((StubScene) scene.impl_getPeer()).setInputMethodCompositionFinishDelegate(
 728                 null);
 729 
 730         assertSame(n2, scene.getFocusOwner());
 731         assertTrue(actionTaken);
 732     }
 733 
 734     // TODO: tests for moving nodes between scenes
 735     // and active and inactive stages
 736 }
   1 /*
   2  * Copyright (c) 2010, 2016, 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 
  26 package test.javafx.scene;
  27 
  28 
  29 import com.sun.javafx.scene.SceneHelper;
  30 import test.com.sun.javafx.pgstub.StubScene;
  31 import test.com.sun.javafx.pgstub.StubToolkit;
  32 import com.sun.javafx.tk.Toolkit;
  33 import static org.junit.Assert.assertEquals;
  34 import static org.junit.Assert.assertFalse;
  35 import static org.junit.Assert.assertNull;
  36 import static org.junit.Assert.assertSame;
  37 import static org.junit.Assert.assertTrue;
  38 import static org.junit.Assert.fail;
  39 
  40 import java.util.ArrayList;
  41 import java.util.List;
  42 import javafx.beans.InvalidationListener;
  43 import javafx.beans.Observable;
  44 import javafx.beans.value.ChangeListener;
  45 import javafx.beans.value.ObservableValue;
  46 import javafx.scene.Group;
  47 import javafx.scene.Node;
  48 import javafx.scene.ParentShim;
  49 import javafx.scene.Scene;


 699         assertFalse(n2.isFocused());
 700 
 701         n2.requestFocus();
 702         assertTrue(n1.isFocused());
 703         assertFalse(n2.isFocused());
 704     }
 705 
 706     @Test public void shouldCancelInputMethodWhenLoosingFocus() {
 707         final Node n1 = n();
 708         final Node n2 = n();
 709         scene.setRoot(new Group(n1, n2));
 710 
 711         stage.show();
 712 
 713         Toolkit.getToolkit().firePulse();
 714 
 715         n1.requestFocus();
 716         assertSame(n1, scene.getFocusOwner());
 717         actionTaken = false;
 718 
 719         ((StubScene) SceneHelper.getPeer(scene)).setInputMethodCompositionFinishDelegate(
 720                 () -> {
 721                     assertSame(n1, scene.getFocusOwner());
 722                     actionTaken = true;
 723                 }
 724         );
 725 
 726         n2.requestFocus();
 727 
 728         ((StubScene) SceneHelper.getPeer(scene)).setInputMethodCompositionFinishDelegate(
 729                 null);
 730 
 731         assertSame(n2, scene.getFocusOwner());
 732         assertTrue(actionTaken);
 733     }
 734 
 735     // TODO: tests for moving nodes between scenes
 736     // and active and inactive stages
 737 }