summaryrefslogtreecommitdiffstats
path: root/svx/qa/unit/gallery/test_gallery.cxx
blob: 4c95e461a52bf626cba46c28ae948a940345c3f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#include <tools/urlobj.hxx>
#include <sfx2/app.hxx>
#include <unotools/tempfile.hxx>
#include <comphelper/DirectoryHelper.hxx>

#include <svx/gallery1.hxx>
#include <svx/galtheme.hxx>
#include <galobj.hxx>

#include <cppunit/TestAssert.h>
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/plugin/TestPlugIn.h>

class GalleryObjTest : public CppUnit::TestFixture
{
public:
    void TestCreateTheme();
    void TestDeleteTheme();
    void TestSetThemeName();
    void TestThemeURLCase();
    void TestThemeCount();
    void TestGalleryThemeEntry();
    void TestInsertGalleryObject();

    CPPUNIT_TEST_SUITE(GalleryObjTest);

    CPPUNIT_TEST(TestCreateTheme);
    CPPUNIT_TEST(TestDeleteTheme);
    CPPUNIT_TEST(TestSetThemeName);
    CPPUNIT_TEST(TestThemeURLCase);
    CPPUNIT_TEST(TestThemeCount);
    CPPUNIT_TEST(TestGalleryThemeEntry);
    CPPUNIT_TEST(TestInsertGalleryObject);

    CPPUNIT_TEST_SUITE_END();
};

// Create and Dereference a theme, check that file exists
void GalleryObjTest::TestCreateTheme()
{
    // Create theme
    std::unique_ptr<utl::TempFile> pTempDir;
    pTempDir.reset(new utl::TempFile(nullptr, true));
    CPPUNIT_ASSERT_MESSAGE("Could not create valid temporary directory", pTempDir->IsValid());
    pTempDir->EnableKillingFile();
    const OUString aGalleryURL = pTempDir->GetURL();

    // Check if directory exists
    CPPUNIT_ASSERT_MESSAGE("Could not create temporary directory",
                           comphelper::DirectoryHelper::dirExists(aGalleryURL));

    std::unique_ptr<Gallery> pGallery(new Gallery(aGalleryURL));
    CPPUNIT_ASSERT_MESSAGE("Could not create gallery instance", (pGallery != nullptr));
    const OUString myThemeName = "addytesttheme";
    CPPUNIT_ASSERT_MESSAGE("Could not create theme", pGallery->CreateTheme(myThemeName));
    CPPUNIT_ASSERT_MESSAGE("Could not find theme", pGallery->HasTheme(myThemeName));

    // check if files exist
    CPPUNIT_ASSERT_MESSAGE(
        "Could not find .thm file inside it",
        comphelper::DirectoryHelper::fileExists(aGalleryURL + "/" + myThemeName + ".thm"));
    CPPUNIT_ASSERT_MESSAGE(
        "Could not find .sdv file inside it",
        comphelper::DirectoryHelper::fileExists(aGalleryURL + "/" + myThemeName + ".sdv"));
}

