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

0001 /* This file is part of the KDE project
0002    Copyright 2011 Juan Aquino <utcl95@gmail.com>
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 #include "TestSort.h"
0020 
0021 #include <QTest>
0022 
0023 #include <KoCanvasBase.h>
0024 
0025 #include "CellStorage.h"
0026 #include "Map.h"
0027 #include "Region.h"
0028 #include "Sheet.h"
0029 #include "ui/Selection.h"
0030 #include "../commands/SortManipulator.h"
0031 
0032 using namespace Calligra::Sheets;
0033 
0034 void TestSort::AscendingOrder()
0035 {
0036     Map map;
0037     Sheet* sheet = new Sheet(&map, "Sheet1");
0038     map.addSheet(sheet);
0039 
0040     Value cellvalue;
0041 
0042     KoCanvasBase* canvas = 0;
0043     Selection* selection = new Selection(canvas);
0044 
0045     selection->setActiveSheet(sheet);
0046 
0047     CellStorage* storage = sheet->cellStorage();
0048     // Data to sort...
0049     // Ascending
0050     // A1 3     
0051     // A2 Empty 
0052     // A3 1     
0053     // more...
0054 
0055     // Ascending
0056     storage->setValue(1,1, Value(3));
0057     storage->setValue(1,2, Value());
0058     storage->setValue(1,3, Value(1));
0059 
0060     // Selection
0061     selection->clear();
0062     selection->initialize(QRect(1,1,1,3), sheet);
0063     QCOMPARE(selection->name(), QString("Sheet1!A1:A3"));
0064 
0065     // Sort Manipulator
0066     SortManipulator *const command = new SortManipulator();
0067     command->setRegisterUndo(0);
0068     command->setSheet(sheet);
0069 
0070     // Parameters.
0071     command->setSortRows(Qt::Vertical);
0072     command->setSkipFirst(false);
0073     command->setCopyFormat(false);
0074 
0075     command->addCriterion(0, Qt::AscendingOrder, Qt::CaseInsensitive);
0076 
0077     command->add(selection->lastRange());
0078     QCOMPARE(selection->lastRange(), QRect(1,1,1,3));
0079 
0080     // Execute sort
0081     command->execute(selection->canvas());
0082 
0083     QCOMPARE(storage->value(1,1),Value(1));
0084     QCOMPARE(storage->value(1,2),Value(3));
0085     QCOMPARE(storage->value(1,3),Value());
0086 }
0087 
0088 void TestSort::DescendingOrder()
0089 {
0090     Map map;
0091     Sheet* sheet = new Sheet(&map, "Sheet1");
0092     map.addSheet(sheet);
0093 
0094     Value cellvalue;
0095 
0096     KoCanvasBase* canvas = 0;
0097     Selection* selection = new Selection(canvas);
0098 
0099     selection->setActiveSheet(sheet);
0100 
0101     CellStorage* storage = sheet->cellStorage();
0102     // Data to sort...
0103     // Descending
0104     // B1 1 
0105     // B2 Empty 
0106     // B3 3
0107 
0108     // Descending 
0109     storage->setValue(2,1, Value(1));
0110     storage->setValue(2,2, Value());
0111     storage->setValue(2,3, Value(3));
0112 
0113     // Selection
0114     selection->clear();
0115     selection->initialize(QRect(2,1,1,3), sheet);
0116     QCOMPARE(selection->name(), QString("Sheet1!B1:B3"));
0117 
0118     // Sort Manipulator
0119     SortManipulator *const command = new SortManipulator();
0120     command->setRegisterUndo(0);
0121     command->setSheet(sheet);
0122 
0123     // Parameters.
0124     command->setSortRows(Qt::Vertical);
0125     command->setSkipFirst(false);
0126     command->setCopyFormat(false);
0127 
0128     command->addCriterion(0, Qt::DescendingOrder, Qt::CaseInsensitive);
0129 
0130     command->add(selection->lastRange());
0131     QCOMPARE(selection->lastRange(), QRect(2,1,1,3));
0132 
0133     // Execute sort
0134     command->execute(selection->canvas());
0135 
0136     QCOMPARE(storage->value(2,1),Value(3));
0137     QCOMPARE(storage->value(2,2),Value(1));
0138     QCOMPARE(storage->value(2,3),Value());
0139 }
0140 
0141 QTEST_MAIN(TestSort)