summaryrefslogtreecommitdiff
path: root/sc/qa/unit/screenshots/screenshots.cxx
blob: ee12727e369f7a2a2ef9b0dfa74ad59987b08da7 (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
307
308
309
/* -*- 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 <test/bootstrapfixture.hxx>
#include <unotest/macros_test.hxx>
#include <test/xmltesttools.hxx>

#include <com/sun/star/frame/Desktop.hpp>
#include <comphelper/dispatchcommand.hxx>
#include <comphelper/processfactory.hxx>
#include <comphelper/propertysequence.hxx>
#include <comphelper/string.hxx>
#include <editeng/editids.hrc>
#include <editeng/editview.hxx>
#include <editeng/outliner.hxx>
#include <osl/conditn.hxx>
#include <osl/file.hxx>
#include <sfx2/dispatch.hxx>
#include <sfx2/viewfrm.hxx>
#include <svl/srchitem.hxx>
#include <vcl/pngwrite.hxx>

#include <tabvwsh.hxx>
#include <docsh.hxx>
#include <document.hxx>
#include <sharedocdlg.hxx>
#include <protectiondlg.hxx>
#include <docuno.hxx>
#include <scabstdlg.hxx>
#include <reffact.hxx>
#include <scui_def.hxx>
#include <impex.hxx>

#include <sc.hrc>
#include <scresid.hxx>
#include <scitems.hxx>

using namespace css;

#if !defined(WNT) && !defined(MACOSX)
static const char* DATA_DIRECTORY = "/sc/qa/unit/screenshots/data/";
static const char* SCREENSHOT_DIRECTORY = "/workdir/screenshots/";
#endif

namespace {
    void splitHelpId( OString& rHelpId, OUString& rDirname, OUString &rBasename )
    {
        sal_Int32 nIndex = rHelpId.lastIndexOf( '/' );

        if( nIndex > 0 )
            rDirname = OStringToOUString( rHelpId.copy( 0, nIndex ), RTL_TEXTENCODING_UTF8 );

        if( rHelpId.getLength() > nIndex+1 )
            rBasename= OStringToOUString( rHelpId.copy( nIndex+1 ), RTL_TEXTENCODING_UTF8 );
    }
}


class ScScreenshotTest : public test::BootstrapFixture, public unotest::MacrosTest, public XmlTestTools
{
public:
    ScScreenshotTest();
    virtual void setUp() SAL_OVERRIDE;
    virtual void tearDown() SAL_OVERRIDE;

#if !defined(WNT) && !defined(MACOSX)
    void testOpeningModalDialogs();
    //void testOpeningModelessDialogs();
#endif

    CPPUNIT_TEST_SUITE(ScScreenshotTest);
#if !defined(WNT) && !defined(MACOSX)
    CPPUNIT_TEST(testOpeningModalDialogs);
    //CPPUNIT_TEST(testOpeningModelessDialogs);
#endif
    CPPUNIT_TEST_SUITE_END();

private:
#if !defined(WNT) && !defined(MACOSX)
    void initializeWithDoc(const char* pName);

    VclAbstractDialog* createDialogByID( sal_uInt32 nID);
    void dumpDialogToPath( VclAbstractDialog& rDialog );
    void saveScreenshot( VclAbstractDialog& rDialog );

#endif

    uno::Reference<lang::XComponent> mxComponent;
    SfxObjectShell* pFoundShell;
    ScDocShellRef xDocSh;
    ScTabViewShell* pViewShell;
    ScAbstractDialogFactory* pFact;
};

ScScreenshotTest::ScScreenshotTest()
{
}

void ScScreenshotTest::setUp()
{
    test::BootstrapFixture::setUp();

    mxDesktop.set(css::frame::Desktop::create(comphelper::getComponentContext(getMultiServiceFactory())));

    osl::FileBase::RC err = osl::Directory::create( m_directories.getURLFromSrc( SCREENSHOT_DIRECTORY ) );
    CPPUNIT_ASSERT_MESSAGE( "Failed to create screenshot directory", (err == osl::FileBase::E_None || err == osl::FileBase::E_EXIST) );
}

void ScScreenshotTest::tearDown()
{
    if (mxComponent.is())
        mxComponent->dispose();

    test::BootstrapFixture::tearDown();
}

#if !defined(WNT) && !defined(MACOSX)
void ScScreenshotTest::initializeWithDoc(const char* pName)
{
    if (mxComponent.is())
        mxComponent->dispose();
    mxComponent = loadFromDesktop(m_directories.getURLFromSrc(DATA_DIRECTORY) + OUString::createFromAscii(pName), "com.sun.star.sheet.SpreadsheetDocument");

    pFoundShell = SfxObjectShell::GetShellFromComponent(mxComponent);
    CPPUNIT_ASSERT_MESSAGE("Failed to access document shell", pFoundShell);

    xDocSh = dynamic_cast<ScDocShell*>(pFoundShell);
    CPPUNIT_ASSERT(xDocSh != nullptr);

    pViewShell = xDocSh->GetBestViewShell(false);
    CPPUNIT_ASSERT(pViewShell != nullptr);

    pFact = ScAbstractDialogFactory::Create();
    CPPUNIT_ASSERT_MESSAGE("Failed to create dialog factory", pFact);
}

VclAbstractDialog* ScScreenshotTest::createDialogByID( sal_uInt32 nID )
{
    VclAbstractDialog *pReturnDialog = nullptr;
    ////FIXME: translatable string here
    const OUString aDefaultSheetName("Sheet1");

    switch ( nID )
    {
        case 0:
        {
            ScViewData& rViewData = pViewShell->GetViewData();
            SCTAB nTabSelCount = rViewData.GetMarkData().GetSelectCount();

            pReturnDialog = pFact->CreateScInsertTableDlg(
                   pViewShell->GetDialogParent(), rViewData, nTabSelCount, false);

            break;
        }

        case 1:
        {
            pReturnDialog = pFact->CreateScDeleteCellDlg( pViewShell->GetDialogParent(), false );
            break;
        }

        case 2:
        {
            pReturnDialog = pFact->CreateScInsertContentsDlg( pViewShell->GetDialogParent() );
            break;
        }

        case 3:
        {
            pReturnDialog = pFact->CreateScColRowLabelDlg( pViewShell->GetDialogParent(), true, false );
            break;
        }

        case 4:
        {
            pReturnDialog = pFact->CreateScDataPilotDatabaseDlg( pViewShell->GetDialogParent() );
            break;
        }
        case 5:
        {

            pReturnDialog = pFact->CreateScDataPilotSourceTypeDlg(pViewShell->GetDialogParent(), true );
            break;
        }

        case 6:
        {
            pReturnDialog = pFact->CreateScDeleteContentsDlg( pViewShell->GetDialogParent() );
            break;
        }

        case 7:
        {
            //// just fake some flags
            sal_uInt16 nFlags = NAME_LEFT | NAME_TOP;
            pReturnDialog = pFact->CreateScNameCreateDlg( pViewShell->GetDialogParent(), nFlags );
            break;
        }

        case 8:
        {
            const OString aEmpty("");
            pReturnDialog = pFact->CreateScStringInputDlg( pViewShell->GetDialogParent(),
                                OUString(ScResId(SCSTR_APDTABLE)), OUString(ScResId(SCSTR_NAME)),
                                aDefaultSheetName, aEmpty, aEmpty );
            break;
        }

        case 9:
        {
            pReturnDialog = pFact->CreateScTabBgColorDlg( pViewShell->GetDialogParent(),
                                OUString(ScResId(SCSTR_SET_TAB_BG_COLOR)),
                                OUString(ScResId(SCSTR_NO_TAB_BG_COLOR)), Color(0xff00ff) );
            break;
        }

        case 10:
        {
            pReturnDialog = pFact->CreateScTextImportOptionsDlg();
            break;
        }

        case 11:
        {
            ////FIXME: looks butt-ugly w/ empty file, move it elsewhere, where
            ////we actually have some data
            pReturnDialog = pFact->CreateScDataFormDlg( pViewShell->GetDialogParent(), pViewShell );
            break;
        }

        case 12:
        {
            pReturnDialog = pFact->CreateScMoveTableDlg( pViewShell->GetDialogParent(), aDefaultSheetName );
            break;
        }

        //case 12:
        //{
        //    const OUString aCsv("some, strings, here, separated, by, commas");
        //    ScImportStringStream aStream( aCsv );
        //    pReturnDialog = pFact->CreateScImportAsciiDlg( OUString(), &aStream, SC_PASTETEXT );
        //    break;
        //}
           //ScopedVclPtrInstance<ScShareDocumentDlg> pDlg14( pViewShell->GetDialogParent(), &rViewData );
            //ScopedVclPtrInstance<ScTableProtectionDlg> pDlg16(pViewShell->GetDialogParent());
        default:
            break;
    }

    //CPPUNIT_ASSERT_MESSAGE( "Failed to create dialog", pReturnDialog );
    return pReturnDialog;
}

void ScScreenshotTest::saveScreenshot( VclAbstractDialog& rDialog )
{
     const Bitmap aScreenshot(rDialog.createScreenshot());

     if (!aScreenshot.IsEmpty())
     {
         OString aScreenshotId = rDialog.GetScreenshotId();
         OUString aDirname, aBasename;
         splitHelpId( aScreenshotId, aDirname, aBasename );
         aDirname = OUString::createFromAscii( SCREENSHOT_DIRECTORY ) + aDirname;

         osl::FileBase::RC err = osl::Directory::createPath( m_directories.getURLFromSrc( aDirname ));
         CPPUNIT_ASSERT_MESSAGE( OUStringToOString( "Failed to create " + aDirname, RTL_TEXTENCODING_UTF8).getStr(),
                         (err == osl::FileBase::E_None || err == osl::FileBase::E_EXIST) );

         OUString aFullPath = m_directories.getSrcRootPath() + aDirname + "/" + aBasename + ".png";
         SvFileStream aNew(aFullPath, StreamMode::WRITE | StreamMode::TRUNC);
         CPPUNIT_ASSERT_MESSAGE( OUStringToOString( "Failed to open " + OUString::number(aNew.GetErrorCode()), RTL_TEXTENCODING_UTF8).getStr(), aNew.IsOpen() );

         vcl::PNGWriter aPNGWriter(aScreenshot);
         aPNGWriter.Write(aNew);
     }
}

void ScScreenshotTest::dumpDialogToPath( VclAbstractDialog& rDialog )
{
    saveScreenshot( rDialog );
}

void ScScreenshotTest::testOpeningModalDialogs()
{
    initializeWithDoc("empty.ods");

    const sal_uInt32 nDialogs = 13;

    for ( sal_uInt32 i = 0; i < nDialogs; i++ )
    {
        VclAbstractDialog *pDialog = createDialogByID( i );

        dumpDialogToPath( *pDialog );
    }
}

#endif

CPPUNIT_TEST_SUITE_REGISTRATION(ScScreenshotTest);

CPPUNIT_PLUGIN_IMPLEMENT();

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */