1 /*
   2  * Copyright (c) 2007, 2018, 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 org.jemmy.swt;
  26 
  27 import org.eclipse.swt.widgets.Control;
  28 import org.eclipse.swt.widgets.Shell;
  29 import org.eclipse.swt.widgets.TabFolder;
  30 import org.eclipse.swt.widgets.ToolBar;
  31 import org.jemmy.Point;
  32 import org.jemmy.control.Wrap;
  33 import org.jemmy.input.StringMenuOwner;
  34 import org.jemmy.input.StringPopupOwner;
  35 import org.jemmy.interfaces.Parent;
  36 import org.jemmy.interfaces.PopupOwner;
  37 import org.jemmy.interfaces.Selectable;
  38 import org.jemmy.resources.StringComparePolicy;
  39 import org.testng.annotations.AfterClass;
  40 import org.testng.annotations.AfterMethod;
  41 import org.testng.annotations.BeforeClass;
  42 import org.testng.annotations.BeforeMethod;
  43 import org.testng.annotations.Test;
  44 
  45 /**
  46  *
  47  * @author shura
  48  */
  49 public class MenuTest {
  50 
  51     public MenuTest() {
  52     }
  53 
  54     @BeforeClass
  55     public static void setUpClass() throws Exception {
  56         new Thread(new Runnable() {
  57 
  58             public void run() {
  59                 Items.main(null);
  60             }
  61         }).start();
  62         Thread.sleep(2000);
  63     }
  64 
  65     @AfterClass
  66     public static void tearDownClass() throws Exception {
  67     }
  68 
  69     static Wrap<? extends Shell> shell;
  70 
  71     @BeforeMethod
  72     public void setUp() {
  73         shell = Shells.SHELLS.lookup().wrap();
  74         ((Selectable)shell.as(Parent.class, Control.class).lookup(TabFolder.class).as(Selectable.class)).selector().select("Toolbar");
  75     }
  76 
  77     @AfterMethod
  78     public void tearDown() {
  79     }
  80     String[][] menus = new String[][]{
  81         {"menu1", "item10"},
  82         {"menu0", "item00"},
  83         {"menu1", "item11"},
  84         {"menu0", "menu00", "item000"},
  85         {"menu1", "item12"},
  86         {"menu0", "item01"},};
  87 
  88     @Test
  89     public void bar() throws InterruptedException {
  90         for (String[] item : menus) {
  91             shell.as(StringMenuOwner.class).push(item);
  92             shell.waitProperty(Wrap.TEXT_PROP_NAME, item[item.length - 1] + " selected");
  93         }
  94     }
  95 
  96     @Test
  97     public void popup() throws InterruptedException {
  98         Wrap<? extends ToolBar> toolbar = shell.as(Parent.class, Control.class).
  99                 lookup(ToolBar.class).wrap();
 100         for (String[] item : menus) {
 101             toolbar.as(StringPopupOwner.class).push(PopupOwner.CENTER, item);
 102             shell.waitProperty(Wrap.TEXT_PROP_NAME, item[item.length - 1] + " selected");
 103         }
 104     }
 105 
 106     @Test
 107     public void popupPoint() throws InterruptedException {
 108         Wrap<? extends ToolBar> toolbar = shell.as(Parent.class, Control.class).
 109                 lookup(ToolBar.class).wrap();
 110         toolbar.as(StringPopupOwner.class).push(new Point(1, 1), menus[0]);
 111         toolbar.as(StringPopupOwner.class).push(new Point(1, 1), menus[3]);
 112     }
 113 
 114     @Test
 115     public void bySubString() {
 116         StringMenuOwner menu = shell.as(StringMenuOwner.class);
 117         menu.setPolicy(StringComparePolicy.SUBSTRING);
 118         menu.push("0", "menu", "");
 119         shell.waitProperty(Wrap.TEXT_PROP_NAME, "item000 selected");
 120     }
 121 }