PageRenderTime 44ms CodeModel.GetById 0ms RepoModel.GetById 0ms 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
Possible License(s): LGPL-2.1
  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. #include "linden_common.h"
  26. #include "../test/lltut.h"
  27. #include "../lltranslate.h"
  28. #include "../llversioninfo.h"
  29. #include "../llviewercontrol.h"
  30. #include "llbufferstream.h"
  31. #include "lltrans.h"
  32. #include "llui.h"
  33. static const std::string GOOGLE_VALID_RESPONSE1 =
  34. "{\
  35. \"data\": {\
  36. \"translations\": [\
  37. {\
  38. \"translatedText\": \"??????\",\
  39. \"detectedSourceLanguage\": \"es\"\
  40. }\
  41. ]\
  42. }\
  43. }";
  44. static const std::string GOOGLE_VALID_RESPONSE2 =
  45. "{\
  46. \"data\": {\
  47. \"translations\": [\
  48. {\
  49. \"translatedText\": \"??????\"\
  50. }\
  51. ]\
  52. }\
  53. }\
  54. ";
  55. static const std::string GOOGLE_VALID_RESPONSE3 =
  56. "{\
  57. \"error\": {\
  58. \"errors\": [\
  59. {\
  60. \"domain\": \"global\",\
  61. \"reason\": \"invalid\",\
  62. \"message\": \"Invalid Value\"\
  63. }\
  64. ],\
  65. \"code\": 400,\
  66. \"message\": \"Invalid Value\"\
  67. }\
  68. }";
  69. static const std::string BING_VALID_RESPONSE1 =
  70. "<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">??????</string>";
  71. static const std::string BING_VALID_RESPONSE2 =
  72. "<html><body><h1>Argument Exception</h1><p>Method: Translate()</p><p>Parameter: </p>\
  73. <p>Message: 'from' must be a valid language</p><code></code>\
  74. <p>message id=3743.V2_Rest.Translate.58E8454F</p></body></html>";
  75. static const std::string BING_VALID_RESPONSE3 =
  76. "<html><body><h1>Argument Exception</h1><p>Method: Translate()</p>\
  77. <p>Parameter: appId</p><p>Message: Invalid appId&#xD;\nParameter name: appId</p>\
  78. <code></code><p>message id=3737.V2_Rest.Translate.56016759</p></body></html>";
  79. namespace tut
  80. {
  81. class translate_test
  82. {
  83. protected:
  84. void test_translation(
  85. LLTranslationAPIHandler& handler,
  86. int status, const std::string& resp,
  87. const std::string& exp_trans, const std::string& exp_lang, const std::string& exp_err)
  88. {
  89. std::string translation, detected_lang, err_msg;
  90. bool rc = handler.parseResponse(status, resp, translation, detected_lang, err_msg);
  91. ensure_equals("rc", rc, (status == 200));
  92. ensure_equals("err_msg", err_msg, exp_err);
  93. ensure_equals("translation", translation, exp_trans);
  94. ensure_equals("detected_lang", detected_lang, exp_lang);
  95. }
  96. LLGoogleTranslationHandler mGoogle;
  97. LLBingTranslationHandler mBing;
  98. };
  99. typedef test_group<translate_test> translate_test_group_t;
  100. typedef translate_test_group_t::object translate_test_object_t;
  101. tut::translate_test_group_t tut_translate("LLTranslate");
  102. template<> template<>
  103. void translate_test_object_t::test<1>()
  104. {
  105. test_translation(mGoogle, 200, GOOGLE_VALID_RESPONSE1, "??????", "es", "");
  106. }
  107. template<> template<>
  108. void translate_test_object_t::test<2>()
  109. {
  110. test_translation(mGoogle, 200, GOOGLE_VALID_RESPONSE2, "??????", "", "");
  111. }
  112. template<> template<>
  113. void translate_test_object_t::test<3>()
  114. {
  115. test_translation(mGoogle, 400, GOOGLE_VALID_RESPONSE3, "", "", "Invalid Value");
  116. }
  117. template<> template<>
  118. void translate_test_object_t::test<4>()
  119. {
  120. test_translation(mGoogle, 400,
  121. "",
  122. "", "", "* Line 1, Column 1\n Syntax error: value, object or array expected.\n");
  123. }
  124. template<> template<>
  125. void translate_test_object_t::test<5>()
  126. {
  127. test_translation(mGoogle, 400,
  128. "[]",
  129. "", "", "");
  130. }
  131. template<> template<>
  132. void translate_test_object_t::test<6>()
  133. {
  134. test_translation(mGoogle, 400,
  135. "{\"oops\": \"invalid\"}",
  136. "", "", "");
  137. }
  138. template<> template<>
  139. void translate_test_object_t::test<7>()
  140. {
  141. test_translation(mGoogle, 400,
  142. "{\"data\": {}}",
  143. "", "", "");
  144. }
  145. template<> template<>
  146. void translate_test_object_t::test<8>()
  147. {
  148. test_translation(mGoogle, 400,
  149. "{\"data\": { \"translations\": [ {} ] }}",
  150. "", "", "");
  151. }
  152. template<> template<>
  153. void translate_test_object_t::test<9>()
  154. {
  155. test_translation(mGoogle, 400,
  156. "{\"data\": { \"translations\": [ { \"translatedTextZZZ\": \"??????\", \"detectedSourceLanguageZZZ\": \"es\" } ] }}",
  157. "", "", "");
  158. }
  159. template<> template<>
  160. void translate_test_object_t::test<10>()
  161. {
  162. test_translation(mBing, 200, BING_VALID_RESPONSE1, "??????", "", "");
  163. }
  164. template<> template<>
  165. void translate_test_object_t::test<11>()
  166. {
  167. test_translation(mBing, 400, BING_VALID_RESPONSE2, "", "", "'from' must be a valid language");
  168. }
  169. template<> template<>
  170. void translate_test_object_t::test<12>()
  171. {
  172. test_translation(mBing, 400, BING_VALID_RESPONSE3, "", "", "Invalid appId\nParameter name: appId");
  173. }
  174. template<> template<>
  175. void translate_test_object_t::test<13>()
  176. {
  177. test_translation(mBing, 200,
  178. "??????</string>",
  179. "??????", "", "");
  180. }
  181. template<> template<>
  182. void translate_test_object_t::test<14>()
  183. {
  184. test_translation(mBing, 200,
  185. "<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">??????",
  186. "??????", "", "");
  187. }
  188. template<> template<>
  189. void translate_test_object_t::test<15>()
  190. {
  191. test_translation(mBing, 200,
  192. "??????",
  193. "??????", "", "");
  194. }
  195. template<> template<>
  196. void translate_test_object_t::test<16>()
  197. {
  198. test_translation(mBing, 400,
  199. "Message: some error</p>",
  200. "", "", "some error");
  201. }
  202. template<> template<>
  203. void translate_test_object_t::test<17>()
  204. {
  205. test_translation(mBing, 400,
  206. "Message: some error",
  207. "", "", "some error");
  208. }
  209. template<> template<>
  210. void translate_test_object_t::test<18>()
  211. {
  212. test_translation(mBing, 400,
  213. "some error</p>",
  214. "", "", "some error");
  215. }
  216. template<> template<>
  217. void translate_test_object_t::test<19>()
  218. {
  219. test_translation(mBing, 400,
  220. "some error",
  221. "", "", "some error");
  222. }
  223. template<> template<>
  224. void translate_test_object_t::test<20>()
  225. {
  226. std::string url;
  227. mBing.getTranslateURL(url, "en", "es", "hi");
  228. ensure_equals("bing URL", url,
  229. "http://api.microsofttranslator.com/v2/Http.svc/Translate?appId=dummy&text=hi&to=es&from=en");
  230. }
  231. template<> template<>
  232. void translate_test_object_t::test<21>()
  233. {
  234. std::string url;
  235. mBing.getTranslateURL(url, "", "es", "hi");
  236. ensure_equals("bing URL", url,
  237. "http://api.microsofttranslator.com/v2/Http.svc/Translate?appId=dummy&text=hi&to=es");
  238. }
  239. template<> template<>
  240. void translate_test_object_t::test<22>()
  241. {
  242. std::string url;
  243. mGoogle.getTranslateURL(url, "en", "es", "hi");
  244. ensure_equals("google URL", url,
  245. "https://www.googleapis.com/language/translate/v2?key=dummy&q=hi&target=es&source=en");
  246. }
  247. template<> template<>
  248. void translate_test_object_t::test<23>()
  249. {
  250. std::string url;
  251. mGoogle.getTranslateURL(url, "", "es", "hi");
  252. ensure_equals("google URL", url,
  253. "https://www.googleapis.com/language/translate/v2?key=dummy&q=hi&target=es");
  254. }
  255. }
  256. //== Misc stubs ===============================================================
  257. LLControlGroup gSavedSettings("test");
  258. std::string LLUI::getLanguage() { return "en"; }
  259. std::string LLTrans::getString(const std::string &xml_desc, const LLStringUtil::format_map_t& args) { return "dummy"; }
  260. LLControlGroup::LLControlGroup(const std::string& name) : LLInstanceTracker<LLControlGroup, std::string>(name) {}
  261. std::string LLControlGroup::getString(const std::string& name) { return "dummy"; }
  262. LLControlGroup::~LLControlGroup() {}
  263. namespace boost {
  264. void intrusive_ptr_add_ref(LLCurl::Responder*) {}
  265. void intrusive_ptr_release(LLCurl::Responder*) {}
  266. }
  267. LLCurl::Responder::Responder() {}
  268. void LLCurl::Responder::completedHeader(U32, std::string const&, LLSD const&) {}
  269. void LLCurl::Responder::completedRaw(U32, const std::string&, const LLChannelDescriptors&, const LLIOPipe::buffer_ptr_t& buffer) {}
  270. void LLCurl::Responder::completed(U32, std::string const&, LLSD const&) {}
  271. void LLCurl::Responder::error(U32, std::string const&) {}
  272. void LLCurl::Responder::errorWithContent(U32, std::string const&, LLSD const&) {}
  273. void LLCurl::Responder::result(LLSD const&) {}
  274. LLCurl::Responder::~Responder() {}
  275. void LLHTTPClient::get(const std::string&, const LLSD&, ResponderPtr, const LLSD&, const F32) {}
  276. void LLHTTPClient::get(const std::string&, boost::intrusive_ptr<LLCurl::Responder>, const LLSD&, const F32) {}
  277. LLBufferStream::LLBufferStream(const LLChannelDescriptors& channels, LLBufferArray* buffer)
  278. : std::iostream(&mStreamBuf), mStreamBuf(channels, buffer) {}
  279. LLBufferStream::~LLBufferStream() {}
  280. LLBufferStreamBuf::LLBufferStreamBuf(const LLChannelDescriptors&, LLBufferArray*) {}
  281. #if( LL_WINDOWS || __GNUC__ > 2)
  282. LLBufferStreamBuf::pos_type LLBufferStreamBuf::seekoff(
  283. off_type off,
  284. std::ios::seekdir way,
  285. std::ios::openmode which)
  286. #else
  287. streampos LLBufferStreamBuf::seekoff(
  288. streamoff off,
  289. std::ios::seekdir way,
  290. std::ios::openmode which)
  291. #endif
  292. { return 0; }
  293. int LLBufferStreamBuf::sync() {return 0;}
  294. int LLBufferStreamBuf::underflow() {return 0;}
  295. int LLBufferStreamBuf::overflow(int) {return 0;}
  296. LLBufferStreamBuf::~LLBufferStreamBuf() {}
  297. S32 LLVersionInfo::getBuild() { return 0; }
  298. const std::string& LLVersionInfo::getChannel() {static std::string dummy; return dummy;}
  299. S32 LLVersionInfo::getMajor() { return 0; }
  300. S32 LLVersionInfo::getMinor() { return 0; }
  301. S32 LLVersionInfo::getPatch() { return 0; }