OLD | NEW |
---|---|
(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 #ifndef CHROME_BROWSER_TRANSLATE_TRANSLATE_BROWSERTEST_H_ | |
6 #define CHROME_BROWSER_TRANSLATE_TRANSLATE_BROWSERTEST_H_ | |
7 | |
8 #include "base/command_line.h" | |
9 #include "base/string_util.h" | |
10 #include "base/utf_string_conversions.h" | |
11 #include "chrome/browser/infobars/infobar_service.h" | |
12 #include "chrome/browser/translate/translate_infobar_delegate.h" | |
13 #include "chrome/browser/ui/browser.h" | |
14 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
15 #include "chrome/common/chrome_notification_types.h" | |
16 #include "chrome/common/chrome_switches.h" | |
17 #include "chrome/test/base/in_process_browser_test.h" | |
18 #include "chrome/test/base/ui_test_utils.h" | |
19 #include "content/public/browser/notification_service.h" | |
20 #include "content/public/test/browser_test_utils.h" | |
21 #include "net/http/http_status_code.h" | |
22 #include "net/test/test_server.h" | |
23 #include "net/url_request/test_url_fetcher_factory.h" | |
24 #include "net/url_request/url_fetcher_delegate.h" | |
25 | |
26 namespace { | |
27 | |
28 const char kTranslateRoot[] = "chrome/test/data/translate"; | |
29 const char kNonSecurePrefix[] = "files/translate/"; | |
30 const char kSecurePrefix[] = "files/"; | |
31 const char kTargetPath[] = "fr_test.html"; | |
32 const char kMainScriptPath[] = "pseudo_main.js"; | |
33 const char kElementMainScriptPath[] = "pseudo_element_main.js"; | |
34 | |
35 }; // namespace | |
36 | |
37 class TranslateBrowserTest : public InProcessBrowserTest { | |
38 public: | |
39 TranslateBrowserTest() | |
40 : https_server_(net::TestServer::TYPE_HTTPS, | |
41 SSLOptions(SSLOptions::CERT_OK), | |
42 base::FilePath(kTranslateRoot)) {} | |
43 | |
44 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { | |
45 ASSERT_TRUE(https_server_.Start()); | |
MAD
2013/04/25 14:01:17
Why is this in SetUpCommandLine? Why not in SetUpI
Takashi Toyoshima
2013/04/25 14:44:07
In this change, it can be placed in SetUpInProcess
| |
46 } | |
47 | |
48 protected: | |
49 GURL GetNonSecureURL(const std::string& path) const { | |
50 std::string prefix(kNonSecurePrefix); | |
51 return test_server()->GetURL(prefix + path); | |
52 } | |
53 | |
54 GURL GetSecureURL(const std::string& path) const { | |
55 std::string prefix(kSecurePrefix); | |
56 return https_server_.GetURL(prefix + path); | |
57 } | |
58 | |
59 private: | |
60 net::TestServer https_server_; | |
61 | |
62 typedef net::TestServer::SSLOptions SSLOptions; | |
63 | |
64 DISALLOW_COPY_AND_ASSIGN(TranslateBrowserTest); | |
65 }; | |
66 | |
67 IN_PROC_BROWSER_TEST_F(TranslateBrowserTest, Translate) { | |
68 ASSERT_TRUE(test_server()->Start()); | |
69 | |
70 content::WebContents* web_contents = | |
71 browser()->tab_strip_model()->GetActiveWebContents(); | |
72 ASSERT_TRUE(web_contents); | |
73 | |
74 net::TestURLFetcherFactory factory; | |
75 | |
76 // Setup infobar observer. | |
77 InfoBarService* infobar_service = | |
78 InfoBarService::FromWebContents(web_contents); | |
79 ASSERT_TRUE(infobar_service); | |
80 ASSERT_EQ(0U, infobar_service->GetInfoBarCount()); | |
81 content::WindowedNotificationObserver infobar( | |
82 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED, | |
83 content::NotificationService::AllSources()); | |
84 | |
85 // Setup page title observer. | |
86 content::TitleWatcher watcher(web_contents, ASCIIToUTF16("PASS")); | |
87 watcher.AlsoWaitForTitle(ASCIIToUTF16("FAIL")); | |
88 | |
89 // Visit non-secure page which is going to be translated. | |
90 ui_test_utils::NavigateToURL(browser(), GetNonSecureURL(kTargetPath)); | |
91 | |
92 // Wait for Chrome Translate infobar. | |
93 infobar.Wait(); | |
94 | |
95 // Perform Chrome Translate. | |
96 InfoBarDelegate* delegate = infobar_service->GetInfoBarDelegateAt(0); | |
97 ASSERT_TRUE(delegate); | |
98 TranslateInfoBarDelegate* translate = delegate->AsTranslateInfoBarDelegate(); | |
99 ASSERT_TRUE(translate); | |
100 translate->Translate(); | |
101 | |
102 // Hook URLFetcher for element.js. | |
103 GURL script1_url = GetSecureURL(kMainScriptPath); | |
104 GURL script2_url = GetSecureURL(kElementMainScriptPath); | |
105 std::string element_js = "main_script_url = '" + script1_url.spec() + "';\n"; | |
106 element_js += "element_main_script_url = '" + script2_url.spec() + "';\n"; | |
107 element_js += | |
108 "google = { 'translate' : { 'TranslateService' : function() { return {\n" | |
109 " isAvailable: function() {\n" | |
110 " var script = document.createElement('script');\n" | |
111 " script.src = main_script_url;\n" | |
112 " document.getElementsByTagName('head')[0].appendChild(script);\n" | |
113 " return true;\n" | |
114 " },\n" | |
115 " translatePage: function(sl, tl, cb) {\n" | |
116 " cb(1, true);\n" | |
117 " }\n" | |
118 "} } } };\n" | |
119 "cr.googleTranslate.onTranslateElementLoad();\n"; | |
120 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | |
121 ASSERT_TRUE(fetcher); | |
122 net::URLRequestStatus status; | |
123 status.set_status(net::URLRequestStatus::SUCCESS); | |
124 fetcher->set_status(status); | |
125 fetcher->set_url(fetcher->GetOriginalURL()); | |
126 fetcher->set_response_code(net::HTTP_OK); | |
127 fetcher->SetResponseString(element_js); | |
128 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
129 | |
130 // Wait for the page title is changed after the test finished. | |
131 const string16 result = watcher.WaitAndGetTitle(); | |
132 EXPECT_TRUE(EqualsASCII(result, "PASS")); | |
133 } | |
134 | |
135 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_BROWSERTEST_H_ | |
OLD | NEW |