1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  */
9 
10 #include <test/sheet/shape.hxx>
11 
12 #include <com/sun/star/beans/XPropertySet.hpp>
13 #include <com/sun/star/sheet/XSpreadsheet.hpp>
14 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
15 #include <com/sun/star/sheet/XSpreadsheets.hpp>
16 #include <com/sun/star/table/XCell.hpp>
17 #include <com/sun/star/uno/Any.hxx>
18 #include <com/sun/star/uno/Reference.hxx>
19 #include <com/sun/star/uno/Sequence.hxx>
20 
21 #include <cppunit/TestAssert.h>
22 
23 using namespace com::sun::star;
24 using namespace com::sun::star::uno;
25 
26 namespace apitest
27 {
testShapePropertiesAnchor()28 void Shape::testShapePropertiesAnchor()
29 {
30     uno::Reference<beans::XPropertySet> xShape(init(), UNO_QUERY_THROW);
31     uno::Any aNewValue;
32 
33     uno::Reference<sheet::XSpreadsheetDocument> xDoc(getXSheetDocument(), UNO_QUERY_THROW);
34     uno::Reference<sheet::XSpreadsheets> xSheets(xDoc->getSheets(), UNO_SET_THROW);
35     uno::Sequence<OUString> sheetNames = xSheets->getElementNames();
36     uno::Reference<sheet::XSpreadsheet> xSheet(xSheets->getByName(sheetNames[0]), UNO_QUERY_THROW);
37     uno::Reference<table::XCell> xCell(xSheet->getCellByPosition(0, 0), UNO_SET_THROW);
38 
39     // Shape should be anchored to sheet by default
40     uno::Reference<sheet::XSpreadsheet> xSheetGet;
41     CPPUNIT_ASSERT_MESSAGE("Unable to get PropertyValue Anchor (XSpreadsheet)",
42                            xShape->getPropertyValue("Anchor") >>= xSheetGet);
43 
44     // Anchor the shape to a cell
45     aNewValue <<= xCell;
46     xShape->setPropertyValue("Anchor", aNewValue);
47     uno::Reference<table::XCell> xCellGet;
48     CPPUNIT_ASSERT_MESSAGE("Unable to get PropertyValue Anchor (XCell)",
49                            xShape->getPropertyValue("Anchor") >>= xCellGet);
50 
51     // Shape should not resize with cell by default
52     bool bIsResizeWithCell = {};
53     CPPUNIT_ASSERT(xShape->getPropertyValue("ResizeWithCell") >>= bIsResizeWithCell);
54     CPPUNIT_ASSERT_MESSAGE("Shape should not resize with the cell", !bIsResizeWithCell);
55 
56     xShape->setPropertyValue("ResizeWithCell", uno::Any(true));
57     CPPUNIT_ASSERT(xShape->getPropertyValue("ResizeWithCell") >>= bIsResizeWithCell);
58     CPPUNIT_ASSERT_MESSAGE("Shape should resize with the cell", bIsResizeWithCell);
59 
60     // Anchoring to a different cell should keep the "ResizeWithCell" attribute
61     uno::Reference<table::XCell> xCell2(xSheet->getCellByPosition(1, 2), UNO_SET_THROW);
62     aNewValue <<= xCell2;
63     xShape->setPropertyValue("Anchor", aNewValue);
64     CPPUNIT_ASSERT(xShape->getPropertyValue("ResizeWithCell") >>= bIsResizeWithCell);
65     CPPUNIT_ASSERT_MESSAGE("ResizeWithCell should still be set", bIsResizeWithCell);
66 
67     // Now anchor to sheet again
68     aNewValue <<= xSheet;
69     xShape->setPropertyValue("Anchor", aNewValue);
70     CPPUNIT_ASSERT(xShape->getPropertyValue("Anchor") >>= xSheetGet);
71     CPPUNIT_ASSERT_MESSAGE("Unable to get PropertyValue Anchor (XSpreadsheet)",
72                            xShape->getPropertyValue("Anchor") >>= xSheetGet);
73 
74     // Setting ResizeWithCell while anchored to page should not have any effect
75     CPPUNIT_ASSERT(xShape->getPropertyValue("ResizeWithCell") >>= bIsResizeWithCell);
76     CPPUNIT_ASSERT_MESSAGE("ResizeWithCell should be false for sheet anchored shapes",
77                            !bIsResizeWithCell);
78     xShape->setPropertyValue("ResizeWithCell", uno::Any(true));
79     CPPUNIT_ASSERT(xShape->getPropertyValue("ResizeWithCell") >>= bIsResizeWithCell);
80     CPPUNIT_ASSERT_MESSAGE("ResizeWithCell should be unchangeable for sheet anchored shapes",
81                            !bIsResizeWithCell);
82 }
83 
testShapePropertiesPosition()84 void Shape::testShapePropertiesPosition()
85 {
86     uno::Reference<beans::XPropertySet> xShape(init(), UNO_QUERY_THROW);
87     uno::Any aNewValue;
88 
89     sal_Int32 nHoriOrientPositionGet = 0;
90     sal_Int32 nHoriOrientPositionSet = 0;
91     CPPUNIT_ASSERT_MESSAGE("Unable to get PropertyValue HoriOrientPosition",
92                            xShape->getPropertyValue("HoriOrientPosition")
93                            >>= nHoriOrientPositionGet);
94 
95     aNewValue <<= nHoriOrientPositionGet + 42;
96     xShape->setPropertyValue("HoriOrientPosition", aNewValue);
97     CPPUNIT_ASSERT(xShape->getPropertyValue("HoriOrientPosition") >>= nHoriOrientPositionSet);
98     CPPUNIT_ASSERT_EQUAL_MESSAGE("Unable to set PropertyValue HoriOrientPosition",
99                                  nHoriOrientPositionGet + 42, nHoriOrientPositionSet);
100 
101     sal_Int32 nVertOrientPositionGet = 0;
102     sal_Int32 nVertOrientPositionSet = 0;
103     CPPUNIT_ASSERT_MESSAGE("Unable to get PropertyValue VertOrientPosition",
104                            xShape->getPropertyValue("VertOrientPosition")
105                            >>= nVertOrientPositionGet);
106 
107     aNewValue <<= nVertOrientPositionGet + 42;
108     xShape->setPropertyValue("VertOrientPosition", aNewValue);
109     CPPUNIT_ASSERT(xShape->getPropertyValue("VertOrientPosition") >>= nVertOrientPositionSet);
110     CPPUNIT_ASSERT_EQUAL_MESSAGE("Unable to set PropertyValue VertOrientPosition",
111                                  nVertOrientPositionGet + 42, nVertOrientPositionSet);
112 }
113 }
114 
115 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
116