blob: 569f4f806ce646758b3db2c98b9972d5ceb61e0a [file] [log] [blame]
[email protected]2253b3b2012-06-03 22:39:151// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]2253b3b2012-06-03 22:39:155#include <algorithm>
6#include <set>
7#include <vector>
8
[email protected]e625b7602013-10-28 09:24:569#include "base/command_line.h"
[email protected]2253b3b2012-06-03 22:39:1510#include "base/memory/scoped_ptr.h"
[email protected]1ab137b2013-03-21 03:33:1811#include "base/prefs/pref_change_registrar.h"
[email protected]3853a4c2013-02-11 17:15:5712#include "base/prefs/pref_service.h"
[email protected]9f0abdb2013-06-10 21:49:3413#include "base/strings/stringprintf.h"
[email protected]2253b3b2012-06-03 22:39:1514#include "chrome/app/chrome_command_ids.h"
[email protected]dcc8fbc2013-07-12 00:54:0915#include "chrome/browser/chrome_notification_types.h"
[email protected]2253b3b2012-06-03 22:39:1516#include "chrome/browser/extensions/test_extension_system.h"
17#include "chrome/browser/infobars/infobar.h"
[email protected]4a8adfa02013-03-19 22:37:4618#include "chrome/browser/infobars/infobar_service.h"
[email protected]f8a2e132012-08-31 18:16:2019#include "chrome/browser/prefs/session_startup_pref.h"
[email protected]2253b3b2012-06-03 22:39:1520#include "chrome/browser/tab_contents/render_view_context_menu.h"
21#include "chrome/browser/translate/translate_infobar_delegate.h"
[email protected]4df61c702013-05-28 10:55:3022#include "chrome/browser/translate/translate_language_list.h"
[email protected]2253b3b2012-06-03 22:39:1523#include "chrome/browser/translate/translate_manager.h"
24#include "chrome/browser/translate/translate_prefs.h"
[email protected]05024712013-07-31 09:46:2925#include "chrome/browser/translate/translate_script.h"
[email protected]bb1c86cd2014-01-29 18:22:1826#include "chrome/browser/translate/translate_service.h"
[email protected]f8a2e132012-08-31 18:16:2027#include "chrome/browser/translate/translate_tab_helper.h"
[email protected]f8a2e132012-08-31 18:16:2028#include "chrome/browser/ui/browser.h"
[email protected]47ae23372013-01-29 01:50:4829#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]e625b7602013-10-28 09:24:5630#include "chrome/browser/ui/translate/translate_bubble_factory.h"
31#include "chrome/browser/ui/translate/translate_bubble_model.h"
32#include "chrome/browser/ui/translate/translate_bubble_model_impl.h"
33#include "chrome/common/chrome_switches.h"
[email protected]2253b3b2012-06-03 22:39:1534#include "chrome/common/pref_names.h"
35#include "chrome/common/render_messages.h"
[email protected]f8a2e132012-08-31 18:16:2036#include "chrome/common/url_constants.h"
[email protected]292ccc92012-10-17 12:46:0237#include "chrome/test/base/chrome_render_view_host_test_harness.h"
[email protected]f8a2e132012-08-31 18:16:2038#include "chrome/test/base/in_process_browser_test.h"
[email protected]2253b3b2012-06-03 22:39:1539#include "chrome/test/base/testing_browser_process.h"
40#include "chrome/test/base/testing_profile.h"
[email protected]f8a2e132012-08-31 18:16:2041#include "chrome/test/base/ui_test_utils.h"
[email protected]bb1c86cd2014-01-29 18:22:1842#include "components/translate/core/browser/translate_download_manager.h"
[email protected]eba93c92014-01-07 17:34:1743#include "components/translate/core/common/language_detection_details.h"
[email protected]2253b3b2012-06-03 22:39:1544#include "content/public/browser/navigation_details.h"
45#include "content/public/browser/navigation_entry.h"
46#include "content/public/browser/notification_details.h"
47#include "content/public/browser/notification_registrar.h"
48#include "content/public/browser/web_contents.h"
[email protected]2253b3b2012-06-03 22:39:1549#include "content/public/test/mock_render_process_host.h"
[email protected]b1e3f202012-06-04 14:45:5050#include "content/public/test/test_renderer_host.h"
[email protected]8f2d8bdd2012-06-19 23:44:0551#include "net/url_request/test_url_fetcher_factory.h"
[email protected]140930a2013-06-21 16:11:0952#include "net/url_request/url_fetcher_delegate.h"
[email protected]2253b3b2012-06-03 22:39:1553#include "testing/gmock/include/gmock/gmock.h"
[email protected]2255a9332013-06-17 05:12:3154#include "third_party/WebKit/public/web/WebContextMenuData.h"
[email protected]2253b3b2012-06-03 22:39:1555
[email protected]2253b3b2012-06-03 22:39:1556
[email protected]f8a2e132012-08-31 18:16:2057// An observer that keeps track of whether a navigation entry was committed.
58class NavEntryCommittedObserver : public content::NotificationObserver {
59 public:
[email protected]05024712013-07-31 09:46:2960 explicit NavEntryCommittedObserver(content::WebContents* web_contents) {
[email protected]f8a2e132012-08-31 18:16:2061 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
[email protected]05024712013-07-31 09:46:2962 content::Source<content::NavigationController>(
63 &web_contents->GetController()));
[email protected]f8a2e132012-08-31 18:16:2064 }
65
66 virtual void Observe(int type,
67 const content::NotificationSource& source,
[email protected]b94584a2013-02-07 03:02:0868 const content::NotificationDetails& details) OVERRIDE {
[email protected]f8a2e132012-08-31 18:16:2069 DCHECK(type == content::NOTIFICATION_NAV_ENTRY_COMMITTED);
70 details_ =
71 *(content::Details<content::LoadCommittedDetails>(details).ptr());
72 }
73
[email protected]05024712013-07-31 09:46:2974 const content::LoadCommittedDetails& load_committed_details() const {
[email protected]f8a2e132012-08-31 18:16:2075 return details_;
76 }
77
78 private:
79 content::LoadCommittedDetails details_;
80 content::NotificationRegistrar registrar_;
81
82 DISALLOW_COPY_AND_ASSIGN(NavEntryCommittedObserver);
83};
84
[email protected]67755672013-04-08 23:25:3285class TranslateManagerBrowserTest : public ChromeRenderViewHostTestHarness,
86 public content::NotificationObserver {
[email protected]2253b3b2012-06-03 22:39:1587 public:
[email protected]67755672013-04-08 23:25:3288 TranslateManagerBrowserTest()
89 : pref_callback_(
90 base::Bind(&TranslateManagerBrowserTest::OnPreferenceChanged,
[email protected]ec04d3f2013-06-06 21:31:3991 base::Unretained(this))) {
[email protected]2253b3b2012-06-03 22:39:1592 }
93
94 // Simulates navigating to a page and getting the page contents and language
95 // for that navigation.
96 void SimulateNavigation(const GURL& url,
97 const std::string& lang,
98 bool page_translatable) {
99 NavigateAndCommit(url);
100 SimulateOnTranslateLanguageDetermined(lang, page_translatable);
101 }
102
103 void SimulateOnTranslateLanguageDetermined(const std::string& lang,
104 bool page_translatable) {
[email protected]27eff892013-05-21 16:40:52105 LanguageDetectionDetails details;
106 details.adopted_language = lang;
[email protected]2fa6318d2013-12-02 22:10:56107 content::RenderViewHostTester::TestOnMessageReceived(
[email protected]2253b3b2012-06-03 22:39:15108 rvh(),
109 ChromeViewHostMsg_TranslateLanguageDetermined(
[email protected]27eff892013-05-21 16:40:52110 0, details, page_translatable));
[email protected]2253b3b2012-06-03 22:39:15111 }
112
[email protected]05024712013-07-31 09:46:29113 void SimulateOnPageTranslated(int routing_id,
114 const std::string& source_lang,
115 const std::string& target_lang,
116 TranslateErrors::Type error) {
[email protected]2fa6318d2013-12-02 22:10:56117 content::RenderViewHostTester::TestOnMessageReceived(
[email protected]05024712013-07-31 09:46:29118 rvh(),
119 ChromeViewHostMsg_PageTranslated(
120 routing_id, 0, source_lang, target_lang, error));
121 }
122
123 void SimulateOnPageTranslated(const std::string& source_lang,
124 const std::string& target_lang) {
125 SimulateOnPageTranslated(0, source_lang, target_lang,
126 TranslateErrors::NONE);
127 }
128
[email protected]2253b3b2012-06-03 22:39:15129 bool GetTranslateMessage(int* page_id,
130 std::string* original_lang,
131 std::string* target_lang) {
132 const IPC::Message* message =
133 process()->sink().GetFirstMessageMatching(
134 ChromeViewMsg_TranslatePage::ID);
135 if (!message)
136 return false;
137 Tuple4<int, std::string, std::string, std::string> translate_param;
138 ChromeViewMsg_TranslatePage::Read(message, &translate_param);
139 if (page_id)
140 *page_id = translate_param.a;
141 // Ignore translate_param.b which is the script injected in the page.
142 if (original_lang)
143 *original_lang = translate_param.c;
144 if (target_lang)
145 *target_lang = translate_param.d;
146 return true;
147 }
148
[email protected]4f822f022012-12-20 19:11:42149 InfoBarService* infobar_service() {
150 return InfoBarService::FromWebContents(web_contents());
[email protected]2253b3b2012-06-03 22:39:15151 }
152
153 // Returns the translate infobar if there is 1 infobar and it is a translate
154 // infobar.
155 TranslateInfoBarDelegate* GetTranslateInfoBar() {
[email protected]a608d172013-04-23 22:27:37156 return (infobar_service()->infobar_count() == 1) ?
[email protected]39308cb2013-12-06 03:01:48157 infobar_service()->infobar_at(0)->delegate()->
158 AsTranslateInfoBarDelegate() : NULL;
[email protected]2253b3b2012-06-03 22:39:15159 }
160
161 // If there is 1 infobar and it is a translate infobar, closes it and returns
162 // true. Returns false otherwise.
163 bool CloseTranslateInfoBar() {
164 InfoBarDelegate* infobar = GetTranslateInfoBar();
165 if (!infobar)
166 return false;
167 infobar->InfoBarDismissed(); // Simulates closing the infobar.
[email protected]39308cb2013-12-06 03:01:48168 infobar_service()->RemoveInfoBar(infobar_service()->infobar_at(0));
[email protected]2253b3b2012-06-03 22:39:15169 return true;
170 }
171
172 // Checks whether |infobar| has been removed and clears the removed infobar
173 // list.
174 bool CheckInfoBarRemovedAndReset(InfoBarDelegate* delegate) {
175 bool found = removed_infobars_.count(delegate) != 0;
176 removed_infobars_.clear();
177 return found;
178 }
179
[email protected]2253b3b2012-06-03 22:39:15180 void ExpireTranslateScriptImmediately() {
[email protected]140930a2013-06-21 16:11:09181 TranslateManager::GetInstance()->SetTranslateScriptExpirationDelay(0);
[email protected]2253b3b2012-06-03 22:39:15182 }
183
184 // If there is 1 infobar and it is a translate infobar, deny translation and
185 // returns true. Returns false otherwise.
186 bool DenyTranslation() {
187 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
188 if (!infobar)
189 return false;
190 infobar->TranslationDeclined();
[email protected]39308cb2013-12-06 03:01:48191 infobar_service()->RemoveInfoBar(infobar_service()->infobar_at(0));
[email protected]2253b3b2012-06-03 22:39:15192 return true;
193 }
194
[email protected]b637bcd2013-03-28 12:50:32195 void ReloadAndWait(bool successful_reload) {
[email protected]921441c2012-10-19 22:16:01196 NavEntryCommittedObserver nav_observer(web_contents());
[email protected]b637bcd2013-03-28 12:50:32197 if (successful_reload)
198 Reload();
199 else
200 FailedReload();
[email protected]f8a2e132012-08-31 18:16:20201
202 // Ensures it is really handled a reload.
203 const content::LoadCommittedDetails& nav_details =
[email protected]05024712013-07-31 09:46:29204 nav_observer.load_committed_details();
[email protected]f8a2e132012-08-31 18:16:20205 EXPECT_TRUE(nav_details.entry != NULL); // There was a navigation.
206 EXPECT_EQ(content::NAVIGATION_TYPE_EXISTING_PAGE, nav_details.type);
207
208 // The TranslateManager class processes the navigation entry committed
209 // notification in a posted task; process that task.
[email protected]b3a25092013-05-28 22:08:16210 base::MessageLoop::current()->RunUntilIdle();
[email protected]f8a2e132012-08-31 18:16:20211 }
212
[email protected]2253b3b2012-06-03 22:39:15213 virtual void Observe(int type,
214 const content::NotificationSource& source,
215 const content::NotificationDetails& details) {
216 DCHECK_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, type);
217 removed_infobars_.insert(
[email protected]39308cb2013-12-06 03:01:48218 content::Details<InfoBar::RemovedDetails>(details)->first->delegate());
[email protected]2253b3b2012-06-03 22:39:15219 }
220
[email protected]116129e02012-11-21 17:26:27221 MOCK_METHOD1(OnPreferenceChanged, void(const std::string&));
[email protected]a6a7ced2012-11-01 17:24:18222
[email protected]2253b3b2012-06-03 22:39:15223 protected:
224 virtual void SetUp() {
[email protected]bc186032013-12-09 19:15:23225 // This test is a unit test but runs in the browser_tests suite. Therefore
226 // it needs to manage its own TestingBrowserProcess.
227 // TODO(jamescook): Figure out how to move this suite back to unit_tests.
228 // Right now it fails to get the translate infobar if you run it there.
229 TestingBrowserProcess::CreateInstance();
[email protected]2253b3b2012-06-03 22:39:15230 // Access the TranslateManager singleton so it is created before we call
[email protected]292ccc92012-10-17 12:46:02231 // ChromeRenderViewHostTestHarness::SetUp() to match what's done in Chrome,
[email protected]2253b3b2012-06-03 22:39:15232 // where the TranslateManager is created before the WebContents. This
233 // matters as they both register for similar events and we want the
234 // notifications to happen in the same sequence (TranslateManager first,
235 // WebContents second). Also clears the translate script so it is fetched
236 // everytime and sets the expiration delay to a large value by default (in
237 // case it was zeroed in a previous test).
[email protected]bb1c86cd2014-01-29 18:22:18238 TranslateService::Initialize();
[email protected]2253b3b2012-06-03 22:39:15239 TranslateManager::GetInstance()->ClearTranslateScript();
240 TranslateManager::GetInstance()->
[email protected]140930a2013-06-21 16:11:09241 SetTranslateScriptExpirationDelay(60 * 60 * 1000);
[email protected]9ad06a12013-05-10 14:49:53242 TranslateManager::GetInstance()->set_translate_max_reload_attemps(0);
[email protected]2253b3b2012-06-03 22:39:15243
[email protected]292ccc92012-10-17 12:46:02244 ChromeRenderViewHostTestHarness::SetUp();
[email protected]4f822f022012-12-20 19:11:42245 InfoBarService::CreateForWebContents(web_contents());
[email protected]292ccc92012-10-17 12:46:02246 TranslateTabHelper::CreateForWebContents(web_contents());
[email protected]2253b3b2012-06-03 22:39:15247
248 notification_registrar_.Add(this,
249 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
[email protected]4f822f022012-12-20 19:11:42250 content::Source<InfoBarService>(infobar_service()));
[email protected]2253b3b2012-06-03 22:39:15251 }
252
253 virtual void TearDown() {
254 process()->sink().ClearMessages();
255
256 notification_registrar_.Remove(this,
257 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
[email protected]4f822f022012-12-20 19:11:42258 content::Source<InfoBarService>(infobar_service()));
[email protected]2253b3b2012-06-03 22:39:15259
[email protected]292ccc92012-10-17 12:46:02260 ChromeRenderViewHostTestHarness::TearDown();
[email protected]bc186032013-12-09 19:15:23261 TestingBrowserProcess::DeleteInstance();
[email protected]2253b3b2012-06-03 22:39:15262 }
263
[email protected]bb1c86cd2014-01-29 18:22:18264 void SetApplicationLocale(const std::string& locale) {
265 g_browser_process->SetApplicationLocale(locale);
266 TranslateDownloadManager::GetInstance()->set_application_locale(
267 g_browser_process->GetApplicationLocale());
268 }
269
[email protected]2253b3b2012-06-03 22:39:15270 void SimulateTranslateScriptURLFetch(bool success) {
[email protected]05024712013-07-31 09:46:29271 net::TestURLFetcher* fetcher =
272 url_fetcher_factory_.GetFetcherByID(TranslateScript::kFetcherId);
[email protected]2253b3b2012-06-03 22:39:15273 ASSERT_TRUE(fetcher);
274 net::URLRequestStatus status;
275 status.set_status(success ? net::URLRequestStatus::SUCCESS :
276 net::URLRequestStatus::FAILED);
277 fetcher->set_url(fetcher->GetOriginalURL());
278 fetcher->set_status(status);
279 fetcher->set_response_code(success ? 200 : 500);
280 fetcher->delegate()->OnURLFetchComplete(fetcher);
281 }
282
283 void SimulateSupportedLanguagesURLFetch(
[email protected]7da697a2013-07-12 08:09:46284 bool success,
285 const std::vector<std::string>& languages,
286 bool use_alpha_languages,
287 const std::vector<std::string>& alpha_languages) {
[email protected]2253b3b2012-06-03 22:39:15288 net::URLRequestStatus status;
289 status.set_status(success ? net::URLRequestStatus::SUCCESS :
290 net::URLRequestStatus::FAILED);
291
292 std::string data;
293 if (success) {
[email protected]4df61c702013-05-28 10:55:30294 data = base::StringPrintf(
295 "%s{\"sl\": {\"bla\": \"bla\"}, \"%s\": {",
296 TranslateLanguageList::kLanguageListCallbackName,
297 TranslateLanguageList::kTargetLanguagesKey);
[email protected]2253b3b2012-06-03 22:39:15298 const char* comma = "";
299 for (size_t i = 0; i < languages.size(); ++i) {
300 data += base::StringPrintf(
[email protected]2a4e1ae2013-04-15 14:41:36301 "%s\"%s\": \"UnusedFullName\"", comma, languages[i].c_str());
[email protected]2253b3b2012-06-03 22:39:15302 if (i == 0)
303 comma = ",";
304 }
[email protected]7da697a2013-07-12 08:09:46305
306 if (use_alpha_languages) {
307 data += base::StringPrintf("},\"%s\": {",
308 TranslateLanguageList::kAlphaLanguagesKey);
309 comma = "";
310 for (size_t i = 0; i < alpha_languages.size(); ++i) {
311 data += base::StringPrintf("%s\"%s\": 1", comma,
312 alpha_languages[i].c_str());
313 if (i == 0)
314 comma = ",";
315 }
316 }
317
[email protected]2253b3b2012-06-03 22:39:15318 data += "}})";
319 }
[email protected]05024712013-07-31 09:46:29320 net::TestURLFetcher* fetcher =
321 url_fetcher_factory_.GetFetcherByID(TranslateLanguageList::kFetcherId);
[email protected]7da697a2013-07-12 08:09:46322 ASSERT_TRUE(fetcher != NULL);
323 fetcher->set_url(fetcher->GetOriginalURL());
324 fetcher->set_status(status);
325 fetcher->set_response_code(success ? 200 : 500);
326 fetcher->SetResponseString(data);
327 fetcher->delegate()->OnURLFetchComplete(fetcher);
[email protected]2253b3b2012-06-03 22:39:15328 }
329
330 void SetPrefObserverExpectation(const char* path) {
[email protected]116129e02012-11-21 17:26:27331 EXPECT_CALL(*this, OnPreferenceChanged(std::string(path)));
[email protected]2253b3b2012-06-03 22:39:15332 }
333
[email protected]116129e02012-11-21 17:26:27334 PrefChangeRegistrar::NamedChangeCallback pref_callback_;
335
[email protected]2253b3b2012-06-03 22:39:15336 private:
337 content::NotificationRegistrar notification_registrar_;
[email protected]8f2d8bdd2012-06-19 23:44:05338 net::TestURLFetcherFactory url_fetcher_factory_;
[email protected]2253b3b2012-06-03 22:39:15339
340 // The infobars that have been removed.
341 // WARNING: the pointers point to deleted objects, use only for comparison.
342 std::set<InfoBarDelegate*> removed_infobars_;
343
[email protected]67755672013-04-08 23:25:32344 DISALLOW_COPY_AND_ASSIGN(TranslateManagerBrowserTest);
[email protected]2253b3b2012-06-03 22:39:15345};
346
[email protected]e625b7602013-10-28 09:24:56347class MockTranslateBubbleFactory : public TranslateBubbleFactory {
348 public:
349 MockTranslateBubbleFactory() {
350 }
351
352 virtual void ShowImplementation(
353 BrowserWindow* window,
354 content::WebContents* web_contents,
[email protected]6d37ab22013-12-03 11:18:30355 TranslateBubbleModel::ViewState view_state,
356 TranslateErrors::Type error_type) OVERRIDE {
[email protected]e625b7602013-10-28 09:24:56357 if (model_) {
358 model_->SetViewState(view_state);
359 return;
360 }
361
362 TranslateTabHelper* translate_tab_helper =
363 TranslateTabHelper::FromWebContents(web_contents);
364 std::string source_language =
[email protected]3155c5272014-01-21 12:38:12365 translate_tab_helper->GetLanguageState().original_language();
[email protected]e625b7602013-10-28 09:24:56366 std::string target_language = TranslateManager::GetLanguageCode(
367 g_browser_process->GetApplicationLocale());
368 scoped_ptr<TranslateUIDelegate> ui_delegate(
369 new TranslateUIDelegate(web_contents,
370 source_language,
[email protected]83fa875a2013-12-12 17:05:28371 target_language));
[email protected]e625b7602013-10-28 09:24:56372 model_.reset(
373 new TranslateBubbleModelImpl(view_state, ui_delegate.Pass()));
374 }
375
376 TranslateBubbleModel* model() { return model_.get(); }
377
378 private:
379 scoped_ptr<TranslateBubbleModel> model_;
380
381 DISALLOW_COPY_AND_ASSIGN(MockTranslateBubbleFactory);
382};
383
[email protected]2253b3b2012-06-03 22:39:15384namespace {
385
386class TestRenderViewContextMenu : public RenderViewContextMenu {
387 public:
388 static TestRenderViewContextMenu* CreateContextMenu(
[email protected]05024712013-07-31 09:46:29389 content::WebContents* web_contents) {
[email protected]2253b3b2012-06-03 22:39:15390 content::ContextMenuParams params;
[email protected]a1221aea2013-11-07 01:31:30391 params.media_type = blink::WebContextMenuData::MediaTypeNone;
[email protected]2253b3b2012-06-03 22:39:15392 params.x = 0;
393 params.y = 0;
[email protected]6462f81112013-08-23 23:00:37394 params.has_image_contents = true;
[email protected]2253b3b2012-06-03 22:39:15395 params.media_flags = 0;
396 params.spellcheck_enabled = false;
397 params.is_editable = false;
398 params.page_url = web_contents->GetController().GetActiveEntry()->GetURL();
399#if defined(OS_MACOSX)
400 params.writing_direction_default = 0;
401 params.writing_direction_left_to_right = 0;
402 params.writing_direction_right_to_left = 0;
403#endif // OS_MACOSX
[email protected]a1221aea2013-11-07 01:31:30404 params.edit_flags = blink::WebContextMenuData::CanTranslate;
[email protected]a09d53ce2014-01-31 00:46:42405 return new TestRenderViewContextMenu(web_contents->GetMainFrame(), params);
[email protected]2253b3b2012-06-03 22:39:15406 }
407
408 bool IsItemPresent(int id) {
409 return menu_model_.GetIndexOfCommandId(id) != -1;
410 }
411
[email protected]b94584a2013-02-07 03:02:08412 virtual void PlatformInit() OVERRIDE { }
413 virtual void PlatformCancel() OVERRIDE { }
[email protected]2253b3b2012-06-03 22:39:15414 virtual bool GetAcceleratorForCommandId(
415 int command_id,
[email protected]b94584a2013-02-07 03:02:08416 ui::Accelerator* accelerator) OVERRIDE { return false; }
[email protected]2253b3b2012-06-03 22:39:15417
418 private:
[email protected]a09d53ce2014-01-31 00:46:42419 TestRenderViewContextMenu(content::RenderFrameHost* render_frame_host,
[email protected]2253b3b2012-06-03 22:39:15420 const content::ContextMenuParams& params)
[email protected]a09d53ce2014-01-31 00:46:42421 : RenderViewContextMenu(render_frame_host, params) {
[email protected]2253b3b2012-06-03 22:39:15422 }
423
424 DISALLOW_COPY_AND_ASSIGN(TestRenderViewContextMenu);
425};
426
427} // namespace
428
[email protected]67755672013-04-08 23:25:32429TEST_F(TranslateManagerBrowserTest, NormalTranslate) {
[email protected]2253b3b2012-06-03 22:39:15430 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
431
432 // We should have an infobar.
433 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
434 ASSERT_TRUE(infobar != NULL);
[email protected]2ebd22d2013-01-07 17:04:27435 EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE,
436 infobar->infobar_type());
[email protected]2253b3b2012-06-03 22:39:15437
438 // Simulate clicking translate.
439 process()->sink().ClearMessages();
440 infobar->Translate();
441
442 // The "Translating..." infobar should be showing.
443 infobar = GetTranslateInfoBar();
444 ASSERT_TRUE(infobar != NULL);
[email protected]2ebd22d2013-01-07 17:04:27445 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATING, infobar->infobar_type());
[email protected]2253b3b2012-06-03 22:39:15446
447 // Simulate the translate script being retrieved (it only needs to be done
448 // once in the test as it is cached).
449 SimulateTranslateScriptURLFetch(true);
450
451 // Test that we sent the right message to the renderer.
452 int page_id = 0;
453 std::string original_lang, target_lang;
454 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
455 EXPECT_EQ("fr", original_lang);
456 EXPECT_EQ("en", target_lang);
457
458 // Simulate the render notifying the translation has been done.
[email protected]05024712013-07-31 09:46:29459 SimulateOnPageTranslated("fr", "en");
[email protected]2253b3b2012-06-03 22:39:15460
461 // The after translate infobar should be showing.
462 infobar = GetTranslateInfoBar();
463 ASSERT_TRUE(infobar != NULL);
[email protected]2ebd22d2013-01-07 17:04:27464 EXPECT_EQ(TranslateInfoBarDelegate::AFTER_TRANSLATE, infobar->infobar_type());
[email protected]2253b3b2012-06-03 22:39:15465
[email protected]9f943482012-09-21 01:05:13466 // Simulate changing the original language and translating.
[email protected]2253b3b2012-06-03 22:39:15467 process()->sink().ClearMessages();
[email protected]9f943482012-09-21 01:05:13468 std::string new_original_lang = infobar->language_code_at(0);
[email protected]3e1c8392013-10-31 09:19:01469 infobar->UpdateOriginalLanguageIndex(0);
[email protected]9f943482012-09-21 01:05:13470 infobar->Translate();
[email protected]2253b3b2012-06-03 22:39:15471 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
472 EXPECT_EQ(new_original_lang, original_lang);
473 EXPECT_EQ("en", target_lang);
474 // Simulate the render notifying the translation has been done.
[email protected]05024712013-07-31 09:46:29475 SimulateOnPageTranslated(new_original_lang, "en");
[email protected]2fa6318d2013-12-02 22:10:56476 infobar = GetTranslateInfoBar();
477 ASSERT_TRUE(infobar != NULL);
[email protected]2253b3b2012-06-03 22:39:15478
[email protected]9f943482012-09-21 01:05:13479 // Simulate changing the target language and translating.
[email protected]2253b3b2012-06-03 22:39:15480 process()->sink().ClearMessages();
[email protected]9f943482012-09-21 01:05:13481 std::string new_target_lang = infobar->language_code_at(1);
[email protected]3e1c8392013-10-31 09:19:01482 infobar->UpdateTargetLanguageIndex(1);
[email protected]9f943482012-09-21 01:05:13483 infobar->Translate();
[email protected]2253b3b2012-06-03 22:39:15484 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
485 EXPECT_EQ(new_original_lang, original_lang);
486 EXPECT_EQ(new_target_lang, target_lang);
487 // Simulate the render notifying the translation has been done.
[email protected]05024712013-07-31 09:46:29488 SimulateOnPageTranslated(new_original_lang, new_target_lang);
[email protected]2fa6318d2013-12-02 22:10:56489 infobar = GetTranslateInfoBar();
490 ASSERT_TRUE(infobar != NULL);
491 EXPECT_EQ(new_target_lang, infobar->target_language_code());
[email protected]f8a2e132012-08-31 18:16:20492
[email protected]b022b9492013-11-26 19:40:04493 // Reloading should trigger translation iff Always Translate is on.
[email protected]b637bcd2013-03-28 12:50:32494 ReloadAndWait(true);
[email protected]2fa6318d2013-12-02 22:10:56495 infobar = GetTranslateInfoBar();
496 ASSERT_TRUE(infobar != NULL);
[email protected]b022b9492013-11-26 19:40:04497 EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE,
498 infobar->infobar_type());
499 infobar->UpdateTargetLanguageIndex(1);
500 infobar->ToggleAlwaysTranslate();
501 ReloadAndWait(true);
[email protected]2fa6318d2013-12-02 22:10:56502 infobar = GetTranslateInfoBar();
503 ASSERT_TRUE(infobar != NULL);
[email protected]b022b9492013-11-26 19:40:04504 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATING, infobar->infobar_type());
505 EXPECT_EQ(new_target_lang, infobar->target_language_code());
[email protected]2253b3b2012-06-03 22:39:15506}
507
[email protected]67755672013-04-08 23:25:32508TEST_F(TranslateManagerBrowserTest, TranslateScriptNotAvailable) {
[email protected]2253b3b2012-06-03 22:39:15509 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
510
511 // We should have an infobar.
512 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
513 ASSERT_TRUE(infobar != NULL);
[email protected]2ebd22d2013-01-07 17:04:27514 EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE,
515 infobar->infobar_type());
[email protected]2253b3b2012-06-03 22:39:15516
517 // Simulate clicking translate.
518 process()->sink().ClearMessages();
519 infobar->Translate();
[email protected]2253b3b2012-06-03 22:39:15520 SimulateTranslateScriptURLFetch(false);
521
522 // We should not have sent any message to translate to the renderer.
523 EXPECT_FALSE(GetTranslateMessage(NULL, NULL, NULL));
524
525 // And we should have an error infobar showing.
526 infobar = GetTranslateInfoBar();
527 ASSERT_TRUE(infobar != NULL);
[email protected]2ebd22d2013-01-07 17:04:27528 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR,
529 infobar->infobar_type());
[email protected]2253b3b2012-06-03 22:39:15530}
531
532// Ensures we deal correctly with pages for which the browser does not recognize
533// the language (the translate server may or not detect the language).
[email protected]67755672013-04-08 23:25:32534TEST_F(TranslateManagerBrowserTest, TranslateUnknownLanguage) {
[email protected]2253b3b2012-06-03 22:39:15535 // Simulate navigating to a page ("und" is the string returned by the CLD for
536 // languages it does not recognize).
537 SimulateNavigation(GURL("http://www.google.mys"), "und", true);
538
539 // We should not have an infobar as we don't know the language.
540 ASSERT_TRUE(GetTranslateInfoBar() == NULL);
541
542 // Translate the page anyway throught the context menu.
543 scoped_ptr<TestRenderViewContextMenu> menu(
[email protected]921441c2012-10-19 22:16:01544 TestRenderViewContextMenu::CreateContextMenu(web_contents()));
[email protected]2253b3b2012-06-03 22:39:15545 menu->Init();
[email protected]96bf34e2013-03-13 04:22:14546 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0);
[email protected]2253b3b2012-06-03 22:39:15547
548 // To test that bug #49018 if fixed, make sure we deal correctly with errors.
549 // Simulate a failure to fetch the translate script.
550 SimulateTranslateScriptURLFetch(false);
551 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
552 ASSERT_TRUE(infobar != NULL);
[email protected]2ebd22d2013-01-07 17:04:27553 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR,
554 infobar->infobar_type());
[email protected]cacaa242013-07-19 22:00:36555 EXPECT_TRUE(infobar->is_error());
[email protected]2253b3b2012-06-03 22:39:15556 infobar->MessageInfoBarButtonPressed();
557 SimulateTranslateScriptURLFetch(true); // This time succeed.
558
559 // Simulate the render notifying the translation has been done, the server
560 // having detected the page was in a known and supported language.
[email protected]05024712013-07-31 09:46:29561 SimulateOnPageTranslated("fr", "en");
[email protected]2253b3b2012-06-03 22:39:15562
563 // The after translate infobar should be showing.
564 infobar = GetTranslateInfoBar();
565 ASSERT_TRUE(infobar != NULL);
[email protected]2ebd22d2013-01-07 17:04:27566 EXPECT_EQ(TranslateInfoBarDelegate::AFTER_TRANSLATE, infobar->infobar_type());
[email protected]9f943482012-09-21 01:05:13567 EXPECT_EQ("fr", infobar->original_language_code());
568 EXPECT_EQ("en", infobar->target_language_code());
[email protected]2253b3b2012-06-03 22:39:15569
570 // Let's run the same steps but this time the server detects the page is
571 // already in English.
572 SimulateNavigation(GURL("http://www.google.com"), "und", true);
[email protected]921441c2012-10-19 22:16:01573 menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents()));
[email protected]2253b3b2012-06-03 22:39:15574 menu->Init();
[email protected]96bf34e2013-03-13 04:22:14575 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0);
[email protected]05024712013-07-31 09:46:29576 SimulateOnPageTranslated(1, "en", "en", TranslateErrors::IDENTICAL_LANGUAGES);
[email protected]2253b3b2012-06-03 22:39:15577 infobar = GetTranslateInfoBar();
578 ASSERT_TRUE(infobar != NULL);
[email protected]2ebd22d2013-01-07 17:04:27579 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR,
580 infobar->infobar_type());
581 EXPECT_EQ(TranslateErrors::IDENTICAL_LANGUAGES, infobar->error_type());
[email protected]2253b3b2012-06-03 22:39:15582
583 // Let's run the same steps again but this time the server fails to detect the
584 // page's language (it returns an empty string).
585 SimulateNavigation(GURL("http://www.google.com"), "und", true);
[email protected]921441c2012-10-19 22:16:01586 menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents()));
[email protected]2253b3b2012-06-03 22:39:15587 menu->Init();
[email protected]96bf34e2013-03-13 04:22:14588 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0);
[email protected]05024712013-07-31 09:46:29589 SimulateOnPageTranslated(2, std::string(), "en",
590 TranslateErrors::UNKNOWN_LANGUAGE);
[email protected]2253b3b2012-06-03 22:39:15591 infobar = GetTranslateInfoBar();
592 ASSERT_TRUE(infobar != NULL);
[email protected]2ebd22d2013-01-07 17:04:27593 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR,
594 infobar->infobar_type());
595 EXPECT_EQ(TranslateErrors::UNKNOWN_LANGUAGE, infobar->error_type());
[email protected]2253b3b2012-06-03 22:39:15596}
597
[email protected]f274d372013-07-22 13:08:50598// Tests that we show/don't show an info-bar for the languages.
599TEST_F(TranslateManagerBrowserTest, TestLanguages) {
600 std::vector<std::string> languages;
601 languages.push_back("en");
602 languages.push_back("ja");
603 languages.push_back("fr");
604 languages.push_back("ht");
605 languages.push_back("xx");
606 languages.push_back("zh");
607 languages.push_back("zh-CN");
608 languages.push_back("und");
[email protected]2253b3b2012-06-03 22:39:15609
610 GURL url("http://www.google.com");
[email protected]f274d372013-07-22 13:08:50611 for (size_t i = 0; i < languages.size(); ++i) {
612 std::string lang = languages[i];
[email protected]2253b3b2012-06-03 22:39:15613 SCOPED_TRACE(::testing::Message() << "Iteration " << i <<
[email protected]f274d372013-07-22 13:08:50614 " language=" << lang);
[email protected]2253b3b2012-06-03 22:39:15615
616 // We should not have a translate infobar.
617 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
618 ASSERT_TRUE(infobar == NULL);
619
[email protected]f274d372013-07-22 13:08:50620 SimulateNavigation(url, lang, true);
[email protected]2253b3b2012-06-03 22:39:15621
622 // Verify we have/don't have an info-bar as expected.
623 infobar = GetTranslateInfoBar();
[email protected]f274d372013-07-22 13:08:50624 bool expected = TranslateManager::IsSupportedLanguage(lang) &&
625 lang != "en";
626 EXPECT_EQ(expected, infobar != NULL);
[email protected]2253b3b2012-06-03 22:39:15627
[email protected]2253b3b2012-06-03 22:39:15628 if (infobar != NULL)
629 EXPECT_TRUE(CloseTranslateInfoBar());
630 }
631}
632
633// Test the fetching of languages from the translate server
[email protected]67755672013-04-08 23:25:32634TEST_F(TranslateManagerBrowserTest, FetchLanguagesFromTranslateServer) {
[email protected]2253b3b2012-06-03 22:39:15635 std::vector<std::string> server_languages;
636 // A list of languages to fake being returned by the translate server.
637 server_languages.push_back("aa");
[email protected]462e9092013-06-14 08:39:38638 server_languages.push_back("ak");
[email protected]2253b3b2012-06-03 22:39:15639 server_languages.push_back("ab");
640 server_languages.push_back("en-CA");
[email protected]462e9092013-06-14 08:39:38641 server_languages.push_back("zh");
642 server_languages.push_back("yi");
[email protected]2253b3b2012-06-03 22:39:15643 server_languages.push_back("fr-FR");
[email protected]462e9092013-06-14 08:39:38644 server_languages.push_back("xx");
[email protected]2253b3b2012-06-03 22:39:15645
[email protected]7da697a2013-07-12 08:09:46646 std::vector<std::string> alpha_languages;
647 alpha_languages.push_back("aa");
648 alpha_languages.push_back("yi");
649
[email protected]296ff3162013-06-20 18:14:23650 // First, get the default languages list. Note that calling
651 // GetSupportedLanguages() invokes RequestLanguageList() internally.
[email protected]2253b3b2012-06-03 22:39:15652 std::vector<std::string> default_supported_languages;
653 TranslateManager::GetSupportedLanguages(&default_supported_languages);
654 // To make sure we got the defaults and don't confuse them with the mocks.
655 ASSERT_NE(default_supported_languages.size(), server_languages.size());
656
[email protected]2253b3b2012-06-03 22:39:15657 // Check that we still get the defaults until the URLFetch has completed.
658 std::vector<std::string> current_supported_languages;
659 TranslateManager::GetSupportedLanguages(&current_supported_languages);
660 EXPECT_EQ(default_supported_languages, current_supported_languages);
661
662 // Also check that it didn't change if we failed the URL fetch.
[email protected]7da697a2013-07-12 08:09:46663 SimulateSupportedLanguagesURLFetch(false, std::vector<std::string>(),
664 true, std::vector<std::string>());
[email protected]2253b3b2012-06-03 22:39:15665 current_supported_languages.clear();
666 TranslateManager::GetSupportedLanguages(&current_supported_languages);
667 EXPECT_EQ(default_supported_languages, current_supported_languages);
668
669 // Now check that we got the appropriate set of languages from the server.
[email protected]7da697a2013-07-12 08:09:46670 SimulateSupportedLanguagesURLFetch(true, server_languages,
671 true, alpha_languages);
[email protected]2253b3b2012-06-03 22:39:15672 current_supported_languages.clear();
673 TranslateManager::GetSupportedLanguages(&current_supported_languages);
[email protected]462e9092013-06-14 08:39:38674 // "xx" can't be displayed in the Translate inforbar, so this is eliminated.
675 EXPECT_EQ(server_languages.size() - 1, current_supported_languages.size());
[email protected]2253b3b2012-06-03 22:39:15676 // Not sure we need to guarantee the order of languages, so we find them.
[email protected]2253b3b2012-06-03 22:39:15677 for (size_t i = 0; i < server_languages.size(); ++i) {
[email protected]7da697a2013-07-12 08:09:46678 const std::string& lang = server_languages[i];
679 if (lang == "xx")
[email protected]462e9092013-06-14 08:39:38680 continue;
[email protected]2253b3b2012-06-03 22:39:15681 EXPECT_NE(current_supported_languages.end(),
682 std::find(current_supported_languages.begin(),
683 current_supported_languages.end(),
[email protected]7da697a2013-07-12 08:09:46684 lang));
685 bool is_alpha = std::find(alpha_languages.begin(),
686 alpha_languages.end(),
687 lang) != alpha_languages.end();
688 EXPECT_EQ(TranslateManager::IsAlphaLanguage(lang), is_alpha);
[email protected]2253b3b2012-06-03 22:39:15689 }
[email protected]2253b3b2012-06-03 22:39:15690}
691
[email protected]7da697a2013-07-12 08:09:46692// Test the fetching of languages from the translate server without 'al'
693// parameter.
694TEST_F(TranslateManagerBrowserTest,
695 FetchLanguagesFromTranslateServerWithoutAlpha) {
696 std::vector<std::string> server_languages;
697 server_languages.push_back("aa");
698 server_languages.push_back("ak");
699 server_languages.push_back("ab");
700 server_languages.push_back("en-CA");
701 server_languages.push_back("zh");
702 server_languages.push_back("yi");
703 server_languages.push_back("fr-FR");
704 server_languages.push_back("xx");
[email protected]2253b3b2012-06-03 22:39:15705
[email protected]7da697a2013-07-12 08:09:46706 std::vector<std::string> alpha_languages;
707 alpha_languages.push_back("aa");
708 alpha_languages.push_back("yi");
709
710 // call GetSupportedLanguages to call RequestLanguageList internally.
711 std::vector<std::string> default_supported_languages;
712 TranslateManager::GetSupportedLanguages(&default_supported_languages);
713
714 SimulateSupportedLanguagesURLFetch(true, server_languages,
715 false, alpha_languages);
716
717 std::vector<std::string> current_supported_languages;
718 TranslateManager::GetSupportedLanguages(&current_supported_languages);
719
720 // "xx" can't be displayed in the Translate inforbar, so this is eliminated.
721 EXPECT_EQ(server_languages.size() - 1, current_supported_languages.size());
722
723 for (size_t i = 0; i < server_languages.size(); ++i) {
724 const std::string& lang = server_languages[i];
725 if (lang == "xx")
726 continue;
727 EXPECT_NE(current_supported_languages.end(),
728 std::find(current_supported_languages.begin(),
729 current_supported_languages.end(),
730 lang));
731 EXPECT_FALSE(TranslateManager::IsAlphaLanguage(lang));
732 }
[email protected]2253b3b2012-06-03 22:39:15733}
734
[email protected]2253b3b2012-06-03 22:39:15735// Tests auto-translate on page.
[email protected]67755672013-04-08 23:25:32736TEST_F(TranslateManagerBrowserTest, AutoTranslateOnNavigate) {
[email protected]2253b3b2012-06-03 22:39:15737 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
738
739 // Simulate the user translating.
740 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
741 ASSERT_TRUE(infobar != NULL);
742 infobar->Translate();
743 // Simulate the translate script being retrieved.
744 SimulateTranslateScriptURLFetch(true);
[email protected]05024712013-07-31 09:46:29745 SimulateOnPageTranslated("fr", "en");
[email protected]2253b3b2012-06-03 22:39:15746
747 // Now navigate to a new page in the same language.
748 process()->sink().ClearMessages();
749 SimulateNavigation(GURL("http://news.google.fr"), "fr", true);
750
751 // This should have automatically triggered a translation.
752 int page_id = 0;
753 std::string original_lang, target_lang;
754 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
755 EXPECT_EQ(1, page_id);
756 EXPECT_EQ("fr", original_lang);
757 EXPECT_EQ("en", target_lang);
758
759 // Now navigate to a page in a different language.
760 process()->sink().ClearMessages();
761 SimulateNavigation(GURL("http://news.google.es"), "es", true);
762
763 // This should not have triggered a translate.
764 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
765}
766
767// Tests that multiple OnPageContents do not cause multiple infobars.
[email protected]67755672013-04-08 23:25:32768TEST_F(TranslateManagerBrowserTest, MultipleOnPageContents) {
[email protected]2253b3b2012-06-03 22:39:15769 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
770
771 // Simulate clicking 'Nope' (don't translate).
772 EXPECT_TRUE(DenyTranslation());
[email protected]a608d172013-04-23 22:27:37773 EXPECT_EQ(0U, infobar_service()->infobar_count());
[email protected]2253b3b2012-06-03 22:39:15774
775 // Send a new PageContents, we should not show an infobar.
776 SimulateOnTranslateLanguageDetermined("fr", true);
[email protected]a608d172013-04-23 22:27:37777 EXPECT_EQ(0U, infobar_service()->infobar_count());
[email protected]2253b3b2012-06-03 22:39:15778
779 // Do the same steps but simulate closing the infobar this time.
780 SimulateNavigation(GURL("http://www.youtube.fr"), "fr", true);
781 EXPECT_TRUE(CloseTranslateInfoBar());
[email protected]a608d172013-04-23 22:27:37782 EXPECT_EQ(0U, infobar_service()->infobar_count());
[email protected]2253b3b2012-06-03 22:39:15783 SimulateOnTranslateLanguageDetermined("fr", true);
[email protected]a608d172013-04-23 22:27:37784 EXPECT_EQ(0U, infobar_service()->infobar_count());
[email protected]2253b3b2012-06-03 22:39:15785}
786
[email protected]b637bcd2013-03-28 12:50:32787// Test that reloading the page brings back the infobar if the
788// reload succeeded and does not bring it back the reload fails.
[email protected]67755672013-04-08 23:25:32789TEST_F(TranslateManagerBrowserTest, Reload) {
[email protected]2253b3b2012-06-03 22:39:15790 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
791
[email protected]2253b3b2012-06-03 22:39:15792 EXPECT_TRUE(CloseTranslateInfoBar());
793
[email protected]2fa6318d2013-12-02 22:10:56794 // Reload should bring back the infobar if the reload succeeds.
[email protected]b637bcd2013-03-28 12:50:32795 ReloadAndWait(true);
[email protected]2253b3b2012-06-03 22:39:15796 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
[email protected]b637bcd2013-03-28 12:50:32797 EXPECT_TRUE(CloseTranslateInfoBar());
798
[email protected]2fa6318d2013-12-02 22:10:56799 // ...But not show it if the reload fails.
[email protected]b637bcd2013-03-28 12:50:32800 ReloadAndWait(false);
[email protected]2fa6318d2013-12-02 22:10:56801 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
[email protected]9ad06a12013-05-10 14:49:53802
[email protected]2fa6318d2013-12-02 22:10:56803 // If we set reload attempts to a high value, we will not see the infobar
804 // immediately.
[email protected]9ad06a12013-05-10 14:49:53805 TranslateManager::GetInstance()->set_translate_max_reload_attemps(100);
806 ReloadAndWait(true);
807 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
[email protected]2253b3b2012-06-03 22:39:15808}
809
810// Test that reloading the page by way of typing again the URL in the
811// location bar brings back the infobar.
[email protected]67755672013-04-08 23:25:32812TEST_F(TranslateManagerBrowserTest, ReloadFromLocationBar) {
[email protected]2253b3b2012-06-03 22:39:15813 GURL url("http://www.google.fr");
[email protected]2253b3b2012-06-03 22:39:15814 SimulateNavigation(url, "fr", true);
815
[email protected]2253b3b2012-06-03 22:39:15816 EXPECT_TRUE(CloseTranslateInfoBar());
817
818 // Create a pending navigation and simulate a page load. That should be the
819 // equivalent of typing the URL again in the location bar.
[email protected]921441c2012-10-19 22:16:01820 NavEntryCommittedObserver nav_observer(web_contents());
821 web_contents()->GetController().LoadURL(url, content::Referrer(),
[email protected]05024712013-07-31 09:46:29822 content::PAGE_TRANSITION_TYPED,
823 std::string());
[email protected]2253b3b2012-06-03 22:39:15824 rvh_tester()->SendNavigate(0, url);
825
826 // Test that we are really getting a same page navigation, the test would be
827 // useless if it was not the case.
828 const content::LoadCommittedDetails& nav_details =
[email protected]05024712013-07-31 09:46:29829 nav_observer.load_committed_details();
[email protected]2253b3b2012-06-03 22:39:15830 EXPECT_TRUE(nav_details.entry != NULL); // There was a navigation.
831 EXPECT_EQ(content::NAVIGATION_TYPE_SAME_PAGE, nav_details.type);
832
833 // The TranslateManager class processes the navigation entry committed
834 // notification in a posted task; process that task.
[email protected]b3a25092013-05-28 22:08:16835 base::MessageLoop::current()->RunUntilIdle();
[email protected]2253b3b2012-06-03 22:39:15836 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
837}
838
839// Tests that a closed translate infobar does not reappear when navigating
840// in-page.
[email protected]67755672013-04-08 23:25:32841TEST_F(TranslateManagerBrowserTest, CloseInfoBarInPageNavigation) {
[email protected]2253b3b2012-06-03 22:39:15842 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
843
[email protected]2253b3b2012-06-03 22:39:15844 EXPECT_TRUE(CloseTranslateInfoBar());
845
846 // Navigate in page, no infobar should be shown.
[email protected]05024712013-07-31 09:46:29847 SimulateNavigation(GURL("http://www.google.fr/#ref1"), "fr", true);
[email protected]2253b3b2012-06-03 22:39:15848 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
849
850 // Navigate out of page, a new infobar should show.
851 SimulateNavigation(GURL("http://www.google.fr/foot"), "fr", true);
852 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
853}
854
855// Tests that a closed translate infobar does not reappear when navigating
856// in a subframe. (http://crbug.com/48215)
[email protected]67755672013-04-08 23:25:32857TEST_F(TranslateManagerBrowserTest, CloseInfoBarInSubframeNavigation) {
[email protected]2253b3b2012-06-03 22:39:15858 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
859
[email protected]2253b3b2012-06-03 22:39:15860 EXPECT_TRUE(CloseTranslateInfoBar());
861
862 // Simulate a sub-frame auto-navigating.
863 rvh_tester()->SendNavigateWithTransition(
864 1, GURL("http://pub.com"), content::PAGE_TRANSITION_AUTO_SUBFRAME);
865 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
866
867 // Simulate the user navigating in a sub-frame.
868 rvh_tester()->SendNavigateWithTransition(
869 2, GURL("http://pub.com"), content::PAGE_TRANSITION_MANUAL_SUBFRAME);
870 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
871
872 // Navigate out of page, a new infobar should show.
873 SimulateNavigation(GURL("http://www.google.fr/foot"), "fr", true);
874 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
875}
876
[email protected]2253b3b2012-06-03 22:39:15877// Tests that denying translation is sticky when navigating in page.
[email protected]67755672013-04-08 23:25:32878TEST_F(TranslateManagerBrowserTest, DenyTranslateInPageNavigation) {
[email protected]2253b3b2012-06-03 22:39:15879 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
880
881 // Simulate clicking 'Nope' (don't translate).
882 EXPECT_TRUE(DenyTranslation());
883
884 // Navigate in page, no infobar should be shown.
885 SimulateNavigation(GURL("http://www.google.fr/#ref1"), "fr", true);
886 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
887
888 // Navigate out of page, a new infobar should show.
889 SimulateNavigation(GURL("http://www.google.fr/foot"), "fr", true);
890 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
891}
892
893// Tests that after translating and closing the infobar, the infobar does not
894// return when navigating in page.
[email protected]67755672013-04-08 23:25:32895TEST_F(TranslateManagerBrowserTest, TranslateCloseInfoBarInPageNavigation) {
[email protected]2253b3b2012-06-03 22:39:15896 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
897
898 // Simulate the user translating.
899 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
900 ASSERT_TRUE(infobar != NULL);
901 infobar->Translate();
902 // Simulate the translate script being retrieved.
903 SimulateTranslateScriptURLFetch(true);
[email protected]05024712013-07-31 09:46:29904 SimulateOnPageTranslated("fr", "en");
[email protected]2253b3b2012-06-03 22:39:15905
[email protected]2253b3b2012-06-03 22:39:15906 EXPECT_TRUE(CloseTranslateInfoBar());
907
908 // Navigate in page, no infobar should be shown.
909 SimulateNavigation(GURL("http://www.google.fr/#ref1"), "fr", true);
910 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
911
912 // Navigate out of page, a new infobar should show.
913 // Note that we navigate to a page in a different language so we don't trigger
914 // the auto-translate feature (it would translate the page automatically and
915 // the before translate inforbar would not be shown).
916 SimulateNavigation(GURL("http://www.google.de"), "de", true);
917 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
918}
919
920// Tests that the after translate the infobar still shows when navigating
921// in-page.
[email protected]67755672013-04-08 23:25:32922TEST_F(TranslateManagerBrowserTest, TranslateInPageNavigation) {
[email protected]2253b3b2012-06-03 22:39:15923 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
924
925 // Simulate the user translating.
926 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
927 ASSERT_TRUE(infobar != NULL);
928 infobar->Translate();
[email protected]2253b3b2012-06-03 22:39:15929 SimulateTranslateScriptURLFetch(true);
[email protected]05024712013-07-31 09:46:29930 SimulateOnPageTranslated("fr", "en");
[email protected]2253b3b2012-06-03 22:39:15931 // The after translate infobar is showing.
932 infobar = GetTranslateInfoBar();
933 ASSERT_TRUE(infobar != NULL);
934
935 // Navigate out of page, a new infobar should show.
936 // See note in TranslateCloseInfoBarInPageNavigation test on why it is
937 // important to navigate to a page in a different language for this test.
938 SimulateNavigation(GURL("http://www.google.de"), "de", true);
939 // The old infobar is gone.
940 EXPECT_TRUE(CheckInfoBarRemovedAndReset(infobar));
941 // And there is a new one.
942 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
943}
944
945// Tests that no translate infobar is shown when navigating to a page in an
946// unsupported language.
[email protected]67755672013-04-08 23:25:32947TEST_F(TranslateManagerBrowserTest, CLDReportsUnsupportedPageLanguage) {
[email protected]2253b3b2012-06-03 22:39:15948 // Simulate navigating to a page and getting an unsupported language.
949 SimulateNavigation(GURL("http://www.google.com"), "qbz", true);
950
951 // No info-bar should be shown.
952 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
953}
954
955// Tests that we deal correctly with unsupported languages returned by the
956// server.
957// The translation server might return a language we don't support.
[email protected]67755672013-04-08 23:25:32958TEST_F(TranslateManagerBrowserTest, ServerReportsUnsupportedLanguage) {
[email protected]2253b3b2012-06-03 22:39:15959 SimulateNavigation(GURL("http://mail.google.fr"), "fr", true);
960 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
961 ASSERT_TRUE(infobar != NULL);
962 process()->sink().ClearMessages();
963 infobar->Translate();
964 SimulateTranslateScriptURLFetch(true);
965 // Simulate the render notifying the translation has been done, but it
966 // reports a language we don't support.
[email protected]05024712013-07-31 09:46:29967 SimulateOnPageTranslated("qbz", "en");
[email protected]2253b3b2012-06-03 22:39:15968
969 // An error infobar should be showing to report that we don't support this
970 // language.
971 infobar = GetTranslateInfoBar();
972 ASSERT_TRUE(infobar != NULL);
[email protected]2ebd22d2013-01-07 17:04:27973 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR,
974 infobar->infobar_type());
[email protected]2253b3b2012-06-03 22:39:15975
976 // This infobar should have a button (so the string should not be empty).
977 ASSERT_FALSE(infobar->GetMessageInfoBarButtonText().empty());
978
979 // Pressing the button on that infobar should revert to the original language.
980 process()->sink().ClearMessages();
981 infobar->MessageInfoBarButtonPressed();
982 const IPC::Message* message =
983 process()->sink().GetFirstMessageMatching(
984 ChromeViewMsg_RevertTranslation::ID);
985 EXPECT_TRUE(message != NULL);
986 // And it should have removed the infobar.
987 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
988}
989
990// Tests that no translate infobar is shown and context menu is disabled, when
991// Chrome is in a language that the translate server does not support.
[email protected]67755672013-04-08 23:25:32992TEST_F(TranslateManagerBrowserTest, UnsupportedUILanguage) {
[email protected]2253b3b2012-06-03 22:39:15993 std::string original_lang = g_browser_process->GetApplicationLocale();
[email protected]bb1c86cd2014-01-29 18:22:18994 SetApplicationLocale("qbz");
[email protected]2253b3b2012-06-03 22:39:15995
996 // Make sure that the accept language list only contains unsupported languages
997 Profile* profile =
[email protected]921441c2012-10-19 22:16:01998 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
[email protected]2253b3b2012-06-03 22:39:15999 PrefService* prefs = profile->GetPrefs();
1000 prefs->SetString(prefs::kAcceptLanguages, "qbz");
1001
1002 // Simulate navigating to a page in a language supported by the translate
1003 // server.
1004 SimulateNavigation(GURL("http://www.google.com"), "en", true);
1005
1006 // No info-bar should be shown.
1007 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
1008
1009 // And the context menu option should be disabled too.
1010 scoped_ptr<TestRenderViewContextMenu> menu(
[email protected]921441c2012-10-19 22:16:011011 TestRenderViewContextMenu::CreateContextMenu(web_contents()));
[email protected]2253b3b2012-06-03 22:39:151012 menu->Init();
1013 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
1014 EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
1015
[email protected]bb1c86cd2014-01-29 18:22:181016 SetApplicationLocale(original_lang);
[email protected]2253b3b2012-06-03 22:39:151017}
1018
1019// Tests that the first supported accept language is selected
[email protected]67755672013-04-08 23:25:321020TEST_F(TranslateManagerBrowserTest, TranslateAcceptLanguage) {
[email protected]2253b3b2012-06-03 22:39:151021 // Set locate to non-existant language
1022 std::string original_lang = g_browser_process->GetApplicationLocale();
[email protected]bb1c86cd2014-01-29 18:22:181023 SetApplicationLocale("qbz");
[email protected]2253b3b2012-06-03 22:39:151024
1025 // Set Qbz and French as the only accepted languages
1026 Profile* profile =
[email protected]921441c2012-10-19 22:16:011027 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
[email protected]2253b3b2012-06-03 22:39:151028 PrefService* prefs = profile->GetPrefs();
1029 prefs->SetString(prefs::kAcceptLanguages, "qbz,fr");
1030
1031 // Go to a German page
1032 SimulateNavigation(GURL("http://google.de"), "de", true);
1033
1034 // Expect the infobar to pop up
1035 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
1036
1037 // Set Qbz and English-US as the only accepted languages to test the country
1038 // code removal code which was causing a crash as filed in Issue 90106,
1039 // a crash caused by a language with a country code that wasn't recognized.
1040 prefs->SetString(prefs::kAcceptLanguages, "qbz,en-us");
1041
1042 // Go to a German page
1043 SimulateNavigation(GURL("http://google.de"), "de", true);
1044
1045 // Expect the infobar to pop up
1046 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
1047}
1048
1049// Tests that the translate enabled preference is honored.
[email protected]67755672013-04-08 23:25:321050TEST_F(TranslateManagerBrowserTest, TranslateEnabledPref) {
[email protected]2253b3b2012-06-03 22:39:151051 // Make sure the pref allows translate.
1052 Profile* profile =
[email protected]921441c2012-10-19 22:16:011053 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
[email protected]2253b3b2012-06-03 22:39:151054 PrefService* prefs = profile->GetPrefs();
1055 prefs->SetBoolean(prefs::kEnableTranslate, true);
1056
[email protected]2253b3b2012-06-03 22:39:151057 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
1058
1059 // An infobar should be shown.
1060 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
1061 EXPECT_TRUE(infobar != NULL);
1062
1063 // Disable translate.
1064 prefs->SetBoolean(prefs::kEnableTranslate, false);
1065
1066 // Navigate to a new page, that should close the previous infobar.
1067 GURL url("http://www.youtube.fr");
1068 NavigateAndCommit(url);
1069 infobar = GetTranslateInfoBar();
1070 EXPECT_TRUE(infobar == NULL);
1071
1072 // Simulate getting the page contents and language, that should not trigger
1073 // a translate infobar.
1074 SimulateOnTranslateLanguageDetermined("fr", true);
1075 infobar = GetTranslateInfoBar();
1076 EXPECT_TRUE(infobar == NULL);
1077}
1078
1079// Tests the "Never translate <language>" pref.
[email protected]67755672013-04-08 23:25:321080TEST_F(TranslateManagerBrowserTest, NeverTranslateLanguagePref) {
[email protected]2253b3b2012-06-03 22:39:151081 GURL url("http://www.google.fr");
1082 SimulateNavigation(url, "fr", true);
1083
1084 // An infobar should be shown.
1085 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
1086
1087 // Select never translate this language.
1088 Profile* profile =
[email protected]921441c2012-10-19 22:16:011089 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
[email protected]2253b3b2012-06-03 22:39:151090 PrefService* prefs = profile->GetPrefs();
1091 PrefChangeRegistrar registrar;
1092 registrar.Init(prefs);
[email protected]032ae93a2013-08-30 09:15:031093 registrar.Add(TranslatePrefs::kPrefTranslateBlockedLanguages,
[email protected]116129e02012-11-21 17:26:271094 pref_callback_);
[email protected]2253b3b2012-06-03 22:39:151095 TranslatePrefs translate_prefs(prefs);
[email protected]5d37b22982013-06-25 06:07:001096 EXPECT_FALSE(translate_prefs.IsBlockedLanguage("fr"));
[email protected]f9db7e02013-06-17 10:29:431097 EXPECT_TRUE(translate_prefs.CanTranslateLanguage(profile, "fr"));
[email protected]032ae93a2013-08-30 09:15:031098 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateBlockedLanguages);
[email protected]5d37b22982013-06-25 06:07:001099 translate_prefs.BlockLanguage("fr");
1100 EXPECT_TRUE(translate_prefs.IsBlockedLanguage("fr"));
[email protected]f9db7e02013-06-17 10:29:431101 EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(url.host()));
1102 EXPECT_FALSE(translate_prefs.CanTranslateLanguage(profile, "fr"));
[email protected]2253b3b2012-06-03 22:39:151103
[email protected]2253b3b2012-06-03 22:39:151104 EXPECT_TRUE(CloseTranslateInfoBar());
1105
1106 // Navigate to a new page also in French.
1107 SimulateNavigation(GURL("http://wwww.youtube.fr"), "fr", true);
1108
1109 // There should not be a translate infobar.
1110 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
1111
1112 // Remove the language from the blacklist.
[email protected]032ae93a2013-08-30 09:15:031113 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateBlockedLanguages);
[email protected]5d37b22982013-06-25 06:07:001114 translate_prefs.UnblockLanguage("fr");
1115 EXPECT_FALSE(translate_prefs.IsBlockedLanguage("fr"));
[email protected]f9db7e02013-06-17 10:29:431116 EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(url.host()));
1117 EXPECT_TRUE(translate_prefs.CanTranslateLanguage(profile, "fr"));
[email protected]2253b3b2012-06-03 22:39:151118
1119 // Navigate to a page in French.
1120 SimulateNavigation(url, "fr", true);
1121
1122 // There should be a translate infobar.
1123 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
1124}
1125
1126// Tests the "Never translate this site" pref.
[email protected]67755672013-04-08 23:25:321127TEST_F(TranslateManagerBrowserTest, NeverTranslateSitePref) {
[email protected]2253b3b2012-06-03 22:39:151128 GURL url("http://www.google.fr");
1129 std::string host(url.host());
1130 SimulateNavigation(url, "fr", true);
1131
1132 // An infobar should be shown.
1133 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
1134
1135 // Select never translate this site.
1136 Profile* profile =
[email protected]921441c2012-10-19 22:16:011137 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
[email protected]2253b3b2012-06-03 22:39:151138 PrefService* prefs = profile->GetPrefs();
1139 PrefChangeRegistrar registrar;
1140 registrar.Init(prefs);
[email protected]116129e02012-11-21 17:26:271141 registrar.Add(TranslatePrefs::kPrefTranslateSiteBlacklist, pref_callback_);
[email protected]2253b3b2012-06-03 22:39:151142 TranslatePrefs translate_prefs(prefs);
1143 EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(host));
[email protected]f9db7e02013-06-17 10:29:431144 EXPECT_TRUE(translate_prefs.CanTranslateLanguage(profile, "fr"));
[email protected]2253b3b2012-06-03 22:39:151145 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateSiteBlacklist);
1146 translate_prefs.BlacklistSite(host);
1147 EXPECT_TRUE(translate_prefs.IsSiteBlacklisted(host));
[email protected]f9db7e02013-06-17 10:29:431148 EXPECT_TRUE(translate_prefs.CanTranslateLanguage(profile, "fr"));
[email protected]2253b3b2012-06-03 22:39:151149
[email protected]2253b3b2012-06-03 22:39:151150 EXPECT_TRUE(CloseTranslateInfoBar());
1151
1152 // Navigate to a new page also on the same site.
1153 SimulateNavigation(GURL("http://www.google.fr/hello"), "fr", true);
1154
1155 // There should not be a translate infobar.
1156 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
1157
1158 // Remove the site from the blacklist.
1159 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateSiteBlacklist);
1160 translate_prefs.RemoveSiteFromBlacklist(host);
1161 EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(host));
[email protected]f9db7e02013-06-17 10:29:431162 EXPECT_TRUE(translate_prefs.CanTranslateLanguage(profile, "fr"));
[email protected]2253b3b2012-06-03 22:39:151163
1164 // Navigate to a page in French.
1165 SimulateNavigation(url, "fr", true);
1166
1167 // There should be a translate infobar.
1168 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
1169}
1170
1171// Tests the "Always translate this language" pref.
[email protected]67755672013-04-08 23:25:321172TEST_F(TranslateManagerBrowserTest, AlwaysTranslateLanguagePref) {
[email protected]2253b3b2012-06-03 22:39:151173 // Select always translate French to English.
1174 Profile* profile =
[email protected]921441c2012-10-19 22:16:011175 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
[email protected]2253b3b2012-06-03 22:39:151176 PrefService* prefs = profile->GetPrefs();
1177 PrefChangeRegistrar registrar;
1178 registrar.Init(prefs);
[email protected]116129e02012-11-21 17:26:271179 registrar.Add(TranslatePrefs::kPrefTranslateWhitelists, pref_callback_);
[email protected]2253b3b2012-06-03 22:39:151180 TranslatePrefs translate_prefs(prefs);
1181 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateWhitelists);
1182 translate_prefs.WhitelistLanguagePair("fr", "en");
1183
1184 // Load a page in French.
1185 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
1186
1187 // It should have triggered an automatic translation to English.
1188
1189 // The translating infobar should be showing.
1190 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
1191 ASSERT_TRUE(infobar != NULL);
[email protected]2ebd22d2013-01-07 17:04:271192 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATING, infobar->infobar_type());
[email protected]2253b3b2012-06-03 22:39:151193 SimulateTranslateScriptURLFetch(true);
1194 int page_id = 0;
1195 std::string original_lang, target_lang;
1196 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
1197 EXPECT_EQ("fr", original_lang);
1198 EXPECT_EQ("en", target_lang);
1199 process()->sink().ClearMessages();
1200
1201 // Try another language, it should not be autotranslated.
1202 SimulateNavigation(GURL("http://www.google.es"), "es", true);
1203 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
1204 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
1205 EXPECT_TRUE(CloseTranslateInfoBar());
1206
1207 // Let's switch to incognito mode, it should not be autotranslated in that
1208 // case either.
1209 TestingProfile* test_profile =
[email protected]921441c2012-10-19 22:16:011210 static_cast<TestingProfile*>(web_contents()->GetBrowserContext());
[email protected]9819fd02013-08-22 10:49:391211 test_profile->ForceIncognito(true);
[email protected]2253b3b2012-06-03 22:39:151212 SimulateNavigation(GURL("http://www.youtube.fr"), "fr", true);
1213 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
1214 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
1215 EXPECT_TRUE(CloseTranslateInfoBar());
[email protected]9819fd02013-08-22 10:49:391216 test_profile->ForceIncognito(false); // Get back to non incognito.
[email protected]2253b3b2012-06-03 22:39:151217
1218 // Now revert the always translate pref and make sure we go back to expected
1219 // behavior, which is show a "before translate" infobar.
1220 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateWhitelists);
1221 translate_prefs.RemoveLanguagePairFromWhitelist("fr", "en");
1222 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
1223 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
1224 infobar = GetTranslateInfoBar();
1225 ASSERT_TRUE(infobar != NULL);
[email protected]2ebd22d2013-01-07 17:04:271226 EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE,
1227 infobar->infobar_type());
[email protected]2253b3b2012-06-03 22:39:151228}
1229
1230// Context menu.
[email protected]67755672013-04-08 23:25:321231TEST_F(TranslateManagerBrowserTest, ContextMenu) {
[email protected]2253b3b2012-06-03 22:39:151232 // Blacklist www.google.fr and French for translation.
1233 GURL url("http://www.google.fr");
1234 Profile* profile =
[email protected]921441c2012-10-19 22:16:011235 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
[email protected]2253b3b2012-06-03 22:39:151236 TranslatePrefs translate_prefs(profile->GetPrefs());
[email protected]5d37b22982013-06-25 06:07:001237 translate_prefs.BlockLanguage("fr");
[email protected]2253b3b2012-06-03 22:39:151238 translate_prefs.BlacklistSite(url.host());
[email protected]5d37b22982013-06-25 06:07:001239 EXPECT_TRUE(translate_prefs.IsBlockedLanguage("fr"));
[email protected]2253b3b2012-06-03 22:39:151240 EXPECT_TRUE(translate_prefs.IsSiteBlacklisted(url.host()));
1241
1242 // Simulate navigating to a page in French. The translate menu should show but
1243 // should only be enabled when the page language has been received.
1244 NavigateAndCommit(url);
1245 scoped_ptr<TestRenderViewContextMenu> menu(
[email protected]921441c2012-10-19 22:16:011246 TestRenderViewContextMenu::CreateContextMenu(web_contents()));
[email protected]2253b3b2012-06-03 22:39:151247 menu->Init();
1248 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
1249 EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
1250
1251 // Simulate receiving the language.
1252 SimulateOnTranslateLanguageDetermined("fr", true);
[email protected]921441c2012-10-19 22:16:011253 menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents()));
[email protected]2253b3b2012-06-03 22:39:151254 menu->Init();
1255 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
1256 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
1257
1258 // Use the menu to translate the page.
[email protected]96bf34e2013-03-13 04:22:141259 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0);
[email protected]2253b3b2012-06-03 22:39:151260
1261 // That should have triggered a translation.
1262 // The "translating..." infobar should be showing.
1263 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
1264 ASSERT_TRUE(infobar != NULL);
[email protected]2ebd22d2013-01-07 17:04:271265 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATING, infobar->infobar_type());
[email protected]2253b3b2012-06-03 22:39:151266 SimulateTranslateScriptURLFetch(true);
1267 int page_id = 0;
1268 std::string original_lang, target_lang;
1269 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
1270 EXPECT_EQ("fr", original_lang);
1271 EXPECT_EQ("en", target_lang);
1272 process()->sink().ClearMessages();
1273
1274 // This should also have reverted the blacklisting of this site and language.
[email protected]5d37b22982013-06-25 06:07:001275 EXPECT_FALSE(translate_prefs.IsBlockedLanguage("fr"));
[email protected]2253b3b2012-06-03 22:39:151276 EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(url.host()));
1277
1278 // Let's simulate the page being translated.
[email protected]05024712013-07-31 09:46:291279 SimulateOnPageTranslated("fr", "en");
[email protected]2253b3b2012-06-03 22:39:151280
1281 // The translate menu should now be disabled.
[email protected]921441c2012-10-19 22:16:011282 menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents()));
[email protected]2253b3b2012-06-03 22:39:151283 menu->Init();
1284 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
1285 EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
1286
1287 // Test that selecting translate in the context menu WHILE the page is being
1288 // translated does nothing (this could happen if autotranslate kicks-in and
1289 // the user selects the menu while the translation is being performed).
1290 SimulateNavigation(GURL("http://www.google.es"), "es", true);
1291 infobar = GetTranslateInfoBar();
1292 ASSERT_TRUE(infobar != NULL);
1293 infobar->Translate();
1294 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
1295 process()->sink().ClearMessages();
[email protected]921441c2012-10-19 22:16:011296 menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents()));
[email protected]2253b3b2012-06-03 22:39:151297 menu->Init();
1298 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
[email protected]96bf34e2013-03-13 04:22:141299 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0);
[email protected]2253b3b2012-06-03 22:39:151300 // No message expected since the translation should have been ignored.
1301 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
1302
1303 // Now test that selecting translate in the context menu AFTER the page has
1304 // been translated does nothing.
1305 SimulateNavigation(GURL("http://www.google.de"), "de", true);
1306 infobar = GetTranslateInfoBar();
1307 ASSERT_TRUE(infobar != NULL);
1308 infobar->Translate();
1309 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
1310 process()->sink().ClearMessages();
[email protected]921441c2012-10-19 22:16:011311 menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents()));
[email protected]2253b3b2012-06-03 22:39:151312 menu->Init();
1313 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
[email protected]05024712013-07-31 09:46:291314 SimulateOnPageTranslated("de", "en");
[email protected]96bf34e2013-03-13 04:22:141315 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0);
[email protected]2253b3b2012-06-03 22:39:151316 // No message expected since the translation should have been ignored.
1317 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
1318
1319 // Test that the translate context menu is enabled when the page is in an
1320 // unknown language.
1321 SimulateNavigation(url, "und", true);
[email protected]921441c2012-10-19 22:16:011322 menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents()));
[email protected]2253b3b2012-06-03 22:39:151323 menu->Init();
1324 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
1325 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
1326
[email protected]8e7d0b472013-05-23 05:41:371327 // Test that the translate context menu is enabled even if the page is in an
[email protected]2253b3b2012-06-03 22:39:151328 // unsupported language.
1329 SimulateNavigation(url, "qbz", true);
[email protected]921441c2012-10-19 22:16:011330 menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents()));
[email protected]2253b3b2012-06-03 22:39:151331 menu->Init();
1332 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
[email protected]8e7d0b472013-05-23 05:41:371333 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
[email protected]2253b3b2012-06-03 22:39:151334}
1335
1336// Tests that an extra always/never translate button is shown on the "before
1337// translate" infobar when the translation is accepted/declined 3 times,
1338// only when not in incognito mode.
[email protected]67755672013-04-08 23:25:321339TEST_F(TranslateManagerBrowserTest, BeforeTranslateExtraButtons) {
[email protected]2253b3b2012-06-03 22:39:151340 Profile* profile =
[email protected]921441c2012-10-19 22:16:011341 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
[email protected]2253b3b2012-06-03 22:39:151342 TranslatePrefs translate_prefs(profile->GetPrefs());
1343 translate_prefs.ResetTranslationAcceptedCount("fr");
1344 translate_prefs.ResetTranslationDeniedCount("fr");
1345 translate_prefs.ResetTranslationAcceptedCount("de");
1346 translate_prefs.ResetTranslationDeniedCount("de");
1347
1348 // We'll do 4 times in incognito mode first to make sure the button is not
1349 // shown in that case, then 4 times in normal mode.
1350 TranslateInfoBarDelegate* infobar;
1351 TestingProfile* test_profile =
[email protected]921441c2012-10-19 22:16:011352 static_cast<TestingProfile*>(web_contents()->GetBrowserContext());
[email protected]bd306722012-07-11 20:43:591353 static_cast<extensions::TestExtensionSystem*>(
1354 extensions::ExtensionSystem::Get(test_profile))->
[email protected]98b6d942013-11-10 00:34:071355 CreateProcessManager();
[email protected]9819fd02013-08-22 10:49:391356 test_profile->ForceIncognito(true);
[email protected]2253b3b2012-06-03 22:39:151357 for (int i = 0; i < 8; ++i) {
1358 SCOPED_TRACE(::testing::Message() << "Iteration " << i <<
1359 " incognito mode=" << test_profile->IsOffTheRecord());
1360 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
1361 infobar = GetTranslateInfoBar();
1362 ASSERT_TRUE(infobar != NULL);
[email protected]2ebd22d2013-01-07 17:04:271363 EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE,
1364 infobar->infobar_type());
[email protected]2253b3b2012-06-03 22:39:151365 if (i < 7) {
[email protected]f17179652013-04-25 18:11:101366 EXPECT_FALSE(infobar->ShouldShowAlwaysTranslateShortcut());
[email protected]2253b3b2012-06-03 22:39:151367 infobar->Translate();
1368 process()->sink().ClearMessages();
1369 } else {
[email protected]f17179652013-04-25 18:11:101370 EXPECT_TRUE(infobar->ShouldShowAlwaysTranslateShortcut());
[email protected]2253b3b2012-06-03 22:39:151371 }
1372 if (i == 3)
[email protected]9819fd02013-08-22 10:49:391373 test_profile->ForceIncognito(false);
[email protected]2253b3b2012-06-03 22:39:151374 }
1375 // Simulate the user pressing "Always translate French".
1376 infobar->AlwaysTranslatePageLanguage();
1377 EXPECT_TRUE(translate_prefs.IsLanguagePairWhitelisted("fr", "en"));
1378 // Simulate the translate script being retrieved (it only needs to be done
1379 // once in the test as it is cached).
1380 SimulateTranslateScriptURLFetch(true);
1381 // That should have triggered a page translate.
1382 int page_id = 0;
1383 std::string original_lang, target_lang;
1384 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
1385 process()->sink().ClearMessages();
1386
1387 // Now test that declining the translation causes a "never translate" button
1388 // to be shown (in non incognito mode only).
[email protected]9819fd02013-08-22 10:49:391389 test_profile->ForceIncognito(true);
[email protected]2253b3b2012-06-03 22:39:151390 for (int i = 0; i < 8; ++i) {
1391 SCOPED_TRACE(::testing::Message() << "Iteration " << i <<
1392 " incognito mode=" << test_profile->IsOffTheRecord());
1393 SimulateNavigation(GURL("http://www.google.de"), "de", true);
1394 infobar = GetTranslateInfoBar();
1395 ASSERT_TRUE(infobar != NULL);
[email protected]2ebd22d2013-01-07 17:04:271396 EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE,
1397 infobar->infobar_type());
[email protected]2253b3b2012-06-03 22:39:151398 if (i < 7) {
[email protected]f17179652013-04-25 18:11:101399 EXPECT_FALSE(infobar->ShouldShowNeverTranslateShortcut());
[email protected]2253b3b2012-06-03 22:39:151400 infobar->TranslationDeclined();
1401 } else {
[email protected]f17179652013-04-25 18:11:101402 EXPECT_TRUE(infobar->ShouldShowNeverTranslateShortcut());
[email protected]2253b3b2012-06-03 22:39:151403 }
1404 if (i == 3)
[email protected]9819fd02013-08-22 10:49:391405 test_profile->ForceIncognito(false);
[email protected]2253b3b2012-06-03 22:39:151406 }
1407 // Simulate the user pressing "Never translate French".
1408 infobar->NeverTranslatePageLanguage();
[email protected]5d37b22982013-06-25 06:07:001409 EXPECT_TRUE(translate_prefs.IsBlockedLanguage("de"));
[email protected]2253b3b2012-06-03 22:39:151410 // No translation should have occured and the infobar should be gone.
1411 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
1412 process()->sink().ClearMessages();
1413 ASSERT_TRUE(GetTranslateInfoBar() == NULL);
1414}
1415
1416// Tests that we don't show a translate infobar when a page instructs that it
1417// should not be translated.
[email protected]67755672013-04-08 23:25:321418TEST_F(TranslateManagerBrowserTest, NonTranslatablePage) {
[email protected]2253b3b2012-06-03 22:39:151419 SimulateNavigation(GURL("http://mail.google.fr"), "fr", false);
1420
1421 // We should not have an infobar.
1422 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
1423
[email protected]54e4b522013-05-21 08:36:231424 // The context menu is enabled to allow users to force translation.
[email protected]2253b3b2012-06-03 22:39:151425 scoped_ptr<TestRenderViewContextMenu> menu(
[email protected]921441c2012-10-19 22:16:011426 TestRenderViewContextMenu::CreateContextMenu(web_contents()));
[email protected]2253b3b2012-06-03 22:39:151427 menu->Init();
1428 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
[email protected]54e4b522013-05-21 08:36:231429 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
[email protected]2253b3b2012-06-03 22:39:151430}
1431
1432// Tests that the script is expired and refetched as expected.
[email protected]67755672013-04-08 23:25:321433TEST_F(TranslateManagerBrowserTest, ScriptExpires) {
[email protected]2253b3b2012-06-03 22:39:151434 ExpireTranslateScriptImmediately();
1435
[email protected]2253b3b2012-06-03 22:39:151436 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
1437 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
1438 ASSERT_TRUE(infobar != NULL);
1439 process()->sink().ClearMessages();
1440 infobar->Translate();
1441 SimulateTranslateScriptURLFetch(true);
[email protected]05024712013-07-31 09:46:291442 SimulateOnPageTranslated("fr", "en");
[email protected]2253b3b2012-06-03 22:39:151443
1444 // A task should have been posted to clear the script, run it.
[email protected]b3a25092013-05-28 22:08:161445 base::MessageLoop::current()->RunUntilIdle();
[email protected]2253b3b2012-06-03 22:39:151446
1447 // Do another navigation and translation.
1448 SimulateNavigation(GURL("http://www.google.es"), "es", true);
1449 infobar = GetTranslateInfoBar();
1450 ASSERT_TRUE(infobar != NULL);
1451 process()->sink().ClearMessages();
1452 infobar->Translate();
1453 // If we don't simulate the URL fetch, the TranslateManager should be waiting
1454 // for the script and no message should have been sent to the renderer.
1455 EXPECT_TRUE(
1456 process()->sink().GetFirstMessageMatching(
[email protected]05024712013-07-31 09:46:291457 ChromeViewMsg_TranslatePage::ID) == NULL);
[email protected]2253b3b2012-06-03 22:39:151458 // Now simulate the URL fetch.
1459 SimulateTranslateScriptURLFetch(true);
1460 // Now the message should have been sent.
1461 int page_id = 0;
1462 std::string original_lang, target_lang;
1463 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
1464 EXPECT_EQ("es", original_lang);
1465 EXPECT_EQ("en", target_lang);
1466}
[email protected]f8a2e132012-08-31 18:16:201467
[email protected]67755672013-04-08 23:25:321468TEST_F(TranslateManagerBrowserTest, DownloadsAndHistoryNotTranslated) {
[email protected]f8a2e132012-08-31 18:16:201469 ASSERT_FALSE(TranslateManager::IsTranslatableURL(
1470 GURL(chrome::kChromeUIDownloadsURL)));
1471 ASSERT_FALSE(TranslateManager::IsTranslatableURL(
1472 GURL(chrome::kChromeUIHistoryURL)));
1473}
1474
[email protected]e625b7602013-10-28 09:24:561475#if !defined(OS_ANDROID) && !defined(OS_IOS)
1476
1477TEST_F(TranslateManagerBrowserTest, BubbleNormalTranslate) {
1478 // Prepare for the bubble
1479 CommandLine* command_line = CommandLine::ForCurrentProcess();
1480 command_line->AppendSwitch(switches::kEnableTranslateNewUX);
1481 MockTranslateBubbleFactory* factory = new MockTranslateBubbleFactory;
1482 scoped_ptr<TranslateBubbleFactory> factory_ptr(factory);
1483 TranslateBubbleFactory::SetFactory(factory);
1484
1485 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
1486
1487 // Check the bubble exists instead of the infobar.
1488 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
1489 ASSERT_TRUE(infobar == NULL);
1490 TranslateBubbleModel* bubble = factory->model();
1491 ASSERT_TRUE(bubble != NULL);
1492 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE,
1493 bubble->GetViewState());
1494
1495 // Simulate clicking translate.
1496 process()->sink().ClearMessages();
1497 bubble->Translate();
1498
1499 // Check the bubble shows "Translating...".
1500 bubble = factory->model();
1501 ASSERT_TRUE(bubble != NULL);
1502 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_TRANSLATING,
1503 bubble->GetViewState());
1504
1505 // Simulate the translate script being retrieved (it only needs to be done
1506 // once in the test as it is cached).
1507 SimulateTranslateScriptURLFetch(true);
1508
1509 // Simulate the render notifying the translation has been done.
1510 SimulateOnPageTranslated("fr", "en");
1511
1512 // Check the bubble shows "Translated."
1513 bubble = factory->model();
1514 ASSERT_TRUE(bubble != NULL);
1515 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_AFTER_TRANSLATE,
1516 bubble->GetViewState());
1517}
1518
1519TEST_F(TranslateManagerBrowserTest, BubbleTranslateScriptNotAvailable) {
1520 // Prepare for the bubble
1521 CommandLine* command_line = CommandLine::ForCurrentProcess();
1522 command_line->AppendSwitch(switches::kEnableTranslateNewUX);
1523 MockTranslateBubbleFactory* factory = new MockTranslateBubbleFactory;
1524 scoped_ptr<TranslateBubbleFactory> factory_ptr(factory);
1525 TranslateBubbleFactory::SetFactory(factory);
1526
1527 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
1528
1529 // Check the bubble exists instead of the infobar.
1530 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
1531 ASSERT_TRUE(infobar == NULL);
1532 TranslateBubbleModel* bubble = factory->model();
1533 ASSERT_TRUE(bubble != NULL);
1534 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE,
1535 bubble->GetViewState());
1536
1537 // Simulate clicking translate.
1538 process()->sink().ClearMessages();
1539 bubble->Translate();
1540 SimulateTranslateScriptURLFetch(false);
1541
1542 // We should not have sent any message to translate to the renderer.
1543 EXPECT_FALSE(GetTranslateMessage(NULL, NULL, NULL));
1544
1545 // And we should have an error infobar showing.
1546 bubble = factory->model();
1547 ASSERT_TRUE(bubble != NULL);
1548 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_ERROR,
1549 bubble->GetViewState());
1550}
1551
1552TEST_F(TranslateManagerBrowserTest, BubbleUnknownLanguage) {
1553 // Prepare for the bubble
1554 CommandLine* command_line = CommandLine::ForCurrentProcess();
1555 command_line->AppendSwitch(switches::kEnableTranslateNewUX);
1556 MockTranslateBubbleFactory* factory = new MockTranslateBubbleFactory;
1557 scoped_ptr<TranslateBubbleFactory> factory_ptr(factory);
1558 TranslateBubbleFactory::SetFactory(factory);
1559
1560 // Simulate navigating to a page ("und" is the string returned by the CLD for
1561 // languages it does not recognize).
1562 SimulateNavigation(GURL("http://www.google.mys"), "und", true);
1563
1564 // We should not have a bubble as we don't know the language.
1565 ASSERT_TRUE(factory->model() == NULL);
1566
1567 // Translate the page anyway throught the context menu.
1568 scoped_ptr<TestRenderViewContextMenu> menu(
1569 TestRenderViewContextMenu::CreateContextMenu(web_contents()));
1570 menu->Init();
1571 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0);
1572
1573 // Check the bubble exists instead of the infobar.
1574 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
1575 ASSERT_TRUE(infobar == NULL);
1576 TranslateBubbleModel* bubble = factory->model();
1577 ASSERT_TRUE(bubble != NULL);
1578 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_TRANSLATING,
1579 bubble->GetViewState());
1580}
1581
1582#endif // !defined(OS_ANDROID) && !defined(OS_IOS)
1583
[email protected]373b4612012-12-15 23:26:411584// 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
[email protected]f8a2e132012-08-31 18:16:201590// Test that session restore restores the translate infobar and other translate
1591// settings.
[email protected]373b4612012-12-15 23:26:411592IN_PROC_BROWSER_TEST_F(InProcessBrowserTest,
1593 MAYBE_PRE_TranslateSessionRestore) {
[email protected]f8a2e132012-08-31 18:16:201594 SessionStartupPref pref(SessionStartupPref::LAST);
1595 SessionStartupPref::SetStartupPref(browser()->profile(), pref);
1596
[email protected]05024712013-07-31 09:46:291597 content::WebContents* current_web_contents =
[email protected]47ae23372013-01-29 01:50:481598 browser()->tab_strip_model()->GetActiveWebContents();
[email protected]f0235f62012-09-28 16:50:571599 TranslateTabHelper* translate_tab_helper =
1600 TranslateTabHelper::FromWebContents(current_web_contents);
[email protected]05024712013-07-31 09:46:291601 content::Source<content::WebContents> source(current_web_contents);
[email protected]f8a2e132012-08-31 18:16:201602
[email protected]27eff892013-05-21 16:40:521603 ui_test_utils::WindowedNotificationObserverWithDetails<
1604 LanguageDetectionDetails>
[email protected]f8a2e132012-08-31 18:16:201605 fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED,
1606 source);
1607
1608 GURL french_url = ui_test_utils::GetTestUrl(
[email protected]650b2d52013-02-10 03:41:451609 base::FilePath(), base::FilePath(FILE_PATH_LITERAL("french_page.html")));
[email protected]f8a2e132012-08-31 18:16:201610 ui_test_utils::NavigateToURL(browser(), french_url);
1611 fr_language_detected_signal.Wait();
[email protected]27eff892013-05-21 16:40:521612 LanguageDetectionDetails details;
[email protected]f8a2e132012-08-31 18:16:201613 EXPECT_TRUE(fr_language_detected_signal.GetDetailsFor(
[email protected]27eff892013-05-21 16:40:521614 source.map_key(), &details));
1615 EXPECT_EQ("fr", details.adopted_language);
[email protected]3155c5272014-01-21 12:38:121616 EXPECT_EQ("fr", translate_tab_helper->GetLanguageState().original_language());
[email protected]f8a2e132012-08-31 18:16:201617}
1618
[email protected]373b4612012-12-15 23:26:411619#if defined (OS_WIN)
1620#define MAYBE_TranslateSessionRestore DISABLED_TranslateSessionRestore
1621#else
1622#define MAYBE_TranslateSessionRestore TranslateSessionRestore
1623#endif
1624IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, MAYBE_TranslateSessionRestore) {
[email protected]05024712013-07-31 09:46:291625 content::WebContents* current_web_contents =
[email protected]47ae23372013-01-29 01:50:481626 browser()->tab_strip_model()->GetActiveWebContents();
[email protected]05024712013-07-31 09:46:291627 content::Source<content::WebContents> source(current_web_contents);
[email protected]f8a2e132012-08-31 18:16:201628
[email protected]27eff892013-05-21 16:40:521629 ui_test_utils::WindowedNotificationObserverWithDetails<
1630 LanguageDetectionDetails>
[email protected]f8a2e132012-08-31 18:16:201631 fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED,
1632 source);
1633 fr_language_detected_signal.Wait();
1634}