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="container_XContainer" 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'*************************************************************************
32' This Interface/Service test depends on the following GLOBAL variables,
33' which must be specified in the object creation:
34
35'      Global oElementToInsert As Object
36'      Global oContainer As Object in case if the component tested does
37'                                  not support XNameContainer
38
39'*************************************************************************
40
41Dim ElIns1 As Integer
42Dim ElIns2 As Integer
43Dim ElRem1 As Integer
44Dim ElRem2 As Integer
45Dim ElRep1 As Integer
46Dim ElRep2 As Integer
47
48
49Sub RunTest()
50
51'*************************************************************************
52' INTERFACE:
53' com.sun.star.container.XContainer
54'*************************************************************************
55On Error Goto ErrHndl
56    Dim bOK As Boolean
57    Dim oListener1 as Object
58    Dim oListener2 as Object
59
60    oListener1 = createUNOListener("CB1_", "com.sun.star.container.XContainerListener")
61    oListener2 = createUNOListener("CB2_", "com.sun.star.container.XContainerListener")
62    bOK = NOT isNULL(oListener1) AND NOT isNULL(oListener2)
63    Out.Log("Listeners creation : " + bOK)
64
65    Test.StartMethod("addContainerListener()")
66    bOK = true
67    oObj.addContainerListener(oListener1)
68    Out.Log("Listener1 was added")
69    oObj.addContainerListener(oListener2)
70    Out.Log("Listener2 was added")
71
72    insertElement()
73
74    Dim bInsOK As Boolean
75    bInsOK = ElIns1 >= 1 AND ElIns2 >= 1
76    Out.Log("... " + bInsOK)
77    bOK = bOK AND bInsOK
78
79    removeElement()
80
81    bRemOK = ElRem1 >= 1 AND ElRem2 >= 1
82    Out.Log("... " + bRemOK)
83    bOK = bOK AND bRemOK
84
85    Dim bNothingToReplace as Boolean
86    bNothingToReplace = replaceElement()
87
88    bRepOK = (ElRep1 >= 1 AND ElRep2 >= 1) OR bNothingToReplace
89    Out.Log("... " + bRepOK)
90    bOK = bOK AND bRepOK
91
92    Test.MethodTested("addContainerListener()", bOK)
93
94
95    Test.StartMethod("removeContainerListener()")
96    bOK = true
97    oObj.removeContainerListener(oListener1)
98    Out.Log("Listener1 was removed")
99
100    insertElement()
101
102    bInsOK = ElIns1 = 0 AND ElIns2 >= 1
103    Out.Log("... " + bInsOK)
104    bOK = bOK AND bInsOK
105
106    removeElement()
107
108    bRemOK = ElRem1 = 0 AND ElRem2 >= 1
109    Out.Log("... " + bRemOK)
110    bOK = bOK AND bRemOK
111
112    bNothingToReplace = replaceElement()
113
114    bRepOK = (ElRep1 = 0 AND ElRep2 >= 1) or bNothingToReplace
115    Out.Log("... " + bRepOK)
116    bOK = bOK AND bRepOK
117    Test.MethodTested("removeContainerListener()", bOK)
118
119    oObj.removeContainerListener(oListener2)
120    Out.Log("Listener2 was removed")
121
122Exit Sub
123ErrHndl:
124    Test.Exception()
125    bOK = false
126    resume next
127End Sub
128
129Sub insertElement()
130    Out.Log("Inserting element ... ")
131    ResetCounters()
132    if hasUnoInterfaces(oObj, "com.sun.star.container.XNameContainer") then
133        oObj.InsertByName(cIfcShortName, oElementToInsert)
134    elseif hasUnoInterfaces(oContainer, "com.sun.star.container.XNameContainer") then
135        oContainer.InsertByName(cIfcShortName, oElementToInsert)
136    elseif hasUnoInterfaces(oContainer, "com.sun.star.awt.XControlContainer") then
137        oContainer.addControl("NewControl", oElementToInsert)
138    else
139        Out.LOG("There is nothig to trigger the Listener!")
140    end if
141end Sub
142
143
144Sub removeElement()
145    Out.Log("Removing element ... ")
146    ResetCounters()
147    if hasUnoInterfaces(oObj, "com.sun.star.container.XNameContainer") then
148        oObj.RemoveByName(cIfcShortName)
149    elseif hasUnoInterfaces(oContainer, "com.sun.star.container.XNameContainer") then
150        oContainer.RemoveByName(cIfcShortName)
151    elseif hasUnoInterfaces(oContainer, "com.sun.star.awt.XControlContainer") then
152        oContainer.removeControl(oElementToInsert)
153    end if
154end Sub
155
156Function replaceElement() as Boolean
157    Out.Log("Replacing element ... ")
158    ResetCounters()
159    Dim bNothingToReplace as Boolean
160    bNothingToReplace = FALSE
161    Dim old As Variant
162    if hasUnoInterfaces(oObj, "com.sun.star.container.XIndexReplace") then
163        old = oObj.getByIndex(0)
164        oObj.ReplaceByIndex(0, oElementToInsert)
165        oObj.ReplaceByIndex(0, old)
166    elseif hasUnoInterfaces(oContainer, "com.sun.star.container.XIndexReplace") then
167        old = oContainer.getByIndex(0)
168        oContainer.ReplaceByIndex(0, oElementToInsert)
169        oContainer.ReplaceByIndex(0, old)
170    elseif (hasUnoInterfaces(oContainer, "com.sun.star.container.XNameAccess") and _
171            hasUnoInterfaces(oContainer, "com.sun.star.container.XNameReplace")) then
172        Dim cNames() as String
173        cNames = oObj.getElementNames()
174        old = oContainer.getByName(cNames(0))
175        oContainer.ReplaceByName(cNames(0), oElementToInsert)
176        oContainer.ReplaceByName(cNames(0), old)
177    elseif hasUnoInterfaces(oContainer, "com.sun.star.awt.XControlContainer") then
178        bNothingToReplace = TRUE
179    else
180        Out.LOG("There is nothig to trigger the Listener!")
181    end if
182    replaceElement() = bNothingToReplace
183end Function
184
185Sub CB1_elementInserted(ev As Object)
186    Out.Log("CB1 called: element was inserted")
187    ElIns1 = ElIns1 + 1
188End Sub
189
190Sub CB1_elementRemoved(ev As Object)
191    Out.Log("CB1 called: element was removed")
192    ElRem1 = ElRem1 + 1
193End Sub
194
195Sub CB1_elementReplaced(ev As Object)
196    Out.Log("CB1 called: element was replaced")
197    ElRep1 = ElRep1 + 1
198End Sub
199
200Sub CB2_elementInserted(ev As Object)
201    Out.Log("CB2 called: element was inserted")
202    ElIns2 = ElIns2 + 1
203End Sub
204
205Sub CB2_elementRemoved(ev As Object)
206    Out.Log("CB2 called: element was removed")
207    ElRem2 = ElRem2 + 1
208End Sub
209
210Sub CB2_elementReplaced(ev As Object)
211    Out.Log("CB2 called: element was replaced")
212    ElRep2 = ElRep2 + 1
213End Sub
214
215Sub ResetCounters()
216    ElIns1 = 0
217    ElIns2 = 0
218    ElRem1 = 0
219    ElRem2 = 0
220    ElRep1 = 0
221    ElRep2 = 0
222End Sub
223</script:module>
224