File indexing completed on 2024-05-12 16:35:58

0001 /* This file is part of the KDE project
0002    Copyright 2009 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "TestSelection.h"
0021 
0022 #include <QTest>
0023 
0024 #include "part/Canvas.h"
0025 #include "Map.h"
0026 #include "ui/Selection.h"
0027 #include "Sheet.h"
0028 
0029 using namespace Calligra::Sheets;
0030 
0031 void TestSelection::initialize()
0032 {
0033     Map map;
0034     Sheet* sheet1 = new Sheet(&map, "Sheet1");
0035     map.addSheet(sheet1);
0036     Sheet* sheet2 = new Sheet(&map, "Sheet2");
0037     map.addSheet(sheet2);
0038     Canvas canvas(0);
0039     Selection selection(&canvas);
0040     selection.setActiveSheet(sheet1);
0041 
0042     QVERIFY(!selection.isEmpty());
0043     QCOMPARE(selection.name(), QString("A1"));
0044 
0045     // Clearing in normal selection mode, results in A1 as residuum.
0046     selection.clear();
0047     QVERIFY(!selection.isEmpty());
0048     QCOMPARE(selection.name(), QString("Sheet1!A1"));
0049 
0050     selection.initialize(QPoint(2, 4), sheet2);
0051     QCOMPARE(selection.name(), QString("Sheet2!B4"));
0052 
0053     selection.initialize(QRect(3, 5, 2, 3));
0054     QCOMPARE(selection.name(), QString("Sheet1!C5:D7"));
0055 
0056     Region region("A1:A4", &map, sheet2);
0057     selection.initialize(region);
0058     QCOMPARE(selection.name(), QString("Sheet2!A1:A4"));
0059 
0060     region = Region("Sheet1!A2:A4", &map, sheet2);
0061     selection.initialize(region);
0062     QCOMPARE(selection.name(), QString("Sheet1!A2:A4"));
0063 
0064     // reference mode:
0065     selection.startReferenceSelection();
0066 
0067     region = Region("Sheet1!A2:A4;B3;C5", &map, sheet2);
0068     selection.initialize(region);
0069     QCOMPARE(selection.name(), QString("A2:A4;Sheet2!B3;Sheet2!C5"));
0070     QCOMPARE(selection.activeSubRegionName(), QString("A2:A4;Sheet2!B3;Sheet2!C5"));
0071 
0072     selection.setActiveSubRegion(1, 1);
0073     QCOMPARE(selection.activeSubRegionName(), QString("Sheet2!B3"));
0074     selection.initialize(QPoint(2, 2));
0075     QCOMPARE(selection.name(), QString("A2:A4;B2;Sheet2!C5"));
0076     QCOMPARE(selection.activeSubRegionName(), QString("B2"));
0077 
0078     selection.setActiveSubRegion(1, 2, 2);
0079     QCOMPARE(selection.activeSubRegionName(), QString("B2;Sheet2!C5"));
0080     selection.initialize(QPoint(3, 3), sheet2);
0081     QCOMPARE(selection.name(), QString("A2:A4;Sheet2!C3"));
0082     QCOMPARE(selection.activeSubRegionName(), QString("Sheet2!C3"));
0083 
0084     selection.setActiveSubRegion(1, 0, 2);
0085     QCOMPARE(selection.activeSubRegionName(), QString());
0086     selection.initialize(QPoint(4, 4), sheet2);
0087     QCOMPARE(selection.name(), QString("A2:A4;Sheet2!D4;Sheet2!C3"));
0088     QCOMPARE(selection.activeSubRegionName(), QString("Sheet2!D4"));
0089 
0090     selection.setActiveSubRegion(1, 1, 2);
0091     QCOMPARE(selection.activeSubRegionName(), QString("Sheet2!D4"));
0092     selection.clearSubRegion();
0093     QCOMPARE(selection.name(), QString("A2:A4;Sheet2!C3"));
0094     QCOMPARE(selection.activeSubRegionName(), QString());
0095 
0096     // Two appendings.
0097     selection.clear();
0098     selection.setActiveSubRegion(0, 0, 0);
0099     QCOMPARE(selection.activeSubRegionName(), QString());
0100     selection.initialize(QPoint(1, 1), sheet1);
0101     QCOMPARE(selection.activeSubRegionName(), QString("A1"));
0102     selection.setActiveSubRegion(1, 0, 1);
0103     QCOMPARE(selection.activeSubRegionName(), QString());
0104     selection.initialize(QPoint(2, 2), sheet1);
0105     QCOMPARE(selection.name(), QString("A1;B2"));
0106     QCOMPARE(selection.activeSubRegionName(), QString("B2"));
0107 }
0108 
0109 void TestSelection::update()
0110 {
0111     Map map;
0112     Sheet *sheet1 = new Sheet(&map, "Sheet1");
0113     map.addSheet(sheet1);
0114     Sheet *sheet2 = new Sheet(&map, "Sheet2");
0115     map.addSheet(sheet2);
0116     Canvas canvas(0);
0117     Selection selection(&canvas);
0118     selection.setActiveSheet(sheet1);
0119 
0120     QVERIFY(!selection.isEmpty());
0121     QCOMPARE(selection.name(), QString("A1"));
0122 
0123     // Selection::update(const QPoint&) works always on the active sheet.
0124 
0125     // normal mode:
0126 
0127     // init with location
0128     selection.initialize(QPoint(1, 6), sheet1);
0129     QCOMPARE(selection.name(), QString("Sheet1!A6"));
0130 
0131     // init with region
0132     selection.initialize(Region("A3", &map, sheet1));
0133     QCOMPARE(selection.name(), QString("Sheet1!A3"));
0134 
0135     // init with location after init with region
0136     selection.initialize(QPoint(1, 5), sheet1);
0137     QCOMPARE(selection.name(), QString("Sheet1!A5"));
0138 
0139     // TODO
0140 
0141     // reference mode:
0142     selection.startReferenceSelection();
0143 
0144     Region region("Sheet1!A2:A4;B3;C5", &map, sheet2);
0145     selection.initialize(region);
0146     QCOMPARE(selection.name(), QString("A2:A4;Sheet2!B3;Sheet2!C5"));
0147 
0148     // Prepend new range.
0149     selection.setActiveSubRegion(0, 0);
0150     QCOMPARE(selection.activeSubRegionName(), QString());
0151     selection.update(QPoint(1, 1));
0152     QCOMPARE(selection.name(), QString("A1;A2:A4;Sheet2!B3;Sheet2!C5"));
0153 
0154     // Insert new range on active sheet.
0155     selection.setActiveSubRegion(2, 0);
0156     QCOMPARE(selection.activeSubRegionName(), QString());
0157     selection.update(QPoint(1, 1));
0158     QCOMPARE(selection.name(), QString("A1;A2:A4;A1;Sheet2!B3;Sheet2!C5"));
0159 
0160     // Append new range.
0161     selection.setActiveSubRegion(5, 0);
0162     QCOMPARE(selection.activeSubRegionName(), QString());
0163     selection.update(QPoint(1, 1));
0164     QCOMPARE(selection.name(), QString("A1;A2:A4;A1;Sheet2!B3;Sheet2!C5;A1"));
0165 
0166     // Update range.
0167     selection.setActiveSubRegion(2, 2);
0168     QCOMPARE(selection.activeSubRegionName(), QString("A1;Sheet2!B3"));
0169     selection.update(QPoint(2, 2));
0170     QCOMPARE(selection.name(), QString("A1;A2:A4;A1:B2;Sheet2!B3;Sheet2!C5;A1"));
0171 
0172     // Try to update range on non-active sheet. Inserts location on active sheet.
0173     selection.setActiveSubRegion(2, 2, 3);
0174     QCOMPARE(selection.activeSubRegionName(), QString("A1:B2;Sheet2!B3"));
0175     selection.update(QPoint(2, 2));
0176     QCOMPARE(selection.name(), QString("A1;A2:A4;A1:B2;Sheet2!B3;B2;Sheet2!C5;A1"));
0177 }
0178 
0179 void TestSelection::extend()
0180 {
0181     Map map;
0182     Sheet *sheet1 = new Sheet(&map, "Sheet1");
0183     map.addSheet(sheet1);
0184     Sheet *sheet2 = new Sheet(&map, "Sheet2");
0185     map.addSheet(sheet2);
0186     Canvas canvas(0);
0187     Selection selection(&canvas);
0188     selection.setActiveSheet(sheet1);
0189 
0190     QVERIFY(!selection.isEmpty());
0191     QCOMPARE(selection.name(), QString("A1"));
0192 
0193     // normal mode:
0194     // TODO
0195 
0196     // reference mode:
0197     selection.startReferenceSelection();
0198 
0199     Region region("Sheet1!A2:A4;B3;C5", &map, sheet2);
0200     selection.initialize(region);
0201     QCOMPARE(selection.name(), QString("A2:A4;Sheet2!B3;Sheet2!C5"));
0202 
0203     // Prepend new location.
0204     selection.setActiveSubRegion(0, 0);
0205     QCOMPARE(selection.activeSubRegionName(), QString());
0206     selection.extend(QPoint(1, 1), sheet2);
0207     QCOMPARE(selection.name(), QString("Sheet2!A1;A2:A4;Sheet2!B3;Sheet2!C5"));
0208     QCOMPARE(selection.activeSubRegionName(), QString("Sheet2!A1"));
0209 
0210     // Try to prepend new location. Prepending needs length = 0.
0211     selection.setActiveSubRegion(0, 2); //  results in: 0, 2, 0
0212     QCOMPARE(selection.activeSubRegionName(), QString("Sheet2!A1;A2:A4"));
0213     selection.extend(QPoint(1, 1), sheet1);
0214     QCOMPARE(selection.name(), QString("Sheet2!A1;A1;A2:A4;Sheet2!B3;Sheet2!C5"));
0215     QCOMPARE(selection.activeSubRegionName(), QString("Sheet2!A1;A1;A2:A4"));
0216 
0217     // Prepend new range.
0218     selection.setActiveSubRegion(0, 0);
0219     QCOMPARE(selection.activeSubRegionName(), QString());
0220     selection.extend(QRect(1, 1, 2, 2), sheet1);
0221     QCOMPARE(selection.name(), QString("A1:B2;Sheet2!A1;A1;A2:A4;Sheet2!B3;Sheet2!C5"));
0222     QCOMPARE(selection.activeSubRegionName(), QString("A1:B2"));
0223 
0224     // Try to prepend new range. Prepending needs length = 0.
0225     selection.setActiveSubRegion(0, 3); //  results in: 0, 3, 0
0226     QCOMPARE(selection.activeSubRegionName(), QString("A1:B2;Sheet2!A1;A1"));
0227     selection.extend(QRect(1, 2, 2, 2), sheet2);
0228     QCOMPARE(selection.name(), QString("A1:B2;Sheet2!A2:B3;Sheet2!A1;A1;A2:A4;Sheet2!B3;Sheet2!C5"));
0229     QCOMPARE(selection.activeSubRegionName(), QString("A1:B2;Sheet2!A2:B3;Sheet2!A1;A1"));
0230 
0231     selection.clear();
0232     QVERIFY(selection.isEmpty());
0233     selection.initialize(region);
0234     QCOMPARE(selection.name(), QString("A2:A4;Sheet2!B3;Sheet2!C5"));
0235 
0236     // Append new location.
0237     selection.setActiveSubRegion(1, 3, 3); // results in: 1, 2, 3
0238     QCOMPARE(selection.activeSubRegionName(), QString("Sheet2!B3;Sheet2!C5"));
0239     selection.extend(QPoint(1, 1), sheet2);
0240     QCOMPARE(selection.name(), QString("A2:A4;Sheet2!B3;Sheet2!C5;Sheet2!A1"));
0241     QCOMPARE(selection.activeSubRegionName(), QString("Sheet2!B3;Sheet2!C5;Sheet2!A1"));
0242 
0243     // Append new location.
0244     selection.setActiveSubRegion(4, 1, 3); // results in: 4, 0, 4
0245     QCOMPARE(selection.activeSubRegionName(), QString());
0246     selection.extend(QPoint(1, 1), sheet1);
0247     QCOMPARE(selection.name(), QString("A2:A4;Sheet2!B3;Sheet2!C5;Sheet2!A1;A1"));
0248     QCOMPARE(selection.activeSubRegionName(), QString("A1"));
0249 
0250     // Append new range.
0251     selection.setActiveSubRegion(5, 0); // results in: 5, 0, 5
0252     QCOMPARE(selection.activeSubRegionName(), QString());
0253     selection.extend(QRect(1, 1, 2, 2), sheet1);
0254     QCOMPARE(selection.name(), QString("A2:A4;Sheet2!B3;Sheet2!C5;Sheet2!A1;A1;A1:B2"));
0255     QCOMPARE(selection.activeSubRegionName(), QString("A1:B2"));
0256 
0257     // Append new range.
0258     selection.setActiveSubRegion(5, 1, 6);
0259     QCOMPARE(selection.activeSubRegionName(), QString("A1:B2"));
0260     selection.extend(QRect(1, 2, 2, 2), sheet2);
0261     QCOMPARE(selection.name(), QString("A2:A4;Sheet2!B3;Sheet2!C5;Sheet2!A1;A1;A1:B2;Sheet2!A2:B3"));
0262     QCOMPARE(selection.activeSubRegionName(), QString("A1:B2;Sheet2!A2:B3"));
0263 
0264     selection.clear();
0265     QVERIFY(selection.isEmpty());
0266     selection.initialize(region);
0267     QCOMPARE(selection.name(), QString("A2:A4;Sheet2!B3;Sheet2!C5"));
0268 
0269     // Insert new location.
0270     selection.setActiveSubRegion(0, 3, 1);
0271     QCOMPARE(selection.activeSubRegionName(), QString("A2:A4;Sheet2!B3;Sheet2!C5"));
0272     selection.extend(QPoint(1, 1), sheet2);
0273     QCOMPARE(selection.name(), QString("A2:A4;Sheet2!B3;Sheet2!A1;Sheet2!C5"));
0274     QCOMPARE(selection.activeSubRegionName(), QString("A2:A4;Sheet2!B3;Sheet2!A1;Sheet2!C5"));
0275 
0276     // Insert new range.
0277     selection.setActiveSubRegion(1, 3, 3);
0278     QCOMPARE(selection.activeSubRegionName(), QString("Sheet2!B3;Sheet2!A1;Sheet2!C5"));
0279     selection.extend(QRect(1, 1, 2, 2), sheet1);
0280     QCOMPARE(selection.name(), QString("A2:A4;Sheet2!B3;Sheet2!A1;Sheet2!C5;A1:B2"));
0281     QCOMPARE(selection.activeSubRegionName(), QString("Sheet2!B3;Sheet2!A1;Sheet2!C5;A1:B2"));
0282 }
0283 
0284 void TestSelection::activeElement()
0285 {
0286     Map map;
0287     Sheet *sheet1 = new Sheet(&map, "Sheet1");
0288     map.addSheet(sheet1);
0289     Sheet *sheet2 = new Sheet(&map, "Sheet2");
0290     map.addSheet(sheet2);
0291     Canvas canvas(0);
0292     Selection selection(&canvas);
0293     selection.setActiveSheet(sheet1);
0294 
0295     QVERIFY(!selection.isEmpty());
0296     QCOMPARE(selection.name(), QString("A1"));
0297     QVERIFY(!selection.activeElement());
0298 
0299     // normal mode:
0300 
0301     //BEGIN Leave the active element unset.
0302     selection.startReferenceSelection();
0303     QVERIFY(!selection.activeElement());
0304 
0305     selection.initialize(Region("A3:A4;B2;C4:D5", &map, sheet1));
0306     QCOMPARE(selection.name(sheet1), QString("A3:A4;B2;C4:D5"));
0307     QVERIFY(!selection.activeElement());
0308 
0309     selection.extend(QRect(1, 1, 4, 4), sheet1);
0310     QCOMPARE(selection.name(sheet1), QString("A3:A4;B2;C4:D5;A1:D4"));
0311     QVERIFY(!selection.activeElement());
0312 
0313     selection.update(QPoint(5, 5));
0314     QCOMPARE(selection.name(sheet1), QString("A3:A4;B2;C4:D5;A1:E5"));
0315     QVERIFY(!selection.activeElement());
0316 
0317     selection.initialize(QPoint(5, 6), sheet1);
0318     QCOMPARE(selection.name(sheet1), QString("E6"));
0319     QVERIFY(!selection.activeElement());
0320 
0321     selection.initialize(QRect(1, 1, 2, 1), sheet1);
0322     QCOMPARE(selection.name(sheet1), QString("A1:B1"));
0323     QVERIFY(!selection.activeElement());
0324 
0325     selection.extend(QPoint(1, 3), sheet1);
0326     QCOMPARE(selection.name(sheet1), QString("A1:B1;A3"));
0327     QVERIFY(!selection.activeElement());
0328     //END Leave the active element unset.
0329 }
0330 
0331 void TestSelection::referenceSelectionMode()
0332 {
0333     Map map;
0334     Sheet *sheet1 = new Sheet(&map, "Sheet1");
0335     map.addSheet(sheet1);
0336     Sheet *sheet2 = new Sheet(&map, "Sheet2");
0337     map.addSheet(sheet2);
0338     Canvas canvas(0);
0339     Selection selection(&canvas);
0340     selection.setActiveSheet(sheet1);
0341 
0342     QVERIFY(!selection.isEmpty());
0343     QCOMPARE(selection.name(), QString("A1"));
0344 
0345     // reference mode:
0346     selection.startReferenceSelection();
0347     QVERIFY(selection.isEmpty());
0348 
0349     selection.initialize(QPoint(2, 2), sheet2);
0350     QCOMPARE(selection.name(), QString("Sheet2!B2"));
0351 
0352     selection.extend(QPoint(1, 2));
0353     QCOMPARE(selection.name(), QString("Sheet2!B2;A2"));
0354 
0355     selection.setActiveSubRegion(1, 1, 1);
0356     QCOMPARE(selection.activeSubRegionName(), QString("A2"));
0357 
0358     selection.endReferenceSelection();
0359     QCOMPARE(selection.name(), QString("A1"));
0360 
0361     selection.initialize(QPoint(2, 2), sheet1);
0362     QCOMPARE(selection.name(), QString("B2"));
0363 
0364     // quit it again
0365     selection.endReferenceSelection();
0366     QCOMPARE(selection.name(), QString("B2"));
0367 
0368     selection.initialize(QRect(2, 2, 2, 2));
0369     QCOMPARE(selection.name(), QString("B2:C3"));
0370 
0371     // start it again
0372     selection.startReferenceSelection();
0373     QVERIFY(selection.isEmpty());
0374 
0375     selection.initialize(Region("A3", &map, sheet1));
0376     QCOMPARE(selection.name(), QString("A3"));
0377 
0378     // set the active sub-region beyond the last range
0379     selection.setActiveSubRegion(1, 0, 1);
0380     QCOMPARE(selection.activeSubRegionName(), QString());
0381 
0382     // quit it again
0383     selection.endReferenceSelection();
0384     QCOMPARE(selection.name(), QString("B2:C3"));
0385 }
0386 
0387 void TestSelection::covering()
0388 {
0389     Map map;
0390     Sheet *sheet1 = new Sheet(&map, "Sheet1");
0391     map.addSheet(sheet1);
0392     Sheet *sheet2 = new Sheet(&map, "Sheet2");
0393     map.addSheet(sheet2);
0394     Canvas canvas(0);
0395     Selection selection(&canvas);
0396     selection.setActiveSheet(sheet1);
0397 
0398     QVERIFY(!selection.isEmpty());
0399     QCOMPARE(selection.name(), QString("A1"));
0400 
0401     // normal mode:
0402     // covered ranges get removed
0403 
0404     selection.initialize(Region("A3:A4;B2;C4:D5", &map, sheet1));
0405     QCOMPARE(selection.name(sheet1), QString("A3:A4;B2;C4:D5"));
0406 
0407     selection.extend(QRect(1, 1, 4, 4), sheet1);
0408     QCOMPARE(selection.name(sheet1), QString("C4:D5;A1:D4"));
0409 
0410     selection.update(QPoint(5, 5));
0411     QCOMPARE(selection.name(sheet1), QString("A1:E5"));
0412 
0413     // reference mode:
0414     // covered ranges get ignored
0415 
0416     selection.startReferenceSelection();
0417 
0418     selection.initialize(Region("A3:A4;B2;C4:D5", &map, sheet1));
0419     QCOMPARE(selection.name(), QString("A3:A4;B2;C4:D5"));
0420 
0421     selection.extend(QRect(1, 1, 4, 4), sheet1);
0422     QCOMPARE(selection.name(), QString("A3:A4;B2;C4:D5;A1:D4"));
0423 
0424     selection.update(QPoint(5, 5));
0425     QCOMPARE(selection.name(sheet1), QString("A3:A4;B2;C4:D5;A1:E5"));
0426 }
0427 
0428 void TestSelection::splitting()
0429 {
0430     Map map;
0431     Sheet *sheet1 = new Sheet(&map, "Sheet1");
0432     map.addSheet(sheet1);
0433     Sheet *sheet2 = new Sheet(&map, "Sheet2");
0434     map.addSheet(sheet2);
0435     Canvas canvas(0);
0436     Selection selection(&canvas);
0437     selection.setActiveSheet(sheet1);
0438 
0439     QVERIFY(!selection.isEmpty());
0440     QCOMPARE(selection.name(), QString("A1"));
0441 
0442     // normal mode:
0443     // ranges get split
0444 
0445     selection.initialize(Region("A1:D5", &map, sheet1));
0446     QCOMPARE(selection.name(sheet1), QString("A1:D5"));
0447 
0448     selection.extend(QPoint(2, 2), sheet1);
0449     QCOMPARE(selection.name(sheet1), QString("A3:D5;C2:D2;A2;A1:D1"));
0450 
0451     // reference mode:
0452     // ranges are not affected
0453 
0454     selection.startReferenceSelection();
0455 
0456     selection.initialize(Region("A1:D5", &map, sheet1));
0457     QCOMPARE(selection.name(), QString("A1:D5"));
0458 
0459     selection.extend(QPoint(2, 2), sheet1);
0460     QCOMPARE(selection.name(), QString("A1:D5;B2"));
0461 }
0462 
0463 QTEST_MAIN(TestSelection)