summaryrefslogtreecommitdiff
path: root/test/source/screenshot_test.cxx
blob: d02e738418273d53cd7cebb1f228aff6ae0c35a6 (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
/* -*- 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/screenshot_test.hxx"

#include <com/sun/star/util/XCloseable.hpp>
#include <com/sun/star/frame/Desktop.hpp>
#include <comphelper/processfactory.hxx>
#include <vcl/abstdlg.hxx>
#include <vcl/pngwrite.hxx>

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 );
    }
}

using namespace css;
using namespace css::uno;

ScreenshotTest::ScreenshotTest()
      : m_aScreenshotDirectory("/workdir/screenshots/")
{
}

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

    mxDesktop = css::frame::Desktop::create( comphelper::getComponentContext(getMultiServiceFactory()) );
    CPPUNIT_ASSERT_MESSAGE("no desktop!", mxDesktop.is());

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

void ScreenshotTest::tearDown()
{
    test::BootstrapFixture::tearDown();
}

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

     if (!aScreenshot.IsEmpty())
     {
         OString aScreenshotId = rDialog.GetScreenshotId();
         OUString aDirname, aBasename;
         splitHelpId( aScreenshotId, aDirname, aBasename );
         aDirname =  m_aScreenshotDirectory + 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 ScreenshotTest::dumpDialogToPath( VclAbstractDialog& rDialog )
{
   const std::vector<OString> aPageDescriptions(rDialog.getAllPageUIXMLDescriptions());

   if (aPageDescriptions.size())
   {
      for (sal_uInt32 a(0); a < aPageDescriptions.size(); a++)
      {
          if (rDialog.selectPageByUIXMLDescription(aPageDescriptions[a]))
          {
              saveScreenshot( rDialog );
          }
          else
          {
              CPPUNIT_ASSERT(false);
          }
      }
   }
   else
   {
      saveScreenshot( rDialog );
   }
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */