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