1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 "ucalc.hxx"
11 #include <editutil.hxx>
12 #include <cellvalue.hxx>
13 #include <editeng/editobj.hxx>
14 #include <svl/languageoptions.hxx>
15
testColumnFindEditCells()16 void Test::testColumnFindEditCells()
17 {
18 m_pDoc->InsertTab(0, "Test");
19
20 // Test the basics with real edit cells, using Column A.
21
22 SCROW nResRow = m_pDoc->GetFirstEditTextRow(ScRange(0,0,0,0,MAXROW,0));
23 CPPUNIT_ASSERT_EQUAL_MESSAGE("There should be no edit cells.", SCROW(-1), nResRow);
24 nResRow = m_pDoc->GetFirstEditTextRow(ScRange(0,0,0,0,0,0));
25 CPPUNIT_ASSERT_EQUAL_MESSAGE("There should be no edit cells.", SCROW(-1), nResRow);
26 nResRow = m_pDoc->GetFirstEditTextRow(ScRange(0,0,0,0,10,0));
27 CPPUNIT_ASSERT_EQUAL_MESSAGE("There should be no edit cells.", SCROW(-1), nResRow);
28
29 ScFieldEditEngine& rEE = m_pDoc->GetEditEngine();
30 rEE.SetText("Test");
31 m_pDoc->SetEditText(ScAddress(0,0,0), rEE.CreateTextObject());
32 const EditTextObject* pObj = m_pDoc->GetEditText(ScAddress(0,0,0));
33 CPPUNIT_ASSERT_MESSAGE("There should be an edit cell here.", pObj);
34
35 ScRange aRange(0,0,0,0,0,0);
36 nResRow = m_pDoc->GetFirstEditTextRow(aRange);
37 CPPUNIT_ASSERT_EQUAL_MESSAGE("There is an edit cell here.", SCROW(0), nResRow);
38
39 aRange.aStart.SetRow(1);
40 aRange.aEnd.SetRow(1);
41 nResRow = m_pDoc->GetFirstEditTextRow(aRange);
42 CPPUNIT_ASSERT_EQUAL_MESSAGE("There shouldn't be an edit cell in specified range.", SCROW(-1), nResRow);
43
44 aRange.aStart.SetRow(2);
45 aRange.aEnd.SetRow(4);
46 nResRow = m_pDoc->GetFirstEditTextRow(aRange);
47 CPPUNIT_ASSERT_EQUAL_MESSAGE("There shouldn't be an edit cell in specified range.", SCROW(-1), nResRow);
48
49 aRange.aStart.SetRow(0);
50 aRange.aEnd.SetRow(MAXROW);
51 nResRow = m_pDoc->GetFirstEditTextRow(aRange);
52 CPPUNIT_ASSERT_EQUAL_MESSAGE("There should be an edit cell in specified range.", SCROW(0), nResRow);
53
54 m_pDoc->SetString(ScAddress(0,0,0), "Test");
55 m_pDoc->SetValue(ScAddress(0,2,0), 1.0);
56 ScRefCellValue aCell;
57 aCell.assign(*m_pDoc, ScAddress(0,0,0));
58 CPPUNIT_ASSERT_EQUAL_MESSAGE("This should be a string cell.", CELLTYPE_STRING, aCell.meType);
59 aCell.assign(*m_pDoc, ScAddress(0,1,0));
60 CPPUNIT_ASSERT_EQUAL_MESSAGE("This should be an empty cell.", CELLTYPE_NONE, aCell.meType);
61 aCell.assign(*m_pDoc, ScAddress(0,2,0));
62 CPPUNIT_ASSERT_EQUAL_MESSAGE("This should be a numeric cell.", CELLTYPE_VALUE, aCell.meType);
63 aCell.assign(*m_pDoc, ScAddress(0,3,0));
64 CPPUNIT_ASSERT_EQUAL_MESSAGE("This should be an empty cell.", CELLTYPE_NONE, aCell.meType);
65
66 aRange.aStart.SetRow(1);
67 aRange.aEnd.SetRow(1);
68 nResRow = m_pDoc->GetFirstEditTextRow(aRange);
69 CPPUNIT_ASSERT_EQUAL_MESSAGE("There shouldn't be an edit cell in specified range.", SCROW(-1), nResRow);
70
71 // Test with non-edit cell but with ambiguous script type.
72
73 m_pDoc->SetString(ScAddress(1,11,0), "Some text");
74 m_pDoc->SetString(ScAddress(1,12,0), "Some text");
75 m_pDoc->SetString(ScAddress(1,13,0), "Other text");
76
77 m_pDoc->SetScriptType(ScAddress(1,11,0), (SvtScriptType::LATIN | SvtScriptType::ASIAN));
78 m_pDoc->SetScriptType(ScAddress(1,12,0), (SvtScriptType::LATIN | SvtScriptType::ASIAN));
79 m_pDoc->SetScriptType(ScAddress(1,13,0), (SvtScriptType::LATIN | SvtScriptType::ASIAN));
80
81 nResRow = m_pDoc->GetFirstEditTextRow(ScAddress(1,11,0));
82 CPPUNIT_ASSERT_EQUAL(static_cast<SCROW>(11), nResRow);
83 nResRow = m_pDoc->GetFirstEditTextRow(ScAddress(1,12,0));
84 CPPUNIT_ASSERT_EQUAL(static_cast<SCROW>(12), nResRow);
85
86 for (SCROW i = 0; i <= 5; ++i)
87 m_pDoc->SetString(ScAddress(2,i,0), "Text");
88
89 m_pDoc->SetScriptType(ScAddress(2,5,0), (SvtScriptType::LATIN | SvtScriptType::ASIAN));
90
91 nResRow = m_pDoc->GetFirstEditTextRow(ScAddress(2,1,0));
92 CPPUNIT_ASSERT_EQUAL(static_cast<SCROW>(-1), nResRow);
93
94 m_pDoc->DeleteTab(0);
95 }
96
97
testSetFormula()98 void Test::testSetFormula()
99 {
100 m_pDoc->InsertTab(0, "Test");
101
102 static struct aInputs
103 {
104 const char* aName;
105 SCROW const nRow;
106 SCCOL const nCol;
107 const char* aFormula1; // Represents the formula that is input to SetFormula function.
108 const char* aFormula2; // Represents the formula that is actually stored in the cell.
109 formula::FormulaGrammar::Grammar const eGram;
110
111 } const aTest[] = {
112 { "Rock and Roll" ,5 , 4 , "=SUM($D$2:$F$3)" ,"=SUM($D$2:$F$3)" , formula::FormulaGrammar::Grammar::GRAM_ENGLISH },
113 { "Blues" ,5 , 5 , "=A1-$C2+B$3-$F$4" ,"=A1-$C2+B$3-$F$4", formula::FormulaGrammar::Grammar::GRAM_NATIVE },
114 { "Acoustic" ,6 , 6 , "=A1-$C2+B$3-$F$4" ,"=A1-$C2+B$3-$F$4", formula::FormulaGrammar::Grammar::GRAM_NATIVE_XL_A1},
115 { "Nursery Rhymes",7 , 8 , "=[.A1]-[.$C2]+[.G$3]-[.$F$4]","=A1-$C2+G$3-$F$4", formula::FormulaGrammar::Grammar::GRAM_ODFF }
116 };
117
118 for(size_t i = 0; i < SAL_N_ELEMENTS(aTest); ++i)
119 {
120 OUString aBuffer;
121 m_pDoc->SetFormula(ScAddress(aTest[i].nCol, aTest[i].nRow, 0), OUString::createFromAscii(aTest[i].aFormula1), aTest[i].eGram);
122 m_pDoc->GetFormula(aTest[i].nCol, aTest[i].nRow, 0, aBuffer);
123
124 CPPUNIT_ASSERT_EQUAL_MESSAGE("Failed to set formula", OUString::createFromAscii(aTest[i].aFormula2), aBuffer);
125 }
126
127 m_pDoc->DeleteTab(0);
128 }
129
testMultipleDataCellsInRange()130 void Test::testMultipleDataCellsInRange()
131 {
132 m_pDoc->InsertTab(0, "Test");
133
134 ScRange aRange(1,2,0); // B3
135 sc::MultiDataCellState aState = m_pDoc->HasMultipleDataCells(aRange);
136 CPPUNIT_ASSERT_EQUAL(sc::MultiDataCellState::Empty, aState.meState);
137
138 // Set a numeric value to B3.
139 m_pDoc->SetValue(ScAddress(1,2,0), 1.0);
140 aState = m_pDoc->HasMultipleDataCells(aRange);
141 CPPUNIT_ASSERT_EQUAL(sc::MultiDataCellState::HasOneCell, aState.meState);
142 CPPUNIT_ASSERT_EQUAL(SCCOL(1), aState.mnCol1);
143 CPPUNIT_ASSERT_EQUAL(SCROW(2), aState.mnRow1);
144
145 // Set another numeric value to B4.
146 m_pDoc->SetValue(ScAddress(1,3,0), 2.0);
147 aRange.aEnd.SetRow(3); // B3:B4
148 aState = m_pDoc->HasMultipleDataCells(aRange);
149 CPPUNIT_ASSERT_EQUAL(sc::MultiDataCellState::HasMultipleCells, aState.meState);
150 CPPUNIT_ASSERT_EQUAL(SCCOL(1), aState.mnCol1);
151 CPPUNIT_ASSERT_EQUAL(SCROW(2), aState.mnRow1);
152
153 // Set the query range to B4:B5. Now it should only report one cell, with
154 // B4 being the first non-empty cell.
155 aRange.aStart.SetRow(3);
156 aRange.aEnd.SetRow(4);
157 aState = m_pDoc->HasMultipleDataCells(aRange);
158 CPPUNIT_ASSERT_EQUAL(sc::MultiDataCellState::HasOneCell, aState.meState);
159 CPPUNIT_ASSERT_EQUAL(SCCOL(1), aState.mnCol1);
160 CPPUNIT_ASSERT_EQUAL(SCROW(3), aState.mnRow1);
161
162 // Set the query range to A1:C3. The first non-empty cell should be B3.
163 aRange = ScRange(0,0,0,2,2,0);
164 aState = m_pDoc->HasMultipleDataCells(aRange);
165 CPPUNIT_ASSERT_EQUAL(sc::MultiDataCellState::HasOneCell, aState.meState);
166 CPPUNIT_ASSERT_EQUAL(SCCOL(1), aState.mnCol1);
167 CPPUNIT_ASSERT_EQUAL(SCROW(2), aState.mnRow1);
168
169 // Set string cells to D4 and F5, and query D3:F5. D4 should be the first
170 // non-empty cell.
171 m_pDoc->SetString(ScAddress(3,3,0), "foo");
172 m_pDoc->SetString(ScAddress(5,4,0), "bar");
173 aRange = ScRange(3,2,0,5,4,0);
174 aState = m_pDoc->HasMultipleDataCells(aRange);
175 CPPUNIT_ASSERT_EQUAL(sc::MultiDataCellState::HasMultipleCells, aState.meState);
176 CPPUNIT_ASSERT_EQUAL(SCCOL(3), aState.mnCol1);
177 CPPUNIT_ASSERT_EQUAL(SCROW(3), aState.mnRow1);
178
179 // TODO : add more test cases as needed.
180
181 m_pDoc->DeleteTab(0);
182 }
183
184 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
185