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