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 package integration.forms; 24 25 import com.sun.star.uno.UnoRuntime; 26 import com.sun.star.beans.XPropertySet; 27 import com.sun.star.sheet.XSpreadsheet; 28 import com.sun.star.sheet.XSpreadsheets; 29 import com.sun.star.sheet.XSpreadsheetView; 30 import com.sun.star.container.XNamed; 31 import com.sun.star.container.XNameContainer; 32 import com.sun.star.container.XIndexContainer; 33 import com.sun.star.drawing.XDrawPageSupplier; 34 import com.sun.star.awt.XControl; 35 import com.sun.star.awt.XControlModel; 36 import com.sun.star.awt.XListBox; 37 import com.sun.star.lang.XMultiServiceFactory; 38 import com.sun.star.script.XLibraryContainer; 39 import com.sun.star.script.XEventAttacherManager; 40 import com.sun.star.script.ScriptEventDescriptor; 41 import com.sun.star.accessibility.XAccessible; 42 import com.sun.star.accessibility.XAccessibleContext; 43 import com.sun.star.accessibility.XAccessibleSelection; 44 import com.sun.star.frame.XStorable; 45 46 import org.junit.After; 47 import org.junit.AfterClass; 48 import org.junit.Before; 49 import org.junit.BeforeClass; 50 import org.junit.Test; 51 import static org.junit.Assert.*; 52 import org.openoffice.test.Argument; 53 import org.openoffice.test.OfficeConnection; 54 import java.io.FileInputStream; 55 import java.io.InputStreamReader; 56 import java.io.FileInputStream; 57 import java.util.Properties; 58 59 60 public class ListSelection extends integration.forms.TestCase 61 { 62 private static final OfficeConnection officeConnection = new OfficeConnection(); 63 private XMultiServiceFactory m_orb = null; 64 private Properties properties; 65 66 @BeforeClass beforeClass()67 public static void beforeClass() throws Exception 68 { 69 officeConnection.setUp(); 70 } 71 72 @AfterClass afterClass()73 public static void afterClass() throws Exception 74 { 75 officeConnection.tearDown(); 76 } 77 78 /** Creates a new instance of ListSelection */ ListSelection()79 public ListSelection() throws Exception 80 { 81 super( DocumentType.CALC ); 82 properties = new Properties(); 83 try (InputStreamReader reader = new InputStreamReader(new FileInputStream(Argument.get("properties")), "UTF-8")) { 84 properties.load(reader); 85 } 86 } 87 88 @Before before()89 public void before() throws Exception 90 { 91 m_orb = UnoRuntime.queryInterface(XMultiServiceFactory.class, officeConnection.getComponentContext().getServiceManager()); 92 } 93 94 /* ------------------------------------------------------------------ */ 95 @Test checkUserListSelection()96 public void checkUserListSelection() throws com.sun.star.uno.Exception, java.lang.Exception 97 { 98 boolean interactiveTest = Boolean.parseBoolean( properties.getProperty("Interactive") ); 99 100 if ( interactiveTest ) 101 { 102 prepareDocument(); 103 waitForUserInput(); 104 closeDocumentByUI(); 105 } 106 else 107 { 108 int runs = Integer.parseInt( properties.getProperty("Runs") ); 109 if ( runs == 0 ) 110 runs = 10; 111 112 for ( int i = 0; i < runs; ++i ) 113 { 114 System.out.println( "Round " + ( i + 1 ) + " of " + runs ); 115 prepareDocument(); 116 impl_clickListBox(); 117 synchronized( this ) { this.wait( 1000 ); } 118 closeDocument(); 119 } 120 } 121 } 122 123 /* ------------------------------------------------------------------ */ impl_clickListBox()124 final private void impl_clickListBox() 125 { 126 try 127 { 128 final int runs = 10; 129 java.util.Random generator = new java.util.Random(); 130 for ( int i = 0; i < runs; ++i ) 131 { 132 // obtain the active sheet 133 XSpreadsheetView view = (XSpreadsheetView)m_document.getCurrentView().query( XSpreadsheetView.class ); 134 XSpreadsheet activeSheet = view.getActiveSheet(); 135 136 // Accessibility access to the list box control in this sheet 137 XAccessible accessibleListBox = (XAccessible)UnoRuntime.queryInterface( 138 XAccessible.class, getListBoxControl( activeSheet ) ); 139 XAccessibleContext context = accessibleListBox.getAccessibleContext(); 140 141 // the first "accessible child" of a list box is its list 142 XAccessibleSelection accessibleList = (XAccessibleSelection)UnoRuntime.queryInterface( 143 XAccessibleSelection.class, context.getAccessibleChild( 1 ) ); 144 145 int selectPosition = generator.nextInt( 5 ); 146 String selectSheetName = getListBoxControl( activeSheet ).getItem( (short)selectPosition ); 147 accessibleList.selectAccessibleChild( selectPosition ); 148 try 149 { 150 synchronized( this ) 151 { 152 this.wait( 500 ); 153 } 154 } 155 catch( java.lang.InterruptedException e ) { } 156 157 XNamed sheetName = (XNamed)UnoRuntime.queryInterface( XNamed.class, view.getActiveSheet() ); 158 assertTrue( "sheet was not selected as expected!", sheetName.getName().equals( selectSheetName ) ); 159 } 160 } 161 catch( com.sun.star.uno.Exception e ) 162 { 163 e.printStackTrace( System.err ); 164 fail( "caught an exception: " + e.toString() ); 165 } 166 } 167 168 /* ------------------------------------------------------------------ */ impl_setupListenerScript()169 final private void impl_setupListenerScript() 170 { 171 try 172 { 173 XPropertySet docProps = dbfTools.queryPropertySet( m_document.getDocument() ); 174 XLibraryContainer basicLibs = (XLibraryContainer)UnoRuntime.queryInterface( 175 XLibraryContainer.class, docProps.getPropertyValue( "BasicLibraries" ) ); 176 XNameContainer basicLib = basicLibs.createLibrary( "default" ); 177 178 String sListSelectionScript = 179 "Option Explicit\n" + 180 "\n" + 181 "Sub onListBoxSelected( oEvent as Object )\n" + 182 " Dim oView as Object\n" + 183 " Dim oSheet as Object\n" + 184 " Dim oSheets as Object\n" + 185 "\n" + 186 " Dim oControlModel as Object\n" + 187 " Dim sSheet as String\n" + 188 "\n" + 189 " if ( oEvent.Selected <> 65535 ) Then\n" + 190 " oControlModel = oEvent.Source.Model\n" + 191 " sSheet = oControlModel.StringItemList( oEvent.Selected )\n" + 192 "\n" + 193 " oSheets = thisComponent.Sheets\n" + 194 " oSheet = oSheets.getByName(sSheet)\n" + 195 "\n" + 196 " oView = thisComponent.CurrentController\n" + 197 " oView.setActiveSheet( oSheet )\n" + 198 " End If\n" + 199 "End Sub\n" + 200 "\n" + 201 "Sub onButtonClicked\n" + 202 " MsgBox \"clicked\"\n" + 203 "End Sub\n"; 204 205 basicLib.insertByName( "callbacks", sListSelectionScript ); 206 } 207 catch( com.sun.star.uno.Exception e ) 208 { 209 e.printStackTrace( System.err ); 210 fail( "caught an exception: " + e.toString() ); 211 } 212 } 213 214 /* ------------------------------------------------------------------ */ impl_assignStarBasicScript( XPropertySet controlModel, String interfaceName, String interfaceMethod, String scriptCode )215 final private void impl_assignStarBasicScript( XPropertySet controlModel, String interfaceName, String interfaceMethod, String scriptCode ) 216 { 217 try 218 { 219 XIndexContainer parentForm = (XIndexContainer)dbfTools.getParent( controlModel, XIndexContainer.class ); 220 221 XEventAttacherManager manager = (XEventAttacherManager)UnoRuntime.queryInterface( 222 XEventAttacherManager.class, parentForm ); 223 224 int containerPosition = -1; 225 for ( int i = 0; i < parentForm.getCount(); ++i ) 226 { 227 XPropertySet child = dbfTools.queryPropertySet( parentForm.getByIndex( i ) ); 228 if ( child.equals( controlModel ) ) 229 { 230 containerPosition = i; 231 break; 232 } 233 } 234 manager.registerScriptEvent( containerPosition, new ScriptEventDescriptor( 235 interfaceName, 236 interfaceMethod, 237 "", 238 "StarBasic", 239 scriptCode 240 ) ); 241 } 242 catch( com.sun.star.uno.Exception e ) 243 { 244 e.printStackTrace( System.err ); 245 fail( "caught an exception: " + e.toString() ); 246 } 247 } 248 249 /* ------------------------------------------------------------------ */ prepareDocument()250 protected void prepareDocument() throws com.sun.star.uno.Exception, java.lang.Exception 251 { 252 super.prepareDocument(m_orb); 253 impl_setupListenerScript(); 254 255 SpreadsheetDocument document = (SpreadsheetDocument)m_document; 256 XSpreadsheets sheets = document.getSheets(); 257 258 // delete all sheets except one 259 String[] sheetNames = sheets.getElementNames(); 260 for ( short i = 1; i < sheetNames.length; ++i ) 261 sheets.removeByName( sheetNames[ i ] ); 262 263 // need 5 sheets 264 String[] newSheetNames = new String[] { "first", "second", "third", "forth", "fifth" }; 265 266 // give the first one the right name 267 XNamed sheet = (XNamed)UnoRuntime.queryInterface( XNamed.class, 268 sheets.getByName( sheetNames[ 0 ] ) 269 ); 270 sheet.setName( newSheetNames[ 0 ] ); 271 272 // add some dummy buttons 273 for ( int i = 0; i < 4; ++i ) 274 { 275 XPropertySet buttonModel = m_formLayer.createControlAndShape( "CommandButton", 10, 10 + i * 10, 30, 8 ); 276 impl_assignStarBasicScript( buttonModel, "XActionListener", "actionPerformed", "document:default.callbacks.onButtonClicked" ); 277 } 278 279 // and a list box 280 XPropertySet listBox = m_formLayer.createControlAndShape( "ListBox", 50, 10, 40, 6 ); 281 listBox.setPropertyValue( "Dropdown", new Boolean( true ) ); 282 listBox.setPropertyValue( "StringItemList", newSheetNames ); 283 listBox.setPropertyValue( "Name", "ListBox" ); 284 285 impl_assignStarBasicScript( listBox, "XItemListener", "itemStateChanged", "document:default.callbacks.onListBoxSelected" ); 286 287 // clone this sheet 288 for ( short i = 1; i < newSheetNames.length; ++i ) 289 sheets.copyByName( newSheetNames[0], newSheetNames[i], i ); 290 291 // switch the thing to alive mode 292 m_document.getCurrentView().toggleFormDesignMode(); 293 294 try 295 { 296 XStorable storable = (XStorable)m_document.query( XStorable.class ); 297 java.io.File testFile = java.io.File.createTempFile( getTestObjectName(),".ods"); 298 storable.storeAsURL( testFile.getAbsoluteFile().toURL().toString(), new com.sun.star.beans.PropertyValue[]{} ); 299 testFile.deleteOnExit(); 300 } 301 catch( java.lang.Throwable e ) 302 { 303 e.printStackTrace(); 304 fail( "caught an exception: " + e.toString() ); 305 } 306 } 307 308 /* ------------------------------------------------------------------ */ getListBoxModel( XSpreadsheet sheet )309 protected XControlModel getListBoxModel( XSpreadsheet sheet ) 310 { 311 XDrawPageSupplier suppPage = (XDrawPageSupplier)UnoRuntime.queryInterface( 312 XDrawPageSupplier.class, sheet ); 313 FormComponent formsRoot = new FormComponent( suppPage.getDrawPage() ); 314 XControlModel listBoxModel = (XControlModel)formsRoot.getByIndex( 0 ). 315 getByName( "ListBox" ).query( XControlModel.class ); 316 return listBoxModel; 317 } 318 319 /* ------------------------------------------------------------------ */ getListBoxControl( XSpreadsheet sheet )320 protected XListBox getListBoxControl( XSpreadsheet sheet ) throws com.sun.star.uno.Exception 321 { 322 return (XListBox)UnoRuntime.queryInterface( 323 XListBox.class, m_document.getCurrentView().getControl( getListBoxModel( sheet ) ) ); 324 } 325 } 326