xref: /AOO41X/main/sw/qa/complex/writer/CheckFlies.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir package complex.writer;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
31*cdf0e10cSrcweir import com.sun.star.container.XNamed;
32*cdf0e10cSrcweir import com.sun.star.container.XNameAccess;
33*cdf0e10cSrcweir import com.sun.star.container.XIndexAccess;
34*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
35*cdf0e10cSrcweir import com.sun.star.text.XTextDocument;
36*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
37*cdf0e10cSrcweir import complexlib.ComplexTestCase;
38*cdf0e10cSrcweir import java.math.BigInteger;
39*cdf0e10cSrcweir import java.util.Collection;
40*cdf0e10cSrcweir import java.util.ArrayList;
41*cdf0e10cSrcweir import org.junit.After;
42*cdf0e10cSrcweir import org.junit.AfterClass;
43*cdf0e10cSrcweir import org.junit.Before;
44*cdf0e10cSrcweir import org.junit.BeforeClass;
45*cdf0e10cSrcweir import org.junit.Test;
46*cdf0e10cSrcweir import org.openoffice.test.OfficeConnection;
47*cdf0e10cSrcweir import static org.junit.Assert.*;
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir public class CheckFlies {
50*cdf0e10cSrcweir     @Test public void checkFlies()
51*cdf0e10cSrcweir         throws com.sun.star.uno.Exception
52*cdf0e10cSrcweir     {
53*cdf0e10cSrcweir         com.sun.star.text.XTextFramesSupplier xTFS = (com.sun.star.text.XTextFramesSupplier)UnoRuntime.queryInterface(
54*cdf0e10cSrcweir             com.sun.star.text.XTextFramesSupplier.class,
55*cdf0e10cSrcweir             document);
56*cdf0e10cSrcweir         checkTextFrames(xTFS);
57*cdf0e10cSrcweir         com.sun.star.text.XTextGraphicObjectsSupplier xTGOS = (com.sun.star.text.XTextGraphicObjectsSupplier)UnoRuntime.queryInterface(
58*cdf0e10cSrcweir             com.sun.star.text.XTextGraphicObjectsSupplier.class,
59*cdf0e10cSrcweir             document);
60*cdf0e10cSrcweir         checkGraphicFrames(xTGOS);
61*cdf0e10cSrcweir         com.sun.star.text.XTextEmbeddedObjectsSupplier xTEOS = (com.sun.star.text.XTextEmbeddedObjectsSupplier)UnoRuntime.queryInterface(
62*cdf0e10cSrcweir             com.sun.star.text.XTextEmbeddedObjectsSupplier.class,
63*cdf0e10cSrcweir             document);
64*cdf0e10cSrcweir         checkEmbeddedFrames(xTEOS);
65*cdf0e10cSrcweir     }
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir     private void checkEmbeddedFrames(com.sun.star.text.XTextEmbeddedObjectsSupplier xTGOS)
68*cdf0e10cSrcweir         throws com.sun.star.uno.Exception
69*cdf0e10cSrcweir     {
70*cdf0e10cSrcweir         Collection<String> vExpectedEmbeddedFrames = new ArrayList<String>();
71*cdf0e10cSrcweir         vExpectedEmbeddedFrames.add("Object1");
72*cdf0e10cSrcweir         int nEmbeddedFrames = vExpectedEmbeddedFrames.size();
73*cdf0e10cSrcweir         com.sun.star.container.XNameAccess xEmbeddedFrames = xTGOS.getEmbeddedObjects();
74*cdf0e10cSrcweir         for(String sFrameName : xEmbeddedFrames.getElementNames())
75*cdf0e10cSrcweir         {
76*cdf0e10cSrcweir             assertTrue(
77*cdf0e10cSrcweir                 "Unexpected frame name",
78*cdf0e10cSrcweir                 vExpectedEmbeddedFrames.remove(sFrameName));
79*cdf0e10cSrcweir             xEmbeddedFrames.getByName(sFrameName);
80*cdf0e10cSrcweir             assertTrue(
81*cdf0e10cSrcweir                 "Could not find embedded frame by name.",
82*cdf0e10cSrcweir                 xEmbeddedFrames.hasByName(sFrameName));
83*cdf0e10cSrcweir         }
84*cdf0e10cSrcweir         assertTrue(
85*cdf0e10cSrcweir             "Missing expected embedded frames.",
86*cdf0e10cSrcweir             vExpectedEmbeddedFrames.isEmpty());
87*cdf0e10cSrcweir         try
88*cdf0e10cSrcweir         {
89*cdf0e10cSrcweir             xEmbeddedFrames.getByName("Nonexisting embedded frame");
90*cdf0e10cSrcweir             fail("Got nonexisting embedded frame");
91*cdf0e10cSrcweir         }
92*cdf0e10cSrcweir         catch(com.sun.star.container.NoSuchElementException e)
93*cdf0e10cSrcweir         {}
94*cdf0e10cSrcweir         assertFalse(
95*cdf0e10cSrcweir             "Has nonexisting embedded frame",
96*cdf0e10cSrcweir             xEmbeddedFrames.hasByName("Nonexisting embedded frame"));
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir         com.sun.star.container.XIndexAccess xEmbeddedFramesIdx = (com.sun.star.container.XIndexAccess)UnoRuntime.queryInterface(
99*cdf0e10cSrcweir             com.sun.star.container.XIndexAccess.class,
100*cdf0e10cSrcweir             xEmbeddedFrames);
101*cdf0e10cSrcweir         assertEquals(
102*cdf0e10cSrcweir             "Unexpected number of embedded frames reported.", nEmbeddedFrames,
103*cdf0e10cSrcweir             xEmbeddedFramesIdx.getCount());
104*cdf0e10cSrcweir         for(int nCurrentFrameIdx = 0; nCurrentFrameIdx < xEmbeddedFramesIdx.getCount(); nCurrentFrameIdx++)
105*cdf0e10cSrcweir         {
106*cdf0e10cSrcweir             xEmbeddedFramesIdx.getByIndex(nCurrentFrameIdx);
107*cdf0e10cSrcweir         }
108*cdf0e10cSrcweir     }
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir     private void checkGraphicFrames(com.sun.star.text.XTextGraphicObjectsSupplier xTGOS)
111*cdf0e10cSrcweir         throws com.sun.star.uno.Exception
112*cdf0e10cSrcweir     {
113*cdf0e10cSrcweir         Collection<String> vExpectedGraphicFrames = new ArrayList<String>();
114*cdf0e10cSrcweir         vExpectedGraphicFrames.add("graphics1");
115*cdf0e10cSrcweir         int nGraphicFrames = vExpectedGraphicFrames.size();
116*cdf0e10cSrcweir         com.sun.star.container.XNameAccess xGraphicFrames = xTGOS.getGraphicObjects();
117*cdf0e10cSrcweir         for(String sFrameName : xGraphicFrames.getElementNames())
118*cdf0e10cSrcweir         {
119*cdf0e10cSrcweir             assertTrue(
120*cdf0e10cSrcweir                 "Unexpected frame name",
121*cdf0e10cSrcweir                 vExpectedGraphicFrames.remove(sFrameName));
122*cdf0e10cSrcweir             xGraphicFrames.getByName(sFrameName);
123*cdf0e10cSrcweir             assertTrue(
124*cdf0e10cSrcweir                 "Could not find graphics frame by name.",
125*cdf0e10cSrcweir                 xGraphicFrames.hasByName(sFrameName));
126*cdf0e10cSrcweir         }
127*cdf0e10cSrcweir         assertTrue(
128*cdf0e10cSrcweir             "Missing expected graphics frames.",
129*cdf0e10cSrcweir             vExpectedGraphicFrames.isEmpty());
130*cdf0e10cSrcweir         try
131*cdf0e10cSrcweir         {
132*cdf0e10cSrcweir             xGraphicFrames.getByName("Nonexisting graphics frame");
133*cdf0e10cSrcweir             fail("Got nonexisting graphics frame");
134*cdf0e10cSrcweir         }
135*cdf0e10cSrcweir         catch(com.sun.star.container.NoSuchElementException e)
136*cdf0e10cSrcweir         {}
137*cdf0e10cSrcweir         assertFalse(
138*cdf0e10cSrcweir             "Has nonexisting graphics frame",
139*cdf0e10cSrcweir             xGraphicFrames.hasByName("Nonexisting graphics frame"));
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir         com.sun.star.container.XIndexAccess xGraphicFramesIdx = (com.sun.star.container.XIndexAccess)UnoRuntime.queryInterface(
142*cdf0e10cSrcweir             com.sun.star.container.XIndexAccess.class,
143*cdf0e10cSrcweir             xGraphicFrames);
144*cdf0e10cSrcweir         assertEquals(
145*cdf0e10cSrcweir             "Unexpected number of graphics frames reported.", nGraphicFrames,
146*cdf0e10cSrcweir             xGraphicFramesIdx.getCount());
147*cdf0e10cSrcweir         for(int nCurrentFrameIdx = 0; nCurrentFrameIdx < xGraphicFramesIdx.getCount(); nCurrentFrameIdx++)
148*cdf0e10cSrcweir         {
149*cdf0e10cSrcweir             xGraphicFramesIdx.getByIndex(nCurrentFrameIdx);
150*cdf0e10cSrcweir         }
151*cdf0e10cSrcweir     }
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir     private void checkTextFrames(com.sun.star.text.XTextFramesSupplier xTFS)
154*cdf0e10cSrcweir         throws com.sun.star.uno.Exception
155*cdf0e10cSrcweir     {
156*cdf0e10cSrcweir         Collection<String> vExpectedTextFrames = new ArrayList<String>();
157*cdf0e10cSrcweir         vExpectedTextFrames.add("Frame1");
158*cdf0e10cSrcweir         vExpectedTextFrames.add("Frame2");
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir         int nTextFrames = vExpectedTextFrames.size();
161*cdf0e10cSrcweir         com.sun.star.container.XNameAccess xTextFrames = xTFS.getTextFrames();
162*cdf0e10cSrcweir         for(String sFrameName : xTextFrames.getElementNames())
163*cdf0e10cSrcweir         {
164*cdf0e10cSrcweir             assertTrue(
165*cdf0e10cSrcweir                 "Unexpected frame name",
166*cdf0e10cSrcweir                 vExpectedTextFrames.remove(sFrameName));
167*cdf0e10cSrcweir             xTextFrames.getByName(sFrameName);
168*cdf0e10cSrcweir             assertTrue(
169*cdf0e10cSrcweir                 "Could not find text frame by name.",
170*cdf0e10cSrcweir                 xTextFrames.hasByName(sFrameName));
171*cdf0e10cSrcweir         }
172*cdf0e10cSrcweir         assertTrue(
173*cdf0e10cSrcweir             "Missing expected text frames.", vExpectedTextFrames.isEmpty());
174*cdf0e10cSrcweir         try
175*cdf0e10cSrcweir         {
176*cdf0e10cSrcweir             xTextFrames.getByName("Nonexisting Textframe");
177*cdf0e10cSrcweir             fail("Got nonexisting text frame.");
178*cdf0e10cSrcweir         }
179*cdf0e10cSrcweir         catch(com.sun.star.container.NoSuchElementException e)
180*cdf0e10cSrcweir         {}
181*cdf0e10cSrcweir         assertFalse(
182*cdf0e10cSrcweir             "Has nonexisting text frame.",
183*cdf0e10cSrcweir             xTextFrames.hasByName("Nonexisting text frame"));
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir         com.sun.star.container.XIndexAccess xTextFramesIdx = (com.sun.star.container.XIndexAccess)UnoRuntime.queryInterface(
186*cdf0e10cSrcweir             com.sun.star.container.XIndexAccess.class,
187*cdf0e10cSrcweir             xTextFrames);
188*cdf0e10cSrcweir         assertEquals(
189*cdf0e10cSrcweir             "Unexpected number of text frames reported.", nTextFrames,
190*cdf0e10cSrcweir             xTextFramesIdx.getCount());
191*cdf0e10cSrcweir         for(int nCurrentFrameIdx = 0; nCurrentFrameIdx < xTextFramesIdx.getCount(); nCurrentFrameIdx++)
192*cdf0e10cSrcweir         {
193*cdf0e10cSrcweir             xTextFramesIdx.getByIndex(nCurrentFrameIdx);
194*cdf0e10cSrcweir         }
195*cdf0e10cSrcweir     }
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir     @Before public void setUpDocument() throws com.sun.star.uno.Exception {
198*cdf0e10cSrcweir         document = util.WriterTools.loadTextDoc(
199*cdf0e10cSrcweir             UnoRuntime.queryInterface(
200*cdf0e10cSrcweir                 XMultiServiceFactory.class,
201*cdf0e10cSrcweir                 connection.getComponentContext().getServiceManager()),
202*cdf0e10cSrcweir             TestDocument.getUrl("CheckFlies.odt"));
203*cdf0e10cSrcweir     }
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir     @After public void tearDownDocument() {
206*cdf0e10cSrcweir         util.DesktopTools.closeDoc(document);
207*cdf0e10cSrcweir     }
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir     private XTextDocument document = null;
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir     @BeforeClass public static void setUpConnection() throws Exception {
212*cdf0e10cSrcweir         connection.setUp();
213*cdf0e10cSrcweir     }
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir     @AfterClass public static void tearDownConnection()
216*cdf0e10cSrcweir         throws InterruptedException, com.sun.star.uno.Exception
217*cdf0e10cSrcweir     {
218*cdf0e10cSrcweir         connection.tearDown();
219*cdf0e10cSrcweir     }
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir     private static final OfficeConnection connection = new OfficeConnection();
222*cdf0e10cSrcweir }
223