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.
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"
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";
49 class TranslateBrowserTest : public InProcessBrowserTest {
51 TranslateBrowserTest()
52 : https_server_(net::SpawnedTestServer::TYPE_HTTPS,
53 SSLOptions(SSLOptions::CERT_OK),
54 base::FilePath(kTranslateRoot)),
55 infobar_service_(NULL) {}
57 virtual void SetUpOnMainThread() OVERRIDE {
58 TranslateService::SetUseInfobar(true);
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());
72 GURL GetNonSecureURL(const std::string& path) const {
73 std::string prefix(kNonSecurePrefix);
74 return embedded_test_server()->GetURL(prefix + path);
77 GURL GetSecureURL(const std::string& path) const {
78 std::string prefix(kSecurePrefix);
79 return https_server_.GetURL(prefix + path);
82 TranslateInfoBarDelegate* GetExistingTranslateInfoBarDelegate() {
83 if (!infobar_service_) {
84 content::WebContents* web_contents =
85 browser()->tab_strip_model()->GetActiveWebContents();
87 infobar_service_ = InfoBarService::FromWebContents(web_contents);
89 if (!infobar_service_) {
90 ADD_FAILURE() << "infobar service is not available";
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
100 if (infobar_service_->infobar_at(i)->delegate()->
101 AsConfirmInfoBarDelegate()) {
105 TranslateInfoBarDelegate* translate =
106 infobar_service_->infobar_at(i)->delegate()->
107 AsTranslateInfoBarDelegate();
109 EXPECT_FALSE(delegate) << "multiple infobars are shown unexpectedly";
110 delegate = translate;
114 // Other infobar should not be shown.
115 EXPECT_TRUE(delegate);
121 net::SpawnedTestServer https_server_;
122 InfoBarService* infobar_service_;
124 typedef net::SpawnedTestServer::SSLOptions SSLOptions;
126 DISALLOW_COPY_AND_ASSIGN(TranslateBrowserTest);
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))
136 net::TestURLFetcherFactory factory;
137 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
139 // Check if there is no Translate infobar.
140 TranslateInfoBarDelegate* translate = GetExistingTranslateInfoBarDelegate();
141 EXPECT_FALSE(translate);
143 // Setup infobar observer.
144 content::WindowedNotificationObserver infobar(
145 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED,
146 content::NotificationService::AllSources());
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"));
155 // Visit non-secure page which is going to be translated.
156 ui_test_utils::NavigateToURL(browser(), GetNonSecureURL(kFrenchTestPath));
158 // Wait for Chrome Translate infobar.
161 // Perform Chrome Translate.
162 translate = GetExistingTranslateInfoBarDelegate();
163 ASSERT_TRUE(translate);
164 translate->Translate();
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";
172 "google = { 'translate' : { 'TranslateService' : function() { return {\n"
173 " isAvailable: function() {\n"
174 " cr.googleTranslate.onLoadJavascript(main_script_url);\n"
177 " translatePage: function(sl, tl, cb) {\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);
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));
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))
205 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
207 // Check if there is no Translate infobar.
208 TranslateInfoBarDelegate* translate = GetExistingTranslateInfoBarDelegate();
209 EXPECT_FALSE(translate);
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"));
218 // Visit a test page.
219 ui_test_utils::NavigateToURL(
221 GetNonSecureURL(kRefreshMetaTagTestPath));
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));
227 // Check if there is no Translate infobar.
228 translate = GetExistingTranslateInfoBarDelegate();
229 EXPECT_FALSE(translate);
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))
240 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
242 // Check if there is no Translate infobar.
243 TranslateInfoBarDelegate* translate = GetExistingTranslateInfoBarDelegate();
244 EXPECT_FALSE(translate);
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"));
253 // Visit a test page.
254 ui_test_utils::NavigateToURL(
256 GetNonSecureURL(kRefreshMetaTagCaseInsensitiveTestPath));
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));
262 // Check if there is no Translate infobar.
263 translate = GetExistingTranslateInfoBarDelegate();
264 EXPECT_FALSE(translate);
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))
274 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
276 // Check if there is no Translate infobar.
277 TranslateInfoBarDelegate* translate = GetExistingTranslateInfoBarDelegate();
278 EXPECT_FALSE(translate);
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"));
287 // Visit a test page.
288 ui_test_utils::NavigateToURL(
290 GetNonSecureURL(kRefreshMetaTagAtOnloadTestPath));
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));
296 // Check if there is no Translate infobar.
297 translate = GetExistingTranslateInfoBarDelegate();
298 EXPECT_FALSE(translate);
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))
308 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
310 // Check if there is no Translate infobar.
311 TranslateInfoBarDelegate* translate = GetExistingTranslateInfoBarDelegate();
312 EXPECT_FALSE(translate);
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"));
321 // Visit a test page.
322 ui_test_utils::NavigateToURL(
324 GetNonSecureURL(kUpdateLocationTestPath));
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));
330 // Check if there is no Translate infobar.
331 translate = GetExistingTranslateInfoBarDelegate();
332 EXPECT_FALSE(translate);
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))
342 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
344 // Check if there is no Translate infobar.
345 TranslateInfoBarDelegate* translate = GetExistingTranslateInfoBarDelegate();
346 EXPECT_FALSE(translate);
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"));
355 // Visit a test page.
356 ui_test_utils::NavigateToURL(
358 GetNonSecureURL(kUpdateLocationAtOnloadTestPath));
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));
364 // Check if there is no Translate infobar.
365 translate = GetExistingTranslateInfoBarDelegate();
366 EXPECT_FALSE(translate);