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
37' Be sure that all variables are dimensioned:
38option explicit
39
40
41Function getPropNames(xPropSet As Object) As Variant
42    Dim props As Variant
43    Dim propNames As Variant
44    Dim i As Integer, propCount As Integer
45
46    props = xPropSet.getPropertySetInfo().getProperties()
47	propCount = 0
48    for i = 0 to ubound (props)
49    	if (props(i).Attributes AND com.sun.star.beans.PropertyAttribute.READONLY) = 0 _
50    		then propCount = propCount + 1
51    next i
52
53    propNames = DimArray(propCount - 1)
54    aProps = DimArray(propCount - 1)
55
56    propCount = 0
57    for i = 0 to ubound(props)
58    	if (props(i).Attributes AND com.sun.star.beans.PropertyAttribute.READONLY) = 0 then
59	        propNames(propCount) = props(i).Name
60	        aProps(propCount) = props(i)
61    		propCount = propCount + 1
62    	endif
63    next i
64
65    getPropNames = propNames
66End Function
67
68Dim aProps As Variant
69
70
71Sub RunTest()
72
73'*************************************************************************
74' INTERFACE:
75' com.sun.star.beans.XMultiPropertyStates
76'*************************************************************************
77On Error Goto ErrHndl
78    Dim bOK As Boolean
79
80    Dim aPropNames As Variant
81    Dim aDefaults As Variant
82    Dim aStates As Variant
83
84    if NOT hasUnoInterfaces(oObj, "com.sun.star.beans.XPropertySet") then
85        Out.Log("The compoent doesn't support XPropertySet this test must be reviewed !!!")
86        exit sub
87    end if
88
89    bOK = true
90    aPropNames = getPropNames(oObj)
91
92    if NOT utils.isSorted(aPropNames) then
93        Out.Log("Property names are not sorted : sorting ...")
94        utils.bubbleSort(aPropNames, false, aProps)
95    end if
96
97    Out.Log("Totally " + (ubound(aPropNames) + 1) + " properties encountered.")
98
99    Test.StartMethod("getPropertyDefaults()")
100
101    aDefaults = oObj.getPropertyDefaults(aPropNames)
102    Out.Log("Number of default values: " + (ubound(aDefaults) + 1))
103    bOK = ubound(aDefaults) = ubound(aPropNames)
104
105    Test.MethodTested("getPropertyDefaults()", bOK)
106
107
108    Test.StartMethod("getPropertyStates()")
109    bOK = true
110
111    aStates = oObj.getPropertyStates(aPropNames)
112    Out.Log("Number of states: " + (ubound(aStates) + 1))
113    bOK = ubound(aStates) = ubound(aPropNames)
114
115    Test.MethodTested("getPropertyStates()", bOK)
116
117    Test.StartMethod("setPropertiesToDefault()")
118    bOK = true
119
120    Dim propName As String
121    Dim propIdx As Integer
122    Dim mayBeDef As Boolean
123    Dim i As Integer
124    propName = aPropNames(0)
125    propIdx = 0
126    mayBeDef = false
127
128    ' searching for property which currently don't have default value and preferable has MAYBEDEFAULT attr
129    ' if no such properties are found then the first one is selected
130    for i = 0 to ubound(aPropNames)
131        if NOT mayBeDef AND aStates(i) &lt;&gt; com.sun.star.beans.PropertyState.DEFAULT_VALUE then
132            propName = aPropNames(i)
133            propIdx = i
134            if (aProps(i).Attributes AND com.sun.star.beans.PropertyAttribute.MAYBEDEFAULT) > 0 then
135                Out.Log("Property " + propName + " 'may be default' and doesn't have default value")
136                mayBeDef = true
137            end if
138        end if
139    next i
140    Out.Log("The property " + propName + " selected")
141
142    oObj.setPropertiesToDefault(Array(propName))
143
144    aStates = oObj.getPropertyStates(aPropNames)
145    if aStates(propIdx) &lt;&gt; com.sun.star.beans.PropertyState.DEFAULT_VALUE then
146        Out.Log("The property didn't change its state to default ...")
147        if mayBeDef then
148            Out.Log("   ... and it may be default - FAILED")
149            bOK = false
150        else
151            Out.Log("   ... but it may not be default - OK")
152        end if
153    end if
154
155    Test.MethodTested("setPropertiesToDefault()", bOK)
156
157    Test.StartMethod("setAllPropertiesToDefault()")
158    bOK = true
159
160    oObj.setAllPropertiesToDefault()
161
162    Out.Log("Checking that all properties are now in DEFAULT state excepting may be those which 'cann't be default'")
163    aStates = oObj.getPropertyStates(aPropNames)
164    for i = 0 to ubound(aStates)
165        if aStates(i) &lt;&gt; com.sun.star.beans.PropertyState.DEFAULT_VALUE then
166            Out.Log("The property " + aPropNames(i) + " didn't change its state to default ...")
167            if (aProps(i).Attributes AND com.sun.star.beans.PropertyAttribute.MAYBEDEFAULT) > 0 then
168                Out.Log("   ... and it has MAYBEDEFAULT attribute - FAILED")
169                bOK = false
170            else
171                Out.Log("   ... but it has no MAYBEDEFAULT attribute - OK")
172            end if
173        end if
174    next i
175
176    Test.MethodTested("setAllPropertiesToDefault()", bOK)
177
178Exit Sub
179ErrHndl:
180    Test.Exception()
181    bOK = false
182    resume next
183End Sub
184</script:module>
185