blob: 3fc8761893fe00e8c32c341b6554c7de553444a1 [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]f8a2e132012-08-31 18:16:2026#include "chrome/browser/translate/translate_tab_helper.h"
27#include "chrome/browser/ui/browser.h"
[email protected]47ae23372013-01-29 01:50:4828#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]e625b7602013-10-28 09:24:5629#include "chrome/browser/ui/translate/translate_bubble_factory.h"
30#include "chrome/browser/ui/translate/translate_bubble_model.h"
31#include "chrome/browser/ui/translate/translate_bubble_model_impl.h"
32#include "chrome/common/chrome_switches.h"
[email protected]2253b3b2012-06-03 22:39:1533#include "chrome/common/pref_names.h"
34#include "chrome/common/render_messages.h"
[email protected]b2ed9c672013-06-06 23:34:0035#include "chrome/common/translate/language_detection_details.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]2253b3b2012-06-03 22:39:1542#include "content/public/browser/navigation_details.h"
43#include "content/public/browser/navigation_entry.h"
44#include "content/public/browser/notification_details.h"
45#include "content/public/browser/notification_registrar.h"
46#include "content/public/browser/web_contents.h"
[email protected]2253b3b2012-06-03 22:39:1547#include "content/public/test/mock_render_process_host.h"
[email protected]b1e3f202012-06-04 14:45:5048#include "content/public/test/test_renderer_host.h"
[email protected]8f2d8bdd2012-06-19 23:44:0549#include "net/url_request/test_url_fetcher_factory.h"
[email protected]140930a2013-06-21 16:11:0950#include "net/url_request/url_fetcher_delegate.h"
[email protected]2253b3b2012-06-03 22:39:1551#include "testing/gmock/include/gmock/gmock.h"
[email protected]2255a9332013-06-17 05:12:3152#include "third_party/WebKit/public/web/WebContextMenuData.h"
[email protected]2253b3b2012-06-03 22:39:1553
[email protected]2253b3b2012-06-03 22:39:1554using content::RenderViewHostTester;
[email protected]2253b3b2012-06-03 22:39:1555
[email protected]f8a2e132012-08-31 18:16:2056// An observer that keeps track of whether a navigation entry was committed.
57class NavEntryCommittedObserver : public content::NotificationObserver {
58 public:
[email protected]05024712013-07-31 09:46:2959 explicit NavEntryCommittedObserver(content::WebContents* web_contents) {
[email protected]f8a2e132012-08-31 18:16:2060 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
[email protected]05024712013-07-31 09:46:2961 content::Source<content::NavigationController>(
62 &web_contents->GetController()));
[email protected]f8a2e132012-08-31 18:16:2063 }
64
65 virtual void Observe(int type,
66 const content::NotificationSource& source,
[email protected]b94584a2013-02-07 03:02:0867 const content::NotificationDetails& details) OVERRIDE {
[email protected]f8a2e132012-08-31 18:16:2068 DCHECK(type == content::NOTIFICATION_NAV_ENTRY_COMMITTED);
69 details_ =
70 *(content::Details<content::LoadCommittedDetails>(details).ptr());
71 }
72
[email protected]05024712013-07-31 09:46:2973 const content::LoadCommittedDetails& load_committed_details() const {
[email protected]f8a2e132012-08-31 18:16:2074 return details_;
75 }
76
77 private:
78 content::LoadCommittedDetails details_;
79 content::NotificationRegistrar registrar_;
80
81 DISALLOW_COPY_AND_ASSIGN(NavEntryCommittedObserver);
82};
83
[email protected]67755672013-04-08 23:25:3284class TranslateManagerBrowserTest : public ChromeRenderViewHostTestHarness,
85 public content::NotificationObserver {
[email protected]2253b3b2012-06-03 22:39:1586 public:
[email protected]67755672013-04-08 23:25:3287 TranslateManagerBrowserTest()
88 : pref_callback_(
89 base::Bind(&TranslateManagerBrowserTest::OnPreferenceChanged,
[email protected]ec04d3f2013-06-06 21:31:3990 base::Unretained(this))) {
[email protected]2253b3b2012-06-03 22:39:1591 }
92
93 // Simulates navigating to a page and getting the page contents and language
94 // for that navigation.
95 void SimulateNavigation(const GURL& url,
96 const std::string& lang,
97 bool page_translatable) {
98 NavigateAndCommit(url);
99 SimulateOnTranslateLanguageDetermined(lang, page_translatable);
100 }
101
102 void SimulateOnTranslateLanguageDetermined(const std::string& lang,
103 bool page_translatable) {
[email protected]27eff892013-05-21 16:40:52104 LanguageDetectionDetails details;
105 details.adopted_language = lang;
[email protected]2253b3b2012-06-03 22:39:15106 RenderViewHostTester::TestOnMessageReceived(
107 rvh(),
108 ChromeViewHostMsg_TranslateLanguageDetermined(
[email protected]27eff892013-05-21 16:40:52109 0, details, page_translatable));
[email protected]2253b3b2012-06-03 22:39:15110 }
111
[email protected]05024712013-07-31 09:46:29112 void SimulateOnPageTranslated(int routing_id,
113 const std::string& source_lang,
114 const std::string& target_lang,
115 TranslateErrors::Type error) {
116 RenderViewHostTester::TestOnMessageReceived(
117 rvh(),
118 ChromeViewHostMsg_PageTranslated(
119 routing_id, 0, source_lang, target_lang, error));
120 }
121
122 void SimulateOnPageTranslated(const std::string& source_lang,
123 const std::string& target_lang) {
124 SimulateOnPageTranslated(0, source_lang, target_lang,
125 TranslateErrors::NONE);
126 }
127
[email protected]2253b3b2012-06-03 22:39:15128 bool GetTranslateMessage(int* page_id,
129 std::string* original_lang,
130 std::string* target_lang) {
131 const IPC::Message* message =
132 process()->sink().GetFirstMessageMatching(
133 ChromeViewMsg_TranslatePage::ID);
134 if (!message)
135 return false;
136 Tuple4<int, std::string, std::string, std::string> translate_param;
137 ChromeViewMsg_TranslatePage::Read(message, &translate_param);
138 if (page_id)
139 *page_id = translate_param.a;
140 // Ignore translate_param.b which is the script injected in the page.
141 if (original_lang)
142 *original_lang = translate_param.c;
143 if (target_lang)
144 *target_lang = translate_param.d;
145 return true;
146 }
147
[email protected]4f822f022012-12-20 19:11:42148 InfoBarService* infobar_service() {
149 return InfoBarService::FromWebContents(web_contents());
[email protected]2253b3b2012-06-03 22:39:15150 }
151
152 // Returns the translate infobar if there is 1 infobar and it is a translate
153 // infobar.
154 TranslateInfoBarDelegate* GetTranslateInfoBar() {
[email protected]a608d172013-04-23 22:27:37155 return (infobar_service()->infobar_count() == 1) ?
156 infobar_service()->infobar_at(0)->AsTranslateInfoBarDelegate() : NULL;
[email protected]2253b3b2012-06-03 22:39:15157 }
158
159 // If there is 1 infobar and it is a translate infobar, closes it and returns
160 // true. Returns false otherwise.
161 bool CloseTranslateInfoBar() {
162 InfoBarDelegate* infobar = GetTranslateInfoBar();
163 if (!infobar)
164 return false;
165 infobar->InfoBarDismissed(); // Simulates closing the infobar.
[email protected]4f822f022012-12-20 19:11:42166 infobar_service()->RemoveInfoBar(infobar);
[email protected]2253b3b2012-06-03 22:39:15167 return true;
168 }
169
170 // Checks whether |infobar| has been removed and clears the removed infobar
171 // list.
172 bool CheckInfoBarRemovedAndReset(InfoBarDelegate* delegate) {
173 bool found = removed_infobars_.count(delegate) != 0;
174 removed_infobars_.clear();
175 return found;
176 }
177
[email protected]2253b3b2012-06-03 22:39:15178 void ExpireTranslateScriptImmediately() {
[email protected]140930a2013-06-21 16:11:09179 TranslateManager::GetInstance()->SetTranslateScriptExpirationDelay(0);
[email protected]2253b3b2012-06-03 22:39:15180 }
181
182 // If there is 1 infobar and it is a translate infobar, deny translation and
183 // returns true. Returns false otherwise.
184 bool DenyTranslation() {
185 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
186 if (!infobar)
187 return false;
188 infobar->TranslationDeclined();
[email protected]4f822f022012-12-20 19:11:42189 infobar_service()->RemoveInfoBar(infobar);
[email protected]2253b3b2012-06-03 22:39:15190 return true;
191 }
192
[email protected]b637bcd2013-03-28 12:50:32193 void ReloadAndWait(bool successful_reload) {
[email protected]921441c2012-10-19 22:16:01194 NavEntryCommittedObserver nav_observer(web_contents());
[email protected]b637bcd2013-03-28 12:50:32195 if (successful_reload)
196 Reload();
197 else
198 FailedReload();
[email protected]f8a2e132012-08-31 18:16:20199
200 // Ensures it is really handled a reload.
201 const content::LoadCommittedDetails& nav_details =
[email protected]05024712013-07-31 09:46:29202 nav_observer.load_committed_details();
[email protected]f8a2e132012-08-31 18:16:20203 EXPECT_TRUE(nav_details.entry != NULL); // There was a navigation.
204 EXPECT_EQ(content::NAVIGATION_TYPE_EXISTING_PAGE, nav_details.type);
205
206 // The TranslateManager class processes the navigation entry committed
207 // notification in a posted task; process that task.
[email protected]b3a25092013-05-28 22:08:16208 base::MessageLoop::current()->RunUntilIdle();
[email protected]f8a2e132012-08-31 18:16:20209 }
210
[email protected]2253b3b2012-06-03 22:39:15211 virtual void Observe(int type,
212 const content::NotificationSource& source,
213 const content::NotificationDetails& details) {
214 DCHECK_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, type);
215 removed_infobars_.insert(
216 content::Details<InfoBarRemovedDetails>(details)->first);
217 }
218
[email protected]116129e02012-11-21 17:26:27219 MOCK_METHOD1(OnPreferenceChanged, void(const std::string&));
[email protected]a6a7ced2012-11-01 17:24:18220
[email protected]2253b3b2012-06-03 22:39:15221 protected:
222 virtual void SetUp() {
[email protected]2253b3b2012-06-03 22:39:15223 // Access the TranslateManager singleton so it is created before we call
[email protected]292ccc92012-10-17 12:46:02224 // ChromeRenderViewHostTestHarness::SetUp() to match what's done in Chrome,
[email protected]2253b3b2012-06-03 22:39:15225 // where the TranslateManager is created before the WebContents. This
226 // matters as they both register for similar events and we want the
227 // notifications to happen in the same sequence (TranslateManager first,
228 // WebContents second). Also clears the translate script so it is fetched
229 // everytime and sets the expiration delay to a large value by default (in
230 // case it was zeroed in a previous test).
231 TranslateManager::GetInstance()->ClearTranslateScript();
232 TranslateManager::GetInstance()->
[email protected]140930a2013-06-21 16:11:09233 SetTranslateScriptExpirationDelay(60 * 60 * 1000);
[email protected]9ad06a12013-05-10 14:49:53234 TranslateManager::GetInstance()->set_translate_max_reload_attemps(0);
[email protected]2253b3b2012-06-03 22:39:15235
[email protected]292ccc92012-10-17 12:46:02236 ChromeRenderViewHostTestHarness::SetUp();
[email protected]4f822f022012-12-20 19:11:42237 InfoBarService::CreateForWebContents(web_contents());
[email protected]292ccc92012-10-17 12:46:02238 TranslateTabHelper::CreateForWebContents(web_contents());
[email protected]2253b3b2012-06-03 22:39:15239
240 notification_registrar_.Add(this,
241 chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED,
[email protected]4f822f022012-12-20 19:11:42242 content::Source<InfoBarService>(infobar_service()));
[email protected]2253b3b2012-06-03 22:39:15243 }
244
245 virtual void TearDown() {
246 process()->sink().ClearMessages();
247
248 notification_registrar_.Remove(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
[email protected]292ccc92012-10-17 12:46:02252 ChromeRenderViewHostTestHarness::TearDown();
[email protected]2253b3b2012-06-03 22:39:15253 }
254
255 void SimulateTranslateScriptURLFetch(bool success) {
[email protected]05024712013-07-31 09:46:29256 net::TestURLFetcher* fetcher =
257 url_fetcher_factory_.GetFetcherByID(TranslateScript::kFetcherId);
[email protected]2253b3b2012-06-03 22:39:15258 ASSERT_TRUE(fetcher);
259 net::URLRequestStatus status;
260 status.set_status(success ? net::URLRequestStatus::SUCCESS :
261 net::URLRequestStatus::FAILED);
262 fetcher->set_url(fetcher->GetOriginalURL());
263 fetcher->set_status(status);
264 fetcher->set_response_code(success ? 200 : 500);
265 fetcher->delegate()->OnURLFetchComplete(fetcher);
266 }
267
268 void SimulateSupportedLanguagesURLFetch(
[email protected]7da697a2013-07-12 08:09:46269 bool success,
270 const std::vector<std::string>& languages,
271 bool use_alpha_languages,
272 const std::vector<std::string>& alpha_languages) {
[email protected]2253b3b2012-06-03 22:39:15273 net::URLRequestStatus status;
274 status.set_status(success ? net::URLRequestStatus::SUCCESS :
275 net::URLRequestStatus::FAILED);
276
277 std::string data;
278 if (success) {
[email protected]4df61c702013-05-28 10:55:30279 data = base::StringPrintf(
280 "%s{\"sl\": {\"bla\": \"bla\"}, \"%s\": {",
281 TranslateLanguageList::kLanguageListCallbackName,
282 TranslateLanguageList::kTargetLanguagesKey);
[email protected]2253b3b2012-06-03 22:39:15283 const char* comma = "";
284 for (size_t i = 0; i < languages.size(); ++i) {
285 data += base::StringPrintf(
[email protected]2a4e1ae2013-04-15 14:41:36286 "%s\"%s\": \"UnusedFullName\"", comma, languages[i].c_str());
[email protected]2253b3b2012-06-03 22:39:15287 if (i == 0)
288 comma = ",";
289 }
[email protected]7da697a2013-07-12 08:09:46290
291 if (use_alpha_languages) {
292 data += base::StringPrintf("},\"%s\": {",
293 TranslateLanguageList::kAlphaLanguagesKey);
294 comma = "";
295 for (size_t i = 0; i < alpha_languages.size(); ++i) {
296 data += base::StringPrintf("%s\"%s\": 1", comma,
297 alpha_languages[i].c_str());
298 if (i == 0)
299 comma = ",";
300 }
301 }
302
[email protected]2253b3b2012-06-03 22:39:15303 data += "}})";
304 }
[email protected]05024712013-07-31 09:46:29305 net::TestURLFetcher* fetcher =
306 url_fetcher_factory_.GetFetcherByID(TranslateLanguageList::kFetcherId);
[email protected]7da697a2013-07-12 08:09:46307 ASSERT_TRUE(fetcher != NULL);
308 fetcher->set_url(fetcher->GetOriginalURL());
309 fetcher->set_status(status);
310 fetcher->set_response_code(success ? 200 : 500);
311 fetcher->SetResponseString(data);
312 fetcher->delegate()->OnURLFetchComplete(fetcher);
[email protected]2253b3b2012-06-03 22:39:15313 }
314
315 void SetPrefObserverExpectation(const char* path) {
[email protected]116129e02012-11-21 17:26:27316 EXPECT_CALL(*this, OnPreferenceChanged(std::string(path)));
[email protected]2253b3b2012-06-03 22:39:15317 }
318
[email protected]116129e02012-11-21 17:26:27319 PrefChangeRegistrar::NamedChangeCallback pref_callback_;
320
[email protected]2253b3b2012-06-03 22:39:15321 private:
322 content::NotificationRegistrar notification_registrar_;
[email protected]8f2d8bdd2012-06-19 23:44:05323 net::TestURLFetcherFactory url_fetcher_factory_;
[email protected]2253b3b2012-06-03 22:39:15324
325 // The infobars that have been removed.
326 // WARNING: the pointers point to deleted objects, use only for comparison.
327 std::set<InfoBarDelegate*> removed_infobars_;
328
[email protected]67755672013-04-08 23:25:32329 DISALLOW_COPY_AND_ASSIGN(TranslateManagerBrowserTest);
[email protected]2253b3b2012-06-03 22:39:15330};
331
[email protected]e625b7602013-10-28 09:24:56332class MockTranslateBubbleFactory : public TranslateBubbleFactory {
333 public:
334 MockTranslateBubbleFactory() {
335 }
336
337 virtual void ShowImplementation(
338 BrowserWindow* window,
339 content::WebContents* web_contents,
340 TranslateBubbleModel::ViewState view_state) OVERRIDE {
341 if (model_) {
342 model_->SetViewState(view_state);
343 return;
344 }
345
346 TranslateTabHelper* translate_tab_helper =
347 TranslateTabHelper::FromWebContents(web_contents);
348 std::string source_language =
349 translate_tab_helper->language_state().original_language();
350 std::string target_language = TranslateManager::GetLanguageCode(
351 g_browser_process->GetApplicationLocale());
352 scoped_ptr<TranslateUIDelegate> ui_delegate(
353 new TranslateUIDelegate(web_contents,
354 source_language,
355 target_language));
356 model_.reset(
357 new TranslateBubbleModelImpl(view_state, ui_delegate.Pass()));
358 }
359
360 TranslateBubbleModel* model() { return model_.get(); }
361
362 private:
363 scoped_ptr<TranslateBubbleModel> model_;
364
365 DISALLOW_COPY_AND_ASSIGN(MockTranslateBubbleFactory);
366};
367
[email protected]2253b3b2012-06-03 22:39:15368namespace {
369
370class TestRenderViewContextMenu : public RenderViewContextMenu {
371 public:
372 static TestRenderViewContextMenu* CreateContextMenu(
[email protected]05024712013-07-31 09:46:29373 content::WebContents* web_contents) {
[email protected]2253b3b2012-06-03 22:39:15374 content::ContextMenuParams params;
375 params.media_type = WebKit::WebContextMenuData::MediaTypeNone;
376 params.x = 0;
377 params.y = 0;
[email protected]6462f81112013-08-23 23:00:37378 params.has_image_contents = true;
[email protected]2253b3b2012-06-03 22:39:15379 params.media_flags = 0;
380 params.spellcheck_enabled = false;
381 params.is_editable = false;
382 params.page_url = web_contents->GetController().GetActiveEntry()->GetURL();
383#if defined(OS_MACOSX)
384 params.writing_direction_default = 0;
385 params.writing_direction_left_to_right = 0;
386 params.writing_direction_right_to_left = 0;
387#endif // OS_MACOSX
[email protected]05024712013-07-31 09:46:29388 params.edit_flags = WebKit::WebContextMenuData::CanTranslate;
[email protected]2253b3b2012-06-03 22:39:15389 return new TestRenderViewContextMenu(web_contents, params);
390 }
391
392 bool IsItemPresent(int id) {
393 return menu_model_.GetIndexOfCommandId(id) != -1;
394 }
395
[email protected]b94584a2013-02-07 03:02:08396 virtual void PlatformInit() OVERRIDE { }
397 virtual void PlatformCancel() OVERRIDE { }
[email protected]2253b3b2012-06-03 22:39:15398 virtual bool GetAcceleratorForCommandId(
399 int command_id,
[email protected]b94584a2013-02-07 03:02:08400 ui::Accelerator* accelerator) OVERRIDE { return false; }
[email protected]2253b3b2012-06-03 22:39:15401
402 private:
[email protected]05024712013-07-31 09:46:29403 TestRenderViewContextMenu(content::WebContents* web_contents,
[email protected]2253b3b2012-06-03 22:39:15404 const content::ContextMenuParams& params)
405 : RenderViewContextMenu(web_contents, params) {
406 }
407
408 DISALLOW_COPY_AND_ASSIGN(TestRenderViewContextMenu);
409};
410
411} // namespace
412
[email protected]67755672013-04-08 23:25:32413TEST_F(TranslateManagerBrowserTest, NormalTranslate) {
[email protected]2253b3b2012-06-03 22:39:15414 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
415
416 // We should have an infobar.
417 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
418 ASSERT_TRUE(infobar != NULL);
[email protected]2ebd22d2013-01-07 17:04:27419 EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE,
420 infobar->infobar_type());
[email protected]2253b3b2012-06-03 22:39:15421
422 // Simulate clicking translate.
423 process()->sink().ClearMessages();
424 infobar->Translate();
425
426 // The "Translating..." infobar should be showing.
427 infobar = GetTranslateInfoBar();
428 ASSERT_TRUE(infobar != NULL);
[email protected]2ebd22d2013-01-07 17:04:27429 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATING, infobar->infobar_type());
[email protected]2253b3b2012-06-03 22:39:15430
431 // Simulate the translate script being retrieved (it only needs to be done
432 // once in the test as it is cached).
433 SimulateTranslateScriptURLFetch(true);
434
435 // Test that we sent the right message to the renderer.
436 int page_id = 0;
437 std::string original_lang, target_lang;
438 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
439 EXPECT_EQ("fr", original_lang);
440 EXPECT_EQ("en", target_lang);
441
442 // Simulate the render notifying the translation has been done.
[email protected]05024712013-07-31 09:46:29443 SimulateOnPageTranslated("fr", "en");
[email protected]2253b3b2012-06-03 22:39:15444
445 // The after translate infobar should be showing.
446 infobar = GetTranslateInfoBar();
447 ASSERT_TRUE(infobar != NULL);
[email protected]2ebd22d2013-01-07 17:04:27448 EXPECT_EQ(TranslateInfoBarDelegate::AFTER_TRANSLATE, infobar->infobar_type());
[email protected]2253b3b2012-06-03 22:39:15449
[email protected]9f943482012-09-21 01:05:13450 // Simulate changing the original language and translating.
[email protected]2253b3b2012-06-03 22:39:15451 process()->sink().ClearMessages();
[email protected]9f943482012-09-21 01:05:13452 std::string new_original_lang = infobar->language_code_at(0);
453 infobar->set_original_language_index(0);
454 infobar->Translate();
[email protected]2253b3b2012-06-03 22:39:15455 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
456 EXPECT_EQ(new_original_lang, original_lang);
457 EXPECT_EQ("en", target_lang);
458 // Simulate the render notifying the translation has been done.
[email protected]05024712013-07-31 09:46:29459 SimulateOnPageTranslated(new_original_lang, "en");
[email protected]2253b3b2012-06-03 22:39:15460 // infobar is now invalid.
461 TranslateInfoBarDelegate* new_infobar = GetTranslateInfoBar();
462 ASSERT_TRUE(new_infobar != NULL);
463 infobar = new_infobar;
464
[email protected]9f943482012-09-21 01:05:13465 // Simulate changing the target language and translating.
[email protected]2253b3b2012-06-03 22:39:15466 process()->sink().ClearMessages();
[email protected]9f943482012-09-21 01:05:13467 std::string new_target_lang = infobar->language_code_at(1);
468 infobar->set_target_language_index(1);
469 infobar->Translate();
[email protected]2253b3b2012-06-03 22:39:15470 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
471 EXPECT_EQ(new_original_lang, original_lang);
472 EXPECT_EQ(new_target_lang, target_lang);
473 // Simulate the render notifying the translation has been done.
[email protected]05024712013-07-31 09:46:29474 SimulateOnPageTranslated(new_original_lang, new_target_lang);
[email protected]2253b3b2012-06-03 22:39:15475 // infobar is now invalid.
476 new_infobar = GetTranslateInfoBar();
477 ASSERT_TRUE(new_infobar != NULL);
[email protected]f8a2e132012-08-31 18:16:20478
479 // Verify reload keeps the same settings.
[email protected]b637bcd2013-03-28 12:50:32480 ReloadAndWait(true);
[email protected]f8a2e132012-08-31 18:16:20481 new_infobar = GetTranslateInfoBar();
482 ASSERT_TRUE(new_infobar != NULL);
[email protected]9f943482012-09-21 01:05:13483 ASSERT_EQ(new_target_lang, infobar->target_language_code());
[email protected]2253b3b2012-06-03 22:39:15484}
485
[email protected]67755672013-04-08 23:25:32486TEST_F(TranslateManagerBrowserTest, TranslateScriptNotAvailable) {
[email protected]2253b3b2012-06-03 22:39:15487 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
488
489 // We should have an infobar.
490 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
491 ASSERT_TRUE(infobar != NULL);
[email protected]2ebd22d2013-01-07 17:04:27492 EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE,
493 infobar->infobar_type());
[email protected]2253b3b2012-06-03 22:39:15494
495 // Simulate clicking translate.
496 process()->sink().ClearMessages();
497 infobar->Translate();
[email protected]2253b3b2012-06-03 22:39:15498 SimulateTranslateScriptURLFetch(false);
499
500 // We should not have sent any message to translate to the renderer.
501 EXPECT_FALSE(GetTranslateMessage(NULL, NULL, NULL));
502
503 // And we should have an error infobar showing.
504 infobar = GetTranslateInfoBar();
505 ASSERT_TRUE(infobar != NULL);
[email protected]2ebd22d2013-01-07 17:04:27506 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR,
507 infobar->infobar_type());
[email protected]2253b3b2012-06-03 22:39:15508}
509
510// Ensures we deal correctly with pages for which the browser does not recognize
511// the language (the translate server may or not detect the language).
[email protected]67755672013-04-08 23:25:32512TEST_F(TranslateManagerBrowserTest, TranslateUnknownLanguage) {
[email protected]2253b3b2012-06-03 22:39:15513 // Simulate navigating to a page ("und" is the string returned by the CLD for
514 // languages it does not recognize).
515 SimulateNavigation(GURL("http://www.google.mys"), "und", true);
516
517 // We should not have an infobar as we don't know the language.
518 ASSERT_TRUE(GetTranslateInfoBar() == NULL);
519
520 // Translate the page anyway throught the context menu.
521 scoped_ptr<TestRenderViewContextMenu> menu(
[email protected]921441c2012-10-19 22:16:01522 TestRenderViewContextMenu::CreateContextMenu(web_contents()));
[email protected]2253b3b2012-06-03 22:39:15523 menu->Init();
[email protected]96bf34e2013-03-13 04:22:14524 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0);
[email protected]2253b3b2012-06-03 22:39:15525
526 // To test that bug #49018 if fixed, make sure we deal correctly with errors.
527 // Simulate a failure to fetch the translate script.
528 SimulateTranslateScriptURLFetch(false);
529 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
530 ASSERT_TRUE(infobar != NULL);
[email protected]2ebd22d2013-01-07 17:04:27531 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR,
532 infobar->infobar_type());
[email protected]cacaa242013-07-19 22:00:36533 EXPECT_TRUE(infobar->is_error());
[email protected]2253b3b2012-06-03 22:39:15534 infobar->MessageInfoBarButtonPressed();
535 SimulateTranslateScriptURLFetch(true); // This time succeed.
536
537 // Simulate the render notifying the translation has been done, the server
538 // having detected the page was in a known and supported language.
[email protected]05024712013-07-31 09:46:29539 SimulateOnPageTranslated("fr", "en");
[email protected]2253b3b2012-06-03 22:39:15540
541 // The after translate infobar should be showing.
542 infobar = GetTranslateInfoBar();
543 ASSERT_TRUE(infobar != NULL);
[email protected]2ebd22d2013-01-07 17:04:27544 EXPECT_EQ(TranslateInfoBarDelegate::AFTER_TRANSLATE, infobar->infobar_type());
[email protected]9f943482012-09-21 01:05:13545 EXPECT_EQ("fr", infobar->original_language_code());
546 EXPECT_EQ("en", infobar->target_language_code());
[email protected]2253b3b2012-06-03 22:39:15547
548 // Let's run the same steps but this time the server detects the page is
549 // already in English.
550 SimulateNavigation(GURL("http://www.google.com"), "und", true);
[email protected]921441c2012-10-19 22:16:01551 menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents()));
[email protected]2253b3b2012-06-03 22:39:15552 menu->Init();
[email protected]96bf34e2013-03-13 04:22:14553 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0);
[email protected]05024712013-07-31 09:46:29554 SimulateOnPageTranslated(1, "en", "en", TranslateErrors::IDENTICAL_LANGUAGES);
[email protected]2253b3b2012-06-03 22:39:15555 infobar = GetTranslateInfoBar();
556 ASSERT_TRUE(infobar != NULL);
[email protected]2ebd22d2013-01-07 17:04:27557 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR,
558 infobar->infobar_type());
559 EXPECT_EQ(TranslateErrors::IDENTICAL_LANGUAGES, infobar->error_type());
[email protected]2253b3b2012-06-03 22:39:15560
561 // Let's run the same steps again but this time the server fails to detect the
562 // page's language (it returns an empty string).
563 SimulateNavigation(GURL("http://www.google.com"), "und", true);
[email protected]921441c2012-10-19 22:16:01564 menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents()));
[email protected]2253b3b2012-06-03 22:39:15565 menu->Init();
[email protected]96bf34e2013-03-13 04:22:14566 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0);
[email protected]05024712013-07-31 09:46:29567 SimulateOnPageTranslated(2, std::string(), "en",
568 TranslateErrors::UNKNOWN_LANGUAGE);
[email protected]2253b3b2012-06-03 22:39:15569 infobar = GetTranslateInfoBar();
570 ASSERT_TRUE(infobar != NULL);
[email protected]2ebd22d2013-01-07 17:04:27571 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR,
572 infobar->infobar_type());
573 EXPECT_EQ(TranslateErrors::UNKNOWN_LANGUAGE, infobar->error_type());
[email protected]2253b3b2012-06-03 22:39:15574}
575
[email protected]f274d372013-07-22 13:08:50576// Tests that we show/don't show an info-bar for the languages.
577TEST_F(TranslateManagerBrowserTest, TestLanguages) {
578 std::vector<std::string> languages;
579 languages.push_back("en");
580 languages.push_back("ja");
581 languages.push_back("fr");
582 languages.push_back("ht");
583 languages.push_back("xx");
584 languages.push_back("zh");
585 languages.push_back("zh-CN");
586 languages.push_back("und");
[email protected]2253b3b2012-06-03 22:39:15587
588 GURL url("http://www.google.com");
[email protected]f274d372013-07-22 13:08:50589 for (size_t i = 0; i < languages.size(); ++i) {
590 std::string lang = languages[i];
[email protected]2253b3b2012-06-03 22:39:15591 SCOPED_TRACE(::testing::Message() << "Iteration " << i <<
[email protected]f274d372013-07-22 13:08:50592 " language=" << lang);
[email protected]2253b3b2012-06-03 22:39:15593
594 // We should not have a translate infobar.
595 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
596 ASSERT_TRUE(infobar == NULL);
597
[email protected]f274d372013-07-22 13:08:50598 SimulateNavigation(url, lang, true);
[email protected]2253b3b2012-06-03 22:39:15599
600 // Verify we have/don't have an info-bar as expected.
601 infobar = GetTranslateInfoBar();
[email protected]f274d372013-07-22 13:08:50602 bool expected = TranslateManager::IsSupportedLanguage(lang) &&
603 lang != "en";
604 EXPECT_EQ(expected, infobar != NULL);
[email protected]2253b3b2012-06-03 22:39:15605
[email protected]2253b3b2012-06-03 22:39:15606 if (infobar != NULL)
607 EXPECT_TRUE(CloseTranslateInfoBar());
608 }
609}
610
611// Test the fetching of languages from the translate server
[email protected]67755672013-04-08 23:25:32612TEST_F(TranslateManagerBrowserTest, FetchLanguagesFromTranslateServer) {
[email protected]2253b3b2012-06-03 22:39:15613 std::vector<std::string> server_languages;
614 // A list of languages to fake being returned by the translate server.
615 server_languages.push_back("aa");
[email protected]462e9092013-06-14 08:39:38616 server_languages.push_back("ak");
[email protected]2253b3b2012-06-03 22:39:15617 server_languages.push_back("ab");
618 server_languages.push_back("en-CA");
[email protected]462e9092013-06-14 08:39:38619 server_languages.push_back("zh");
620 server_languages.push_back("yi");
[email protected]2253b3b2012-06-03 22:39:15621 server_languages.push_back("fr-FR");
[email protected]462e9092013-06-14 08:39:38622 server_languages.push_back("xx");
[email protected]2253b3b2012-06-03 22:39:15623
[email protected]7da697a2013-07-12 08:09:46624 std::vector<std::string> alpha_languages;
625 alpha_languages.push_back("aa");
626 alpha_languages.push_back("yi");
627
[email protected]296ff3162013-06-20 18:14:23628 // First, get the default languages list. Note that calling
629 // GetSupportedLanguages() invokes RequestLanguageList() internally.
[email protected]2253b3b2012-06-03 22:39:15630 std::vector<std::string> default_supported_languages;
631 TranslateManager::GetSupportedLanguages(&default_supported_languages);
632 // To make sure we got the defaults and don't confuse them with the mocks.
633 ASSERT_NE(default_supported_languages.size(), server_languages.size());
634
[email protected]2253b3b2012-06-03 22:39:15635 // Check that we still get the defaults until the URLFetch has completed.
636 std::vector<std::string> current_supported_languages;
637 TranslateManager::GetSupportedLanguages(&current_supported_languages);
638 EXPECT_EQ(default_supported_languages, current_supported_languages);
639
640 // Also check that it didn't change if we failed the URL fetch.
[email protected]7da697a2013-07-12 08:09:46641 SimulateSupportedLanguagesURLFetch(false, std::vector<std::string>(),
642 true, std::vector<std::string>());
[email protected]2253b3b2012-06-03 22:39:15643 current_supported_languages.clear();
644 TranslateManager::GetSupportedLanguages(&current_supported_languages);
645 EXPECT_EQ(default_supported_languages, current_supported_languages);
646
647 // Now check that we got the appropriate set of languages from the server.
[email protected]7da697a2013-07-12 08:09:46648 SimulateSupportedLanguagesURLFetch(true, server_languages,
649 true, alpha_languages);
[email protected]2253b3b2012-06-03 22:39:15650 current_supported_languages.clear();
651 TranslateManager::GetSupportedLanguages(&current_supported_languages);
[email protected]462e9092013-06-14 08:39:38652 // "xx" can't be displayed in the Translate inforbar, so this is eliminated.
653 EXPECT_EQ(server_languages.size() - 1, current_supported_languages.size());
[email protected]2253b3b2012-06-03 22:39:15654 // Not sure we need to guarantee the order of languages, so we find them.
[email protected]2253b3b2012-06-03 22:39:15655 for (size_t i = 0; i < server_languages.size(); ++i) {
[email protected]7da697a2013-07-12 08:09:46656 const std::string& lang = server_languages[i];
657 if (lang == "xx")
[email protected]462e9092013-06-14 08:39:38658 continue;
[email protected]2253b3b2012-06-03 22:39:15659 EXPECT_NE(current_supported_languages.end(),
660 std::find(current_supported_languages.begin(),
661 current_supported_languages.end(),
[email protected]7da697a2013-07-12 08:09:46662 lang));
663 bool is_alpha = std::find(alpha_languages.begin(),
664 alpha_languages.end(),
665 lang) != alpha_languages.end();
666 EXPECT_EQ(TranslateManager::IsAlphaLanguage(lang), is_alpha);
[email protected]2253b3b2012-06-03 22:39:15667 }
[email protected]2253b3b2012-06-03 22:39:15668}
669
[email protected]7da697a2013-07-12 08:09:46670// Test the fetching of languages from the translate server without 'al'
671// parameter.
672TEST_F(TranslateManagerBrowserTest,
673 FetchLanguagesFromTranslateServerWithoutAlpha) {
674 std::vector<std::string> server_languages;
675 server_languages.push_back("aa");
676 server_languages.push_back("ak");
677 server_languages.push_back("ab");
678 server_languages.push_back("en-CA");
679 server_languages.push_back("zh");
680 server_languages.push_back("yi");
681 server_languages.push_back("fr-FR");
682 server_languages.push_back("xx");
[email protected]2253b3b2012-06-03 22:39:15683
[email protected]7da697a2013-07-12 08:09:46684 std::vector<std::string> alpha_languages;
685 alpha_languages.push_back("aa");
686 alpha_languages.push_back("yi");
687
688 // call GetSupportedLanguages to call RequestLanguageList internally.
689 std::vector<std::string> default_supported_languages;
690 TranslateManager::GetSupportedLanguages(&default_supported_languages);
691
692 SimulateSupportedLanguagesURLFetch(true, server_languages,
693 false, alpha_languages);
694
695 std::vector<std::string> current_supported_languages;
696 TranslateManager::GetSupportedLanguages(&current_supported_languages);
697
698 // "xx" can't be displayed in the Translate inforbar, so this is eliminated.
699 EXPECT_EQ(server_languages.size() - 1, current_supported_languages.size());
700
701 for (size_t i = 0; i < server_languages.size(); ++i) {
702 const std::string& lang = server_languages[i];
703 if (lang == "xx")
704 continue;
705 EXPECT_NE(current_supported_languages.end(),
706 std::find(current_supported_languages.begin(),
707 current_supported_languages.end(),
708 lang));
709 EXPECT_FALSE(TranslateManager::IsAlphaLanguage(lang));
710 }
[email protected]2253b3b2012-06-03 22:39:15711}
712
[email protected]2253b3b2012-06-03 22:39:15713// Tests auto-translate on page.
[email protected]67755672013-04-08 23:25:32714TEST_F(TranslateManagerBrowserTest, AutoTranslateOnNavigate) {
[email protected]2253b3b2012-06-03 22:39:15715 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
716
717 // Simulate the user translating.
718 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
719 ASSERT_TRUE(infobar != NULL);
720 infobar->Translate();
721 // Simulate the translate script being retrieved.
722 SimulateTranslateScriptURLFetch(true);
[email protected]05024712013-07-31 09:46:29723 SimulateOnPageTranslated("fr", "en");
[email protected]2253b3b2012-06-03 22:39:15724
725 // Now navigate to a new page in the same language.
726 process()->sink().ClearMessages();
727 SimulateNavigation(GURL("http://news.google.fr"), "fr", true);
728
729 // This should have automatically triggered a translation.
730 int page_id = 0;
731 std::string original_lang, target_lang;
732 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
733 EXPECT_EQ(1, page_id);
734 EXPECT_EQ("fr", original_lang);
735 EXPECT_EQ("en", target_lang);
736
737 // Now navigate to a page in a different language.
738 process()->sink().ClearMessages();
739 SimulateNavigation(GURL("http://news.google.es"), "es", true);
740
741 // This should not have triggered a translate.
742 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
743}
744
745// Tests that multiple OnPageContents do not cause multiple infobars.
[email protected]67755672013-04-08 23:25:32746TEST_F(TranslateManagerBrowserTest, MultipleOnPageContents) {
[email protected]2253b3b2012-06-03 22:39:15747 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
748
749 // Simulate clicking 'Nope' (don't translate).
750 EXPECT_TRUE(DenyTranslation());
[email protected]a608d172013-04-23 22:27:37751 EXPECT_EQ(0U, infobar_service()->infobar_count());
[email protected]2253b3b2012-06-03 22:39:15752
753 // Send a new PageContents, we should not show an infobar.
754 SimulateOnTranslateLanguageDetermined("fr", true);
[email protected]a608d172013-04-23 22:27:37755 EXPECT_EQ(0U, infobar_service()->infobar_count());
[email protected]2253b3b2012-06-03 22:39:15756
757 // Do the same steps but simulate closing the infobar this time.
758 SimulateNavigation(GURL("http://www.youtube.fr"), "fr", true);
759 EXPECT_TRUE(CloseTranslateInfoBar());
[email protected]a608d172013-04-23 22:27:37760 EXPECT_EQ(0U, infobar_service()->infobar_count());
[email protected]2253b3b2012-06-03 22:39:15761 SimulateOnTranslateLanguageDetermined("fr", true);
[email protected]a608d172013-04-23 22:27:37762 EXPECT_EQ(0U, infobar_service()->infobar_count());
[email protected]2253b3b2012-06-03 22:39:15763}
764
[email protected]b637bcd2013-03-28 12:50:32765// Test that reloading the page brings back the infobar if the
766// reload succeeded and does not bring it back the reload fails.
[email protected]67755672013-04-08 23:25:32767TEST_F(TranslateManagerBrowserTest, Reload) {
[email protected]2253b3b2012-06-03 22:39:15768 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
769
[email protected]2253b3b2012-06-03 22:39:15770 EXPECT_TRUE(CloseTranslateInfoBar());
771
[email protected]b637bcd2013-03-28 12:50:32772 // Reload should bring back the infobar if the page succeds
773 ReloadAndWait(true);
[email protected]2253b3b2012-06-03 22:39:15774 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
[email protected]b637bcd2013-03-28 12:50:32775
[email protected]b637bcd2013-03-28 12:50:32776 EXPECT_TRUE(CloseTranslateInfoBar());
777
778 // And not show it if the reload fails
779 ReloadAndWait(false);
780 EXPECT_EQ(NULL, GetTranslateInfoBar());
[email protected]9ad06a12013-05-10 14:49:53781
782 // Set reload attempts to a high value, we will not see the infobar
783 // immediatly.
784 TranslateManager::GetInstance()->set_translate_max_reload_attemps(100);
785 ReloadAndWait(true);
786 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
[email protected]2253b3b2012-06-03 22:39:15787}
788
789// Test that reloading the page by way of typing again the URL in the
790// location bar brings back the infobar.
[email protected]67755672013-04-08 23:25:32791TEST_F(TranslateManagerBrowserTest, ReloadFromLocationBar) {
[email protected]2253b3b2012-06-03 22:39:15792 GURL url("http://www.google.fr");
[email protected]2253b3b2012-06-03 22:39:15793 SimulateNavigation(url, "fr", true);
794
[email protected]2253b3b2012-06-03 22:39:15795 EXPECT_TRUE(CloseTranslateInfoBar());
796
797 // Create a pending navigation and simulate a page load. That should be the
798 // equivalent of typing the URL again in the location bar.
[email protected]921441c2012-10-19 22:16:01799 NavEntryCommittedObserver nav_observer(web_contents());
800 web_contents()->GetController().LoadURL(url, content::Referrer(),
[email protected]05024712013-07-31 09:46:29801 content::PAGE_TRANSITION_TYPED,
802 std::string());
[email protected]2253b3b2012-06-03 22:39:15803 rvh_tester()->SendNavigate(0, url);
804
805 // Test that we are really getting a same page navigation, the test would be
806 // useless if it was not the case.
807 const content::LoadCommittedDetails& nav_details =
[email protected]05024712013-07-31 09:46:29808 nav_observer.load_committed_details();
[email protected]2253b3b2012-06-03 22:39:15809 EXPECT_TRUE(nav_details.entry != NULL); // There was a navigation.
810 EXPECT_EQ(content::NAVIGATION_TYPE_SAME_PAGE, nav_details.type);
811
812 // The TranslateManager class processes the navigation entry committed
813 // notification in a posted task; process that task.
[email protected]b3a25092013-05-28 22:08:16814 base::MessageLoop::current()->RunUntilIdle();
[email protected]2253b3b2012-06-03 22:39:15815 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
816}
817
818// Tests that a closed translate infobar does not reappear when navigating
819// in-page.
[email protected]67755672013-04-08 23:25:32820TEST_F(TranslateManagerBrowserTest, CloseInfoBarInPageNavigation) {
[email protected]2253b3b2012-06-03 22:39:15821 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
822
[email protected]2253b3b2012-06-03 22:39:15823 EXPECT_TRUE(CloseTranslateInfoBar());
824
825 // Navigate in page, no infobar should be shown.
[email protected]05024712013-07-31 09:46:29826 SimulateNavigation(GURL("http://www.google.fr/#ref1"), "fr", true);
[email protected]2253b3b2012-06-03 22:39:15827 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
828
829 // Navigate out of page, a new infobar should show.
830 SimulateNavigation(GURL("http://www.google.fr/foot"), "fr", true);
831 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
832}
833
834// Tests that a closed translate infobar does not reappear when navigating
835// in a subframe. (http://crbug.com/48215)
[email protected]67755672013-04-08 23:25:32836TEST_F(TranslateManagerBrowserTest, CloseInfoBarInSubframeNavigation) {
[email protected]2253b3b2012-06-03 22:39:15837 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
838
[email protected]2253b3b2012-06-03 22:39:15839 EXPECT_TRUE(CloseTranslateInfoBar());
840
841 // Simulate a sub-frame auto-navigating.
842 rvh_tester()->SendNavigateWithTransition(
843 1, GURL("http://pub.com"), content::PAGE_TRANSITION_AUTO_SUBFRAME);
844 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
845
846 // Simulate the user navigating in a sub-frame.
847 rvh_tester()->SendNavigateWithTransition(
848 2, GURL("http://pub.com"), content::PAGE_TRANSITION_MANUAL_SUBFRAME);
849 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
850
851 // Navigate out of page, a new infobar should show.
852 SimulateNavigation(GURL("http://www.google.fr/foot"), "fr", true);
853 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
854}
855
[email protected]2253b3b2012-06-03 22:39:15856// Tests that denying translation is sticky when navigating in page.
[email protected]67755672013-04-08 23:25:32857TEST_F(TranslateManagerBrowserTest, DenyTranslateInPageNavigation) {
[email protected]2253b3b2012-06-03 22:39:15858 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
859
860 // Simulate clicking 'Nope' (don't translate).
861 EXPECT_TRUE(DenyTranslation());
862
863 // Navigate in page, no infobar should be shown.
864 SimulateNavigation(GURL("http://www.google.fr/#ref1"), "fr", true);
865 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
866
867 // Navigate out of page, a new infobar should show.
868 SimulateNavigation(GURL("http://www.google.fr/foot"), "fr", true);
869 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
870}
871
872// Tests that after translating and closing the infobar, the infobar does not
873// return when navigating in page.
[email protected]67755672013-04-08 23:25:32874TEST_F(TranslateManagerBrowserTest, TranslateCloseInfoBarInPageNavigation) {
[email protected]2253b3b2012-06-03 22:39:15875 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
876
877 // Simulate the user translating.
878 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
879 ASSERT_TRUE(infobar != NULL);
880 infobar->Translate();
881 // Simulate the translate script being retrieved.
882 SimulateTranslateScriptURLFetch(true);
[email protected]05024712013-07-31 09:46:29883 SimulateOnPageTranslated("fr", "en");
[email protected]2253b3b2012-06-03 22:39:15884
[email protected]2253b3b2012-06-03 22:39:15885 EXPECT_TRUE(CloseTranslateInfoBar());
886
887 // Navigate in page, no infobar should be shown.
888 SimulateNavigation(GURL("http://www.google.fr/#ref1"), "fr", true);
889 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
890
891 // Navigate out of page, a new infobar should show.
892 // Note that we navigate to a page in a different language so we don't trigger
893 // the auto-translate feature (it would translate the page automatically and
894 // the before translate inforbar would not be shown).
895 SimulateNavigation(GURL("http://www.google.de"), "de", true);
896 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
897}
898
899// Tests that the after translate the infobar still shows when navigating
900// in-page.
[email protected]67755672013-04-08 23:25:32901TEST_F(TranslateManagerBrowserTest, TranslateInPageNavigation) {
[email protected]2253b3b2012-06-03 22:39:15902 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
903
904 // Simulate the user translating.
905 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
906 ASSERT_TRUE(infobar != NULL);
907 infobar->Translate();
[email protected]2253b3b2012-06-03 22:39:15908 SimulateTranslateScriptURLFetch(true);
[email protected]05024712013-07-31 09:46:29909 SimulateOnPageTranslated("fr", "en");
[email protected]2253b3b2012-06-03 22:39:15910 // The after translate infobar is showing.
911 infobar = GetTranslateInfoBar();
912 ASSERT_TRUE(infobar != NULL);
913
914 // Navigate out of page, a new infobar should show.
915 // See note in TranslateCloseInfoBarInPageNavigation test on why it is
916 // important to navigate to a page in a different language for this test.
917 SimulateNavigation(GURL("http://www.google.de"), "de", true);
918 // The old infobar is gone.
919 EXPECT_TRUE(CheckInfoBarRemovedAndReset(infobar));
920 // And there is a new one.
921 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
922}
923
924// Tests that no translate infobar is shown when navigating to a page in an
925// unsupported language.
[email protected]67755672013-04-08 23:25:32926TEST_F(TranslateManagerBrowserTest, CLDReportsUnsupportedPageLanguage) {
[email protected]2253b3b2012-06-03 22:39:15927 // Simulate navigating to a page and getting an unsupported language.
928 SimulateNavigation(GURL("http://www.google.com"), "qbz", true);
929
930 // No info-bar should be shown.
931 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
932}
933
934// Tests that we deal correctly with unsupported languages returned by the
935// server.
936// The translation server might return a language we don't support.
[email protected]67755672013-04-08 23:25:32937TEST_F(TranslateManagerBrowserTest, ServerReportsUnsupportedLanguage) {
[email protected]2253b3b2012-06-03 22:39:15938 SimulateNavigation(GURL("http://mail.google.fr"), "fr", true);
939 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
940 ASSERT_TRUE(infobar != NULL);
941 process()->sink().ClearMessages();
942 infobar->Translate();
943 SimulateTranslateScriptURLFetch(true);
944 // Simulate the render notifying the translation has been done, but it
945 // reports a language we don't support.
[email protected]05024712013-07-31 09:46:29946 SimulateOnPageTranslated("qbz", "en");
[email protected]2253b3b2012-06-03 22:39:15947
948 // An error infobar should be showing to report that we don't support this
949 // language.
950 infobar = GetTranslateInfoBar();
951 ASSERT_TRUE(infobar != NULL);
[email protected]2ebd22d2013-01-07 17:04:27952 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR,
953 infobar->infobar_type());
[email protected]2253b3b2012-06-03 22:39:15954
955 // This infobar should have a button (so the string should not be empty).
956 ASSERT_FALSE(infobar->GetMessageInfoBarButtonText().empty());
957
958 // Pressing the button on that infobar should revert to the original language.
959 process()->sink().ClearMessages();
960 infobar->MessageInfoBarButtonPressed();
961 const IPC::Message* message =
962 process()->sink().GetFirstMessageMatching(
963 ChromeViewMsg_RevertTranslation::ID);
964 EXPECT_TRUE(message != NULL);
965 // And it should have removed the infobar.
966 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
967}
968
969// Tests that no translate infobar is shown and context menu is disabled, when
970// Chrome is in a language that the translate server does not support.
[email protected]67755672013-04-08 23:25:32971TEST_F(TranslateManagerBrowserTest, UnsupportedUILanguage) {
[email protected]2253b3b2012-06-03 22:39:15972 std::string original_lang = g_browser_process->GetApplicationLocale();
973 g_browser_process->SetApplicationLocale("qbz");
974
975 // Make sure that the accept language list only contains unsupported languages
976 Profile* profile =
[email protected]921441c2012-10-19 22:16:01977 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
[email protected]2253b3b2012-06-03 22:39:15978 PrefService* prefs = profile->GetPrefs();
979 prefs->SetString(prefs::kAcceptLanguages, "qbz");
980
981 // Simulate navigating to a page in a language supported by the translate
982 // server.
983 SimulateNavigation(GURL("http://www.google.com"), "en", true);
984
985 // No info-bar should be shown.
986 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
987
988 // And the context menu option should be disabled too.
989 scoped_ptr<TestRenderViewContextMenu> menu(
[email protected]921441c2012-10-19 22:16:01990 TestRenderViewContextMenu::CreateContextMenu(web_contents()));
[email protected]2253b3b2012-06-03 22:39:15991 menu->Init();
992 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
993 EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
994
995 g_browser_process->SetApplicationLocale(original_lang);
996}
997
998// Tests that the first supported accept language is selected
[email protected]67755672013-04-08 23:25:32999TEST_F(TranslateManagerBrowserTest, TranslateAcceptLanguage) {
[email protected]2253b3b2012-06-03 22:39:151000 // Set locate to non-existant language
1001 std::string original_lang = g_browser_process->GetApplicationLocale();
1002 g_browser_process->SetApplicationLocale("qbz");
1003
1004 // Set Qbz and French as the only accepted languages
1005 Profile* profile =
[email protected]921441c2012-10-19 22:16:011006 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
[email protected]2253b3b2012-06-03 22:39:151007 PrefService* prefs = profile->GetPrefs();
1008 prefs->SetString(prefs::kAcceptLanguages, "qbz,fr");
1009
1010 // Go to a German page
1011 SimulateNavigation(GURL("http://google.de"), "de", true);
1012
1013 // Expect the infobar to pop up
1014 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
1015
1016 // Set Qbz and English-US as the only accepted languages to test the country
1017 // code removal code which was causing a crash as filed in Issue 90106,
1018 // a crash caused by a language with a country code that wasn't recognized.
1019 prefs->SetString(prefs::kAcceptLanguages, "qbz,en-us");
1020
1021 // Go to a German page
1022 SimulateNavigation(GURL("http://google.de"), "de", true);
1023
1024 // Expect the infobar to pop up
1025 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
1026}
1027
1028// Tests that the translate enabled preference is honored.
[email protected]67755672013-04-08 23:25:321029TEST_F(TranslateManagerBrowserTest, TranslateEnabledPref) {
[email protected]2253b3b2012-06-03 22:39:151030 // Make sure the pref allows translate.
1031 Profile* profile =
[email protected]921441c2012-10-19 22:16:011032 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
[email protected]2253b3b2012-06-03 22:39:151033 PrefService* prefs = profile->GetPrefs();
1034 prefs->SetBoolean(prefs::kEnableTranslate, true);
1035
[email protected]2253b3b2012-06-03 22:39:151036 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
1037
1038 // An infobar should be shown.
1039 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
1040 EXPECT_TRUE(infobar != NULL);
1041
1042 // Disable translate.
1043 prefs->SetBoolean(prefs::kEnableTranslate, false);
1044
1045 // Navigate to a new page, that should close the previous infobar.
1046 GURL url("http://www.youtube.fr");
1047 NavigateAndCommit(url);
1048 infobar = GetTranslateInfoBar();
1049 EXPECT_TRUE(infobar == NULL);
1050
1051 // Simulate getting the page contents and language, that should not trigger
1052 // a translate infobar.
1053 SimulateOnTranslateLanguageDetermined("fr", true);
1054 infobar = GetTranslateInfoBar();
1055 EXPECT_TRUE(infobar == NULL);
1056}
1057
1058// Tests the "Never translate <language>" pref.
[email protected]67755672013-04-08 23:25:321059TEST_F(TranslateManagerBrowserTest, NeverTranslateLanguagePref) {
[email protected]2253b3b2012-06-03 22:39:151060 GURL url("http://www.google.fr");
1061 SimulateNavigation(url, "fr", true);
1062
1063 // An infobar should be shown.
1064 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
1065
1066 // Select never translate this language.
1067 Profile* profile =
[email protected]921441c2012-10-19 22:16:011068 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
[email protected]2253b3b2012-06-03 22:39:151069 PrefService* prefs = profile->GetPrefs();
1070 PrefChangeRegistrar registrar;
1071 registrar.Init(prefs);
[email protected]032ae93a2013-08-30 09:15:031072 registrar.Add(TranslatePrefs::kPrefTranslateBlockedLanguages,
[email protected]116129e02012-11-21 17:26:271073 pref_callback_);
[email protected]2253b3b2012-06-03 22:39:151074 TranslatePrefs translate_prefs(prefs);
[email protected]5d37b22982013-06-25 06:07:001075 EXPECT_FALSE(translate_prefs.IsBlockedLanguage("fr"));
[email protected]f9db7e02013-06-17 10:29:431076 EXPECT_TRUE(translate_prefs.CanTranslateLanguage(profile, "fr"));
[email protected]032ae93a2013-08-30 09:15:031077 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateBlockedLanguages);
[email protected]5d37b22982013-06-25 06:07:001078 translate_prefs.BlockLanguage("fr");
1079 EXPECT_TRUE(translate_prefs.IsBlockedLanguage("fr"));
[email protected]f9db7e02013-06-17 10:29:431080 EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(url.host()));
1081 EXPECT_FALSE(translate_prefs.CanTranslateLanguage(profile, "fr"));
[email protected]2253b3b2012-06-03 22:39:151082
[email protected]2253b3b2012-06-03 22:39:151083 EXPECT_TRUE(CloseTranslateInfoBar());
1084
1085 // Navigate to a new page also in French.
1086 SimulateNavigation(GURL("http://wwww.youtube.fr"), "fr", true);
1087
1088 // There should not be a translate infobar.
1089 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
1090
1091 // Remove the language from the blacklist.
[email protected]032ae93a2013-08-30 09:15:031092 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateBlockedLanguages);
[email protected]5d37b22982013-06-25 06:07:001093 translate_prefs.UnblockLanguage("fr");
1094 EXPECT_FALSE(translate_prefs.IsBlockedLanguage("fr"));
[email protected]f9db7e02013-06-17 10:29:431095 EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(url.host()));
1096 EXPECT_TRUE(translate_prefs.CanTranslateLanguage(profile, "fr"));
[email protected]2253b3b2012-06-03 22:39:151097
1098 // Navigate to a page in French.
1099 SimulateNavigation(url, "fr", true);
1100
1101 // There should be a translate infobar.
1102 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
1103}
1104
1105// Tests the "Never translate this site" pref.
[email protected]67755672013-04-08 23:25:321106TEST_F(TranslateManagerBrowserTest, NeverTranslateSitePref) {
[email protected]2253b3b2012-06-03 22:39:151107 GURL url("http://www.google.fr");
1108 std::string host(url.host());
1109 SimulateNavigation(url, "fr", true);
1110
1111 // An infobar should be shown.
1112 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
1113
1114 // Select never translate this site.
1115 Profile* profile =
[email protected]921441c2012-10-19 22:16:011116 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
[email protected]2253b3b2012-06-03 22:39:151117 PrefService* prefs = profile->GetPrefs();
1118 PrefChangeRegistrar registrar;
1119 registrar.Init(prefs);
[email protected]116129e02012-11-21 17:26:271120 registrar.Add(TranslatePrefs::kPrefTranslateSiteBlacklist, pref_callback_);
[email protected]2253b3b2012-06-03 22:39:151121 TranslatePrefs translate_prefs(prefs);
1122 EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(host));
[email protected]f9db7e02013-06-17 10:29:431123 EXPECT_TRUE(translate_prefs.CanTranslateLanguage(profile, "fr"));
[email protected]2253b3b2012-06-03 22:39:151124 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateSiteBlacklist);
1125 translate_prefs.BlacklistSite(host);
1126 EXPECT_TRUE(translate_prefs.IsSiteBlacklisted(host));
[email protected]f9db7e02013-06-17 10:29:431127 EXPECT_TRUE(translate_prefs.CanTranslateLanguage(profile, "fr"));
[email protected]2253b3b2012-06-03 22:39:151128
[email protected]2253b3b2012-06-03 22:39:151129 EXPECT_TRUE(CloseTranslateInfoBar());
1130
1131 // Navigate to a new page also on the same site.
1132 SimulateNavigation(GURL("http://www.google.fr/hello"), "fr", true);
1133
1134 // There should not be a translate infobar.
1135 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
1136
1137 // Remove the site from the blacklist.
1138 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateSiteBlacklist);
1139 translate_prefs.RemoveSiteFromBlacklist(host);
1140 EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(host));
[email protected]f9db7e02013-06-17 10:29:431141 EXPECT_TRUE(translate_prefs.CanTranslateLanguage(profile, "fr"));
[email protected]2253b3b2012-06-03 22:39:151142
1143 // Navigate to a page in French.
1144 SimulateNavigation(url, "fr", true);
1145
1146 // There should be a translate infobar.
1147 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
1148}
1149
1150// Tests the "Always translate this language" pref.
[email protected]67755672013-04-08 23:25:321151TEST_F(TranslateManagerBrowserTest, AlwaysTranslateLanguagePref) {
[email protected]2253b3b2012-06-03 22:39:151152 // Select always translate French to English.
1153 Profile* profile =
[email protected]921441c2012-10-19 22:16:011154 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
[email protected]2253b3b2012-06-03 22:39:151155 PrefService* prefs = profile->GetPrefs();
1156 PrefChangeRegistrar registrar;
1157 registrar.Init(prefs);
[email protected]116129e02012-11-21 17:26:271158 registrar.Add(TranslatePrefs::kPrefTranslateWhitelists, pref_callback_);
[email protected]2253b3b2012-06-03 22:39:151159 TranslatePrefs translate_prefs(prefs);
1160 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateWhitelists);
1161 translate_prefs.WhitelistLanguagePair("fr", "en");
1162
1163 // Load a page in French.
1164 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
1165
1166 // It should have triggered an automatic translation to English.
1167
1168 // The translating infobar should be showing.
1169 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
1170 ASSERT_TRUE(infobar != NULL);
[email protected]2ebd22d2013-01-07 17:04:271171 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATING, infobar->infobar_type());
[email protected]2253b3b2012-06-03 22:39:151172 SimulateTranslateScriptURLFetch(true);
1173 int page_id = 0;
1174 std::string original_lang, target_lang;
1175 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
1176 EXPECT_EQ("fr", original_lang);
1177 EXPECT_EQ("en", target_lang);
1178 process()->sink().ClearMessages();
1179
1180 // Try another language, it should not be autotranslated.
1181 SimulateNavigation(GURL("http://www.google.es"), "es", true);
1182 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
1183 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
1184 EXPECT_TRUE(CloseTranslateInfoBar());
1185
1186 // Let's switch to incognito mode, it should not be autotranslated in that
1187 // case either.
1188 TestingProfile* test_profile =
[email protected]921441c2012-10-19 22:16:011189 static_cast<TestingProfile*>(web_contents()->GetBrowserContext());
[email protected]9819fd02013-08-22 10:49:391190 test_profile->ForceIncognito(true);
[email protected]2253b3b2012-06-03 22:39:151191 SimulateNavigation(GURL("http://www.youtube.fr"), "fr", true);
1192 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
1193 EXPECT_TRUE(GetTranslateInfoBar() != NULL);
1194 EXPECT_TRUE(CloseTranslateInfoBar());
[email protected]9819fd02013-08-22 10:49:391195 test_profile->ForceIncognito(false); // Get back to non incognito.
[email protected]2253b3b2012-06-03 22:39:151196
1197 // Now revert the always translate pref and make sure we go back to expected
1198 // behavior, which is show a "before translate" infobar.
1199 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateWhitelists);
1200 translate_prefs.RemoveLanguagePairFromWhitelist("fr", "en");
1201 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
1202 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
1203 infobar = GetTranslateInfoBar();
1204 ASSERT_TRUE(infobar != NULL);
[email protected]2ebd22d2013-01-07 17:04:271205 EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE,
1206 infobar->infobar_type());
[email protected]2253b3b2012-06-03 22:39:151207}
1208
1209// Context menu.
[email protected]67755672013-04-08 23:25:321210TEST_F(TranslateManagerBrowserTest, ContextMenu) {
[email protected]2253b3b2012-06-03 22:39:151211 // Blacklist www.google.fr and French for translation.
1212 GURL url("http://www.google.fr");
1213 Profile* profile =
[email protected]921441c2012-10-19 22:16:011214 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
[email protected]2253b3b2012-06-03 22:39:151215 TranslatePrefs translate_prefs(profile->GetPrefs());
[email protected]5d37b22982013-06-25 06:07:001216 translate_prefs.BlockLanguage("fr");
[email protected]2253b3b2012-06-03 22:39:151217 translate_prefs.BlacklistSite(url.host());
[email protected]5d37b22982013-06-25 06:07:001218 EXPECT_TRUE(translate_prefs.IsBlockedLanguage("fr"));
[email protected]2253b3b2012-06-03 22:39:151219 EXPECT_TRUE(translate_prefs.IsSiteBlacklisted(url.host()));
1220
1221 // Simulate navigating to a page in French. The translate menu should show but
1222 // should only be enabled when the page language has been received.
1223 NavigateAndCommit(url);
1224 scoped_ptr<TestRenderViewContextMenu> menu(
[email protected]921441c2012-10-19 22:16:011225 TestRenderViewContextMenu::CreateContextMenu(web_contents()));
[email protected]2253b3b2012-06-03 22:39:151226 menu->Init();
1227 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
1228 EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
1229
1230 // Simulate receiving the language.
1231 SimulateOnTranslateLanguageDetermined("fr", true);
[email protected]921441c2012-10-19 22:16:011232 menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents()));
[email protected]2253b3b2012-06-03 22:39:151233 menu->Init();
1234 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
1235 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
1236
1237 // Use the menu to translate the page.
[email protected]96bf34e2013-03-13 04:22:141238 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0);
[email protected]2253b3b2012-06-03 22:39:151239
1240 // That should have triggered a translation.
1241 // The "translating..." infobar should be showing.
1242 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
1243 ASSERT_TRUE(infobar != NULL);
[email protected]2ebd22d2013-01-07 17:04:271244 EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATING, infobar->infobar_type());
[email protected]2253b3b2012-06-03 22:39:151245 SimulateTranslateScriptURLFetch(true);
1246 int page_id = 0;
1247 std::string original_lang, target_lang;
1248 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
1249 EXPECT_EQ("fr", original_lang);
1250 EXPECT_EQ("en", target_lang);
1251 process()->sink().ClearMessages();
1252
1253 // This should also have reverted the blacklisting of this site and language.
[email protected]5d37b22982013-06-25 06:07:001254 EXPECT_FALSE(translate_prefs.IsBlockedLanguage("fr"));
[email protected]2253b3b2012-06-03 22:39:151255 EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(url.host()));
1256
1257 // Let's simulate the page being translated.
[email protected]05024712013-07-31 09:46:291258 SimulateOnPageTranslated("fr", "en");
[email protected]2253b3b2012-06-03 22:39:151259
1260 // The translate menu should now be disabled.
[email protected]921441c2012-10-19 22:16:011261 menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents()));
[email protected]2253b3b2012-06-03 22:39:151262 menu->Init();
1263 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
1264 EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
1265
1266 // Test that selecting translate in the context menu WHILE the page is being
1267 // translated does nothing (this could happen if autotranslate kicks-in and
1268 // the user selects the menu while the translation is being performed).
1269 SimulateNavigation(GURL("http://www.google.es"), "es", true);
1270 infobar = GetTranslateInfoBar();
1271 ASSERT_TRUE(infobar != NULL);
1272 infobar->Translate();
1273 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
1274 process()->sink().ClearMessages();
[email protected]921441c2012-10-19 22:16:011275 menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents()));
[email protected]2253b3b2012-06-03 22:39:151276 menu->Init();
1277 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
[email protected]96bf34e2013-03-13 04:22:141278 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0);
[email protected]2253b3b2012-06-03 22:39:151279 // No message expected since the translation should have been ignored.
1280 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
1281
1282 // Now test that selecting translate in the context menu AFTER the page has
1283 // been translated does nothing.
1284 SimulateNavigation(GURL("http://www.google.de"), "de", true);
1285 infobar = GetTranslateInfoBar();
1286 ASSERT_TRUE(infobar != NULL);
1287 infobar->Translate();
1288 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
1289 process()->sink().ClearMessages();
[email protected]921441c2012-10-19 22:16:011290 menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents()));
[email protected]2253b3b2012-06-03 22:39:151291 menu->Init();
1292 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
[email protected]05024712013-07-31 09:46:291293 SimulateOnPageTranslated("de", "en");
[email protected]96bf34e2013-03-13 04:22:141294 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0);
[email protected]2253b3b2012-06-03 22:39:151295 // No message expected since the translation should have been ignored.
1296 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
1297
1298 // Test that the translate context menu is enabled when the page is in an
1299 // unknown language.
1300 SimulateNavigation(url, "und", true);
[email protected]921441c2012-10-19 22:16:011301 menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents()));
[email protected]2253b3b2012-06-03 22:39:151302 menu->Init();
1303 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
1304 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
1305
[email protected]8e7d0b472013-05-23 05:41:371306 // Test that the translate context menu is enabled even if the page is in an
[email protected]2253b3b2012-06-03 22:39:151307 // unsupported language.
1308 SimulateNavigation(url, "qbz", true);
[email protected]921441c2012-10-19 22:16:011309 menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents()));
[email protected]2253b3b2012-06-03 22:39:151310 menu->Init();
1311 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
[email protected]8e7d0b472013-05-23 05:41:371312 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
[email protected]2253b3b2012-06-03 22:39:151313}
1314
1315// Tests that an extra always/never translate button is shown on the "before
1316// translate" infobar when the translation is accepted/declined 3 times,
1317// only when not in incognito mode.
[email protected]67755672013-04-08 23:25:321318TEST_F(TranslateManagerBrowserTest, BeforeTranslateExtraButtons) {
[email protected]2253b3b2012-06-03 22:39:151319 Profile* profile =
[email protected]921441c2012-10-19 22:16:011320 Profile::FromBrowserContext(web_contents()->GetBrowserContext());
[email protected]2253b3b2012-06-03 22:39:151321 TranslatePrefs translate_prefs(profile->GetPrefs());
1322 translate_prefs.ResetTranslationAcceptedCount("fr");
1323 translate_prefs.ResetTranslationDeniedCount("fr");
1324 translate_prefs.ResetTranslationAcceptedCount("de");
1325 translate_prefs.ResetTranslationDeniedCount("de");
1326
1327 // We'll do 4 times in incognito mode first to make sure the button is not
1328 // shown in that case, then 4 times in normal mode.
1329 TranslateInfoBarDelegate* infobar;
1330 TestingProfile* test_profile =
[email protected]921441c2012-10-19 22:16:011331 static_cast<TestingProfile*>(web_contents()->GetBrowserContext());
[email protected]bd306722012-07-11 20:43:591332 static_cast<extensions::TestExtensionSystem*>(
1333 extensions::ExtensionSystem::Get(test_profile))->
[email protected]2253b3b2012-06-03 22:39:151334 CreateExtensionProcessManager();
[email protected]9819fd02013-08-22 10:49:391335 test_profile->ForceIncognito(true);
[email protected]2253b3b2012-06-03 22:39:151336 for (int i = 0; i < 8; ++i) {
1337 SCOPED_TRACE(::testing::Message() << "Iteration " << i <<
1338 " incognito mode=" << test_profile->IsOffTheRecord());
1339 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
1340 infobar = GetTranslateInfoBar();
1341 ASSERT_TRUE(infobar != NULL);
[email protected]2ebd22d2013-01-07 17:04:271342 EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE,
1343 infobar->infobar_type());
[email protected]2253b3b2012-06-03 22:39:151344 if (i < 7) {
[email protected]f17179652013-04-25 18:11:101345 EXPECT_FALSE(infobar->ShouldShowAlwaysTranslateShortcut());
[email protected]2253b3b2012-06-03 22:39:151346 infobar->Translate();
1347 process()->sink().ClearMessages();
1348 } else {
[email protected]f17179652013-04-25 18:11:101349 EXPECT_TRUE(infobar->ShouldShowAlwaysTranslateShortcut());
[email protected]2253b3b2012-06-03 22:39:151350 }
1351 if (i == 3)
[email protected]9819fd02013-08-22 10:49:391352 test_profile->ForceIncognito(false);
[email protected]2253b3b2012-06-03 22:39:151353 }
1354 // Simulate the user pressing "Always translate French".
1355 infobar->AlwaysTranslatePageLanguage();
1356 EXPECT_TRUE(translate_prefs.IsLanguagePairWhitelisted("fr", "en"));
1357 // Simulate the translate script being retrieved (it only needs to be done
1358 // once in the test as it is cached).
1359 SimulateTranslateScriptURLFetch(true);
1360 // That should have triggered a page translate.
1361 int page_id = 0;
1362 std::string original_lang, target_lang;
1363 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
1364 process()->sink().ClearMessages();
1365
1366 // Now test that declining the translation causes a "never translate" button
1367 // to be shown (in non incognito mode only).
[email protected]9819fd02013-08-22 10:49:391368 test_profile->ForceIncognito(true);
[email protected]2253b3b2012-06-03 22:39:151369 for (int i = 0; i < 8; ++i) {
1370 SCOPED_TRACE(::testing::Message() << "Iteration " << i <<
1371 " incognito mode=" << test_profile->IsOffTheRecord());
1372 SimulateNavigation(GURL("http://www.google.de"), "de", true);
1373 infobar = GetTranslateInfoBar();
1374 ASSERT_TRUE(infobar != NULL);
[email protected]2ebd22d2013-01-07 17:04:271375 EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE,
1376 infobar->infobar_type());
[email protected]2253b3b2012-06-03 22:39:151377 if (i < 7) {
[email protected]f17179652013-04-25 18:11:101378 EXPECT_FALSE(infobar->ShouldShowNeverTranslateShortcut());
[email protected]2253b3b2012-06-03 22:39:151379 infobar->TranslationDeclined();
1380 } else {
[email protected]f17179652013-04-25 18:11:101381 EXPECT_TRUE(infobar->ShouldShowNeverTranslateShortcut());
[email protected]2253b3b2012-06-03 22:39:151382 }
1383 if (i == 3)
[email protected]9819fd02013-08-22 10:49:391384 test_profile->ForceIncognito(false);
[email protected]2253b3b2012-06-03 22:39:151385 }
1386 // Simulate the user pressing "Never translate French".
1387 infobar->NeverTranslatePageLanguage();
[email protected]5d37b22982013-06-25 06:07:001388 EXPECT_TRUE(translate_prefs.IsBlockedLanguage("de"));
[email protected]2253b3b2012-06-03 22:39:151389 // No translation should have occured and the infobar should be gone.
1390 EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
1391 process()->sink().ClearMessages();
1392 ASSERT_TRUE(GetTranslateInfoBar() == NULL);
1393}
1394
1395// Tests that we don't show a translate infobar when a page instructs that it
1396// should not be translated.
[email protected]67755672013-04-08 23:25:321397TEST_F(TranslateManagerBrowserTest, NonTranslatablePage) {
[email protected]2253b3b2012-06-03 22:39:151398 SimulateNavigation(GURL("http://mail.google.fr"), "fr", false);
1399
1400 // We should not have an infobar.
1401 EXPECT_TRUE(GetTranslateInfoBar() == NULL);
1402
[email protected]54e4b522013-05-21 08:36:231403 // The context menu is enabled to allow users to force translation.
[email protected]2253b3b2012-06-03 22:39:151404 scoped_ptr<TestRenderViewContextMenu> menu(
[email protected]921441c2012-10-19 22:16:011405 TestRenderViewContextMenu::CreateContextMenu(web_contents()));
[email protected]2253b3b2012-06-03 22:39:151406 menu->Init();
1407 EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE));
[email protected]54e4b522013-05-21 08:36:231408 EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE));
[email protected]2253b3b2012-06-03 22:39:151409}
1410
1411// Tests that the script is expired and refetched as expected.
[email protected]67755672013-04-08 23:25:321412TEST_F(TranslateManagerBrowserTest, ScriptExpires) {
[email protected]2253b3b2012-06-03 22:39:151413 ExpireTranslateScriptImmediately();
1414
[email protected]2253b3b2012-06-03 22:39:151415 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
1416 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
1417 ASSERT_TRUE(infobar != NULL);
1418 process()->sink().ClearMessages();
1419 infobar->Translate();
1420 SimulateTranslateScriptURLFetch(true);
[email protected]05024712013-07-31 09:46:291421 SimulateOnPageTranslated("fr", "en");
[email protected]2253b3b2012-06-03 22:39:151422
1423 // A task should have been posted to clear the script, run it.
[email protected]b3a25092013-05-28 22:08:161424 base::MessageLoop::current()->RunUntilIdle();
[email protected]2253b3b2012-06-03 22:39:151425
1426 // Do another navigation and translation.
1427 SimulateNavigation(GURL("http://www.google.es"), "es", true);
1428 infobar = GetTranslateInfoBar();
1429 ASSERT_TRUE(infobar != NULL);
1430 process()->sink().ClearMessages();
1431 infobar->Translate();
1432 // If we don't simulate the URL fetch, the TranslateManager should be waiting
1433 // for the script and no message should have been sent to the renderer.
1434 EXPECT_TRUE(
1435 process()->sink().GetFirstMessageMatching(
[email protected]05024712013-07-31 09:46:291436 ChromeViewMsg_TranslatePage::ID) == NULL);
[email protected]2253b3b2012-06-03 22:39:151437 // Now simulate the URL fetch.
1438 SimulateTranslateScriptURLFetch(true);
1439 // Now the message should have been sent.
1440 int page_id = 0;
1441 std::string original_lang, target_lang;
1442 EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang));
1443 EXPECT_EQ("es", original_lang);
1444 EXPECT_EQ("en", target_lang);
1445}
[email protected]f8a2e132012-08-31 18:16:201446
[email protected]67755672013-04-08 23:25:321447TEST_F(TranslateManagerBrowserTest, DownloadsAndHistoryNotTranslated) {
[email protected]f8a2e132012-08-31 18:16:201448 ASSERT_FALSE(TranslateManager::IsTranslatableURL(
1449 GURL(chrome::kChromeUIDownloadsURL)));
1450 ASSERT_FALSE(TranslateManager::IsTranslatableURL(
1451 GURL(chrome::kChromeUIHistoryURL)));
1452}
1453
[email protected]e625b7602013-10-28 09:24:561454#if !defined(OS_ANDROID) && !defined(OS_IOS)
1455
1456TEST_F(TranslateManagerBrowserTest, BubbleNormalTranslate) {
1457 // Prepare for the bubble
1458 CommandLine* command_line = CommandLine::ForCurrentProcess();
1459 command_line->AppendSwitch(switches::kEnableTranslateNewUX);
1460 MockTranslateBubbleFactory* factory = new MockTranslateBubbleFactory;
1461 scoped_ptr<TranslateBubbleFactory> factory_ptr(factory);
1462 TranslateBubbleFactory::SetFactory(factory);
1463
1464 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
1465
1466 // Check the bubble exists instead of the infobar.
1467 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
1468 ASSERT_TRUE(infobar == NULL);
1469 TranslateBubbleModel* bubble = factory->model();
1470 ASSERT_TRUE(bubble != NULL);
1471 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE,
1472 bubble->GetViewState());
1473
1474 // Simulate clicking translate.
1475 process()->sink().ClearMessages();
1476 bubble->Translate();
1477
1478 // Check the bubble shows "Translating...".
1479 bubble = factory->model();
1480 ASSERT_TRUE(bubble != NULL);
1481 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_TRANSLATING,
1482 bubble->GetViewState());
1483
1484 // Simulate the translate script being retrieved (it only needs to be done
1485 // once in the test as it is cached).
1486 SimulateTranslateScriptURLFetch(true);
1487
1488 // Simulate the render notifying the translation has been done.
1489 SimulateOnPageTranslated("fr", "en");
1490
1491 // Check the bubble shows "Translated."
1492 bubble = factory->model();
1493 ASSERT_TRUE(bubble != NULL);
1494 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_AFTER_TRANSLATE,
1495 bubble->GetViewState());
1496}
1497
1498TEST_F(TranslateManagerBrowserTest, BubbleTranslateScriptNotAvailable) {
1499 // Prepare for the bubble
1500 CommandLine* command_line = CommandLine::ForCurrentProcess();
1501 command_line->AppendSwitch(switches::kEnableTranslateNewUX);
1502 MockTranslateBubbleFactory* factory = new MockTranslateBubbleFactory;
1503 scoped_ptr<TranslateBubbleFactory> factory_ptr(factory);
1504 TranslateBubbleFactory::SetFactory(factory);
1505
1506 SimulateNavigation(GURL("http://www.google.fr"), "fr", true);
1507
1508 // Check the bubble exists instead of the infobar.
1509 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
1510 ASSERT_TRUE(infobar == NULL);
1511 TranslateBubbleModel* bubble = factory->model();
1512 ASSERT_TRUE(bubble != NULL);
1513 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE,
1514 bubble->GetViewState());
1515
1516 // Simulate clicking translate.
1517 process()->sink().ClearMessages();
1518 bubble->Translate();
1519 SimulateTranslateScriptURLFetch(false);
1520
1521 // We should not have sent any message to translate to the renderer.
1522 EXPECT_FALSE(GetTranslateMessage(NULL, NULL, NULL));
1523
1524 // And we should have an error infobar showing.
1525 bubble = factory->model();
1526 ASSERT_TRUE(bubble != NULL);
1527 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_ERROR,
1528 bubble->GetViewState());
1529}
1530
1531TEST_F(TranslateManagerBrowserTest, BubbleUnknownLanguage) {
1532 // Prepare for the bubble
1533 CommandLine* command_line = CommandLine::ForCurrentProcess();
1534 command_line->AppendSwitch(switches::kEnableTranslateNewUX);
1535 MockTranslateBubbleFactory* factory = new MockTranslateBubbleFactory;
1536 scoped_ptr<TranslateBubbleFactory> factory_ptr(factory);
1537 TranslateBubbleFactory::SetFactory(factory);
1538
1539 // Simulate navigating to a page ("und" is the string returned by the CLD for
1540 // languages it does not recognize).
1541 SimulateNavigation(GURL("http://www.google.mys"), "und", true);
1542
1543 // We should not have a bubble as we don't know the language.
1544 ASSERT_TRUE(factory->model() == NULL);
1545
1546 // Translate the page anyway throught the context menu.
1547 scoped_ptr<TestRenderViewContextMenu> menu(
1548 TestRenderViewContextMenu::CreateContextMenu(web_contents()));
1549 menu->Init();
1550 menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0);
1551
1552 // Check the bubble exists instead of the infobar.
1553 TranslateInfoBarDelegate* infobar = GetTranslateInfoBar();
1554 ASSERT_TRUE(infobar == NULL);
1555 TranslateBubbleModel* bubble = factory->model();
1556 ASSERT_TRUE(bubble != NULL);
1557 EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_TRANSLATING,
1558 bubble->GetViewState());
1559}
1560
1561#endif // !defined(OS_ANDROID) && !defined(OS_IOS)
1562
[email protected]373b4612012-12-15 23:26:411563// Test is flaky on Win http://crbug.com/166334
1564#if defined(OS_WIN)
1565#define MAYBE_PRE_TranslateSessionRestore DISABLED_PRE_TranslateSessionRestore
1566#else
1567#define MAYBE_PRE_TranslateSessionRestore PRE_TranslateSessionRestore
1568#endif
[email protected]f8a2e132012-08-31 18:16:201569// Test that session restore restores the translate infobar and other translate
1570// settings.
[email protected]373b4612012-12-15 23:26:411571IN_PROC_BROWSER_TEST_F(InProcessBrowserTest,
1572 MAYBE_PRE_TranslateSessionRestore) {
[email protected]f8a2e132012-08-31 18:16:201573 SessionStartupPref pref(SessionStartupPref::LAST);
1574 SessionStartupPref::SetStartupPref(browser()->profile(), pref);
1575
[email protected]05024712013-07-31 09:46:291576 content::WebContents* current_web_contents =
[email protected]47ae23372013-01-29 01:50:481577 browser()->tab_strip_model()->GetActiveWebContents();
[email protected]f0235f62012-09-28 16:50:571578 TranslateTabHelper* translate_tab_helper =
1579 TranslateTabHelper::FromWebContents(current_web_contents);
[email protected]05024712013-07-31 09:46:291580 content::Source<content::WebContents> source(current_web_contents);
[email protected]f8a2e132012-08-31 18:16:201581
[email protected]27eff892013-05-21 16:40:521582 ui_test_utils::WindowedNotificationObserverWithDetails<
1583 LanguageDetectionDetails>
[email protected]f8a2e132012-08-31 18:16:201584 fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED,
1585 source);
1586
1587 GURL french_url = ui_test_utils::GetTestUrl(
[email protected]650b2d52013-02-10 03:41:451588 base::FilePath(), base::FilePath(FILE_PATH_LITERAL("french_page.html")));
[email protected]f8a2e132012-08-31 18:16:201589 ui_test_utils::NavigateToURL(browser(), french_url);
1590 fr_language_detected_signal.Wait();
[email protected]27eff892013-05-21 16:40:521591 LanguageDetectionDetails details;
[email protected]f8a2e132012-08-31 18:16:201592 EXPECT_TRUE(fr_language_detected_signal.GetDetailsFor(
[email protected]27eff892013-05-21 16:40:521593 source.map_key(), &details));
1594 EXPECT_EQ("fr", details.adopted_language);
[email protected]f0235f62012-09-28 16:50:571595 EXPECT_EQ("fr", translate_tab_helper->language_state().original_language());
[email protected]f8a2e132012-08-31 18:16:201596}
1597
[email protected]373b4612012-12-15 23:26:411598#if defined (OS_WIN)
1599#define MAYBE_TranslateSessionRestore DISABLED_TranslateSessionRestore
1600#else
1601#define MAYBE_TranslateSessionRestore TranslateSessionRestore
1602#endif
1603IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, MAYBE_TranslateSessionRestore) {
[email protected]05024712013-07-31 09:46:291604 content::WebContents* current_web_contents =
[email protected]47ae23372013-01-29 01:50:481605 browser()->tab_strip_model()->GetActiveWebContents();
[email protected]05024712013-07-31 09:46:291606 content::Source<content::WebContents> source(current_web_contents);
[email protected]f8a2e132012-08-31 18:16:201607
[email protected]27eff892013-05-21 16:40:521608 ui_test_utils::WindowedNotificationObserverWithDetails<
1609 LanguageDetectionDetails>
[email protected]f8a2e132012-08-31 18:16:201610 fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED,
1611 source);
1612 fr_language_detected_signal.Wait();
1613}