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