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

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

Issue 186063007: Move TranslateManager unit tests out of the browser_tests target. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: translatetests: comment Created 6 years, 9 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <algorithm> 5 #include <algorithm>
6 #include <set> 6 #include <set>
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/command_line.h"
10 #include "base/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
11 #include "base/prefs/pref_change_registrar.h" 10 #include "base/prefs/pref_change_registrar.h"
12 #include "base/prefs/pref_service.h" 11 #include "base/prefs/pref_service.h"
13 #include "base/strings/stringprintf.h" 12 #include "base/strings/stringprintf.h"
14 #include "chrome/app/chrome_command_ids.h" 13 #include "chrome/app/chrome_command_ids.h"
15 #include "chrome/browser/chrome_notification_types.h" 14 #include "chrome/browser/chrome_notification_types.h"
16 #include "chrome/browser/extensions/test_extension_system.h" 15 #include "chrome/browser/extensions/test_extension_system.h"
17 #include "chrome/browser/infobars/infobar.h" 16 #include "chrome/browser/infobars/infobar.h"
18 #include "chrome/browser/infobars/infobar_service.h" 17 #include "chrome/browser/infobars/infobar_service.h"
19 #include "chrome/browser/prefs/session_startup_pref.h"
20 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h" 18 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h"
21 #include "chrome/browser/translate/translate_infobar_delegate.h" 19 #include "chrome/browser/translate/translate_infobar_delegate.h"
22 #include "chrome/browser/translate/translate_manager.h" 20 #include "chrome/browser/translate/translate_manager.h"
23 #include "chrome/browser/translate/translate_service.h" 21 #include "chrome/browser/translate/translate_service.h"
24 #include "chrome/browser/translate/translate_tab_helper.h" 22 #include "chrome/browser/translate/translate_tab_helper.h"
25 #include "chrome/browser/ui/browser.h"
26 #include "chrome/browser/ui/tabs/tab_strip_model.h"
27 #include "chrome/browser/ui/translate/translate_bubble_factory.h" 23 #include "chrome/browser/ui/translate/translate_bubble_factory.h"
28 #include "chrome/browser/ui/translate/translate_bubble_model.h" 24 #include "chrome/browser/ui/translate/translate_bubble_model.h"
29 #include "chrome/browser/ui/translate/translate_bubble_model_impl.h" 25 #include "chrome/browser/ui/translate/translate_bubble_model_impl.h"
30 #include "chrome/common/chrome_switches.h" 26 #include "chrome/common/chrome_switches.h"
31 #include "chrome/common/pref_names.h" 27 #include "chrome/common/pref_names.h"
32 #include "chrome/common/render_messages.h" 28 #include "chrome/common/render_messages.h"
33 #include "chrome/common/url_constants.h" 29 #include "chrome/common/url_constants.h"
34 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 30 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
35 #include "chrome/test/base/in_process_browser_test.h"
36 #include "chrome/test/base/testing_browser_process.h" 31 #include "chrome/test/base/testing_browser_process.h"
37 #include "chrome/test/base/testing_profile.h" 32 #include "chrome/test/base/testing_profile.h"
38 #include "chrome/test/base/ui_test_utils.h"
39 #include "components/translate/core/browser/translate_accept_languages.h" 33 #include "components/translate/core/browser/translate_accept_languages.h"
40 #include "components/translate/core/browser/translate_download_manager.h" 34 #include "components/translate/core/browser/translate_download_manager.h"
41 #include "components/translate/core/browser/translate_language_list.h" 35 #include "components/translate/core/browser/translate_language_list.h"
42 #include "components/translate/core/browser/translate_prefs.h" 36 #include "components/translate/core/browser/translate_prefs.h"
43 #include "components/translate/core/browser/translate_script.h" 37 #include "components/translate/core/browser/translate_script.h"
44 #include "components/translate/core/common/language_detection_details.h" 38 #include "components/translate/core/common/language_detection_details.h"
45 #include "components/translate/core/common/translate_pref_names.h" 39 #include "components/translate/core/common/translate_pref_names.h"
46 #include "content/public/browser/navigation_details.h" 40 #include "content/public/browser/navigation_details.h"
47 #include "content/public/browser/navigation_entry.h" 41 #include "content/public/browser/navigation_entry.h"
48 #include "content/public/browser/notification_details.h" 42 #include "content/public/browser/notification_details.h"
49 #include "content/public/browser/notification_registrar.h" 43 #include "content/public/browser/notification_registrar.h"
50 #include "content/public/browser/web_contents.h" 44 #include "content/public/browser/web_contents.h"
45 #include "content/public/common/url_constants.h"
51 #include "content/public/test/mock_render_process_host.h" 46 #include "content/public/test/mock_render_process_host.h"
52 #include "content/public/test/test_renderer_host.h" 47 #include "content/public/test/test_renderer_host.h"
53 #include "net/url_request/test_url_fetcher_factory.h" 48 #include "net/url_request/test_url_fetcher_factory.h"
54 #include "net/url_request/url_fetcher_delegate.h" 49 #include "net/url_request/url_fetcher_delegate.h"
55 #include "testing/gmock/include/gmock/gmock.h" 50 #include "testing/gmock/include/gmock/gmock.h"
56 #include "third_party/WebKit/public/web/WebContextMenuData.h" 51 #include "third_party/WebKit/public/web/WebContextMenuData.h"
57 52 #include "url/gurl.h"
58 53
59 // An observer that keeps track of whether a navigation entry was committed. 54 // An observer that keeps track of whether a navigation entry was committed.
60 class NavEntryCommittedObserver : public content::NotificationObserver { 55 class NavEntryCommittedObserver : public content::NotificationObserver {
61 public: 56 public:
62 explicit NavEntryCommittedObserver(content::WebContents* web_contents) { 57 explicit NavEntryCommittedObserver(content::WebContents* web_contents) {
63 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, 58 registrar_.Add(this,
59 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
64 content::Source<content::NavigationController>( 60 content::Source<content::NavigationController>(
65 &web_contents->GetController())); 61 &web_contents->GetController()));
66 } 62 }
67 63
68 virtual void Observe(int type, 64 virtual void Observe(int type,
69 const content::NotificationSource& source, 65 const content::NotificationSource& source,
70 const content::NotificationDetails& details) OVERRIDE { 66 const content::NotificationDetails& details) OVERRIDE {
71 DCHECK(type == content::NOTIFICATION_NAV_ENTRY_COMMITTED); 67 DCHECK(type == content::NOTIFICATION_NAV_ENTRY_COMMITTED);
72 details_ = 68 details_ =
73 *(content::Details<content::LoadCommittedDetails>(details).ptr()); 69 *(content::Details<content::LoadCommittedDetails>(details).ptr());
74 } 70 }
75 71
76 const content::LoadCommittedDetails& load_committed_details() const { 72 const content::LoadCommittedDetails& load_committed_details() const {
77 return details_; 73 return details_;
78 } 74 }
79 75
80 private: 76 private:
81 content::LoadCommittedDetails details_; 77 content::LoadCommittedDetails details_;
82 content::NotificationRegistrar registrar_; 78 content::NotificationRegistrar registrar_;
83 79
84 DISALLOW_COPY_AND_ASSIGN(NavEntryCommittedObserver); 80 DISALLOW_COPY_AND_ASSIGN(NavEntryCommittedObserver);
85 }; 81 };
86 82
87 class TranslateManagerBrowserTest : public ChromeRenderViewHostTestHarness, 83 class TranslateManagerRenderViewHostTest
88 public content::NotificationObserver { 84 : public ChromeRenderViewHostTestHarness,
85 public content::NotificationObserver {
89 public: 86 public:
90 TranslateManagerBrowserTest() 87 TranslateManagerRenderViewHostTest()
91 : pref_callback_( 88 : pref_callback_(
92 base::Bind(&TranslateManagerBrowserTest::OnPreferenceChanged, 89 base::Bind(&TranslateManagerRenderViewHostTest::OnPreferenceChanged,
93 base::Unretained(this))) { 90 base::Unretained(this))) {}
94 }
95 91
96 // Simulates navigating to a page and getting the page contents and language 92 // Simulates navigating to a page and getting the page contents and language
97 // for that navigation. 93 // for that navigation.
98 void SimulateNavigation(const GURL& url, 94 void SimulateNavigation(const GURL& url,
99 const std::string& lang, 95 const std::string& lang,
100 bool page_translatable) { 96 bool page_translatable) {
101 NavigateAndCommit(url); 97 NavigateAndCommit(url);
102 SimulateOnTranslateLanguageDetermined(lang, page_translatable); 98 SimulateOnTranslateLanguageDetermined(lang, page_translatable);
103 } 99 }
104 100
(...skipping 12 matching lines...) Expand all
117 const std::string& target_lang, 113 const std::string& target_lang,
118 TranslateErrors::Type error) { 114 TranslateErrors::Type error) {
119 content::RenderViewHostTester::TestOnMessageReceived( 115 content::RenderViewHostTester::TestOnMessageReceived(
120 rvh(), 116 rvh(),
121 ChromeViewHostMsg_PageTranslated( 117 ChromeViewHostMsg_PageTranslated(
122 routing_id, 0, source_lang, target_lang, error)); 118 routing_id, 0, source_lang, target_lang, error));
123 } 119 }
124 120
125 void SimulateOnPageTranslated(const std::string& source_lang, 121 void SimulateOnPageTranslated(const std::string& source_lang,
126 const std::string& target_lang) { 122 const std::string& target_lang) {
127 SimulateOnPageTranslated(0, source_lang, target_lang, 123 SimulateOnPageTranslated(
128 TranslateErrors::NONE); 124 0, source_lang, target_lang, TranslateErrors::NONE);
129 } 125 }
130 126
131 bool GetTranslateMessage(int* page_id, 127 bool GetTranslateMessage(int* page_id,
132 std::string* original_lang, 128 std::string* original_lang,
133 std::string* target_lang) { 129 std::string* target_lang) {
134 const IPC::Message* message = 130 const IPC::Message* message = process()->sink().GetFirstMessageMatching(
135 process()->sink().GetFirstMessageMatching( 131 ChromeViewMsg_TranslatePage::ID);
136 ChromeViewMsg_TranslatePage::ID);
137 if (!message) 132 if (!message)
138 return false; 133 return false;
139 Tuple4<int, std::string, std::string, std::string> translate_param; 134 Tuple4<int, std::string, std::string, std::string> translate_param;
140 ChromeViewMsg_TranslatePage::Read(message, &translate_param); 135 ChromeViewMsg_TranslatePage::Read(message, &translate_param);
141 if (page_id) 136 if (page_id)
142 *page_id = translate_param.a; 137 *page_id = translate_param.a;
143 // Ignore translate_param.b which is the script injected in the page. 138 // Ignore translate_param.b which is the script injected in the page.
144 if (original_lang) 139 if (original_lang)
145 *original_lang = translate_param.c; 140 *original_lang = translate_param.c;
146 if (target_lang) 141 if (target_lang)
147 *target_lang = translate_param.d; 142 *target_lang = translate_param.d;
148 return true; 143 return true;
149 } 144 }
150 145
151 InfoBarService* infobar_service() { 146 InfoBarService* infobar_service() {
152 return InfoBarService::FromWebContents(web_contents()); 147 return InfoBarService::FromWebContents(web_contents());
153 } 148 }
154 149
155 // Returns the translate infobar if there is 1 infobar and it is a translate 150 // Returns the translate infobar if there is 1 infobar and it is a translate
156 // infobar. 151 // infobar.
157 TranslateInfoBarDelegate* GetTranslateInfoBar() { 152 TranslateInfoBarDelegate* GetTranslateInfoBar() {
158 return (infobar_service()->infobar_count() == 1) ? 153 return (infobar_service()->infobar_count() == 1)
159 infobar_service()->infobar_at(0)->delegate()-> 154 ? infobar_service()
160 AsTranslateInfoBarDelegate() : NULL; 155 ->infobar_at(0)
156 ->delegate()
157 ->AsTranslateInfoBarDelegate()
158 : NULL;
161 } 159 }
162 160
163 // If there is 1 infobar and it is a translate infobar, closes it and returns 161 // If there is 1 infobar and it is a translate infobar, closes it and returns
164 // true. Returns false otherwise. 162 // true. Returns false otherwise.
165 bool CloseTranslateInfoBar() { 163 bool CloseTranslateInfoBar() {
166 InfoBarDelegate* infobar = GetTranslateInfoBar(); 164 InfoBarDelegate* infobar = GetTranslateInfoBar();
167 if (!infobar) 165 if (!infobar)
168 return false; 166 return false;
169 infobar->InfoBarDismissed(); // Simulates closing the infobar. 167 infobar->InfoBarDismissed(); // Simulates closing the infobar.
170 infobar_service()->RemoveInfoBar(infobar_service()->infobar_at(0)); 168 infobar_service()->RemoveInfoBar(infobar_service()->infobar_at(0));
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 const content::NotificationDetails& details) { 216 const content::NotificationDetails& details) {
219 DCHECK_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, type); 217 DCHECK_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, type);
220 removed_infobars_.insert( 218 removed_infobars_.insert(
221 content::Details<InfoBar::RemovedDetails>(details)->first->delegate()); 219 content::Details<InfoBar::RemovedDetails>(details)->first->delegate());
222 } 220 }
223 221
224 MOCK_METHOD1(OnPreferenceChanged, void(const std::string&)); 222 MOCK_METHOD1(OnPreferenceChanged, void(const std::string&));
225 223
226 protected: 224 protected:
227 virtual void SetUp() { 225 virtual void SetUp() {
228 // This test is a unit test but runs in the browser_tests suite. Therefore 226 TranslateService::InitializeForTesting();
229 // it needs to manage its own TestingBrowserProcess.
230 // TODO(jamescook): Figure out how to move this suite back to unit_tests.
231 // Right now it fails to get the translate infobar if you run it there.
232 TestingBrowserProcess::CreateInstance();
233
234 TranslateService::Initialize();
235 TranslateService::SetUseInfobar(true); 227 TranslateService::SetUseInfobar(true);
236 228
237 // Clears the translate script so it is fetched everytime and sets the 229 // Clears the translate script so it is fetched everytime and sets the
238 // expiration delay to a large value by default (in case it was zeroed in a 230 // expiration delay to a large value by default (in case it was zeroed in a
239 // previous test). 231 // previous test).
240 TranslateDownloadManager* download_manager = 232 TranslateDownloadManager* download_manager =
241 TranslateDownloadManager::GetInstance(); 233 TranslateDownloadManager::GetInstance();
242 download_manager->ClearTranslateScriptForTesting(); 234 download_manager->ClearTranslateScriptForTesting();
243 download_manager->SetTranslateScriptExpirationDelay(60 * 60 * 1000); 235 download_manager->SetTranslateScriptExpirationDelay(60 * 60 * 1000);
244 236
245 ChromeRenderViewHostTestHarness::SetUp(); 237 ChromeRenderViewHostTestHarness::SetUp();
246 InfoBarService::CreateForWebContents(web_contents()); 238 InfoBarService::CreateForWebContents(web_contents());
247 TranslateTabHelper::CreateForWebContents(web_contents()); 239 TranslateTabHelper::CreateForWebContents(web_contents());
248 TranslateManager* manager = 240 TranslateManager* manager =
249 TranslateTabHelper::GetManagerFromWebContents(web_contents()); 241 TranslateTabHelper::GetManagerFromWebContents(web_contents());
250 manager->set_translate_max_reload_attemps(0); 242 manager->set_translate_max_reload_attemps(0);
251 243
252 notification_registrar_.Add(this, 244 notification_registrar_.Add(
245 this,
253 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, 246 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
254 content::Source<InfoBarService>(infobar_service())); 247 content::Source<InfoBarService>(infobar_service()));
255 } 248 }
256 249
257 virtual void TearDown() { 250 virtual void TearDown() {
258 process()->sink().ClearMessages(); 251 process()->sink().ClearMessages();
259 252
260 notification_registrar_.Remove(this, 253 notification_registrar_.Remove(
254 this,
261 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, 255 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
262 content::Source<InfoBarService>(infobar_service())); 256 content::Source<InfoBarService>(infobar_service()));
263 257
264 ChromeRenderViewHostTestHarness::TearDown(); 258 ChromeRenderViewHostTestHarness::TearDown();
265 TestingBrowserProcess::DeleteInstance(); 259 TranslateService::ShutdownForTesting();
266 } 260 }
267 261
268 void SetApplicationLocale(const std::string& locale) { 262 void SetApplicationLocale(const std::string& locale) {
269 g_browser_process->SetApplicationLocale(locale); 263 g_browser_process->SetApplicationLocale(locale);
270 TranslateDownloadManager::GetInstance()->set_application_locale( 264 TranslateDownloadManager::GetInstance()->set_application_locale(
271 g_browser_process->GetApplicationLocale()); 265 g_browser_process->GetApplicationLocale());
272 } 266 }
273 267
274 void SimulateTranslateScriptURLFetch(bool success) { 268 void SimulateTranslateScriptURLFetch(bool success) {
275 net::TestURLFetcher* fetcher = 269 net::TestURLFetcher* fetcher =
276 url_fetcher_factory_.GetFetcherByID(TranslateScript::kFetcherId); 270 url_fetcher_factory_.GetFetcherByID(TranslateScript::kFetcherId);
277 ASSERT_TRUE(fetcher); 271 ASSERT_TRUE(fetcher);
278 net::URLRequestStatus status; 272 net::URLRequestStatus status;
279 status.set_status(success ? net::URLRequestStatus::SUCCESS : 273 status.set_status(success ? net::URLRequestStatus::SUCCESS
280 net::URLRequestStatus::FAILED); 274 : net::URLRequestStatus::FAILED);
281 fetcher->set_url(fetcher->GetOriginalURL()); 275 fetcher->set_url(fetcher->GetOriginalURL());
282 fetcher->set_status(status); 276 fetcher->set_status(status);
283 fetcher->set_response_code(success ? 200 : 500); 277 fetcher->set_response_code(success ? 200 : 500);
284 fetcher->delegate()->OnURLFetchComplete(fetcher); 278 fetcher->delegate()->OnURLFetchComplete(fetcher);
285 } 279 }
286 280
287 void SimulateSupportedLanguagesURLFetch( 281 void SimulateSupportedLanguagesURLFetch(
288 bool success, 282 bool success,
289 const std::vector<std::string>& languages, 283 const std::vector<std::string>& languages,
290 bool use_alpha_languages, 284 bool use_alpha_languages,
291 const std::vector<std::string>& alpha_languages) { 285 const std::vector<std::string>& alpha_languages) {
292 net::URLRequestStatus status; 286 net::URLRequestStatus status;
293 status.set_status(success ? net::URLRequestStatus::SUCCESS : 287 status.set_status(success ? net::URLRequestStatus::SUCCESS
294 net::URLRequestStatus::FAILED); 288 : net::URLRequestStatus::FAILED);
295 289
296 std::string data; 290 std::string data;
297 if (success) { 291 if (success) {
298 data = base::StringPrintf( 292 data =
299 "%s{\"sl\": {\"bla\": \"bla\"}, \"%s\": {", 293 base::StringPrintf("%s{\"sl\": {\"bla\": \"bla\"}, \"%s\": {",
300 TranslateLanguageList::kLanguageListCallbackName, 294 TranslateLanguageList::kLanguageListCallbackName,
301 TranslateLanguageList::kTargetLanguagesKey); 295 TranslateLanguageList::kTargetLanguagesKey);
302 const char* comma = ""; 296 const char* comma = "";
303 for (size_t i = 0; i < languages.size(); ++i) { 297 for (size_t i = 0; i < languages.size(); ++i) {
304 data += base::StringPrintf( 298 data += base::StringPrintf(
305 "%s\"%s\": \"UnusedFullName\"", comma, languages[i].c_str()); 299 "%s\"%s\": \"UnusedFullName\"", comma, languages[i].c_str());
306 if (i == 0) 300 if (i == 0)
307 comma = ","; 301 comma = ",";
308 } 302 }
309 303
310 if (use_alpha_languages) { 304 if (use_alpha_languages) {
311 data += base::StringPrintf("},\"%s\": {", 305 data += base::StringPrintf("},\"%s\": {",
312 TranslateLanguageList::kAlphaLanguagesKey); 306 TranslateLanguageList::kAlphaLanguagesKey);
313 comma = ""; 307 comma = "";
314 for (size_t i = 0; i < alpha_languages.size(); ++i) { 308 for (size_t i = 0; i < alpha_languages.size(); ++i) {
315 data += base::StringPrintf("%s\"%s\": 1", comma, 309 data += base::StringPrintf(
316 alpha_languages[i].c_str()); 310 "%s\"%s\": 1", comma, alpha_languages[i].c_str());
317 if (i == 0) 311 if (i == 0)
318 comma = ","; 312 comma = ",";
319 } 313 }
320 } 314 }
321 315
322 data += "}})"; 316 data += "}})";
323 } 317 }
324 net::TestURLFetcher* fetcher = 318 net::TestURLFetcher* fetcher =
325 url_fetcher_factory_.GetFetcherByID(TranslateLanguageList::kFetcherId); 319 url_fetcher_factory_.GetFetcherByID(TranslateLanguageList::kFetcherId);
326 ASSERT_TRUE(fetcher != NULL); 320 ASSERT_TRUE(fetcher != NULL);
(...skipping 11 matching lines...) Expand all
338 PrefChangeRegistrar::NamedChangeCallback pref_callback_; 332 PrefChangeRegistrar::NamedChangeCallback pref_callback_;
339 333
340 private: 334 private:
341 content::NotificationRegistrar notification_registrar_; 335 content::NotificationRegistrar notification_registrar_;
342 net::TestURLFetcherFactory url_fetcher_factory_; 336 net::TestURLFetcherFactory url_fetcher_factory_;
343 337
344 // The infobars that have been removed. 338 // The infobars that have been removed.
345 // WARNING: the pointers point to deleted objects, use only for comparison. 339 // WARNING: the pointers point to deleted objects, use only for comparison.
346 std::set<InfoBarDelegate*> removed_infobars_; 340 std::set<InfoBarDelegate*> removed_infobars_;
347 341
348 DISALLOW_COPY_AND_ASSIGN(TranslateManagerBrowserTest); 342 DISALLOW_COPY_AND_ASSIGN(TranslateManagerRenderViewHostTest);
349 }; 343 };
350 344
351 class MockTranslateBubbleFactory : public TranslateBubbleFactory { 345 class MockTranslateBubbleFactory : public TranslateBubbleFactory {
352 public: 346 public:
353 MockTranslateBubbleFactory() { 347 MockTranslateBubbleFactory() {}
354 }
355 348
356 virtual void ShowImplementation(BrowserWindow* window, 349 virtual void ShowImplementation(BrowserWindow* window,
357 content::WebContents* web_contents, 350 content::WebContents* web_contents,
358 TranslateTabHelper::TranslateStep step, 351 TranslateTabHelper::TranslateStep step,
359 TranslateErrors::Type error_type) OVERRIDE { 352 TranslateErrors::Type error_type) OVERRIDE {
360 if (model_) { 353 if (model_) {
361 model_->SetViewState( 354 model_->SetViewState(
362 TranslateBubbleModelImpl::TranslateStepToViewState(step)); 355 TranslateBubbleModelImpl::TranslateStepToViewState(step));
363 return; 356 return;
364 } 357 }
365 358
366 TranslateTabHelper* translate_tab_helper = 359 TranslateTabHelper* translate_tab_helper =
367 TranslateTabHelper::FromWebContents(web_contents); 360 TranslateTabHelper::FromWebContents(web_contents);
368 std::string source_language = 361 std::string source_language =
369 translate_tab_helper->GetLanguageState().original_language(); 362 translate_tab_helper->GetLanguageState().original_language();
370 std::string target_language = TranslateDownloadManager::GetLanguageCode( 363 std::string target_language = TranslateDownloadManager::GetLanguageCode(
371 g_browser_process->GetApplicationLocale()); 364 g_browser_process->GetApplicationLocale());
372 scoped_ptr<TranslateUIDelegate> ui_delegate( 365 scoped_ptr<TranslateUIDelegate> ui_delegate(new TranslateUIDelegate(
373 new TranslateUIDelegate(web_contents, 366 web_contents, source_language, target_language));
374 source_language,
375 target_language));
376 model_.reset(new TranslateBubbleModelImpl(step, ui_delegate.Pass())); 367 model_.reset(new TranslateBubbleModelImpl(step, ui_delegate.Pass()));
377 } 368 }
378 369
379 TranslateBubbleModel* model() { return model_.get(); } 370 TranslateBubbleModel* model() { return model_.get(); }
380 371
381 private: 372 private:
382 scoped_ptr<TranslateBubbleModel> model_; 373 scoped_ptr<TranslateBubbleModel> model_;
383 374
384 DISALLOW_COPY_AND_ASSIGN(MockTranslateBubbleFactory); 375 DISALLOW_COPY_AND_ASSIGN(MockTranslateBubbleFactory);
385 }; 376 };
(...skipping 19 matching lines...) Expand all
405 params.writing_direction_right_to_left = 0; 396 params.writing_direction_right_to_left = 0;
406 #endif // OS_MACOSX 397 #endif // OS_MACOSX
407 params.edit_flags = blink::WebContextMenuData::CanTranslate; 398 params.edit_flags = blink::WebContextMenuData::CanTranslate;
408 return new TestRenderViewContextMenu(web_contents->GetMainFrame(), params); 399 return new TestRenderViewContextMenu(web_contents->GetMainFrame(), params);
409 } 400 }
410 401
411 bool IsItemPresent(int id) { 402 bool IsItemPresent(int id) {
412 return menu_model_.GetIndexOfCommandId(id) != -1; 403 return menu_model_.GetIndexOfCommandId(id) != -1;
413 } 404 }
414 405
415 virtual void PlatformInit() OVERRIDE { } 406 virtual void PlatformInit() OVERRIDE {}
416 virtual void PlatformCancel() OVERRIDE { } 407 virtual void PlatformCancel() OVERRIDE {}
417 virtual bool GetAcceleratorForCommandId( 408 virtual bool GetAcceleratorForCommandId(int command_id,
418 int command_id, 409 ui::Accelerator* accelerator)
419 ui::Accelerator* accelerator) OVERRIDE { return false; } 410 OVERRIDE {
411 return false;
412 }
420 413
421 private: 414 private:
422 TestRenderViewContextMenu(content::RenderFrameHost* render_frame_host, 415 TestRenderViewContextMenu(content::RenderFrameHost* render_frame_host,
423 const content::ContextMenuParams& params) 416 const content::ContextMenuParams& params)
424 : RenderViewContextMenu(render_frame_host, params) { 417 : RenderViewContextMenu(render_frame_host, params) {}
425 }
426 418
427 DISALLOW_COPY_AND_ASSIGN(TestRenderViewContextMenu); 419 DISALLOW_COPY_AND_ASSIGN(TestRenderViewContextMenu);
428 }; 420 };
429 421
430 } // namespace 422 } // namespace
431 423
432 TEST_F(TranslateManagerBrowserTest, NormalTranslate) { 424 TEST_F(TranslateManagerRenderViewHostTest, NormalTranslate) {
433 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); 425 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
434 426
435 // We should have an infobar. 427 // We should have an infobar.
436 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); 428 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
437 ASSERT_TRUE(infobar != NULL); 429 ASSERT_TRUE(infobar != NULL);
438 EXPECT_EQ(TranslateTabHelper::BEFORE_TRANSLATE, infobar->translate_step()); 430 EXPECT_EQ(TranslateTabHelper::BEFORE_TRANSLATE, infobar->translate_step());
439 431
440 // Simulate clicking translate. 432 // Simulate clicking translate.
441 process()->sink().ClearMessages(); 433 process()->sink().ClearMessages();
442 infobar->Translate(); 434 infobar->Translate();
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
499 EXPECT_EQ(TranslateTabHelper::BEFORE_TRANSLATE, infobar->translate_step()); 491 EXPECT_EQ(TranslateTabHelper::BEFORE_TRANSLATE, infobar->translate_step());
500 infobar->UpdateTargetLanguageIndex(1); 492 infobar->UpdateTargetLanguageIndex(1);
501 infobar->ToggleAlwaysTranslate(); 493 infobar->ToggleAlwaysTranslate();
502 ReloadAndWait(true); 494 ReloadAndWait(true);
503 infobar = GetTranslateInfoBar(); 495 infobar = GetTranslateInfoBar();
504 ASSERT_TRUE(infobar != NULL); 496 ASSERT_TRUE(infobar != NULL);
505 EXPECT_EQ(TranslateTabHelper::TRANSLATING, infobar->translate_step()); 497 EXPECT_EQ(TranslateTabHelper::TRANSLATING, infobar->translate_step());
506 EXPECT_EQ(new_target_lang, infobar->target_language_code()); 498 EXPECT_EQ(new_target_lang, infobar->target_language_code());
507 } 499 }
508 500
509 TEST_F(TranslateManagerBrowserTest, TranslateScriptNotAvailable) { 501 TEST_F(TranslateManagerRenderViewHostTest, TranslateScriptNotAvailable) {
510 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); 502 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
511 503
512 // We should have an infobar. 504 // We should have an infobar.
513 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); 505 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
514 ASSERT_TRUE(infobar != NULL); 506 ASSERT_TRUE(infobar != NULL);
515 EXPECT_EQ(TranslateTabHelper::BEFORE_TRANSLATE, infobar->translate_step()); 507 EXPECT_EQ(TranslateTabHelper::BEFORE_TRANSLATE, infobar->translate_step());
516 508
517 // Simulate clicking translate. 509 // Simulate clicking translate.
518 process()->sink().ClearMessages(); 510 process()->sink().ClearMessages();
519 infobar->Translate(); 511 infobar->Translate();
520 SimulateTranslateScriptURLFetch(false); 512 SimulateTranslateScriptURLFetch(false);
521 513
522 // We should not have sent any message to translate to the renderer. 514 // We should not have sent any message to translate to the renderer.
523 EXPECT_FALSE(GetTranslateMessage(NULL, NULL, NULL)); 515 EXPECT_FALSE(GetTranslateMessage(NULL, NULL, NULL));
524 516
525 // And we should have an error infobar showing. 517 // And we should have an error infobar showing.
526 infobar = GetTranslateInfoBar(); 518 infobar = GetTranslateInfoBar();
527 ASSERT_TRUE(infobar != NULL); 519 ASSERT_TRUE(infobar != NULL);
528 EXPECT_EQ(TranslateTabHelper::TRANSLATE_ERROR, infobar->translate_step()); 520 EXPECT_EQ(TranslateTabHelper::TRANSLATE_ERROR, infobar->translate_step());
529 } 521 }
530 522
531 // Ensures we deal correctly with pages for which the browser does not recognize 523 // Ensures we deal correctly with pages for which the browser does not recognize
532 // the language (the translate server may or not detect the language). 524 // the language (the translate server may or not detect the language).
533 TEST_F(TranslateManagerBrowserTest, TranslateUnknownLanguage) { 525 TEST_F(TranslateManagerRenderViewHostTest, TranslateUnknownLanguage) {
534 // Simulate navigating to a page ("und" is the string returned by the CLD for 526 // Simulate navigating to a page ("und" is the string returned by the CLD for
535 // languages it does not recognize). 527 // languages it does not recognize).
536 SimulateNavigation(GURL("http://www.google.mys"), "und", true); 528 SimulateNavigation(GURL("http://www.google.mys"), "und", true);
537 529
538 // We should not have an infobar as we don't know the language. 530 // We should not have an infobar as we don't know the language.
539 ASSERT_TRUE(GetTranslateInfoBar() == NULL); 531 ASSERT_TRUE(GetTranslateInfoBar() == NULL);
540 532
541 // Translate the page anyway throught the context menu. 533 // Translate the page anyway throught the context menu.
542 scoped_ptr<TestRenderViewContextMenu> menu( 534 scoped_ptr<TestRenderViewContextMenu> menu(
543 TestRenderViewContextMenu::CreateContextMenu(web_contents())); 535 TestRenderViewContextMenu::CreateContextMenu(web_contents()));
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
576 ASSERT_TRUE(infobar != NULL); 568 ASSERT_TRUE(infobar != NULL);
577 EXPECT_EQ(TranslateTabHelper::TRANSLATE_ERROR, infobar->translate_step()); 569 EXPECT_EQ(TranslateTabHelper::TRANSLATE_ERROR, infobar->translate_step());
578 EXPECT_EQ(TranslateErrors::IDENTICAL_LANGUAGES, infobar->error_type()); 570 EXPECT_EQ(TranslateErrors::IDENTICAL_LANGUAGES, infobar->error_type());
579 571
580 // Let's run the same steps again but this time the server fails to detect the 572 // Let's run the same steps again but this time the server fails to detect the
581 // page's language (it returns an empty string). 573 // page's language (it returns an empty string).
582 SimulateNavigation(GURL("http://www.google.com"), "und", true); 574 SimulateNavigation(GURL("http://www.google.com"), "und", true);
583 menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents())); 575 menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents()));
584 menu->Init(); 576 menu->Init();
585 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0); 577 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0);
586 SimulateOnPageTranslated(2, std::string(), "en", 578 SimulateOnPageTranslated(
587 TranslateErrors::UNKNOWN_LANGUAGE); 579 2, std::string(), "en", TranslateErrors::UNKNOWN_LANGUAGE);
588 infobar = GetTranslateInfoBar(); 580 infobar = GetTranslateInfoBar();
589 ASSERT_TRUE(infobar != NULL); 581 ASSERT_TRUE(infobar != NULL);
590 EXPECT_EQ(TranslateTabHelper::TRANSLATE_ERROR, infobar->translate_step()); 582 EXPECT_EQ(TranslateTabHelper::TRANSLATE_ERROR, infobar->translate_step());
591 EXPECT_EQ(TranslateErrors::UNKNOWN_LANGUAGE, infobar->error_type()); 583 EXPECT_EQ(TranslateErrors::UNKNOWN_LANGUAGE, infobar->error_type());
592 } 584 }
593 585
594 // Tests that we show/don't show an info-bar for the languages. 586 // Tests that we show/don't show an info-bar for the languages.
595 TEST_F(TranslateManagerBrowserTest, TestLanguages) { 587 TEST_F(TranslateManagerRenderViewHostTest, TestLanguages) {
596 std::vector<std::string> languages; 588 std::vector<std::string> languages;
597 languages.push_back("en"); 589 languages.push_back("en");
598 languages.push_back("ja"); 590 languages.push_back("ja");
599 languages.push_back("fr"); 591 languages.push_back("fr");
600 languages.push_back("ht"); 592 languages.push_back("ht");
601 languages.push_back("xx"); 593 languages.push_back("xx");
602 languages.push_back("zh"); 594 languages.push_back("zh");
603 languages.push_back("zh-CN"); 595 languages.push_back("zh-CN");
604 languages.push_back("und"); 596 languages.push_back("und");
605 597
606 GURL url("http://www.google.com"); 598 GURL url("http://www.google.com");
607 for (size_t i = 0; i < languages.size(); ++i) { 599 for (size_t i = 0; i < languages.size(); ++i) {
608 std::string lang = languages[i]; 600 std::string lang = languages[i];
609 SCOPED_TRACE(::testing::Message() << "Iteration " << i << 601 SCOPED_TRACE(::testing::Message() << "Iteration " << i
610 " language=" << lang); 602 << " language=" << lang);
611 603
612 // We should not have a translate infobar. 604 // We should not have a translate infobar.
613 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); 605 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
614 ASSERT_TRUE(infobar == NULL); 606 ASSERT_TRUE(infobar == NULL);
615 607
616 SimulateNavigation(url, lang, true); 608 SimulateNavigation(url, lang, true);
617 609
618 // Verify we have/don't have an info-bar as expected. 610 // Verify we have/don't have an info-bar as expected.
619 infobar = GetTranslateInfoBar(); 611 infobar = GetTranslateInfoBar();
620 bool expected = 612 bool expected =
621 TranslateDownloadManager::IsSupportedLanguage(lang) && lang != "en"; 613 TranslateDownloadManager::IsSupportedLanguage(lang) && lang != "en";
622 EXPECT_EQ(expected, infobar != NULL); 614 EXPECT_EQ(expected, infobar != NULL);
623 615
624 if (infobar != NULL) 616 if (infobar != NULL)
625 EXPECT_TRUE(CloseTranslateInfoBar()); 617 EXPECT_TRUE(CloseTranslateInfoBar());
626 } 618 }
627 } 619 }
628 620
629 // Test the fetching of languages from the translate server 621 // Test the fetching of languages from the translate server
630 TEST_F(TranslateManagerBrowserTest, FetchLanguagesFromTranslateServer) { 622 TEST_F(TranslateManagerRenderViewHostTest, FetchLanguagesFromTranslateServer) {
631 std::vector<std::string> server_languages; 623 std::vector<std::string> server_languages;
632 // A list of languages to fake being returned by the translate server. 624 // A list of languages to fake being returned by the translate server.
633 server_languages.push_back("aa"); 625 server_languages.push_back("aa");
634 server_languages.push_back("ak"); 626 server_languages.push_back("ak");
635 server_languages.push_back("ab"); 627 server_languages.push_back("ab");
636 server_languages.push_back("en-CA"); 628 server_languages.push_back("en-CA");
637 server_languages.push_back("zh"); 629 server_languages.push_back("zh");
638 server_languages.push_back("yi"); 630 server_languages.push_back("yi");
639 server_languages.push_back("fr-FR"); 631 server_languages.push_back("fr-FR");
640 server_languages.push_back("xx"); 632 server_languages.push_back("xx");
641 633
642 std::vector<std::string> alpha_languages; 634 std::vector<std::string> alpha_languages;
643 alpha_languages.push_back("aa"); 635 alpha_languages.push_back("aa");
644 alpha_languages.push_back("yi"); 636 alpha_languages.push_back("yi");
645 637
646 // First, get the default languages list. Note that calling 638 // First, get the default languages list. Note that calling
647 // GetSupportedLanguages() invokes RequestLanguageList() internally. 639 // GetSupportedLanguages() invokes RequestLanguageList() internally.
648 std::vector<std::string> default_supported_languages; 640 std::vector<std::string> default_supported_languages;
649 TranslateDownloadManager::GetSupportedLanguages(&default_supported_languages); 641 TranslateDownloadManager::GetSupportedLanguages(&default_supported_languages);
650 // To make sure we got the defaults and don't confuse them with the mocks. 642 // To make sure we got the defaults and don't confuse them with the mocks.
651 ASSERT_NE(default_supported_languages.size(), server_languages.size()); 643 ASSERT_NE(default_supported_languages.size(), server_languages.size());
652 644
653 // Check that we still get the defaults until the URLFetch has completed. 645 // Check that we still get the defaults until the URLFetch has completed.
654 std::vector<std::string> current_supported_languages; 646 std::vector<std::string> current_supported_languages;
655 TranslateDownloadManager::GetSupportedLanguages(&current_supported_languages); 647 TranslateDownloadManager::GetSupportedLanguages(&current_supported_languages);
656 EXPECT_EQ(default_supported_languages, current_supported_languages); 648 EXPECT_EQ(default_supported_languages, current_supported_languages);
657 649
658 // Also check that it didn't change if we failed the URL fetch. 650 // Also check that it didn't change if we failed the URL fetch.
659 SimulateSupportedLanguagesURLFetch(false, std::vector<std::string>(), 651 SimulateSupportedLanguagesURLFetch(
660 true, std::vector<std::string>()); 652 false, std::vector<std::string>(), true, std::vector<std::string>());
661 current_supported_languages.clear(); 653 current_supported_languages.clear();
662 TranslateDownloadManager::GetSupportedLanguages(&current_supported_languages); 654 TranslateDownloadManager::GetSupportedLanguages(&current_supported_languages);
663 EXPECT_EQ(default_supported_languages, current_supported_languages); 655 EXPECT_EQ(default_supported_languages, current_supported_languages);
664 656
665 // Now check that we got the appropriate set of languages from the server. 657 // Now check that we got the appropriate set of languages from the server.
666 SimulateSupportedLanguagesURLFetch(true, server_languages, 658 SimulateSupportedLanguagesURLFetch(
667 true, alpha_languages); 659 true, server_languages, true, alpha_languages);
668 current_supported_languages.clear(); 660 current_supported_languages.clear();
669 TranslateDownloadManager::GetSupportedLanguages(&current_supported_languages); 661 TranslateDownloadManager::GetSupportedLanguages(&current_supported_languages);
670 // "xx" can't be displayed in the Translate inforbar, so this is eliminated. 662 // "xx" can't be displayed in the Translate inforbar, so this is eliminated.
671 EXPECT_EQ(server_languages.size() - 1, current_supported_languages.size()); 663 EXPECT_EQ(server_languages.size() - 1, current_supported_languages.size());
672 // Not sure we need to guarantee the order of languages, so we find them. 664 // Not sure we need to guarantee the order of languages, so we find them.
673 for (size_t i = 0; i < server_languages.size(); ++i) { 665 for (size_t i = 0; i < server_languages.size(); ++i) {
674 const std::string& lang = server_languages[i]; 666 const std::string& lang = server_languages[i];
675 if (lang == "xx") 667 if (lang == "xx")
676 continue; 668 continue;
677 EXPECT_NE(current_supported_languages.end(), 669 EXPECT_NE(current_supported_languages.end(),
678 std::find(current_supported_languages.begin(), 670 std::find(current_supported_languages.begin(),
679 current_supported_languages.end(), 671 current_supported_languages.end(),
680 lang)); 672 lang));
681 bool is_alpha = std::find(alpha_languages.begin(), 673 bool is_alpha =
682 alpha_languages.end(), 674 std::find(alpha_languages.begin(), alpha_languages.end(), lang) !=
683 lang) != alpha_languages.end(); 675 alpha_languages.end();
684 EXPECT_EQ(TranslateDownloadManager::IsAlphaLanguage(lang), is_alpha); 676 EXPECT_EQ(TranslateDownloadManager::IsAlphaLanguage(lang), is_alpha);
685 } 677 }
686 } 678 }
687 679
688 // Test the fetching of languages from the translate server without 'al' 680 // Test the fetching of languages from the translate server without 'al'
689 // parameter. 681 // parameter.
690 TEST_F(TranslateManagerBrowserTest, 682 TEST_F(TranslateManagerRenderViewHostTest,
691 FetchLanguagesFromTranslateServerWithoutAlpha) { 683 FetchLanguagesFromTranslateServerWithoutAlpha) {
692 std::vector<std::string> server_languages; 684 std::vector<std::string> server_languages;
693 server_languages.push_back("aa"); 685 server_languages.push_back("aa");
694 server_languages.push_back("ak"); 686 server_languages.push_back("ak");
695 server_languages.push_back("ab"); 687 server_languages.push_back("ab");
696 server_languages.push_back("en-CA"); 688 server_languages.push_back("en-CA");
697 server_languages.push_back("zh"); 689 server_languages.push_back("zh");
698 server_languages.push_back("yi"); 690 server_languages.push_back("yi");
699 server_languages.push_back("fr-FR"); 691 server_languages.push_back("fr-FR");
700 server_languages.push_back("xx"); 692 server_languages.push_back("xx");
701 693
702 std::vector<std::string> alpha_languages; 694 std::vector<std::string> alpha_languages;
703 alpha_languages.push_back("aa"); 695 alpha_languages.push_back("aa");
704 alpha_languages.push_back("yi"); 696 alpha_languages.push_back("yi");
705 697
706 // call GetSupportedLanguages to call RequestLanguageList internally. 698 // call GetSupportedLanguages to call RequestLanguageList internally.
707 std::vector<std::string> default_supported_languages; 699 std::vector<std::string> default_supported_languages;
708 TranslateDownloadManager::GetSupportedLanguages(&default_supported_languages); 700 TranslateDownloadManager::GetSupportedLanguages(&default_supported_languages);
709 701
710 SimulateSupportedLanguagesURLFetch(true, server_languages, 702 SimulateSupportedLanguagesURLFetch(
711 false, alpha_languages); 703 true, server_languages, false, alpha_languages);
712 704
713 std::vector<std::string> current_supported_languages; 705 std::vector<std::string> current_supported_languages;
714 TranslateDownloadManager::GetSupportedLanguages(&current_supported_languages); 706 TranslateDownloadManager::GetSupportedLanguages(&current_supported_languages);
715 707
716 // "xx" can't be displayed in the Translate inforbar, so this is eliminated. 708 // "xx" can't be displayed in the Translate inforbar, so this is eliminated.
717 EXPECT_EQ(server_languages.size() - 1, current_supported_languages.size()); 709 EXPECT_EQ(server_languages.size() - 1, current_supported_languages.size());
718 710
719 for (size_t i = 0; i < server_languages.size(); ++i) { 711 for (size_t i = 0; i < server_languages.size(); ++i) {
720 const std::string& lang = server_languages[i]; 712 const std::string& lang = server_languages[i];
721 if (lang == "xx") 713 if (lang == "xx")
722 continue; 714 continue;
723 EXPECT_NE(current_supported_languages.end(), 715 EXPECT_NE(current_supported_languages.end(),
724 std::find(current_supported_languages.begin(), 716 std::find(current_supported_languages.begin(),
725 current_supported_languages.end(), 717 current_supported_languages.end(),
726 lang)); 718 lang));
727 EXPECT_FALSE(TranslateDownloadManager::IsAlphaLanguage(lang)); 719 EXPECT_FALSE(TranslateDownloadManager::IsAlphaLanguage(lang));
728 } 720 }
729 } 721 }
730 722
731 // Tests auto-translate on page. 723 // Tests auto-translate on page.
732 TEST_F(TranslateManagerBrowserTest, AutoTranslateOnNavigate) { 724 TEST_F(TranslateManagerRenderViewHostTest, AutoTranslateOnNavigate) {
733 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); 725 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
734 726
735 // Simulate the user translating. 727 // Simulate the user translating.
736 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); 728 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
737 ASSERT_TRUE(infobar != NULL); 729 ASSERT_TRUE(infobar != NULL);
738 infobar->Translate(); 730 infobar->Translate();
739 // Simulate the translate script being retrieved. 731 // Simulate the translate script being retrieved.
740 SimulateTranslateScriptURLFetch(true); 732 SimulateTranslateScriptURLFetch(true);
741 SimulateOnPageTranslated("fr", "en"); 733 SimulateOnPageTranslated("fr", "en");
742 734
(...skipping 11 matching lines...) Expand all
754 746
755 // Now navigate to a page in a different language. 747 // Now navigate to a page in a different language.
756 process()->sink().ClearMessages(); 748 process()->sink().ClearMessages();
757 SimulateNavigation(GURL("http://news.google.es"), "es", true); 749 SimulateNavigation(GURL("http://news.google.es"), "es", true);
758 750
759 // This should not have triggered a translate. 751 // This should not have triggered a translate.
760 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); 752 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
761 } 753 }
762 754
763 // Tests that multiple OnPageContents do not cause multiple infobars. 755 // Tests that multiple OnPageContents do not cause multiple infobars.
764 TEST_F(TranslateManagerBrowserTest, MultipleOnPageContents) { 756 TEST_F(TranslateManagerRenderViewHostTest, MultipleOnPageContents) {
765 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); 757 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
766 758
767 // Simulate clicking 'Nope' (don't translate). 759 // Simulate clicking 'Nope' (don't translate).
768 EXPECT_TRUE(DenyTranslation()); 760 EXPECT_TRUE(DenyTranslation());
769 EXPECT_EQ(0U, infobar_service()->infobar_count()); 761 EXPECT_EQ(0U, infobar_service()->infobar_count());
770 762
771 // Send a new PageContents, we should not show an infobar. 763 // Send a new PageContents, we should not show an infobar.
772 SimulateOnTranslateLanguageDetermined("fr", true); 764 SimulateOnTranslateLanguageDetermined("fr", true);
773 EXPECT_EQ(0U, infobar_service()->infobar_count()); 765 EXPECT_EQ(0U, infobar_service()->infobar_count());
774 766
775 // Do the same steps but simulate closing the infobar this time. 767 // Do the same steps but simulate closing the infobar this time.
776 SimulateNavigation(GURL("http://www.youtube.fr"), "fr", true); 768 SimulateNavigation(GURL("http://www.youtube.fr"), "fr", true);
777 EXPECT_TRUE(CloseTranslateInfoBar()); 769 EXPECT_TRUE(CloseTranslateInfoBar());
778 EXPECT_EQ(0U, infobar_service()->infobar_count()); 770 EXPECT_EQ(0U, infobar_service()->infobar_count());
779 SimulateOnTranslateLanguageDetermined("fr", true); 771 SimulateOnTranslateLanguageDetermined("fr", true);
780 EXPECT_EQ(0U, infobar_service()->infobar_count()); 772 EXPECT_EQ(0U, infobar_service()->infobar_count());
781 } 773 }
782 774
783 // Test that reloading the page brings back the infobar if the 775 // Test that reloading the page brings back the infobar if the
784 // reload succeeded and does not bring it back the reload fails. 776 // reload succeeded and does not bring it back the reload fails.
785 TEST_F(TranslateManagerBrowserTest, Reload) { 777 TEST_F(TranslateManagerRenderViewHostTest, Reload) {
786 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); 778 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
787 779
788 EXPECT_TRUE(CloseTranslateInfoBar()); 780 EXPECT_TRUE(CloseTranslateInfoBar());
789 781
790 // Reload should bring back the infobar if the reload succeeds. 782 // Reload should bring back the infobar if the reload succeeds.
791 ReloadAndWait(true); 783 ReloadAndWait(true);
792 EXPECT_TRUE(GetTranslateInfoBar() != NULL); 784 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
793 EXPECT_TRUE(CloseTranslateInfoBar()); 785 EXPECT_TRUE(CloseTranslateInfoBar());
794 786
795 // ...But not show it if the reload fails. 787 // ...But not show it if the reload fails.
796 ReloadAndWait(false); 788 ReloadAndWait(false);
797 EXPECT_TRUE(GetTranslateInfoBar() == NULL); 789 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
798 790
799 // If we set reload attempts to a high value, we will not see the infobar 791 // If we set reload attempts to a high value, we will not see the infobar
800 // immediately. 792 // immediately.
801 TranslateManager* manager = 793 TranslateManager* manager =
802 TranslateTabHelper::GetManagerFromWebContents(web_contents()); 794 TranslateTabHelper::GetManagerFromWebContents(web_contents());
803 manager->set_translate_max_reload_attemps(100); 795 manager->set_translate_max_reload_attemps(100);
804 ReloadAndWait(true); 796 ReloadAndWait(true);
805 EXPECT_TRUE(GetTranslateInfoBar() == NULL); 797 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
806 } 798 }
807 799
808 // Test that reloading the page by way of typing again the URL in the 800 // Test that reloading the page by way of typing again the URL in the
809 // location bar brings back the infobar. 801 // location bar brings back the infobar.
810 TEST_F(TranslateManagerBrowserTest, ReloadFromLocationBar) { 802 TEST_F(TranslateManagerRenderViewHostTest, ReloadFromLocationBar) {
811 GURL url("http://www.google.fr"); 803 GURL url("http://www.google.fr");
812 SimulateNavigation(url, "fr", true); 804 SimulateNavigation(url, "fr", true);
813 805
814 EXPECT_TRUE(CloseTranslateInfoBar()); 806 EXPECT_TRUE(CloseTranslateInfoBar());
815 807
816 // Create a pending navigation and simulate a page load. That should be the 808 // Create a pending navigation and simulate a page load. That should be the
817 // equivalent of typing the URL again in the location bar. 809 // equivalent of typing the URL again in the location bar.
818 NavEntryCommittedObserver nav_observer(web_contents()); 810 NavEntryCommittedObserver nav_observer(web_contents());
819 web_contents()->GetController().LoadURL(url, content::Referrer(), 811 web_contents()->GetController().LoadURL(
820 content::PAGE_TRANSITION_TYPED, 812 url, content::Referrer(), content::PAGE_TRANSITION_TYPED, std::string());
821 std::string());
822 rvh_tester()->SendNavigate(0, url); 813 rvh_tester()->SendNavigate(0, url);
823 814
824 // Test that we are really getting a same page navigation, the test would be 815 // Test that we are really getting a same page navigation, the test would be
825 // useless if it was not the case. 816 // useless if it was not the case.
826 const content::LoadCommittedDetails& nav_details = 817 const content::LoadCommittedDetails& nav_details =
827 nav_observer.load_committed_details(); 818 nav_observer.load_committed_details();
828 EXPECT_TRUE(nav_details.entry != NULL); // There was a navigation. 819 EXPECT_TRUE(nav_details.entry != NULL); // There was a navigation.
829 EXPECT_EQ(content::NAVIGATION_TYPE_SAME_PAGE, nav_details.type); 820 EXPECT_EQ(content::NAVIGATION_TYPE_SAME_PAGE, nav_details.type);
830 821
831 // The TranslateManager class processes the navigation entry committed 822 // The TranslateManager class processes the navigation entry committed
832 // notification in a posted task; process that task. 823 // notification in a posted task; process that task.
833 base::MessageLoop::current()->RunUntilIdle(); 824 base::MessageLoop::current()->RunUntilIdle();
834 EXPECT_TRUE(GetTranslateInfoBar() != NULL); 825 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
835 } 826 }
836 827
837 // Tests that a closed translate infobar does not reappear when navigating 828 // Tests that a closed translate infobar does not reappear when navigating
838 // in-page. 829 // in-page.
839 TEST_F(TranslateManagerBrowserTest, CloseInfoBarInPageNavigation) { 830 TEST_F(TranslateManagerRenderViewHostTest, CloseInfoBarInPageNavigation) {
840 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); 831 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
841 832
842 EXPECT_TRUE(CloseTranslateInfoBar()); 833 EXPECT_TRUE(CloseTranslateInfoBar());
843 834
844 // Navigate in page, no infobar should be shown. 835 // Navigate in page, no infobar should be shown.
845 SimulateNavigation(GURL("http://www.google.fr/#ref1"), "fr", true); 836 SimulateNavigation(GURL("http://www.google.fr/#ref1"), "fr", true);
846 EXPECT_TRUE(GetTranslateInfoBar() == NULL); 837 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
847 838
848 // Navigate out of page, a new infobar should show. 839 // Navigate out of page, a new infobar should show.
849 SimulateNavigation(GURL("http://www.google.fr/foot"), "fr", true); 840 SimulateNavigation(GURL("http://www.google.fr/foot"), "fr", true);
850 EXPECT_TRUE(GetTranslateInfoBar() != NULL); 841 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
851 } 842 }
852 843
853 // Tests that a closed translate infobar does not reappear when navigating 844 // Tests that a closed translate infobar does not reappear when navigating
854 // in a subframe. (http://crbug.com/48215) 845 // in a subframe. (http://crbug.com/48215)
855 TEST_F(TranslateManagerBrowserTest, CloseInfoBarInSubframeNavigation) { 846 TEST_F(TranslateManagerRenderViewHostTest, CloseInfoBarInSubframeNavigation) {
856 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); 847 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
857 848
858 EXPECT_TRUE(CloseTranslateInfoBar()); 849 EXPECT_TRUE(CloseTranslateInfoBar());
859 850
860 // Simulate a sub-frame auto-navigating. 851 // Simulate a sub-frame auto-navigating.
861 rvh_tester()->SendNavigateWithTransition( 852 rvh_tester()->SendNavigateWithTransition(
862 1, GURL("http://pub.com"), content::PAGE_TRANSITION_AUTO_SUBFRAME); 853 1, GURL("http://pub.com"), content::PAGE_TRANSITION_AUTO_SUBFRAME);
863 EXPECT_TRUE(GetTranslateInfoBar() == NULL); 854 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
864 855
865 // Simulate the user navigating in a sub-frame. 856 // Simulate the user navigating in a sub-frame.
866 rvh_tester()->SendNavigateWithTransition( 857 rvh_tester()->SendNavigateWithTransition(
867 2, GURL("http://pub.com"), content::PAGE_TRANSITION_MANUAL_SUBFRAME); 858 2, GURL("http://pub.com"), content::PAGE_TRANSITION_MANUAL_SUBFRAME);
868 EXPECT_TRUE(GetTranslateInfoBar() == NULL); 859 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
869 860
870 // Navigate out of page, a new infobar should show. 861 // Navigate out of page, a new infobar should show.
871 SimulateNavigation(GURL("http://www.google.fr/foot"), "fr", true); 862 SimulateNavigation(GURL("http://www.google.fr/foot"), "fr", true);
872 EXPECT_TRUE(GetTranslateInfoBar() != NULL); 863 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
873 } 864 }
874 865
875 // Tests that denying translation is sticky when navigating in page. 866 // Tests that denying translation is sticky when navigating in page.
876 TEST_F(TranslateManagerBrowserTest, DenyTranslateInPageNavigation) { 867 TEST_F(TranslateManagerRenderViewHostTest, DenyTranslateInPageNavigation) {
877 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); 868 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
878 869
879 // Simulate clicking 'Nope' (don't translate). 870 // Simulate clicking 'Nope' (don't translate).
880 EXPECT_TRUE(DenyTranslation()); 871 EXPECT_TRUE(DenyTranslation());
881 872
882 // Navigate in page, no infobar should be shown. 873 // Navigate in page, no infobar should be shown.
883 SimulateNavigation(GURL("http://www.google.fr/#ref1"), "fr", true); 874 SimulateNavigation(GURL("http://www.google.fr/#ref1"), "fr", true);
884 EXPECT_TRUE(GetTranslateInfoBar() == NULL); 875 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
885 876
886 // Navigate out of page, a new infobar should show. 877 // Navigate out of page, a new infobar should show.
887 SimulateNavigation(GURL("http://www.google.fr/foot"), "fr", true); 878 SimulateNavigation(GURL("http://www.google.fr/foot"), "fr", true);
888 EXPECT_TRUE(GetTranslateInfoBar() != NULL); 879 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
889 } 880 }
890 881
891 // Tests that after translating and closing the infobar, the infobar does not 882 // Tests that after translating and closing the infobar, the infobar does not
892 // return when navigating in page. 883 // return when navigating in page.
893 TEST_F(TranslateManagerBrowserTest, TranslateCloseInfoBarInPageNavigation) { 884 TEST_F(TranslateManagerRenderViewHostTest,
885 TranslateCloseInfoBarInPageNavigation) {
894 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); 886 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
895 887
896 // Simulate the user translating. 888 // Simulate the user translating.
897 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); 889 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
898 ASSERT_TRUE(infobar != NULL); 890 ASSERT_TRUE(infobar != NULL);
899 infobar->Translate(); 891 infobar->Translate();
900 // Simulate the translate script being retrieved. 892 // Simulate the translate script being retrieved.
901 SimulateTranslateScriptURLFetch(true); 893 SimulateTranslateScriptURLFetch(true);
902 SimulateOnPageTranslated("fr", "en"); 894 SimulateOnPageTranslated("fr", "en");
903 895
904 EXPECT_TRUE(CloseTranslateInfoBar()); 896 EXPECT_TRUE(CloseTranslateInfoBar());
905 897
906 // Navigate in page, no infobar should be shown. 898 // Navigate in page, no infobar should be shown.
907 SimulateNavigation(GURL("http://www.google.fr/#ref1"), "fr", true); 899 SimulateNavigation(GURL("http://www.google.fr/#ref1"), "fr", true);
908 EXPECT_TRUE(GetTranslateInfoBar() == NULL); 900 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
909 901
910 // Navigate out of page, a new infobar should show. 902 // Navigate out of page, a new infobar should show.
911 // Note that we navigate to a page in a different language so we don't trigger 903 // Note that we navigate to a page in a different language so we don't trigger
912 // the auto-translate feature (it would translate the page automatically and 904 // the auto-translate feature (it would translate the page automatically and
913 // the before translate inforbar would not be shown). 905 // the before translate inforbar would not be shown).
914 SimulateNavigation(GURL("http://www.google.de"), "de", true); 906 SimulateNavigation(GURL("http://www.google.de"), "de", true);
915 EXPECT_TRUE(GetTranslateInfoBar() != NULL); 907 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
916 } 908 }
917 909
918 // Tests that the after translate the infobar still shows when navigating 910 // Tests that the after translate the infobar still shows when navigating
919 // in-page. 911 // in-page.
920 TEST_F(TranslateManagerBrowserTest, TranslateInPageNavigation) { 912 TEST_F(TranslateManagerRenderViewHostTest, TranslateInPageNavigation) {
921 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); 913 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
922 914
923 // Simulate the user translating. 915 // Simulate the user translating.
924 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); 916 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
925 ASSERT_TRUE(infobar != NULL); 917 ASSERT_TRUE(infobar != NULL);
926 infobar->Translate(); 918 infobar->Translate();
927 SimulateTranslateScriptURLFetch(true); 919 SimulateTranslateScriptURLFetch(true);
928 SimulateOnPageTranslated("fr", "en"); 920 SimulateOnPageTranslated("fr", "en");
929 // The after translate infobar is showing. 921 // The after translate infobar is showing.
930 infobar = GetTranslateInfoBar(); 922 infobar = GetTranslateInfoBar();
931 ASSERT_TRUE(infobar != NULL); 923 ASSERT_TRUE(infobar != NULL);
932 924
933 // Navigate out of page, a new infobar should show. 925 // Navigate out of page, a new infobar should show.
934 // See note in TranslateCloseInfoBarInPageNavigation test on why it is 926 // See note in TranslateCloseInfoBarInPageNavigation test on why it is
935 // important to navigate to a page in a different language for this test. 927 // important to navigate to a page in a different language for this test.
936 SimulateNavigation(GURL("http://www.google.de"), "de", true); 928 SimulateNavigation(GURL("http://www.google.de"), "de", true);
937 // The old infobar is gone. 929 // The old infobar is gone.
938 EXPECT_TRUE(CheckInfoBarRemovedAndReset(infobar)); 930 EXPECT_TRUE(CheckInfoBarRemovedAndReset(infobar));
939 // And there is a new one. 931 // And there is a new one.
940 EXPECT_TRUE(GetTranslateInfoBar() != NULL); 932 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
941 } 933 }
942 934
943 // Tests that no translate infobar is shown when navigating to a page in an 935 // Tests that no translate infobar is shown when navigating to a page in an
944 // unsupported language. 936 // unsupported language.
945 TEST_F(TranslateManagerBrowserTest, CLDReportsUnsupportedPageLanguage) { 937 TEST_F(TranslateManagerRenderViewHostTest, CLDReportsUnsupportedPageLanguage) {
946 // Simulate navigating to a page and getting an unsupported language. 938 // Simulate navigating to a page and getting an unsupported language.
947 SimulateNavigation(GURL("http://www.google.com"), "qbz", true); 939 SimulateNavigation(GURL("http://www.google.com"), "qbz", true);
948 940
949 // No info-bar should be shown. 941 // No info-bar should be shown.
950 EXPECT_TRUE(GetTranslateInfoBar() == NULL); 942 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
951 } 943 }
952 944
953 // Tests that we deal correctly with unsupported languages returned by the 945 // Tests that we deal correctly with unsupported languages returned by the
954 // server. 946 // server.
955 // The translation server might return a language we don't support. 947 // The translation server might return a language we don't support.
956 TEST_F(TranslateManagerBrowserTest, ServerReportsUnsupportedLanguage) { 948 TEST_F(TranslateManagerRenderViewHostTest, ServerReportsUnsupportedLanguage) {
957 SimulateNavigation(GURL("http://mail.google.fr"), "fr", true); 949 SimulateNavigation(GURL("http://mail.google.fr"), "fr", true);
958 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); 950 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
959 ASSERT_TRUE(infobar != NULL); 951 ASSERT_TRUE(infobar != NULL);
960 process()->sink().ClearMessages(); 952 process()->sink().ClearMessages();
961 infobar->Translate(); 953 infobar->Translate();
962 SimulateTranslateScriptURLFetch(true); 954 SimulateTranslateScriptURLFetch(true);
963 // Simulate the render notifying the translation has been done, but it 955 // Simulate the render notifying the translation has been done, but it
964 // reports a language we don't support. 956 // reports a language we don't support.
965 SimulateOnPageTranslated("qbz", "en"); 957 SimulateOnPageTranslated("qbz", "en");
966 958
967 // An error infobar should be showing to report that we don't support this 959 // An error infobar should be showing to report that we don't support this
968 // language. 960 // language.
969 infobar = GetTranslateInfoBar(); 961 infobar = GetTranslateInfoBar();
970 ASSERT_TRUE(infobar != NULL); 962 ASSERT_TRUE(infobar != NULL);
971 EXPECT_EQ(TranslateTabHelper::TRANSLATE_ERROR, infobar->translate_step()); 963 EXPECT_EQ(TranslateTabHelper::TRANSLATE_ERROR, infobar->translate_step());
972 964
973 // This infobar should have a button (so the string should not be empty). 965 // This infobar should have a button (so the string should not be empty).
974 ASSERT_FALSE(infobar->GetMessageInfoBarButtonText().empty()); 966 ASSERT_FALSE(infobar->GetMessageInfoBarButtonText().empty());
975 967
976 // Pressing the button on that infobar should revert to the original language. 968 // Pressing the button on that infobar should revert to the original language.
977 process()->sink().ClearMessages(); 969 process()->sink().ClearMessages();
978 infobar->MessageInfoBarButtonPressed(); 970 infobar->MessageInfoBarButtonPressed();
979 const IPC::Message* message = 971 const IPC::Message* message = process()->sink().GetFirstMessageMatching(
980 process()->sink().GetFirstMessageMatching( 972 ChromeViewMsg_RevertTranslation::ID);
981 ChromeViewMsg_RevertTranslation::ID);
982 EXPECT_TRUE(message != NULL); 973 EXPECT_TRUE(message != NULL);
983 // And it should have removed the infobar. 974 // And it should have removed the infobar.
984 EXPECT_TRUE(GetTranslateInfoBar() == NULL); 975 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
985 } 976 }
986 977
987 // Tests that no translate infobar is shown and context menu is disabled, when 978 // Tests that no translate infobar is shown and context menu is disabled, when
988 // Chrome is in a language that the translate server does not support. 979 // Chrome is in a language that the translate server does not support.
989 TEST_F(TranslateManagerBrowserTest, UnsupportedUILanguage) { 980 TEST_F(TranslateManagerRenderViewHostTest, UnsupportedUILanguage) {
990 std::string original_lang = g_browser_process->GetApplicationLocale(); 981 std::string original_lang = g_browser_process->GetApplicationLocale();
991 SetApplicationLocale("qbz"); 982 SetApplicationLocale("qbz");
992 983
993 // Make sure that the accept language list only contains unsupported languages 984 // Make sure that the accept language list only contains unsupported languages
994 Profile* profile = 985 Profile* profile =
995 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); 986 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
996 PrefService* prefs = profile->GetPrefs(); 987 PrefService* prefs = profile->GetPrefs();
997 prefs->SetString(prefs::kAcceptLanguages, "qbz"); 988 prefs->SetString(prefs::kAcceptLanguages, "qbz");
998 989
999 // Simulate navigating to a page in a language supported by the translate 990 // Simulate navigating to a page in a language supported by the translate
1000 // server. 991 // server.
1001 SimulateNavigation(GURL("http://www.google.com"), "en", true); 992 SimulateNavigation(GURL("http://www.google.com"), "en", true);
1002 993
1003 // No info-bar should be shown. 994 // No info-bar should be shown.
1004 EXPECT_TRUE(GetTranslateInfoBar() == NULL); 995 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
1005 996
1006 // And the context menu option should be disabled too. 997 // And the context menu option should be disabled too.
1007 scoped_ptr<TestRenderViewContextMenu> menu( 998 scoped_ptr<TestRenderViewContextMenu> menu(
1008 TestRenderViewContextMenu::CreateContextMenu(web_contents())); 999 TestRenderViewContextMenu::CreateContextMenu(web_contents()));
1009 menu->Init(); 1000 menu->Init();
1010 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); 1001 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
1011 EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); 1002 EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
1012 1003
1013 SetApplicationLocale(original_lang); 1004 SetApplicationLocale(original_lang);
1014 } 1005 }
1015 1006
1016 // Tests that the first supported accept language is selected 1007 // Tests that the first supported accept language is selected
1017 TEST_F(TranslateManagerBrowserTest, TranslateAcceptLanguage) { 1008 TEST_F(TranslateManagerRenderViewHostTest, TranslateAcceptLanguage) {
1018 // Set locate to non-existant language 1009 // Set locate to non-existant language
1019 std::string original_lang = g_browser_process->GetApplicationLocale(); 1010 std::string original_lang = g_browser_process->GetApplicationLocale();
1020 SetApplicationLocale("qbz"); 1011 SetApplicationLocale("qbz");
1021 1012
1022 // Set Qbz and French as the only accepted languages 1013 // Set Qbz and French as the only accepted languages
1023 Profile* profile = 1014 Profile* profile =
1024 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); 1015 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
1025 PrefService* prefs = profile->GetPrefs(); 1016 PrefService* prefs = profile->GetPrefs();
1026 prefs->SetString(prefs::kAcceptLanguages, "qbz,fr"); 1017 prefs->SetString(prefs::kAcceptLanguages, "qbz,fr");
1027 1018
1028 // Go to a German page 1019 // Go to a German page
1029 SimulateNavigation(GURL("http://google.de"), "de", true); 1020 SimulateNavigation(GURL("http://google.de"), "de", true);
1030 1021
1031 // Expect the infobar to pop up 1022 // Expect the infobar to pop up
1032 EXPECT_TRUE(GetTranslateInfoBar() != NULL); 1023 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
1033 1024
1034 // Set Qbz and English-US as the only accepted languages to test the country 1025 // Set Qbz and English-US as the only accepted languages to test the country
1035 // code removal code which was causing a crash as filed in Issue 90106, 1026 // code removal code which was causing a crash as filed in Issue 90106,
1036 // a crash caused by a language with a country code that wasn't recognized. 1027 // a crash caused by a language with a country code that wasn't recognized.
1037 prefs->SetString(prefs::kAcceptLanguages, "qbz,en-us"); 1028 prefs->SetString(prefs::kAcceptLanguages, "qbz,en-us");
1038 1029
1039 // Go to a German page 1030 // Go to a German page
1040 SimulateNavigation(GURL("http://google.de"), "de", true); 1031 SimulateNavigation(GURL("http://google.de"), "de", true);
1041 1032
1042 // Expect the infobar to pop up 1033 // Expect the infobar to pop up
1043 EXPECT_TRUE(GetTranslateInfoBar() != NULL); 1034 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
1044 } 1035 }
1045 1036
1046 // Tests that the translate enabled preference is honored. 1037 // Tests that the translate enabled preference is honored.
1047 TEST_F(TranslateManagerBrowserTest, TranslateEnabledPref) { 1038 TEST_F(TranslateManagerRenderViewHostTest, TranslateEnabledPref) {
1048 // Make sure the pref allows translate. 1039 // Make sure the pref allows translate.
1049 Profile* profile = 1040 Profile* profile =
1050 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); 1041 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
1051 PrefService* prefs = profile->GetPrefs(); 1042 PrefService* prefs = profile->GetPrefs();
1052 prefs->SetBoolean(prefs::kEnableTranslate, true); 1043 prefs->SetBoolean(prefs::kEnableTranslate, true);
1053 1044
1054 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); 1045 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
1055 1046
1056 // An infobar should be shown. 1047 // An infobar should be shown.
1057 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); 1048 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
1058 EXPECT_TRUE(infobar != NULL); 1049 EXPECT_TRUE(infobar != NULL);
1059 1050
1060 // Disable translate. 1051 // Disable translate.
1061 prefs->SetBoolean(prefs::kEnableTranslate, false); 1052 prefs->SetBoolean(prefs::kEnableTranslate, false);
1062 1053
1063 // Navigate to a new page, that should close the previous infobar. 1054 // Navigate to a new page, that should close the previous infobar.
1064 GURL url("http://www.youtube.fr"); 1055 GURL url("http://www.youtube.fr");
1065 NavigateAndCommit(url); 1056 NavigateAndCommit(url);
1066 infobar = GetTranslateInfoBar(); 1057 infobar = GetTranslateInfoBar();
1067 EXPECT_TRUE(infobar == NULL); 1058 EXPECT_TRUE(infobar == NULL);
1068 1059
1069 // Simulate getting the page contents and language, that should not trigger 1060 // Simulate getting the page contents and language, that should not trigger
1070 // a translate infobar. 1061 // a translate infobar.
1071 SimulateOnTranslateLanguageDetermined("fr", true); 1062 SimulateOnTranslateLanguageDetermined("fr", true);
1072 infobar = GetTranslateInfoBar(); 1063 infobar = GetTranslateInfoBar();
1073 EXPECT_TRUE(infobar == NULL); 1064 EXPECT_TRUE(infobar == NULL);
1074 } 1065 }
1075 1066
1076 // Tests the "Never translate <language>" pref. 1067 // Tests the "Never translate <language>" pref.
1077 TEST_F(TranslateManagerBrowserTest, NeverTranslateLanguagePref) { 1068 TEST_F(TranslateManagerRenderViewHostTest, NeverTranslateLanguagePref) {
1078 GURL url("http://www.google.fr"); 1069 GURL url("http://www.google.fr");
1079 SimulateNavigation(url, "fr", true); 1070 SimulateNavigation(url, "fr", true);
1080 1071
1081 // An infobar should be shown. 1072 // An infobar should be shown.
1082 EXPECT_TRUE(GetTranslateInfoBar() != NULL); 1073 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
1083 1074
1084 // Select never translate this language. 1075 // Select never translate this language.
1085 Profile* profile = 1076 Profile* profile =
1086 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); 1077 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
1087 PrefService* prefs = profile->GetPrefs(); 1078 PrefService* prefs = profile->GetPrefs();
1088 PrefChangeRegistrar registrar; 1079 PrefChangeRegistrar registrar;
1089 registrar.Init(prefs); 1080 registrar.Init(prefs);
1090 registrar.Add(TranslatePrefs::kPrefTranslateBlockedLanguages, 1081 registrar.Add(TranslatePrefs::kPrefTranslateBlockedLanguages, pref_callback_);
1091 pref_callback_);
1092 scoped_ptr<TranslatePrefs> translate_prefs( 1082 scoped_ptr<TranslatePrefs> translate_prefs(
1093 TranslateTabHelper::CreateTranslatePrefs(prefs)); 1083 TranslateTabHelper::CreateTranslatePrefs(prefs));
1094 EXPECT_FALSE(translate_prefs->IsBlockedLanguage("fr")); 1084 EXPECT_FALSE(translate_prefs->IsBlockedLanguage("fr"));
1095 TranslateAcceptLanguages* accept_languages = 1085 TranslateAcceptLanguages* accept_languages =
1096 TranslateTabHelper::GetTranslateAcceptLanguages(profile); 1086 TranslateTabHelper::GetTranslateAcceptLanguages(profile);
1097 EXPECT_TRUE(translate_prefs->CanTranslateLanguage(accept_languages, "fr")); 1087 EXPECT_TRUE(translate_prefs->CanTranslateLanguage(accept_languages, "fr"));
1098 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateBlockedLanguages); 1088 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateBlockedLanguages);
1099 translate_prefs->BlockLanguage("fr"); 1089 translate_prefs->BlockLanguage("fr");
1100 EXPECT_TRUE(translate_prefs->IsBlockedLanguage("fr")); 1090 EXPECT_TRUE(translate_prefs->IsBlockedLanguage("fr"));
1101 EXPECT_FALSE(translate_prefs->IsSiteBlacklisted(url.host())); 1091 EXPECT_FALSE(translate_prefs->IsSiteBlacklisted(url.host()));
(...skipping 15 matching lines...) Expand all
1117 EXPECT_TRUE(translate_prefs->CanTranslateLanguage(accept_languages, "fr")); 1107 EXPECT_TRUE(translate_prefs->CanTranslateLanguage(accept_languages, "fr"));
1118 1108
1119 // Navigate to a page in French. 1109 // Navigate to a page in French.
1120 SimulateNavigation(url, "fr", true); 1110 SimulateNavigation(url, "fr", true);
1121 1111
1122 // There should be a translate infobar. 1112 // There should be a translate infobar.
1123 EXPECT_TRUE(GetTranslateInfoBar() != NULL); 1113 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
1124 } 1114 }
1125 1115
1126 // Tests the "Never translate this site" pref. 1116 // Tests the "Never translate this site" pref.
1127 TEST_F(TranslateManagerBrowserTest, NeverTranslateSitePref) { 1117 TEST_F(TranslateManagerRenderViewHostTest, NeverTranslateSitePref) {
1128 GURL url("http://www.google.fr"); 1118 GURL url("http://www.google.fr");
1129 std::string host(url.host()); 1119 std::string host(url.host());
1130 SimulateNavigation(url, "fr", true); 1120 SimulateNavigation(url, "fr", true);
1131 1121
1132 // An infobar should be shown. 1122 // An infobar should be shown.
1133 EXPECT_TRUE(GetTranslateInfoBar() != NULL); 1123 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
1134 1124
1135 // Select never translate this site. 1125 // Select never translate this site.
1136 Profile* profile = 1126 Profile* profile =
1137 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); 1127 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
(...skipping 27 matching lines...) Expand all
1165 EXPECT_TRUE(translate_prefs->CanTranslateLanguage(accept_languages, "fr")); 1155 EXPECT_TRUE(translate_prefs->CanTranslateLanguage(accept_languages, "fr"));
1166 1156
1167 // Navigate to a page in French. 1157 // Navigate to a page in French.
1168 SimulateNavigation(url, "fr", true); 1158 SimulateNavigation(url, "fr", true);
1169 1159
1170 // There should be a translate infobar. 1160 // There should be a translate infobar.
1171 EXPECT_TRUE(GetTranslateInfoBar() != NULL); 1161 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
1172 } 1162 }
1173 1163
1174 // Tests the "Always translate this language" pref. 1164 // Tests the "Always translate this language" pref.
1175 TEST_F(TranslateManagerBrowserTest, AlwaysTranslateLanguagePref) { 1165 TEST_F(TranslateManagerRenderViewHostTest, AlwaysTranslateLanguagePref) {
1176 // Select always translate French to English. 1166 // Select always translate French to English.
1177 Profile* profile = 1167 Profile* profile =
1178 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); 1168 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
1179 PrefService* prefs = profile->GetPrefs(); 1169 PrefService* prefs = profile->GetPrefs();
1180 PrefChangeRegistrar registrar; 1170 PrefChangeRegistrar registrar;
1181 registrar.Init(prefs); 1171 registrar.Init(prefs);
1182 registrar.Add(TranslatePrefs::kPrefTranslateWhitelists, pref_callback_); 1172 registrar.Add(TranslatePrefs::kPrefTranslateWhitelists, pref_callback_);
1183 scoped_ptr<TranslatePrefs> translate_prefs( 1173 scoped_ptr<TranslatePrefs> translate_prefs(
1184 TranslateTabHelper::CreateTranslatePrefs(prefs)); 1174 TranslateTabHelper::CreateTranslatePrefs(prefs));
1185 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateWhitelists); 1175 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateWhitelists);
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
1224 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateWhitelists); 1214 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateWhitelists);
1225 translate_prefs->RemoveLanguagePairFromWhitelist("fr", "en"); 1215 translate_prefs->RemoveLanguagePairFromWhitelist("fr", "en");
1226 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); 1216 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
1227 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); 1217 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
1228 infobar = GetTranslateInfoBar(); 1218 infobar = GetTranslateInfoBar();
1229 ASSERT_TRUE(infobar != NULL); 1219 ASSERT_TRUE(infobar != NULL);
1230 EXPECT_EQ(TranslateTabHelper::BEFORE_TRANSLATE, infobar->translate_step()); 1220 EXPECT_EQ(TranslateTabHelper::BEFORE_TRANSLATE, infobar->translate_step());
1231 } 1221 }
1232 1222
1233 // Context menu. 1223 // Context menu.
1234 TEST_F(TranslateManagerBrowserTest, ContextMenu) { 1224 TEST_F(TranslateManagerRenderViewHostTest, ContextMenu) {
1235 // Blacklist www.google.fr and French for translation. 1225 // Blacklist www.google.fr and French for translation.
1236 GURL url("http://www.google.fr"); 1226 GURL url("http://www.google.fr");
1237 Profile* profile = 1227 Profile* profile =
1238 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); 1228 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
1239 scoped_ptr<TranslatePrefs> translate_prefs( 1229 scoped_ptr<TranslatePrefs> translate_prefs(
1240 TranslateTabHelper::CreateTranslatePrefs(profile->GetPrefs())); 1230 TranslateTabHelper::CreateTranslatePrefs(profile->GetPrefs()));
1241 translate_prefs->BlockLanguage("fr"); 1231 translate_prefs->BlockLanguage("fr");
1242 translate_prefs->BlacklistSite(url.host()); 1232 translate_prefs->BlacklistSite(url.host());
1243 EXPECT_TRUE(translate_prefs->IsBlockedLanguage("fr")); 1233 EXPECT_TRUE(translate_prefs->IsBlockedLanguage("fr"));
1244 EXPECT_TRUE(translate_prefs->IsSiteBlacklisted(url.host())); 1234 EXPECT_TRUE(translate_prefs->IsSiteBlacklisted(url.host()));
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
1333 SimulateNavigation(url, "qbz", true); 1323 SimulateNavigation(url, "qbz", true);
1334 menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents())); 1324 menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents()));
1335 menu->Init(); 1325 menu->Init();
1336 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); 1326 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
1337 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); 1327 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
1338 } 1328 }
1339 1329
1340 // Tests that an extra always/never translate button is shown on the "before 1330 // Tests that an extra always/never translate button is shown on the "before
1341 // translate" infobar when the translation is accepted/declined 3 times, 1331 // translate" infobar when the translation is accepted/declined 3 times,
1342 // only when not in incognito mode. 1332 // only when not in incognito mode.
1343 TEST_F(TranslateManagerBrowserTest, BeforeTranslateExtraButtons) { 1333 TEST_F(TranslateManagerRenderViewHostTest, BeforeTranslateExtraButtons) {
1344 Profile* profile = 1334 Profile* profile =
1345 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); 1335 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
1346 scoped_ptr<TranslatePrefs> translate_prefs( 1336 scoped_ptr<TranslatePrefs> translate_prefs(
1347 TranslateTabHelper::CreateTranslatePrefs(profile->GetPrefs())); 1337 TranslateTabHelper::CreateTranslatePrefs(profile->GetPrefs()));
1348 translate_prefs->ResetTranslationAcceptedCount("fr"); 1338 translate_prefs->ResetTranslationAcceptedCount("fr");
1349 translate_prefs->ResetTranslationDeniedCount("fr"); 1339 translate_prefs->ResetTranslationDeniedCount("fr");
1350 translate_prefs->ResetTranslationAcceptedCount("de"); 1340 translate_prefs->ResetTranslationAcceptedCount("de");
1351 translate_prefs->ResetTranslationDeniedCount("de"); 1341 translate_prefs->ResetTranslationDeniedCount("de");
1352 1342
1353 // We'll do 4 times in incognito mode first to make sure the button is not 1343 // We'll do 4 times in incognito mode first to make sure the button is not
1354 // shown in that case, then 4 times in normal mode. 1344 // shown in that case, then 4 times in normal mode.
1355 TranslateInfoBarDelegate* infobar; 1345 TranslateInfoBarDelegate* infobar;
1356 TestingProfile* test_profile = 1346 TestingProfile* test_profile =
1357 static_cast<TestingProfile*>(web_contents()->GetBrowserContext()); 1347 static_cast<TestingProfile*>(web_contents()->GetBrowserContext());
1358 static_cast<extensions::TestExtensionSystem*>( 1348 static_cast<extensions::TestExtensionSystem*>(
1359 extensions::ExtensionSystem::Get(test_profile))-> 1349 extensions::ExtensionSystem::Get(test_profile))->CreateProcessManager();
1360 CreateProcessManager();
1361 test_profile->ForceIncognito(true); 1350 test_profile->ForceIncognito(true);
1362 for (int i = 0; i < 8; ++i) { 1351 for (int i = 0; i < 8; ++i) {
1363 SCOPED_TRACE(::testing::Message() << "Iteration " << i << 1352 SCOPED_TRACE(::testing::Message() << "Iteration " << i << " incognito mode="
1364 " incognito mode=" << test_profile->IsOffTheRecord()); 1353 << test_profile->IsOffTheRecord());
1365 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); 1354 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
1366 infobar = GetTranslateInfoBar(); 1355 infobar = GetTranslateInfoBar();
1367 ASSERT_TRUE(infobar != NULL); 1356 ASSERT_TRUE(infobar != NULL);
1368 EXPECT_EQ(TranslateTabHelper::BEFORE_TRANSLATE, infobar->translate_step()); 1357 EXPECT_EQ(TranslateTabHelper::BEFORE_TRANSLATE, infobar->translate_step());
1369 if (i < 7) { 1358 if (i < 7) {
1370 EXPECT_FALSE(infobar->ShouldShowAlwaysTranslateShortcut()); 1359 EXPECT_FALSE(infobar->ShouldShowAlwaysTranslateShortcut());
1371 infobar->Translate(); 1360 infobar->Translate();
1372 process()->sink().ClearMessages(); 1361 process()->sink().ClearMessages();
1373 } else { 1362 } else {
1374 EXPECT_TRUE(infobar->ShouldShowAlwaysTranslateShortcut()); 1363 EXPECT_TRUE(infobar->ShouldShowAlwaysTranslateShortcut());
(...skipping 10 matching lines...) Expand all
1385 // That should have triggered a page translate. 1374 // That should have triggered a page translate.
1386 int page_id = 0; 1375 int page_id = 0;
1387 std::string original_lang, target_lang; 1376 std::string original_lang, target_lang;
1388 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); 1377 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
1389 process()->sink().ClearMessages(); 1378 process()->sink().ClearMessages();
1390 1379
1391 // Now test that declining the translation causes a "never translate" button 1380 // Now test that declining the translation causes a "never translate" button
1392 // to be shown (in non incognito mode only). 1381 // to be shown (in non incognito mode only).
1393 test_profile->ForceIncognito(true); 1382 test_profile->ForceIncognito(true);
1394 for (int i = 0; i < 8; ++i) { 1383 for (int i = 0; i < 8; ++i) {
1395 SCOPED_TRACE(::testing::Message() << "Iteration " << i << 1384 SCOPED_TRACE(::testing::Message() << "Iteration " << i << " incognito mode="
1396 " incognito mode=" << test_profile->IsOffTheRecord()); 1385 << test_profile->IsOffTheRecord());
1397 SimulateNavigation(GURL("http://www.google.de"), "de", true); 1386 SimulateNavigation(GURL("http://www.google.de"), "de", true);
1398 infobar = GetTranslateInfoBar(); 1387 infobar = GetTranslateInfoBar();
1399 ASSERT_TRUE(infobar != NULL); 1388 ASSERT_TRUE(infobar != NULL);
1400 EXPECT_EQ(TranslateTabHelper::BEFORE_TRANSLATE, infobar->translate_step()); 1389 EXPECT_EQ(TranslateTabHelper::BEFORE_TRANSLATE, infobar->translate_step());
1401 if (i < 7) { 1390 if (i < 7) {
1402 EXPECT_FALSE(infobar->ShouldShowNeverTranslateShortcut()); 1391 EXPECT_FALSE(infobar->ShouldShowNeverTranslateShortcut());
1403 infobar->TranslationDeclined(); 1392 infobar->TranslationDeclined();
1404 } else { 1393 } else {
1405 EXPECT_TRUE(infobar->ShouldShowNeverTranslateShortcut()); 1394 EXPECT_TRUE(infobar->ShouldShowNeverTranslateShortcut());
1406 } 1395 }
1407 if (i == 3) 1396 if (i == 3)
1408 test_profile->ForceIncognito(false); 1397 test_profile->ForceIncognito(false);
1409 } 1398 }
1410 // Simulate the user pressing "Never translate French". 1399 // Simulate the user pressing "Never translate French".
1411 infobar->NeverTranslatePageLanguage(); 1400 infobar->NeverTranslatePageLanguage();
1412 EXPECT_TRUE(translate_prefs->IsBlockedLanguage("de")); 1401 EXPECT_TRUE(translate_prefs->IsBlockedLanguage("de"));
1413 // No translation should have occured and the infobar should be gone. 1402 // No translation should have occured and the infobar should be gone.
1414 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); 1403 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
1415 process()->sink().ClearMessages(); 1404 process()->sink().ClearMessages();
1416 ASSERT_TRUE(GetTranslateInfoBar() == NULL); 1405 ASSERT_TRUE(GetTranslateInfoBar() == NULL);
1417 } 1406 }
1418 1407
1419 // Tests that we don't show a translate infobar when a page instructs that it 1408 // Tests that we don't show a translate infobar when a page instructs that it
1420 // should not be translated. 1409 // should not be translated.
1421 TEST_F(TranslateManagerBrowserTest, NonTranslatablePage) { 1410 TEST_F(TranslateManagerRenderViewHostTest, NonTranslatablePage) {
1422 SimulateNavigation(GURL("http://mail.google.fr"), "fr", false); 1411 SimulateNavigation(GURL("http://mail.google.fr"), "fr", false);
1423 1412
1424 // We should not have an infobar. 1413 // We should not have an infobar.
1425 EXPECT_TRUE(GetTranslateInfoBar() == NULL); 1414 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
1426 1415
1427 // The context menu is enabled to allow users to force translation. 1416 // The context menu is enabled to allow users to force translation.
1428 scoped_ptr<TestRenderViewContextMenu> menu( 1417 scoped_ptr<TestRenderViewContextMenu> menu(
1429 TestRenderViewContextMenu::CreateContextMenu(web_contents())); 1418 TestRenderViewContextMenu::CreateContextMenu(web_contents()));
1430 menu->Init(); 1419 menu->Init();
1431 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); 1420 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
1432 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); 1421 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
1433 } 1422 }
1434 1423
1435 // Tests that the script is expired and refetched as expected. 1424 // Tests that the script is expired and refetched as expected.
1436 TEST_F(TranslateManagerBrowserTest, ScriptExpires) { 1425 TEST_F(TranslateManagerRenderViewHostTest, ScriptExpires) {
1437 ExpireTranslateScriptImmediately(); 1426 ExpireTranslateScriptImmediately();
1438 1427
1439 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); 1428 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
1440 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); 1429 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
1441 ASSERT_TRUE(infobar != NULL); 1430 ASSERT_TRUE(infobar != NULL);
1442 process()->sink().ClearMessages(); 1431 process()->sink().ClearMessages();
1443 infobar->Translate(); 1432 infobar->Translate();
1444 SimulateTranslateScriptURLFetch(true); 1433 SimulateTranslateScriptURLFetch(true);
1445 SimulateOnPageTranslated("fr", "en"); 1434 SimulateOnPageTranslated("fr", "en");
1446 1435
1447 // A task should have been posted to clear the script, run it. 1436 // A task should have been posted to clear the script, run it.
1448 base::MessageLoop::current()->RunUntilIdle(); 1437 base::MessageLoop::current()->RunUntilIdle();
1449 1438
1450 // Do another navigation and translation. 1439 // Do another navigation and translation.
1451 SimulateNavigation(GURL("http://www.google.es"), "es", true); 1440 SimulateNavigation(GURL("http://www.google.es"), "es", true);
1452 infobar = GetTranslateInfoBar(); 1441 infobar = GetTranslateInfoBar();
1453 ASSERT_TRUE(infobar != NULL); 1442 ASSERT_TRUE(infobar != NULL);
1454 process()->sink().ClearMessages(); 1443 process()->sink().ClearMessages();
1455 infobar->Translate(); 1444 infobar->Translate();
1456 // If we don't simulate the URL fetch, the TranslateManager should be waiting 1445 // If we don't simulate the URL fetch, the TranslateManager should be waiting
1457 // for the script and no message should have been sent to the renderer. 1446 // for the script and no message should have been sent to the renderer.
1458 EXPECT_TRUE( 1447 EXPECT_TRUE(process()->sink().GetFirstMessageMatching(
1459 process()->sink().GetFirstMessageMatching( 1448 ChromeViewMsg_TranslatePage::ID) == NULL);
1460 ChromeViewMsg_TranslatePage::ID) == NULL);
1461 // Now simulate the URL fetch. 1449 // Now simulate the URL fetch.
1462 SimulateTranslateScriptURLFetch(true); 1450 SimulateTranslateScriptURLFetch(true);
1463 // Now the message should have been sent. 1451 // Now the message should have been sent.
1464 int page_id = 0; 1452 int page_id = 0;
1465 std::string original_lang, target_lang; 1453 std::string original_lang, target_lang;
1466 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); 1454 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
1467 EXPECT_EQ("es", original_lang); 1455 EXPECT_EQ("es", original_lang);
1468 EXPECT_EQ("en", target_lang); 1456 EXPECT_EQ("en", target_lang);
1469 } 1457 }
1470 1458
1471 TEST_F(TranslateManagerBrowserTest, DownloadsAndHistoryNotTranslated) { 1459 TEST_F(TranslateManagerRenderViewHostTest, DownloadsAndHistoryNotTranslated) {
1472 ASSERT_FALSE(TranslateManager::IsTranslatableURL( 1460 ASSERT_FALSE(
1473 GURL(chrome::kChromeUIDownloadsURL))); 1461 TranslateManager::IsTranslatableURL(GURL(chrome::kChromeUIDownloadsURL)));
1474 ASSERT_FALSE(TranslateManager::IsTranslatableURL( 1462 ASSERT_FALSE(
1475 GURL(chrome::kChromeUIHistoryURL))); 1463 TranslateManager::IsTranslatableURL(GURL(chrome::kChromeUIHistoryURL)));
1476 } 1464 }
1477 1465
1478 #if defined(USE_AURA) 1466 #if defined(USE_AURA)
1479 1467
1480 TEST_F(TranslateManagerBrowserTest, BubbleNormalTranslate) { 1468 TEST_F(TranslateManagerRenderViewHostTest, BubbleNormalTranslate) {
1481 // Prepare for the bubble 1469 // Prepare for the bubble
1482 TranslateService::SetUseInfobar(false); 1470 TranslateService::SetUseInfobar(false);
1483 MockTranslateBubbleFactory* factory = new MockTranslateBubbleFactory; 1471 MockTranslateBubbleFactory* factory = new MockTranslateBubbleFactory;
1484 scoped_ptr<TranslateBubbleFactory> factory_ptr(factory); 1472 scoped_ptr<TranslateBubbleFactory> factory_ptr(factory);
1485 TranslateBubbleFactory::SetFactory(factory); 1473 TranslateBubbleFactory::SetFactory(factory);
1486 1474
1487 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); 1475 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
1488 1476
1489 // Check the bubble exists instead of the infobar. 1477 // Check the bubble exists instead of the infobar.
1490 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); 1478 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
(...skipping 20 matching lines...) Expand all
1511 // Simulate the render notifying the translation has been done. 1499 // Simulate the render notifying the translation has been done.
1512 SimulateOnPageTranslated("fr", "en"); 1500 SimulateOnPageTranslated("fr", "en");
1513 1501
1514 // Check the bubble shows "Translated." 1502 // Check the bubble shows "Translated."
1515 bubble = factory->model(); 1503 bubble = factory->model();
1516 ASSERT_TRUE(bubble != NULL); 1504 ASSERT_TRUE(bubble != NULL);
1517 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_AFTER_TRANSLATE, 1505 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_AFTER_TRANSLATE,
1518 bubble->GetViewState()); 1506 bubble->GetViewState());
1519 } 1507 }
1520 1508
1521 TEST_F(TranslateManagerBrowserTest, BubbleTranslateScriptNotAvailable) { 1509 TEST_F(TranslateManagerRenderViewHostTest, BubbleTranslateScriptNotAvailable) {
1522 // Prepare for the bubble 1510 // Prepare for the bubble
1523 TranslateService::SetUseInfobar(false); 1511 TranslateService::SetUseInfobar(false);
1524 MockTranslateBubbleFactory* factory = new MockTranslateBubbleFactory; 1512 MockTranslateBubbleFactory* factory = new MockTranslateBubbleFactory;
1525 scoped_ptr<TranslateBubbleFactory> factory_ptr(factory); 1513 scoped_ptr<TranslateBubbleFactory> factory_ptr(factory);
1526 TranslateBubbleFactory::SetFactory(factory); 1514 TranslateBubbleFactory::SetFactory(factory);
1527 1515
1528 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); 1516 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
1529 1517
1530 // Check the bubble exists instead of the infobar. 1518 // Check the bubble exists instead of the infobar.
1531 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); 1519 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
1532 ASSERT_TRUE(infobar == NULL); 1520 ASSERT_TRUE(infobar == NULL);
1533 TranslateBubbleModel* bubble = factory->model(); 1521 TranslateBubbleModel* bubble = factory->model();
1534 ASSERT_TRUE(bubble != NULL); 1522 ASSERT_TRUE(bubble != NULL);
1535 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE, 1523 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE,
1536 bubble->GetViewState()); 1524 bubble->GetViewState());
1537 1525
1538 // Simulate clicking translate. 1526 // Simulate clicking translate.
1539 process()->sink().ClearMessages(); 1527 process()->sink().ClearMessages();
1540 bubble->Translate(); 1528 bubble->Translate();
1541 SimulateTranslateScriptURLFetch(false); 1529 SimulateTranslateScriptURLFetch(false);
1542 1530
1543 // We should not have sent any message to translate to the renderer. 1531 // We should not have sent any message to translate to the renderer.
1544 EXPECT_FALSE(GetTranslateMessage(NULL, NULL, NULL)); 1532 EXPECT_FALSE(GetTranslateMessage(NULL, NULL, NULL));
1545 1533
1546 // And we should have an error infobar showing. 1534 // And we should have an error infobar showing.
1547 bubble = factory->model(); 1535 bubble = factory->model();
1548 ASSERT_TRUE(bubble != NULL); 1536 ASSERT_TRUE(bubble != NULL);
1549 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_ERROR, 1537 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_ERROR, bubble->GetViewState());
1550 bubble->GetViewState());
1551 } 1538 }
1552 1539
1553 TEST_F(TranslateManagerBrowserTest, BubbleUnknownLanguage) { 1540 TEST_F(TranslateManagerRenderViewHostTest, BubbleUnknownLanguage) {
1554 // Prepare for the bubble 1541 // Prepare for the bubble
1555 TranslateService::SetUseInfobar(false); 1542 TranslateService::SetUseInfobar(false);
1556 MockTranslateBubbleFactory* factory = new MockTranslateBubbleFactory; 1543 MockTranslateBubbleFactory* factory = new MockTranslateBubbleFactory;
1557 scoped_ptr<TranslateBubbleFactory> factory_ptr(factory); 1544 scoped_ptr<TranslateBubbleFactory> factory_ptr(factory);
1558 TranslateBubbleFactory::SetFactory(factory); 1545 TranslateBubbleFactory::SetFactory(factory);
1559 1546
1560 // Simulate navigating to a page ("und" is the string returned by the CLD for 1547 // Simulate navigating to a page ("und" is the string returned by the CLD for
1561 // languages it does not recognize). 1548 // languages it does not recognize).
1562 SimulateNavigation(GURL("http://www.google.mys"), "und", true); 1549 SimulateNavigation(GURL("http://www.google.mys"), "und", true);
1563 1550
1564 // We should not have a bubble as we don't know the language. 1551 // We should not have a bubble as we don't know the language.
1565 ASSERT_TRUE(factory->model() == NULL); 1552 ASSERT_TRUE(factory->model() == NULL);
1566 1553
1567 // Translate the page anyway throught the context menu. 1554 // Translate the page anyway throught the context menu.
1568 scoped_ptr<TestRenderViewContextMenu> menu( 1555 scoped_ptr<TestRenderViewContextMenu> menu(
1569 TestRenderViewContextMenu::CreateContextMenu(web_contents())); 1556 TestRenderViewContextMenu::CreateContextMenu(web_contents()));
1570 menu->Init(); 1557 menu->Init();
1571 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0); 1558 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0);
1572 1559
1573 // Check the bubble exists instead of the infobar. 1560 // Check the bubble exists instead of the infobar.
1574 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); 1561 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
1575 ASSERT_TRUE(infobar == NULL); 1562 ASSERT_TRUE(infobar == NULL);
1576 TranslateBubbleModel* bubble = factory->model(); 1563 TranslateBubbleModel* bubble = factory->model();
1577 ASSERT_TRUE(bubble != NULL); 1564 ASSERT_TRUE(bubble != NULL);
1578 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_TRANSLATING, 1565 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_TRANSLATING,
1579 bubble->GetViewState()); 1566 bubble->GetViewState());
1580 } 1567 }
1581 1568
1582 #endif // defined(USE_AURA) 1569 #endif // defined(USE_AURA)
1583
1584 // Test is flaky on Win http://crbug.com/166334
1585 #if defined(OS_WIN)
1586 #define MAYBE_PRE_TranslateSessionRestore DISABLED_PRE_TranslateSessionRestore
1587 #else
1588 #define MAYBE_PRE_TranslateSessionRestore PRE_TranslateSessionRestore
1589 #endif
1590 // Test that session restore restores the translate infobar and other translate
1591 // settings.
1592 IN_PROC_BROWSER_TEST_F(InProcessBrowserTest,
1593 MAYBE_PRE_TranslateSessionRestore) {
1594 SessionStartupPref pref(SessionStartupPref::LAST);
1595 SessionStartupPref::SetStartupPref(browser()->profile(), pref);
1596
1597 content::WebContents* current_web_contents =
1598 browser()->tab_strip_model()->GetActiveWebContents();
1599 TranslateTabHelper* translate_tab_helper =
1600 TranslateTabHelper::FromWebContents(current_web_contents);
1601 content::Source<content::WebContents> source(current_web_contents);
1602
1603 ui_test_utils::WindowedNotificationObserverWithDetails<
1604 LanguageDetectionDetails>
1605 fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED,
1606 source);
1607
1608 GURL french_url = ui_test_utils::GetTestUrl(
1609 base::FilePath(), base::FilePath(FILE_PATH_LITERAL("french_page.html")));
1610 ui_test_utils::NavigateToURL(browser(), french_url);
1611 fr_language_detected_signal.Wait();
1612 LanguageDetectionDetails details;
1613 EXPECT_TRUE(fr_language_detected_signal.GetDetailsFor(
1614 source.map_key(), &details));
1615 EXPECT_EQ("fr", details.adopted_language);
1616 EXPECT_EQ("fr", translate_tab_helper->GetLanguageState().original_language());
1617 }
1618
1619 #if defined (OS_WIN)
1620 #define MAYBE_TranslateSessionRestore DISABLED_TranslateSessionRestore
1621 #else
1622 #define MAYBE_TranslateSessionRestore TranslateSessionRestore
1623 #endif
1624 IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, MAYBE_TranslateSessionRestore) {
1625 content::WebContents* current_web_contents =
1626 browser()->tab_strip_model()->GetActiveWebContents();
1627 content::Source<content::WebContents> source(current_web_contents);
1628
1629 ui_test_utils::WindowedNotificationObserverWithDetails<
1630 LanguageDetectionDetails>
1631 fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED,
1632 source);
1633 fr_language_detected_signal.Wait();
1634 }
OLDNEW
« no previous file with comments | « chrome/browser/translate/translate_manager_browsertest.cc ('k') | chrome/browser/translate/translate_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698