1<?xml version="1.0" encoding="UTF-8"?>
2<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
3<script:module xmlns:script="http://openoffice.org/2000/script" script:name="beans_XMultiPropertyStates" script:language="StarBasic">
4
5
6'*************************************************************************
7'
8' DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9'
10' Copyright 2000, 2010 Oracle and/or its affiliates.
11'
12' OpenOffice.org - a multi-platform office productivity suite
13'
14' This file is part of OpenOffice.org.
15'
16' OpenOffice.org is free software: you can redistribute it and/or modify
17' it under the terms of the GNU Lesser General Public License version 3
18' only, as published by the Free Software Foundation.
19'
20' OpenOffice.org is distributed in the hope that it will be useful,
21' but WITHOUT ANY WARRANTY; without even the implied warranty of
22' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23' GNU Lesser General Public License version 3 for more details
24' (a copy is included in the LICENSE file that accompanied this code).
25'
26' You should have received a copy of the GNU Lesser General Public License
27' version 3 along with OpenOffice.org.  If not, see
28' <http://www.openoffice.org/license.html>
29' for a copy of the LGPLv3 License.
30'
31'*************************************************************************
32'*************************************************************************
33
34
35
36' Be sure that all variables are dimensioned:
37option explicit
38
39
40Function getPropNames(xPropSet As Object) As Variant
41    Dim props As Variant
42    Dim propNames As Variant
43    Dim i As Integer, propCount As Integer
44
45    props = xPropSet.getPropertySetInfo().getProperties()
46	propCount = 0
47    for i = 0 to ubound (props)
48    	if (props(i).Attributes AND com.sun.star.beans.PropertyAttribute.READONLY) = 0 _
49    		then propCount = propCount + 1
50    next i
51
52    propNames = DimArray(propCount - 1)
53    aProps = DimArray(propCount - 1)
54
55    propCount = 0
56    for i = 0 to ubound(props)
57    	if (props(i).Attributes AND com.sun.star.beans.PropertyAttribute.READONLY) = 0 then
58	        propNames(propCount) = props(i).Name
59	        aProps(propCount) = props(i)
60    		propCount = propCount + 1
61    	endif
62    next i
63
64    getPropNames = propNames
65End Function
66
67Dim aProps As Variant
68
69
70Sub RunTest()
71
72'*************************************************************************
73' INTERFACE:
74' com.sun.star.beans.XMultiPropertyStates
75'*************************************************************************
76On Error Goto ErrHndl
77    Dim bOK As Boolean
78
79    Dim aPropNames As Variant
80    Dim aDefaults As Variant
81    Dim aStates As Variant
82
83    if NOT hasUnoInterfaces(oObj, "com.sun.star.beans.XPropertySet") then
84        Out.Log("The compoent doesn't support XPropertySet this test must be reviewed !!!")
85        exit sub
86    end if
87
88    bOK = true
89    aPropNames = getPropNames(oObj)
90
91    if NOT utils.isSorted(aPropNames) then
92        Out.Log("Property names are not sorted : sorting ...")
93        utils.bubbleSort(aPropNames, false, aProps)
94    end if
95
96    Out.Log("Totally " + (ubound(aPropNames) + 1) + " properties encountered.")
97
98    Test.StartMethod("getPropertyDefaults()")
99
100    aDefaults = oObj.getPropertyDefaults(aPropNames)
101    Out.Log("Number of default values: " + (ubound(aDefaults) + 1))
102    bOK = ubound(aDefaults) = ubound(aPropNames)
103
104    Test.MethodTested("getPropertyDefaults()", bOK)
105
106
107    Test.StartMethod("getPropertyStates()")
108    bOK = true
109
110    aStates = oObj.getPropertyStates(aPropNames)
111    Out.Log("Number of states: " + (ubound(aStates) + 1))
112    bOK = ubound(aStates) = ubound(aPropNames)
113
114    Test.MethodTested("getPropertyStates()", bOK)
115
116    Test.StartMethod("setPropertiesToDefault()")
117    bOK = true
118
119    Dim propName As String
120    Dim propIdx As Integer
121    Dim mayBeDef As Boolean
122    Dim i As Integer
123    propName = aPropNames(0)
124    propIdx = 0
125    mayBeDef = false
126
127    ' searching for property which currently don't have default value and preferable has MAYBEDEFAULT attr
128    ' if no such properties are found then the first one is selected
129    for i = 0 to ubound(aPropNames)
130        if NOT mayBeDef AND aStates(i) &lt;&gt; com.sun.star.beans.PropertyState.DEFAULT_VALUE then
131            propName = aPropNames(i)
132            propIdx = i
133            if (aProps(i).Attributes AND com.sun.star.beans.PropertyAttribute.MAYBEDEFAULT) > 0 then
134                Out.Log("Property " + propName + " 'may be default' and doesn't have default value")
135                mayBeDef = true
136            end if
137        end if
138    next i
139    Out.Log("The property " + propName + " selected")
140
141    oObj.setPropertiesToDefault(Array(propName))
142
143    aStates = oObj.getPropertyStates(aPropNames)
144    if aStates(propIdx) &lt;&gt; com.sun.star.beans.PropertyState.DEFAULT_VALUE then
145        Out.Log("The property didn't change its state to default ...")
146        if mayBeDef then
147            Out.Log("   ... and it may be default - FAILED")
148            bOK = false
149        else
150            Out.Log("   ... but it may not be default - OK")
151        end if
152    end if
153
154    Test.MethodTested("setPropertiesToDefault()", bOK)
155
156    Test.StartMethod("setAllPropertiesToDefault()")
157    bOK = true
158
159    oObj.setAllPropertiesToDefault()
160
161    Out.Log("Checking that all properties are now in DEFAULT state excepting may be those which 'cann't be default'")
162    aStates = oObj.getPropertyStates(aPropNames)
163    for i = 0 to ubound(aStates)
164        if aStates(i) &lt;&gt; com.sun.star.beans.PropertyState.DEFAULT_VALUE then
165            Out.Log("The property " + aPropNames(i) + " didn't change its state to default ...")
166            if (aProps(i).Attributes AND com.sun.star.beans.PropertyAttribute.MAYBEDEFAULT) > 0 then
167                Out.Log("   ... and it has MAYBEDEFAULT attribute - FAILED")
168                bOK = false
169            else
170                Out.Log("   ... but it has no MAYBEDEFAULT attribute - OK")
171            end if
172        end if
173    next i
174
175    Test.MethodTested("setAllPropertiesToDefault()", bOK)
176
177Exit Sub
178ErrHndl:
179    Test.Exception()
180    bOK = false
181    resume next
182End Sub
183</script:module>
184