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'  Licensed to the Apache Software Foundation (ASF) under one
9'  or more contributor license agreements.  See the NOTICE file
10'  distributed with this work for additional information
11'  regarding copyright ownership.  The ASF licenses this file
12'  to you under the Apache License, Version 2.0 (the
13'  "License"); you may not use this file except in compliance
14'  with the License.  You may obtain a copy of the License at
15'
16'    http://www.apache.org/licenses/LICENSE-2.0
17'
18'  Unless required by applicable law or agreed to in writing,
19'  software distributed under the License is distributed on an
20'  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21'  KIND, either express or implied.  See the License for the
22'  specific language governing permissions and limitations
23'  under the License.
24'
25'*************************************************************************
26
27
28
29
30
31' Be sure that all variables are dimensioned:
32option explicit
33
34
35Function getPropNames(xPropSet As Object) As Variant
36    Dim props As Variant
37    Dim propNames As Variant
38    Dim i As Integer, propCount As Integer
39
40    props = xPropSet.getPropertySetInfo().getProperties()
41	propCount = 0
42    for i = 0 to ubound (props)
43    	if (props(i).Attributes AND com.sun.star.beans.PropertyAttribute.READONLY) = 0 _
44    		then propCount = propCount + 1
45    next i
46
47    propNames = DimArray(propCount - 1)
48    aProps = DimArray(propCount - 1)
49
50    propCount = 0
51    for i = 0 to ubound(props)
52    	if (props(i).Attributes AND com.sun.star.beans.PropertyAttribute.READONLY) = 0 then
53	        propNames(propCount) = props(i).Name
54	        aProps(propCount) = props(i)
55    		propCount = propCount + 1
56    	endif
57    next i
58
59    getPropNames = propNames
60End Function
61
62Dim aProps As Variant
63
64
65Sub RunTest()
66
67'*************************************************************************
68' INTERFACE:
69' com.sun.star.beans.XMultiPropertyStates
70'*************************************************************************
71On Error Goto ErrHndl
72    Dim bOK As Boolean
73
74    Dim aPropNames As Variant
75    Dim aDefaults As Variant
76    Dim aStates As Variant
77
78    if NOT hasUnoInterfaces(oObj, "com.sun.star.beans.XPropertySet") then
79        Out.Log("The compoent doesn't support XPropertySet this test must be reviewed !!!")
80        exit sub
81    end if
82
83    bOK = true
84    aPropNames = getPropNames(oObj)
85
86    if NOT utils.isSorted(aPropNames) then
87        Out.Log("Property names are not sorted : sorting ...")
88        utils.bubbleSort(aPropNames, false, aProps)
89    end if
90
91    Out.Log("Totally " + (ubound(aPropNames) + 1) + " properties encountered.")
92
93    Test.StartMethod("getPropertyDefaults()")
94
95    aDefaults = oObj.getPropertyDefaults(aPropNames)
96    Out.Log("Number of default values: " + (ubound(aDefaults) + 1))
97    bOK = ubound(aDefaults) = ubound(aPropNames)
98
99    Test.MethodTested("getPropertyDefaults()", bOK)
100
101
102    Test.StartMethod("getPropertyStates()")
103    bOK = true
104
105    aStates = oObj.getPropertyStates(aPropNames)
106    Out.Log("Number of states: " + (ubound(aStates) + 1))
107    bOK = ubound(aStates) = ubound(aPropNames)
108
109    Test.MethodTested("getPropertyStates()", bOK)
110
111    Test.StartMethod("setPropertiesToDefault()")
112    bOK = true
113
114    Dim propName As String
115    Dim propIdx As Integer
116    Dim mayBeDef As Boolean
117    Dim i As Integer
118    propName = aPropNames(0)
119    propIdx = 0
120    mayBeDef = false
121
122    ' searching for property which currently don't have default value and preferable has MAYBEDEFAULT attr
123    ' if no such properties are found then the first one is selected
124    for i = 0 to ubound(aPropNames)
125        if NOT mayBeDef AND aStates(i) &lt;&gt; com.sun.star.beans.PropertyState.DEFAULT_VALUE then
126            propName = aPropNames(i)
127            propIdx = i
128            if (aProps(i).Attributes AND com.sun.star.beans.PropertyAttribute.MAYBEDEFAULT) > 0 then
129                Out.Log("Property " + propName + " 'may be default' and doesn't have default value")
130                mayBeDef = true
131            end if
132        end if
133    next i
134    Out.Log("The property " + propName + " selected")
135
136    oObj.setPropertiesToDefault(Array(propName))
137
138    aStates = oObj.getPropertyStates(aPropNames)
139    if aStates(propIdx) &lt;&gt; com.sun.star.beans.PropertyState.DEFAULT_VALUE then
140        Out.Log("The property didn't change its state to default ...")
141        if mayBeDef then
142            Out.Log("   ... and it may be default - FAILED")
143            bOK = false
144        else
145            Out.Log("   ... but it may not be default - OK")
146        end if
147    end if
148
149    Test.MethodTested("setPropertiesToDefault()", bOK)
150
151    Test.StartMethod("setAllPropertiesToDefault()")
152    bOK = true
153
154    oObj.setAllPropertiesToDefault()
155
156    Out.Log("Checking that all properties are now in DEFAULT state excepting may be those which 'cann't be default'")
157    aStates = oObj.getPropertyStates(aPropNames)
158    for i = 0 to ubound(aStates)
159        if aStates(i) &lt;&gt; com.sun.star.beans.PropertyState.DEFAULT_VALUE then
160            Out.Log("The property " + aPropNames(i) + " didn't change its state to default ...")
161            if (aProps(i).Attributes AND com.sun.star.beans.PropertyAttribute.MAYBEDEFAULT) > 0 then
162                Out.Log("   ... and it has MAYBEDEFAULT attribute - FAILED")
163                bOK = false
164            else
165                Out.Log("   ... but it has no MAYBEDEFAULT attribute - OK")
166            end if
167        end if
168    next i
169
170    Test.MethodTested("setAllPropertiesToDefault()", bOK)
171
172Exit Sub
173ErrHndl:
174    Test.Exception()
175    bOK = false
176    resume next
177End Sub
178</script:module>
179