18 #include <QTemporaryDir>
19 #include <QTemporaryFile>
22 #include "fileformat.h"
23 #include "filemanager.h"
26 #include "bitmapimage.h"
27 #include "layerbitmap.h"
30 TEST_CASE(
"FileManager Initial Test")
32 SECTION(
"Initial error code")
35 REQUIRE(f.error() == Status::OK);
39 TEST_CASE(
"FileManager invalid operations")
41 SECTION(
"Open a non-existing file")
45 QString strDummyPath =
"hahaha_blala.pcl";
46 Object* obj = fm.load(strDummyPath);
48 REQUIRE(obj ==
nullptr);
49 REQUIRE(fm.error().code() == Status::FILE_NOT_FOUND);
57 QFile badXMLFile(strBadXMLPath);
61 fout <<
"%% haha, this is not a xml file.";
65 Object* pObj = fm.load(strBadXMLPath);
67 REQUIRE(pObj ==
nullptr);
68 REQUIRE(fm.error().code() == Status::ERROR_INVALID_XML_FILE);
71 SECTION(
"A valid xml, but doesn't follow pencil2d's rule")
75 QFile badXMLFile(strBadXMLPath);
79 fout <<
"<!DOCTYPE BlahBlahRoot><document></document>";
83 Object* pObj = fm.load(strBadXMLPath);
85 REQUIRE(pObj ==
nullptr);
86 REQUIRE(fm.error().code() == Status::ERROR_INVALID_PENCIL_FILE);
90 TEST_CASE(
"FileManager Loading XML Tests")
92 SECTION(
"Minimal working xml")
95 if (minimalDoc.
open())
101 fout <<
"<!DOCTYPE PencilDocument><document>";
102 fout <<
" <object></object>";
103 fout <<
"</document>";
109 REQUIRE(o !=
nullptr);
110 REQUIRE(fm.error().ok());
111 REQUIRE(o->getLayerCount() == 1);
117 SECTION(
"Xml with one bitmap layer")
128 fout <<
"<!DOCTYPE PencilDocument><document>";
130 fout <<
" <layer name='MyLayer' id='5' visibility='1' type='1'></layer>";
131 fout <<
" </object>";
132 fout <<
"</document>";
136 Object* obj = fm.load(theXML.fileName());
137 REQUIRE(obj->getLayerCount() == 2);
138 REQUIRE(obj->getLayer(0)->name() ==
"MyLayer");
139 REQUIRE(obj->getLayer(0)->id() == 5);
140 REQUIRE(obj->getLayer(0)->visible() ==
true);
141 REQUIRE(obj->getLayer(0)->type() == Layer::BITMAP);
146 SECTION(
"xml with one bitmap layer and one key")
157 fout <<
"<!DOCTYPE PencilDocument><document>";
159 fout <<
" <layer name='GoodLayer' id='5' visibility='0' type='1' >";
160 fout <<
" <image frame='1' topLeftY='0' src='003.001.png' topLeftX='0' />";
162 fout <<
" </object>";
163 fout <<
"</document>";
167 Object* obj = fm.load(theXML.fileName());
169 Layer* layer = obj->getLayer(0);
170 REQUIRE(layer->name() ==
"GoodLayer");
171 REQUIRE(layer->id() == 5);
172 REQUIRE(layer->visible() ==
false);
173 REQUIRE(layer->type() == Layer::BITMAP);
175 REQUIRE(layer->getKeyFrameAt(1) !=
nullptr);
176 REQUIRE(layer->keyFrameCount() == 1);
181 SECTION(
"xml with 1 bitmap layer and 2 keys")
192 fout <<
"<!DOCTYPE PencilDocument><document>";
194 fout <<
" <layer name='MyLayer' id='5' visibility='1' type='1' >";
195 fout <<
" <image frame='1' topLeftY='0' src='003.001.png' topLeftX='0' />";
196 fout <<
" <image frame='2' topLeftY='0' src='003.001.png' topLeftX='0' />";
198 fout <<
" </object>";
199 fout <<
"</document>";
203 Object* obj = fm.load(theXML.fileName());
205 Layer* layer = obj->getLayer(0);
206 REQUIRE(layer->name() ==
"MyLayer");
207 REQUIRE(layer->id() == 5);
208 REQUIRE(layer->visible() ==
true);
209 REQUIRE(layer->type() == Layer::BITMAP);
211 REQUIRE(layer->getKeyFrameAt(1) !=
nullptr);
222 qWarning() << __FUNCTION__ <<
"Cannot open" << rscPath;
229 QFile fout(filePathOnDisk);
232 qWarning() << __FUNCTION__ <<
"Cannot write to" << filePathOnDisk;
236 return filePathOnDisk;
239 TEST_CASE(
"FileManager Load PCLX")
241 SECTION(
"Empty PCLX")
246 Object* o = fm.load(QtResourceToFile(
":/empty.pclx",
"empty.pclx", tempDir));
247 REQUIRE(o !=
nullptr);
251 REQUIRE(o->getLayerCount() == 4);
256 SECTION(
"Chinese Filename")
261 Object* o = fm.load(QtResourceToFile(
":/cjk-test.pclx",
"許功蓋.pclx", tempDir));
262 REQUIRE(o !=
nullptr);
266 REQUIRE(o->getLayerCount() == 4);
271 SECTION(
"Japanese Filename")
276 Object* o = fm.load(QtResourceToFile(
":/cjk-test.pclx",
"構わない.pclx", tempDir));
277 REQUIRE(o !=
nullptr);
281 REQUIRE(o->getLayerCount() == 4);
286 SECTION(
"Korean Filename")
291 Object* o = fm.load(QtResourceToFile(
":/cjk-test.pclx",
"대박이야.pclx", tempDir));
292 REQUIRE(o !=
nullptr);
296 REQUIRE(o->getLayerCount() == 4);
302 TEST_CASE(
"FileManager File-saving")
305 SECTION(
"#939 Clear action not properly saved")
312 o1->createDefaultLayers();
315 REQUIRE(layer->addNewKeyFrameAt(2));
321 QString animationPath = testDir.path() +
"/abc.pclx";
322 fm.save(o1, animationPath);
326 Object* o2 = fm.load(animationPath);
327 layer =
dynamic_cast<LayerBitmap*
>(o2->getLayer(2));
332 fm.save(o2, animationPath);
336 Object* o3 = fm.load(animationPath);
337 layer =
dynamic_cast<LayerBitmap*
>(o3->getLayer(2));
340 REQUIRE(b3->bounds().
isEmpty());
346 SECTION(
"#966 Moving more than 200 frames corrupts frames upon save")
353 o1->createDefaultLayers();
356 for (
int i = 100; i < 150; ++i)
358 layer->addNewKeyFrameAt(i);
359 auto bitmap = layer->getBitmapImageAtFrame(i);
364 QString animationPath = testDir.path() +
"/abc.pclx";
365 fm.save(o1, animationPath);
369 Object* o2 = fm.load(animationPath);
370 o2->setActiveFramePoolSize(20);
372 layer =
dynamic_cast<LayerBitmap*
>(o2->getLayer(2));
373 for (
int i = 1; i < 150; ++i)
374 o2->updateActiveFrames(i);
377 for (
int i = 100; i < 150; ++i)
378 layer->setFrameSelected(i,
true);
380 layer->moveSelectedFrames(-55);
381 fm.save(o2, animationPath);
385 Object* o3 = fm.load(animationPath);
386 layer =
dynamic_cast<LayerBitmap*
>(o3->getLayer(2));
387 for (
int i = 2; i < 150; ++i)
389 auto bitmap = layer->getBitmapImageAtFrame(i);
392 REQUIRE(bitmap->image()->width() > 1);
393 REQUIRE(bitmap->image()->height() > 1);
400 TEST_CASE(
"Empty Sound Frames")
402 SECTION(
"Invalid src value")
406 if (soundFrameDoc.
open())
412 fout <<
"<!DOCTYPE PencilDocument><document>";
414 fout <<
" <layer type='4' id='5' name='GoodLayer' visibility='1'>";
415 fout <<
" <sound frame='1' name='' src=''/>";
417 fout <<
" </object>";
418 fout <<
"</document>";
425 REQUIRE(newObj !=
nullptr);
426 REQUIRE(fm.error().ok());
427 REQUIRE(newObj->getLayerCount() == 2);
428 REQUIRE(newObj->getLayer(0)->type() == 4);
429 REQUIRE(newObj->getLayer(0)->id() == 5);
430 REQUIRE(newObj->getLayer(0)->name() ==
"GoodLayer");
431 REQUIRE(newObj->getLayer(0)->getVisibility() ==
true);
432 REQUIRE(newObj->getLayer(0)->getKeyFrameAt(1) ==
nullptr);
QString filePath(const QString &fileName) const const
virtual bool open(QIODevice::OpenMode mode) override
CompositionMode_SourceOver
bool isEmpty() const const
virtual QString fileName() const const override