xref: /haiku/src/tests/kits/translation/TranslationUtilsTest.cpp (revision e81a954787e50e56a7f06f72705b7859b6ab06d1)
1 /*****************************************************************************/
2 // OpenBeOS Translation Kit Test
3 // Author: Michael Wilber
4 // Version:
5 //
6 // This is the Test application for BTranslationUtils
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 "TranslationUtilsTest.h"
34 #include <TranslatorFormats.h>
35 	// for B_TRANSLATOR_EXT_*
36 #include <TranslatorRoster.h>
37 #include <Application.h>
38 #include <TextView.h>
39 #include <Menu.h>
40 #include <MenuItem.h>
41 #include <Bitmap.h>
42 #include <BitmapStream.h>
43 #include <Entry.h>
44 #include <OS.h>
45 
46 #include <stdio.h>
47 #include <string.h>
48 
49 /* cppunit framework */
50 #include <cppunit/Test.h>
51 #include <cppunit/TestCaller.h>
52 #include <cppunit/TestSuite.h>
53 
54 /**
55  * Default constructor - no work
56  */
57 TranslationUtilsTest::TranslationUtilsTest(std::string name)
58 	: BTestCase(name)
59 {
60 }
61 
62 /**
63  * Default destructor - no work
64  */
65 TranslationUtilsTest::~TranslationUtilsTest()
66 {
67 }
68 
69 CppUnit::Test *
70 TranslationUtilsTest::Suite()
71 {
72 	/* create our suite */
73 	CppUnit::TestSuite *suite = new CppUnit::TestSuite("TranslationUtils");
74 
75 	/* add suckers */
76 	suite->addTest(new CppUnit::TestCaller<TranslationUtilsTest>("TranslationUtilsTest::GetBitmap Test", &TranslationUtilsTest::GetBitmapTest));
77 	suite->addTest(new CppUnit::TestCaller<TranslationUtilsTest>("TranslationUtilsTest::GetPutStyledText Test", &TranslationUtilsTest::GetPutStyledTextTest));
78 	suite->addTest(new CppUnit::TestCaller<TranslationUtilsTest>("TranslationUtilsTest::GetDefaultSettings Test", &TranslationUtilsTest::GetDefaultSettingsTest));
79 	suite->addTest(new CppUnit::TestCaller<TranslationUtilsTest>("TranslationUtilsTest::AddTranslationItems Test", &TranslationUtilsTest::AddTranslationItemsTest));
80 
81 	return suite;
82 }
83 
84 void
85 CheckBitmap(BBitmap *pbits)
86 {
87 	CPPUNIT_ASSERT(pbits);
88 	CPPUNIT_ASSERT(pbits->Bits());
89 	CPPUNIT_ASSERT(pbits->BitsLength() == 443904);
90 	CPPUNIT_ASSERT(pbits->BytesPerRow() == 1536);
91 	CPPUNIT_ASSERT(pbits->Bounds().IntegerWidth() == 383);
92 	CPPUNIT_ASSERT(pbits->Bounds().IntegerHeight() == 288);
93 }
94 
95 void
96 TranslationUtilsTest::GetBitmapTest()
97 {
98 	// File
99 	NextSubTest();
100 	BApplication app(
101 		"application/x-vnd.OpenBeOS-translationkit_translationutilstest");
102 	BBitmap *pbits = NULL;
103 	pbits = BTranslationUtils::GetBitmap(
104 		"../../src/tests/kits/translation/data/images/image.png");
105 	CheckBitmap(pbits);
106 	delete pbits;
107 	pbits = NULL;
108 
109 	// File (passing it a file that isn't actually there)
110 	pbits = BTranslationUtils::GetBitmap("/tmp/no-file-here.bmp");
111 	CPPUNIT_ASSERT(pbits == NULL);
112 
113 	// File (GetBitmapFile)
114 	NextSubTest();
115 	pbits = BTranslationUtils::GetBitmapFile(
116 		"../../src/tests/kits/translation/data/images/image.png");
117 	CheckBitmap(pbits);
118 	delete pbits;
119 	pbits = NULL;
120 
121 	// File (passing GetBitmapFile a file that isn't there)
122 	NextSubTest();
123 	pbits = BTranslationUtils::GetBitmapFile("/tmp/no-file-here.bmp");
124 	CPPUNIT_ASSERT(pbits == NULL);
125 
126 	// File (entry_ref)
127 	NextSubTest();
128 	entry_ref ref;
129 	BEntry bent(
130 		"../src/tests/kits/translation/data/images/image.png");
131 	CPPUNIT_ASSERT(bent.InitCheck() == B_OK);
132 	CPPUNIT_ASSERT(bent.GetRef(&ref) == B_OK);
133 	pbits = BTranslationUtils::GetBitmap(&ref);
134 	CheckBitmap(pbits);
135 	delete pbits;
136 	pbits = NULL;
137 
138 	// File (NULL entry_ref)
139 	NextSubTest();
140 	entry_ref *pref = NULL;
141 	pbits = BTranslationUtils::GetBitmap(pref);
142 	CPPUNIT_ASSERT(pbits == NULL);
143 
144 	// Resource
145 	NextSubTest();
146 	pbits = BTranslationUtils::GetBitmap("res_image");
147 	CheckBitmap(pbits);
148 	delete pbits;
149 	pbits = NULL;
150 
151 	// Resource (bad resource name)
152 	NextSubTest();
153 	pbits = BTranslationUtils::GetBitmap("Michael Wilber");
154 	CPPUNIT_ASSERT(pbits == NULL);
155 
156 	// Resource by Type & Id
157 	NextSubTest();
158 	pbits = BTranslationUtils::GetBitmap(
159 		B_TRANSLATOR_BITMAP, 246);
160 	CheckBitmap(pbits);
161 	delete pbits;
162 	pbits = NULL;
163 
164 	// Resource by Type & Id (wrong resource type)
165 	NextSubTest();
166 	pbits = BTranslationUtils::GetBitmap(B_TRANSLATOR_TEXT, 246);
167 	CPPUNIT_ASSERT(pbits == NULL);
168 
169 	// Resource by Type & Id (wrong resource Id)
170 	NextSubTest();
171 	pbits = BTranslationUtils::GetBitmap(B_TRANSLATOR_BITMAP,
172 		166);
173 	CPPUNIT_ASSERT(pbits == NULL);
174 
175 	// Resource by Type & Name
176 	NextSubTest();
177 	pbits = BTranslationUtils::GetBitmap(
178 		B_TRANSLATOR_BITMAP, "res_image");
179 	CheckBitmap(pbits);
180 	delete pbits;
181 	pbits = NULL;
182 
183 	// Resource by Type & Name (wrong resource type)
184 	NextSubTest();
185 	pbits = BTranslationUtils::GetBitmap(B_TRANSLATOR_TEXT,
186 		"res_image");
187 	CPPUNIT_ASSERT(pbits == NULL);
188 
189 	// Resource by Type & Name (wrong resource name)
190 	NextSubTest();
191 	pbits = BTranslationUtils::GetBitmap(B_TRANSLATOR_BITMAP,
192 		"Michael Wilber");
193 	CPPUNIT_ASSERT(pbits == NULL);
194 
195 	// Stream (open file, translate to BBitmapStream,
196 	// pass that BBitmapStream to GetBitmap)
197 	NextSubTest();
198 	BFile imgfile(
199 		"../src/tests/kits/translation/data/images/image.png",
200 		B_READ_ONLY);
201 	CPPUNIT_ASSERT(imgfile.InitCheck() == B_OK);
202 	BTranslatorRoster *proster = BTranslatorRoster::Default();
203 	CPPUNIT_ASSERT(proster);
204 	BBitmapStream stream;
205 	CPPUNIT_ASSERT(proster->Translate(&imgfile, NULL, NULL,
206 		&stream, B_TRANSLATOR_BITMAP) == B_OK);
207 	pbits = BTranslationUtils::GetBitmap(&stream);
208 	CheckBitmap(pbits);
209 	delete pbits;
210 	pbits = NULL;
211 }
212 
213 void
214 TranslationUtilsTest::GetPutStyledTextTest()
215 {
216 	// Insert text into view with styles OFF
217 	NextSubTest();
218 	BApplication app(
219 		"application/x-vnd.OpenBeOS-translationkit_translationutilstest");
220 	BTextView *ptextview = NULL;
221 	ptextview = new BTextView(BRect(0, 0, 100, 100),
222 		"utilstest_textview", BRect(0, 0, 100, 100),
223 		B_FOLLOW_ALL_SIDES, B_WILL_DRAW | B_PULSE_NEEDED);
224 	CPPUNIT_ASSERT(ptextview);
225 	ptextview->SetStylable(false);
226 	BFile stylfile(
227 		"../src/tests/kits/translation/data/text/sentence.stxt",
228 		B_READ_ONLY);
229 	CPPUNIT_ASSERT(stylfile.InitCheck() == B_OK);
230 	CPPUNIT_ASSERT(BTranslationUtils::GetStyledText(&stylfile, ptextview) == B_OK);
231 	CPPUNIT_ASSERT(ptextview->TextLength() == 77);
232 	CPPUNIT_ASSERT(strcmp(ptextview->Text(),
233 		"Ask not what open source can do for you, ask what you can do for open source.") == 0);
234 
235 	// Insert text into view with styles ON
236 	NextSubTest();
237 	ptextview->SetText("");
238 	CPPUNIT_ASSERT(ptextview->TextLength() == 0);
239 	ptextview->SetStylable(true);
240 	CPPUNIT_ASSERT(BTranslationUtils::GetStyledText(&stylfile, ptextview) == B_OK);
241 	CPPUNIT_ASSERT(ptextview->TextLength() == 77);
242 	CPPUNIT_ASSERT(strcmp(ptextview->Text(),
243 		"Ask not what open source can do for you, ask what you can do for open source.") == 0);
244 
245 	// Write current contents of the BTextView to a file, and make
246 	// sure that it matches the source file exactly
247 	NextSubTest();
248 	BFile tmpfile("/tmp/out_styled_text.stxt", B_READ_WRITE | B_CREATE_FILE | B_ERASE_FILE);
249 	CPPUNIT_ASSERT(tmpfile.InitCheck() == B_OK);
250 	CPPUNIT_ASSERT(BTranslationUtils::PutStyledText(ptextview, &tmpfile) == B_OK);
251 	off_t stylsize = 0, tmpsize = 0;
252 	CPPUNIT_ASSERT(stylfile.GetSize(&stylsize) == B_OK);
253 	CPPUNIT_ASSERT(tmpfile.GetSize(&tmpsize) == B_OK);
254 	CPPUNIT_ASSERT(stylsize == tmpsize);
255 	char *stylbuf = NULL, *tmpbuf = NULL;
256 	stylbuf = new char[stylsize];
257 	tmpbuf = new char[tmpsize];
258 	CPPUNIT_ASSERT(stylbuf && tmpbuf);
259 	CPPUNIT_ASSERT(stylfile.ReadAt(0, stylbuf, stylsize) == stylsize);
260 	CPPUNIT_ASSERT(tmpfile.ReadAt(0, tmpbuf, tmpsize) == tmpsize);
261 	CPPUNIT_ASSERT(memcmp(stylbuf, tmpbuf, stylsize) == 0);
262 	delete[] stylbuf;
263 	delete[] tmpbuf;
264 	stylbuf = NULL;
265 	tmpbuf = NULL;
266 
267 	// Send NULL pointers to GetStyledText
268 	NextSubTest();
269 	CPPUNIT_ASSERT(BTranslationUtils::GetStyledText(NULL, ptextview) == B_BAD_VALUE);
270 	CPPUNIT_ASSERT(BTranslationUtils::GetStyledText(&stylfile, NULL) == B_BAD_VALUE);
271 
272 	// Send NULL pointers to PutStyledText
273 	NextSubTest();
274 	CPPUNIT_ASSERT(BTranslationUtils::PutStyledText(NULL, &tmpfile) == B_BAD_VALUE);
275 	CPPUNIT_ASSERT(BTranslationUtils::PutStyledText(ptextview, NULL) == B_BAD_VALUE);
276 }
277 
278 void
279 TranslationUtilsTest::GetDefaultSettingsTest()
280 {
281 	// Test translator_id version with a BTranslatorRoster * supplied
282 	NextSubTest();
283 	BApplication app(
284 		"application/x-vnd.OpenBeOS-translationkit_translationutilstest");
285 	BMessage *pmsg = NULL;
286 	bool bdummy = false;
287 	translator_id *pids = NULL;
288 	int32 ncount = 0;
289 	BTranslatorRoster *proster = BTranslatorRoster::Default();
290 	CPPUNIT_ASSERT(proster);
291 	CPPUNIT_ASSERT(proster->GetAllTranslators(&pids, &ncount) == B_OK);
292 	CPPUNIT_ASSERT(pids && ncount > 0);
293 	pmsg = BTranslationUtils::GetDefaultSettings(pids[0], proster);
294 	CPPUNIT_ASSERT(pmsg);
295 	delete pmsg;
296 	pmsg = NULL;
297 
298 	// Test translator_id version without a BTranslatorRoster supplied
299 	NextSubTest();
300 	pmsg = BTranslationUtils::GetDefaultSettings(pids[0]);
301 	CPPUNIT_ASSERT(pmsg);
302 	delete pmsg;
303 	pmsg = NULL;
304 	delete[] pids;
305 	pids = NULL;
306 
307 	// Get settings from the OBOS TGATranslator and ensure that
308 	// all of its settings are there
309 	NextSubTest();
310 	pmsg = BTranslationUtils::GetDefaultSettings("TGA Images", 100);
311 	CPPUNIT_ASSERT(pmsg);
312 	CPPUNIT_ASSERT(pmsg->FindBool(B_TRANSLATOR_EXT_HEADER_ONLY,
313 		&bdummy) == B_OK);
314 	CPPUNIT_ASSERT(pmsg->FindBool(B_TRANSLATOR_EXT_DATA_ONLY,
315 		&bdummy) == B_OK);
316 	CPPUNIT_ASSERT(pmsg->FindBool("tga /rle", &bdummy) == B_OK);
317 	delete pmsg;
318 	pmsg = NULL;
319 
320 	// Try to get a translator that isn't there
321 	NextSubTest();
322 	pmsg = BTranslationUtils::GetDefaultSettings("Michael Wilber", 0);
323 	CPPUNIT_ASSERT(pmsg == NULL);
324 
325 	// Try to get a version of a translator that we don't have
326 	NextSubTest();
327 	pmsg = BTranslationUtils::GetDefaultSettings("PPM Images", 1);
328 	CPPUNIT_ASSERT(pmsg == NULL);
329 }
330 
331 void
332 TranslationUtilsTest::AddTranslationItemsTest()
333 {
334 	// Make menu of bitmap translators
335 	NextSubTest();
336 	BApplication app(
337 		"application/x-vnd.OpenBeOS-translationkit_translationutilstest");
338 	BMenu *pmenu = new BMenu("utilstest_menu");
339 	CPPUNIT_ASSERT(pmenu);
340 	CPPUNIT_ASSERT(BTranslationUtils::AddTranslationItems(pmenu,
341 		B_TRANSLATOR_BITMAP) == B_OK);
342 	CPPUNIT_ASSERT(pmenu->CountItems() > 0);
343 
344 	// Make menu of text translators
345 	NextSubTest();
346 	int32 nitem = 0;
347 	while (pmenu->CountItems())
348 		delete pmenu->RemoveItem(nitem);
349 	CPPUNIT_ASSERT(pmenu->CountItems() == 0);
350 	CPPUNIT_ASSERT(BTranslationUtils::AddTranslationItems(pmenu,
351 		B_TRANSLATOR_TEXT) == B_OK);
352 	CPPUNIT_ASSERT(pmenu->CountItems() > 0);
353 
354 	// Make menu of 1 translator
355 	NextSubTest();
356 	while (pmenu->CountItems())
357 		delete pmenu->RemoveItem(nitem);
358 	CPPUNIT_ASSERT(pmenu->CountItems() == 0);
359 	BTranslatorRoster *proster = new BTranslatorRoster();
360 	CPPUNIT_ASSERT(proster);
361 	CPPUNIT_ASSERT(proster->AddTranslators(
362 		"/boot/beos/system/add-ons/Translators/PPMTranslator"
363 		) == B_OK);
364 	CPPUNIT_ASSERT(BTranslationUtils::AddTranslationItems(pmenu,
365 		B_TRANSLATOR_BITMAP, NULL, NULL, NULL, proster) == B_OK);
366 	CPPUNIT_ASSERT(pmenu->CountItems() == 1);
367 	delete proster;
368 	proster = NULL;
369 
370 	delete pmenu;
371 	pmenu = NULL;
372 
373 	// Bad Input
374 	NextSubTest();
375 	CPPUNIT_ASSERT(BTranslationUtils::AddTranslationItems(NULL,
376 		B_TRANSLATOR_BITMAP) == B_BAD_VALUE);
377 }
378 
379