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.toolkit;
25 
26 import com.sun.star.awt.XUnitConversion;
27 import com.sun.star.uno.UnoRuntime;
28 
29 import com.sun.star.awt.XWindow;
30 import com.sun.star.lang.XMultiServiceFactory;
31 import com.sun.star.awt.XWindowPeer;
32 
33 import util.DesktopTools;
34 
35 import org.junit.AfterClass;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.openoffice.test.OfficeConnection;
39 import static org.junit.Assert.*;
40 
41 /**
42  * This complex test is only for testing the com.sun.star.awt.XUnitConversion methods
43  * These are converter methods to get the size of a well known awt component
44  * in a com.sun.star.util.MeasureUnit you want.
45  * You don't need to know the factors to calculate by hand.
46  *
47  * @author ll93751
48  */
49 public class UnitConversion
50 {
51     /**
52      * returns the delta value between a and b
53      * @param a
54      * @param b
55      * @return
56      */
delta(int a, int b)57     private int delta(int a, int b)
58     {
59         final int n = Math.abs(a - b);
60         return n;
61     }
62 
63     private XUnitConversion m_xConversion = null;
64 
65     /**
66      * Not really a check,
67      * only a simple test call to convertSizeToLogic(...) with different parameters
68      * @param _aSize
69      * @param _aMeasureUnit
70      * @param _sEinheit
71      */
checkSize(com.sun.star.awt.Size _aSize, short _aMeasureUnit, String _sEinheit)72     private void checkSize(com.sun.star.awt.Size _aSize, short _aMeasureUnit, String _sEinheit)
73     {
74         try
75         {
76             com.sun.star.awt.Size aSizeIn = m_xConversion.convertSizeToLogic(_aSize, _aMeasureUnit);
77             System.out.println("Window size:");
78             System.out.println("Width:" + aSizeIn.Width + " " + _sEinheit);
79             System.out.println("Height:" + aSizeIn.Height + " " + _sEinheit);
80             System.out.println("");
81         }
82         catch (com.sun.star.lang.IllegalArgumentException e)
83         {
84             System.out.println("Caught IllegalArgumentException in convertSizeToLogic with '" + _sEinheit + "' " + e.getMessage());
85         }
86     }
87 
88     /**
89      * The real test function
90      * 1. try to get the XMultiServiceFactory of an already running office. Therefore make sure an (open|star)office is running with
91      *    parameters like -accept="socket,host=localhost,port=8100;urp;"
92      * 2. try to create an empty window
93      * 3. try to convert the WindowPeer to an XWindow
94      * 4. try to resize and move the window to an other position, so we get a well knowing position and size.
95      * 5. run some more tests
96      *
97      * If no test fails, the test is well done and returns with 'PASSED, OK'
98      *
99      */
100     @Test
testXUnitConversion()101     public void testXUnitConversion()
102     {
103         final XMultiServiceFactory xMSF = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
104 
105         assertNotNull("failed: There is no office.", xMSF);
106 
107         // create a window
108         XWindowPeer xWindowPeer = DesktopTools.createFloatingWindow(xMSF);
109         assertNotNull("failed: there is no window peer", xWindowPeer);
110 
111 
112         // resize and move the window to a well known position and size
113         XWindow xWindow = UnoRuntime.queryInterface(XWindow.class, xWindowPeer);
114         assertNotNull("failed: there is no window, cast wrong?", xWindow);
115 
116         xWindow.setVisible(Boolean.TRUE);
117 
118         int x = 100;
119         int y = 100;
120         int width = 640;
121         int height = 480;
122         xWindow.setPosSize(x, y, width, height, com.sun.star.awt.PosSize.POSSIZE);
123 
124         com.sun.star.awt.Rectangle aRect = xWindow.getPosSize();
125         com.sun.star.awt.Point aPoint = new com.sun.star.awt.Point(aRect.X, aRect.Y);
126         com.sun.star.awt.Size aSize = new com.sun.star.awt.Size(aRect.Width, aRect.Height);
127 
128         System.out.println("Window position and size in pixel:");
129         System.out.println("X:" + aPoint.X);
130         System.out.println("Y:" + aPoint.Y);
131         System.out.println("Width:" + aSize.Width);
132         System.out.println("Height:" + aSize.Height);
133         System.out.println("");
134 
135         assertTrue("Window pos size wrong", aSize.Width == width && aSize.Height == height && aPoint.X == x && aPoint.Y == y);
136 
137         m_xConversion = UnoRuntime.queryInterface(XUnitConversion.class, xWindowPeer);
138 
139         // try to get the position of the window in 1/100mm with the XUnitConversion method
140         try
141         {
142             com.sun.star.awt.Point aPointInMM_100TH = m_xConversion.convertPointToLogic(aPoint, com.sun.star.util.MeasureUnit.MM_100TH);
143             System.out.println("Window position:");
144             System.out.println("X:" + aPointInMM_100TH.X + " 1/100mm");
145             System.out.println("Y:" + aPointInMM_100TH.Y + " 1/100mm");
146             System.out.println("");
147         }
148         catch (com.sun.star.lang.IllegalArgumentException e)
149         {
150             fail("failed: IllegalArgumentException caught in convertPointToLogic " + e.getMessage());
151         }
152 
153         // try to get the size of the window in 1/100mm with the XUnitConversion method
154         com.sun.star.awt.Size aSizeInMM_100TH = null;
155         com.sun.star.awt.Size aSizeInMM_10TH = null;
156         try
157         {
158             aSizeInMM_100TH = m_xConversion.convertSizeToLogic(aSize, com.sun.star.util.MeasureUnit.MM_100TH);
159             System.out.println("Window size:");
160             System.out.println("Width:" + aSizeInMM_100TH.Width + " 1/100mm");
161             System.out.println("Height:" + aSizeInMM_100TH.Height + " 1/100mm");
162             System.out.println("");
163 
164             // try to get the size of the window in 1/10mm with the XUnitConversion method
165 
166             aSizeInMM_10TH = m_xConversion.convertSizeToLogic(aSize, com.sun.star.util.MeasureUnit.MM_10TH);
167             System.out.println("Window size:");
168             System.out.println("Width:" + aSizeInMM_10TH.Width + " 1/10mm");
169             System.out.println("Height:" + aSizeInMM_10TH.Height + " 1/10mm");
170             System.out.println("");
171 
172             // check the size with a delta which must be smaller a given difference
173             assertTrue("Size.Width  not correct", delta(aSizeInMM_100TH.Width, aSizeInMM_10TH.Width * 10) < 10);
174             assertTrue("Size.Height not correct", delta(aSizeInMM_100TH.Height, aSizeInMM_10TH.Height * 10) < 10);
175 
176             // new
177             checkSize(aSize, com.sun.star.util.MeasureUnit.PIXEL, "pixel");
178             checkSize(aSize, com.sun.star.util.MeasureUnit.APPFONT, "appfont");
179             checkSize(aSize, com.sun.star.util.MeasureUnit.SYSFONT, "sysfont");
180 
181             // simply check some more parameters
182             checkSize(aSize, com.sun.star.util.MeasureUnit.MM, "mm");
183             checkSize(aSize, com.sun.star.util.MeasureUnit.CM, "cm");
184             checkSize(aSize, com.sun.star.util.MeasureUnit.INCH_1000TH, "1/1000inch");
185             checkSize(aSize, com.sun.star.util.MeasureUnit.INCH_100TH, "1/100inch");
186             checkSize(aSize, com.sun.star.util.MeasureUnit.INCH_10TH, "1/10inch");
187             checkSize(aSize, com.sun.star.util.MeasureUnit.INCH, "inch");
188             // checkSize(aSize, com.sun.star.util.MeasureUnit.M, "m");
189             checkSize(aSize, com.sun.star.util.MeasureUnit.POINT, "point");
190             checkSize(aSize, com.sun.star.util.MeasureUnit.TWIP, "twip");
191             // checkSize(aSize, com.sun.star.util.MeasureUnit.KM, "km");
192             // checkSize(aSize, com.sun.star.util.MeasureUnit.PICA, "pica");
193             // checkSize(aSize, com.sun.star.util.MeasureUnit.FOOT, "foot");
194             // checkSize(aSize, com.sun.star.util.MeasureUnit.MILE, "mile");
195         }
196         catch (com.sun.star.lang.IllegalArgumentException e)
197         {
198             fail("failed: IllegalArgumentException caught in convertSizeToLogic " + e.getMessage());
199         }
200 
201         // convert the 1/100mm window size back to pixel
202         try
203         {
204             com.sun.star.awt.Size aNewSize = m_xConversion.convertSizeToPixel(aSizeInMM_100TH, com.sun.star.util.MeasureUnit.MM_100TH);
205             System.out.println("Window size:");
206             System.out.println("Width:" + aNewSize.Width + " pixel");
207             System.out.println("Height:" + aNewSize.Height + " pixel");
208 
209             // assure the pixels are the same as we already know
210             assertTrue("failed: Size from pixel to 1/100mm to pixel", aSize.Width == aNewSize.Width && aSize.Height == aNewSize.Height);
211         }
212         catch (com.sun.star.lang.IllegalArgumentException e)
213         {
214             fail("failed: IllegalArgumentException caught in convertSizeToPixel " + e.getMessage());
215         }
216 
217         // close the window.
218         // IMHO a little bit stupid, but the XWindow doesn't support a XCloseable interface
219         xWindow.dispose();
220     }
221 
222     @BeforeClass
setUpConnection()223     public static void setUpConnection() throws Exception
224     {
225         System.out.println( "--------------------------------------------------------------------------------" );
226         System.out.println( "starting class: " + UnitConversion.class.getName() );
227         System.out.println( "connecting ..." );
228         connection.setUp();
229     }
230 
231     @AfterClass
tearDownConnection()232     public static void tearDownConnection()
233         throws InterruptedException, com.sun.star.uno.Exception
234     {
235         System.out.println();
236         System.out.println( "tearing down connection" );
237         connection.tearDown();
238         System.out.println( "finished class: " + UnitConversion.class.getName() );
239         System.out.println( "--------------------------------------------------------------------------------" );
240     }
241 
242     private static final OfficeConnection connection = new OfficeConnection();
243 }
244