xref: /haiku/src/tests/add-ons/translators/stxttranslator/STXTTranslatorTest.cpp (revision 77aa0e2a5ef8e6d00570dbeb7a12aeb23e9e5cf0)
1b0ec1a79SMatthew Wilber // STXTTranslatorTest.cpp
2b0ec1a79SMatthew Wilber #include "STXTTranslatorTest.h"
3b0ec1a79SMatthew Wilber #include <cppunit/Test.h>
4b0ec1a79SMatthew Wilber #include <cppunit/TestCaller.h>
5b0ec1a79SMatthew Wilber #include <cppunit/TestSuite.h>
6b0ec1a79SMatthew Wilber #include <stdio.h>
7b0ec1a79SMatthew Wilber #include <string.h>
8b0ec1a79SMatthew Wilber #include <unistd.h>
9b0ec1a79SMatthew Wilber #include <image.h>
10b0ec1a79SMatthew Wilber #include <Translator.h>
11b0ec1a79SMatthew Wilber #include <TranslatorFormats.h>
12a2c12890SMatthew Wilber #include <TranslatorRoster.h>
13b0ec1a79SMatthew Wilber #include <Message.h>
14b0ec1a79SMatthew Wilber #include <View.h>
15b0ec1a79SMatthew Wilber #include <Rect.h>
16b0ec1a79SMatthew Wilber #include <File.h>
17b0ec1a79SMatthew Wilber #include <DataIO.h>
18a2c12890SMatthew Wilber #include <Errors.h>
19a2c12890SMatthew Wilber #include <OS.h>
20d1363d0dSMatthew Wilber #include "TranslatorTestAddOn.h"
21b0ec1a79SMatthew Wilber 
22b0ec1a79SMatthew Wilber // Suite
23b0ec1a79SMatthew Wilber CppUnit::Test *
Suite()24b0ec1a79SMatthew Wilber STXTTranslatorTest::Suite()
25b0ec1a79SMatthew Wilber {
26b0ec1a79SMatthew Wilber 	CppUnit::TestSuite *suite = new CppUnit::TestSuite();
27b0ec1a79SMatthew Wilber 	typedef CppUnit::TestCaller<STXTTranslatorTest> TC;
28b0ec1a79SMatthew Wilber 
29b0ec1a79SMatthew Wilber 	suite->addTest(
30a2c12890SMatthew Wilber 		new TC("STXTTranslator IdentifyTest",
31a2c12890SMatthew Wilber 			&STXTTranslatorTest::IdentifyTest));
32b0ec1a79SMatthew Wilber 
33a2c12890SMatthew Wilber 	suite->addTest(
34a2c12890SMatthew Wilber 		new TC("STXTTranslator TranslateTest",
35a2c12890SMatthew Wilber 			&STXTTranslatorTest::TranslateTest));
36a2c12890SMatthew Wilber 
37fd6e40cdSMatthew Wilber 	suite->addTest(
38fd6e40cdSMatthew Wilber 		new TC("STXTTranslator ConfigMessageTest",
39fd6e40cdSMatthew Wilber 			&STXTTranslatorTest::ConfigMessageTest));
40fd6e40cdSMatthew Wilber 
41a2c12890SMatthew Wilber #if !TEST_R5
42a2c12890SMatthew Wilber 	suite->addTest(
43a2c12890SMatthew Wilber 		new TC("STXTTranslator LoadAddOnTest",
44a2c12890SMatthew Wilber 			&STXTTranslatorTest::LoadAddOnTest));
45b0ec1a79SMatthew Wilber #endif
46b0ec1a79SMatthew Wilber 
47b0ec1a79SMatthew Wilber 	return suite;
48b0ec1a79SMatthew Wilber }
49b0ec1a79SMatthew Wilber 
50b0ec1a79SMatthew Wilber // setUp
51b0ec1a79SMatthew Wilber void
setUp()52b0ec1a79SMatthew Wilber STXTTranslatorTest::setUp()
53b0ec1a79SMatthew Wilber {
54b0ec1a79SMatthew Wilber 	BTestCase::setUp();
55b0ec1a79SMatthew Wilber }
56b0ec1a79SMatthew Wilber 
57b0ec1a79SMatthew Wilber // tearDown
58b0ec1a79SMatthew Wilber void
tearDown()59b0ec1a79SMatthew Wilber STXTTranslatorTest::tearDown()
60b0ec1a79SMatthew Wilber {
61b0ec1a79SMatthew Wilber 	BTestCase::tearDown();
62b0ec1a79SMatthew Wilber }
63b0ec1a79SMatthew Wilber 
64a2c12890SMatthew Wilber void
CheckStyled(translator_info * pti)65201df792SMatthew Wilber CheckStyled(translator_info *pti)
66201df792SMatthew Wilber {
67f4949fcaSMatthew Wilber 	CheckTranslatorInfo(pti, B_STYLED_TEXT_FORMAT, B_TRANSLATOR_TEXT,
68f4949fcaSMatthew Wilber 		0.5f, 0.5f, "Be styled text file", "text/x-vnd.Be-stxt");
69201df792SMatthew Wilber }
70201df792SMatthew Wilber 
71201df792SMatthew Wilber void
CheckPlain(translator_info * pti)72201df792SMatthew Wilber CheckPlain(translator_info *pti)
73201df792SMatthew Wilber {
74f4949fcaSMatthew Wilber 	CheckTranslatorInfo(pti, B_TRANSLATOR_TEXT, B_TRANSLATOR_TEXT,
75f4949fcaSMatthew Wilber 		0.4f, 0.6f, "Plain text file", "text/plain");
76201df792SMatthew Wilber }
77201df792SMatthew Wilber 
78201df792SMatthew Wilber void
IdentifyTests(STXTTranslatorTest * ptest,BTranslatorRoster * proster,const char ** paths,int32 len,bool bplain)79201df792SMatthew Wilber IdentifyTests(STXTTranslatorTest *ptest, BTranslatorRoster *proster,
80fb4fed81SMatthew Wilber 	const char **paths, int32 len, bool bplain)
81201df792SMatthew Wilber {
82201df792SMatthew Wilber 	translator_info ti;
83201df792SMatthew Wilber 	printf(" [%d] ", (int) bplain);
84201df792SMatthew Wilber 
85fb4fed81SMatthew Wilber 	for (int32 i = 0; i < len; i++) {
86201df792SMatthew Wilber 		ptest->NextSubTest();
87201df792SMatthew Wilber 		BFile file;
88201df792SMatthew Wilber 		printf(" [%s] ", paths[i]);
89201df792SMatthew Wilber 		CPPUNIT_ASSERT(file.SetTo(paths[i], B_READ_ONLY) == B_OK);
90201df792SMatthew Wilber 
91201df792SMatthew Wilber 		// Identify (output: B_TRANSLATOR_ANY_TYPE)
92201df792SMatthew Wilber 		ptest->NextSubTest();
93201df792SMatthew Wilber 		memset(&ti, 0, sizeof(translator_info));
94201df792SMatthew Wilber 		CPPUNIT_ASSERT(proster->Identify(&file, NULL, &ti) == B_OK);
95201df792SMatthew Wilber 		if (bplain)
96201df792SMatthew Wilber 			CheckPlain(&ti);
97201df792SMatthew Wilber 		else
98201df792SMatthew Wilber 			CheckStyled(&ti);
99201df792SMatthew Wilber 
100201df792SMatthew Wilber 		// Identify (output: B_TRANSLATOR_TEXT)
101201df792SMatthew Wilber 		ptest->NextSubTest();
102201df792SMatthew Wilber 		memset(&ti, 0, sizeof(translator_info));
103201df792SMatthew Wilber 		CPPUNIT_ASSERT(proster->Identify(&file, NULL, &ti, 0, NULL,
104201df792SMatthew Wilber 			B_TRANSLATOR_TEXT) == B_OK);
105201df792SMatthew Wilber 		if (bplain)
106201df792SMatthew Wilber 			CheckPlain(&ti);
107201df792SMatthew Wilber 		else
108201df792SMatthew Wilber 			CheckStyled(&ti);
109201df792SMatthew Wilber 
110201df792SMatthew Wilber 		// Identify (output: B_STYLED_TEXT_FORMAT)
111201df792SMatthew Wilber 		ptest->NextSubTest();
112201df792SMatthew Wilber 		memset(&ti, 0, sizeof(translator_info));
113201df792SMatthew Wilber 		CPPUNIT_ASSERT(proster->Identify(&file, NULL, &ti, 0, NULL,
114201df792SMatthew Wilber 			B_STYLED_TEXT_FORMAT) == B_OK);
115201df792SMatthew Wilber 		if (bplain)
116201df792SMatthew Wilber 			CheckPlain(&ti);
117201df792SMatthew Wilber 		else
118201df792SMatthew Wilber 			CheckStyled(&ti);
119201df792SMatthew Wilber 	}
120201df792SMatthew Wilber }
121201df792SMatthew Wilber 
122201df792SMatthew Wilber void
IdentifyTest()123a2c12890SMatthew Wilber STXTTranslatorTest::IdentifyTest()
124a2c12890SMatthew Wilber {
125a2c12890SMatthew Wilber 	// Init
126a2c12890SMatthew Wilber 	NextSubTest();
127a2c12890SMatthew Wilber 	status_t result = B_ERROR;
128a2c12890SMatthew Wilber 	BTranslatorRoster *proster = new BTranslatorRoster();
129a2c12890SMatthew Wilber 	CPPUNIT_ASSERT(proster);
130a2c12890SMatthew Wilber 	CPPUNIT_ASSERT(proster->AddTranslators(
131a2c12890SMatthew Wilber 		"/boot/home/config/add-ons/Translators/STXTTranslator") == B_OK);
132a2c12890SMatthew Wilber 	BFile wronginput("../src/tests/kits/translation/data/images/image.jpg",
133a2c12890SMatthew Wilber 		B_READ_ONLY);
134a2c12890SMatthew Wilber 	CPPUNIT_ASSERT(wronginput.InitCheck() == B_OK);
135a2c12890SMatthew Wilber 
136a2c12890SMatthew Wilber 	// Identify (bad input, output types)
137a2c12890SMatthew Wilber 	NextSubTest();
138a2c12890SMatthew Wilber 	translator_info ti;
139a2c12890SMatthew Wilber 	memset(&ti, 0, sizeof(translator_info));
140a2c12890SMatthew Wilber 	result = proster->Identify(&wronginput, NULL, &ti, 0,
141a2c12890SMatthew Wilber 		NULL, B_TRANSLATOR_BITMAP);
142a2c12890SMatthew Wilber 	CPPUNIT_ASSERT(result == B_NO_TRANSLATOR);
143a2c12890SMatthew Wilber 	CPPUNIT_ASSERT(ti.type == 0 && ti.translator == 0);
144a2c12890SMatthew Wilber 
145a2c12890SMatthew Wilber 	// Identify (wrong type of input data)
146a2c12890SMatthew Wilber 	NextSubTest();
147a2c12890SMatthew Wilber 	memset(&ti, 0, sizeof(translator_info));
148a2c12890SMatthew Wilber 	result = proster->Identify(&wronginput, NULL, &ti);
149a0c057d4SMatthew Wilber 	CPPUNIT_ASSERT(result == B_OK);
150a0c057d4SMatthew Wilber 	CheckTranslatorInfo(&ti, B_TRANSLATOR_TEXT, B_TRANSLATOR_TEXT, 0.4f, 0.36f,
151a0c057d4SMatthew Wilber 		"Plain text file", "text/plain");
152a2c12890SMatthew Wilber 
153201df792SMatthew Wilber 	// Identify (wrong magic)
154a2c12890SMatthew Wilber 	NextSubTest();
155a2c12890SMatthew Wilber 	memset(&ti, 0, sizeof(translator_info));
156201df792SMatthew Wilber 	BFile wrongmagic("../src/tests/kits/translation/data/text/wrong_magic.stxt",
157201df792SMatthew Wilber 		B_READ_ONLY);
158201df792SMatthew Wilber 	CPPUNIT_ASSERT(wrongmagic.InitCheck() == B_OK);
159201df792SMatthew Wilber 	result = proster->Identify(&wrongmagic, NULL, &ti);
160a0c057d4SMatthew Wilber 	CPPUNIT_ASSERT(result == B_OK);
161a0c057d4SMatthew Wilber 	CheckTranslatorInfo(&ti, B_TRANSLATOR_TEXT, B_TRANSLATOR_TEXT, 0.4f, 0.36f,
162a0c057d4SMatthew Wilber 		"Plain text file", "text/plain");
163a2c12890SMatthew Wilber 
164201df792SMatthew Wilber 	// Identify (wrong version)
165a2c12890SMatthew Wilber 	NextSubTest();
166a2c12890SMatthew Wilber 	memset(&ti, 0, sizeof(translator_info));
167201df792SMatthew Wilber 	BFile wrongversion("../src/tests/kits/translation/data/text/wrong_version.stxt",
168201df792SMatthew Wilber 		B_READ_ONLY);
169201df792SMatthew Wilber 	CPPUNIT_ASSERT(wrongversion.InitCheck() == B_OK);
170201df792SMatthew Wilber 	result = proster->Identify(&wrongversion, NULL, &ti);
171a0c057d4SMatthew Wilber 	CPPUNIT_ASSERT(result == B_OK);
172a0c057d4SMatthew Wilber 	CheckTranslatorInfo(&ti, B_TRANSLATOR_TEXT, B_TRANSLATOR_TEXT, 0.4f, 0.36f,
173a0c057d4SMatthew Wilber 		"Plain text file", "text/plain");
174a2c12890SMatthew Wilber 
175201df792SMatthew Wilber 	const char *aPlainFiles[] = {
176201df792SMatthew Wilber 		"../src/tests/kits/translation/data/text/ascii.txt",
177201df792SMatthew Wilber 		"../src/tests/kits/translation/data/text/japanese.txt",
178201df792SMatthew Wilber 		"../src/tests/kits/translation/data/text/multi_byte.txt",
179201df792SMatthew Wilber 		"../src/tests/kits/translation/data/text/one_length.txt",
180201df792SMatthew Wilber 		"../src/tests/kits/translation/data/text/sentence.txt",
181201df792SMatthew Wilber 		"../src/tests/kits/translation/data/text/symbols.txt",
182fb4fed81SMatthew Wilber 		"../src/tests/kits/translation/data/text/zero_length.txt"
183201df792SMatthew Wilber 	};
184201df792SMatthew Wilber 	const char *aStyledFiles[] = {
185201df792SMatthew Wilber 		"../src/tests/kits/translation/data/text/ascii.stxt",
186201df792SMatthew Wilber 		"../src/tests/kits/translation/data/text/japanese.stxt",
187201df792SMatthew Wilber 		"../src/tests/kits/translation/data/text/multi_byte.stxt",
188201df792SMatthew Wilber 		"../src/tests/kits/translation/data/text/one_length.stxt",
189201df792SMatthew Wilber 		"../src/tests/kits/translation/data/text/sentence.stxt",
190201df792SMatthew Wilber 		"../src/tests/kits/translation/data/text/symbols.stxt",
191201df792SMatthew Wilber 		"../src/tests/kits/translation/data/text/zero_length.stxt",
192201df792SMatthew Wilber 	};
193fb4fed81SMatthew Wilber 	IdentifyTests(this, proster, aPlainFiles,
194fb4fed81SMatthew Wilber 		sizeof(aPlainFiles) / sizeof(const char *), true);
195fb4fed81SMatthew Wilber 	IdentifyTests(this, proster, aStyledFiles,
196fb4fed81SMatthew Wilber 		sizeof(aStyledFiles) / sizeof(const char *), false);
197a2c12890SMatthew Wilber }
198a2c12890SMatthew Wilber 
199915dc0d2SMatthew Wilber void
TranslateTests(STXTTranslatorTest * ptest,BTranslatorRoster * proster,const char ** paths,int32 len,bool bplain)200915dc0d2SMatthew Wilber TranslateTests(STXTTranslatorTest *ptest, BTranslatorRoster *proster,
201915dc0d2SMatthew Wilber 	const char **paths, int32 len, bool bplain)
202915dc0d2SMatthew Wilber {
203915dc0d2SMatthew Wilber 	int32 nlongest = 0, ncurrent = 0;
204915dc0d2SMatthew Wilber 	// find the length of the longest string
205915dc0d2SMatthew Wilber 	for (int32 i = 0; i < len; i++) {
206915dc0d2SMatthew Wilber 		ncurrent = strlen(paths[i]);
207915dc0d2SMatthew Wilber 		if (ncurrent > nlongest)
208915dc0d2SMatthew Wilber 			nlongest = ncurrent;
209915dc0d2SMatthew Wilber 	}
210915dc0d2SMatthew Wilber 
211915dc0d2SMatthew Wilber 	char *styled_path = NULL, *plain_path = NULL;
212915dc0d2SMatthew Wilber 	styled_path = new char[nlongest + 6];
213915dc0d2SMatthew Wilber 	plain_path = new char[nlongest + 6];
214915dc0d2SMatthew Wilber 
215*77aa0e2aSOliver Tappe 	BFile styled_file, plain_file;
216*77aa0e2aSOliver Tappe 
217915dc0d2SMatthew Wilber 	// Perform translations on every file in the array
218915dc0d2SMatthew Wilber 	for (int32 i = 0; i < len; i++) {
219915dc0d2SMatthew Wilber 		strcpy(styled_path, paths[i]);
220915dc0d2SMatthew Wilber 		strcat(styled_path, ".stxt");
221915dc0d2SMatthew Wilber 		strcpy(plain_path, paths[i]);
222915dc0d2SMatthew Wilber 		strcat(plain_path, ".txt");
223915dc0d2SMatthew Wilber 
224915dc0d2SMatthew Wilber 		// Setup input files
225915dc0d2SMatthew Wilber 		ptest->NextSubTest();
226915dc0d2SMatthew Wilber 		CPPUNIT_ASSERT(styled_file.SetTo(styled_path, B_READ_ONLY) == B_OK);
227915dc0d2SMatthew Wilber 		CPPUNIT_ASSERT(plain_file.SetTo(plain_path, B_READ_ONLY) == B_OK);
228*77aa0e2aSOliver Tappe 
229*77aa0e2aSOliver Tappe 		BFile *pinput_file;
2309c721ddeSMatthew Wilber 		if (bplain) {
2319c721ddeSMatthew Wilber 			printf(" [%s] ", plain_path);
232915dc0d2SMatthew Wilber 			pinput_file = &plain_file;
2339c721ddeSMatthew Wilber 		} else {
2349c721ddeSMatthew Wilber 			printf(" [%s] ", styled_path);
235915dc0d2SMatthew Wilber 			pinput_file = &styled_file;
2369c721ddeSMatthew Wilber 		}
237915dc0d2SMatthew Wilber 
238d138c6bbSMatthew Wilber 		BMallocIO mallio, dmallio;
239915dc0d2SMatthew Wilber 
240915dc0d2SMatthew Wilber 		// Convert to B_TRANSLATOR_ANY_TYPE (should be B_TRANSLATOR_TEXT)
241915dc0d2SMatthew Wilber 		ptest->NextSubTest();
2429c721ddeSMatthew Wilber 		CPPUNIT_ASSERT(mallio.Seek(0, SEEK_SET) == 0);
243915dc0d2SMatthew Wilber 		CPPUNIT_ASSERT(mallio.SetSize(0) == B_OK);
244915dc0d2SMatthew Wilber 		CPPUNIT_ASSERT(proster->Translate(pinput_file, NULL, NULL, &mallio,
245915dc0d2SMatthew Wilber 			B_TRANSLATOR_ANY_TYPE) == B_OK);
246915dc0d2SMatthew Wilber 		CPPUNIT_ASSERT(CompareStreams(mallio, plain_file) == true);
247915dc0d2SMatthew Wilber 
248915dc0d2SMatthew Wilber 		// Convert to B_TRANSLATOR_TEXT
249915dc0d2SMatthew Wilber 		ptest->NextSubTest();
2509c721ddeSMatthew Wilber 		CPPUNIT_ASSERT(mallio.Seek(0, SEEK_SET) == 0);
251915dc0d2SMatthew Wilber 		CPPUNIT_ASSERT(mallio.SetSize(0) == B_OK);
252915dc0d2SMatthew Wilber 		CPPUNIT_ASSERT(proster->Translate(pinput_file, NULL, NULL, &mallio,
253915dc0d2SMatthew Wilber 			B_TRANSLATOR_TEXT) == B_OK);
254915dc0d2SMatthew Wilber 		CPPUNIT_ASSERT(CompareStreams(mallio, plain_file) == true);
255915dc0d2SMatthew Wilber 
256d138c6bbSMatthew Wilber 		// Convert plain mallio to B_TRANSLATOR_TEXT dmallio
257d138c6bbSMatthew Wilber 		ptest->NextSubTest();
258d138c6bbSMatthew Wilber 		CPPUNIT_ASSERT(dmallio.Seek(0, SEEK_SET) == 0);
259d138c6bbSMatthew Wilber 		CPPUNIT_ASSERT(dmallio.SetSize(0) == B_OK);
260d138c6bbSMatthew Wilber 		CPPUNIT_ASSERT(proster->Translate(&mallio, NULL, NULL, &dmallio,
261d138c6bbSMatthew Wilber 			B_TRANSLATOR_TEXT) == B_OK);
262d138c6bbSMatthew Wilber 		CPPUNIT_ASSERT(CompareStreams(dmallio, plain_file) == true);
263d138c6bbSMatthew Wilber 
264915dc0d2SMatthew Wilber 		// Convert to B_STYLED_TEXT_FORMAT
265915dc0d2SMatthew Wilber 		ptest->NextSubTest();
2669c721ddeSMatthew Wilber 		CPPUNIT_ASSERT(mallio.Seek(0, SEEK_SET) == 0);
267915dc0d2SMatthew Wilber 		CPPUNIT_ASSERT(mallio.SetSize(0) == B_OK);
268915dc0d2SMatthew Wilber 		CPPUNIT_ASSERT(proster->Translate(pinput_file, NULL, NULL, &mallio,
269915dc0d2SMatthew Wilber 			B_STYLED_TEXT_FORMAT) == B_OK);
270915dc0d2SMatthew Wilber 		CPPUNIT_ASSERT(CompareStreams(mallio, styled_file) == true);
271d138c6bbSMatthew Wilber 
272d138c6bbSMatthew Wilber 		// Convert styled mallio to B_TRANSLATOR_TEXT dmallio
273d138c6bbSMatthew Wilber 		ptest->NextSubTest();
274d138c6bbSMatthew Wilber 		CPPUNIT_ASSERT(dmallio.Seek(0, SEEK_SET) == 0);
275d138c6bbSMatthew Wilber 		CPPUNIT_ASSERT(dmallio.SetSize(0) == B_OK);
276d138c6bbSMatthew Wilber 		CPPUNIT_ASSERT(proster->Translate(&mallio, NULL, NULL, &dmallio,
277d138c6bbSMatthew Wilber 			B_TRANSLATOR_TEXT) == B_OK);
278d138c6bbSMatthew Wilber 		CPPUNIT_ASSERT(CompareStreams(dmallio, plain_file) == true);
279d138c6bbSMatthew Wilber 
280d138c6bbSMatthew Wilber 		// Convert styled mallio to B_STYLED_TEXT_FORMAT dmallio
281d138c6bbSMatthew Wilber 		ptest->NextSubTest();
282d138c6bbSMatthew Wilber 		CPPUNIT_ASSERT(dmallio.Seek(0, SEEK_SET) == 0);
283d138c6bbSMatthew Wilber 		CPPUNIT_ASSERT(dmallio.SetSize(0) == B_OK);
284d138c6bbSMatthew Wilber 		CPPUNIT_ASSERT(proster->Translate(&mallio, NULL, NULL, &dmallio,
285d138c6bbSMatthew Wilber 			B_STYLED_TEXT_FORMAT) == B_OK);
286d138c6bbSMatthew Wilber 		CPPUNIT_ASSERT(CompareStreams(dmallio, styled_file) == true);
287915dc0d2SMatthew Wilber 	}
288915dc0d2SMatthew Wilber 
289915dc0d2SMatthew Wilber 	delete[] styled_path;
290915dc0d2SMatthew Wilber 	styled_path = NULL;
291915dc0d2SMatthew Wilber 	delete[] plain_path;
292915dc0d2SMatthew Wilber 	plain_path = NULL;
293915dc0d2SMatthew Wilber }
294915dc0d2SMatthew Wilber 
295a2c12890SMatthew Wilber void
TranslateTest()296a2c12890SMatthew Wilber STXTTranslatorTest::TranslateTest()
297a2c12890SMatthew Wilber {
298a2c12890SMatthew Wilber 	// Init
299a2c12890SMatthew Wilber 	NextSubTest();
300a2c12890SMatthew Wilber 	status_t result = B_ERROR;
301a2c12890SMatthew Wilber 	off_t filesize = -1;
302a2c12890SMatthew Wilber 	BTranslatorRoster *proster = new BTranslatorRoster();
303a2c12890SMatthew Wilber 	CPPUNIT_ASSERT(proster);
304a2c12890SMatthew Wilber 	CPPUNIT_ASSERT(proster->AddTranslators(
305a2c12890SMatthew Wilber 		"/boot/home/config/add-ons/Translators/STXTTranslator") == B_OK);
306a2c12890SMatthew Wilber 	BFile wronginput("../src/tests/kits/translation/data/images/image.jpg",
307a2c12890SMatthew Wilber 		B_READ_ONLY);
308a2c12890SMatthew Wilber 	CPPUNIT_ASSERT(wronginput.InitCheck() == B_OK);
309a0c057d4SMatthew Wilber 	BFile output("/tmp/stxt_test.out", B_READ_WRITE |
310a2c12890SMatthew Wilber 		B_CREATE_FILE | B_ERASE_FILE);
311a2c12890SMatthew Wilber 	CPPUNIT_ASSERT(output.InitCheck() == B_OK);
312a2c12890SMatthew Wilber 
313a2c12890SMatthew Wilber 	// Translate (bad input, output types)
314a2c12890SMatthew Wilber 	NextSubTest();
315a2c12890SMatthew Wilber 	result = proster->Translate(&wronginput, NULL, NULL, &output,
316a2c12890SMatthew Wilber 		B_TRANSLATOR_BITMAP);
317a2c12890SMatthew Wilber 	CPPUNIT_ASSERT(result == B_NO_TRANSLATOR);
318a2c12890SMatthew Wilber 	CPPUNIT_ASSERT(output.GetSize(&filesize) == B_OK);
319a2c12890SMatthew Wilber 	CPPUNIT_ASSERT(filesize == 0);
320a2c12890SMatthew Wilber 
321a2c12890SMatthew Wilber 	// Translate (wrong type of input data)
322a2c12890SMatthew Wilber 	NextSubTest();
323a2c12890SMatthew Wilber 	result = proster->Translate(&wronginput, NULL, NULL, &output,
324a2c12890SMatthew Wilber 		B_TRANSLATOR_TEXT);
325a0c057d4SMatthew Wilber 	CPPUNIT_ASSERT(result == B_OK);
326a0c057d4SMatthew Wilber 	CPPUNIT_ASSERT(CompareStreams(wronginput, output) == true);
327a2c12890SMatthew Wilber 
328a2c12890SMatthew Wilber 	// Translate (wrong type of input, B_TRANSLATOR_ANY_TYPE output)
329a2c12890SMatthew Wilber 	NextSubTest();
330a0c057d4SMatthew Wilber 	CPPUNIT_ASSERT(output.Seek(0, SEEK_SET) == 0);
331a0c057d4SMatthew Wilber 	CPPUNIT_ASSERT(output.SetSize(0) == B_OK);
332a2c12890SMatthew Wilber 	result = proster->Translate(&wronginput, NULL, NULL, &output,
333a2c12890SMatthew Wilber 		B_TRANSLATOR_ANY_TYPE);
334a0c057d4SMatthew Wilber 	CPPUNIT_ASSERT(result == B_OK);
335a0c057d4SMatthew Wilber 	CPPUNIT_ASSERT(CompareStreams(wronginput, output) == true);
336915dc0d2SMatthew Wilber 
337a0c057d4SMatthew Wilber 	// Translate (wrong magic)
338915dc0d2SMatthew Wilber 	NextSubTest();
339a0c057d4SMatthew Wilber 	CPPUNIT_ASSERT(output.Seek(0, SEEK_SET) == 0);
340a0c057d4SMatthew Wilber 	CPPUNIT_ASSERT(output.SetSize(0) == B_OK);
341915dc0d2SMatthew Wilber 	BFile wrongmagic("../src/tests/kits/translation/data/text/wrong_magic.stxt",
342915dc0d2SMatthew Wilber 		B_READ_ONLY);
343915dc0d2SMatthew Wilber 	CPPUNIT_ASSERT(wrongmagic.InitCheck() == B_OK);
344915dc0d2SMatthew Wilber 	result = proster->Translate(&wrongmagic, NULL, NULL, &output,
345915dc0d2SMatthew Wilber 		B_TRANSLATOR_ANY_TYPE);
346a0c057d4SMatthew Wilber 	CPPUNIT_ASSERT(result == B_OK);
347a0c057d4SMatthew Wilber 	CPPUNIT_ASSERT(CompareStreams(wrongmagic, output) == true);
348915dc0d2SMatthew Wilber 
349a0c057d4SMatthew Wilber 	// Translate (wrong version)
350915dc0d2SMatthew Wilber 	NextSubTest();
351a0c057d4SMatthew Wilber 	CPPUNIT_ASSERT(output.Seek(0, SEEK_SET) == 0);
352a0c057d4SMatthew Wilber 	CPPUNIT_ASSERT(output.SetSize(0) == B_OK);
353915dc0d2SMatthew Wilber 	BFile wrongversion("../src/tests/kits/translation/data/text/wrong_version.stxt",
354915dc0d2SMatthew Wilber 		B_READ_ONLY);
355915dc0d2SMatthew Wilber 	CPPUNIT_ASSERT(wrongversion.InitCheck() == B_OK);
356915dc0d2SMatthew Wilber 	result = proster->Translate(&wrongversion, NULL, NULL, &output,
357915dc0d2SMatthew Wilber 		B_TRANSLATOR_ANY_TYPE);
358a0c057d4SMatthew Wilber 	CPPUNIT_ASSERT(result == B_OK);
359a0c057d4SMatthew Wilber 	CPPUNIT_ASSERT(CompareStreams(wrongversion, output) == true);
360915dc0d2SMatthew Wilber 
361915dc0d2SMatthew Wilber 	// Translate various data
3629c721ddeSMatthew Wilber 	const char *aPlainTextFiles[] = {
3639c721ddeSMatthew Wilber 		"../src/tests/kits/translation/data/text/ascii",
3649c721ddeSMatthew Wilber 		"../src/tests/kits/translation/data/text/japanese",
3659c721ddeSMatthew Wilber 		"../src/tests/kits/translation/data/text/multi_byte",
3669c721ddeSMatthew Wilber 		"../src/tests/kits/translation/data/text/one_length",
3679c721ddeSMatthew Wilber 		"../src/tests/kits/translation/data/text/symbols",
3689c721ddeSMatthew Wilber 		"../src/tests/kits/translation/data/text/zero_length"
3699c721ddeSMatthew Wilber 	};
3709c721ddeSMatthew Wilber 	const char *aStyledTextFiles[] = {
371915dc0d2SMatthew Wilber 		"../src/tests/kits/translation/data/text/ascii",
372915dc0d2SMatthew Wilber 		"../src/tests/kits/translation/data/text/japanese",
373915dc0d2SMatthew Wilber 		"../src/tests/kits/translation/data/text/multi_byte",
374915dc0d2SMatthew Wilber 		"../src/tests/kits/translation/data/text/one_length",
375915dc0d2SMatthew Wilber 		"../src/tests/kits/translation/data/text/sentence",
376915dc0d2SMatthew Wilber 		"../src/tests/kits/translation/data/text/symbols",
377915dc0d2SMatthew Wilber 		"../src/tests/kits/translation/data/text/zero_length"
378915dc0d2SMatthew Wilber 	};
3799c721ddeSMatthew Wilber 	TranslateTests(this, proster, aPlainTextFiles,
3809c721ddeSMatthew Wilber 		sizeof(aPlainTextFiles) / sizeof(const char *), true);
3819c721ddeSMatthew Wilber 	TranslateTests(this, proster, aStyledTextFiles,
3829c721ddeSMatthew Wilber 		sizeof(aStyledTextFiles) / sizeof(const char *), false);
383a2c12890SMatthew Wilber }
384a2c12890SMatthew Wilber 
385fd6e40cdSMatthew Wilber // Make certain that the STXTTranslator does not
386fd6e40cdSMatthew Wilber // provide a configuration message
387fd6e40cdSMatthew Wilber void
ConfigMessageTest()388fd6e40cdSMatthew Wilber STXTTranslatorTest::ConfigMessageTest()
389fd6e40cdSMatthew Wilber {
390fd6e40cdSMatthew Wilber 	// Init
391fd6e40cdSMatthew Wilber 	NextSubTest();
392fd6e40cdSMatthew Wilber 	BTranslatorRoster *proster = new BTranslatorRoster();
393fd6e40cdSMatthew Wilber 	CPPUNIT_ASSERT(proster);
394fd6e40cdSMatthew Wilber 	CPPUNIT_ASSERT(proster->AddTranslators(
395fd6e40cdSMatthew Wilber 		"/boot/home/config/add-ons/Translators/STXTTranslator") == B_OK);
396fd6e40cdSMatthew Wilber 
397fd6e40cdSMatthew Wilber 	// GetAllTranslators
398fd6e40cdSMatthew Wilber 	NextSubTest();
399fd6e40cdSMatthew Wilber 	translator_id tid, *pids = NULL;
400fd6e40cdSMatthew Wilber 	int32 count = 0;
401fd6e40cdSMatthew Wilber 	CPPUNIT_ASSERT(proster->GetAllTranslators(&pids, &count) == B_OK);
402fd6e40cdSMatthew Wilber 	CPPUNIT_ASSERT(pids);
403fd6e40cdSMatthew Wilber 	CPPUNIT_ASSERT(count == 1);
404fd6e40cdSMatthew Wilber 	tid = pids[0];
405fd6e40cdSMatthew Wilber 	delete[] pids;
406fd6e40cdSMatthew Wilber 	pids = NULL;
407fd6e40cdSMatthew Wilber 
408fd6e40cdSMatthew Wilber 	// GetConfigurationMessage
409fd6e40cdSMatthew Wilber 	NextSubTest();
410fd6e40cdSMatthew Wilber 	BMessage msg;
41128cac36cSMatthew Wilber 	CPPUNIT_ASSERT(proster->GetConfigurationMessage(tid, &msg) == B_OK);
41228cac36cSMatthew Wilber 	CPPUNIT_ASSERT(!msg.IsEmpty());
41328cac36cSMatthew Wilber 
41428cac36cSMatthew Wilber 	// B_TRANSLATOR_EXT_HEADER_ONLY
41528cac36cSMatthew Wilber 	NextSubTest();
41628cac36cSMatthew Wilber 	bool bheaderonly = true;
41728cac36cSMatthew Wilber 	CPPUNIT_ASSERT(
41828cac36cSMatthew Wilber 		msg.FindBool(B_TRANSLATOR_EXT_HEADER_ONLY, &bheaderonly) == B_OK);
41928cac36cSMatthew Wilber 	CPPUNIT_ASSERT(bheaderonly == false);
42028cac36cSMatthew Wilber 
42128cac36cSMatthew Wilber 	// B_TRANSLATOR_EXT_DATA_ONLY
42228cac36cSMatthew Wilber 	NextSubTest();
42328cac36cSMatthew Wilber 	bool bdataonly = true;
42428cac36cSMatthew Wilber 	CPPUNIT_ASSERT(
42528cac36cSMatthew Wilber 		msg.FindBool(B_TRANSLATOR_EXT_DATA_ONLY, &bdataonly) == B_OK);
42628cac36cSMatthew Wilber 	CPPUNIT_ASSERT(bdataonly == false);
427fd6e40cdSMatthew Wilber }
428fd6e40cdSMatthew Wilber 
429b0ec1a79SMatthew Wilber #if !TEST_R5
430b0ec1a79SMatthew Wilber 
431f4949fcaSMatthew Wilber // The input formats that this translator is supposed to support
432f4949fcaSMatthew Wilber translation_format gSTXTInputFormats[] = {
433f4949fcaSMatthew Wilber 	{
434f4949fcaSMatthew Wilber 		B_TRANSLATOR_TEXT,
435f4949fcaSMatthew Wilber 		B_TRANSLATOR_TEXT,
436f4949fcaSMatthew Wilber 		0.4f, // quality
437f4949fcaSMatthew Wilber 		0.6f, // capability
438f4949fcaSMatthew Wilber 		"text/plain",
439f4949fcaSMatthew Wilber 		"Plain text file"
440f4949fcaSMatthew Wilber 	},
441f4949fcaSMatthew Wilber 	{
442f4949fcaSMatthew Wilber 		B_STYLED_TEXT_FORMAT,
443f4949fcaSMatthew Wilber 		B_TRANSLATOR_TEXT,
444f4949fcaSMatthew Wilber 		0.5f,
445f4949fcaSMatthew Wilber 		0.5f,
446f4949fcaSMatthew Wilber 		"text/x-vnd.Be-stxt",
447f4949fcaSMatthew Wilber 		"Be styled text file"
448f4949fcaSMatthew Wilber 	}
449f4949fcaSMatthew Wilber };
450f4949fcaSMatthew Wilber 
451f4949fcaSMatthew Wilber // The output formats that this translator is supposed to support
452f4949fcaSMatthew Wilber translation_format gSTXTOutputFormats[] = {
453f4949fcaSMatthew Wilber 	{
454f4949fcaSMatthew Wilber 		B_TRANSLATOR_TEXT,
455f4949fcaSMatthew Wilber 		B_TRANSLATOR_TEXT,
456f4949fcaSMatthew Wilber 		0.4f, // quality
457f4949fcaSMatthew Wilber 		0.6f, // capability
458f4949fcaSMatthew Wilber 		"text/plain",
459f4949fcaSMatthew Wilber 		"Plain text file"
460f4949fcaSMatthew Wilber 	},
461f4949fcaSMatthew Wilber 	{
462f4949fcaSMatthew Wilber 		B_STYLED_TEXT_FORMAT,
463f4949fcaSMatthew Wilber 		B_TRANSLATOR_TEXT,
464f4949fcaSMatthew Wilber 		0.5f,
465f4949fcaSMatthew Wilber 		0.5f,
466f4949fcaSMatthew Wilber 		"text/x-vnd.Be-stxt",
467f4949fcaSMatthew Wilber 		"Be styled text file"
468f4949fcaSMatthew Wilber 	}
469f4949fcaSMatthew Wilber };
470f4949fcaSMatthew Wilber 
471b0ec1a79SMatthew Wilber void
LoadAddOnTest()472a2c12890SMatthew Wilber STXTTranslatorTest::LoadAddOnTest()
473b0ec1a79SMatthew Wilber {
474f4949fcaSMatthew Wilber 	TranslatorLoadAddOnTest("/boot/home/config/add-ons/Translators/STXTTranslator",
475f4949fcaSMatthew Wilber 		this,
476f4949fcaSMatthew Wilber 		gSTXTInputFormats, sizeof(gSTXTInputFormats) / sizeof(translation_format),
47739bd4409SMatthew Wilber 		gSTXTOutputFormats, sizeof(gSTXTOutputFormats) / sizeof(translation_format),
478dfb8c585SMatthew Wilber 		B_TRANSLATION_MAKE_VERSION(1,0,0));
479b0ec1a79SMatthew Wilber }
480a2c12890SMatthew Wilber 
481a2c12890SMatthew Wilber #endif // #if !TEST_R5
482