All Classes Namespaces Functions Variables Enumerations Properties Pages
test_object.cpp
1 /*
2 
3 Pencil2D - Traditional Animation Software
4 Copyright (C) 2012-2020 Matthew Chiawen Chang
5 
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; version 2 of the License.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 */
16 #include "catch.hpp"
17 
18 #include <memory>
19 #include <QDomDocument>
20 #include <QDomElement>
21 #include <QTemporaryDir>
22 #include "object.h"
23 #include "layerbitmap.h"
24 #include "layervector.h"
25 #include "layersound.h"
26 
27 
28 TEST_CASE("Object::addXXXLayer()")
29 {
30  Object* obj = new Object;
31 
32  SECTION("Init an Object")
33  {
34  obj->init();
35  REQUIRE(obj->getLayerCount() == 0);
36  REQUIRE(obj->getColorCount() > 0);
37  }
38 
39  SECTION("Add a bitmap layer")
40  {
41  REQUIRE(obj->getLayerCount() == 0);
42  obj->addNewBitmapLayer();
43  REQUIRE(obj->getLayerCount() == 1);
44 
45  REQUIRE(obj->getLayer(0)->type() == Layer::BITMAP);
46  }
47 
48  SECTION("Add a vector layer")
49  {
50  REQUIRE(obj->getLayerCount() == 0);
51  obj->addNewVectorLayer();
52  REQUIRE(obj->getLayerCount() == 1);
53 
54  REQUIRE(obj->getLayer(0)->type() == Layer::VECTOR);
55  }
56 
57  SECTION("Add a camera layer")
58  {
59  REQUIRE(obj->getLayerCount() == 0);
60  obj->addNewCameraLayer();
61  REQUIRE(obj->getLayerCount() == 1);
62 
63  REQUIRE(obj->getLayer(0)->type() == Layer::CAMERA);
64  }
65 
66  SECTION("Add a sound layer")
67  {
68  REQUIRE(obj->getLayerCount() == 0);
69  obj->addNewSoundLayer();
70  REQUIRE(obj->getLayerCount() == 1);
71  REQUIRE(obj->getLayer(0)->type() == Layer::SOUND);
72  }
73 
74  SECTION("Add 3 layers")
75  {
76  REQUIRE(obj->getLayerCount() == 0);
77 
78  obj->addNewSoundLayer();
79  REQUIRE(obj->getLayerCount() == 1);
80  REQUIRE(obj->getLayer(0)->type() == Layer::SOUND);
81 
82  obj->addNewCameraLayer();
83  REQUIRE(obj->getLayerCount() == 2);
84  REQUIRE(obj->getLayer(1)->type() == Layer::CAMERA);
85 
86  obj->addNewBitmapLayer();
87  REQUIRE(obj->getLayerCount() == 3);
88  REQUIRE(obj->getLayer(2)->type() == Layer::BITMAP);
89  }
90 
91  SECTION("Add 500 layers")
92  {
93  REQUIRE(obj->getLayerCount() == 0);
94  for (int i = 0; i < 500; ++i)
95  {
96  obj->addNewBitmapLayer();
97  }
98  REQUIRE(obj->getLayerCount() == 500);
99  }
100 
101  delete obj;
102 }
103 
104 
105 
106 TEST_CASE("Object::getUniqueLayerID()")
107 {
108  SECTION("getUniqueLayerID")
109  {
110  std::unique_ptr<Object> obj(new Object);
111 
112  Layer* bitmapLayer = obj->addNewBitmapLayer();
113  REQUIRE(bitmapLayer->id() == 1);
114  REQUIRE(obj->getUniqueLayerID() == 2);
115 
116  Layer* vectorLayer = obj->addNewVectorLayer();
117  REQUIRE(vectorLayer->id() == 2);
118  REQUIRE(obj->getUniqueLayerID() == 3);
119  }
120 }
121 
122 /*
123 void TestObject::testMoveLayer()
124 {
125  std::unique_ptr< Object > obj( new Object );
126 
127  obj->addNewBitmapLayer();
128  obj->addNewVectorLayer();
129  QCOMPARE( obj->getLayer( 0 )->id(), 1 );
130  QCOMPARE( obj->getLayer( 1 )->id(), 2 );
131 
132  obj->moveLayer( 0, 1 );
133  QCOMPARE( obj->getLayer( 0 )->id(), 2 );
134  QCOMPARE( obj->getLayer( 1 )->id(), 1 );
135 
136 }
137 
138 void TestObject::testLoadXML()
139 {
140  std::unique_ptr< Object > obj( new Object );
141 
142  QString strXMLContent;
143  QTextStream sout( &strXMLContent );
144  sout << "<!DOCTYPE PencilDocument><object>";
145  sout << "</object>";
146  sout.flush();
147 
148  QDomDocument doc;
149  doc.setContent( strXMLContent );
150  QDomElement e = doc.firstChildElement( "object" );
151  QVERIFY( !e.isNull() );
152 
153  QVERIFY( obj->loadXML( e ) );
154 
155 }
156 
157 void TestObject::testExportColorPalette()
158 {
159  std::shared_ptr< Object > obj = std::make_shared<Object>();
160 
161  obj->addColor(ColorRef(QColor(255, 254, 253, 100), "TestColor1"));
162 
163  QTemporaryDir dir;
164  if (dir.isValid())
165  {
166  QString sOutPath = dir.path() + "/testPalette.xml";
167  QVERIFY(obj->exportPalette(sOutPath));
168  QVERIFY(obj->importPalette(sOutPath));
169 
170  ColorRef c = obj->getColor(0);
171 
172  QVERIFY(c.name == "TestColor1");
173  QCOMPARE(c.color.red(), 255);
174  QCOMPARE(c.color.green(), 254);
175  QCOMPARE(c.color.blue(), 253);
176  QCOMPARE(c.color.alpha(), 100);
177  }
178 
179 }
180 */
Definition: layer.h:38
Definition: object.h:41