//  Create and Delete Theme, check the file doesn't exist
void GalleryObjTest::TestDeleteTheme()
{
    // Create theme
    std::unique_ptr<utl::TempFile> pTempDir;
    pTempDir.reset(new utl::TempFile(nullptr, true));
    CPPUNIT_ASSERT_MESSAGE("Could not create valid temporary directory", pTempDir->IsValid());
    pTempDir->EnableKillingFile();
    const OUString aGalleryURL = pTempDir->GetURL();

    std::unique_ptr<Gallery> pGallery(new Gallery(aGalleryURL));
    CPPUNIT_ASSERT_MESSAGE("Could not create gallery instance", (pGallery != nullptr));
    const OUString myThemeName = "addytesttheme";
    CPPUNIT_ASSERT_MESSAGE("Could not create theme", pGallery->CreateTheme(myThemeName));
    CPPUNIT_ASSERT_MESSAGE("Could not find theme", pGallery->HasTheme(myThemeName));

    // Delete Theme
    CPPUNIT_ASSERT_MESSAGE("Could not remove theme", pGallery->RemoveTheme(myThemeName));
    CPPUNIT_ASSERT_MESSAGE("Could not remove theme, theme found even after trying to remove",
                           !pGallery->HasTheme(myThemeName));

    // Check that files do not exist
    CPPUNIT_ASSERT_MESSAGE(
        "Found .thm file inside it after deletion",
        !comphelper::DirectoryHelper::fileExists(aGalleryURL + "/" + myThemeName + ".thm"));
    CPPUNIT_ASSERT_MESSAGE(
        "Found .sdv file inside it after deletion",
        !comphelper::DirectoryHelper::fileExists(aGalleryURL + "/" + myThemeName + ".sdv"));
    CPPUNIT_ASSERT_MESSAGE(
        "Found .sdg file inside it after deletion",
        !comphelper::DirectoryHelper::fileExists(aGalleryURL + "/" + myThemeName + ".sdg"));
    CPPUNIT_ASSERT_MESSAGE(
        "Found .str file inside it after deletion",
        !comphelper::DirectoryHelper::fileExists(aGalleryURL + "/" + myThemeName + ".str"));
}

// Create theme, set name, assert the name is expected
void GalleryObjTest::TestSetThemeName()
{
    // Create theme
    std::unique_ptr<utl::TempFile> pTempDir;
    pTempDir.reset(new utl::TempFile(nullptr, true));
    CPPUNIT_ASSERT_MESSAGE("Could not create valid temporary directory", pTempDir->IsValid());
    pTempDir->EnableKillingFile();
    const OUString aGalleryURL = pTempDir->GetURL();

    std::unique_ptr<Gallery> pGallery(new Gallery(aGalleryURL));
    CPPUNIT_ASSERT_MESSAGE("Could not create gallery instance", (pGallery != nullptr));
    const OUString myThemeName = "addytesttheme";
    CPPUNIT_ASSERT_MESSAGE("Could not create theme", pGallery->CreateTheme(myThemeName));
    CPPUNIT_ASSERT_MESSAGE("Could not find theme", pGallery->HasTheme(myThemeName));

    // Rename theme
    const OUString myNewThemeName = "addytestthemenew";
    pGallery->RenameTheme(myThemeName, myNewThemeName);
    CPPUNIT_ASSERT_MESSAGE("Could not rename theme because old theme name still exists",
                           !pGallery->HasTheme(myThemeName));
    CPPUNIT_ASSERT_MESSAGE("Could not find new renamed theme", pGallery->HasTheme(myNewThemeName));

    // Check that files are not renamed
    CPPUNIT_ASSERT_MESSAGE(
        "Could not find .thm file inside it",
        comphelper::DirectoryHelper::fileExists(aGalleryURL + "/" + myThemeName + ".thm"));
    CPPUNIT_ASSERT_MESSAGE(
        "Could not find .sdv file inside it",
        comphelper::DirectoryHelper::fileExists(aGalleryURL + "/" + myThemeName + ".sdv"));
}

void GalleryObjTest::TestThemeURLCase()
{
    // Create theme
    std::unique_ptr<utl::TempFile> pTempDir;
    pTempDir.reset(new utl::TempFile(nullptr, true));
    CPPUNIT_ASSERT_MESSAGE("Could not create valid temporary directory", pTempDir->IsValid());
    pTempDir->EnableKillingFile();
    const OUString aGalleryURL = pTempDir->GetURL();

    // Check if directory exists
    CPPUNIT_ASSERT_MESSAGE("Could not create temporary directory",
                           comphelper::DirectoryHelper::dirExists(aGalleryURL));

    std::unique_ptr<Gallery> pGallery(new Gallery(aGalleryURL));
    CPPUNIT_ASSERT_MESSAGE("Could not create gallery instance", (pGallery != nullptr));

    // Mixed Case Theme Name
    const OUString myThemeName = "AddyTestTheme";

    CPPUNIT_ASSERT_MESSAGE("Could not create theme", pGallery->CreateTheme(myThemeName));
    CPPUNIT_ASSERT_MESSAGE("Could not find theme", pGallery->HasTheme(myThemeName));

#if defined(LINUX)
    CPPUNIT_ASSERT_MESSAGE("[LINUX] Could not find .thm in lowercase",
                           comphelper::DirectoryHelper::fileExists(
                               aGalleryURL + "/" + myThemeName.toAsciiLowerCase() + ".thm"));
    CPPUNIT_ASSERT_MESSAGE("[LINUX] Could not find .sdv in lowercase",
                           comphelper::DirectoryHelper::fileExists(
                               aGalleryURL + "/" + myThemeName.toAsciiLowerCase() + ".sdv"));
#else
    CPPUNIT_ASSERT_MESSAGE(
        "[WINDOWS] Could not find .thm in mixed case",
        comphelper::DirectoryHelper::fileExists(aGalleryURL + "/" + myThemeName + ".thm"));
    CPPUNIT_ASSERT_MESSAGE(
        "[WINDOWS] Could not find .sdv in mixed case",
        comphelper::DirectoryHelper::fileExists(aGalleryURL + "/" + myThemeName + ".sdv"));
#endif
}

