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