Pencil2D  ff90c0872e88be3bf81c548cd60f01983012ec49
Pencil2D is an animation software for both bitmap and vector graphics. It is free, multi-platform, and open source.
 All Classes Functions
test_filemanager.cpp
1 
2 #include "test_filemanager.h"
3 
4 #include <QTemporaryDir>
5 #include <QTemporaryFile>
6 #include <QScopedPointer>
7 #include <QImage>
8 #include "JlCompress.h"
9 #include "fileformat.h"
10 #include "filemanager.h"
11 #include "util.h"
12 #include "object.h"
13 
14 typedef std::shared_ptr< FileManager > FileManagerPtr;
15 
16 
17 void TestFileManager::testCase1()
18 {
19  FileManagerPtr fm = std::make_shared< FileManager >();
20  QVERIFY( fm->error() == Status::OK );
21 }
22 
23 void TestFileManager::testNotExistFile()
24 {
25  FileManager fm;
26 
27  QString strDummyPath = "hahaha_blala.pcl";
28  Object* obj = fm.load( strDummyPath );
29 
30  QVERIFY2( obj == NULL, "File doesn't exist.");
31  QVERIFY2( fm.error().code() == Status::FILE_NOT_FOUND, "" );
32 }
33 
34 void TestFileManager::testInvalidXML()
35 {
36  QString strBadXMLPath = QDir::tempPath() + "/bad.pcl";
37 
38  // make a fake xml.
39  QFile badXMLFile( strBadXMLPath );
40  badXMLFile.open( QIODevice::WriteOnly );
41 
42  QTextStream fout( &badXMLFile );
43  fout << "%% haha, this is not a xml file.";
44  badXMLFile.close();
45 
46  FileManager fm;
47  Object* pObj = fm.load( strBadXMLPath );
48 
49  QVERIFY( pObj == NULL );
50  QVERIFY( fm.error().code() == Status::ERROR_INVALID_XML_FILE );
51 }
52 
53 void TestFileManager::testInvalidPencilDocument()
54 {
55  QString strBadXMLPath = QDir::tempPath() + "/bad.pcl";
56 
57  QFile badXMLFile( strBadXMLPath );
58  badXMLFile.open( QIODevice::WriteOnly );
59 
60  QTextStream fout( &badXMLFile );
61  fout << "<!DOCTYPE NotPencilDocument><document></document>";
62  badXMLFile.close();
63 
64  FileManager fm;
65  Object* pObj = fm.load( strBadXMLPath );
66 
67  QVERIFY( pObj == NULL );
68  QVERIFY( fm.error().code() == Status::ERROR_INVALID_PENCIL_FILE );
69 }
70 
71 void TestFileManager::testMinimalOldPencilDocument()
72 {
73  QTemporaryFile minimalDoc;
74  if ( minimalDoc.open() )
75  {
76  QFile minXML( minimalDoc.fileName() );
77  minXML.open( QIODevice::WriteOnly );
78 
79  QTextStream fout( &minXML );
80  fout << "<!DOCTYPE PencilDocument><document>";
81  fout << " <object></object>";
82  fout << "</document>";
83  minXML.close();
84 
85  FileManager fm;
86  Object* o = fm.load( minimalDoc.fileName() );
87  OnScopeExit( delete o );
88 
89  QVERIFY( o != nullptr );
90  QVERIFY( fm.error().ok() );
91  QVERIFY( o->getLayerCount() == 0 );
92  }
93  else
94  {
95  QFAIL( "Can't open temp file." );
96  }
97 }
98 
99 void TestFileManager::testOneLayerInFile()
100 {
101  QTemporaryFile tmpFile;
102  if ( !tmpFile.open() )
103  {
104  QFAIL( "temp file" );
105  }
106  QFile theXML( tmpFile.fileName() );
107  theXML.open( QIODevice::WriteOnly );
108 
109  QTextStream fout( &theXML );
110  fout << "<!DOCTYPE PencilDocument><document>";
111  fout << " <object>";
112  fout << " <layer name='MyLayer' id='5' visibility='1' type='1'></layer>";
113  fout << " </object>";
114  fout << "</document>";
115  theXML.close();
116 
117  FileManager fm;
118  Object* obj = fm.load( theXML.fileName() );
119  OnScopeExit( delete obj );
120 
121  QVERIFY( obj->getLayerCount() == 1 );
122 }
123 
124 void TestFileManager::testBitmapLayer()
125 {
126  QTemporaryFile tmpFile;
127  if ( !tmpFile.open() )
128  {
129  QFAIL( "temp file" );
130  }
131  QFile theXML( tmpFile.fileName() );
132  theXML.open( QIODevice::WriteOnly );
133 
134  QTextStream fout( &theXML );
135  fout << "<!DOCTYPE PencilDocument><document>";
136  fout << " <object>";
137  fout << " <layer name='MyLayer' id='5' visibility='1' type='1' >";
138  fout << " <image frame='1' topLeftY='0' src='003.001.png' topLeftX='0' />";
139  fout << " </layer>";
140  fout << " </object>";
141  fout << "</document>";
142  theXML.close();
143 
144  FileManager fm;
145  Object* obj = fm.load( theXML.fileName() );
146  OnScopeExit( delete obj );
147 
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 );
153 
154  QVERIFY( layer->getKeyFrameAt( 1 ) != nullptr );
155 
156 }
157 
158 void TestFileManager::testBitmapLayer2()
159 {
160  QTemporaryFile tmpFile;
161  if ( !tmpFile.open() )
162  {
163  QFAIL( "temp file" );
164  }
165  QFile theXML( tmpFile.fileName() );
166  theXML.open( QIODevice::WriteOnly );
167 
168  QTextStream fout( &theXML );
169  fout << "<!DOCTYPE PencilDocument><document>";
170  fout << " <object>";
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' />";
174  fout << " </layer>";
175  fout << " </object>";
176  fout << "</document>";
177  theXML.close();
178 
179  FileManager fm;
180  Object* obj = fm.load( theXML.fileName() );
181  OnScopeExit( delete obj );
182 
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 );
188 
189  QVERIFY( layer->getKeyFrameAt( 1 ) != nullptr );
190 }
191 
192 void TestFileManager::testGeneratePCLX()
193 {
194  QTemporaryDir testDir( "PENCIL_TEST_XXXXXXXX" );
195  if ( !testDir.isValid() )
196  {
197  QFAIL( "bad." );
198  }
199 
200  QString strMainXMLPath = testDir.path() + "/" + PFF_XML_FILE_NAME;
201 
202  QFile theXML( strMainXMLPath );
203  theXML.open( QIODevice::WriteOnly );
204 
205  QTextStream fout( &theXML );
206  fout << "<!DOCTYPE PencilDocument><document>";
207  fout << " <object>";
208  fout << " <layer name='MyLayer' id='5' visibility='1' type='1' >";
209  fout << " <image frame='1' topLeftY='0' src='003.001.png' topLeftX='0' />";
210  fout << " </layer>";
211  fout << " </object>";
212  fout << "</document>";
213  theXML.close();
214 
215  QDir dir( testDir.path() );
216  if ( dir.mkdir( PFF_DATA_DIR ) )
217  {
218  dir.cd( PFF_DATA_DIR );
219  }
220  QImage img( 10, 10, QImage::Format_ARGB32_Premultiplied );
221  img.save( dir.path() + "/003.001.png" );
222 
223  QTemporaryFile tmpPCLX( "PENCIL_TEST_XXXXXXXX.pclx" );
224  tmpPCLX.open();
225 
226  bool ok = JlCompress::compressDir( tmpPCLX.fileName(), testDir.path() );
227  QVERIFY( ok );
228 }
229 
230 void TestFileManager::testLoadPCLX()
231 {
232  QTemporaryDir testDir( "PENCIL_TEST_XXXXXXXX" );
233  if ( !testDir.isValid() )
234  {
235  QFAIL( "bad." );
236  }
237 
238  QString strMainXMLPath = testDir.path() + "/" + PFF_XML_FILE_NAME;
239 
240  QFile theXML( strMainXMLPath );
241  theXML.open( QIODevice::WriteOnly );
242 
243  QTextStream fout( &theXML );
244  fout << "<!DOCTYPE PencilDocument><document>";
245  fout << " <object>";
246  fout << " <layer name='MyBitmapLayer' id='5' visibility='1' type='1' >";
247  fout << " <image frame='1' topLeftY='0' src='005.001.png' topLeftX='0' />";
248  fout << " </layer>";
249  fout << " </object>";
250  fout << "</document>";
251  theXML.close();
252 
253  QDir dir( testDir.path() );
254  if ( dir.mkdir( PFF_DATA_DIR ) )
255  {
256  dir.cd( PFF_DATA_DIR );
257  }
258  QImage img( 10, 10, QImage::Format_ARGB32_Premultiplied );
259  img.save( dir.path() + "/005.001.png" );
260 
261  QTemporaryFile tmpPCLX( "PENCIL_TEST_XXXXXXXX.pclx" );
262  tmpPCLX.open();
263 
264  JlCompress::compressDir( tmpPCLX.fileName(), testDir.path() );
265 
266  FileManager fm;
267  Object* o = fm.load( tmpPCLX.fileName() );
268 
269  QVERIFY( fm.error().ok() );
270 
271  Layer* layer = o->getLayer( 0 );
272  QVERIFY( layer->name() == "MyBitmapLayer" );
273  QVERIFY( layer->id() == 5 );
274 }
Definition: layer.h:32
Definition: object.h:71