void GalleryObjTest::TestThemeCount()
{
    std::unique_ptr<utl::TempFile> pTempDir;
    pTempDir.reset(new utl::TempFile(nullptr, true));
    CPPUNIT_ASSERT_MESSAGE("Could not create valid temporary directory", pTempDir->IsValid());
    pTempDir->EnableKillingFile();
    const OUString aGalleryURL = pTempDir->GetURL();

    // Check if directory exists
    CPPUNIT_ASSERT_MESSAGE("Could not create temporary directory",
                           comphelper::DirectoryHelper::dirExists(aGalleryURL));

    std::unique_ptr<Gallery> pGallery(new Gallery(aGalleryURL));
    CPPUNIT_ASSERT_MESSAGE("Could not create gallery instance", (pGallery != nullptr));

    // Loop through and test theme count in each pass.
    sal_uInt32 nLimit = 10;
    for (sal_uInt32 i = 1; i <= nLimit; i++)
    {
        OUString myThemeName = "addytesttheme" + OUString::number(i);
        // Create theme
        CPPUNIT_ASSERT_MESSAGE("Could not create theme", pGallery->CreateTheme(myThemeName));
        CPPUNIT_ASSERT_MESSAGE("Could not find theme", pGallery->HasTheme(myThemeName));
        CPPUNIT_ASSERT_EQUAL_MESSAGE("Inconsistent theme count",
                                     static_cast<sal_uInt32>(pGallery->GetThemeCount()), i);
    }
    CPPUNIT_ASSERT_EQUAL_MESSAGE("Inconsistent theme count",
                                 static_cast<sal_uInt32>(pGallery->GetThemeCount()), nLimit);
    for (sal_uInt32 i = nLimit; i > 0; i--)
    {
        OUString myThemeName = "addytesttheme" + OUString::number(i);
        // Delete Theme
        CPPUNIT_ASSERT_MESSAGE("Could not remove theme", pGallery->RemoveTheme(myThemeName));
        CPPUNIT_ASSERT_MESSAGE("Could not remove theme, theme found even after trying to remove",
                               !pGallery->HasTheme(myThemeName));
        CPPUNIT_ASSERT_EQUAL_MESSAGE("Inconsistent theme count",
                                     static_cast<sal_uInt32>(pGallery->GetThemeCount()), i - 1);
    }
}

