xref: /haiku/src/tests/kits/translation/TranslatorTest.cpp (revision 4c8e85b316c35a9161f5a1c50ad70bc91c83a76f)
1 /*****************************************************************************/
2 // Haiku Translation Kit Test
3 // Author: Michael Wilber
4 // Version:
5 //
6 // This is the Test application for BTranslator
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 Haiku 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 "TranslatorTest.h"
34 #include <TranslatorRoster.h>
35 #include <Application.h>
36 #include <OS.h>
37 #include <stdio.h>
38 
39 /* cppunit framework */
40 #include <cppunit/Test.h>
41 #include <cppunit/TestCaller.h>
42 #include <cppunit/TestSuite.h>
43 
44 // BTranslator derived class to test with
45 class BTranslatorTester : public BTranslator {
46 public:
47 	BTranslatorTester();
48 
49 	virtual const char *TranslatorName() const { return "NoName"; };
50 	virtual const char *TranslatorInfo() const { return "NoInfo"; };
51 	virtual int32 TranslatorVersion() const { return 100; };
52 
53 	virtual const translation_format *InputFormats(int32 *out_count) const;
54 	virtual const translation_format *OutputFormats(int32 *out_count) const;
55 
56 	virtual status_t Identify(BPositionIO *inSource,
57 		const translation_format *inFormat, BMessage *ioExtension,
58 		translator_info *outInfo, uint32 outType) { return B_ERROR; };
59 
60 	virtual status_t Translate(BPositionIO *inSource,
61 		const translator_info *inInfo, BMessage *ioExtension,
62 		uint32 outType, BPositionIO *outDestination) { return B_ERROR; };
63 };
64 
65 BTranslatorTester::BTranslatorTester()
66 	: BTranslator()
67 {
68 }
69 
70 const translation_format *
71 BTranslatorTester::InputFormats(int32 *out_count) const
72 {
73 	if (out_count) *out_count = 0;
74 	return NULL;
75 }
76 
77 const translation_format *
78 BTranslatorTester::OutputFormats(int32 *out_count) const
79 {
80 	if (out_count) *out_count = 0;
81 	return NULL;
82 }
83 
84 /**
85  * Default constructor - no work
86  */
87 TranslatorTest::TranslatorTest(std::string name)
88 	: BTestCase(name)
89 {
90 }
91 
92 /**
93  * Default destructor - no work
94  */
95 TranslatorTest::~TranslatorTest()
96 {
97 }
98 
99 CppUnit::Test *
100 TranslatorTest::Suite()
101 {
102 	/* create our suite */
103 	CppUnit::TestSuite *suite = new CppUnit::TestSuite("Translator");
104 
105 	/* add suckers */
106 	suite->addTest(new CppUnit::TestCaller<TranslatorTest>("TranslatorTest::AcquireRelease Test", &TranslatorTest::AcquireReleaseTest));
107 	suite->addTest(new CppUnit::TestCaller<TranslatorTest>("TranslatorTest::MakeConfigurationView Test", &TranslatorTest::MakeConfigurationViewTest));
108 	suite->addTest(new CppUnit::TestCaller<TranslatorTest>("TranslatorTest::GetConfigurationMessage Test", &TranslatorTest::GetConfigurationMessageTest));
109 
110 	return suite;
111 }
112 
113 void
114 TranslatorTest::AcquireReleaseTest()
115 {
116 	// Create new BTranslator
117 	NextSubTest();
118 	BTranslatorTester *ptt = new BTranslatorTester();
119 	CPPUNIT_ASSERT(ptt);
120 	BTranslator *ptranslator = static_cast<BTranslator *>(ptt);
121 	CPPUNIT_ASSERT(ptranslator);
122 	CPPUNIT_ASSERT(ptranslator->ReferenceCount() == 1);
123 
124 	// Do some Acquire()ing
125 	NextSubTest();
126 	BTranslator *pcomptran;
127 	pcomptran = ptranslator->Acquire();
128 	CPPUNIT_ASSERT(pcomptran == ptranslator);
129 	CPPUNIT_ASSERT(ptranslator->ReferenceCount() == 2);
130 
131 	// Release()
132 	NextSubTest();
133 	CPPUNIT_ASSERT(ptranslator->Release() == ptranslator);
134 	CPPUNIT_ASSERT(ptranslator->ReferenceCount() == 1);
135 
136 	// Destroy
137 	NextSubTest();
138 	CPPUNIT_ASSERT(ptranslator->Release() == NULL);
139 	ptranslator = NULL;
140 	ptt = NULL;
141 	pcomptran = NULL;
142 }
143 
144 void
145 TranslatorTest::MakeConfigurationViewTest()
146 {
147 	// Create new BTranslator
148 	NextSubTest();
149 	BApplication app(
150 		"application/x-vnd.OpenBeOS-translationkit_translatortest");
151 	BTranslatorTester *ptt = new BTranslatorTester();
152 	CPPUNIT_ASSERT(ptt);
153 	BTranslator *ptranslator = static_cast<BTranslator *>(ptt);
154 	CPPUNIT_ASSERT(ptranslator);
155 	CPPUNIT_ASSERT(ptranslator->ReferenceCount() == 1);
156 
157 	// Try GetConfigurationMessage
158 	NextSubTest();
159 	BMessage bmsg;
160 	BView *pview = NULL;
161 	BRect rect;
162 	CPPUNIT_ASSERT(ptranslator->MakeConfigurationView(NULL,
163 		NULL, NULL) == B_ERROR);
164 	CPPUNIT_ASSERT(ptranslator->MakeConfigurationView(&bmsg,
165 		&pview, &rect) == B_ERROR);
166 	CPPUNIT_ASSERT(bmsg.IsEmpty() == true);
167 
168 	// Destroy
169 	NextSubTest();
170 	CPPUNIT_ASSERT(ptranslator->Release() == NULL);
171 	ptranslator = NULL;
172 	ptt = NULL;
173 }
174 
175 void
176 TranslatorTest::GetConfigurationMessageTest()
177 {
178 	// Create new BTranslator
179 	NextSubTest();
180 	BTranslatorTester *ptt = new BTranslatorTester();
181 	CPPUNIT_ASSERT(ptt);
182 	BTranslator *ptranslator = static_cast<BTranslator *>(ptt);
183 	CPPUNIT_ASSERT(ptranslator);
184 	CPPUNIT_ASSERT(ptranslator->ReferenceCount() == 1);
185 
186 	// Try GetConfigurationMessage
187 	NextSubTest();
188 	BMessage bmsg;
189 	CPPUNIT_ASSERT(ptranslator->GetConfigurationMessage(NULL) == B_ERROR);
190 	CPPUNIT_ASSERT(ptranslator->GetConfigurationMessage(&bmsg) == B_ERROR);
191 	CPPUNIT_ASSERT(bmsg.IsEmpty() == true);
192 
193 	// Destroy
194 	NextSubTest();
195 	CPPUNIT_ASSERT(ptranslator->Release() == NULL);
196 	ptranslator = NULL;
197 	ptt = NULL;
198 }
199 
200