All Classes Namespaces Functions Variables Enumerations Properties Pages
test_bitmapimage.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 "bitmapimage.h"
19 
20 TEST_CASE("BitmapImage constructors")
21 {
22  SECTION("Init an Bitmap Image")
23  {
24  auto b = std::make_shared<BitmapImage>();
25  REQUIRE(b->image()->isNull());
26 
27  REQUIRE(b->width() == 0);
28  REQUIRE(b->height() == 0);
29  REQUIRE(b->top() == 0);
30  REQUIRE(b->left() == 0);
31  }
32 
33  SECTION("Init with color and boundary")
34  {
35  auto b = std::make_shared<BitmapImage>(QRect(10, 20, 30, 40), Qt::red);
36 
37  REQUIRE(b->left() == 10);
38  REQUIRE(b->top() == 20);
39  REQUIRE(b->width() == 30);
40  REQUIRE(b->height() == 40);
41 
42  QRgb rgb = b->image()->pixel(0, 0);
43  REQUIRE(rgb == qRgb(255, 0, 0));
44  }
45 
46  SECTION("Clone a BitmapImage")
47  {
48  auto b = std::make_shared<BitmapImage>(QRect(20, 20, 100, 100), Qt::red);
49  auto b2 = b->clone();
50 
51  REQUIRE(b->pos() == b2->pos());
52  REQUIRE(b->length() == b2->length());
53 
54  REQUIRE(b->left() == b2->left());
55  REQUIRE(b->right() == b2->right());
56  REQUIRE(b->width() == b2->width());
57  REQUIRE(b->height() == b2->height());
58 
59  QImage* img1 = b->image();
60  QImage* img2 = b2->image();
61  REQUIRE(img1 != img2);
62  REQUIRE((*img1) == (*img2));
63  }
64 
65  SECTION("#947 Initial Color")
66  {
67  // A new bitmap image must be fully transparent
68  // otherwise a pixel dot will appear in center of canvas
69  // ref: https://github.com/pencil2d/pencil/pull/947
70 
71  auto b = std::make_shared<BitmapImage>();
72  for (int x = 0; x < b->width(); ++x)
73  {
74  for (int y = 0; y < b->height(); ++y)
75  {
76  QRgb color = b->pixel(x, y);
77  REQUIRE(qAlpha(color) == 0);
78  }
79  }
80  }
81 }
82 
83 TEST_CASE("BitmapImage functions")
84 {
85  SECTION("moveTopLeft()")
86  {
87  auto b = std::make_shared<BitmapImage>(QRect(0, 0, 50, 50), Qt::red);
88  b->moveTopLeft(QPoint(20, 10));
89 
90  REQUIRE(b->topLeft() == QPoint(20, 10));
91  REQUIRE(b->width() == 50);
92  REQUIRE(b->height() == 50);
93  }
94 }