Avoid potential negative array index access to cached text.
[LibreOffice.git] / sc / qa / unit / ucalc_DocumentThemes.cxx
blobd0bd29052c168a55fac1341c905b4de02c1dfe4c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 #include "helper/qahelper.hxx"
12 #include <docmodel/theme/Theme.hxx>
13 #include <svx/svdpage.hxx>
14 #include <editeng/brushitem.hxx>
15 #include <editeng/colritem.hxx>
17 #include <docsh.hxx>
18 #include <patattr.hxx>
19 #include <attrib.hxx>
20 #include <docpool.hxx>
21 #include <scitems.hxx>
22 #include <undomanager.hxx>
23 #include <ThemeColorChanger.hxx>
25 using namespace css;
27 class DocumentThemesTest : public ScUcalcTestBase
31 namespace
33 CPPUNIT_TEST_FIXTURE(DocumentThemesTest, testGetTheme)
35 m_pDoc->InitDrawLayer();
36 m_pDoc->InsertTab(0, "Test");
38 ScDrawLayer* pDrawLayer = m_pDoc->GetDrawLayer();
39 CPPUNIT_ASSERT(pDrawLayer);
40 auto const& pTheme = pDrawLayer->getTheme();
41 CPPUNIT_ASSERT(pTheme);
44 CPPUNIT_TEST_FIXTURE(DocumentThemesTest, testChangeTheme)
46 m_pDoc->InitDrawLayer();
47 m_pDoc->InsertTab(0, "Test");
49 ScDrawLayer* pDrawLayer = m_pDoc->GetDrawLayer();
50 CPPUNIT_ASSERT(pDrawLayer);
51 auto const& pTheme = pDrawLayer->getTheme();
52 CPPUNIT_ASSERT(pTheme);
54 Color aBackgroundThemeColor(0xc99c00);
55 Color aCellTextThemeColor(0x0369a3);
57 auto eBackgroundThemeType = model::ThemeColorType::Accent5;
58 auto eCellTextThemeType = model::ThemeColorType::Accent2;
60 ScPatternAttr aNewPattern(m_pDoc->GetPool());
62 model::ComplexColor aComplexColor;
63 aComplexColor.setThemeColor(eBackgroundThemeType);
64 Color aColor = pTheme->getColorSet()->resolveColor(aComplexColor);
65 aNewPattern.GetItemSet().Put(SvxBrushItem(aColor, aComplexColor, ATTR_BACKGROUND));
68 model::ComplexColor aComplexColor;
69 aComplexColor.setThemeColor(eCellTextThemeType);
70 Color aColor = pTheme->getColorSet()->resolveColor(aComplexColor);
71 aNewPattern.GetItemSet().Put(SvxColorItem(aColor, aComplexColor, ATTR_FONT_COLOR));
74 // Apply the pattern to cells C3:E5 (2,2 - 4,4) on the first sheet
75 m_pDoc->ApplyPatternAreaTab(2, 2, 4, 4, 0, aNewPattern);
78 const SfxPoolItem* pItem = nullptr;
79 auto* pPattern = m_pDoc->GetPattern(ScAddress(3, 3, 0));
80 CPPUNIT_ASSERT(pPattern);
82 pPattern->GetItemSet().HasItem(ATTR_BACKGROUND, &pItem);
83 auto pBrushItem = static_cast<const SvxBrushItem*>(pItem);
84 CPPUNIT_ASSERT_EQUAL(aBackgroundThemeColor, pBrushItem->GetColor());
85 pPattern->GetItemSet().HasItem(ATTR_FONT_COLOR, &pItem);
86 auto pColorItem = static_cast<const SvxColorItem*>(pItem);
87 CPPUNIT_ASSERT_EQUAL(aCellTextThemeColor, pColorItem->getColor());
90 auto pColorSet = std::make_shared<model::ColorSet>("TestColorScheme");
91 pColorSet->add(model::ThemeColorType::Dark1, 0x000000);
92 pColorSet->add(model::ThemeColorType::Light1, 0x111111);
93 pColorSet->add(model::ThemeColorType::Dark2, 0x222222);
94 pColorSet->add(model::ThemeColorType::Light2, 0x333333);
95 pColorSet->add(model::ThemeColorType::Accent1, 0x444444);
96 pColorSet->add(model::ThemeColorType::Accent2, 0x555555);
97 pColorSet->add(model::ThemeColorType::Accent3, 0x666666);
98 pColorSet->add(model::ThemeColorType::Accent4, 0x777777);
99 pColorSet->add(model::ThemeColorType::Accent5, 0x888888);
100 pColorSet->add(model::ThemeColorType::Accent6, 0x999999);
101 pColorSet->add(model::ThemeColorType::Hyperlink, 0xaaaaaa);
102 pColorSet->add(model::ThemeColorType::FollowedHyperlink, 0xbbbbbb);
104 sc::ThemeColorChanger aChanger(*m_xDocShell);
105 aChanger.apply(pColorSet);
108 const SfxPoolItem* pItem = nullptr;
109 auto* pPattern = m_pDoc->GetPattern(ScAddress(3, 3, 0));
110 CPPUNIT_ASSERT(pPattern);
112 pPattern->GetItemSet().HasItem(ATTR_BACKGROUND, &pItem);
113 auto pBrushItem = static_cast<const SvxBrushItem*>(pItem);
114 CPPUNIT_ASSERT_EQUAL(pColorSet->getColor(eBackgroundThemeType), pBrushItem->GetColor());
115 pPattern->GetItemSet().HasItem(ATTR_FONT_COLOR, &pItem);
116 auto pColorItem = static_cast<const SvxColorItem*>(pItem);
117 CPPUNIT_ASSERT_EQUAL(pColorSet->getColor(eCellTextThemeType), pColorItem->getColor());
120 // Undo / Redo
122 m_pDoc->GetUndoManager()->Undo();
125 const SfxPoolItem* pItem = nullptr;
126 auto* pPattern = m_pDoc->GetPattern(ScAddress(3, 3, 0));
127 CPPUNIT_ASSERT(pPattern);
129 pPattern->GetItemSet().HasItem(ATTR_BACKGROUND, &pItem);
130 auto pBrushItem = static_cast<const SvxBrushItem*>(pItem);
131 CPPUNIT_ASSERT_EQUAL(aBackgroundThemeColor, pBrushItem->GetColor());
132 pPattern->GetItemSet().HasItem(ATTR_FONT_COLOR, &pItem);
133 auto pColorItem = static_cast<const SvxColorItem*>(pItem);
134 CPPUNIT_ASSERT_EQUAL(aCellTextThemeColor, pColorItem->getColor());
137 m_pDoc->GetUndoManager()->Redo();
140 const SfxPoolItem* pItem = nullptr;
141 auto* pPattern = m_pDoc->GetPattern(ScAddress(3, 3, 0));
142 CPPUNIT_ASSERT(pPattern);
144 pPattern->GetItemSet().HasItem(ATTR_BACKGROUND, &pItem);
145 auto pBrushItem = static_cast<const SvxBrushItem*>(pItem);
146 CPPUNIT_ASSERT_EQUAL(pColorSet->getColor(eBackgroundThemeType), pBrushItem->GetColor());
147 pPattern->GetItemSet().HasItem(ATTR_FONT_COLOR, &pItem);
148 auto pColorItem = static_cast<const SvxColorItem*>(pItem);
149 CPPUNIT_ASSERT_EQUAL(pColorSet->getColor(eCellTextThemeType), pColorItem->getColor());
153 } // end anonymous namespace
155 CPPUNIT_PLUGIN_IMPLEMENT();
157 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */