2016-07-21 16:12:47 +02:00
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2016-06-30 22:39:28 +02:00
/*
* 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>
2016-07-15 15:25:59 +02:00
# include <vcl/svapp.hxx>
2016-11-17 00:47:33 +01:00
# include <unotools/configmgr.hxx>
2016-11-16 23:41:08 +01:00
# include <unotools/syslocaleoptions.hxx>
2016-06-30 22:39:28 +02:00
namespace {
2016-07-15 15:25:59 +02:00
void splitHelpId ( const OString & rHelpId , OUString & rDirname , OUString & rBasename )
2016-06-30 22:39:28 +02:00
{
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 ( )
2017-08-23 16:17:36 +02:00
: m_aScreenshotDirectory ( " screenshots " ) ,
2016-07-21 16:12:47 +02:00
maKnownDialogs ( )
{
2016-11-16 23:41:08 +01:00
SvtSysLocaleOptions localeOptions ;
2016-12-02 19:59:50 +01:00
maCurrentLanguage = localeOptions . GetRealUILanguageTag ( ) . getBcp47 ( ) ;
2016-07-21 16:12:47 +02:00
}
ScreenshotTest : : ~ ScreenshotTest ( )
2016-06-30 22:39:28 +02:00
{
}
void ScreenshotTest : : setUp ( )
{
test : : BootstrapFixture : : setUp ( ) ;
mxDesktop = css : : frame : : Desktop : : create ( comphelper : : getComponentContext ( getMultiServiceFactory ( ) ) ) ;
CPPUNIT_ASSERT_MESSAGE ( " no desktop! " , mxDesktop . is ( ) ) ;
2016-12-16 14:44:58 +01:00
osl : : Directory : : create ( m_directories . getURLFromWorkdir ( OUStringToOString ( m_aScreenshotDirectory , RTL_TEXTENCODING_UTF8 ) . getStr ( ) ) ) ;
2016-07-21 16:12:47 +02:00
// initialize maKnownDialogs
if ( maKnownDialogs . empty ( ) )
{
registerKnownDialogsByID ( maKnownDialogs ) ;
}
2016-06-30 22:39:28 +02:00
}
2016-07-15 15:25:59 +02:00
void ScreenshotTest : : implSaveScreenshot ( const Bitmap & rScreenshot , const OString & rScreenshotId )
2016-06-30 22:39:28 +02:00
{
2016-07-15 15:25:59 +02:00
OUString aDirname , aBasename ;
splitHelpId ( rScreenshotId , aDirname , aBasename ) ;
2016-11-21 22:06:30 +01:00
aDirname = m_aScreenshotDirectory + " / " + aDirname +
( ( maCurrentLanguage = = " en-US " ) ? OUString ( ) : " / " + maCurrentLanguage ) ;
2016-07-15 15:25:59 +02:00
2017-08-23 16:15:37 +02:00
auto const path = m_directories . getURLFromWorkdir (
OUStringToOString ( aDirname , RTL_TEXTENCODING_UTF8 ) . getStr ( ) ) ;
auto const e = osl : : Directory : : createPath ( path ) ;
if ( e ! = osl : : FileBase : : E_EXIST ) {
CPPUNIT_ASSERT_EQUAL_MESSAGE (
OUStringToOString (
" Failed to create " + path , RTL_TEXTENCODING_UTF8 ) . getStr ( ) ,
osl : : FileBase : : E_None , e ) ;
}
2016-07-15 15:25:59 +02:00
2017-08-23 16:17:36 +02:00
OUString aFullPath = m_directories . getPathFromWorkdir ( OUStringToOString ( " / " + aDirname + " / " + aBasename + " .png " , RTL_TEXTENCODING_UTF8 ) . getStr ( ) ) ;
2016-07-15 15:25:59 +02:00
SvFileStream aNew ( aFullPath , StreamMode : : WRITE | StreamMode : : TRUNC ) ;
2017-08-22 17:58:44 +02:00
CPPUNIT_ASSERT_MESSAGE ( OUStringToOString ( " Failed to open < " + aFullPath + " >: " + OUString : : number ( sal_uInt32 ( aNew . GetErrorCode ( ) ) ) , RTL_TEXTENCODING_UTF8 ) . getStr ( ) , aNew . IsOpen ( ) ) ;
2016-07-15 15:25:59 +02:00
vcl : : PNGWriter aPNGWriter ( rScreenshot ) ;
aPNGWriter . Write ( aNew ) ;
}
2017-08-01 10:09:18 +02:00
void ScreenshotTest : : saveScreenshot ( VclAbstractDialog const & rDialog )
2016-07-15 15:25:59 +02:00
{
const Bitmap aScreenshot ( rDialog . createScreenshot ( ) ) ;
if ( ! aScreenshot . IsEmpty ( ) )
{
const OString aScreenshotId = rDialog . GetScreenshotId ( ) ;
if ( ! aScreenshotId . isEmpty ( ) )
{
implSaveScreenshot ( aScreenshot , aScreenshotId ) ;
}
}
}
void ScreenshotTest : : saveScreenshot ( Dialog & rDialog )
{
const Bitmap aScreenshot ( rDialog . createScreenshot ( ) ) ;
if ( ! aScreenshot . IsEmpty ( ) )
{
const OString aScreenshotId = rDialog . GetScreenshotId ( ) ;
if ( ! aScreenshotId . isEmpty ( ) )
{
implSaveScreenshot ( aScreenshot , aScreenshotId ) ;
}
}
}
2016-11-10 22:00:18 +01:00
VclPtr < VclAbstractDialog > ScreenshotTest : : createDialogByName ( const OString & rName )
2016-07-21 16:12:47 +02:00
{
const mapType : : const_iterator aHit = maKnownDialogs . find ( rName ) ;
if ( aHit ! = maKnownDialogs . end ( ) )
{
return createDialogByID ( ( * aHit ) . second ) ;
}
2016-11-10 22:00:18 +01:00
return VclPtr < VclAbstractDialog > ( ) ;
2016-07-21 16:12:47 +02:00
}
2016-07-15 15:25:59 +02:00
void ScreenshotTest : : dumpDialogToPath ( VclAbstractDialog & rDialog )
{
const std : : vector < OString > aPageDescriptions ( rDialog . getAllPageUIXMLDescriptions ( ) ) ;
if ( aPageDescriptions . size ( ) )
{
2016-07-28 17:10:15 +02:00
for ( size_t a ( 0 ) ; a < aPageDescriptions . size ( ) ; a + + )
2016-07-15 15:25:59 +02:00
{
if ( rDialog . selectPageByUIXMLDescription ( aPageDescriptions [ a ] ) )
{
saveScreenshot ( rDialog ) ;
}
else
{
CPPUNIT_ASSERT ( false ) ;
}
}
}
else
{
saveScreenshot ( rDialog ) ;
}
2016-06-30 22:39:28 +02:00
}
2016-07-15 15:25:59 +02:00
void ScreenshotTest : : dumpDialogToPath ( Dialog & rDialog )
2016-06-30 22:39:28 +02:00
{
2016-07-15 15:25:59 +02:00
const std : : vector < OString > aPageDescriptions ( rDialog . getAllPageUIXMLDescriptions ( ) ) ;
if ( aPageDescriptions . size ( ) )
{
2016-07-28 17:10:15 +02:00
for ( size_t a ( 0 ) ; a < aPageDescriptions . size ( ) ; a + + )
2016-07-15 15:25:59 +02:00
{
if ( rDialog . selectPageByUIXMLDescription ( aPageDescriptions [ a ] ) )
{
saveScreenshot ( rDialog ) ;
}
else
{
CPPUNIT_ASSERT ( false ) ;
}
}
}
else
{
saveScreenshot ( rDialog ) ;
}
2016-06-30 22:39:28 +02:00
}
2016-07-15 15:25:59 +02:00
void ScreenshotTest : : dumpDialogToPath ( const OString & rUIXMLDescription )
{
if ( ! rUIXMLDescription . isEmpty ( ) )
{
VclPtrInstance < Dialog > pDialog ( Application : : GetDefDialogParent ( ) , WB_STDDIALOG | WB_SIZEABLE , Dialog : : InitFlag : : NoParent ) ;
2016-07-21 17:05:32 +02:00
{
VclBuilder aBuilder ( pDialog , VclBuilderContainer : : getUIRootDir ( ) , OStringToOUString ( rUIXMLDescription , RTL_TEXTENCODING_UTF8 ) ) ;
vcl : : Window * pRoot = aBuilder . get_widget_root ( ) ;
Dialog * pRealDialog = dynamic_cast < Dialog * > ( pRoot ) ;
if ( ! pRealDialog )
{
pRealDialog = pDialog ;
}
2016-11-17 00:47:33 +01:00
pRealDialog - > SetText ( utl : : ConfigManager : : getProductName ( ) ) ;
2016-07-21 17:05:32 +02:00
pRealDialog - > SetStyle ( pDialog - > GetStyle ( ) | WB_CLOSEABLE ) ;
dumpDialogToPath ( * pRealDialog ) ;
}
2016-07-15 15:25:59 +02:00
2016-07-21 17:05:32 +02:00
pDialog . disposeAndClear ( ) ;
2016-07-15 15:25:59 +02:00
}
}
2016-07-28 12:22:00 +02:00
void ScreenshotTest : : processAllKnownDialogs ( )
{
2016-09-04 09:23:01 +02:00
for ( mapType : : const_iterator i = getKnownDialogs ( ) . begin ( ) ; i ! = getKnownDialogs ( ) . end ( ) ; + + i )
2016-07-28 12:22:00 +02:00
{
2016-10-28 09:11:02 +02:00
ScopedVclPtr < VclAbstractDialog > pDlg ( createDialogByID ( ( * i ) . second ) ) ;
2016-07-28 12:22:00 +02:00
if ( pDlg )
{
// known dialog, dump screenshot to path
dumpDialogToPath ( * pDlg ) ;
}
else
{
// unknown dialog, should not happen in this basic loop.
// You have probably forgotten to add a case and
// implementastion to createDialogByID, please do this
}
}
}
void ScreenshotTest : : processDialogBatchFile ( const OUString & rFile )
{
test : : Directories aDirectories ;
const OUString aURL ( aDirectories . getURLFromSrc ( rFile ) ) ;
SvFileStream aStream ( aURL , StreamMode : : READ ) ;
OString aNextUIFile ;
const OString aComment ( " # " ) ;
while ( aStream . ReadLine ( aNextUIFile ) )
{
if ( ! aNextUIFile . isEmpty ( ) & & ! aNextUIFile . startsWith ( aComment ) )
{
// first check if it's a known dialog
2016-10-28 09:11:02 +02:00
ScopedVclPtr < VclAbstractDialog > pDlg ( createDialogByName ( aNextUIFile ) ) ;
2016-07-28 12:22:00 +02:00
if ( pDlg )
{
// known dialog, dump screenshot to path
dumpDialogToPath ( * pDlg ) ;
}
else
{
// unknown dialog, try fallback to generic created
// VclBuilder-generated instance. Keep in mind that Dialogs
// using this mechanism will probably not be layouted well
// since the setup/initialization part is missing. Thus,
// only use for fallback when only the UI file is available.
dumpDialogToPath ( aNextUIFile ) ;
}
}
}
}
2016-06-30 22:39:28 +02:00
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */