(Automated Tests) Added several negative test cases
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-PixelData.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <cstdlib>
19 #include <dali-test-suite-utils.h>
20
21 #include <dali/public-api/images/pixel.h>
22 #include <dali/public-api/images/pixel-data.h>
23 #include <dali/public-api/common/dali-vector.h>
24
25 using namespace Dali;
26
27 int UtcDaliPixelData01(void)
28 {
29   TestApplication application;
30
31   unsigned int width = 10u;
32   unsigned int height = 10u;
33   unsigned int bufferSize = width*height*Pixel::GetBytesPerPixel( Pixel::RGB888 );
34
35   unsigned char* buffer= reinterpret_cast<unsigned char*>( malloc( bufferSize ) );
36   PixelData pixelData = PixelData::New( buffer, bufferSize, width, height, Pixel::RGB888, PixelData::FREE );
37
38   DALI_TEST_CHECK( pixelData );
39   DALI_TEST_CHECK( pixelData.GetWidth() == width );
40   DALI_TEST_CHECK( pixelData.GetHeight() == height );
41   DALI_TEST_CHECK( pixelData.GetPixelFormat() == Pixel::RGB888 );
42
43   END_TEST;
44 }
45
46 int UtcDaliPixelData02(void)
47 {
48   TestApplication application;
49
50   unsigned int width = 10u;
51   unsigned int height = 10u;
52   unsigned int bufferSize = width*height*Pixel::GetBytesPerPixel( Pixel::L8 );
53   unsigned char* buffer = new unsigned char [ bufferSize ];
54   buffer[0] = 'a';
55
56   PixelData pixelData = PixelData::New( buffer, bufferSize, width, height, Pixel::L8, PixelData::DELETE_ARRAY );
57
58   DALI_TEST_CHECK( pixelData);
59   DALI_TEST_CHECK( pixelData.GetWidth() == width );
60   DALI_TEST_CHECK( pixelData.GetHeight() == height );
61   DALI_TEST_CHECK( pixelData.GetPixelFormat() == Pixel::L8 );
62
63   END_TEST;
64 }
65
66 int UtcDaliPixelDataCopyConstructor(void)
67 {
68   TestApplication application;
69
70   unsigned int width = 10u;
71   unsigned int height = 10u;
72   unsigned int bufferSize = width*height*Pixel::GetBytesPerPixel( Pixel::L8 );
73   unsigned char* buffer = new unsigned char [ bufferSize ];
74   PixelData pixelData = PixelData::New( buffer, bufferSize, width, height, Pixel::L8, PixelData::DELETE_ARRAY );
75
76   PixelData pixelDataCopy(pixelData);
77
78   DALI_TEST_EQUALS( (bool)pixelDataCopy, true, TEST_LOCATION );
79   END_TEST;
80 }
81
82 int UtcDaliPixelDataAssignmentOperator(void)
83 {
84   TestApplication application;
85
86   unsigned int width = 10u;
87   unsigned int height = 10u;
88   unsigned int bufferSize = width*height*Pixel::GetBytesPerPixel( Pixel::L8 );
89   unsigned char* buffer = new unsigned char [ bufferSize ];
90   PixelData pixelData = PixelData::New( buffer, bufferSize, width, height, Pixel::L8, PixelData::DELETE_ARRAY );
91
92   PixelData pixelData2;
93   DALI_TEST_EQUALS( (bool)pixelData2, false, TEST_LOCATION );
94
95   pixelData2 = pixelData;
96   DALI_TEST_EQUALS( (bool)pixelData2, true, TEST_LOCATION );
97
98   END_TEST;
99 }
100
101 int UtcDaliPixelDataMoveConstructor(void)
102 {
103   TestApplication application;
104
105   unsigned int width = 10u;
106   unsigned int height = 10u;
107   unsigned int bufferSize = width*height*Pixel::GetBytesPerPixel( Pixel::L8 );
108   unsigned char* buffer = new unsigned char [ bufferSize ];
109
110   PixelData pixelData = PixelData::New( buffer, bufferSize, width, height, Pixel::L8, PixelData::DELETE_ARRAY );
111   DALI_TEST_CHECK( pixelData );
112   DALI_TEST_EQUALS( width, pixelData.GetWidth(), TEST_LOCATION );
113   DALI_TEST_EQUALS( height, pixelData.GetHeight(), TEST_LOCATION );
114
115   PixelData moved = std::move( pixelData);
116   DALI_TEST_CHECK( moved );
117   DALI_TEST_EQUALS( width, moved.GetWidth(), TEST_LOCATION );
118   DALI_TEST_EQUALS( height, moved.GetHeight(), TEST_LOCATION );
119   DALI_TEST_CHECK( !pixelData );
120
121   END_TEST;
122 }
123
124 int UtcDaliPixelDataMoveAssignment(void)
125 {
126   TestApplication application;
127
128   unsigned int width = 10u;
129   unsigned int height = 10u;
130   unsigned int bufferSize = width*height*Pixel::GetBytesPerPixel( Pixel::L8 );
131   unsigned char* buffer = new unsigned char [ bufferSize ];
132
133   PixelData pixelData = PixelData::New( buffer, bufferSize, width, height, Pixel::L8, PixelData::DELETE_ARRAY );
134   DALI_TEST_CHECK( pixelData );
135   DALI_TEST_EQUALS( width, pixelData.GetWidth(), TEST_LOCATION );
136   DALI_TEST_EQUALS( height, pixelData.GetHeight(), TEST_LOCATION );
137
138   PixelData moved;
139   moved = std::move( pixelData);
140   DALI_TEST_CHECK( moved );
141   DALI_TEST_EQUALS( width, moved.GetWidth(), TEST_LOCATION );
142   DALI_TEST_EQUALS( height, moved.GetHeight(), TEST_LOCATION );
143   DALI_TEST_CHECK( !pixelData );
144
145   END_TEST;
146 }
147
148 int UtcDaliPixelDataGetPixelFormatNegative(void)
149 {
150   TestApplication application;
151   Dali::PixelData instance;
152   try
153   {
154     instance.GetPixelFormat();
155     DALI_TEST_CHECK(false); // Should not get here
156   }
157   catch(...)
158   {
159     DALI_TEST_CHECK(true); // We expect an assert
160   }
161   END_TEST;
162 }
163
164 int UtcDaliPixelDataGetWidthNegative(void)
165 {
166   TestApplication application;
167   Dali::PixelData instance;
168   try
169   {
170     instance.GetWidth();
171     DALI_TEST_CHECK(false); // Should not get here
172   }
173   catch(...)
174   {
175     DALI_TEST_CHECK(true); // We expect an assert
176   }
177   END_TEST;
178 }
179
180 int UtcDaliPixelDataGetHeightNegative(void)
181 {
182   TestApplication application;
183   Dali::PixelData instance;
184   try
185   {
186     instance.GetHeight();
187     DALI_TEST_CHECK(false); // Should not get here
188   }
189   catch(...)
190   {
191     DALI_TEST_CHECK(true); // We expect an assert
192   }
193   END_TEST;
194 }