Kaydet (Commit) 7e51276d authored tarafından Michael Stahl's avatar Michael Stahl

toolkit: convert UnitConversion test to C++

Just translate the test, it checks just a tiny fraction of what it does
just like the Java version.

Change-Id: I85b4c890d970a00402d7f1b6f13d4e9ea2dd6e3c
Reviewed-on: https://gerrit.libreoffice.org/47443Tested-by: 's avatarJenkins <ci@libreoffice.org>
Reviewed-by: 's avatarMichael Stahl <mstahl@redhat.com>
üst e0b4c9a8
# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*-
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
$(eval $(call gb_CppunitTest_CppunitTest,toolkit))
$(eval $(call gb_CppunitTest_add_exception_objects,toolkit, \
toolkit/qa/cppunit/UnitConversion \
))
$(eval $(call gb_CppunitTest_use_libraries,toolkit, \
basegfx \
comphelper \
cppu \
cppuhelper \
sal \
salhelper \
test \
tk \
tl \
unotest \
vcl \
))
$(eval $(call gb_CppunitTest_use_api,toolkit,\
offapi \
udkapi \
))
$(eval $(call gb_CppunitTest_use_ure,toolkit))
$(eval $(call gb_CppunitTest_use_configuration,toolkit))
$(eval $(call gb_CppunitTest_use_vcl,toolkit))
$(eval $(call gb_CppunitTest_use_components,toolkit,\
comphelper/util/comphelp \
configmgr/source/configmgr \
framework/util/fwk \
i18npool/util/i18npool \
sfx2/util/sfx \
toolkit/util/tk \
ucb/source/core/ucb1 \
ucb/source/ucp/file/ucpfile1 \
))
# vim: set noet sw=4 ts=4:
......@@ -36,12 +36,10 @@ $(eval $(call gb_JunitTest_add_sourcefiles,toolkit_complex,\
toolkit/qa/complex/toolkit/awtgrid/TMutableGridDataModel \
toolkit/qa/complex/toolkit/awtgrid/DummyColumn \
toolkit/qa/complex/toolkit/GridControl \
toolkit/qa/complex/toolkit/UnitConversion \
))
$(eval $(call gb_JunitTest_add_classes,toolkit_complex,\
complex.toolkit.GridControl \
complex.toolkit.UnitConversion \
))
# vim: set noet sw=4 ts=4:
......@@ -23,6 +23,10 @@ $(eval $(call gb_Module_add_targets,toolkit,\
Library_tk \
))
$(eval $(call gb_Module_add_check_targets,toolkit,\
CppunitTest_toolkit \
))
ifneq ($(OOO_JUNIT_JAR),)
$(eval $(call gb_Module_add_subsequentcheck_targets,toolkit,\
JunitTest_toolkit_complex \
......
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
*
************************************************************************/
package complex.toolkit;
import com.sun.star.awt.XUnitConversion;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.awt.XWindow;
import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.awt.XWindowPeer;
import util.DesktopTools;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openoffice.test.OfficeConnection;
import static org.junit.Assert.*;
/**
* This complex test is only for testing the com.sun.star.awt.XUnitConversion methods
* These are converter methods to get the size of a well known awt component
* in a com.sun.star.util.MeasureUnit you want.
* You don't need to know the factors to calculate by hand.
*
*/
public class UnitConversion
{
/**
* returns the delta value between a and b
* @param a
* @param b
* @return
*/
private int delta(int a, int b)
{
final int n = Math.abs(a - b);
return n;
}
private XUnitConversion m_xConversion = null;
/**
* Not really a check,
* only a simple test call to convertSizeToLogic(...) with different parameters
* @param _aSize
* @param _aMeasureUnit
* @param _sUnit
*/
private void checkSize(com.sun.star.awt.Size _aSize, short _aMeasureUnit, String _sUnit)
{
com.sun.star.awt.Size aSizeIn = m_xConversion.convertSizeToLogic(_aSize, _aMeasureUnit);
System.out.println("Window size:");
System.out.println("Width:" + aSizeIn.Width + " " + _sUnit);
System.out.println("Height:" + aSizeIn.Height + " " + _sUnit);
System.out.println("");
}
/**
* The real test function
* 1. try to get the XMultiServiceFactory of an already running office. Therefore make sure an (open|star)office is running with
* parameters like --accept="socket,host=localhost,port=8100;urp;"
* 2. try to create an empty window
* 3. try to convert the WindowPeer to an XWindow
* 4. try to resize and move the window to an other position, so we get a well knowing position and size.
* 5. run some more tests
*
* If no test fails, the test is well done and returns with 'COMPLETED.OK'
*
*/
@Test
public void testXUnitConversion()
{
final XMultiServiceFactory xMSF = UnoRuntime.queryInterface(XMultiServiceFactory.class, connection.getComponentContext().getServiceManager());
assertNotNull("failed: There is no office.", xMSF);
// create a window
int x = 100;
int y = 100;
int width = 640;
int height = 480;
XWindowPeer xWindowPeer = DesktopTools.createFloatingWindow(
xMSF, x, y, width, height);
assertNotNull("failed: there is no window peer", xWindowPeer);
// resize and move the window to a well known position and size
XWindow xWindow = UnoRuntime.queryInterface(XWindow.class, xWindowPeer);
assertNotNull("failed: there is no window, cast wrong?", xWindow);
xWindow.setVisible(Boolean.TRUE);
com.sun.star.awt.Rectangle aRect = xWindow.getPosSize();
com.sun.star.awt.Point aPoint = new com.sun.star.awt.Point(aRect.X, aRect.Y);
com.sun.star.awt.Size aSize = new com.sun.star.awt.Size(aRect.Width, aRect.Height);
System.out.println("Window position and size in pixel:");
System.out.println("X:" + aPoint.X);
System.out.println("Y:" + aPoint.Y);
System.out.println("Width:" + aSize.Width);
System.out.println("Height:" + aSize.Height);
System.out.println("");
assertTrue("Window pos size wrong", aSize.Width == width && aSize.Height == height && aPoint.X == x && aPoint.Y == y);
m_xConversion = UnoRuntime.queryInterface(XUnitConversion.class, xWindowPeer);
// try to get the position of the window in 1/100mm with the XUnitConversion method
com.sun.star.awt.Point aPointInMM_100TH = m_xConversion.convertPointToLogic(aPoint, com.sun.star.util.MeasureUnit.MM_100TH);
System.out.println("Window position:");
System.out.println("X:" + aPointInMM_100TH.X + " 1/100mm");
System.out.println("Y:" + aPointInMM_100TH.Y + " 1/100mm");
System.out.println("");
// try to get the size of the window in 1/100mm with the XUnitConversion method
com.sun.star.awt.Size aSizeInMM_100TH = null;
com.sun.star.awt.Size aSizeInMM_10TH = null;
aSizeInMM_100TH = m_xConversion.convertSizeToLogic(aSize, com.sun.star.util.MeasureUnit.MM_100TH);
System.out.println("Window size:");
System.out.println("Width:" + aSizeInMM_100TH.Width + " 1/100mm");
System.out.println("Height:" + aSizeInMM_100TH.Height + " 1/100mm");
System.out.println("");
// try to get the size of the window in 1/10mm with the XUnitConversion method
aSizeInMM_10TH = m_xConversion.convertSizeToLogic(aSize, com.sun.star.util.MeasureUnit.MM_10TH);
System.out.println("Window size:");
System.out.println("Width:" + aSizeInMM_10TH.Width + " 1/10mm");
System.out.println("Height:" + aSizeInMM_10TH.Height + " 1/10mm");
System.out.println("");
// check the size with a delta which must be smaller a given difference
assertTrue("Size.Width not correct", delta(aSizeInMM_100TH.Width, aSizeInMM_10TH.Width * 10) < 10);
assertTrue("Size.Height not correct", delta(aSizeInMM_100TH.Height, aSizeInMM_10TH.Height * 10) < 10);
// new
checkSize(aSize, com.sun.star.util.MeasureUnit.PIXEL, "pixel");
checkSize(aSize, com.sun.star.util.MeasureUnit.APPFONT, "appfont");
checkSize(aSize, com.sun.star.util.MeasureUnit.SYSFONT, "sysfont");
// simply check some more parameters
checkSize(aSize, com.sun.star.util.MeasureUnit.MM, "mm");
checkSize(aSize, com.sun.star.util.MeasureUnit.CM, "cm");
checkSize(aSize, com.sun.star.util.MeasureUnit.INCH_1000TH, "1/1000inch");
checkSize(aSize, com.sun.star.util.MeasureUnit.INCH_100TH, "1/100inch");
checkSize(aSize, com.sun.star.util.MeasureUnit.INCH_10TH, "1/10inch");
checkSize(aSize, com.sun.star.util.MeasureUnit.INCH, "inch");
checkSize(aSize, com.sun.star.util.MeasureUnit.POINT, "point");
checkSize(aSize, com.sun.star.util.MeasureUnit.TWIP, "twip");
// convert the 1/100mm window size back to pixel
com.sun.star.awt.Size aNewSize = m_xConversion.convertSizeToPixel(aSizeInMM_100TH, com.sun.star.util.MeasureUnit.MM_100TH);
System.out.println("Window size:");
System.out.println("Width:" + aNewSize.Width + " pixel");
System.out.println("Height:" + aNewSize.Height + " pixel");
// assure the pixels are the same as we already know
assertTrue("failed: Size from pixel to 1/100mm to pixel", aSize.Width == aNewSize.Width && aSize.Height == aNewSize.Height);
// close the window.
// IMHO a little bit stupid, but the XWindow doesn't support a XCloseable interface
xWindow.dispose();
}
@BeforeClass
public static void setUpConnection() throws Exception
{
System.out.println( "--------------------------------------------------------------------------------" );
System.out.println( "starting class: " + UnitConversion.class.getName() );
System.out.println( "connecting ..." );
connection.setUp();
}
@AfterClass
public static void tearDownConnection()
throws InterruptedException, com.sun.star.uno.Exception
{
System.out.println();
System.out.println( "tearing down connection" );
connection.tearDown();
System.out.println( "finished class: " + UnitConversion.class.getName() );
System.out.println( "--------------------------------------------------------------------------------" );
}
private static final OfficeConnection connection = new OfficeConnection();
}
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <test/bootstrapfixture.hxx>
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#include <com/sun/star/util/MeasureUnit.hpp>
#include <com/sun/star/awt/VclWindowPeerAttribute.hpp>
#include <com/sun/star/awt/WindowAttribute.hpp>
#include <com/sun/star/awt/WindowDescriptor.hpp>
#include <com/sun/star/awt/XUnitConversion.hpp>
#include <com/sun/star/awt/XWindowPeer.hpp>
#include <com/sun/star/awt/XWindow.hpp>
using namespace ::com::sun::star;
namespace
{
class ToolkitTest : public test::BootstrapFixture
{
public:
void testXUnitConversion();
CPPUNIT_TEST_SUITE(ToolkitTest);
CPPUNIT_TEST(testXUnitConversion);
CPPUNIT_TEST_SUITE_END();
};
/**
* Creates a floating XWindow on the given position and size.
* @return a floating XWindow
* @param X the X-Position of the floating XWindow
* @param Y the Y-Position of the floating XWindow
* @param width the width of the floating XWindow
* @param height the height of the floating XWindow
* @param xMSF the MultiServiceFactory
*/
uno::Reference<awt::XWindowPeer>
createFloatingWindow(uno::Reference<lang::XMultiServiceFactory> const& xMSF, sal_Int32 const nX,
sal_Int32 const nY, sal_Int32 const nWidth, sal_Int32 const nHeight)
{
uno::Reference<awt::XToolkit> const xTk(xMSF->createInstance("com.sun.star.awt.Toolkit"),
uno::UNO_QUERY);
awt::WindowDescriptor descriptor;
descriptor.Type = awt::WindowClass_TOP;
descriptor.WindowServiceName = "modelessdialog";
descriptor.ParentIndex = -1;
descriptor.Bounds.X = nX;
descriptor.Bounds.Y = nY;
descriptor.Bounds.Width = nWidth;
descriptor.Bounds.Height = nHeight;
descriptor.WindowAttributes
= (awt::WindowAttribute::BORDER + awt::WindowAttribute::MOVEABLE
+ awt::WindowAttribute::SIZEABLE + awt::WindowAttribute::CLOSEABLE
+ awt::VclWindowPeerAttribute::CLIPCHILDREN);
return xTk->createWindow(descriptor);
}
/**
* Not really a check,
* only a simple test call to convertSizeToLogic(...) with different parameters
*/
void checkSize(uno::Reference<awt::XUnitConversion> const& xConv, awt::Size const& rSize,
sal_Int16 const nMeasureUnit, OUString const& rUnit)
{
awt::Size const aSizeIn = xConv->convertSizeToLogic(rSize, nMeasureUnit);
std::cerr << "Window size:\n";
std::cerr << "Width: " << aSizeIn.Width << " " << rUnit << "\n";
std::cerr << "Height: " << aSizeIn.Height << " " << rUnit << "\n";
}
/**
* The real test function
* 2. try to create an empty window
* 3. try to convert the WindowPeer to an XWindow
* 4. try to resize and move the window to an other position, so we get a well knowing position and size.
* 5. run some more tests
*/
void ToolkitTest::testXUnitConversion()
{
// create a window
sal_Int32 x = 100;
sal_Int32 y = 100;
sal_Int32 width = 640;
sal_Int32 height = 480;
uno::Reference<awt::XWindowPeer> const xWindowPeer
= createFloatingWindow(getMultiServiceFactory(), x, y, width, height);
CPPUNIT_ASSERT(xWindowPeer.is());
// resize and move the window to a well known position and size
uno::Reference<awt::XWindow> const xWindow(xWindowPeer, uno::UNO_QUERY);
CPPUNIT_ASSERT(xWindow.is());
xWindow->setVisible(true);
awt::Rectangle aRect = xWindow->getPosSize();
awt::Point aPoint(aRect.X, aRect.Y);
awt::Size aSize(aRect.Width, aRect.Height);
std::cerr << "Window position and size in pixel:\n";
std::cerr << "X: " << aPoint.X << "\n";
std::cerr << "Y: " << aPoint.Y << "\n";
std::cerr << "Width: " << aSize.Width << "\n";
std::cerr << "Height: " << aSize.Height << "\n";
CPPUNIT_ASSERT_EQUAL_MESSAGE("Window size wrong", width, aSize.Width);
CPPUNIT_ASSERT_EQUAL_MESSAGE("Window size wrong", height, aSize.Height);
CPPUNIT_ASSERT_EQUAL_MESSAGE("Window pos wrong", x, aPoint.X);
CPPUNIT_ASSERT_EQUAL_MESSAGE("Window pos wrong", y, aPoint.Y);
uno::Reference<awt::XUnitConversion> const xConv(xWindowPeer, uno::UNO_QUERY);
// try to get the position of the window in 1/100mm with the XUnitConversion method
awt::Point const aPointInMM_100TH
= xConv->convertPointToLogic(aPoint, util::MeasureUnit::MM_100TH);
std::cerr << "Window position:\n";
std::cerr << "X: " << aPointInMM_100TH.X << " 1/100mm\n";
std::cerr << "Y: " << aPointInMM_100TH.Y << " 1/100mm\n";
// try to get the size of the window in 1/100mm with the XUnitConversion method
awt::Size const aSizeInMM_100TH = xConv->convertSizeToLogic(aSize, util::MeasureUnit::MM_100TH);
std::cerr << "Window size:\n";
std::cerr << "Width: " << aSizeInMM_100TH.Width << " 1/100mm\n";
std::cerr << "Height: " << aSizeInMM_100TH.Height << " 1/100mm\n";
// try to get the size of the window in 1/10mm with the XUnitConversion method
awt::Size const aSizeInMM_10TH = xConv->convertSizeToLogic(aSize, util::MeasureUnit::MM_10TH);
std::cerr << "Window size:\n";
std::cerr << "Width: " << aSizeInMM_10TH.Width << " 1/10mm\n";
std::cerr << "Height: " << aSizeInMM_10TH.Height << " 1/10mm\n";
// check the size with a delta which must be smaller a given difference
CPPUNIT_ASSERT_MESSAGE("Size.Width not correct",
std::abs(aSizeInMM_100TH.Width - aSizeInMM_10TH.Width * 10) < 10);
CPPUNIT_ASSERT_MESSAGE("Size.Height not correct",
std::abs(aSizeInMM_100TH.Height - aSizeInMM_10TH.Height * 10) < 10);
// new
checkSize(xConv, aSize, util::MeasureUnit::PIXEL, "pixel");
checkSize(xConv, aSize, util::MeasureUnit::APPFONT, "appfont");
checkSize(xConv, aSize, util::MeasureUnit::SYSFONT, "sysfont");
// simply check some more parameters
checkSize(xConv, aSize, util::MeasureUnit::MM, "mm");
checkSize(xConv, aSize, util::MeasureUnit::CM, "cm");
checkSize(xConv, aSize, util::MeasureUnit::INCH_1000TH, "1/1000inch");
checkSize(xConv, aSize, util::MeasureUnit::INCH_100TH, "1/100inch");
checkSize(xConv, aSize, util::MeasureUnit::INCH_10TH, "1/10inch");
checkSize(xConv, aSize, util::MeasureUnit::INCH, "inch");
checkSize(xConv, aSize, util::MeasureUnit::POINT, "point");
checkSize(xConv, aSize, util::MeasureUnit::TWIP, "twip");
// convert the 1/100mm window size back to pixel
awt::Size const aNewSize
= xConv->convertSizeToPixel(aSizeInMM_100TH, util::MeasureUnit::MM_100TH);
std::cerr << "Window size:\n";
std::cerr << "Width: " << aNewSize.Width << " pixel\n";
std::cerr << "Height: " << aNewSize.Height << " pixel\n";
// assure the pixels are the same as we already know
CPPUNIT_ASSERT_MESSAGE("failed: Size from pixel to 1/100mm to pixel",
aSize.Width == aNewSize.Width && aSize.Height == aNewSize.Height);
// close the window.
xWindow->dispose();
}
CPPUNIT_TEST_SUITE_REGISTRATION(ToolkitTest);
}
CPPUNIT_PLUGIN_IMPLEMENT();
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment