xref: /haiku/src/tests/add-ons/translators/stxttranslator/STXTTranslatorTest.cpp (revision e5430a086c769ea76c3944046b1f07cf049c1ae0)
1 // STXTTranslatorTest.cpp
2 #include "STXTTranslatorTest.h"
3 #include <cppunit/Test.h>
4 #include <cppunit/TestCaller.h>
5 #include <cppunit/TestSuite.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <unistd.h>
9 #include <image.h>
10 #include <Translator.h>
11 #include <TranslatorFormats.h>
12 #include <TranslatorRoster.h>
13 #include <Message.h>
14 #include <View.h>
15 #include <Rect.h>
16 #include <File.h>
17 #include <DataIO.h>
18 #include <Errors.h>
19 #include <OS.h>
20 #include "TranslatorTestAddOn.h"
21 
22 // Suite
23 CppUnit::Test *
24 STXTTranslatorTest::Suite()
25 {
26 	CppUnit::TestSuite *suite = new CppUnit::TestSuite();
27 	typedef CppUnit::TestCaller<STXTTranslatorTest> TC;
28 
29 	suite->addTest(
30 		new TC("STXTTranslator IdentifyTest",
31 			&STXTTranslatorTest::IdentifyTest));
32 
33 	suite->addTest(
34 		new TC("STXTTranslator TranslateTest",
35 			&STXTTranslatorTest::TranslateTest));
36 
37 	suite->addTest(
38 		new TC("STXTTranslator ConfigMessageTest",
39 			&STXTTranslatorTest::ConfigMessageTest));
40 
41 #if !TEST_R5
42 	suite->addTest(
43 		new TC("STXTTranslator LoadAddOnTest",
44 			&STXTTranslatorTest::LoadAddOnTest));
45 #endif
46 
47 	return suite;
48 }
49 
50 // setUp
51 void
52 STXTTranslatorTest::setUp()
53 {
54 	BTestCase::setUp();
55 }
56 
57 // tearDown
58 void
59 STXTTranslatorTest::tearDown()
60 {
61 	BTestCase::tearDown();
62 }
63 
64 void
65 CheckStyled(translator_info *pti)
66 {
67 	CheckTranslatorInfo(pti, B_STYLED_TEXT_FORMAT, B_TRANSLATOR_TEXT,
68 		0.5f, 0.5f, "Be styled text file", "text/x-vnd.Be-stxt");
69 }
70 
71 void
72 CheckPlain(translator_info *pti)
73 {
74 	CheckTranslatorInfo(pti, B_TRANSLATOR_TEXT, B_TRANSLATOR_TEXT,
75 		0.4f, 0.6f, "Plain text file", "text/plain");
76 }
77 
78 void
79 IdentifyTests(STXTTranslatorTest *ptest, BTranslatorRoster *proster,
80 	const char **paths, int32 len, bool bplain)
81 {
82 	translator_info ti;
83 	printf(" [%d] ", (int) bplain);
84 
85 	for (int32 i = 0; i < len; i++) {
86 		ptest->NextSubTest();
87 		BFile file;
88 		printf(" [%s] ", paths[i]);
89 		CPPUNIT_ASSERT(file.SetTo(paths[i], B_READ_ONLY) == B_OK);
90 
91 		// Identify (output: B_TRANSLATOR_ANY_TYPE)
92 		ptest->NextSubTest();
93 		memset(&ti, 0, sizeof(translator_info));
94 		CPPUNIT_ASSERT(proster->Identify(&file, NULL, &ti) == B_OK);
95 		if (bplain)
96 			CheckPlain(&ti);
97 		else
98 			CheckStyled(&ti);
99 
100 		// Identify (output: B_TRANSLATOR_TEXT)
101 		ptest->NextSubTest();
102 		memset(&ti, 0, sizeof(translator_info));
103 		CPPUNIT_ASSERT(proster->Identify(&file, NULL, &ti, 0, NULL,
104 			B_TRANSLATOR_TEXT) == B_OK);
105 		if (bplain)
106 			CheckPlain(&ti);
107 		else
108 			CheckStyled(&ti);
109 
110 		// Identify (output: B_STYLED_TEXT_FORMAT)
111 		ptest->NextSubTest();
112 		memset(&ti, 0, sizeof(translator_info));
113 		CPPUNIT_ASSERT(proster->Identify(&file, NULL, &ti, 0, NULL,
114 			B_STYLED_TEXT_FORMAT) == B_OK);
115 		if (bplain)
116 			CheckPlain(&ti);
117 		else
118 			CheckStyled(&ti);
119 	}
120 }
121 
122 void
123 STXTTranslatorTest::IdentifyTest()
124 {
125 	// Init
126 	NextSubTest();
127 	status_t result = B_ERROR;
128 	BTranslatorRoster *proster = new BTranslatorRoster();
129 	CPPUNIT_ASSERT(proster);
130 	CPPUNIT_ASSERT(proster->AddTranslators(
131 		"/boot/home/config/add-ons/Translators/STXTTranslator") == B_OK);
132 	BFile wronginput("../src/tests/kits/translation/data/images/image.jpg",
133 		B_READ_ONLY);
134 	CPPUNIT_ASSERT(wronginput.InitCheck() == B_OK);
135 
136 	// Identify (bad input, output types)
137 	NextSubTest();
138 	translator_info ti;
139 	memset(&ti, 0, sizeof(translator_info));
140 	result = proster->Identify(&wronginput, NULL, &ti, 0,
141 		NULL, B_TRANSLATOR_BITMAP);
142 	CPPUNIT_ASSERT(result == B_NO_TRANSLATOR);
143 	CPPUNIT_ASSERT(ti.type == 0 && ti.translator == 0);
144 
145 	// Identify (wrong type of input data)
146 	NextSubTest();
147 	memset(&ti, 0, sizeof(translator_info));
148 	result = proster->Identify(&wronginput, NULL, &ti);
149 	CPPUNIT_ASSERT(result == B_NO_TRANSLATOR);
150 	CPPUNIT_ASSERT(ti.type == 0 && ti.translator == 0);
151 
152 	// Identify (wrong magic)
153 	NextSubTest();
154 	memset(&ti, 0, sizeof(translator_info));
155 	BFile wrongmagic("../src/tests/kits/translation/data/text/wrong_magic.stxt",
156 		B_READ_ONLY);
157 	CPPUNIT_ASSERT(wrongmagic.InitCheck() == B_OK);
158 	result = proster->Identify(&wrongmagic, NULL, &ti);
159 	CPPUNIT_ASSERT(result == B_NO_TRANSLATOR);
160 	CPPUNIT_ASSERT(ti.type == 0 && ti.translator == 0);
161 
162 	// Identify (wrong version)
163 	NextSubTest();
164 	memset(&ti, 0, sizeof(translator_info));
165 	BFile wrongversion("../src/tests/kits/translation/data/text/wrong_version.stxt",
166 		B_READ_ONLY);
167 	CPPUNIT_ASSERT(wrongversion.InitCheck() == B_OK);
168 	result = proster->Identify(&wrongversion, NULL, &ti);
169 	CPPUNIT_ASSERT(result == B_NO_TRANSLATOR);
170 	CPPUNIT_ASSERT(ti.type == 0 && ti.translator == 0);
171 
172 	const char *aPlainFiles[] = {
173 		"../src/tests/kits/translation/data/text/ascii.txt",
174 		"../src/tests/kits/translation/data/text/japanese.txt",
175 		"../src/tests/kits/translation/data/text/multi_byte.txt",
176 		"../src/tests/kits/translation/data/text/one_length.txt",
177 		"../src/tests/kits/translation/data/text/sentence.txt",
178 		"../src/tests/kits/translation/data/text/symbols.txt",
179 		"../src/tests/kits/translation/data/text/zero_length.txt"
180 	};
181 	const char *aStyledFiles[] = {
182 		"../src/tests/kits/translation/data/text/ascii.stxt",
183 		"../src/tests/kits/translation/data/text/japanese.stxt",
184 		"../src/tests/kits/translation/data/text/multi_byte.stxt",
185 		"../src/tests/kits/translation/data/text/one_length.stxt",
186 		"../src/tests/kits/translation/data/text/sentence.stxt",
187 		"../src/tests/kits/translation/data/text/symbols.stxt",
188 		"../src/tests/kits/translation/data/text/zero_length.stxt",
189 	};
190 	IdentifyTests(this, proster, aPlainFiles,
191 		sizeof(aPlainFiles) / sizeof(const char *), true);
192 	IdentifyTests(this, proster, aStyledFiles,
193 		sizeof(aStyledFiles) / sizeof(const char *), false);
194 }
195 
196 void
197 TranslateTests(STXTTranslatorTest *ptest, BTranslatorRoster *proster,
198 	const char **paths, int32 len, bool bplain)
199 {
200 	int32 nlongest = 0, ncurrent = 0;
201 	// find the length of the longest string
202 	for (int32 i = 0; i < len; i++) {
203 		ncurrent = strlen(paths[i]);
204 		if (ncurrent > nlongest)
205 			nlongest = ncurrent;
206 	}
207 
208 	char *styled_path = NULL, *plain_path = NULL;
209 	styled_path = new char[nlongest + 6];
210 	plain_path = new char[nlongest + 6];
211 
212 	// Perform translations on every file in the array
213 	for (int32 i = 0; i < len; i++) {
214 		strcpy(styled_path, paths[i]);
215 		strcat(styled_path, ".stxt");
216 		strcpy(plain_path, paths[i]);
217 		strcat(plain_path, ".txt");
218 
219 		// Setup input files
220 		ptest->NextSubTest();
221 		BFile styled_file, plain_file, *pinput_file;
222 		CPPUNIT_ASSERT(styled_file.SetTo(styled_path, B_READ_ONLY) == B_OK);
223 		CPPUNIT_ASSERT(plain_file.SetTo(plain_path, B_READ_ONLY) == B_OK);
224 		if (bplain) {
225 			printf(" [%s] ", plain_path);
226 			pinput_file = &plain_file;
227 		} else {
228 			printf(" [%s] ", styled_path);
229 			pinput_file = &styled_file;
230 		}
231 
232 		BMallocIO mallio, dmallio;
233 
234 		// Convert to B_TRANSLATOR_ANY_TYPE (should be B_TRANSLATOR_TEXT)
235 		ptest->NextSubTest();
236 		CPPUNIT_ASSERT(mallio.Seek(0, SEEK_SET) == 0);
237 		CPPUNIT_ASSERT(mallio.SetSize(0) == B_OK);
238 		CPPUNIT_ASSERT(proster->Translate(pinput_file, NULL, NULL, &mallio,
239 			B_TRANSLATOR_ANY_TYPE) == B_OK);
240 		CPPUNIT_ASSERT(CompareStreams(mallio, plain_file) == true);
241 
242 		// Convert to B_TRANSLATOR_TEXT
243 		ptest->NextSubTest();
244 		CPPUNIT_ASSERT(mallio.Seek(0, SEEK_SET) == 0);
245 		CPPUNIT_ASSERT(mallio.SetSize(0) == B_OK);
246 		CPPUNIT_ASSERT(proster->Translate(pinput_file, NULL, NULL, &mallio,
247 			B_TRANSLATOR_TEXT) == B_OK);
248 		CPPUNIT_ASSERT(CompareStreams(mallio, plain_file) == true);
249 
250 		// Convert plain mallio to B_TRANSLATOR_TEXT dmallio
251 		ptest->NextSubTest();
252 		CPPUNIT_ASSERT(dmallio.Seek(0, SEEK_SET) == 0);
253 		CPPUNIT_ASSERT(dmallio.SetSize(0) == B_OK);
254 		CPPUNIT_ASSERT(proster->Translate(&mallio, NULL, NULL, &dmallio,
255 			B_TRANSLATOR_TEXT) == B_OK);
256 		CPPUNIT_ASSERT(CompareStreams(dmallio, plain_file) == true);
257 
258 		// Convert to B_STYLED_TEXT_FORMAT
259 		ptest->NextSubTest();
260 		CPPUNIT_ASSERT(mallio.Seek(0, SEEK_SET) == 0);
261 		CPPUNIT_ASSERT(mallio.SetSize(0) == B_OK);
262 		CPPUNIT_ASSERT(proster->Translate(pinput_file, NULL, NULL, &mallio,
263 			B_STYLED_TEXT_FORMAT) == B_OK);
264 		CPPUNIT_ASSERT(CompareStreams(mallio, styled_file) == true);
265 
266 		// Convert styled mallio to B_TRANSLATOR_TEXT dmallio
267 		ptest->NextSubTest();
268 		CPPUNIT_ASSERT(dmallio.Seek(0, SEEK_SET) == 0);
269 		CPPUNIT_ASSERT(dmallio.SetSize(0) == B_OK);
270 		CPPUNIT_ASSERT(proster->Translate(&mallio, NULL, NULL, &dmallio,
271 			B_TRANSLATOR_TEXT) == B_OK);
272 		CPPUNIT_ASSERT(CompareStreams(dmallio, plain_file) == true);
273 
274 		// Convert styled mallio to B_STYLED_TEXT_FORMAT dmallio
275 		ptest->NextSubTest();
276 		CPPUNIT_ASSERT(dmallio.Seek(0, SEEK_SET) == 0);
277 		CPPUNIT_ASSERT(dmallio.SetSize(0) == B_OK);
278 		CPPUNIT_ASSERT(proster->Translate(&mallio, NULL, NULL, &dmallio,
279 			B_STYLED_TEXT_FORMAT) == B_OK);
280 		CPPUNIT_ASSERT(CompareStreams(dmallio, styled_file) == true);
281 	}
282 
283 	delete[] styled_path;
284 	styled_path = NULL;
285 	delete[] plain_path;
286 	plain_path = NULL;
287 }
288 
289 void
290 STXTTranslatorTest::TranslateTest()
291 {
292 	// Init
293 	NextSubTest();
294 	status_t result = B_ERROR;
295 	off_t filesize = -1;
296 	BTranslatorRoster *proster = new BTranslatorRoster();
297 	CPPUNIT_ASSERT(proster);
298 	CPPUNIT_ASSERT(proster->AddTranslators(
299 		"/boot/home/config/add-ons/Translators/STXTTranslator") == B_OK);
300 	BFile wronginput("../src/tests/kits/translation/data/images/image.jpg",
301 		B_READ_ONLY);
302 	CPPUNIT_ASSERT(wronginput.InitCheck() == B_OK);
303 	BFile output("/tmp/stxt_test.out", B_WRITE_ONLY |
304 		B_CREATE_FILE | B_ERASE_FILE);
305 	CPPUNIT_ASSERT(output.InitCheck() == B_OK);
306 
307 	// Translate (bad input, output types)
308 	NextSubTest();
309 	result = proster->Translate(&wronginput, NULL, NULL, &output,
310 		B_TRANSLATOR_BITMAP);
311 	CPPUNIT_ASSERT(result == B_NO_TRANSLATOR);
312 	CPPUNIT_ASSERT(output.GetSize(&filesize) == B_OK);
313 	CPPUNIT_ASSERT(filesize == 0);
314 
315 	// Translate (wrong type of input data)
316 	NextSubTest();
317 	result = proster->Translate(&wronginput, NULL, NULL, &output,
318 		B_TRANSLATOR_TEXT);
319 	CPPUNIT_ASSERT(result == B_NO_TRANSLATOR);
320 	CPPUNIT_ASSERT(output.GetSize(&filesize) == B_OK);
321 	CPPUNIT_ASSERT(filesize == 0);
322 
323 	// Translate (wrong type of input, B_TRANSLATOR_ANY_TYPE output)
324 	NextSubTest();
325 	result = proster->Translate(&wronginput, NULL, NULL, &output,
326 		B_TRANSLATOR_ANY_TYPE);
327 	CPPUNIT_ASSERT(result == B_NO_TRANSLATOR);
328 	CPPUNIT_ASSERT(output.GetSize(&filesize) == B_OK);
329 	CPPUNIT_ASSERT(filesize == 0);
330 
331 	// Identify (wrong magic)
332 	NextSubTest();
333 	BFile wrongmagic("../src/tests/kits/translation/data/text/wrong_magic.stxt",
334 		B_READ_ONLY);
335 	CPPUNIT_ASSERT(wrongmagic.InitCheck() == B_OK);
336 	result = proster->Translate(&wrongmagic, NULL, NULL, &output,
337 		B_TRANSLATOR_ANY_TYPE);
338 	CPPUNIT_ASSERT(result == B_NO_TRANSLATOR);
339 
340 	// Identify (wrong version)
341 	NextSubTest();
342 	BFile wrongversion("../src/tests/kits/translation/data/text/wrong_version.stxt",
343 		B_READ_ONLY);
344 	CPPUNIT_ASSERT(wrongversion.InitCheck() == B_OK);
345 	result = proster->Translate(&wrongversion, NULL, NULL, &output,
346 		B_TRANSLATOR_ANY_TYPE);
347 	CPPUNIT_ASSERT(result == B_NO_TRANSLATOR);
348 
349 	// Translate various data
350 	const char *aPlainTextFiles[] = {
351 		"../src/tests/kits/translation/data/text/ascii",
352 		"../src/tests/kits/translation/data/text/japanese",
353 		"../src/tests/kits/translation/data/text/multi_byte",
354 		"../src/tests/kits/translation/data/text/one_length",
355 		"../src/tests/kits/translation/data/text/symbols",
356 		"../src/tests/kits/translation/data/text/zero_length"
357 	};
358 	const char *aStyledTextFiles[] = {
359 		"../src/tests/kits/translation/data/text/ascii",
360 		"../src/tests/kits/translation/data/text/japanese",
361 		"../src/tests/kits/translation/data/text/multi_byte",
362 		"../src/tests/kits/translation/data/text/one_length",
363 		"../src/tests/kits/translation/data/text/sentence",
364 		"../src/tests/kits/translation/data/text/symbols",
365 		"../src/tests/kits/translation/data/text/zero_length"
366 	};
367 	TranslateTests(this, proster, aPlainTextFiles,
368 		sizeof(aPlainTextFiles) / sizeof(const char *), true);
369 	TranslateTests(this, proster, aStyledTextFiles,
370 		sizeof(aStyledTextFiles) / sizeof(const char *), false);
371 }
372 
373 // Make certain that the STXTTranslator does not
374 // provide a configuration message
375 void
376 STXTTranslatorTest::ConfigMessageTest()
377 {
378 	// Init
379 	NextSubTest();
380 	status_t result = B_ERROR;
381 	BTranslatorRoster *proster = new BTranslatorRoster();
382 	CPPUNIT_ASSERT(proster);
383 	CPPUNIT_ASSERT(proster->AddTranslators(
384 		"/boot/home/config/add-ons/Translators/STXTTranslator") == B_OK);
385 
386 	// GetAllTranslators
387 	NextSubTest();
388 	translator_id tid, *pids = NULL;
389 	int32 count = 0;
390 	CPPUNIT_ASSERT(proster->GetAllTranslators(&pids, &count) == B_OK);
391 	CPPUNIT_ASSERT(pids);
392 	CPPUNIT_ASSERT(count == 1);
393 	tid = pids[0];
394 	delete[] pids;
395 	pids = NULL;
396 
397 	// GetConfigurationMessage
398 	NextSubTest();
399 	BMessage msg;
400 	CPPUNIT_ASSERT(proster->GetConfigurationMessage(tid, &msg) == B_ERROR);
401 	CPPUNIT_ASSERT(msg.IsEmpty());
402 }
403 
404 #if !TEST_R5
405 
406 // The input formats that this translator is supposed to support
407 translation_format gSTXTInputFormats[] = {
408 	{
409 		B_TRANSLATOR_TEXT,
410 		B_TRANSLATOR_TEXT,
411 		0.4f, // quality
412 		0.6f, // capability
413 		"text/plain",
414 		"Plain text file"
415 	},
416 	{
417 		B_STYLED_TEXT_FORMAT,
418 		B_TRANSLATOR_TEXT,
419 		0.5f,
420 		0.5f,
421 		"text/x-vnd.Be-stxt",
422 		"Be styled text file"
423 	}
424 };
425 
426 // The output formats that this translator is supposed to support
427 translation_format gSTXTOutputFormats[] = {
428 	{
429 		B_TRANSLATOR_TEXT,
430 		B_TRANSLATOR_TEXT,
431 		0.4f, // quality
432 		0.6f, // capability
433 		"text/plain",
434 		"Plain text file"
435 	},
436 	{
437 		B_STYLED_TEXT_FORMAT,
438 		B_TRANSLATOR_TEXT,
439 		0.5f,
440 		0.5f,
441 		"text/x-vnd.Be-stxt",
442 		"Be styled text file"
443 	}
444 };
445 
446 void
447 STXTTranslatorTest::LoadAddOnTest()
448 {
449 	TranslatorLoadAddOnTest("/boot/home/config/add-ons/Translators/STXTTranslator",
450 		this,
451 		gSTXTInputFormats, sizeof(gSTXTInputFormats) / sizeof(translation_format),
452 		gSTXTOutputFormats, sizeof(gSTXTOutputFormats) / sizeof(translation_format));
453 }
454 
455 #endif // #if !TEST_R5
456