summaryrefslogtreecommitdiff
path: root/indra/newview/tests/lltranslate_test.cpp
blob: 5e73dbb981dab970d337ea8c46403218079217ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/** 
 * @file lltranslate_test.cpp
 *
 * $LicenseInfo:firstyear=2011&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2011, Linden Research, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#include "linden_common.h"

#include "../test/lltut.h"
#include "../lltranslate.h"
#include "../llversioninfo.h"
#include "../llviewercontrol.h"

#include "llbufferstream.h"
#include "lltrans.h"
#include "llui.h"

#include "../../llmessage/llhttpconstants.cpp"

static const std::string GOOGLE_VALID_RESPONSE1 =
"{\
 \"data\": {\
  \"translations\": [\
   {\
    \"translatedText\": \"привет\",\
    \"detectedSourceLanguage\": \"es\"\
   }\
  ]\
 }\
}";

static const std::string GOOGLE_VALID_RESPONSE2 =
"{\
 \"data\": {\
  \"translations\": [\
   {\
    \"translatedText\": \"привет\"\
   }\
  ]\
 }\
}\
";

static const std::string GOOGLE_VALID_RESPONSE3 =
"{\
 \"error\": {\
  \"errors\": [\
   {\
    \"domain\": \"global\",\
    \"reason\": \"invalid\",\
    \"message\": \"Invalid Value\"\
   }\
  ],\
  \"code\": 400,\
  \"message\": \"Invalid Value\"\
 }\
}";

static const std::string BING_VALID_RESPONSE1 =
"<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">Привет</string>";

static const std::string BING_VALID_RESPONSE2 =
"<html><body><h1>Argument Exception</h1><p>Method: Translate()</p><p>Parameter: </p>\
<p>Message: 'from' must be a valid language</p><code></code>\
<p>message id=3743.V2_Rest.Translate.58E8454F</p></body></html>";

static const std::string BING_VALID_RESPONSE3 =
"<html><body><h1>Argument Exception</h1><p>Method: Translate()</p>\
<p>Parameter: appId</p><p>Message: Invalid appId&#xD;\nParameter name: appId</p>\
<code></code><p>message id=3737.V2_Rest.Translate.56016759</p></body></html>";

namespace tut
{
	class translate_test
	{
	protected:
		void test_translation(
			LLTranslationAPIHandler& handler,
			int status, const std::string& resp,
			const std::string& exp_trans, const std::string& exp_lang, const std::string& exp_err)
		{
			std::string translation, detected_lang, err_msg;
			bool rc = handler.parseResponse(status, resp, translation, detected_lang, err_msg);
			ensure_equals("rc", rc, (status == 200));
			ensure_equals("err_msg", err_msg, exp_err);
			ensure_equals("translation", translation, exp_trans);
			ensure_equals("detected_lang", detected_lang,  exp_lang);
		}

		LLGoogleTranslationHandler mGoogle;
		LLBingTranslationHandler mBing;
	};

	typedef test_group<translate_test> translate_test_group_t;
	typedef translate_test_group_t::object translate_test_object_t;
	tut::translate_test_group_t tut_translate("LLTranslate");

	template<> template<>
	void translate_test_object_t::test<1>()
	{
		test_translation(mGoogle, 200, GOOGLE_VALID_RESPONSE1, "привет", "es", "");
	}

	template<> template<>
	void translate_test_object_t::test<2>()
	{
		test_translation(mGoogle, 200, GOOGLE_VALID_RESPONSE2, "привет", "", "");
	}

	template<> template<>
	void translate_test_object_t::test<3>()
	{
		test_translation(mGoogle, 400, GOOGLE_VALID_RESPONSE3, "", "", "Invalid Value");
	}

	template<> template<>
	void translate_test_object_t::test<4>()
	{
		test_translation(mGoogle, 400,
			"",
			"", "", "* Line 1, Column 1\n  Syntax error: value, object or array expected.\n");
	}

	template<> template<>
	void translate_test_object_t::test<5>()
	{
		test_translation(mGoogle, 400,
			"[]",
			"", "", "");
	}

	template<> template<>
	void translate_test_object_t::test<6>()
	{
		test_translation(mGoogle, 400,
			"{\"oops\": \"invalid\"}",
			"", "", "");
	}

	template<> template<>
	void translate_test_object_t::test<7>()
	{
		test_translation(mGoogle, 400,
			"{\"data\": {}}",
			"", "", "");
	}

	template<> template<>
	void translate_test_object_t::test<8>()
	{
		test_translation(mGoogle, 400,
				"{\"data\": { \"translations\": [ {} ] }}",
			"", "", "");
	}

	template<> template<>
	void translate_test_object_t::test<9>()
	{
		test_translation(mGoogle, 400,
				"{\"data\": { \"translations\": [ { \"translatedTextZZZ\": \"привет\", \"detectedSourceLanguageZZZ\": \"es\" } ] }}",
			"", "", "");
	}

	template<> template<>
	void translate_test_object_t::test<10>()
	{
		test_translation(mBing, 200, BING_VALID_RESPONSE1, "Привет", "", "");
	}

	template<> template<>
	void translate_test_object_t::test<11>()
	{
		test_translation(mBing, 400, BING_VALID_RESPONSE2, "", "", "'from' must be a valid language");
	}

	template<> template<>
	void translate_test_object_t::test<12>()
	{
		test_translation(mBing, 400, BING_VALID_RESPONSE3, "", "", "Invalid appId\nParameter name: appId");
	}

	template<> template<>
	void translate_test_object_t::test<13>()
	{
		test_translation(mBing, 200,
			"Привет</string>",
			"Привет", "", "");
	}

	template<> template<>
	void translate_test_object_t::test<14>()
	{
		test_translation(mBing, 200,
			"<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">Привет",
			"Привет", "", "");
	}

	template<> template<>
	void translate_test_object_t::test<15>()
	{
		test_translation(mBing, 200,
			"Привет",
			"Привет", "", "");
	}

	template<> template<>
	void translate_test_object_t::test<16>()
	{
		test_translation(mBing, 400,
			"Message: some error</p>",
			"", "", "some error");
	}

	template<> template<>
	void translate_test_object_t::test<17>()
	{
		test_translation(mBing, 400,
			"Message: some error",
			"", "", "some error");
	}

	template<> template<>
	void translate_test_object_t::test<18>()
	{
		test_translation(mBing, 400,
			"some error</p>",
			"", "", "some error");
	}

	template<> template<>
	void translate_test_object_t::test<19>()
	{
		test_translation(mBing, 400,
			"some error",
			"", "", "some error");
	}

	template<> template<>
	void translate_test_object_t::test<20>()
	{
		std::string url;
		mBing.getTranslateURL(url, "en", "es", "hi");
		ensure_equals("bing URL", url,
			"http://api.microsofttranslator.com/v2/Http.svc/Translate?appId=dummy&text=hi&to=es&from=en");
	}

	template<> template<>
	void translate_test_object_t::test<21>()
	{
		std::string url;
		mBing.getTranslateURL(url, "", "es", "hi");
		ensure_equals("bing URL", url,
			"http://api.microsofttranslator.com/v2/Http.svc/Translate?appId=dummy&text=hi&to=es");
	}

	template<> template<>
	void translate_test_object_t::test<22>()
	{
		std::string url;
		mGoogle.getTranslateURL(url, "en", "es", "hi");
		ensure_equals("google URL", url,
			"https://www.googleapis.com/language/translate/v2?key=dummy&q=hi&target=es&source=en");
	}

	template<> template<>
	void translate_test_object_t::test<23>()
	{
		std::string url;
		mGoogle.getTranslateURL(url, "", "es", "hi");
		ensure_equals("google URL", url,
			"https://www.googleapis.com/language/translate/v2?key=dummy&q=hi&target=es");
	}
}

//== Misc stubs ===============================================================
LLControlGroup gSavedSettings("test");

std::string LLUI::getLanguage() { return "en"; }
std::string LLTrans::getString(const std::string &xml_desc, const LLStringUtil::format_map_t& args) { return "dummy"; }

LLControlGroup::LLControlGroup(const std::string& name) : LLInstanceTracker<LLControlGroup, std::string>(name) {}
std::string LLControlGroup::getString(const std::string& name) { return "dummy"; }
LLControlGroup::~LLControlGroup() {}

LLCurl::Responder::Responder() {}
void LLCurl::Responder::httpFailure() { }
void LLCurl::Responder::httpSuccess() { }
void LLCurl::Responder::httpCompleted() { }
void LLCurl::Responder::completedRaw(LLChannelDescriptors const &,boost::shared_ptr<LLBufferArray> const &) { }
LLCurl::Responder::~Responder() {}

void LLHTTPClient::get(const std::string&, const LLSD&, ResponderPtr, const LLSD&, const F32, bool) {}
void LLHTTPClient::get(const std::string&, LLPointer<LLCurl::Responder>, const LLSD&, const F32, bool) {}

LLBufferStream::LLBufferStream(const LLChannelDescriptors& channels, LLBufferArray* buffer)
:	std::iostream(&mStreamBuf), mStreamBuf(channels, buffer) {}
LLBufferStream::~LLBufferStream() {}

LLBufferStreamBuf::LLBufferStreamBuf(const LLChannelDescriptors&, LLBufferArray*) {}
#if( LL_WINDOWS || __GNUC__ > 2)
LLBufferStreamBuf::pos_type LLBufferStreamBuf::seekoff(
	off_type off,
	std::ios::seekdir way,
	std::ios::openmode which)
#else
streampos LLBufferStreamBuf::seekoff(
	streamoff off,
	std::ios::seekdir way,
	std::ios::openmode which)
#endif
{ return 0; }
int LLBufferStreamBuf::sync() {return 0;}
int LLBufferStreamBuf::underflow() {return 0;}
int LLBufferStreamBuf::overflow(int) {return 0;}
LLBufferStreamBuf::~LLBufferStreamBuf() {}

S32 LLVersionInfo::getBuild() { return 0; }
const std::string& LLVersionInfo::getChannel() {static std::string dummy; return dummy;}
S32 LLVersionInfo::getMajor() { return 0; }
S32 LLVersionInfo::getMinor() { return 0; }
S32 LLVersionInfo::getPatch() { return 0; }