update emoji autocorrect entries from po-files
[LibreOffice.git] / sc / qa / extras / sccondformats.cxx
blob8b6f66647f2cdec2a68899348bcac575003b913b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 */
10 #include <test/calc_unoapi_test.hxx>
11 #include <svx/svdograf.hxx>
12 #include <svx/svdpage.hxx>
13 #include <sfx2/dispatch.hxx>
15 #include <com/sun/star/beans/XPropertySet.hpp>
16 #include <com/sun/star/sheet/ConditionOperator.hpp>
17 #include <com/sun/star/sheet/XSheetConditionalEntries.hpp>
18 #include <com/sun/star/sheet/XSpreadsheetDocument.hpp>
19 #include <com/sun/star/sheet/XSpreadsheet.hpp>
20 #include <com/sun/star/table/CellAddress.hpp>
21 #include <unonames.hxx>
23 #include "tabvwsh.hxx"
24 #include "docsh.hxx"
26 #include "sc.hrc"
28 using namespace css;
30 namespace sc_apitest {
32 #define NUMBER_OF_TESTS 1
34 class ScConditionalFormatTest : public CalcUnoApiTest
36 public:
37 ScConditionalFormatTest();
39 virtual void setUp() SAL_OVERRIDE;
40 virtual void tearDown() SAL_OVERRIDE;
42 uno::Reference< uno::XInterface > init();
43 void testCondFormat();
44 void testUndoAnchor();
46 CPPUNIT_TEST_SUITE(ScConditionalFormatTest);
47 CPPUNIT_TEST(testCondFormat);
48 CPPUNIT_TEST(testUndoAnchor);
49 CPPUNIT_TEST_SUITE_END();
50 private:
52 static sal_Int32 nTest;
53 static uno::Reference< lang::XComponent > mxComponent;
56 sal_Int32 ScConditionalFormatTest::nTest = 0;
57 uno::Reference< lang::XComponent > ScConditionalFormatTest::mxComponent;
59 ScConditionalFormatTest::ScConditionalFormatTest()
60 : CalcUnoApiTest("sc/qa/unit/data/ods")
64 uno::Reference< uno::XInterface > ScConditionalFormatTest::init()
66 // get the test file
67 OUString aFileURL;
68 createFileURL(OUString("new_cond_format_test.ods"), aFileURL);
69 if(!mxComponent.is())
70 mxComponent = loadFromDesktop(aFileURL);
71 CPPUNIT_ASSERT_MESSAGE("Component not loaded",mxComponent.is());
73 // get the first sheet
74 uno::Reference< sheet::XSpreadsheetDocument > xDoc(mxComponent, uno::UNO_QUERY_THROW);
75 uno::Reference< container::XIndexAccess > xIndex (xDoc->getSheets(), uno::UNO_QUERY_THROW);
76 uno::Reference< sheet::XSpreadsheet > xSheet( xIndex->getByIndex(0), uno::UNO_QUERY_THROW);
78 return xSheet;
81 void ScConditionalFormatTest::testCondFormat()
83 uno::Reference< sheet::XSpreadsheet > aSheet(init(), uno::UNO_QUERY_THROW);
84 uno::Reference< beans::XPropertySet > xProps;
85 uno::Reference< sheet::XSheetConditionalEntries > xSheetConditionalEntries;
87 xProps.set(aSheet->getCellRangeByPosition(1, 1, 1, 21), uno::UNO_QUERY_THROW);
88 xSheetConditionalEntries.set(xProps->getPropertyValue(SC_UNONAME_CONDFMT), uno::UNO_QUERY_THROW);
89 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), xSheetConditionalEntries->getCount());
91 uno::Sequence< beans::PropertyValue > aPropertyValueList(5);
92 aPropertyValueList[0].Name = SC_UNONAME_STYLENAME;
93 aPropertyValueList[0].Value <<= uno::makeAny<OUString>("Result2");
94 aPropertyValueList[1].Name = SC_UNONAME_FORMULA1;
95 aPropertyValueList[1].Value <<= uno::makeAny<OUString>("$Sheet1.$B$2");
96 aPropertyValueList[2].Name = SC_UNONAME_FORMULA2;
97 aPropertyValueList[2].Value <<= uno::makeAny<OUString>("$Sheet1.$A$2");
98 aPropertyValueList[3].Name = SC_UNONAME_OPERATOR;
99 aPropertyValueList[3].Value <<= sheet::ConditionOperator_EQUAL;
100 aPropertyValueList[4].Name = SC_UNONAME_SOURCEPOS;
101 aPropertyValueList[4].Value <<= uno::makeAny(table::CellAddress(0, 0, 16));
102 xSheetConditionalEntries->addNew(aPropertyValueList);
103 CPPUNIT_ASSERT_EQUAL(sal_Int32(2), xSheetConditionalEntries->getCount());
104 xProps->setPropertyValue(SC_UNONAME_CONDFMT, uno::makeAny(xSheetConditionalEntries));
106 xProps.set(aSheet->getCellByPosition(0, 16), uno::UNO_QUERY_THROW);
107 xSheetConditionalEntries.set(xProps->getPropertyValue(SC_UNONAME_CONDFMT), uno::UNO_QUERY_THROW);
108 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), xSheetConditionalEntries->getCount());
110 // clear conditional formatting in cell $B$17
111 xProps.set(aSheet->getCellByPosition(1, 16), uno::UNO_QUERY_THROW);
112 xSheetConditionalEntries.set(xProps->getPropertyValue(SC_UNONAME_CONDFMT), uno::UNO_QUERY_THROW);
113 CPPUNIT_ASSERT_EQUAL(sal_Int32(2), xSheetConditionalEntries->getCount());
114 xSheetConditionalEntries->clear();
115 xProps->setPropertyValue(SC_UNONAME_CONDFMT, uno::makeAny(xSheetConditionalEntries));
117 xProps.set(aSheet->getCellByPosition(1, 16), uno::UNO_QUERY_THROW);
118 xSheetConditionalEntries.set(xProps->getPropertyValue(SC_UNONAME_CONDFMT), uno::UNO_QUERY_THROW);
119 // This was 1 before - conditional formats were not removed
120 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), xSheetConditionalEntries->getCount());
123 void ScConditionalFormatTest::testUndoAnchor()
125 const OString sFailedMessage = OString("Failed on :");
126 OUString aFileURL;
127 createFileURL(OUString("document_with_linked_graphic.ods"), aFileURL);
128 // open the document with graphic included
129 uno::Reference< com::sun::star::lang::XComponent > xComponent = loadFromDesktop(aFileURL);
130 CPPUNIT_ASSERT(xComponent.is());
132 // Get the document model
133 SfxObjectShell* pFoundShell = SfxObjectShell::GetShellFromComponent(xComponent);
134 CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);
136 ScDocShell* xDocSh = dynamic_cast<ScDocShell*>(pFoundShell);
137 CPPUNIT_ASSERT(xDocSh != NULL);
139 // Check whether graphic imported well
140 ScDocument& rDoc = xDocSh->GetDocument();
141 ScDrawLayer* pDrawLayer = rDoc.GetDrawLayer();
142 CPPUNIT_ASSERT_MESSAGE( sFailedMessage.getStr(), pDrawLayer != NULL );
144 const SdrPage *pPage = pDrawLayer->GetPage(0);
145 CPPUNIT_ASSERT_MESSAGE( sFailedMessage.getStr(), pPage != NULL );
147 SdrGrafObj* pObject = dynamic_cast<SdrGrafObj*>(pPage->GetObj(0));
148 CPPUNIT_ASSERT_MESSAGE( sFailedMessage.getStr(), pObject != NULL );
149 CPPUNIT_ASSERT_MESSAGE( sFailedMessage.getStr(), pObject->IsLinkedGraphic() );
151 const GraphicObject& rGraphicObj = pObject->GetGraphicObject(true);
152 CPPUNIT_ASSERT_MESSAGE( sFailedMessage.getStr(), !rGraphicObj.IsSwappedOut());
153 CPPUNIT_ASSERT_EQUAL_MESSAGE( sFailedMessage.getStr(), GRAPHIC_BITMAP, rGraphicObj.GetGraphic().GetType());
154 CPPUNIT_ASSERT_EQUAL_MESSAGE( sFailedMessage.getStr(), sal_uLong(864900), rGraphicObj.GetSizeBytes());
156 // Get the document controller
157 ScTabViewShell* pViewShell = xDocSh->GetBestViewShell(false);
158 CPPUNIT_ASSERT_MESSAGE( sFailedMessage.getStr(), pViewShell != NULL );
160 // Get the draw view of the document
161 ScDrawView* pDrawView = pViewShell->GetViewData().GetScDrawView();
162 CPPUNIT_ASSERT_MESSAGE( sFailedMessage.getStr(), pDrawView != NULL );
164 // Select graphic object
165 pDrawView->MarkNextObj(false);
166 CPPUNIT_ASSERT_MESSAGE( sFailedMessage.getStr(), pDrawView->AreObjectsMarked() );
168 // Set Cell Anchor
169 ScDrawLayer::SetCellAnchoredFromPosition(*pObject, rDoc, 0);
170 // Check state
171 ScAnchorType oldType = ScDrawLayer::GetAnchorType(*pObject);
172 CPPUNIT_ASSERT_MESSAGE( sFailedMessage.getStr(), oldType == SCA_CELL );
174 // Change all selected objects to page anchor
175 pViewShell->GetViewData().GetDispatcher().Execute(SID_ANCHOR_PAGE);
176 // Check state
177 ScAnchorType newType = ScDrawLayer::GetAnchorType(*pObject);
178 CPPUNIT_ASSERT_MESSAGE( sFailedMessage.getStr(), newType == SCA_PAGE );
180 // Undo and check its result.
181 SfxUndoManager* pUndoMgr = rDoc.GetUndoManager();
182 CPPUNIT_ASSERT(pUndoMgr);
183 pUndoMgr->Undo();
185 // Check anchor type
186 CPPUNIT_ASSERT_MESSAGE( sFailedMessage.getStr(), oldType == ScDrawLayer::GetAnchorType(*pObject) );
187 CPPUNIT_ASSERT_EQUAL_MESSAGE( sFailedMessage.getStr(), GRAPHIC_BITMAP, rGraphicObj.GetGraphic().GetType());
188 CPPUNIT_ASSERT_EQUAL_MESSAGE( sFailedMessage.getStr(), sal_uLong(864900), rGraphicObj.GetSizeBytes());
190 pUndoMgr->Redo();
192 // Check anchor type
193 CPPUNIT_ASSERT_MESSAGE( sFailedMessage.getStr(), newType == ScDrawLayer::GetAnchorType(*pObject) );
194 CPPUNIT_ASSERT_EQUAL_MESSAGE( sFailedMessage.getStr(), GRAPHIC_BITMAP, rGraphicObj.GetGraphic().GetType());
195 CPPUNIT_ASSERT_EQUAL_MESSAGE( sFailedMessage.getStr(), sal_uLong(864900), rGraphicObj.GetSizeBytes());
197 ScDrawLayer::SetPageAnchored(*pObject);
198 // Check state
199 oldType = ScDrawLayer::GetAnchorType(*pObject);
200 CPPUNIT_ASSERT_MESSAGE( sFailedMessage.getStr(), oldType == SCA_PAGE );
202 // Change all selected objects to cell anchor
203 pViewShell->GetViewData().GetDispatcher().Execute(SID_ANCHOR_CELL);
204 // Check state
205 newType = ScDrawLayer::GetAnchorType(*pObject);
206 CPPUNIT_ASSERT_MESSAGE( sFailedMessage.getStr(), newType == SCA_CELL );
208 pUndoMgr->Undo();
210 // Check anchor type
211 CPPUNIT_ASSERT_MESSAGE( sFailedMessage.getStr(), oldType == ScDrawLayer::GetAnchorType(*pObject) );
212 CPPUNIT_ASSERT_EQUAL_MESSAGE( sFailedMessage.getStr(), GRAPHIC_BITMAP, rGraphicObj.GetGraphic().GetType());
213 CPPUNIT_ASSERT_EQUAL_MESSAGE( sFailedMessage.getStr(), sal_uLong(864900), rGraphicObj.GetSizeBytes());
215 pUndoMgr->Redo();
217 // Check anchor type
218 CPPUNIT_ASSERT_MESSAGE( sFailedMessage.getStr(), newType == ScDrawLayer::GetAnchorType(*pObject) );
219 CPPUNIT_ASSERT_EQUAL_MESSAGE( sFailedMessage.getStr(), GRAPHIC_BITMAP, rGraphicObj.GetGraphic().GetType());
220 CPPUNIT_ASSERT_EQUAL_MESSAGE( sFailedMessage.getStr(), sal_uLong(864900), rGraphicObj.GetSizeBytes());
223 void ScConditionalFormatTest::setUp()
225 nTest++;
226 CalcUnoApiTest::setUp();
229 void ScConditionalFormatTest::tearDown()
231 if (nTest == NUMBER_OF_TESTS)
233 closeDocument(mxComponent);
234 mxComponent.clear();
237 CalcUnoApiTest::tearDown();
240 CPPUNIT_TEST_SUITE_REGISTRATION(ScConditionalFormatTest);
244 CPPUNIT_PLUGIN_IMPLEMENT();
246 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */