xref: /trunk/main/sw/qa/complex/writer/CheckFlies.java (revision 3309286857f19787ae62bd793a98b5af4edd2ad3)
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
10cdf0e10cSrcweir  *
11*86250549SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
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.
19cdf0e10cSrcweir  *
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 java.math.BigInteger;
34cdf0e10cSrcweir import java.util.Collection;
35cdf0e10cSrcweir import java.util.ArrayList;
36cdf0e10cSrcweir import org.junit.After;
37cdf0e10cSrcweir import org.junit.AfterClass;
38cdf0e10cSrcweir import org.junit.Before;
39cdf0e10cSrcweir import org.junit.BeforeClass;
40cdf0e10cSrcweir import org.junit.Test;
41cdf0e10cSrcweir import org.openoffice.test.OfficeConnection;
42cdf0e10cSrcweir import static org.junit.Assert.*;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir public class CheckFlies {
checkFlies()45cdf0e10cSrcweir     @Test public void checkFlies()
46cdf0e10cSrcweir         throws com.sun.star.uno.Exception
47cdf0e10cSrcweir     {
48cdf0e10cSrcweir         com.sun.star.text.XTextFramesSupplier xTFS = (com.sun.star.text.XTextFramesSupplier)UnoRuntime.queryInterface(
49cdf0e10cSrcweir             com.sun.star.text.XTextFramesSupplier.class,
50cdf0e10cSrcweir             document);
51cdf0e10cSrcweir         checkTextFrames(xTFS);
52cdf0e10cSrcweir         com.sun.star.text.XTextGraphicObjectsSupplier xTGOS = (com.sun.star.text.XTextGraphicObjectsSupplier)UnoRuntime.queryInterface(
53cdf0e10cSrcweir             com.sun.star.text.XTextGraphicObjectsSupplier.class,
54cdf0e10cSrcweir             document);
55cdf0e10cSrcweir         checkGraphicFrames(xTGOS);
56cdf0e10cSrcweir         com.sun.star.text.XTextEmbeddedObjectsSupplier xTEOS = (com.sun.star.text.XTextEmbeddedObjectsSupplier)UnoRuntime.queryInterface(
57cdf0e10cSrcweir             com.sun.star.text.XTextEmbeddedObjectsSupplier.class,
58cdf0e10cSrcweir             document);
59cdf0e10cSrcweir         checkEmbeddedFrames(xTEOS);
60cdf0e10cSrcweir     }
61cdf0e10cSrcweir 
checkEmbeddedFrames(com.sun.star.text.XTextEmbeddedObjectsSupplier xTGOS)62cdf0e10cSrcweir     private void checkEmbeddedFrames(com.sun.star.text.XTextEmbeddedObjectsSupplier xTGOS)
63cdf0e10cSrcweir         throws com.sun.star.uno.Exception
64cdf0e10cSrcweir     {
65cdf0e10cSrcweir         Collection<String> vExpectedEmbeddedFrames = new ArrayList<String>();
66cdf0e10cSrcweir         vExpectedEmbeddedFrames.add("Object1");
67cdf0e10cSrcweir         int nEmbeddedFrames = vExpectedEmbeddedFrames.size();
68cdf0e10cSrcweir         com.sun.star.container.XNameAccess xEmbeddedFrames = xTGOS.getEmbeddedObjects();
69cdf0e10cSrcweir         for(String sFrameName : xEmbeddedFrames.getElementNames())
70cdf0e10cSrcweir         {
71cdf0e10cSrcweir             assertTrue(
72cdf0e10cSrcweir                 "Unexpected frame name",
73cdf0e10cSrcweir                 vExpectedEmbeddedFrames.remove(sFrameName));
74cdf0e10cSrcweir             xEmbeddedFrames.getByName(sFrameName);
75cdf0e10cSrcweir             assertTrue(
76cdf0e10cSrcweir                 "Could not find embedded frame by name.",
77cdf0e10cSrcweir                 xEmbeddedFrames.hasByName(sFrameName));
78cdf0e10cSrcweir         }
79cdf0e10cSrcweir         assertTrue(
80cdf0e10cSrcweir             "Missing expected embedded frames.",
81cdf0e10cSrcweir             vExpectedEmbeddedFrames.isEmpty());
82cdf0e10cSrcweir         try
83cdf0e10cSrcweir         {
84cdf0e10cSrcweir             xEmbeddedFrames.getByName("Nonexisting embedded frame");
85cdf0e10cSrcweir             fail("Got nonexisting embedded frame");
86cdf0e10cSrcweir         }
87cdf0e10cSrcweir         catch(com.sun.star.container.NoSuchElementException e)
88cdf0e10cSrcweir         {}
89cdf0e10cSrcweir         assertFalse(
90cdf0e10cSrcweir             "Has nonexisting embedded frame",
91cdf0e10cSrcweir             xEmbeddedFrames.hasByName("Nonexisting embedded frame"));
92cdf0e10cSrcweir 
93cdf0e10cSrcweir         com.sun.star.container.XIndexAccess xEmbeddedFramesIdx = (com.sun.star.container.XIndexAccess)UnoRuntime.queryInterface(
94cdf0e10cSrcweir             com.sun.star.container.XIndexAccess.class,
95cdf0e10cSrcweir             xEmbeddedFrames);
96cdf0e10cSrcweir         assertEquals(
97cdf0e10cSrcweir             "Unexpected number of embedded frames reported.", nEmbeddedFrames,
98cdf0e10cSrcweir             xEmbeddedFramesIdx.getCount());
99cdf0e10cSrcweir         for(int nCurrentFrameIdx = 0; nCurrentFrameIdx < xEmbeddedFramesIdx.getCount(); nCurrentFrameIdx++)
100cdf0e10cSrcweir         {
101cdf0e10cSrcweir             xEmbeddedFramesIdx.getByIndex(nCurrentFrameIdx);
102cdf0e10cSrcweir         }
103cdf0e10cSrcweir     }
104cdf0e10cSrcweir 
checkGraphicFrames(com.sun.star.text.XTextGraphicObjectsSupplier xTGOS)105cdf0e10cSrcweir     private void checkGraphicFrames(com.sun.star.text.XTextGraphicObjectsSupplier xTGOS)
106cdf0e10cSrcweir         throws com.sun.star.uno.Exception
107cdf0e10cSrcweir     {
108cdf0e10cSrcweir         Collection<String> vExpectedGraphicFrames = new ArrayList<String>();
109cdf0e10cSrcweir         vExpectedGraphicFrames.add("graphics1");
110cdf0e10cSrcweir         int nGraphicFrames = vExpectedGraphicFrames.size();
111cdf0e10cSrcweir         com.sun.star.container.XNameAccess xGraphicFrames = xTGOS.getGraphicObjects();
112cdf0e10cSrcweir         for(String sFrameName : xGraphicFrames.getElementNames())
113cdf0e10cSrcweir         {
114cdf0e10cSrcweir             assertTrue(
115cdf0e10cSrcweir                 "Unexpected frame name",
116cdf0e10cSrcweir                 vExpectedGraphicFrames.remove(sFrameName));
117cdf0e10cSrcweir             xGraphicFrames.getByName(sFrameName);
118cdf0e10cSrcweir             assertTrue(
119cdf0e10cSrcweir                 "Could not find graphics frame by name.",
120cdf0e10cSrcweir                 xGraphicFrames.hasByName(sFrameName));
121cdf0e10cSrcweir         }
122cdf0e10cSrcweir         assertTrue(
123cdf0e10cSrcweir             "Missing expected graphics frames.",
124cdf0e10cSrcweir             vExpectedGraphicFrames.isEmpty());
125cdf0e10cSrcweir         try
126cdf0e10cSrcweir         {
127cdf0e10cSrcweir             xGraphicFrames.getByName("Nonexisting graphics frame");
128cdf0e10cSrcweir             fail("Got nonexisting graphics frame");
129cdf0e10cSrcweir         }
130cdf0e10cSrcweir         catch(com.sun.star.container.NoSuchElementException e)
131cdf0e10cSrcweir         {}
132cdf0e10cSrcweir         assertFalse(
133cdf0e10cSrcweir             "Has nonexisting graphics frame",
134cdf0e10cSrcweir             xGraphicFrames.hasByName("Nonexisting graphics frame"));
135cdf0e10cSrcweir 
136cdf0e10cSrcweir         com.sun.star.container.XIndexAccess xGraphicFramesIdx = (com.sun.star.container.XIndexAccess)UnoRuntime.queryInterface(
137cdf0e10cSrcweir             com.sun.star.container.XIndexAccess.class,
138cdf0e10cSrcweir             xGraphicFrames);
139cdf0e10cSrcweir         assertEquals(
140cdf0e10cSrcweir             "Unexpected number of graphics frames reported.", nGraphicFrames,
141cdf0e10cSrcweir             xGraphicFramesIdx.getCount());
142cdf0e10cSrcweir         for(int nCurrentFrameIdx = 0; nCurrentFrameIdx < xGraphicFramesIdx.getCount(); nCurrentFrameIdx++)
143cdf0e10cSrcweir         {
144cdf0e10cSrcweir             xGraphicFramesIdx.getByIndex(nCurrentFrameIdx);
145cdf0e10cSrcweir         }
146cdf0e10cSrcweir     }
147cdf0e10cSrcweir 
checkTextFrames(com.sun.star.text.XTextFramesSupplier xTFS)148cdf0e10cSrcweir     private void checkTextFrames(com.sun.star.text.XTextFramesSupplier xTFS)
149cdf0e10cSrcweir         throws com.sun.star.uno.Exception
150cdf0e10cSrcweir     {
151cdf0e10cSrcweir         Collection<String> vExpectedTextFrames = new ArrayList<String>();
152cdf0e10cSrcweir         vExpectedTextFrames.add("Frame1");
153cdf0e10cSrcweir         vExpectedTextFrames.add("Frame2");
154cdf0e10cSrcweir 
155cdf0e10cSrcweir         int nTextFrames = vExpectedTextFrames.size();
156cdf0e10cSrcweir         com.sun.star.container.XNameAccess xTextFrames = xTFS.getTextFrames();
157cdf0e10cSrcweir         for(String sFrameName : xTextFrames.getElementNames())
158cdf0e10cSrcweir         {
159cdf0e10cSrcweir             assertTrue(
160cdf0e10cSrcweir                 "Unexpected frame name",
161cdf0e10cSrcweir                 vExpectedTextFrames.remove(sFrameName));
162cdf0e10cSrcweir             xTextFrames.getByName(sFrameName);
163cdf0e10cSrcweir             assertTrue(
164cdf0e10cSrcweir                 "Could not find text frame by name.",
165cdf0e10cSrcweir                 xTextFrames.hasByName(sFrameName));
166cdf0e10cSrcweir         }
167cdf0e10cSrcweir         assertTrue(
168cdf0e10cSrcweir             "Missing expected text frames.", vExpectedTextFrames.isEmpty());
169cdf0e10cSrcweir         try
170cdf0e10cSrcweir         {
171cdf0e10cSrcweir             xTextFrames.getByName("Nonexisting Textframe");
172cdf0e10cSrcweir             fail("Got nonexisting text frame.");
173cdf0e10cSrcweir         }
174cdf0e10cSrcweir         catch(com.sun.star.container.NoSuchElementException e)
175cdf0e10cSrcweir         {}
176cdf0e10cSrcweir         assertFalse(
177cdf0e10cSrcweir             "Has nonexisting text frame.",
178cdf0e10cSrcweir             xTextFrames.hasByName("Nonexisting text frame"));
179cdf0e10cSrcweir 
180cdf0e10cSrcweir         com.sun.star.container.XIndexAccess xTextFramesIdx = (com.sun.star.container.XIndexAccess)UnoRuntime.queryInterface(
181cdf0e10cSrcweir             com.sun.star.container.XIndexAccess.class,
182cdf0e10cSrcweir             xTextFrames);
183cdf0e10cSrcweir         assertEquals(
184cdf0e10cSrcweir             "Unexpected number of text frames reported.", nTextFrames,
185cdf0e10cSrcweir             xTextFramesIdx.getCount());
186cdf0e10cSrcweir         for(int nCurrentFrameIdx = 0; nCurrentFrameIdx < xTextFramesIdx.getCount(); nCurrentFrameIdx++)
187cdf0e10cSrcweir         {
188cdf0e10cSrcweir             xTextFramesIdx.getByIndex(nCurrentFrameIdx);
189cdf0e10cSrcweir         }
190cdf0e10cSrcweir     }
191cdf0e10cSrcweir 
setUpDocument()192cdf0e10cSrcweir     @Before public void setUpDocument() throws com.sun.star.uno.Exception {
193cdf0e10cSrcweir         document = util.WriterTools.loadTextDoc(
194cdf0e10cSrcweir             UnoRuntime.queryInterface(
195cdf0e10cSrcweir                 XMultiServiceFactory.class,
196cdf0e10cSrcweir                 connection.getComponentContext().getServiceManager()),
197cdf0e10cSrcweir             TestDocument.getUrl("CheckFlies.odt"));
198cdf0e10cSrcweir     }
199cdf0e10cSrcweir 
tearDownDocument()200cdf0e10cSrcweir     @After public void tearDownDocument() {
201cdf0e10cSrcweir         util.DesktopTools.closeDoc(document);
202cdf0e10cSrcweir     }
203cdf0e10cSrcweir 
204cdf0e10cSrcweir     private XTextDocument document = null;
205cdf0e10cSrcweir 
setUpConnection()206cdf0e10cSrcweir     @BeforeClass public static void setUpConnection() throws Exception {
207cdf0e10cSrcweir         connection.setUp();
208cdf0e10cSrcweir     }
209cdf0e10cSrcweir 
tearDownConnection()210cdf0e10cSrcweir     @AfterClass public static void tearDownConnection()
211cdf0e10cSrcweir         throws InterruptedException, com.sun.star.uno.Exception
212cdf0e10cSrcweir     {
213cdf0e10cSrcweir         connection.tearDown();
214cdf0e10cSrcweir     }
215cdf0e10cSrcweir 
216cdf0e10cSrcweir     private static final OfficeConnection connection = new OfficeConnection();
217cdf0e10cSrcweir }
218