added quite a few more tests, including tests for failures

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@2242 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Matthew Wilber
2002-12-14 22:53:32 +00:00
parent 1d9d5fec0e
commit 3bc3ad25ab
2 changed files with 158 additions and 10 deletions
@@ -36,6 +36,9 @@
// for B_TRANSLATOR_EXT_*
#include <TranslatorRoster.h>
#include <Application.h>
#include <TextView.h>
#include <Menu.h>
#include <MenuItem.h>
#include <Bitmap.h>
#include <BitmapStream.h>
#include <Entry.h>
@@ -69,8 +72,7 @@ TranslationUtilsTest::Suite()
/* add suckers */
suite->addTest(new CppUnit::TestCaller<TranslationUtilsTest>("TranslationUtilsTest::GetBitmap Test", &TranslationUtilsTest::GetBitmapTest));
suite->addTest(new CppUnit::TestCaller<TranslationUtilsTest>("TranslationUtilsTest::GetStyledText Test", &TranslationUtilsTest::GetStyledTextTest));
suite->addTest(new CppUnit::TestCaller<TranslationUtilsTest>("TranslationUtilsTest::PutStyledText Test", &TranslationUtilsTest::PutStyledTextTest));
suite->addTest(new CppUnit::TestCaller<TranslationUtilsTest>("TranslationUtilsTest::GetPutStyledText Test", &TranslationUtilsTest::GetPutStyledTextTest));
suite->addTest(new CppUnit::TestCaller<TranslationUtilsTest>("TranslationUtilsTest::GetDefaultSettings Test", &TranslationUtilsTest::GetDefaultSettingsTest));
suite->addTest(new CppUnit::TestCaller<TranslationUtilsTest>("TranslationUtilsTest::AddTranslationItems Test", &TranslationUtilsTest::AddTranslationItemsTest));
@@ -102,6 +104,10 @@ TranslationUtilsTest::GetBitmapTest()
delete pbits;
pbits = NULL;
// File (passing it a file that isn't actually there)
pbits = BTranslationUtils::GetBitmap("/tmp/no-file-here.bmp");
CPPUNIT_ASSERT(pbits == NULL);
// File (GetBitmapFile)
NextSubTest();
pbits = BTranslationUtils::GetBitmapFile(
@@ -110,6 +116,11 @@ TranslationUtilsTest::GetBitmapTest()
delete pbits;
pbits = NULL;
// File (passing GetBitmapFile a file that isn't there)
NextSubTest();
pbits = BTranslationUtils::GetBitmapFile("/tmp/no-file-here.bmp");
CPPUNIT_ASSERT(pbits == NULL);
// File (entry_ref)
NextSubTest();
entry_ref ref;
@@ -122,6 +133,12 @@ TranslationUtilsTest::GetBitmapTest()
delete pbits;
pbits = NULL;
// File (NULL entry_ref)
NextSubTest();
entry_ref *pref = NULL;
pbits = BTranslationUtils::GetBitmap(pref);
CPPUNIT_ASSERT(pbits == NULL);
// Resource
NextSubTest();
pbits = BTranslationUtils::GetBitmap("res_image");
@@ -129,6 +146,11 @@ TranslationUtilsTest::GetBitmapTest()
delete pbits;
pbits = NULL;
// Resource (bad resource name)
NextSubTest();
pbits = BTranslationUtils::GetBitmap("Michael Wilber");
CPPUNIT_ASSERT(pbits == NULL);
// Resource by Type & Id
NextSubTest();
pbits = BTranslationUtils::GetBitmap(
@@ -137,6 +159,17 @@ TranslationUtilsTest::GetBitmapTest()
delete pbits;
pbits = NULL;
// Resource by Type & Id (wrong resource type)
NextSubTest();
pbits = BTranslationUtils::GetBitmap(B_TRANSLATOR_TEXT, 246);
CPPUNIT_ASSERT(pbits == NULL);
// Resource by Type & Id (wrong resource Id)
NextSubTest();
pbits = BTranslationUtils::GetBitmap(B_TRANSLATOR_BITMAP,
166);
CPPUNIT_ASSERT(pbits == NULL);
// Resource by Type & Name
NextSubTest();
pbits = BTranslationUtils::GetBitmap(
@@ -145,6 +178,18 @@ TranslationUtilsTest::GetBitmapTest()
delete pbits;
pbits = NULL;
// Resource by Type & Name (wrong resource type)
NextSubTest();
pbits = BTranslationUtils::GetBitmap(B_TRANSLATOR_TEXT,
"res_image");
CPPUNIT_ASSERT(pbits == NULL);
// Resource by Type & Name (wrong resource name)
NextSubTest();
pbits = BTranslationUtils::GetBitmap(B_TRANSLATOR_BITMAP,
"Michael Wilber");
CPPUNIT_ASSERT(pbits == NULL);
// Stream (open file, translate to BBitmapStream,
// pass that BBitmapStream to GetBitmap)
NextSubTest();
@@ -164,15 +209,68 @@ TranslationUtilsTest::GetBitmapTest()
}
void
TranslationUtilsTest::GetStyledTextTest()
TranslationUtilsTest::GetPutStyledTextTest()
{
// Insert text into view with styles OFF
NextSubTest();
}
void
TranslationUtilsTest::PutStyledTextTest()
{
BApplication app(
"application/x-vnd.OpenBeOS-translationkit_translationutilstest");
BTextView *ptextview = NULL;
ptextview = new BTextView(BRect(0, 0, 100, 100),
"utilstest_textview", BRect(0, 0, 100, 100),
B_FOLLOW_ALL_SIDES, B_WILL_DRAW | B_PULSE_NEEDED);
CPPUNIT_ASSERT(ptextview);
ptextview->SetStylable(false);
BFile stylfile(
"../src/tests/kits/translation/data/styled_text.stxt",
B_READ_ONLY);
CPPUNIT_ASSERT(stylfile.InitCheck() == B_OK);
CPPUNIT_ASSERT(BTranslationUtils::GetStyledText(&stylfile, ptextview) == B_OK);
CPPUNIT_ASSERT(ptextview->TextLength() == 77);
CPPUNIT_ASSERT(strcmp(ptextview->Text(),
"Ask not what open source can do for you, ask what you can do for open source.") == 0);
// Insert text into view with styles ON
NextSubTest();
ptextview->SetText("");
CPPUNIT_ASSERT(ptextview->TextLength() == 0);
ptextview->SetStylable(true);
CPPUNIT_ASSERT(BTranslationUtils::GetStyledText(&stylfile, ptextview) == B_OK);
CPPUNIT_ASSERT(ptextview->TextLength() == 77);
CPPUNIT_ASSERT(strcmp(ptextview->Text(),
"Ask not what open source can do for you, ask what you can do for open source.") == 0);
// Write current contents of the BTextView to a file, and make
// sure that it matches the source file exactly
NextSubTest();
BFile tmpfile("/tmp/out_styled_text.stxt", B_READ_WRITE | B_CREATE_FILE | B_ERASE_FILE);
CPPUNIT_ASSERT(tmpfile.InitCheck() == B_OK);
CPPUNIT_ASSERT(BTranslationUtils::PutStyledText(ptextview, &tmpfile) == B_OK);
off_t stylsize = 0, tmpsize = 0;
CPPUNIT_ASSERT(stylfile.GetSize(&stylsize) == B_OK);
CPPUNIT_ASSERT(tmpfile.GetSize(&tmpsize) == B_OK);
CPPUNIT_ASSERT(stylsize == tmpsize);
char *stylbuf = NULL, *tmpbuf = NULL;
stylbuf = new char[stylsize];
tmpbuf = new char[tmpsize];
CPPUNIT_ASSERT(stylbuf && tmpbuf);
CPPUNIT_ASSERT(stylfile.ReadAt(0, stylbuf, stylsize) == stylsize);
CPPUNIT_ASSERT(tmpfile.ReadAt(0, tmpbuf, tmpsize) == tmpsize);
CPPUNIT_ASSERT(memcmp(stylbuf, tmpbuf, stylsize) == 0);
delete[] stylbuf;
delete[] tmpbuf;
stylbuf = NULL;
tmpbuf = NULL;
// Send NULL pointers to GetStyledText
NextSubTest();
CPPUNIT_ASSERT(BTranslationUtils::GetStyledText(NULL, ptextview) == B_BAD_VALUE);
CPPUNIT_ASSERT(BTranslationUtils::GetStyledText(&stylfile, NULL) == B_BAD_VALUE);
// Send NULL pointers to PutStyledText
NextSubTest();
CPPUNIT_ASSERT(BTranslationUtils::PutStyledText(NULL, &tmpfile) == B_BAD_VALUE);
CPPUNIT_ASSERT(BTranslationUtils::PutStyledText(ptextview, NULL) == B_BAD_VALUE);
}
void
@@ -216,11 +314,62 @@ TranslationUtilsTest::GetDefaultSettingsTest()
CPPUNIT_ASSERT(pmsg->FindBool("tga /rle", &bdummy) == B_OK);
delete pmsg;
pmsg = NULL;
// Try to get a translator that isn't there
NextSubTest();
pmsg = BTranslationUtils::GetDefaultSettings("Michael Wilber", 0);
CPPUNIT_ASSERT(pmsg == NULL);
// Try to get a version of a translator that we don't have
NextSubTest();
pmsg = BTranslationUtils::GetDefaultSettings("PPM Images", 1);
CPPUNIT_ASSERT(pmsg == NULL);
}
void
TranslationUtilsTest::AddTranslationItemsTest()
{
// Make menu of bitmap translators
NextSubTest();
BApplication app(
"application/x-vnd.OpenBeOS-translationkit_translationutilstest");
BMenu *pmenu = new BMenu("utilstest_menu");
CPPUNIT_ASSERT(pmenu);
CPPUNIT_ASSERT(BTranslationUtils::AddTranslationItems(pmenu,
B_TRANSLATOR_BITMAP) == B_OK);
CPPUNIT_ASSERT(pmenu->CountItems() > 0);
// Make menu of text translators
NextSubTest();
int32 nitem = 0;
while (pmenu->CountItems())
delete pmenu->RemoveItem(nitem);
CPPUNIT_ASSERT(pmenu->CountItems() == 0);
CPPUNIT_ASSERT(BTranslationUtils::AddTranslationItems(pmenu,
B_TRANSLATOR_TEXT) == B_OK);
CPPUNIT_ASSERT(pmenu->CountItems() > 0);
// Make menu of 1 translator
NextSubTest();
while (pmenu->CountItems())
delete pmenu->RemoveItem(nitem);
CPPUNIT_ASSERT(pmenu->CountItems() == 0);
BTranslatorRoster *proster = new BTranslatorRoster();
CPPUNIT_ASSERT(proster);
CPPUNIT_ASSERT(proster->AddTranslators(
"/boot/beos/system/add-ons/Translators/PPMTranslator") == B_OK);
CPPUNIT_ASSERT(BTranslationUtils::AddTranslationItems(pmenu,
B_TRANSLATOR_BITMAP, NULL, NULL, NULL, proster) == B_OK);
CPPUNIT_ASSERT(pmenu->CountItems() == 1);
delete proster;
proster = NULL;
delete pmenu;
pmenu = NULL;
// Bad Input
NextSubTest();
CPPUNIT_ASSERT(BTranslationUtils::AddTranslationItems(NULL,
B_TRANSLATOR_BITMAP) == B_BAD_VALUE);
}
@@ -49,8 +49,7 @@ public:
//actual tests
void GetBitmapTest();
void GetStyledTextTest();
void PutStyledTextTest();
void GetPutStyledTextTest();
void GetDefaultSettingsTest();
void AddTranslationItemsTest();