Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / translate / translate_browsertest.cc
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/command_line.h"
6 #include "base/files/file_path.h"
7 #include "base/strings/string_util.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/infobars/infobar_service.h"
11 #include "chrome/browser/translate/translate_infobar_delegate.h"
12 #include "chrome/browser/translate/translate_service.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/tabs/tab_strip_model.h"
15 #include "chrome/test/base/in_process_browser_test.h"
16 #include "chrome/test/base/test_switches.h"
17 #include "chrome/test/base/ui_test_utils.h"
18 #include "components/infobars/core/infobar.h"
19 #include "components/translate/core/browser/translate_manager.h"
20 #include "components/translate/core/browser/translate_script.h"
21 #include "components/translate/core/common/translate_switches.h"
22 #include "content/public/browser/notification_service.h"
23 #include "content/public/test/browser_test_utils.h"
24 #include "net/http/http_status_code.h"
25 #include "net/test/embedded_test_server/embedded_test_server.h"
26 #include "net/test/spawned_test_server/spawned_test_server.h"
27 #include "net/url_request/test_url_fetcher_factory.h"
28 #include "net/url_request/url_fetcher_delegate.h"
29
30 namespace {
31
32 const base::FilePath::CharType kTranslateRoot[] =
33     FILE_PATH_LITERAL("chrome/test/data/translate");
34 const char kNonSecurePrefix[] = "/translate/";
35 const char kSecurePrefix[] = "files/";
36 const char kFrenchTestPath[] = "fr_test.html";
37 const char kRefreshMetaTagTestPath[] = "refresh_meta_tag.html";
38 const char kRefreshMetaTagCaseInsensitiveTestPath[] =
39     "refresh_meta_tag_casei.html";
40 const char kRefreshMetaTagAtOnloadTestPath[] =
41     "refresh_meta_tag_at_onload.html";
42 const char kUpdateLocationTestPath[] = "update_location.html";
43 const char kUpdateLocationAtOnloadTestPath[] = "update_location_at_onload.html";
44 const char kMainScriptPath[] = "pseudo_main.js";
45 const char kElementMainScriptPath[] = "pseudo_element_main.js";
46
47 };  // namespace
48
49 class TranslateBrowserTest : public InProcessBrowserTest {
50  public:
51   TranslateBrowserTest()
52       : https_server_(net::SpawnedTestServer::TYPE_HTTPS,
53                       SSLOptions(SSLOptions::CERT_OK),
54                       base::FilePath(kTranslateRoot)),
55         infobar_service_(NULL) {}
56
57   virtual void SetUpOnMainThread() OVERRIDE {
58     TranslateService::SetUseInfobar(true);
59   }
60
61   virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
62     ASSERT_TRUE(https_server_.Start());
63     // Setup alternate security origin for testing in order to allow XHR against
64     // local test server. Note that this flag shows a confirm infobar in tests.
65     GURL base_url = GetSecureURL("");
66     command_line->AppendSwitchASCII(
67         translate::switches::kTranslateSecurityOrigin,
68         base_url.GetOrigin().spec());
69   }
70
71  protected:
72   GURL GetNonSecureURL(const std::string& path) const {
73     std::string prefix(kNonSecurePrefix);
74     return embedded_test_server()->GetURL(prefix + path);
75   }
76
77   GURL GetSecureURL(const std::string& path) const {
78     std::string prefix(kSecurePrefix);
79     return https_server_.GetURL(prefix + path);
80   }
81
82   TranslateInfoBarDelegate* GetExistingTranslateInfoBarDelegate() {
83     if (!infobar_service_) {
84       content::WebContents* web_contents =
85           browser()->tab_strip_model()->GetActiveWebContents();
86       if (web_contents)
87         infobar_service_ = InfoBarService::FromWebContents(web_contents);
88     }
89     if (!infobar_service_) {
90       ADD_FAILURE() << "infobar service is not available";
91       return NULL;
92     }
93
94     TranslateInfoBarDelegate* delegate = NULL;
95     for (size_t i = 0; i < infobar_service_->infobar_count(); ++i) {
96       // Check if the shown infobar is a confirm infobar coming from the
97       // |kTranslateSecurityOrigin| flag specified in SetUpCommandLine().
98       // This infobar appears in all tests of TranslateBrowserTest and can be
99       // ignored here.
100       if (infobar_service_->infobar_at(i)->delegate()->
101               AsConfirmInfoBarDelegate()) {
102         continue;
103       }
104
105       TranslateInfoBarDelegate* translate =
106           infobar_service_->infobar_at(i)->delegate()->
107               AsTranslateInfoBarDelegate();
108       if (translate) {
109         EXPECT_FALSE(delegate) << "multiple infobars are shown unexpectedly";
110         delegate = translate;
111         continue;
112       }
113
114       // Other infobar should not be shown.
115       EXPECT_TRUE(delegate);
116     }
117     return delegate;
118   }
119
120  private:
121   net::SpawnedTestServer https_server_;
122   InfoBarService* infobar_service_;
123
124   typedef net::SpawnedTestServer::SSLOptions SSLOptions;
125
126   DISALLOW_COPY_AND_ASSIGN(TranslateBrowserTest);
127 };
128
129 IN_PROC_BROWSER_TEST_F(TranslateBrowserTest, TranslateInIsolatedWorld) {
130 #if defined(OS_WIN) && defined(USE_ASH)
131   // Disable this test in Metro+Ash for now (http://crbug.com/262796).
132   if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
133     return;
134 #endif
135
136   net::TestURLFetcherFactory factory;
137   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
138
139   // Check if there is no Translate infobar.
140   TranslateInfoBarDelegate* translate = GetExistingTranslateInfoBarDelegate();
141   EXPECT_FALSE(translate);
142
143   // Setup infobar observer.
144   content::WindowedNotificationObserver infobar(
145       chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED,
146       content::NotificationService::AllSources());
147
148   // Setup page title observer.
149   content::WebContents* web_contents =
150       browser()->tab_strip_model()->GetActiveWebContents();
151   ASSERT_TRUE(web_contents);
152   content::TitleWatcher watcher(web_contents, base::ASCIIToUTF16("PASS"));
153   watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL"));
154
155   // Visit non-secure page which is going to be translated.
156   ui_test_utils::NavigateToURL(browser(), GetNonSecureURL(kFrenchTestPath));
157
158   // Wait for Chrome Translate infobar.
159   infobar.Wait();
160
161   // Perform Chrome Translate.
162   translate = GetExistingTranslateInfoBarDelegate();
163   ASSERT_TRUE(translate);
164   translate->Translate();
165
166   // Hook URLFetcher for element.js.
167   GURL script1_url = GetSecureURL(kMainScriptPath);
168   GURL script2_url = GetSecureURL(kElementMainScriptPath);
169   std::string element_js = "main_script_url = '" + script1_url.spec() + "';\n";
170   element_js += "element_main_script_url = '" + script2_url.spec() + "';\n";
171   element_js +=
172     "google = { 'translate' : { 'TranslateService' : function() { return {\n"
173     "  isAvailable: function() {\n"
174     "    cr.googleTranslate.onLoadJavascript(main_script_url);\n"
175     "    return true;\n"
176     "  },\n"
177     "  translatePage: function(sl, tl, cb) {\n"
178     "    cb(1, true);\n"
179     "  }\n"
180     "} } } };\n"
181     "cr.googleTranslate.onTranslateElementLoad();\n";
182   net::TestURLFetcher* fetcher =
183       factory.GetFetcherByID(TranslateScript::kFetcherId);
184   ASSERT_TRUE(fetcher);
185   net::URLRequestStatus status;
186   status.set_status(net::URLRequestStatus::SUCCESS);
187   fetcher->set_status(status);
188   fetcher->set_url(fetcher->GetOriginalURL());
189   fetcher->set_response_code(net::HTTP_OK);
190   fetcher->SetResponseString(element_js);
191   fetcher->delegate()->OnURLFetchComplete(fetcher);
192
193   // Wait for the page title is changed after the test finished.
194   const base::string16 result = watcher.WaitAndGetTitle();
195   EXPECT_EQ("PASS", base::UTF16ToASCII(result));
196 }
197
198 IN_PROC_BROWSER_TEST_F(TranslateBrowserTest, IgnoreRefreshMetaTag) {
199 #if defined(OS_WIN) && defined(USE_ASH)
200   // Disable this test in Metro+Ash for now (http://crbug.com/262796).
201   if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
202     return;
203 #endif
204
205   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
206
207   // Check if there is no Translate infobar.
208   TranslateInfoBarDelegate* translate = GetExistingTranslateInfoBarDelegate();
209   EXPECT_FALSE(translate);
210
211   // Setup page title observer.
212   content::WebContents* web_contents =
213       browser()->tab_strip_model()->GetActiveWebContents();
214   ASSERT_TRUE(web_contents);
215   content::TitleWatcher watcher(web_contents, base::ASCIIToUTF16("PASS"));
216   watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL"));
217
218   // Visit a test page.
219   ui_test_utils::NavigateToURL(
220       browser(),
221       GetNonSecureURL(kRefreshMetaTagTestPath));
222
223   // Wait for the page title is changed after the test finished.
224   const base::string16 result = watcher.WaitAndGetTitle();
225   EXPECT_EQ("PASS", base::UTF16ToASCII(result));
226
227   // Check if there is no Translate infobar.
228   translate = GetExistingTranslateInfoBarDelegate();
229   EXPECT_FALSE(translate);
230 }
231
232 IN_PROC_BROWSER_TEST_F(TranslateBrowserTest,
233                        IgnoreRefreshMetaTagInCaseInsensitive) {
234 #if defined(OS_WIN) && defined(USE_ASH)
235   // Disable this test in Metro+Ash for now (http://crbug.com/262796).
236   if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
237     return;
238 #endif
239
240   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
241
242   // Check if there is no Translate infobar.
243   TranslateInfoBarDelegate* translate = GetExistingTranslateInfoBarDelegate();
244   EXPECT_FALSE(translate);
245
246   // Setup page title observer.
247   content::WebContents* web_contents =
248       browser()->tab_strip_model()->GetActiveWebContents();
249   ASSERT_TRUE(web_contents);
250   content::TitleWatcher watcher(web_contents, base::ASCIIToUTF16("PASS"));
251   watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL"));
252
253   // Visit a test page.
254   ui_test_utils::NavigateToURL(
255       browser(),
256       GetNonSecureURL(kRefreshMetaTagCaseInsensitiveTestPath));
257
258   // Wait for the page title is changed after the test finished.
259   const base::string16 result = watcher.WaitAndGetTitle();
260   EXPECT_EQ("PASS", base::UTF16ToASCII(result));
261
262   // Check if there is no Translate infobar.
263   translate = GetExistingTranslateInfoBarDelegate();
264   EXPECT_FALSE(translate);
265 }
266
267 IN_PROC_BROWSER_TEST_F(TranslateBrowserTest, IgnoreRefreshMetaTagAtOnload) {
268 #if defined(OS_WIN) && defined(USE_ASH)
269   // Disable this test in Metro+Ash for now (http://crbug.com/262796).
270   if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
271     return;
272 #endif
273
274   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
275
276   // Check if there is no Translate infobar.
277   TranslateInfoBarDelegate* translate = GetExistingTranslateInfoBarDelegate();
278   EXPECT_FALSE(translate);
279
280   // Setup page title observer.
281   content::WebContents* web_contents =
282       browser()->tab_strip_model()->GetActiveWebContents();
283   ASSERT_TRUE(web_contents);
284   content::TitleWatcher watcher(web_contents, base::ASCIIToUTF16("PASS"));
285   watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL"));
286
287   // Visit a test page.
288   ui_test_utils::NavigateToURL(
289       browser(),
290       GetNonSecureURL(kRefreshMetaTagAtOnloadTestPath));
291
292   // Wait for the page title is changed after the test finished.
293   const base::string16 result = watcher.WaitAndGetTitle();
294   EXPECT_EQ("PASS", base::UTF16ToASCII(result));
295
296   // Check if there is no Translate infobar.
297   translate = GetExistingTranslateInfoBarDelegate();
298   EXPECT_FALSE(translate);
299 }
300
301 IN_PROC_BROWSER_TEST_F(TranslateBrowserTest, UpdateLocation) {
302 #if defined(OS_WIN) && defined(USE_ASH)
303   // Disable this test in Metro+Ash for now (http://crbug.com/262796).
304   if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
305     return;
306 #endif
307
308   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
309
310   // Check if there is no Translate infobar.
311   TranslateInfoBarDelegate* translate = GetExistingTranslateInfoBarDelegate();
312   EXPECT_FALSE(translate);
313
314   // Setup page title observer.
315   content::WebContents* web_contents =
316       browser()->tab_strip_model()->GetActiveWebContents();
317   ASSERT_TRUE(web_contents);
318   content::TitleWatcher watcher(web_contents, base::ASCIIToUTF16("PASS"));
319   watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL"));
320
321   // Visit a test page.
322   ui_test_utils::NavigateToURL(
323       browser(),
324       GetNonSecureURL(kUpdateLocationTestPath));
325
326   // Wait for the page title is changed after the test finished.
327   const base::string16 result = watcher.WaitAndGetTitle();
328   EXPECT_EQ("PASS", base::UTF16ToASCII(result));
329
330   // Check if there is no Translate infobar.
331   translate = GetExistingTranslateInfoBarDelegate();
332   EXPECT_FALSE(translate);
333 }
334
335 IN_PROC_BROWSER_TEST_F(TranslateBrowserTest, UpdateLocationAtOnload) {
336 #if defined(OS_WIN) && defined(USE_ASH)
337   // Disable this test in Metro+Ash for now (http://crbug.com/262796).
338   if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAshBrowserTests))
339     return;
340 #endif
341
342   ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
343
344   // Check if there is no Translate infobar.
345   TranslateInfoBarDelegate* translate = GetExistingTranslateInfoBarDelegate();
346   EXPECT_FALSE(translate);
347
348   // Setup page title observer.
349   content::WebContents* web_contents =
350       browser()->tab_strip_model()->GetActiveWebContents();
351   ASSERT_TRUE(web_contents);
352   content::TitleWatcher watcher(web_contents, base::ASCIIToUTF16("PASS"));
353   watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL"));
354
355   // Visit a test page.
356   ui_test_utils::NavigateToURL(
357       browser(),
358       GetNonSecureURL(kUpdateLocationAtOnloadTestPath));
359
360   // Wait for the page title is changed after the test finished.
361   const base::string16 result = watcher.WaitAndGetTitle();
362   EXPECT_EQ("PASS", base::UTF16ToASCII(result));
363
364   // Check if there is no Translate infobar.
365   translate = GetExistingTranslateInfoBarDelegate();
366   EXPECT_FALSE(translate);
367 }