xref: /trunk/main/sw/qa/complex/writer/CheckFlies.java (revision f04976d9)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package complex.writer;
25 
26 import com.sun.star.beans.PropertyValue;
27 import com.sun.star.container.XNamed;
28 import com.sun.star.container.XNameAccess;
29 import com.sun.star.container.XIndexAccess;
30 import com.sun.star.lang.XMultiServiceFactory;
31 import com.sun.star.text.XTextDocument;
32 import com.sun.star.uno.UnoRuntime;
33 import java.math.BigInteger;
34 import java.util.Collection;
35 import java.util.ArrayList;
36 import org.junit.After;
37 import org.junit.AfterClass;
38 import org.junit.Before;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 import org.openoffice.test.OfficeConnection;
42 import static org.junit.Assert.*;
43 
44 public class CheckFlies {
checkFlies()45     @Test public void checkFlies()
46         throws com.sun.star.uno.Exception
47     {
48         com.sun.star.text.XTextFramesSupplier xTFS = (com.sun.star.text.XTextFramesSupplier)UnoRuntime.queryInterface(
49             com.sun.star.text.XTextFramesSupplier.class,
50             document);
51         checkTextFrames(xTFS);
52         com.sun.star.text.XTextGraphicObjectsSupplier xTGOS = (com.sun.star.text.XTextGraphicObjectsSupplier)UnoRuntime.queryInterface(
53             com.sun.star.text.XTextGraphicObjectsSupplier.class,
54             document);
55         checkGraphicFrames(xTGOS);
56         com.sun.star.text.XTextEmbeddedObjectsSupplier xTEOS = (com.sun.star.text.XTextEmbeddedObjectsSupplier)UnoRuntime.queryInterface(
57             com.sun.star.text.XTextEmbeddedObjectsSupplier.class,
58             document);
59         checkEmbeddedFrames(xTEOS);
60     }
61 
checkEmbeddedFrames(com.sun.star.text.XTextEmbeddedObjectsSupplier xTGOS)62     private void checkEmbeddedFrames(com.sun.star.text.XTextEmbeddedObjectsSupplier xTGOS)
63         throws com.sun.star.uno.Exception
64     {
65         Collection<String> vExpectedEmbeddedFrames = new ArrayList<String>();
66         vExpectedEmbeddedFrames.add("Object1");
67         int nEmbeddedFrames = vExpectedEmbeddedFrames.size();
68         com.sun.star.container.XNameAccess xEmbeddedFrames = xTGOS.getEmbeddedObjects();
69         for(String sFrameName : xEmbeddedFrames.getElementNames())
70         {
71             assertTrue(
72                 "Unexpected frame name",
73                 vExpectedEmbeddedFrames.remove(sFrameName));
74             xEmbeddedFrames.getByName(sFrameName);
75             assertTrue(
76                 "Could not find embedded frame by name.",
77                 xEmbeddedFrames.hasByName(sFrameName));
78         }
79         assertTrue(
80             "Missing expected embedded frames.",
81             vExpectedEmbeddedFrames.isEmpty());
82         try
83         {
84             xEmbeddedFrames.getByName("Nonexisting embedded frame");
85             fail("Got nonexisting embedded frame");
86         }
87         catch(com.sun.star.container.NoSuchElementException e)
88         {}
89         assertFalse(
90             "Has nonexisting embedded frame",
91             xEmbeddedFrames.hasByName("Nonexisting embedded frame"));
92 
93         com.sun.star.container.XIndexAccess xEmbeddedFramesIdx = (com.sun.star.container.XIndexAccess)UnoRuntime.queryInterface(
94             com.sun.star.container.XIndexAccess.class,
95             xEmbeddedFrames);
96         assertEquals(
97             "Unexpected number of embedded frames reported.", nEmbeddedFrames,
98             xEmbeddedFramesIdx.getCount());
99         for(int nCurrentFrameIdx = 0; nCurrentFrameIdx < xEmbeddedFramesIdx.getCount(); nCurrentFrameIdx++)
100         {
101             xEmbeddedFramesIdx.getByIndex(nCurrentFrameIdx);
102         }
103     }
104 
checkGraphicFrames(com.sun.star.text.XTextGraphicObjectsSupplier xTGOS)105     private void checkGraphicFrames(com.sun.star.text.XTextGraphicObjectsSupplier xTGOS)
106         throws com.sun.star.uno.Exception
107     {
108         Collection<String> vExpectedGraphicFrames = new ArrayList<String>();
109         vExpectedGraphicFrames.add("graphics1");
110         int nGraphicFrames = vExpectedGraphicFrames.size();
111         com.sun.star.container.XNameAccess xGraphicFrames = xTGOS.getGraphicObjects();
112         for(String sFrameName : xGraphicFrames.getElementNames())
113         {
114             assertTrue(
115                 "Unexpected frame name",
116                 vExpectedGraphicFrames.remove(sFrameName));
117             xGraphicFrames.getByName(sFrameName);
118             assertTrue(
119                 "Could not find graphics frame by name.",
120                 xGraphicFrames.hasByName(sFrameName));
121         }
122         assertTrue(
123             "Missing expected graphics frames.",
124             vExpectedGraphicFrames.isEmpty());
125         try
126         {
127             xGraphicFrames.getByName("Nonexisting graphics frame");
128             fail("Got nonexisting graphics frame");
129         }
130         catch(com.sun.star.container.NoSuchElementException e)
131         {}
132         assertFalse(
133             "Has nonexisting graphics frame",
134             xGraphicFrames.hasByName("Nonexisting graphics frame"));
135 
136         com.sun.star.container.XIndexAccess xGraphicFramesIdx = (com.sun.star.container.XIndexAccess)UnoRuntime.queryInterface(
137             com.sun.star.container.XIndexAccess.class,
138             xGraphicFrames);
139         assertEquals(
140             "Unexpected number of graphics frames reported.", nGraphicFrames,
141             xGraphicFramesIdx.getCount());
142         for(int nCurrentFrameIdx = 0; nCurrentFrameIdx < xGraphicFramesIdx.getCount(); nCurrentFrameIdx++)
143         {
144             xGraphicFramesIdx.getByIndex(nCurrentFrameIdx);
145         }
146     }
147 
checkTextFrames(com.sun.star.text.XTextFramesSupplier xTFS)148     private void checkTextFrames(com.sun.star.text.XTextFramesSupplier xTFS)
149         throws com.sun.star.uno.Exception
150     {
151         Collection<String> vExpectedTextFrames = new ArrayList<String>();
152         vExpectedTextFrames.add("Frame1");
153         vExpectedTextFrames.add("Frame2");
154 
155         int nTextFrames = vExpectedTextFrames.size();
156         com.sun.star.container.XNameAccess xTextFrames = xTFS.getTextFrames();
157         for(String sFrameName : xTextFrames.getElementNames())
158         {
159             assertTrue(
160                 "Unexpected frame name",
161                 vExpectedTextFrames.remove(sFrameName));
162             xTextFrames.getByName(sFrameName);
163             assertTrue(
164                 "Could not find text frame by name.",
165                 xTextFrames.hasByName(sFrameName));
166         }
167         assertTrue(
168             "Missing expected text frames.", vExpectedTextFrames.isEmpty());
169         try
170         {
171             xTextFrames.getByName("Nonexisting Textframe");
172             fail("Got nonexisting text frame.");
173         }
174         catch(com.sun.star.container.NoSuchElementException e)
175         {}
176         assertFalse(
177             "Has nonexisting text frame.",
178             xTextFrames.hasByName("Nonexisting text frame"));
179 
180         com.sun.star.container.XIndexAccess xTextFramesIdx = (com.sun.star.container.XIndexAccess)UnoRuntime.queryInterface(
181             com.sun.star.container.XIndexAccess.class,
182             xTextFrames);
183         assertEquals(
184             "Unexpected number of text frames reported.", nTextFrames,
185             xTextFramesIdx.getCount());
186         for(int nCurrentFrameIdx = 0; nCurrentFrameIdx < xTextFramesIdx.getCount(); nCurrentFrameIdx++)
187         {
188             xTextFramesIdx.getByIndex(nCurrentFrameIdx);
189         }
190     }
191 
setUpDocument()192     @Before public void setUpDocument() throws com.sun.star.uno.Exception {
193         document = util.WriterTools.loadTextDoc(
194             UnoRuntime.queryInterface(
195                 XMultiServiceFactory.class,
196                 connection.getComponentContext().getServiceManager()),
197             TestDocument.getUrl("CheckFlies.odt"));
198     }
199 
tearDownDocument()200     @After public void tearDownDocument() {
201         util.DesktopTools.closeDoc(document);
202     }
203 
204     private XTextDocument document = null;
205 
setUpConnection()206     @BeforeClass public static void setUpConnection() throws Exception {
207         connection.setUp();
208     }
209 
tearDownConnection()210     @AfterClass public static void tearDownConnection()
211         throws InterruptedException, com.sun.star.uno.Exception
212     {
213         connection.tearDown();
214     }
215 
216     private static final OfficeConnection connection = new OfficeConnection();
217 }
218