Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(221)

Side by Side Diff: chrome/renderer/translate/translate_script_browsertest.cc

Issue 23548019: Translate: add RenderViewTest to check if translate.js handles errors (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/chrome_tests.gypi ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/strings/utf_string_conversions.h"
6 #include "chrome/common/translate/translate_errors.h"
7 #include "chrome/test/base/chrome_render_view_test.h"
8 #include "grit/browser_resources.h"
9 #include "third_party/WebKit/public/web/WebFrame.h"
10 #include "third_party/WebKit/public/web/WebScriptSource.h"
11 #include "ui/base/resource/resource_bundle.h"
12
13 using WebKit::WebFrame;
14 using WebKit::WebScriptSource;
15
16 namespace {
17
18 // JavaScript code to set runtime test flags.
19 const char kThrowInitializationError[] = "throwInitializationError = true";
20 const char kThrowUnexpectedScriptError[] = "throwUnexpectedScriptError = true";
21 const char kCallbackReturnBooleanError[] = "callbackReturnBooleanError = true";
22 const char kCallbackReturnNumberError[] = "callbackReturnNumberError = true";
23 const char kSetCallbackErrorCode[] = "callbackErrorCode = ";
24
25 // JavaScript code to check if any error happens.
26 const char kError[] = "cr.googleTranslate.error";
27
28 // JavaScript code to get error code.
29 const char kErrorCode[] = "cr.googleTranslate.errorCode";
30
31 // JavaScript code to check if the library is ready.
32 const char kLibReady[] = "cr.googleTranslate.libReady";
33
34 // JavaScript code to perform translation.
35 const char kTranslate[] = "cr.googleTranslate.translate('auto', 'en')";
36
37 // JavaScript code to mimic element.js provided by a translate server.
38 const char kElementJs[] =
39 "translateApiKey = '';"
40 "google = {};"
41 "google.translate = {};"
42 "google.translate.TranslateService = function() {"
43 " if (window['throwInitializationError']) {"
44 " throw 'API initialization error';"
45 " }"
46 " return {"
47 " isAvailable: function() { return true; },"
48 " restore: function() {},"
49 " translatePage: function(originalLang, targetLang, cb) {"
50 " if (window['throwUnexpectedScriptError']) {"
51 " throw 'all your base are belong to us';"
52 " }"
53 " if (window['callbackReturnBooleanError']) {"
54 " cb(0, false, true);"
55 " }"
56 " if (window['callbackReturnNumberError']) {"
57 " cb(0, false, callbackErrorCode);"
58 " }"
59 " }"
60 " };"
61 "};"
62 "cr.googleTranslate.onTranslateElementLoad();";
63
64 }; // namespace
65
66 // This class is for testing resource/translate.js works and reports errors
67 // correctly.
68 class TranslateScriptBrowserTest : public ChromeRenderViewTest {
69 public:
70 TranslateScriptBrowserTest() {}
71
72 protected:
73 void InjectElementLibrary() {
74 std::string script;
75 base::StringPiece translate_js = ResourceBundle::GetSharedInstance().
76 GetRawDataResource(IDR_TRANSLATE_JS);
77 translate_js.CopyToString(&script);
78 script += kElementJs;
79 ExecuteScript(script);
80 }
81
82 void ExecuteScript(const std::string& script) {
83 WebScriptSource source = WebScriptSource(ASCIIToUTF16(script));
84 GetMainFrame()->executeScript(source);
85 }
86
87 bool Error() {
hajimehoshi 2013/09/09 06:17:42 GetError?
Takashi Toyoshima 2013/09/09 10:07:51 Done. Maybe you may want to say applying the same
88 return ExecuteScriptAndGetBoolResult(kError);
89 }
90
91 double ErrorCode() {
92 return ExecuteScriptAndGetNumberResult(kErrorCode);
93 }
94
95 bool IsLibReady() {
96 return ExecuteScriptAndGetBoolResult(kLibReady);
97 }
98
99 private:
100 virtual void SetUp() OVERRIDE {
101 ChromeRenderViewTest::SetUp();
102 }
103
104 virtual void TearDown() OVERRIDE {
105 ChromeRenderViewTest::TearDown();
106 }
107
108 double ExecuteScriptAndGetNumberResult(const std::string& script) {
109 WebScriptSource source = WebScriptSource(ASCIIToUTF16(script));
110 v8::HandleScope handle_scope;
111 v8::Handle<v8::Value> result =
112 GetMainFrame()->executeScriptAndReturnValue(source);
113 if (result.IsEmpty() || !result->IsNumber()) {
114 NOTREACHED();
115 return 0.0;
hajimehoshi 2013/09/09 06:17:42 (optional) Why don't you use NAN at cmath?
Takashi Toyoshima 2013/09/09 10:07:51 It sounds good, but for now I'll keep it as 0.0 an
116 }
117 return result->NumberValue();
118 }
119
120 bool ExecuteScriptAndGetBoolResult(const std::string& script) {
121 WebScriptSource source = WebScriptSource(ASCIIToUTF16(script));
122 v8::HandleScope handle_scope;
123 v8::Handle<v8::Value> result =
124 GetMainFrame()->executeScriptAndReturnValue(source);
125 if (result.IsEmpty() || !result->IsBoolean()) {
126 NOTREACHED();
127 return false;
128 }
129 return result->BooleanValue();
130 }
131
132 DISALLOW_COPY_AND_ASSIGN(TranslateScriptBrowserTest);
133 };
134
135 // Test if onTranslateElementLoad() succeeds to initialize the element library.
136 TEST_F(TranslateScriptBrowserTest, ElementLoadSuccess) {
137 InjectElementLibrary();
138 EXPECT_TRUE(IsLibReady());
139 EXPECT_FALSE(Error());
140 EXPECT_EQ(TranslateErrors::NONE, ErrorCode());
141 }
142
143 // Test if onTranslateElementLoad() fails to initialize the element library and
144 // report the right error code.
145 TEST_F(TranslateScriptBrowserTest, ElementLoadFailure) {
146 ExecuteScript(kThrowInitializationError);
147
148 InjectElementLibrary();
149 EXPECT_FALSE(IsLibReady());
150 EXPECT_TRUE(Error());
151 EXPECT_EQ(TranslateErrors::INITIALIZATION_ERROR, ErrorCode());
152 }
153
154 // Test if cr.googleTranslate.translate() works.
155 TEST_F(TranslateScriptBrowserTest, TranslateSuccess) {
156 InjectElementLibrary();
157 EXPECT_TRUE(IsLibReady());
158 EXPECT_FALSE(Error());
159 EXPECT_EQ(TranslateErrors::NONE, ErrorCode());
160
161 ExecuteScript(kTranslate);
162
163 EXPECT_FALSE(Error());
164 EXPECT_EQ(TranslateErrors::NONE, ErrorCode());
165 }
166
167 // Test if cr.googleTranslate.translate() handles library exception correctly.
168 TEST_F(TranslateScriptBrowserTest, TranslateFail) {
169 ExecuteScript(kThrowUnexpectedScriptError);
170
171 InjectElementLibrary();
172 EXPECT_TRUE(IsLibReady());
173 EXPECT_FALSE(Error());
174 EXPECT_EQ(TranslateErrors::NONE, ErrorCode());
175
176 ExecuteScript(kTranslate);
177
178 EXPECT_TRUE(Error());
179 EXPECT_EQ(TranslateErrors::UNEXPECTED_SCRIPT_ERROR, ErrorCode());
180 }
181
182 // Test if onTranslateProgress callback handles boolean type error correctly.
183 // Remove this test once server side changes the API to return a number.
184 TEST_F(TranslateScriptBrowserTest, CallbackGetBooleanError) {
185 ExecuteScript(kCallbackReturnBooleanError);
186
187 InjectElementLibrary();
188 EXPECT_TRUE(IsLibReady());
189 EXPECT_FALSE(Error());
190 EXPECT_EQ(TranslateErrors::NONE, ErrorCode());
191
192 ExecuteScript(kTranslate);
193
194 EXPECT_TRUE(Error());
195 EXPECT_EQ(TranslateErrors::TRANSLATION_ERROR, ErrorCode());
196 }
197
198 // Test if onTranslateProgress callback handles number type error correctly and
199 // converts it to the proper error code.
200 TEST_F(TranslateScriptBrowserTest, CallbackGetNumberError1) {
201 ExecuteScript(kCallbackReturnNumberError);
202 std::string setError(kSetCallbackErrorCode);
203 setError += "1";
hajimehoshi 2013/09/09 06:17:42 This is a little uneasy to read. How about create
Takashi Toyoshima 2013/09/09 10:07:51 Done.
204 ExecuteScript(setError);
205
206 InjectElementLibrary();
207 EXPECT_TRUE(IsLibReady());
208 EXPECT_FALSE(Error());
209 EXPECT_EQ(TranslateErrors::NONE, ErrorCode());
210
211 ExecuteScript(kTranslate);
212
213 EXPECT_TRUE(Error());
214 EXPECT_EQ(TranslateErrors::TRANSLATION_ERROR, ErrorCode());
215 }
216
217 // Test if onTranslateProgress callback handles number type error correctly and
218 // converts it to the proper error code.
219 TEST_F(TranslateScriptBrowserTest, CallbackGetNumberError2) {
220 ExecuteScript(kCallbackReturnNumberError);
221 std::string setError(kSetCallbackErrorCode);
222 setError += "2";
223 ExecuteScript(setError);
224
225 InjectElementLibrary();
226 EXPECT_TRUE(IsLibReady());
227 EXPECT_FALSE(Error());
228 EXPECT_EQ(TranslateErrors::NONE, ErrorCode());
229
230 ExecuteScript(kTranslate);
231
232 EXPECT_TRUE(Error());
233 EXPECT_EQ(TranslateErrors::UNSUPPORTED_LANGUAGE, ErrorCode());
234 }
235
236 // TODO(toyoshim): Add test for onLoadJavaScript.
OLDNEW
« no previous file with comments | « chrome/chrome_tests.gypi ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698