PageRenderTime 105ms CodeModel.GetById 20ms app.highlight 69ms RepoModel.GetById 11ms app.codeStats 0ms

/indra/newview/tests/lltranslate_test.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 345 lines | 276 code | 44 blank | 25 comment | 2 complexity | 3214a257ccf87069c29f09330287401e MD5 | raw file
  1/** 
  2 * @file lltranslate_test.cpp
  3 *
  4 * $LicenseInfo:firstyear=2011&license=viewerlgpl$
  5 * Second Life Viewer Source Code
  6 * Copyright (C) 2011, Linden Research, Inc.
  7 * 
  8 * This library is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU Lesser General Public
 10 * License as published by the Free Software Foundation;
 11 * version 2.1 of the License only.
 12 * 
 13 * This library is distributed in the hope that it will be useful,
 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 16 * Lesser General Public License for more details.
 17 * 
 18 * You should have received a copy of the GNU Lesser General Public
 19 * License along with this library; if not, write to the Free Software
 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 21 * 
 22 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 23 * $/LicenseInfo$
 24 */
 25
 26#include "linden_common.h"
 27
 28#include "../test/lltut.h"
 29#include "../lltranslate.h"
 30#include "../llversioninfo.h"
 31#include "../llviewercontrol.h"
 32
 33#include "llbufferstream.h"
 34#include "lltrans.h"
 35#include "llui.h"
 36
 37static const std::string GOOGLE_VALID_RESPONSE1 =
 38"{\
 39 \"data\": {\
 40  \"translations\": [\
 41   {\
 42    \"translatedText\": \"??????\",\
 43    \"detectedSourceLanguage\": \"es\"\
 44   }\
 45  ]\
 46 }\
 47}";
 48
 49static const std::string GOOGLE_VALID_RESPONSE2 =
 50"{\
 51 \"data\": {\
 52  \"translations\": [\
 53   {\
 54    \"translatedText\": \"??????\"\
 55   }\
 56  ]\
 57 }\
 58}\
 59";
 60
 61static const std::string GOOGLE_VALID_RESPONSE3 =
 62"{\
 63 \"error\": {\
 64  \"errors\": [\
 65   {\
 66    \"domain\": \"global\",\
 67    \"reason\": \"invalid\",\
 68    \"message\": \"Invalid Value\"\
 69   }\
 70  ],\
 71  \"code\": 400,\
 72  \"message\": \"Invalid Value\"\
 73 }\
 74}";
 75
 76static const std::string BING_VALID_RESPONSE1 =
 77"<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">??????</string>";
 78
 79static const std::string BING_VALID_RESPONSE2 =
 80"<html><body><h1>Argument Exception</h1><p>Method: Translate()</p><p>Parameter: </p>\
 81<p>Message: 'from' must be a valid language</p><code></code>\
 82<p>message id=3743.V2_Rest.Translate.58E8454F</p></body></html>";
 83
 84static const std::string BING_VALID_RESPONSE3 =
 85"<html><body><h1>Argument Exception</h1><p>Method: Translate()</p>\
 86<p>Parameter: appId</p><p>Message: Invalid appId&#xD;\nParameter name: appId</p>\
 87<code></code><p>message id=3737.V2_Rest.Translate.56016759</p></body></html>";
 88
 89namespace tut
 90{
 91	class translate_test
 92	{
 93	protected:
 94		void test_translation(
 95			LLTranslationAPIHandler& handler,
 96			int status, const std::string& resp,
 97			const std::string& exp_trans, const std::string& exp_lang, const std::string& exp_err)
 98		{
 99			std::string translation, detected_lang, err_msg;
100			bool rc = handler.parseResponse(status, resp, translation, detected_lang, err_msg);
101			ensure_equals("rc", rc, (status == 200));
102			ensure_equals("err_msg", err_msg, exp_err);
103			ensure_equals("translation", translation, exp_trans);
104			ensure_equals("detected_lang", detected_lang,  exp_lang);
105		}
106
107		LLGoogleTranslationHandler mGoogle;
108		LLBingTranslationHandler mBing;
109	};
110
111	typedef test_group<translate_test> translate_test_group_t;
112	typedef translate_test_group_t::object translate_test_object_t;
113	tut::translate_test_group_t tut_translate("LLTranslate");
114
115	template<> template<>
116	void translate_test_object_t::test<1>()
117	{
118		test_translation(mGoogle, 200, GOOGLE_VALID_RESPONSE1, "??????", "es", "");
119	}
120
121	template<> template<>
122	void translate_test_object_t::test<2>()
123	{
124		test_translation(mGoogle, 200, GOOGLE_VALID_RESPONSE2, "??????", "", "");
125	}
126
127	template<> template<>
128	void translate_test_object_t::test<3>()
129	{
130		test_translation(mGoogle, 400, GOOGLE_VALID_RESPONSE3, "", "", "Invalid Value");
131	}
132
133	template<> template<>
134	void translate_test_object_t::test<4>()
135	{
136		test_translation(mGoogle, 400,
137			"",
138			"", "", "* Line 1, Column 1\n  Syntax error: value, object or array expected.\n");
139	}
140
141	template<> template<>
142	void translate_test_object_t::test<5>()
143	{
144		test_translation(mGoogle, 400,
145			"[]",
146			"", "", "");
147	}
148
149	template<> template<>
150	void translate_test_object_t::test<6>()
151	{
152		test_translation(mGoogle, 400,
153			"{\"oops\": \"invalid\"}",
154			"", "", "");
155	}
156
157	template<> template<>
158	void translate_test_object_t::test<7>()
159	{
160		test_translation(mGoogle, 400,
161			"{\"data\": {}}",
162			"", "", "");
163	}
164
165	template<> template<>
166	void translate_test_object_t::test<8>()
167	{
168		test_translation(mGoogle, 400,
169				"{\"data\": { \"translations\": [ {} ] }}",
170			"", "", "");
171	}
172
173	template<> template<>
174	void translate_test_object_t::test<9>()
175	{
176		test_translation(mGoogle, 400,
177				"{\"data\": { \"translations\": [ { \"translatedTextZZZ\": \"??????\", \"detectedSourceLanguageZZZ\": \"es\" } ] }}",
178			"", "", "");
179	}
180
181	template<> template<>
182	void translate_test_object_t::test<10>()
183	{
184		test_translation(mBing, 200, BING_VALID_RESPONSE1, "??????", "", "");
185	}
186
187	template<> template<>
188	void translate_test_object_t::test<11>()
189	{
190		test_translation(mBing, 400, BING_VALID_RESPONSE2, "", "", "'from' must be a valid language");
191	}
192
193	template<> template<>
194	void translate_test_object_t::test<12>()
195	{
196		test_translation(mBing, 400, BING_VALID_RESPONSE3, "", "", "Invalid appId\nParameter name: appId");
197	}
198
199	template<> template<>
200	void translate_test_object_t::test<13>()
201	{
202		test_translation(mBing, 200,
203			"??????</string>",
204			"??????", "", "");
205	}
206
207	template<> template<>
208	void translate_test_object_t::test<14>()
209	{
210		test_translation(mBing, 200,
211			"<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">??????",
212			"??????", "", "");
213	}
214
215	template<> template<>
216	void translate_test_object_t::test<15>()
217	{
218		test_translation(mBing, 200,
219			"??????",
220			"??????", "", "");
221	}
222
223	template<> template<>
224	void translate_test_object_t::test<16>()
225	{
226		test_translation(mBing, 400,
227			"Message: some error</p>",
228			"", "", "some error");
229	}
230
231	template<> template<>
232	void translate_test_object_t::test<17>()
233	{
234		test_translation(mBing, 400,
235			"Message: some error",
236			"", "", "some error");
237	}
238
239	template<> template<>
240	void translate_test_object_t::test<18>()
241	{
242		test_translation(mBing, 400,
243			"some error</p>",
244			"", "", "some error");
245	}
246
247	template<> template<>
248	void translate_test_object_t::test<19>()
249	{
250		test_translation(mBing, 400,
251			"some error",
252			"", "", "some error");
253	}
254
255	template<> template<>
256	void translate_test_object_t::test<20>()
257	{
258		std::string url;
259		mBing.getTranslateURL(url, "en", "es", "hi");
260		ensure_equals("bing URL", url,
261			"http://api.microsofttranslator.com/v2/Http.svc/Translate?appId=dummy&text=hi&to=es&from=en");
262	}
263
264	template<> template<>
265	void translate_test_object_t::test<21>()
266	{
267		std::string url;
268		mBing.getTranslateURL(url, "", "es", "hi");
269		ensure_equals("bing URL", url,
270			"http://api.microsofttranslator.com/v2/Http.svc/Translate?appId=dummy&text=hi&to=es");
271	}
272
273	template<> template<>
274	void translate_test_object_t::test<22>()
275	{
276		std::string url;
277		mGoogle.getTranslateURL(url, "en", "es", "hi");
278		ensure_equals("google URL", url,
279			"https://www.googleapis.com/language/translate/v2?key=dummy&q=hi&target=es&source=en");
280	}
281
282	template<> template<>
283	void translate_test_object_t::test<23>()
284	{
285		std::string url;
286		mGoogle.getTranslateURL(url, "", "es", "hi");
287		ensure_equals("google URL", url,
288			"https://www.googleapis.com/language/translate/v2?key=dummy&q=hi&target=es");
289	}
290}
291
292//== Misc stubs ===============================================================
293LLControlGroup gSavedSettings("test");
294
295std::string LLUI::getLanguage() { return "en"; }
296std::string LLTrans::getString(const std::string &xml_desc, const LLStringUtil::format_map_t& args) { return "dummy"; }
297
298LLControlGroup::LLControlGroup(const std::string& name) : LLInstanceTracker<LLControlGroup, std::string>(name) {}
299std::string LLControlGroup::getString(const std::string& name) { return "dummy"; }
300LLControlGroup::~LLControlGroup() {}
301
302namespace boost {
303	void intrusive_ptr_add_ref(LLCurl::Responder*) {}
304	void intrusive_ptr_release(LLCurl::Responder*) {}
305}
306
307LLCurl::Responder::Responder() {}
308void LLCurl::Responder::completedHeader(U32, std::string const&, LLSD const&) {}
309void LLCurl::Responder::completedRaw(U32, const std::string&, const LLChannelDescriptors&, const LLIOPipe::buffer_ptr_t& buffer) {}
310void LLCurl::Responder::completed(U32, std::string const&, LLSD const&) {}
311void LLCurl::Responder::error(U32, std::string const&) {}
312void LLCurl::Responder::errorWithContent(U32, std::string const&, LLSD const&) {}
313void LLCurl::Responder::result(LLSD const&) {}
314LLCurl::Responder::~Responder() {}
315
316void LLHTTPClient::get(const std::string&, const LLSD&, ResponderPtr, const LLSD&, const F32) {}
317void LLHTTPClient::get(const std::string&, boost::intrusive_ptr<LLCurl::Responder>, const LLSD&, const F32) {}
318
319LLBufferStream::LLBufferStream(const LLChannelDescriptors& channels, LLBufferArray* buffer)
320:	std::iostream(&mStreamBuf), mStreamBuf(channels, buffer) {}
321LLBufferStream::~LLBufferStream() {}
322
323LLBufferStreamBuf::LLBufferStreamBuf(const LLChannelDescriptors&, LLBufferArray*) {}
324#if( LL_WINDOWS || __GNUC__ > 2)
325LLBufferStreamBuf::pos_type LLBufferStreamBuf::seekoff(
326	off_type off,
327	std::ios::seekdir way,
328	std::ios::openmode which)
329#else
330streampos LLBufferStreamBuf::seekoff(
331	streamoff off,
332	std::ios::seekdir way,
333	std::ios::openmode which)
334#endif
335{ return 0; }
336int LLBufferStreamBuf::sync() {return 0;}
337int LLBufferStreamBuf::underflow() {return 0;}
338int LLBufferStreamBuf::overflow(int) {return 0;}
339LLBufferStreamBuf::~LLBufferStreamBuf() {}
340
341S32 LLVersionInfo::getBuild() { return 0; }
342const std::string& LLVersionInfo::getChannel() {static std::string dummy; return dummy;}
343S32 LLVersionInfo::getMajor() { return 0; }
344S32 LLVersionInfo::getMinor() { return 0; }
345S32 LLVersionInfo::getPatch() { return 0; }