1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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/.
10 #include <test/table/xtablecolumns.hxx>
12 #include <com/sun/star/table/XCellRange.hpp>
13 #include <com/sun/star/table/XTableColumns.hpp>
14 #include <com/sun/star/text/XSimpleText.hpp>
15 #include <com/sun/star/uno/RuntimeException.hpp>
17 #include <com/sun/star/uno/Reference.hxx>
19 #include <cppunit/TestAssert.h>
25 void XTableColumns::testInsertByIndex()
27 uno::Reference
<table::XTableColumns
> xTC(init(), uno::UNO_QUERY_THROW
);
28 uno::Reference
<table::XCellRange
> xCR(m_xSheet
, uno::UNO_QUERY_THROW
);
30 // insert one column at position one
31 xTC
->insertByIndex(1, 1);
32 CPPUNIT_ASSERT_EQUAL(OUString("0a"), getCellText(xCR
->getCellByPosition(0, 0)));
33 CPPUNIT_ASSERT_EQUAL(OUString("0b"), getCellText(xCR
->getCellByPosition(0, 1)));
34 CPPUNIT_ASSERT_EQUAL(OUString(""), getCellText(xCR
->getCellByPosition(1, 0)));
35 CPPUNIT_ASSERT_EQUAL(OUString(""), getCellText(xCR
->getCellByPosition(1, 1)));
36 CPPUNIT_ASSERT_EQUAL(OUString("1a"), getCellText(xCR
->getCellByPosition(2, 0)));
37 CPPUNIT_ASSERT_EQUAL(OUString("1b"), getCellText(xCR
->getCellByPosition(2, 1)));
38 CPPUNIT_ASSERT_EQUAL(OUString("2a"), getCellText(xCR
->getCellByPosition(3, 0)));
39 CPPUNIT_ASSERT_EQUAL(OUString("2b"), getCellText(xCR
->getCellByPosition(3, 1)));
40 CPPUNIT_ASSERT_EQUAL(OUString(""), getCellText(xCR
->getCellByPosition(4, 0)));
41 CPPUNIT_ASSERT_EQUAL(OUString(""), getCellText(xCR
->getCellByPosition(4, 1)));
43 // insert one column at position zero
44 xTC
->insertByIndex(0, 1);
45 CPPUNIT_ASSERT_EQUAL(OUString(""), getCellText(xCR
->getCellByPosition(0, 0)));
46 CPPUNIT_ASSERT_EQUAL(OUString(""), getCellText(xCR
->getCellByPosition(0, 1)));
47 CPPUNIT_ASSERT_EQUAL(OUString("0a"), getCellText(xCR
->getCellByPosition(1, 0)));
48 CPPUNIT_ASSERT_EQUAL(OUString("0b"), getCellText(xCR
->getCellByPosition(1, 1)));
49 CPPUNIT_ASSERT_EQUAL(OUString(""), getCellText(xCR
->getCellByPosition(2, 0)));
50 CPPUNIT_ASSERT_EQUAL(OUString(""), getCellText(xCR
->getCellByPosition(2, 1)));
51 CPPUNIT_ASSERT_EQUAL(OUString("1a"), getCellText(xCR
->getCellByPosition(3, 0)));
52 CPPUNIT_ASSERT_EQUAL(OUString("1b"), getCellText(xCR
->getCellByPosition(3, 1)));
53 CPPUNIT_ASSERT_EQUAL(OUString("2a"), getCellText(xCR
->getCellByPosition(4, 0)));
54 CPPUNIT_ASSERT_EQUAL(OUString("2b"), getCellText(xCR
->getCellByPosition(4, 1)));
55 CPPUNIT_ASSERT_EQUAL(OUString(""), getCellText(xCR
->getCellByPosition(5, 0)));
56 CPPUNIT_ASSERT_EQUAL(OUString(""), getCellText(xCR
->getCellByPosition(5, 1)));
58 // insert two columns at position zero
59 xTC
->insertByIndex(0, 2);
60 CPPUNIT_ASSERT_EQUAL(OUString(""), getCellText(xCR
->getCellByPosition(0, 0)));
61 CPPUNIT_ASSERT_EQUAL(OUString(""), getCellText(xCR
->getCellByPosition(0, 1)));
62 CPPUNIT_ASSERT_EQUAL(OUString(""), getCellText(xCR
->getCellByPosition(1, 0)));
63 CPPUNIT_ASSERT_EQUAL(OUString(""), getCellText(xCR
->getCellByPosition(1, 1)));
64 CPPUNIT_ASSERT_EQUAL(OUString(""), getCellText(xCR
->getCellByPosition(2, 0)));
65 CPPUNIT_ASSERT_EQUAL(OUString(""), getCellText(xCR
->getCellByPosition(2, 1)));
66 CPPUNIT_ASSERT_EQUAL(OUString("0a"), getCellText(xCR
->getCellByPosition(3, 0)));
67 CPPUNIT_ASSERT_EQUAL(OUString("0b"), getCellText(xCR
->getCellByPosition(3, 1)));
68 CPPUNIT_ASSERT_EQUAL(OUString(""), getCellText(xCR
->getCellByPosition(4, 0)));
69 CPPUNIT_ASSERT_EQUAL(OUString(""), getCellText(xCR
->getCellByPosition(4, 1)));
70 CPPUNIT_ASSERT_EQUAL(OUString("1a"), getCellText(xCR
->getCellByPosition(5, 0)));
71 CPPUNIT_ASSERT_EQUAL(OUString("1b"), getCellText(xCR
->getCellByPosition(5, 1)));
72 CPPUNIT_ASSERT_EQUAL(OUString("2a"), getCellText(xCR
->getCellByPosition(6, 0)));
73 CPPUNIT_ASSERT_EQUAL(OUString("2b"), getCellText(xCR
->getCellByPosition(6, 1)));
74 CPPUNIT_ASSERT_EQUAL(OUString(""), getCellText(xCR
->getCellByPosition(7, 0)));
75 CPPUNIT_ASSERT_EQUAL(OUString(""), getCellText(xCR
->getCellByPosition(7, 1)));
78 void XTableColumns::testInsertByIndexWithNegativeIndex()
80 uno::Reference
<table::XTableColumns
> xTC(init(), uno::UNO_QUERY_THROW
);
81 CPPUNIT_ASSERT_THROW(xTC
->insertByIndex(-1, 1), uno::RuntimeException
);
84 void XTableColumns::testInsertByIndexWithNoColumn()
86 uno::Reference
<table::XTableColumns
> xTC(init(), uno::UNO_QUERY_THROW
);
87 CPPUNIT_ASSERT_THROW(xTC
->insertByIndex(0, 0), uno::RuntimeException
);
90 void XTableColumns::testInsertByIndexWithOutOfBoundIndex()
92 uno::Reference
<table::XTableColumns
> xTC(init(), uno::UNO_QUERY_THROW
);
94 CPPUNIT_ASSERT_THROW(xTC
->insertByIndex(xTC
->getCount(), 1), uno::RuntimeException
);
97 void XTableColumns::testRemoveByIndex()
99 uno::Reference
<table::XTableColumns
> xTC(init(), uno::UNO_QUERY_THROW
);
100 uno::Reference
<table::XCellRange
> xCR(m_xSheet
, uno::UNO_QUERY_THROW
);
102 xTC
->insertByIndex(1, 1); // insert one column at position one
103 xTC
->insertByIndex(0, 1); // insert one column at position zero
104 xTC
->insertByIndex(0, 2); // insert two columns at position zero
106 // remove two columns at position zero
107 xTC
->removeByIndex(0, 2);
108 CPPUNIT_ASSERT_EQUAL(OUString(""), getCellText(xCR
->getCellByPosition(0, 0)));
109 CPPUNIT_ASSERT_EQUAL(OUString(""), getCellText(xCR
->getCellByPosition(0, 1)));
110 CPPUNIT_ASSERT_EQUAL(OUString("0a"), getCellText(xCR
->getCellByPosition(1, 0)));
111 CPPUNIT_ASSERT_EQUAL(OUString("0b"), getCellText(xCR
->getCellByPosition(1, 1)));
112 CPPUNIT_ASSERT_EQUAL(OUString(""), getCellText(xCR
->getCellByPosition(2, 0)));
113 CPPUNIT_ASSERT_EQUAL(OUString(""), getCellText(xCR
->getCellByPosition(2, 1)));
114 CPPUNIT_ASSERT_EQUAL(OUString("1a"), getCellText(xCR
->getCellByPosition(3, 0)));
115 CPPUNIT_ASSERT_EQUAL(OUString("1b"), getCellText(xCR
->getCellByPosition(3, 1)));
116 CPPUNIT_ASSERT_EQUAL(OUString("2a"), getCellText(xCR
->getCellByPosition(4, 0)));
117 CPPUNIT_ASSERT_EQUAL(OUString("2b"), getCellText(xCR
->getCellByPosition(4, 1)));
118 CPPUNIT_ASSERT_EQUAL(OUString(""), getCellText(xCR
->getCellByPosition(5, 0)));
119 CPPUNIT_ASSERT_EQUAL(OUString(""), getCellText(xCR
->getCellByPosition(5, 1)));
121 // remove one column at position zero
122 xTC
->removeByIndex(0, 1);
123 CPPUNIT_ASSERT_EQUAL(OUString("0a"), getCellText(xCR
->getCellByPosition(0, 0)));
124 CPPUNIT_ASSERT_EQUAL(OUString("0b"), getCellText(xCR
->getCellByPosition(0, 1)));
125 CPPUNIT_ASSERT_EQUAL(OUString(""), getCellText(xCR
->getCellByPosition(1, 0)));
126 CPPUNIT_ASSERT_EQUAL(OUString(""), getCellText(xCR
->getCellByPosition(1, 1)));
127 CPPUNIT_ASSERT_EQUAL(OUString("1a"), getCellText(xCR
->getCellByPosition(2, 0)));
128 CPPUNIT_ASSERT_EQUAL(OUString("1b"), getCellText(xCR
->getCellByPosition(2, 1)));
129 CPPUNIT_ASSERT_EQUAL(OUString("2a"), getCellText(xCR
->getCellByPosition(3, 0)));
130 CPPUNIT_ASSERT_EQUAL(OUString("2b"), getCellText(xCR
->getCellByPosition(3, 1)));
131 CPPUNIT_ASSERT_EQUAL(OUString(""), getCellText(xCR
->getCellByPosition(4, 0)));
132 CPPUNIT_ASSERT_EQUAL(OUString(""), getCellText(xCR
->getCellByPosition(4, 1)));
134 // remove one column at position one
135 xTC
->removeByIndex(1, 1);
136 CPPUNIT_ASSERT_EQUAL(OUString("0a"), getCellText(xCR
->getCellByPosition(0, 0)));
137 CPPUNIT_ASSERT_EQUAL(OUString("0b"), getCellText(xCR
->getCellByPosition(0, 1)));
138 CPPUNIT_ASSERT_EQUAL(OUString("1a"), getCellText(xCR
->getCellByPosition(1, 0)));
139 CPPUNIT_ASSERT_EQUAL(OUString("1b"), getCellText(xCR
->getCellByPosition(1, 1)));
140 CPPUNIT_ASSERT_EQUAL(OUString("2a"), getCellText(xCR
->getCellByPosition(2, 0)));
141 CPPUNIT_ASSERT_EQUAL(OUString("2b"), getCellText(xCR
->getCellByPosition(2, 1)));
142 CPPUNIT_ASSERT_EQUAL(OUString(""), getCellText(xCR
->getCellByPosition(3, 0)));
143 CPPUNIT_ASSERT_EQUAL(OUString(""), getCellText(xCR
->getCellByPosition(3, 1)));
146 void XTableColumns::testRemoveByIndexWithNegativeIndex()
148 uno::Reference
<table::XTableColumns
> xTC(init(), uno::UNO_QUERY_THROW
);
149 CPPUNIT_ASSERT_THROW(xTC
->removeByIndex(-1, 1), uno::RuntimeException
);
152 void XTableColumns::testRemoveByIndexWithNoColumn()
154 uno::Reference
<table::XTableColumns
> xTC(init(), uno::UNO_QUERY_THROW
);
155 CPPUNIT_ASSERT_THROW(xTC
->removeByIndex(0, 0), uno::RuntimeException
);
158 void XTableColumns::testRemoveByIndexWithOutOfBoundIndex()
160 uno::Reference
<table::XTableColumns
> xTC(init(), uno::UNO_QUERY_THROW
);
162 CPPUNIT_ASSERT_THROW(xTC
->removeByIndex(xTC
->getCount(), 1), uno::RuntimeException
);
165 OUString
XTableColumns::getCellText(const uno::Reference
<table::XCell
>& r_xCell
)
167 uno::Reference
<text::XSimpleText
> xST(r_xCell
, uno::UNO_QUERY_THROW
);
168 return xST
->getString();
171 } // namespace apitest
173 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */