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

0001 /* This file is part of the KDE project
0002    Copyright 2010 Marijn Kruisselbrink <mkruisselbrink@kde.org>
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; only
0007    version 2 of the License.
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 "TestStyleStorage.h"
0021 
0022 #include <StyleStorage.h>
0023 #include <Map.h>
0024 
0025 #include <QTest>
0026 
0027 using namespace Calligra::Sheets;
0028 
0029 class MyStyleStorage : public StyleStorage
0030 {
0031 public:
0032     MyStyleStorage(Map* map) : StyleStorage(map) {}
0033     using StyleStorage::garbageCollection;
0034 };
0035 
0036 void TestStyleStorage::testGarbageCollection()
0037 {
0038     Map map;
0039     MyStyleStorage storage(&map);
0040 
0041     QRect rect(5, 5, 1, 1);
0042     QColor c1(Qt::red);
0043     QColor c2(Qt::blue);
0044     SharedSubStyle style1(new SubStyleOne<Style::BackgroundColor, QColor>(c1));
0045     SharedSubStyle style2(new SubStyleOne<Style::BackgroundColor, QColor>(c2));
0046     // we need to do this for multiple cells, so hopefully we'll end up with substyles that are for the same cell but not in the same leafnode in the rtree
0047     for (int i = 0; i < 100; i++)
0048         storage.insert(rect.adjusted(10*i, 0, 10*i, 0), style1);
0049     for (int i = 0; i < 100; i++)
0050         QCOMPARE(storage.contains(rect.adjusted(10*i, 0, 10*i, 0)).backgroundColor(), c1);
0051     for (int i = 0; i < 100; i++)
0052         storage.insert(rect.adjusted(10*i, 0, 10*i, 0), style2);
0053     for (int i = 0; i < 100; i++)
0054         QCOMPARE(storage.contains(rect.adjusted(10*i, 0, 10*i, 0)).backgroundColor(), c2);
0055     for (int i = 0; i < 100; i++)
0056         storage.insert(rect.adjusted(10*i, 0, 10*i, 0), style1);
0057     for (int i = 0; i < 100; i++)
0058         QCOMPARE(storage.contains(rect.adjusted(10*i, 0, 10*i, 0)).backgroundColor(), c1);
0059     for (int j = 0; j < 1000; j++) {
0060         storage.garbageCollection();
0061         for (int i = 0; i < 100; i++)
0062             QCOMPARE(storage.contains(rect.adjusted(10*i, 0, 10*i, 0)).backgroundColor(), c1);
0063     }
0064 }
0065 
0066 QTEST_MAIN(TestStyleStorage)