xref: /haiku/src/tests/kits/translation/BitmapStreamTest.cpp (revision 7120e97489acbf17d86d3f33e3b2e68974fd4b23)
1 /*****************************************************************************/
2 // OpenBeOS Translation Kit Test
3 // Author: Brian Matzon <brian@matzon.dk>
4 // Version: 0.1.0
5 //
6 // This is the Test application for BBitmapStream
7 //
8 //
9 // This application and all source files used in its construction, except
10 // where noted, are licensed under the MIT License, and have been written
11 // and are:
12 //
13 // Copyright (c) 2002 OpenBeOS Project
14 //
15 // Permission is hereby granted, free of charge, to any person obtaining a
16 // copy of this software and associated documentation files (the "Software"),
17 // to deal in the Software without restriction, including without limitation
18 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
19 // and/or sell copies of the Software, and to permit persons to whom the
20 // Software is furnished to do so, subject to the following conditions:
21 //
22 // The above copyright notice and this permission notice shall be included
23 // in all copies or substantial portions of the Software.
24 //
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
26 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31 // DEALINGS IN THE SOFTWARE.
32 /*****************************************************************************/
33 #include "BitmapStreamTest.h"
34 #include <stdio.h>
35 #include <TranslatorRoster.h>
36 #include <Application.h>
37 #include <Bitmap.h>
38 
39 /* cppunit framework */
40 #include <cppunit/Test.h>
41 #include <cppunit/TestCaller.h>
42 #include <cppunit/TestSuite.h>
43 
44 /**
45  * Default constructor - no work
46  */
47 BitmapStreamTest::BitmapStreamTest(std::string name)
48 	: BTestCase(name)
49 {
50 }
51 
52 /**
53  * Default destructor - no work
54  */
55 BitmapStreamTest::~BitmapStreamTest()
56 {
57 }
58 
59 CppUnit::Test *
60 BitmapStreamTest::Suite()
61 {
62 	/* create our suite */
63 	CppUnit::TestSuite *suite = new CppUnit::TestSuite("BitmapStream");
64 
65 	/* add suckers */
66 	suite->addTest(new CppUnit::TestCaller<BitmapStreamTest>("BitmapStreamTest::Constructor Test", &BitmapStreamTest::ConstructorTest));
67 	suite->addTest(new CppUnit::TestCaller<BitmapStreamTest>("BitmapStreamTest::DetachBitmap Test", &BitmapStreamTest::DetachBitmapTest));
68 	suite->addTest(new CppUnit::TestCaller<BitmapStreamTest>("BitmapStreamTest::ReadWrite Test", &BitmapStreamTest::ReadWriteTest));
69 	suite->addTest(new CppUnit::TestCaller<BitmapStreamTest>("BitmapStreamTest::Seek Test", &BitmapStreamTest::SeekTest));
70 	suite->addTest(new CppUnit::TestCaller<BitmapStreamTest>("BitmapStreamTest::SetSize Test", &BitmapStreamTest::SetSizeTest));
71 
72 	return suite;
73 }
74 
75 /**
76  * Tests:
77  * BBitmapStream(BBitmap *map = NULL)
78  */
79 void
80 BitmapStreamTest::ConstructorTest()
81 {
82 	//BBitmapStream with a blank bitmap
83 	NextSubTest();
84 	BBitmapStream streamEmpty;
85 	BBitmap *pbits = NULL;
86 	CPPUNIT_ASSERT(streamEmpty.Position() == 0);
87 	CPPUNIT_ASSERT(streamEmpty.Size() == 0);
88 	CPPUNIT_ASSERT(streamEmpty.DetachBitmap(&pbits) == B_ERROR);
89 	CPPUNIT_ASSERT(pbits == NULL);
90 
91 	//constructor is created with a value in DetachBitmapTest()
92 }
93 
94 /**
95  * Tests:
96  * status_t DetachBitmap(BBitmap **outMap)
97  */
98 void
99 BitmapStreamTest::DetachBitmapTest()
100 {
101 	BApplication
102 		app("application/x-vnd.OpenBeOS-translationkit_bitmapstreamtest");
103 
104 	NextSubTest();
105 	BFile file("../src/tests/kits/translation/data/images/image.jpg",
106 		B_READ_ONLY);
107 	CPPUNIT_ASSERT(file.InitCheck() == B_OK);
108 
109 	//translate a file into a BBitmapStream
110 	NextSubTest();
111 	BBitmapStream *pstream = new BBitmapStream;
112 	BBitmap *pbits = NULL;
113 	BTranslatorRoster *proster = BTranslatorRoster::Default();
114 	CPPUNIT_ASSERT(proster->Translate(&file, NULL, NULL, pstream,
115 		B_TRANSLATOR_BITMAP) == B_OK);
116 	CPPUNIT_ASSERT(pstream->DetachBitmap(&pbits) == B_NO_ERROR);
117 	CPPUNIT_ASSERT(pbits);
118 	CPPUNIT_ASSERT(pbits->IsValid());
119 
120 	//B_BAD_VALUE
121 	NextSubTest();
122 	CPPUNIT_ASSERT(pstream->DetachBitmap(NULL) == B_BAD_VALUE);
123 
124 	//Attempt double detach
125 	NextSubTest();
126 	BBitmap *pdbits = NULL;
127 	CPPUNIT_ASSERT(pstream->DetachBitmap(&pdbits) == B_ERROR);
128 
129 	//make sure that deleting the stream
130 	//does not destroy the detached BBitmap
131 	NextSubTest();
132 	delete pstream;
133 	pstream = NULL;
134 	CPPUNIT_ASSERT(pbits->IsValid());
135 	CPPUNIT_ASSERT(pbits->BitsLength() > 0);
136 
137 	//create a new stream using the BBitmap
138 	//created by the first stream
139 	NextSubTest();
140 	BBitmapStream *pfullstream = new BBitmapStream(pbits);
141 	CPPUNIT_ASSERT(pfullstream->Position() == 0);
142 	CPPUNIT_ASSERT(pfullstream->Size() ==
143 		pbits->BitsLength() + sizeof(TranslatorBitmap));
144 
145 	//deleting pfullstream should also destroy
146 	//the bitmap attached to it (pbits)
147 	NextSubTest();
148 	delete pfullstream;
149 	pfullstream = NULL;
150 	CPPUNIT_ASSERT(pbits->BitsLength() == 0);
151 }
152 
153 /**
154  * Tests:
155  * ssize_t ReadAt(off_t pos, void *buffer, size_t numBytes)
156  * ssize_t WriteAt(off_t pos, void *buffer, size_t numBytes)
157  */
158 void
159 BitmapStreamTest::ReadWriteTest()
160 {
161 	NextSubTest();
162 	BApplication
163 		app("application/x-vnd.OpenBeOS-translationkit_bitmapstreamtest");
164 	char chbuf[sizeof(TranslatorBitmap)],
165 		chheader[sizeof(TranslatorBitmap)], *pch;
166 	TranslatorBitmap sheader;
167 	BBitmapStream stream;
168 	int32 width = 5, height = 5;
169 
170 	sheader.magic = B_TRANSLATOR_BITMAP;
171 	sheader.bounds.left = 0;
172 	sheader.bounds.top = 0;
173 	sheader.bounds.right = width - 1;
174 	sheader.bounds.bottom = height - 1;
175 	sheader.rowBytes = width * 4;
176 	sheader.colors = B_RGB32;
177 	sheader.dataSize = sheader.rowBytes * height;
178 
179 	memcpy(&chheader, &sheader, sizeof(TranslatorBitmap));
180 	CPPUNIT_ASSERT(swap_data(B_UINT32_TYPE, &(chheader[0]),
181 		sizeof(TranslatorBitmap), B_SWAP_HOST_TO_BENDIAN) == B_OK);
182 
183 	// Write header, 1 byte at a time
184 	NextSubTest();
185 	off_t nPos;
186 	for (nPos = 0; nPos < sizeof(TranslatorBitmap); nPos++) {
187 		pch = (char *)(&(chheader[0])) + nPos;
188 		CPPUNIT_ASSERT(stream.WriteAt(nPos, pch, 1) == 1);
189 	}
190 	// Check size
191 	NextSubTest();
192 	CPPUNIT_ASSERT(stream.Size() ==
193 		sizeof(TranslatorBitmap) + sheader.dataSize);
194 
195 	// Read header, 1 byte at a time
196 	NextSubTest();
197 	for (nPos = 0; nPos < sizeof(TranslatorBitmap); nPos++) {
198 		pch = (char *)(&(chheader[0])) + nPos;
199 		CPPUNIT_ASSERT(stream.ReadAt(nPos, &(chbuf[0]), 1) == 1);
200 		CPPUNIT_ASSERT(pch[0] == chbuf[0]);
201 	}
202 
203 	// Write header, all at once
204 	NextSubTest();
205 	pch = (char *)(&(chheader[0]));
206 	CPPUNIT_ASSERT(stream.WriteAt(0, pch,
207 		sizeof(TranslatorBitmap)) == sizeof(TranslatorBitmap));
208 	// Check size
209 	NextSubTest();
210 	CPPUNIT_ASSERT(stream.Size() ==
211 		sizeof(TranslatorBitmap) + sheader.dataSize);
212 
213 	// Read header, all at once
214 	NextSubTest();
215 	CPPUNIT_ASSERT(stream.ReadAt(0, &(chbuf[0]),
216 		sizeof(TranslatorBitmap)) == sizeof(TranslatorBitmap));
217 	CPPUNIT_ASSERT(memcmp(pch, &(chbuf[0]), sizeof(TranslatorBitmap)) == 0);
218 
219 	// Write bitmap data
220 	NextSubTest();
221 	int32 bytesLeft = sheader.dataSize;
222 	char byt = 0xCF;
223 	nPos = sizeof(TranslatorBitmap);
224 	while (bytesLeft--)
225 		CPPUNIT_ASSERT(stream.WriteAt(nPos++, &byt, 1) == 1);
226 	// Check size
227 	NextSubTest();
228 	CPPUNIT_ASSERT(stream.Size() ==
229 		sizeof(TranslatorBitmap) + sheader.dataSize);
230 
231 	// Read bitmap data
232 	NextSubTest();
233 	bytesLeft = sheader.dataSize;
234 	nPos = sizeof(TranslatorBitmap);
235 	while (bytesLeft--) {
236 		CPPUNIT_ASSERT(stream.ReadAt(nPos++, &(chbuf[0]), 1) == 1);
237 		CPPUNIT_ASSERT(chbuf[0] == byt);
238 	}
239 
240 	// Send erroneous and weird data to WriteAt()
241 	NextSubTest();
242 	CPPUNIT_ASSERT(stream.WriteAt(0, &byt, 0) == 0);
243 	CPPUNIT_ASSERT(stream.WriteAt(-1, &byt, 1) == B_BAD_VALUE);
244 	CPPUNIT_ASSERT(stream.WriteAt(stream.Size(), &byt, 1) == B_BAD_VALUE);
245 	CPPUNIT_ASSERT(stream.WriteAt(stream.Size() + 1, &byt, 1) == B_BAD_VALUE);
246 	CPPUNIT_ASSERT(stream.WriteAt(0, NULL, 1) == B_BAD_VALUE);
247 
248 	// Send erroneous and weird data to ReadAt()
249 	NextSubTest();
250 	CPPUNIT_ASSERT(stream.ReadAt(0, &(chbuf[0]), 0) == 0);
251 	CPPUNIT_ASSERT(stream.ReadAt(-1, &(chbuf[0]), 1) == B_BAD_VALUE);
252 	CPPUNIT_ASSERT(stream.ReadAt(stream.Size(), &(chbuf[0]),
253 		 1) == B_ERROR);
254 	CPPUNIT_ASSERT(stream.ReadAt(stream.Size() + 1,
255 		&(chbuf[0]), 1) == B_ERROR);
256 	CPPUNIT_ASSERT(stream.ReadAt(0, NULL, 1) == B_BAD_VALUE);
257 }
258 
259 /**
260  * Tests:
261  * off_t Seek(off_t position, uint32 whence)
262  * off_t Position() const
263  */
264 void
265 BitmapStreamTest::SeekTest()
266 {
267 	BApplication
268 		app("application/x-vnd.OpenBeOS-translationkit_bitmapstreamtest");
269 
270 	NextSubTest();
271 	BFile file("../src/tests/kits/translation/data/images/image.jpg",
272 		B_READ_ONLY);
273 	CPPUNIT_ASSERT(file.InitCheck() == B_OK);
274 
275 	//translate a file into a BBitmapStream
276 	NextSubTest();
277 	BBitmapStream stream;
278 	BTranslatorRoster *proster = BTranslatorRoster::Default();
279 	CPPUNIT_ASSERT(proster->Translate(&file, NULL, NULL, &stream,
280 		B_TRANSLATOR_BITMAP) == B_OK);
281 
282 	// Test SEEK_END
283 	NextSubTest();
284 	off_t nPos;
285 	nPos = stream.Size();
286 	CPPUNIT_ASSERT(stream.Seek(0, SEEK_END) == nPos);
287 	CPPUNIT_ASSERT(stream.Position() == nPos);
288 
289 	nPos = 0;
290 	CPPUNIT_ASSERT(stream.Seek(-stream.Size(), SEEK_END) == nPos);
291 	CPPUNIT_ASSERT(stream.Position() == nPos);
292 
293 	nPos = stream.Size() - 15;
294 	CPPUNIT_ASSERT(stream.Seek(-15, SEEK_END) == nPos);
295 	CPPUNIT_ASSERT(stream.Position() == nPos);
296 
297 	CPPUNIT_ASSERT(stream.Seek(1, SEEK_END) == B_BAD_VALUE);
298 	CPPUNIT_ASSERT(stream.Position() == nPos);
299 
300 	CPPUNIT_ASSERT(stream.Seek(-(stream.Size() + 1),
301 		SEEK_END) == B_BAD_VALUE);
302 	CPPUNIT_ASSERT(stream.Position() == nPos);
303 
304 	// Test SEEK_SET
305 	NextSubTest();
306 	nPos = 0;
307 	CPPUNIT_ASSERT(stream.Seek(0, SEEK_SET) == nPos);
308 	CPPUNIT_ASSERT(stream.Position() == nPos);
309 
310 	nPos = stream.Size();
311 	CPPUNIT_ASSERT(stream.Seek(nPos, SEEK_SET) == nPos);
312 	CPPUNIT_ASSERT(stream.Position() == nPos);
313 
314 	nPos /= 2;
315 	CPPUNIT_ASSERT(stream.Seek(nPos, SEEK_SET) == nPos);
316 	CPPUNIT_ASSERT(stream.Position() == nPos);
317 
318 	CPPUNIT_ASSERT(stream.Seek(-1, SEEK_SET) == B_BAD_VALUE);
319 	CPPUNIT_ASSERT(stream.Position() == nPos);
320 
321 	CPPUNIT_ASSERT(stream.Seek(stream.Size() + 1, SEEK_SET) == B_BAD_VALUE);
322 	CPPUNIT_ASSERT(stream.Position() == nPos);
323 
324 	// Test SEEK_CUR
325 	NextSubTest();
326 	CPPUNIT_ASSERT(stream.Seek(-nPos, SEEK_CUR) == 0);
327 	CPPUNIT_ASSERT(stream.Position() == 0);
328 
329 	nPos = stream.Size();
330 	CPPUNIT_ASSERT(stream.Seek(nPos, SEEK_CUR) == nPos);
331 	CPPUNIT_ASSERT(stream.Position() == nPos);
332 
333 	nPos -= 11;
334 	CPPUNIT_ASSERT(stream.Seek(-11, SEEK_CUR) == nPos);
335 	CPPUNIT_ASSERT(stream.Position() == nPos);
336 
337 	nPos += 8;
338 	CPPUNIT_ASSERT(stream.Seek(8, SEEK_CUR) == nPos);
339 	CPPUNIT_ASSERT(stream.Position() == nPos);
340 
341 	CPPUNIT_ASSERT(stream.Seek(-(stream.Position() + 1),
342 		SEEK_CUR) == B_BAD_VALUE);
343 	CPPUNIT_ASSERT(stream.Position() == nPos);
344 
345 	CPPUNIT_ASSERT(stream.Seek((stream.Size() - stream.Position()) + 1,
346 		SEEK_CUR) == B_BAD_VALUE);
347 	CPPUNIT_ASSERT(stream.Position() == nPos);
348 }
349 
350 /**
351  * Tests:
352  * status_t SetSize(off_t size) const
353  */
354 void
355 BitmapStreamTest::SetSizeTest()
356 {
357 	BApplication
358 		app("application/x-vnd.OpenBeOS-translationkit_bitmapstreamtest");
359 
360 	NextSubTest();
361 	BFile file("../src/tests/kits/translation/data/images/image.jpg",
362 		B_READ_ONLY);
363 	CPPUNIT_ASSERT(file.InitCheck() == B_OK);
364 
365 	//translate a file into a BBitmapStream
366 	NextSubTest();
367 	BBitmapStream stream;
368 	BTranslatorRoster *proster = BTranslatorRoster::Default();
369 	CPPUNIT_ASSERT(proster->Translate(&file, NULL, NULL, &stream,
370 		B_TRANSLATOR_BITMAP) == B_OK);
371 
372 	// Send crap to SetSize
373 	NextSubTest();
374 	CPPUNIT_ASSERT(stream.SetSize(-1) == B_BAD_VALUE);
375 	CPPUNIT_ASSERT(stream.SetSize(stream.Size() + 1) == B_BAD_VALUE);
376 }
377