LibreOffice Module test (master)  1
xfunctiondescriptions.cxx
Go to the documentation of this file.
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 <random>
11 
13 
14 #include <com/sun/star/beans/PropertyValue.hpp>
15 #include <com/sun/star/sheet/XFunctionDescriptions.hpp>
16 #include <com/sun/star/lang/IllegalArgumentException.hpp>
17 #include <com/sun/star/uno/Reference.hxx>
18 
19 #include <cppunit/TestAssert.h>
20 
21 using namespace css;
22 using namespace com::sun::star;
23 using namespace com::sun::star::uno;
24 
25 namespace apitest
26 {
27 void XFunctionDescriptions::testGetById()
28 {
29  uno::Reference<sheet::XFunctionDescriptions> xFD(init(), UNO_QUERY_THROW);
30 
31  const sal_Int32 nCount = xFD->getCount();
32  CPPUNIT_ASSERT_MESSAGE("No FunctionDescriptions available", 0 < nCount);
33 
34  // first grab a random function descriptions
35  std::random_device rd;
36  std::mt19937 gen(rd());
37  std::uniform_int_distribution<> distr(0, nCount - 1);
38  int nNumber = distr(gen);
39 
40  sal_Int32 aId1 = 0;
41  OUString aName1;
42  uno::Sequence<beans::PropertyValue> aProps1;
43  CPPUNIT_ASSERT(xFD->getByIndex(nNumber) >>= aProps1);
44  for (const auto& aProp : std::as_const(aProps1))
45  {
46  if (aProp.Name == "Id")
47  aId1 = aProp.Value.get<sal_Int32>();
48  if (aProp.Name == "Name")
49  aName1 = aProp.Value.get<OUString>();
50  }
51 
52  // fetch the same descriptions by its id
53  sal_Int32 aId2 = 0;
54  OUString aName2;
55  const uno::Sequence<beans::PropertyValue> aProps2 = xFD->getById(aId1);
56  CPPUNIT_ASSERT_MESSAGE("Received empty FunctionDescriptions from getById()",
57  aProps2.hasElements());
58  for (const auto& aProp : aProps2)
59  {
60  if (aProp.Name == "Id")
61  aId2 = aProp.Value.get<sal_Int32>();
62  if (aProp.Name == "Name")
63  aName2 = aProp.Value.get<OUString>();
64  }
65  CPPUNIT_ASSERT_EQUAL_MESSAGE("Received wrong FunctionDescriptions (Id)", aId1, aId2);
66  CPPUNIT_ASSERT_EQUAL_MESSAGE("Received wrong FunctionDescriptions (Name)", aName1, aName2);
67 
68  CPPUNIT_ASSERT_THROW_MESSAGE("No IllegalArgumentException thrown", xFD->getById(-1),
69  css::lang::IllegalArgumentException);
70 }
71 }
72 
73 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
int nCount
FILE * init(int, char **)