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