< prev index next >

modules/javafx.controls/src/test/java/test/javafx/scene/control/skin/MenuBarSkinTest.java

Print this page

  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.control.skin;
 27 
 28 import static org.junit.Assert.assertEquals;

 29 
 30 import com.sun.javafx.menu.MenuBase;
 31 import com.sun.javafx.stage.WindowHelper;
 32 import test.com.sun.javafx.pgstub.StubToolkit;
 33 import com.sun.javafx.tk.Toolkit;
 34 import javafx.beans.value.ObservableValue;
 35 import javafx.geometry.Insets;
 36 import javafx.scene.Group;
 37 import javafx.scene.Scene;
 38 import javafx.scene.control.Menu;
 39 import javafx.scene.control.MenuBar;

 40 import javafx.stage.Stage;
 41 
 42 import java.util.List;
 43 import javafx.scene.control.skin.MenuBarSkin;
 44 
 45 import org.junit.Before;
 46 import org.junit.BeforeClass;
 47 import org.junit.Test;
 48 
 49 /**
 50  * This fails with IllegalStateException because of the toolkit's check for the FX application thread
 51  */
 52 public class MenuBarSkinTest {
 53     private MenuBar menubar;
 54     private MenuBarSkinMock skin;
 55     private static Toolkit tk;
 56     private Scene scene;
 57     private Stage stage;
 58 
 59 

146 
147             // adding the menubar from the scene should add back the system menus.
148             ((Group)scene.getRoot()).getChildren().add(menubar);
149             assertEquals(menubar.getMenus().size(), getSystemMenus().size());
150 
151             // remove, then set useSystemMenuBar to false. Upon re-adding,
152             // there should still be no system menu.
153             ((Group)scene.getRoot()).getChildren().remove(menubar);
154             assertEquals(0, getSystemMenus().size());
155 
156             menubar.setUseSystemMenuBar(false);
157             ((Group)scene.getRoot()).getChildren().add(menubar);
158             assertEquals(0, getSystemMenus().size());
159 
160             // setting useSystemMenuBar to true again, should add back the system menus.
161             menubar.setUseSystemMenuBar(true);
162             assertEquals(menubar.getMenus().size(), getSystemMenus().size());
163         }
164     }
165 





























166     public static final class MenuBarSkinMock extends MenuBarSkin {
167         boolean propertyChanged = false;
168         int propertyChangeCount = 0;
169         public MenuBarSkinMock(MenuBar menubar) {
170             super(menubar);
171         }
172 
173         public void addWatchedProperty(ObservableValue<?> p) {
174             p.addListener(o -> {
175                 propertyChanged = true;
176                 propertyChangeCount++;
177             });
178         }
179     }
180 
181     private List<MenuBase> getSystemMenus() {
182         return ((StubToolkit.StubSystemMenu)tk.getSystemMenu()).getMenus();
183     }
184 
185 }

  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.control.skin;
 27 
 28 import static org.junit.Assert.assertEquals;
 29 import static org.junit.Assert.assertTrue;
 30 
 31 import com.sun.javafx.menu.MenuBase;
 32 import com.sun.javafx.stage.WindowHelper;
 33 import test.com.sun.javafx.pgstub.StubToolkit;
 34 import com.sun.javafx.tk.Toolkit;
 35 import javafx.beans.value.ObservableValue;
 36 import javafx.geometry.Insets;
 37 import javafx.scene.Group;
 38 import javafx.scene.Scene;
 39 import javafx.scene.control.Menu;
 40 import javafx.scene.control.MenuBar;
 41 import javafx.scene.control.MenuItem;
 42 import javafx.stage.Stage;
 43 
 44 import java.util.List;
 45 import javafx.scene.control.skin.MenuBarSkin;
 46 
 47 import org.junit.Before;
 48 import org.junit.BeforeClass;
 49 import org.junit.Test;
 50 
 51 /**
 52  * This fails with IllegalStateException because of the toolkit's check for the FX application thread
 53  */
 54 public class MenuBarSkinTest {
 55     private MenuBar menubar;
 56     private MenuBarSkinMock skin;
 57     private static Toolkit tk;
 58     private Scene scene;
 59     private Stage stage;
 60 
 61 

148 
149             // adding the menubar from the scene should add back the system menus.
150             ((Group)scene.getRoot()).getChildren().add(menubar);
151             assertEquals(menubar.getMenus().size(), getSystemMenus().size());
152 
153             // remove, then set useSystemMenuBar to false. Upon re-adding,
154             // there should still be no system menu.
155             ((Group)scene.getRoot()).getChildren().remove(menubar);
156             assertEquals(0, getSystemMenus().size());
157 
158             menubar.setUseSystemMenuBar(false);
159             ((Group)scene.getRoot()).getChildren().add(menubar);
160             assertEquals(0, getSystemMenus().size());
161 
162             // setting useSystemMenuBar to true again, should add back the system menus.
163             menubar.setUseSystemMenuBar(true);
164             assertEquals(menubar.getMenus().size(), getSystemMenus().size());
165         }
166     }
167 
168     @Test public void testModifyingNonSystemMenuBar() {
169         if (tk.getSystemMenu().isSupported()) {
170             // Set system menubar to true
171             menubar.setUseSystemMenuBar(true);
172 
173             // Create a secondary menubar that is not
174             // a system menubar
175             MenuBar secondaryMenuBar = new MenuBar(
176                     new Menu("Menu 1", null, new MenuItem("Item 1")),
177                     new Menu("Menu 2", null, new MenuItem("Item 2")));
178             secondaryMenuBar.setSkin(new MenuBarSkin(secondaryMenuBar));
179 
180             // Add the secondary menubar to the scene
181             ((Group)scene.getRoot()).getChildren().add(secondaryMenuBar);
182 
183             // Verify that the menubar is the system menubar
184             assertTrue(menubar.isUseSystemMenuBar());
185 
186             // Remove a menu from the secondary menubar
187             // to trigger a rebuild of its UI and a call
188             // to the sceneProperty listener
189             secondaryMenuBar.getMenus().remove(1);
190 
191             // Verify that this has not affected whether the
192             // original menubar is the system menubar
193             assertTrue(menubar.isUseSystemMenuBar());
194         }
195     }
196 
197     public static final class MenuBarSkinMock extends MenuBarSkin {
198         boolean propertyChanged = false;
199         int propertyChangeCount = 0;
200         public MenuBarSkinMock(MenuBar menubar) {
201             super(menubar);
202         }
203 
204         public void addWatchedProperty(ObservableValue<?> p) {
205             p.addListener(o -> {
206                 propertyChanged = true;
207                 propertyChangeCount++;
208             });
209         }
210     }
211 
212     private List<MenuBase> getSystemMenus() {
213         return ((StubToolkit.StubSystemMenu)tk.getSystemMenu()).getMenus();
214     }
215 
216 }
< prev index next >