diff --git a/src/tests/add-ons/translators/stxttranslator/STXTTranslatorTest.cpp b/src/tests/add-ons/translators/stxttranslator/STXTTranslatorTest.cpp index 251134f26e..dbc54f3eac 100644 --- a/src/tests/add-ons/translators/stxttranslator/STXTTranslatorTest.cpp +++ b/src/tests/add-ons/translators/stxttranslator/STXTTranslatorTest.cpp @@ -9,12 +9,15 @@ #include #include #include +#include #include #include #include #include #include -//#include "../../../../add-ons/translators/bmptranslator/STXTTranslator.h" +#include +#include +#include "../../../../add-ons/translators/stxttranslator/STXTTranslator.h" // Suite CppUnit::Test * @@ -22,12 +25,19 @@ STXTTranslatorTest::Suite() { CppUnit::TestSuite *suite = new CppUnit::TestSuite(); typedef CppUnit::TestCaller TC; - + suite->addTest( - new TC("STXTTranslator DummyTest", - &STXTTranslatorTest::DummyTest)); -#if !TEST_R5 + new TC("STXTTranslator IdentifyTest", + &STXTTranslatorTest::IdentifyTest)); + suite->addTest( + new TC("STXTTranslator TranslateTest", + &STXTTranslatorTest::TranslateTest)); + +#if !TEST_R5 + suite->addTest( + new TC("STXTTranslator LoadAddOnTest", + &STXTTranslatorTest::LoadAddOnTest)); #endif return suite; @@ -47,16 +57,316 @@ STXTTranslatorTest::tearDown() BTestCase::tearDown(); } +void +STXTTranslatorTest::IdentifyTest() +{ + // Init + NextSubTest(); + status_t result = B_ERROR; + BTranslatorRoster *proster = new BTranslatorRoster(); + CPPUNIT_ASSERT(proster); + CPPUNIT_ASSERT(proster->AddTranslators( + "/boot/home/config/add-ons/Translators/STXTTranslator") == B_OK); + BFile wronginput("../src/tests/kits/translation/data/images/image.jpg", + B_READ_ONLY); + CPPUNIT_ASSERT(wronginput.InitCheck() == B_OK); + BFile sinput("../src/tests/kits/translation/data/styled_text.stxt", + B_READ_ONLY); + CPPUNIT_ASSERT(sinput.InitCheck() == B_OK); + BFile tinput("../src/tests/kits/translation/data/plain_text.txt", + B_READ_ONLY); + CPPUNIT_ASSERT(tinput.InitCheck() == B_OK); + + // Identify (bad input, output types) + NextSubTest(); + translator_info ti; + memset(&ti, 0, sizeof(translator_info)); + result = proster->Identify(&wronginput, NULL, &ti, 0, + NULL, B_TRANSLATOR_BITMAP); + CPPUNIT_ASSERT(result == B_NO_TRANSLATOR); + CPPUNIT_ASSERT(ti.type == 0 && ti.translator == 0); + + // Identify (wrong type of input data) + NextSubTest(); + memset(&ti, 0, sizeof(translator_info)); + result = proster->Identify(&wronginput, NULL, &ti); + CPPUNIT_ASSERT(result == B_NO_TRANSLATOR); + CPPUNIT_ASSERT(ti.type == 0 && ti.translator == 0); + + // Identify (input: styled output: B_TRANSLATOR_ANY_TYPE) + NextSubTest(); + memset(&ti, 0, sizeof(translator_info)); + result = proster->Identify(&sinput, NULL, &ti); + CPPUNIT_ASSERT(result == B_OK); + CPPUNIT_ASSERT(ti.type == B_STYLED_TEXT_FORMAT); + CPPUNIT_ASSERT(ti.translator != 0); + CPPUNIT_ASSERT(ti.group == B_TRANSLATOR_TEXT); + CPPUNIT_ASSERT(ti.quality == 0.5); + CPPUNIT_ASSERT(ti.capability == 0.5); + CPPUNIT_ASSERT(strcmp(ti.name, "Be styled text file") == 0); + CPPUNIT_ASSERT(strcmp(ti.MIME, "text/x-vnd.Be-stxt") == 0); + + // Identify (input: styled output: B_TRANSLATOR_TEXT) + NextSubTest(); + memset(&ti, 0, sizeof(translator_info)); + result = proster->Identify(&sinput, NULL, &ti, 0, NULL, + B_TRANSLATOR_TEXT); + CPPUNIT_ASSERT(result == B_OK); + CPPUNIT_ASSERT(ti.type == B_STYLED_TEXT_FORMAT); + CPPUNIT_ASSERT(ti.translator != 0); + CPPUNIT_ASSERT(ti.group == B_TRANSLATOR_TEXT); + CPPUNIT_ASSERT(ti.quality == 0.5); + CPPUNIT_ASSERT(ti.capability == 0.5); + CPPUNIT_ASSERT(strcmp(ti.name, "Be styled text file") == 0); + CPPUNIT_ASSERT(strcmp(ti.MIME, "text/x-vnd.Be-stxt") == 0); + + // Identify (input: styled output: B_STYLED_TEXT_FORMAT) + NextSubTest(); + memset(&ti, 0, sizeof(translator_info)); + result = proster->Identify(&sinput, NULL, &ti, 0, NULL, + B_STYLED_TEXT_FORMAT); + CPPUNIT_ASSERT(result == B_OK); + CPPUNIT_ASSERT(ti.type == B_STYLED_TEXT_FORMAT); + CPPUNIT_ASSERT(ti.translator != 0); + CPPUNIT_ASSERT(ti.group == B_TRANSLATOR_TEXT); + CPPUNIT_ASSERT(ti.quality == 0.5); + CPPUNIT_ASSERT(ti.capability == 0.5); + CPPUNIT_ASSERT(strcmp(ti.name, "Be styled text file") == 0); + CPPUNIT_ASSERT(strcmp(ti.MIME, "text/x-vnd.Be-stxt") == 0); + + // Identify (input: plain output: B_TRANSLATOR_ANY_TYPE) + NextSubTest(); + memset(&ti, 0, sizeof(translator_info)); + result = proster->Identify(&tinput, NULL, &ti); + CPPUNIT_ASSERT(result == B_OK); + CPPUNIT_ASSERT(ti.type == B_TRANSLATOR_TEXT); + CPPUNIT_ASSERT(ti.translator != 0); + CPPUNIT_ASSERT(ti.group == B_TRANSLATOR_TEXT); + CPPUNIT_ASSERT(ti.quality > 0.39 && ti.quality < 0.41); + CPPUNIT_ASSERT(ti.capability > 0.59 && ti.capability < 0.61); + CPPUNIT_ASSERT(strcmp(ti.name, "Plain text file") == 0); + CPPUNIT_ASSERT(strcmp(ti.MIME, "text/plain") == 0); + + // Identify (input: plain output: B_TRANSLATOR_TEXT) + NextSubTest(); + memset(&ti, 0, sizeof(translator_info)); + result = proster->Identify(&tinput, NULL, &ti, 0, NULL, + B_TRANSLATOR_TEXT); + CPPUNIT_ASSERT(result == B_OK); + CPPUNIT_ASSERT(ti.type == B_TRANSLATOR_TEXT); + CPPUNIT_ASSERT(ti.translator != 0); + CPPUNIT_ASSERT(ti.group == B_TRANSLATOR_TEXT); + CPPUNIT_ASSERT(ti.quality > 0.39 && ti.quality < 0.41); + CPPUNIT_ASSERT(ti.capability > 0.59 && ti.capability < 0.61); + CPPUNIT_ASSERT(strcmp(ti.name, "Plain text file") == 0); + CPPUNIT_ASSERT(strcmp(ti.MIME, "text/plain") == 0); + + // Identify (input: plain output: B_STYLED_TEXT_FORMAT) + NextSubTest(); + memset(&ti, 0, sizeof(translator_info)); + result = proster->Identify(&tinput, NULL, &ti, 0, NULL, + B_STYLED_TEXT_FORMAT); + CPPUNIT_ASSERT(result == B_OK); + CPPUNIT_ASSERT(ti.type == B_TRANSLATOR_TEXT); + CPPUNIT_ASSERT(ti.translator != 0); + CPPUNIT_ASSERT(ti.group == B_TRANSLATOR_TEXT); + CPPUNIT_ASSERT(ti.quality > 0.39 && ti.quality < 0.41); + CPPUNIT_ASSERT(ti.capability > 0.59 && ti.capability < 0.61); + CPPUNIT_ASSERT(strcmp(ti.name, "Plain text file") == 0); + CPPUNIT_ASSERT(strcmp(ti.MIME, "text/plain") == 0); +} + +void +STXTTranslatorTest::TranslateTest() +{ + // Init + NextSubTest(); + status_t result = B_ERROR; + off_t filesize = -1; + BTranslatorRoster *proster = new BTranslatorRoster(); + CPPUNIT_ASSERT(proster); + CPPUNIT_ASSERT(proster->AddTranslators( + "/boot/home/config/add-ons/Translators/STXTTranslator") == B_OK); + BFile wronginput("../src/tests/kits/translation/data/images/image.jpg", + B_READ_ONLY); + CPPUNIT_ASSERT(wronginput.InitCheck() == B_OK); + BFile output("/tmp/stxt_test.out", B_WRITE_ONLY | + B_CREATE_FILE | B_ERASE_FILE); + CPPUNIT_ASSERT(output.InitCheck() == B_OK); + + // Translate (bad input, output types) + NextSubTest(); + result = proster->Translate(&wronginput, NULL, NULL, &output, + B_TRANSLATOR_BITMAP); + CPPUNIT_ASSERT(result == B_NO_TRANSLATOR); + CPPUNIT_ASSERT(output.GetSize(&filesize) == B_OK); + CPPUNIT_ASSERT(filesize == 0); + + // Translate (wrong type of input data) + NextSubTest(); + result = proster->Translate(&wronginput, NULL, NULL, &output, + B_TRANSLATOR_TEXT); + CPPUNIT_ASSERT(result == B_NO_TRANSLATOR); + CPPUNIT_ASSERT(output.GetSize(&filesize) == B_OK); + CPPUNIT_ASSERT(filesize == 0); + + // Translate (wrong type of input, B_TRANSLATOR_ANY_TYPE output) + NextSubTest(); + result = proster->Translate(&wronginput, NULL, NULL, &output, + B_TRANSLATOR_ANY_TYPE); + CPPUNIT_ASSERT(result == B_NO_TRANSLATOR); + CPPUNIT_ASSERT(output.GetSize(&filesize) == B_OK); + CPPUNIT_ASSERT(filesize == 0); +} + +void +TestBTranslator(STXTTranslatorTest *ptest, BTranslator *ptran) +{ + // . The translator should only have one reference + ptest->NextSubTest(); + CPPUNIT_ASSERT(ptran->ReferenceCount() == 1); + + // . Make sure Acquire returns a BTranslator even though its + // already been Acquired once + ptest->NextSubTest(); + CPPUNIT_ASSERT(ptran->Acquire() == ptran); + + // . Acquired twice, refcount should be 2 + ptest->NextSubTest(); + CPPUNIT_ASSERT(ptran->ReferenceCount() == 2); + + // . Release should return ptran because it is still acquired + ptest->NextSubTest(); + CPPUNIT_ASSERT(ptran->Release() == ptran); + + ptest->NextSubTest(); + CPPUNIT_ASSERT(ptran->ReferenceCount() == 1); + + ptest->NextSubTest(); + CPPUNIT_ASSERT(ptran->Acquire() == ptran); + + ptest->NextSubTest(); + CPPUNIT_ASSERT(ptran->ReferenceCount() == 2); + + ptest->NextSubTest(); + CPPUNIT_ASSERT(ptran->Release() == ptran); + + ptest->NextSubTest(); + CPPUNIT_ASSERT(ptran->ReferenceCount() == 1); + + // . A name would be nice + ptest->NextSubTest(); + const char *tranname = ptran->TranslatorName(); + CPPUNIT_ASSERT(tranname); + printf(" {%s} ", tranname); + + // . More info would be nice + ptest->NextSubTest(); + const char *traninfo = ptran->TranslatorInfo(); + CPPUNIT_ASSERT(traninfo); + printf(" {%s} ", traninfo); + + // . What version are you? + // (when ver == 100, that means that version is 1.00) + ptest->NextSubTest(); + int32 ver = ptran->TranslatorVersion(); + CPPUNIT_ASSERT((ver / 100) > 0); + printf(" {%d} ", (int) ver); + + // . Input formats? + ptest->NextSubTest(); + { + int32 incount = 0; + const translation_format *pins = ptran->InputFormats(&incount); + CPPUNIT_ASSERT(incount == 2); + CPPUNIT_ASSERT(pins); + // . must support STXT and TEXT formats + for (int32 i = 0; i < incount; i++) { + CPPUNIT_ASSERT(pins[i].group == B_TRANSLATOR_TEXT); + CPPUNIT_ASSERT(pins[i].quality > 0 && pins[i].quality <= 1); + CPPUNIT_ASSERT(pins[i].capability > 0 && pins[i].capability <= 1); + CPPUNIT_ASSERT(pins[i].MIME); + CPPUNIT_ASSERT(pins[i].name); + + if (pins[i].type == B_TRANSLATOR_TEXT) { + CPPUNIT_ASSERT(strcmp(pins[i].MIME, TEXT_MIME_STRING) == 0); + CPPUNIT_ASSERT(strcmp(pins[i].name, + "Plain text file") == 0); + } else if (pins[i].type == B_STYLED_TEXT_FORMAT) { + CPPUNIT_ASSERT(strcmp(pins[i].MIME, STXT_MIME_STRING) == 0); + CPPUNIT_ASSERT(strcmp(pins[i].name, "Be styled text file") == 0); + } else + CPPUNIT_ASSERT(false); + } + } + + // . Output formats? + ptest->NextSubTest(); + { + int32 outcount = 0; + const translation_format *pouts = ptran->OutputFormats(&outcount); + CPPUNIT_ASSERT(outcount == 2); + CPPUNIT_ASSERT(pouts); + // . must support STXT and TEXT formats + for (int32 i = 0; i < outcount; i++) { + CPPUNIT_ASSERT(pouts[i].group == B_TRANSLATOR_TEXT); + CPPUNIT_ASSERT(pouts[i].quality > 0 && pouts[i].quality <= 1); + CPPUNIT_ASSERT(pouts[i].capability > 0 && pouts[i].capability <= 1); + CPPUNIT_ASSERT(pouts[i].MIME); + CPPUNIT_ASSERT(pouts[i].name); + + if (pouts[i].type == B_TRANSLATOR_TEXT) { + CPPUNIT_ASSERT(strcmp(pouts[i].MIME, TEXT_MIME_STRING) == 0); + CPPUNIT_ASSERT(strcmp(pouts[i].name, + "Plain text file") == 0); + } else if (pouts[i].type == B_STYLED_TEXT_FORMAT) { + CPPUNIT_ASSERT(strcmp(pouts[i].MIME, STXT_MIME_STRING) == 0); + CPPUNIT_ASSERT(strcmp(pouts[i].name, "Be styled text file") == 0); + } else + CPPUNIT_ASSERT(false); + } + } + + // . Release should return NULL because Release has been called + // as many times as it has been acquired + ptest->NextSubTest(); + CPPUNIT_ASSERT(ptran->Release() == NULL); +} + #if !TEST_R5 -#endif // #if !TEST_R5 - -// DummyTest void -STXTTranslatorTest::DummyTest() +STXTTranslatorTest::LoadAddOnTest() { - // 0. Tautology + // . Make sure the add_on loads NextSubTest(); - printf("Hello from mars."); - CPPUNIT_ASSERT( true == true ); + const char *path = "/boot/home/config/add-ons/Translators/STXTTranslator"; + image_id image = load_add_on(path); + CPPUNIT_ASSERT(image >= 0); + + // . Load in function to make the object + NextSubTest(); + BTranslator *(*pMakeNthTranslator)(int32 n,image_id you,uint32 flags,...); + status_t err = get_image_symbol(image, "make_nth_translator", + B_SYMBOL_TYPE_TEXT, (void **)&pMakeNthTranslator); + CPPUNIT_ASSERT(!err); + + // . Make sure the function returns a pointer to a BTranslator + NextSubTest(); + BTranslator *ptran = pMakeNthTranslator(0, image, 0); + CPPUNIT_ASSERT(ptran); + + // . Make sure the function only returns one BTranslator + NextSubTest(); + CPPUNIT_ASSERT(!pMakeNthTranslator(1, image, 0)); + + // Run a number of tests on the BTranslator object + TestBTranslator(this, ptran); + // NOTE: this function Release()s ptran + + // . Unload Add-on + NextSubTest(); + CPPUNIT_ASSERT(unload_add_on(image) == B_OK); } + +#endif // #if !TEST_R5 diff --git a/src/tests/add-ons/translators/stxttranslator/STXTTranslatorTest.h b/src/tests/add-ons/translators/stxttranslator/STXTTranslatorTest.h index 4acf77cdd4..b3065342ed 100644 --- a/src/tests/add-ons/translators/stxttranslator/STXTTranslatorTest.h +++ b/src/tests/add-ons/translators/stxttranslator/STXTTranslatorTest.h @@ -6,6 +6,9 @@ #include #include +#define TEXT_MIME_STRING "text/plain" +#define STXT_MIME_STRING "text/x-vnd.Be-stxt" + class CppUnit::Test; class STXTTranslatorTest : public BTestCase { @@ -22,10 +25,10 @@ public: // Test functions //------------------------------------------------------------ #if !TEST_R5 - + void LoadAddOnTest(); #endif - - void DummyTest(); + void IdentifyTest(); + void TranslateTest(); }; #endif // STXT_TRANSLATOR_TEST_H