2 #include "test_filemanager.h"
4 #include <QTemporaryDir>
5 #include <QTemporaryFile>
6 #include <QScopedPointer>
8 #include "JlCompress.h"
9 #include "fileformat.h"
10 #include "filemanager.h"
14 typedef std::shared_ptr< FileManager > FileManagerPtr;
17 void TestFileManager::testCase1()
19 FileManagerPtr fm = std::make_shared< FileManager >();
20 QVERIFY( fm->error() == Status::OK );
23 void TestFileManager::testNotExistFile()
27 QString strDummyPath =
"hahaha_blala.pcl";
28 Object* obj = fm.load( strDummyPath );
30 QVERIFY2( obj == NULL,
"File doesn't exist.");
31 QVERIFY2( fm.error().code() == Status::FILE_NOT_FOUND,
"" );
34 void TestFileManager::testInvalidXML()
36 QString strBadXMLPath = QDir::tempPath() +
"/bad.pcl";
39 QFile badXMLFile( strBadXMLPath );
40 badXMLFile.open( QIODevice::WriteOnly );
42 QTextStream fout( &badXMLFile );
43 fout <<
"%% haha, this is not a xml file.";
47 Object* pObj = fm.load( strBadXMLPath );
49 QVERIFY( pObj == NULL );
50 QVERIFY( fm.error().code() == Status::ERROR_INVALID_XML_FILE );
53 void TestFileManager::testInvalidPencilDocument()
55 QString strBadXMLPath = QDir::tempPath() +
"/bad.pcl";
57 QFile badXMLFile( strBadXMLPath );
58 badXMLFile.open( QIODevice::WriteOnly );
60 QTextStream fout( &badXMLFile );
61 fout <<
"<!DOCTYPE NotPencilDocument><document></document>";
65 Object* pObj = fm.load( strBadXMLPath );
67 QVERIFY( pObj == NULL );
68 QVERIFY( fm.error().code() == Status::ERROR_INVALID_PENCIL_FILE );
71 void TestFileManager::testMinimalOldPencilDocument()
73 QTemporaryFile minimalDoc;
74 if ( minimalDoc.open() )
76 QFile minXML( minimalDoc.fileName() );
77 minXML.open( QIODevice::WriteOnly );
79 QTextStream fout( &minXML );
80 fout <<
"<!DOCTYPE PencilDocument><document>";
81 fout <<
" <object></object>";
82 fout <<
"</document>";
86 Object* o = fm.load( minimalDoc.fileName() );
87 OnScopeExit(
delete o );
89 QVERIFY( o !=
nullptr );
90 QVERIFY( fm.error().ok() );
91 QVERIFY( o->getLayerCount() == 0 );
95 QFAIL(
"Can't open temp file." );
99 void TestFileManager::testOneLayerInFile()
101 QTemporaryFile tmpFile;
102 if ( !tmpFile.open() )
104 QFAIL(
"temp file" );
106 QFile theXML( tmpFile.fileName() );
107 theXML.open( QIODevice::WriteOnly );
109 QTextStream fout( &theXML );
110 fout <<
"<!DOCTYPE PencilDocument><document>";
112 fout <<
" <layer name='MyLayer' id='5' visibility='1' type='1'></layer>";
113 fout <<
" </object>";
114 fout <<
"</document>";
118 Object* obj = fm.load( theXML.fileName() );
119 OnScopeExit(
delete obj );
121 QVERIFY( obj->getLayerCount() == 1 );
124 void TestFileManager::testBitmapLayer()
126 QTemporaryFile tmpFile;
127 if ( !tmpFile.open() )
129 QFAIL(
"temp file" );
131 QFile theXML( tmpFile.fileName() );
132 theXML.open( QIODevice::WriteOnly );
134 QTextStream fout( &theXML );
135 fout <<
"<!DOCTYPE PencilDocument><document>";
137 fout <<
" <layer name='MyLayer' id='5' visibility='1' type='1' >";
138 fout <<
" <image frame='1' topLeftY='0' src='003.001.png' topLeftX='0' />";
140 fout <<
" </object>";
141 fout <<
"</document>";
145 Object* obj = fm.load( theXML.fileName() );
146 OnScopeExit(
delete obj );
148 Layer* layer = obj->getLayer( 0 );
149 QVERIFY2( layer->name() ==
"MyLayer",
"LayerName is different" );
150 QCOMPARE( layer->id(), 5 );
151 QCOMPARE( layer->visible(), true );
152 QCOMPARE( layer->type(), Layer::BITMAP );
154 QVERIFY( layer->getKeyFrameAt( 1 ) != nullptr );
158 void TestFileManager::testBitmapLayer2()
160 QTemporaryFile tmpFile;
161 if ( !tmpFile.open() )
163 QFAIL(
"temp file" );
165 QFile theXML( tmpFile.fileName() );
166 theXML.open( QIODevice::WriteOnly );
168 QTextStream fout( &theXML );
169 fout <<
"<!DOCTYPE PencilDocument><document>";
171 fout <<
" <layer name='MyLayer' id='5' visibility='1' type='1' >";
172 fout <<
" <image frame='1' topLeftY='0' src='003.001.png' topLeftX='0' />";
173 fout <<
" <image frame='2' topLeftY='0' src='003.001.png' topLeftX='0' />";
175 fout <<
" </object>";
176 fout <<
"</document>";
180 Object* obj = fm.load( theXML.fileName() );
181 OnScopeExit(
delete obj );
183 Layer* layer = obj->getLayer( 0 );
184 QVERIFY2( layer->name() ==
"MyLayer",
"LayerName is different" );
185 QCOMPARE( layer->id(), 5 );
186 QCOMPARE( layer->visible(), true );
187 QCOMPARE( layer->type(), Layer::BITMAP );
189 QVERIFY( layer->getKeyFrameAt( 1 ) != nullptr );
192 void TestFileManager::testGeneratePCLX()
194 QTemporaryDir testDir(
"PENCIL_TEST_XXXXXXXX" );
195 if ( !testDir.isValid() )
200 QString strMainXMLPath = testDir.path() +
"/" + PFF_XML_FILE_NAME;
202 QFile theXML( strMainXMLPath );
203 theXML.open( QIODevice::WriteOnly );
205 QTextStream fout( &theXML );
206 fout <<
"<!DOCTYPE PencilDocument><document>";
208 fout <<
" <layer name='MyLayer' id='5' visibility='1' type='1' >";
209 fout <<
" <image frame='1' topLeftY='0' src='003.001.png' topLeftX='0' />";
211 fout <<
" </object>";
212 fout <<
"</document>";
215 QDir dir( testDir.path() );
216 if ( dir.mkdir( PFF_DATA_DIR ) )
218 dir.cd( PFF_DATA_DIR );
220 QImage img( 10, 10, QImage::Format_ARGB32_Premultiplied );
221 img.save( dir.path() +
"/003.001.png" );
223 QTemporaryFile tmpPCLX(
"PENCIL_TEST_XXXXXXXX.pclx" );
226 bool ok = JlCompress::compressDir( tmpPCLX.fileName(), testDir.path() );
230 void TestFileManager::testLoadPCLX()
232 QTemporaryDir testDir(
"PENCIL_TEST_XXXXXXXX" );
233 if ( !testDir.isValid() )
238 QString strMainXMLPath = testDir.path() +
"/" + PFF_XML_FILE_NAME;
240 QFile theXML( strMainXMLPath );
241 theXML.open( QIODevice::WriteOnly );
243 QTextStream fout( &theXML );
244 fout <<
"<!DOCTYPE PencilDocument><document>";
246 fout <<
" <layer name='MyBitmapLayer' id='5' visibility='1' type='1' >";
247 fout <<
" <image frame='1' topLeftY='0' src='005.001.png' topLeftX='0' />";
249 fout <<
" </object>";
250 fout <<
"</document>";
253 QDir dir( testDir.path() );
254 if ( dir.mkdir( PFF_DATA_DIR ) )
256 dir.cd( PFF_DATA_DIR );
258 QImage img( 10, 10, QImage::Format_ARGB32_Premultiplied );
259 img.save( dir.path() +
"/005.001.png" );
261 QTemporaryFile tmpPCLX(
"PENCIL_TEST_XXXXXXXX.pclx" );
264 JlCompress::compressDir( tmpPCLX.fileName(), testDir.path() );
267 Object* o = fm.load( tmpPCLX.fileName() );
269 QVERIFY( fm.error().ok() );
271 Layer* layer = o->getLayer( 0 );
272 QVERIFY( layer->name() ==
"MyBitmapLayer" );
273 QVERIFY( layer->id() == 5 );