Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1406)

Side by Side Diff: chrome/browser/translate/translate_browsertest.cc

Issue 13825006: Translate: browser test to verify whole translate process (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: win build fix 2 Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/chrome_tests.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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/files/file_path.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 base::FilePath::CharType kTranslateRoot[] =
29 FILE_PATH_LITERAL("chrome/test/data/translate");
30 const char kNonSecurePrefix[] = "files/translate/";
31 const char kSecurePrefix[] = "files/";
32 const char kTargetPath[] = "fr_test.html";
33 const char kMainScriptPath[] = "pseudo_main.js";
34 const char kElementMainScriptPath[] = "pseudo_element_main.js";
35
36 }; // namespace
37
38 class TranslateBrowserTest : public InProcessBrowserTest {
39 public:
40 TranslateBrowserTest()
41 : https_server_(net::TestServer::TYPE_HTTPS,
42 SSLOptions(SSLOptions::CERT_OK),
43 base::FilePath(kTranslateRoot)) {}
44
45 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
46 ASSERT_TRUE(https_server_.Start());
47 }
48
49 protected:
50 GURL GetNonSecureURL(const std::string& path) const {
51 std::string prefix(kNonSecurePrefix);
52 return test_server()->GetURL(prefix + path);
53 }
54
55 GURL GetSecureURL(const std::string& path) const {
56 std::string prefix(kSecurePrefix);
57 return https_server_.GetURL(prefix + path);
58 }
59
60 private:
61 net::TestServer https_server_;
62
63 typedef net::TestServer::SSLOptions SSLOptions;
64
65 DISALLOW_COPY_AND_ASSIGN(TranslateBrowserTest);
66 };
67
68 IN_PROC_BROWSER_TEST_F(TranslateBrowserTest, Translate) {
69 ASSERT_TRUE(test_server()->Start());
70
71 content::WebContents* web_contents =
72 browser()->tab_strip_model()->GetActiveWebContents();
73 ASSERT_TRUE(web_contents);
74
75 net::TestURLFetcherFactory factory;
76
77 // Setup infobar observer.
78 InfoBarService* infobar_service =
79 InfoBarService::FromWebContents(web_contents);
80 ASSERT_TRUE(infobar_service);
81 ASSERT_EQ(0U, infobar_service->infobar_count());
82 content::WindowedNotificationObserver infobar(
83 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_ADDED,
84 content::NotificationService::AllSources());
85
86 // Setup page title observer.
87 content::TitleWatcher watcher(web_contents, ASCIIToUTF16("PASS"));
88 watcher.AlsoWaitForTitle(ASCIIToUTF16("FAIL"));
89
90 // Visit non-secure page which is going to be translated.
91 ui_test_utils::NavigateToURL(browser(), GetNonSecureURL(kTargetPath));
92
93 // Wait for Chrome Translate infobar.
94 infobar.Wait();
95
96 // Perform Chrome Translate.
97 InfoBarDelegate* delegate = infobar_service->infobar_at(0);
98 ASSERT_TRUE(delegate);
99 TranslateInfoBarDelegate* translate = delegate->AsTranslateInfoBarDelegate();
100 ASSERT_TRUE(translate);
101 translate->Translate();
102
103 // Hook URLFetcher for element.js.
104 GURL script1_url = GetSecureURL(kMainScriptPath);
105 GURL script2_url = GetSecureURL(kElementMainScriptPath);
106 std::string element_js = "main_script_url = '" + script1_url.spec() + "';\n";
107 element_js += "element_main_script_url = '" + script2_url.spec() + "';\n";
108 element_js +=
109 "google = { 'translate' : { 'TranslateService' : function() { return {\n"
110 " isAvailable: function() {\n"
111 " var script = document.createElement('script');\n"
112 " script.src = main_script_url;\n"
113 " document.getElementsByTagName('head')[0].appendChild(script);\n"
114 " return true;\n"
115 " },\n"
116 " translatePage: function(sl, tl, cb) {\n"
117 " cb(1, true);\n"
118 " }\n"
119 "} } } };\n"
120 "cr.googleTranslate.onTranslateElementLoad();\n";
121 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
122 ASSERT_TRUE(fetcher);
123 net::URLRequestStatus status;
124 status.set_status(net::URLRequestStatus::SUCCESS);
125 fetcher->set_status(status);
126 fetcher->set_url(fetcher->GetOriginalURL());
127 fetcher->set_response_code(net::HTTP_OK);
128 fetcher->SetResponseString(element_js);
129 fetcher->delegate()->OnURLFetchComplete(fetcher);
130
131 // Wait for the page title is changed after the test finished.
132 const string16 result = watcher.WaitAndGetTitle();
133 EXPECT_TRUE(EqualsASCII(result, "PASS"));
134 }
135
136 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_BROWSERTEST_H_
OLDNEW
« no previous file with comments | « no previous file | chrome/chrome_tests.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698