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