Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / translate / translate_browsertest.cc
blobb77a1cc4a71fda9082d0a436497d86d30d34ce4b
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_service.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/tabs/tab_strip_model.h"
14 #include "chrome/test/base/in_process_browser_test.h"
15 #include "chrome/test/base/test_switches.h"
16 #include "chrome/test/base/ui_test_utils.h"
17 #include "components/infobars/core/infobar.h"
18 #include "components/translate/core/browser/translate_infobar_delegate.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"
30 namespace {
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";
47 }; // namespace
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) {}
57 void SetUp() override {
58 translate::TranslateManager::SetIgnoreMissingKeyForTesting(true);
59 InProcessBrowserTest::SetUp();
62 void SetUpCommandLine(base::CommandLine* command_line) override {
63 ASSERT_TRUE(https_server_.Start());
64 // Setup alternate security origin for testing in order to allow XHR against
65 // local test server. Note that this flag shows a confirm infobar in tests.
66 GURL base_url = GetSecureURL("");
67 command_line->AppendSwitchASCII(
68 translate::switches::kTranslateSecurityOrigin,
69 base_url.GetOrigin().spec());
72 protected:
73 GURL GetNonSecureURL(const std::string& path) const {
74 std::string prefix(kNonSecurePrefix);
75 return embedded_test_server()->GetURL(prefix + path);
78 GURL GetSecureURL(const std::string& path) const {
79 std::string prefix(kSecurePrefix);
80 return https_server_.GetURL(prefix + path);
83 translate::TranslateInfoBarDelegate* GetExistingTranslateInfoBarDelegate() {
84 if (!infobar_service_) {
85 content::WebContents* web_contents =
86 browser()->tab_strip_model()->GetActiveWebContents();
87 if (web_contents)
88 infobar_service_ = InfoBarService::FromWebContents(web_contents);
90 if (!infobar_service_) {
91 ADD_FAILURE() << "infobar service is not available";
92 return NULL;
95 translate::TranslateInfoBarDelegate* delegate = NULL;
96 for (size_t i = 0; i < infobar_service_->infobar_count(); ++i) {
97 // Check if the shown infobar is a confirm infobar coming from the
98 // |kTranslateSecurityOrigin| flag specified in SetUpCommandLine().
99 // This infobar appears in all tests of TranslateBrowserTest and can be
100 // ignored here.
101 if (infobar_service_->infobar_at(i)->delegate()->
102 AsConfirmInfoBarDelegate()) {
103 continue;
106 translate::TranslateInfoBarDelegate* translate =
107 infobar_service_->infobar_at(i)
108 ->delegate()
109 ->AsTranslateInfoBarDelegate();
110 if (translate) {
111 EXPECT_FALSE(delegate) << "multiple infobars are shown unexpectedly";
112 delegate = translate;
113 continue;
116 // Other infobar should not be shown.
117 EXPECT_TRUE(delegate);
119 return delegate;
122 private:
123 net::SpawnedTestServer https_server_;
124 InfoBarService* infobar_service_;
126 typedef net::SpawnedTestServer::SSLOptions SSLOptions;
128 DISALLOW_COPY_AND_ASSIGN(TranslateBrowserTest);
131 IN_PROC_BROWSER_TEST_F(TranslateBrowserTest, TranslateInIsolatedWorld) {
132 // TODO(port): Test corresponding bubble translate UX: http://crbug.com/383235
133 if (TranslateService::IsTranslateBubbleEnabled())
134 return;
136 net::TestURLFetcherFactory factory;
137 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
139 // Check if there is no Translate infobar.
140 translate::TranslateInfoBarDelegate* translate =
141 GetExistingTranslateInfoBarDelegate();
142 EXPECT_FALSE(translate);
144 // Setup infobar observer.
145 content::WindowedNotificationObserver infobar(
146 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED,
147 content::NotificationService::AllSources());
149 // Setup page title observer.
150 content::WebContents* web_contents =
151 browser()->tab_strip_model()->GetActiveWebContents();
152 ASSERT_TRUE(web_contents);
153 content::TitleWatcher watcher(web_contents, base::ASCIIToUTF16("PASS"));
154 watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL"));
156 // Visit non-secure page which is going to be translated.
157 ui_test_utils::NavigateToURL(browser(), GetNonSecureURL(kFrenchTestPath));
159 // Wait for Chrome Translate infobar.
160 infobar.Wait();
162 // Perform Chrome Translate.
163 translate = GetExistingTranslateInfoBarDelegate();
164 ASSERT_TRUE(translate);
165 translate->Translate();
167 // Hook URLFetcher for element.js.
168 GURL script1_url = GetSecureURL(kMainScriptPath);
169 GURL script2_url = GetSecureURL(kElementMainScriptPath);
170 std::string element_js = "main_script_url = '" + script1_url.spec() + "';\n";
171 element_js += "element_main_script_url = '" + script2_url.spec() + "';\n";
172 element_js +=
173 "google = { 'translate' : { 'TranslateService' : function() { return {\n"
174 " isAvailable: function() {\n"
175 " cr.googleTranslate.onLoadJavascript(main_script_url);\n"
176 " return true;\n"
177 " },\n"
178 " translatePage: function(sl, tl, cb) {\n"
179 " cb(1, true);\n"
180 " }\n"
181 "} } } };\n"
182 "cr.googleTranslate.onTranslateElementLoad();\n";
183 net::TestURLFetcher* fetcher =
184 factory.GetFetcherByID(translate::TranslateScript::kFetcherId);
185 ASSERT_TRUE(fetcher);
186 fetcher->set_status(net::URLRequestStatus());
187 fetcher->set_url(fetcher->GetOriginalURL());
188 fetcher->set_response_code(net::HTTP_OK);
189 fetcher->SetResponseString(element_js);
190 fetcher->delegate()->OnURLFetchComplete(fetcher);
192 // Wait for the page title is changed after the test finished.
193 const base::string16 result = watcher.WaitAndGetTitle();
194 EXPECT_EQ("PASS", base::UTF16ToASCII(result));
197 IN_PROC_BROWSER_TEST_F(TranslateBrowserTest, IgnoreRefreshMetaTag) {
198 // TODO(port): Test corresponding bubble translate UX: http://crbug.com/383235
199 if (TranslateService::IsTranslateBubbleEnabled())
200 return;
202 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
204 // Check if there is no Translate infobar.
205 translate::TranslateInfoBarDelegate* translate =
206 GetExistingTranslateInfoBarDelegate();
207 EXPECT_FALSE(translate);
209 // Setup page title observer.
210 content::WebContents* web_contents =
211 browser()->tab_strip_model()->GetActiveWebContents();
212 ASSERT_TRUE(web_contents);
213 content::TitleWatcher watcher(web_contents, base::ASCIIToUTF16("PASS"));
214 watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL"));
216 // Visit a test page.
217 ui_test_utils::NavigateToURL(
218 browser(),
219 GetNonSecureURL(kRefreshMetaTagTestPath));
221 // Wait for the page title is changed after the test finished.
222 const base::string16 result = watcher.WaitAndGetTitle();
223 EXPECT_EQ("PASS", base::UTF16ToASCII(result));
225 // Check if there is no Translate infobar.
226 translate = GetExistingTranslateInfoBarDelegate();
227 EXPECT_FALSE(translate);
230 IN_PROC_BROWSER_TEST_F(TranslateBrowserTest,
231 IgnoreRefreshMetaTagInCaseInsensitive) {
232 // TODO(port): Test corresponding bubble translate UX: http://crbug.com/383235
233 if (TranslateService::IsTranslateBubbleEnabled())
234 return;
236 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
238 // Check if there is no Translate infobar.
239 translate::TranslateInfoBarDelegate* translate =
240 GetExistingTranslateInfoBarDelegate();
241 EXPECT_FALSE(translate);
243 // Setup page title observer.
244 content::WebContents* web_contents =
245 browser()->tab_strip_model()->GetActiveWebContents();
246 ASSERT_TRUE(web_contents);
247 content::TitleWatcher watcher(web_contents, base::ASCIIToUTF16("PASS"));
248 watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL"));
250 // Visit a test page.
251 ui_test_utils::NavigateToURL(
252 browser(),
253 GetNonSecureURL(kRefreshMetaTagCaseInsensitiveTestPath));
255 // Wait for the page title is changed after the test finished.
256 const base::string16 result = watcher.WaitAndGetTitle();
257 EXPECT_EQ("PASS", base::UTF16ToASCII(result));
259 // Check if there is no Translate infobar.
260 translate = GetExistingTranslateInfoBarDelegate();
261 EXPECT_FALSE(translate);
264 IN_PROC_BROWSER_TEST_F(TranslateBrowserTest, IgnoreRefreshMetaTagAtOnload) {
265 // TODO(port): Test corresponding bubble translate UX: http://crbug.com/383235
266 if (TranslateService::IsTranslateBubbleEnabled())
267 return;
269 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
271 // Check if there is no Translate infobar.
272 translate::TranslateInfoBarDelegate* translate =
273 GetExistingTranslateInfoBarDelegate();
274 EXPECT_FALSE(translate);
276 // Setup page title observer.
277 content::WebContents* web_contents =
278 browser()->tab_strip_model()->GetActiveWebContents();
279 ASSERT_TRUE(web_contents);
280 content::TitleWatcher watcher(web_contents, base::ASCIIToUTF16("PASS"));
281 watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL"));
283 // Visit a test page.
284 ui_test_utils::NavigateToURL(
285 browser(),
286 GetNonSecureURL(kRefreshMetaTagAtOnloadTestPath));
288 // Wait for the page title is changed after the test finished.
289 const base::string16 result = watcher.WaitAndGetTitle();
290 EXPECT_EQ("PASS", base::UTF16ToASCII(result));
292 // Check if there is no Translate infobar.
293 translate = GetExistingTranslateInfoBarDelegate();
294 EXPECT_FALSE(translate);
297 IN_PROC_BROWSER_TEST_F(TranslateBrowserTest, UpdateLocation) {
298 // TODO(port): Test corresponding bubble translate UX: http://crbug.com/383235
299 if (TranslateService::IsTranslateBubbleEnabled())
300 return;
302 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
304 // Check if there is no Translate infobar.
305 translate::TranslateInfoBarDelegate* translate =
306 GetExistingTranslateInfoBarDelegate();
307 EXPECT_FALSE(translate);
309 // Setup page title observer.
310 content::WebContents* web_contents =
311 browser()->tab_strip_model()->GetActiveWebContents();
312 ASSERT_TRUE(web_contents);
313 content::TitleWatcher watcher(web_contents, base::ASCIIToUTF16("PASS"));
314 watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL"));
316 // Visit a test page.
317 ui_test_utils::NavigateToURL(
318 browser(),
319 GetNonSecureURL(kUpdateLocationTestPath));
321 // Wait for the page title is changed after the test finished.
322 const base::string16 result = watcher.WaitAndGetTitle();
323 EXPECT_EQ("PASS", base::UTF16ToASCII(result));
325 // Check if there is no Translate infobar.
326 translate = GetExistingTranslateInfoBarDelegate();
327 EXPECT_FALSE(translate);
330 IN_PROC_BROWSER_TEST_F(TranslateBrowserTest, UpdateLocationAtOnload) {
331 // TODO(port): Test corresponding bubble translate UX: http://crbug.com/383235
332 if (TranslateService::IsTranslateBubbleEnabled())
333 return;
335 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
337 // Check if there is no Translate infobar.
338 translate::TranslateInfoBarDelegate* translate =
339 GetExistingTranslateInfoBarDelegate();
340 EXPECT_FALSE(translate);
342 // Setup page title observer.
343 content::WebContents* web_contents =
344 browser()->tab_strip_model()->GetActiveWebContents();
345 ASSERT_TRUE(web_contents);
346 content::TitleWatcher watcher(web_contents, base::ASCIIToUTF16("PASS"));
347 watcher.AlsoWaitForTitle(base::ASCIIToUTF16("FAIL"));
349 // Visit a test page.
350 ui_test_utils::NavigateToURL(
351 browser(),
352 GetNonSecureURL(kUpdateLocationAtOnloadTestPath));
354 // Wait for the page title is changed after the test finished.
355 const base::string16 result = watcher.WaitAndGetTitle();
356 EXPECT_EQ("PASS", base::UTF16ToASCII(result));
358 // Check if there is no Translate infobar.
359 translate = GetExistingTranslateInfoBarDelegate();
360 EXPECT_FALSE(translate);