1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 package org.openoffice.test.vcl.widgets; 25 26 import org.openoffice.test.common.SystemUtil; 27 import org.openoffice.test.vcl.Tester; 28 import org.openoffice.test.vcl.client.Constant; 29 30 /** 31 * 32 */ 33 public class VclMenuItem extends VclWidget { 34 35 private int id = -1; 36 37 private String[] path = null; 38 39 private VclMenu menu = null; 40 41 /** 42 * Construct menu item with its ID 43 * 44 * @param id 45 */ VclMenuItem(VclApp app, int id)46 public VclMenuItem(VclApp app, int id) { 47 super(app); 48 this.id = id; 49 } 50 51 /** 52 * Construct menu item with its path like 53 * "RootMenuItem->Level1Item->Level2Item". 54 * 55 * @param path 56 */ VclMenuItem(VclApp app, String path)57 public VclMenuItem(VclApp app, String path) { 58 super(app); 59 this.path = path.split("->"); 60 } 61 62 /** 63 * Vcl Menu Item on menu bar 64 * 65 * @param menu 66 * @param id 67 */ VclMenuItem(VclMenu menu, int id)68 public VclMenuItem(VclMenu menu, int id) { 69 super(menu.app); 70 this.id = id; 71 this.menu = menu; 72 this.app = menu.app; 73 } 74 75 /** 76 * Vcl Menu Item on menu bar 77 * 78 * @param menu 79 * @param path 80 */ VclMenuItem(VclMenu menu, String path)81 public VclMenuItem(VclMenu menu, String path) { 82 super(menu.app); 83 this.path = path.split("->"); 84 this.menu = menu; 85 this.app = menu.app; 86 } 87 invoke(int methodId)88 private Object invoke(int methodId) { 89 int id = getId(); 90 if (id == -1) 91 throw new RuntimeException("Menu item '" + path[path.length - 1] + "' can be found!"); 92 return app.caller.callCommand(methodId, new Object[] { id }); 93 } 94 95 /** 96 * 97 * @return 98 */ getId()99 public int getId() { 100 VclMenu menu = new VclMenu(app); 101 if (path != null) { 102 int count = menu.getItemCount(); 103 for (int i = 0; i < count; i++) { 104 VclMenuItem item = menu.getItem(i); 105 if (item == null) 106 continue; 107 String itemText = path[path.length - 1]; 108 // if (item.getTextWithoutMneumonic().contains(itemText)) { 109 //Change "contains" into "equals" to avoid the blocking while search for menu item 110 if (item.getTextWithoutMneumonic().equals(itemText)) { 111 return item.getId(); 112 } 113 } 114 115 return -1; 116 } 117 118 return this.id; 119 } 120 121 /** 122 * Select the menu item 123 * 124 */ select()125 public void select() { 126 if (menu != null) 127 menu.use(); 128 for (int i = 0; i < path.length; i++) { 129 new VclMenuItem(app, path[i]).pick(); 130 Tester.sleep(0.5); 131 } 132 } 133 pick()134 private void pick() { 135 invoke(Constant.RC_MenuSelect); 136 } 137 138 /** 139 * Select the parent of the item 140 * 141 */ selectParent()142 public void selectParent() { 143 if (menu != null) 144 menu.use(); 145 for (int i = 0; i < path.length - 1; i++) 146 new VclMenuItem(app, path[i]).pick(); 147 } 148 149 /** 150 * Check if the menu item exists 151 * 152 * @return 153 */ exists()154 public boolean exists() { 155 return getId() != -1; 156 } 157 158 /** 159 * Check if the menu item is selected! 160 * 161 * @return 162 */ isSelected()163 public boolean isSelected() { 164 return ((Boolean) invoke(Constant.RC_MenuIsItemChecked)).booleanValue(); 165 } 166 167 /** 168 * Check if the menu item is enabled 169 * 170 * @return 171 */ isEnabled()172 public boolean isEnabled() { 173 return ((Boolean) invoke(Constant.RC_MenuIsItemEnabled)).booleanValue(); 174 } 175 176 /** 177 * Get the menu item position 178 * 179 * @return 180 */ getPosition()181 public int getPosition() { 182 return ((Long) invoke(Constant.RC_MenuGetItemPos)).intValue(); 183 } 184 185 /** 186 * Get the menu item text 187 * 188 * @return 189 */ getText()190 public String getText() { 191 return (String) invoke(Constant.RC_MenuGetItemText); 192 } 193 194 /** 195 * Get the command id which is UNO-Slot 196 * 197 * @return 198 */ getCommand()199 public String getCommand() { 200 return (String) invoke(Constant.RC_MenuGetItemCommand); 201 } 202 203 /** 204 * Get the accelerator character 205 */ getAccelerator()206 public int getAccelerator() { 207 String text = this.getText(); 208 if (text == null) 209 return 0; 210 int index = text.indexOf("~"); 211 return index != -1 && index + 1 < text.length() ? text.charAt(index + 1) : 0; 212 } 213 214 /** 215 * Get text without mneumonic 216 */ getTextWithoutMneumonic()217 public String getTextWithoutMneumonic() { 218 String text = this.getText(); 219 return text != null ? text.replace("~", "") : text; 220 } 221 222 /** 223 * Check if the menu item is showing 224 */ isShowing()225 public boolean isShowing() { 226 return exists(); 227 } 228 229 /** 230 * Check if the menu item has sub menu 231 * 232 * @return 233 */ hasSubMenu()234 public boolean hasSubMenu() { 235 return (Boolean) invoke(Constant.RC_MenuHasSubMenu); 236 } 237 toString()238 public String toString() { 239 return "ID:" + getId() + ", Text:" + getText() + ", Selected:" + isSelected() + ", Enabled:" + isEnabled() + ", Command:" + getCommand() 240 + ", Position:" + getPosition(); 241 } 242 243 /** 244 * Check if the widget exists in a period of time 245 */ exists(double iTimeout)246 public boolean exists(double iTimeout) { 247 return exists(iTimeout, 1); 248 } 249 250 /** 251 * Check if the widget exists in a period of time 252 */ exists(double iTimeout, double interval)253 public boolean exists(double iTimeout, double interval) { 254 long startTime = System.currentTimeMillis(); 255 while (System.currentTimeMillis() - startTime < iTimeout * 1000) { 256 if (exists()) 257 return true; 258 SystemUtil.sleep(interval); 259 } 260 261 return exists(); 262 } 263 } 264