void GalleryObjTest::TestGalleryThemeEntry()
{
    // Create theme
    std::unique_ptr<utl::TempFile> pTempDir;
    pTempDir.reset(new utl::TempFile(nullptr, true));
    CPPUNIT_ASSERT_MESSAGE("Could not create valid temporary directory", pTempDir->IsValid());
    pTempDir->EnableKillingFile();
    const OUString aGalleryURL = pTempDir->GetURL();

    // Check if directory exists
    CPPUNIT_ASSERT_MESSAGE("Could not create temporary directory",
                           comphelper::DirectoryHelper::dirExists(aGalleryURL));

    std::unique_ptr<Gallery> pGallery(new Gallery(aGalleryURL));
    CPPUNIT_ASSERT_MESSAGE("Could not create gallery instance", (pGallery != nullptr));
    const OUString myThemeName = "addytesttheme";
    CPPUNIT_ASSERT_MESSAGE("Could not create theme", pGallery->CreateTheme(myThemeName));
    CPPUNIT_ASSERT_MESSAGE("Could not find theme", pGallery->HasTheme(myThemeName));

    // Get Theme Entry Object
    const GalleryThemeEntry* mpThemeEntry = pGallery->GetThemeInfo(myThemeName);

    // Check Theme Name
    CPPUNIT_ASSERT_EQUAL_MESSAGE("Theme name doesn't match", mpThemeEntry->GetThemeName(),
                                 myThemeName);

    // Check URLs
    INetURLObject aURL(aGalleryURL);
    aURL.Append(myThemeName);
    INetURLObject aThemeURL(aURL), aSdvURL(aURL), aSdgURL(aURL), aStrURL(aURL);
    aThemeURL.setExtension("thm");
    aSdvURL.setExtension("sdv");
    aSdgURL.setExtension("sdg");
    aStrURL.setExtension("str");
    CPPUNIT_ASSERT_EQUAL_MESSAGE(
        "Theme URL doesn't match",
        mpThemeEntry->GetThmURL().GetMainURL(INetURLObject::DecodeMechanism::Unambiguous),
        aThemeURL.GetMainURL(INetURLObject::DecodeMechanism::Unambiguous));
    CPPUNIT_ASSERT_EQUAL_MESSAGE(
        "Sdv URL doesn't match",
        mpThemeEntry->GetSdvURL().GetMainURL(INetURLObject::DecodeMechanism::Unambiguous),
        aSdvURL.GetMainURL(INetURLObject::DecodeMechanism::Unambiguous));
    CPPUNIT_ASSERT_EQUAL_MESSAGE(
        "Sdg URL doesn't match",
        mpThemeEntry->GetSdgURL().GetMainURL(INetURLObject::DecodeMechanism::Unambiguous),
        aSdgURL.GetMainURL(INetURLObject::DecodeMechanism::Unambiguous));
    CPPUNIT_ASSERT_EQUAL_MESSAGE(
        "Str URL doesn't match",
        mpThemeEntry->GetStrURL().GetMainURL(INetURLObject::DecodeMechanism::Unambiguous),
        aStrURL.GetMainURL(INetURLObject::DecodeMechanism::Unambiguous));
}

void GalleryObjTest::TestInsertGalleryObject()
{
    // Create theme
    std::unique_ptr<utl::TempFile> pTempDir;
    pTempDir.reset(new utl::TempFile(nullptr, true));
    CPPUNIT_ASSERT_MESSAGE("Could not create valid temporary directory", pTempDir->IsValid());
    pTempDir->EnableKillingFile();
    const OUString aGalleryURL = pTempDir->GetURL();

    // Check if directory exists
    CPPUNIT_ASSERT_MESSAGE("Could not create temporary directory",
                           comphelper::DirectoryHelper::dirExists(aGalleryURL));

    std::unique_ptr<Gallery> pGallery(new Gallery(aGalleryURL));
    CPPUNIT_ASSERT_MESSAGE("Could not create gallery instance", (pGallery != nullptr));
    const OUString myThemeName = "addytesttheme";
    CPPUNIT_ASSERT_MESSAGE("Could not create theme", pGallery->CreateTheme(myThemeName));
    CPPUNIT_ASSERT_MESSAGE("Could not find theme", pGallery->HasTheme(myThemeName));

    // Insert Objects Into Theme
    SfxListener aListener;
    SfxApplication::GetOrCreate();
    GalleryTheme* pGalleryTheme = pGallery->AcquireTheme(myThemeName, aListener);
    CPPUNIT_ASSERT_EQUAL(static_cast<sal_uInt32>(0), pGalleryTheme->GetObjectCount());

    pGallery->ReleaseTheme(pGalleryTheme, aListener);
}

CPPUNIT_TEST_SUITE_REGISTRATION(GalleryObjTest);

CPPUNIT_PLUGIN_IMPLEMENT();