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="container_XContainer" script:language="StarBasic">
4cdf0e10cSrcweir
5cdf0e10cSrcweir
6cdf0e10cSrcweir'*************************************************************************
7cdf0e10cSrcweir'
8*eebed415SAndrew Rist'  Licensed to the Apache Software Foundation (ASF) under one
9*eebed415SAndrew Rist'  or more contributor license agreements.  See the NOTICE file
10*eebed415SAndrew Rist'  distributed with this work for additional information
11*eebed415SAndrew Rist'  regarding copyright ownership.  The ASF licenses this file
12*eebed415SAndrew Rist'  to you under the Apache License, Version 2.0 (the
13*eebed415SAndrew Rist'  "License"); you may not use this file except in compliance
14*eebed415SAndrew Rist'  with the License.  You may obtain a copy of the License at
15*eebed415SAndrew Rist'
16*eebed415SAndrew Rist'    http://www.apache.org/licenses/LICENSE-2.0
17*eebed415SAndrew Rist'
18*eebed415SAndrew Rist'  Unless required by applicable law or agreed to in writing,
19*eebed415SAndrew Rist'  software distributed under the License is distributed on an
20*eebed415SAndrew Rist'  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21*eebed415SAndrew Rist'  KIND, either express or implied.  See the License for the
22*eebed415SAndrew Rist'  specific language governing permissions and limitations
23*eebed415SAndrew Rist'  under the License.
24cdf0e10cSrcweir'
25cdf0e10cSrcweir'*************************************************************************
26cdf0e10cSrcweir
27cdf0e10cSrcweir
28cdf0e10cSrcweir
29*eebed415SAndrew Rist
30*eebed415SAndrew Rist
31cdf0e10cSrcweir'*************************************************************************
32cdf0e10cSrcweir' This Interface/Service test depends on the following GLOBAL variables,
33cdf0e10cSrcweir' which must be specified in the object creation:
34cdf0e10cSrcweir
35cdf0e10cSrcweir'      Global oElementToInsert As Object
36cdf0e10cSrcweir'      Global oContainer As Object in case if the component tested does
37cdf0e10cSrcweir'                                  not support XNameContainer
38cdf0e10cSrcweir
39cdf0e10cSrcweir'*************************************************************************
40cdf0e10cSrcweir
41cdf0e10cSrcweirDim ElIns1 As Integer
42cdf0e10cSrcweirDim ElIns2 As Integer
43cdf0e10cSrcweirDim ElRem1 As Integer
44cdf0e10cSrcweirDim ElRem2 As Integer
45cdf0e10cSrcweirDim ElRep1 As Integer
46cdf0e10cSrcweirDim ElRep2 As Integer
47cdf0e10cSrcweir
48cdf0e10cSrcweir
49cdf0e10cSrcweirSub RunTest()
50cdf0e10cSrcweir
51cdf0e10cSrcweir'*************************************************************************
52cdf0e10cSrcweir' INTERFACE:
53cdf0e10cSrcweir' com.sun.star.container.XContainer
54cdf0e10cSrcweir'*************************************************************************
55cdf0e10cSrcweirOn Error Goto ErrHndl
56cdf0e10cSrcweir    Dim bOK As Boolean
57cdf0e10cSrcweir    Dim oListener1 as Object
58cdf0e10cSrcweir    Dim oListener2 as Object
59cdf0e10cSrcweir
60cdf0e10cSrcweir    oListener1 = createUNOListener("CB1_", "com.sun.star.container.XContainerListener")
61cdf0e10cSrcweir    oListener2 = createUNOListener("CB2_", "com.sun.star.container.XContainerListener")
62cdf0e10cSrcweir    bOK = NOT isNULL(oListener1) AND NOT isNULL(oListener2)
63cdf0e10cSrcweir    Out.Log("Listeners creation : " + bOK)
64cdf0e10cSrcweir
65cdf0e10cSrcweir    Test.StartMethod("addContainerListener()")
66cdf0e10cSrcweir    bOK = true
67cdf0e10cSrcweir    oObj.addContainerListener(oListener1)
68cdf0e10cSrcweir    Out.Log("Listener1 was added")
69cdf0e10cSrcweir    oObj.addContainerListener(oListener2)
70cdf0e10cSrcweir    Out.Log("Listener2 was added")
71cdf0e10cSrcweir
72cdf0e10cSrcweir    insertElement()
73cdf0e10cSrcweir
74cdf0e10cSrcweir    Dim bInsOK As Boolean
75cdf0e10cSrcweir    bInsOK = ElIns1 >= 1 AND ElIns2 >= 1
76cdf0e10cSrcweir    Out.Log("... " + bInsOK)
77cdf0e10cSrcweir    bOK = bOK AND bInsOK
78cdf0e10cSrcweir
79cdf0e10cSrcweir    removeElement()
80cdf0e10cSrcweir
81cdf0e10cSrcweir    bRemOK = ElRem1 >= 1 AND ElRem2 >= 1
82cdf0e10cSrcweir    Out.Log("... " + bRemOK)
83cdf0e10cSrcweir    bOK = bOK AND bRemOK
84cdf0e10cSrcweir
85cdf0e10cSrcweir    Dim bNothingToReplace as Boolean
86cdf0e10cSrcweir    bNothingToReplace = replaceElement()
87cdf0e10cSrcweir
88cdf0e10cSrcweir    bRepOK = (ElRep1 >= 1 AND ElRep2 >= 1) OR bNothingToReplace
89cdf0e10cSrcweir    Out.Log("... " + bRepOK)
90cdf0e10cSrcweir    bOK = bOK AND bRepOK
91cdf0e10cSrcweir
92cdf0e10cSrcweir    Test.MethodTested("addContainerListener()", bOK)
93cdf0e10cSrcweir
94cdf0e10cSrcweir
95cdf0e10cSrcweir    Test.StartMethod("removeContainerListener()")
96cdf0e10cSrcweir    bOK = true
97cdf0e10cSrcweir    oObj.removeContainerListener(oListener1)
98cdf0e10cSrcweir    Out.Log("Listener1 was removed")
99cdf0e10cSrcweir
100cdf0e10cSrcweir    insertElement()
101cdf0e10cSrcweir
102cdf0e10cSrcweir    bInsOK = ElIns1 = 0 AND ElIns2 >= 1
103cdf0e10cSrcweir    Out.Log("... " + bInsOK)
104cdf0e10cSrcweir    bOK = bOK AND bInsOK
105cdf0e10cSrcweir
106cdf0e10cSrcweir    removeElement()
107cdf0e10cSrcweir
108cdf0e10cSrcweir    bRemOK = ElRem1 = 0 AND ElRem2 >= 1
109cdf0e10cSrcweir    Out.Log("... " + bRemOK)
110cdf0e10cSrcweir    bOK = bOK AND bRemOK
111cdf0e10cSrcweir
112cdf0e10cSrcweir    bNothingToReplace = replaceElement()
113cdf0e10cSrcweir
114cdf0e10cSrcweir    bRepOK = (ElRep1 = 0 AND ElRep2 >= 1) or bNothingToReplace
115cdf0e10cSrcweir    Out.Log("... " + bRepOK)
116cdf0e10cSrcweir    bOK = bOK AND bRepOK
117cdf0e10cSrcweir    Test.MethodTested("removeContainerListener()", bOK)
118cdf0e10cSrcweir
119cdf0e10cSrcweir    oObj.removeContainerListener(oListener2)
120cdf0e10cSrcweir    Out.Log("Listener2 was removed")
121cdf0e10cSrcweir
122cdf0e10cSrcweirExit Sub
123cdf0e10cSrcweirErrHndl:
124cdf0e10cSrcweir    Test.Exception()
125cdf0e10cSrcweir    bOK = false
126cdf0e10cSrcweir    resume next
127cdf0e10cSrcweirEnd Sub
128cdf0e10cSrcweir
129cdf0e10cSrcweirSub insertElement()
130cdf0e10cSrcweir    Out.Log("Inserting element ... ")
131cdf0e10cSrcweir    ResetCounters()
132cdf0e10cSrcweir    if hasUnoInterfaces(oObj, "com.sun.star.container.XNameContainer") then
133cdf0e10cSrcweir        oObj.InsertByName(cIfcShortName, oElementToInsert)
134cdf0e10cSrcweir    elseif hasUnoInterfaces(oContainer, "com.sun.star.container.XNameContainer") then
135cdf0e10cSrcweir        oContainer.InsertByName(cIfcShortName, oElementToInsert)
136cdf0e10cSrcweir    elseif hasUnoInterfaces(oContainer, "com.sun.star.awt.XControlContainer") then
137cdf0e10cSrcweir        oContainer.addControl("NewControl", oElementToInsert)
138cdf0e10cSrcweir    else
139cdf0e10cSrcweir        Out.LOG("There is nothig to trigger the Listener!")
140cdf0e10cSrcweir    end if
141cdf0e10cSrcweirend Sub
142cdf0e10cSrcweir
143cdf0e10cSrcweir
144cdf0e10cSrcweirSub removeElement()
145cdf0e10cSrcweir    Out.Log("Removing element ... ")
146cdf0e10cSrcweir    ResetCounters()
147cdf0e10cSrcweir    if hasUnoInterfaces(oObj, "com.sun.star.container.XNameContainer") then
148cdf0e10cSrcweir        oObj.RemoveByName(cIfcShortName)
149cdf0e10cSrcweir    elseif hasUnoInterfaces(oContainer, "com.sun.star.container.XNameContainer") then
150cdf0e10cSrcweir        oContainer.RemoveByName(cIfcShortName)
151cdf0e10cSrcweir    elseif hasUnoInterfaces(oContainer, "com.sun.star.awt.XControlContainer") then
152cdf0e10cSrcweir        oContainer.removeControl(oElementToInsert)
153cdf0e10cSrcweir    end if
154cdf0e10cSrcweirend Sub
155cdf0e10cSrcweir
156cdf0e10cSrcweirFunction replaceElement() as Boolean
157cdf0e10cSrcweir    Out.Log("Replacing element ... ")
158cdf0e10cSrcweir    ResetCounters()
159cdf0e10cSrcweir    Dim bNothingToReplace as Boolean
160cdf0e10cSrcweir    bNothingToReplace = FALSE
161cdf0e10cSrcweir    Dim old As Variant
162cdf0e10cSrcweir    if hasUnoInterfaces(oObj, "com.sun.star.container.XIndexReplace") then
163cdf0e10cSrcweir        old = oObj.getByIndex(0)
164cdf0e10cSrcweir        oObj.ReplaceByIndex(0, oElementToInsert)
165cdf0e10cSrcweir        oObj.ReplaceByIndex(0, old)
166cdf0e10cSrcweir    elseif hasUnoInterfaces(oContainer, "com.sun.star.container.XIndexReplace") then
167cdf0e10cSrcweir        old = oContainer.getByIndex(0)
168cdf0e10cSrcweir        oContainer.ReplaceByIndex(0, oElementToInsert)
169cdf0e10cSrcweir        oContainer.ReplaceByIndex(0, old)
170cdf0e10cSrcweir    elseif (hasUnoInterfaces(oContainer, "com.sun.star.container.XNameAccess") and _
171cdf0e10cSrcweir            hasUnoInterfaces(oContainer, "com.sun.star.container.XNameReplace")) then
172cdf0e10cSrcweir        Dim cNames() as String
173cdf0e10cSrcweir        cNames = oObj.getElementNames()
174cdf0e10cSrcweir        old = oContainer.getByName(cNames(0))
175cdf0e10cSrcweir        oContainer.ReplaceByName(cNames(0), oElementToInsert)
176cdf0e10cSrcweir        oContainer.ReplaceByName(cNames(0), old)
177cdf0e10cSrcweir    elseif hasUnoInterfaces(oContainer, "com.sun.star.awt.XControlContainer") then
178cdf0e10cSrcweir        bNothingToReplace = TRUE
179cdf0e10cSrcweir    else
180cdf0e10cSrcweir        Out.LOG("There is nothig to trigger the Listener!")
181cdf0e10cSrcweir    end if
182cdf0e10cSrcweir    replaceElement() = bNothingToReplace
183cdf0e10cSrcweirend Function
184cdf0e10cSrcweir
185cdf0e10cSrcweirSub CB1_elementInserted(ev As Object)
186cdf0e10cSrcweir    Out.Log("CB1 called: element was inserted")
187cdf0e10cSrcweir    ElIns1 = ElIns1 + 1
188cdf0e10cSrcweirEnd Sub
189cdf0e10cSrcweir
190cdf0e10cSrcweirSub CB1_elementRemoved(ev As Object)
191cdf0e10cSrcweir    Out.Log("CB1 called: element was removed")
192cdf0e10cSrcweir    ElRem1 = ElRem1 + 1
193cdf0e10cSrcweirEnd Sub
194cdf0e10cSrcweir
195cdf0e10cSrcweirSub CB1_elementReplaced(ev As Object)
196cdf0e10cSrcweir    Out.Log("CB1 called: element was replaced")
197cdf0e10cSrcweir    ElRep1 = ElRep1 + 1
198cdf0e10cSrcweirEnd Sub
199cdf0e10cSrcweir
200cdf0e10cSrcweirSub CB2_elementInserted(ev As Object)
201cdf0e10cSrcweir    Out.Log("CB2 called: element was inserted")
202cdf0e10cSrcweir    ElIns2 = ElIns2 + 1
203cdf0e10cSrcweirEnd Sub
204cdf0e10cSrcweir
205cdf0e10cSrcweirSub CB2_elementRemoved(ev As Object)
206cdf0e10cSrcweir    Out.Log("CB2 called: element was removed")
207cdf0e10cSrcweir    ElRem2 = ElRem2 + 1
208cdf0e10cSrcweirEnd Sub
209cdf0e10cSrcweir
210cdf0e10cSrcweirSub CB2_elementReplaced(ev As Object)
211cdf0e10cSrcweir    Out.Log("CB2 called: element was replaced")
212cdf0e10cSrcweir    ElRep2 = ElRep2 + 1
213cdf0e10cSrcweirEnd Sub
214cdf0e10cSrcweir
215cdf0e10cSrcweirSub ResetCounters()
216cdf0e10cSrcweir    ElIns1 = 0
217cdf0e10cSrcweir    ElIns2 = 0
218cdf0e10cSrcweir    ElRem1 = 0
219cdf0e10cSrcweir    ElRem2 = 0
220cdf0e10cSrcweir    ElRep1 = 0
221cdf0e10cSrcweir    ElRep2 = 0
222cdf0e10cSrcweirEnd Sub
223cdf0e10cSrcweir</script:module>
224