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