[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1 | // 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] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 5 | #include <algorithm> |
| 6 | #include <set> |
| 7 | #include <vector> |
| 8 | |
[email protected] | e625b760 | 2013-10-28 09:24:56 | [diff] [blame] | 9 | #include "base/command_line.h" |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 10 | #include "base/memory/scoped_ptr.h" |
[email protected] | 1ab137b | 2013-03-21 03:33:18 | [diff] [blame] | 11 | #include "base/prefs/pref_change_registrar.h" |
[email protected] | 3853a4c | 2013-02-11 17:15:57 | [diff] [blame] | 12 | #include "base/prefs/pref_service.h" |
[email protected] | 9f0abdb | 2013-06-10 21:49:34 | [diff] [blame] | 13 | #include "base/strings/stringprintf.h" |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 14 | #include "chrome/app/chrome_command_ids.h" |
[email protected] | dcc8fbc | 2013-07-12 00:54:09 | [diff] [blame] | 15 | #include "chrome/browser/chrome_notification_types.h" |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 16 | #include "chrome/browser/extensions/test_extension_system.h" |
| 17 | #include "chrome/browser/infobars/infobar.h" |
[email protected] | 4a8adfa0 | 2013-03-19 22:37:46 | [diff] [blame] | 18 | #include "chrome/browser/infobars/infobar_service.h" |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 19 | #include "chrome/browser/prefs/session_startup_pref.h" |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 20 | #include "chrome/browser/tab_contents/render_view_context_menu.h" |
| 21 | #include "chrome/browser/translate/translate_infobar_delegate.h" |
[email protected] | 4df61c70 | 2013-05-28 10:55:30 | [diff] [blame] | 22 | #include "chrome/browser/translate/translate_language_list.h" |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 23 | #include "chrome/browser/translate/translate_manager.h" |
| 24 | #include "chrome/browser/translate/translate_prefs.h" |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 25 | #include "chrome/browser/translate/translate_script.h" |
[email protected] | bb1c86cd | 2014-01-29 18:22:18 | [diff] [blame] | 26 | #include "chrome/browser/translate/translate_service.h" |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 27 | #include "chrome/browser/translate/translate_tab_helper.h" |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 28 | #include "chrome/browser/ui/browser.h" |
[email protected] | 47ae2337 | 2013-01-29 01:50:48 | [diff] [blame] | 29 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
[email protected] | e625b760 | 2013-10-28 09:24:56 | [diff] [blame] | 30 | #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] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 34 | #include "chrome/common/pref_names.h" |
| 35 | #include "chrome/common/render_messages.h" |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 36 | #include "chrome/common/url_constants.h" |
[email protected] | 292ccc9 | 2012-10-17 12:46:02 | [diff] [blame] | 37 | #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 38 | #include "chrome/test/base/in_process_browser_test.h" |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 39 | #include "chrome/test/base/testing_browser_process.h" |
| 40 | #include "chrome/test/base/testing_profile.h" |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 41 | #include "chrome/test/base/ui_test_utils.h" |
[email protected] | bb1c86cd | 2014-01-29 18:22:18 | [diff] [blame] | 42 | #include "components/translate/core/browser/translate_download_manager.h" |
[email protected] | eba93c9 | 2014-01-07 17:34:17 | [diff] [blame] | 43 | #include "components/translate/core/common/language_detection_details.h" |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 44 | #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] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 49 | #include "content/public/test/mock_render_process_host.h" |
[email protected] | b1e3f20 | 2012-06-04 14:45:50 | [diff] [blame] | 50 | #include "content/public/test/test_renderer_host.h" |
[email protected] | 8f2d8bdd | 2012-06-19 23:44:05 | [diff] [blame] | 51 | #include "net/url_request/test_url_fetcher_factory.h" |
[email protected] | 140930a | 2013-06-21 16:11:09 | [diff] [blame] | 52 | #include "net/url_request/url_fetcher_delegate.h" |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 53 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | 2255a933 | 2013-06-17 05:12:31 | [diff] [blame] | 54 | #include "third_party/WebKit/public/web/WebContextMenuData.h" |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 55 | |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 56 | |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 57 | // An observer that keeps track of whether a navigation entry was committed. |
| 58 | class NavEntryCommittedObserver : public content::NotificationObserver { |
| 59 | public: |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 60 | explicit NavEntryCommittedObserver(content::WebContents* web_contents) { |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 61 | registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 62 | content::Source<content::NavigationController>( |
| 63 | &web_contents->GetController())); |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | virtual void Observe(int type, |
| 67 | const content::NotificationSource& source, |
[email protected] | b94584a | 2013-02-07 03:02:08 | [diff] [blame] | 68 | const content::NotificationDetails& details) OVERRIDE { |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 69 | DCHECK(type == content::NOTIFICATION_NAV_ENTRY_COMMITTED); |
| 70 | details_ = |
| 71 | *(content::Details<content::LoadCommittedDetails>(details).ptr()); |
| 72 | } |
| 73 | |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 74 | const content::LoadCommittedDetails& load_committed_details() const { |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 75 | 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] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 85 | class TranslateManagerBrowserTest : public ChromeRenderViewHostTestHarness, |
| 86 | public content::NotificationObserver { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 87 | public: |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 88 | TranslateManagerBrowserTest() |
| 89 | : pref_callback_( |
| 90 | base::Bind(&TranslateManagerBrowserTest::OnPreferenceChanged, |
[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame] | 91 | base::Unretained(this))) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 92 | } |
| 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] | 27eff89 | 2013-05-21 16:40:52 | [diff] [blame] | 105 | LanguageDetectionDetails details; |
| 106 | details.adopted_language = lang; |
[email protected] | 2fa6318d | 2013-12-02 22:10:56 | [diff] [blame] | 107 | content::RenderViewHostTester::TestOnMessageReceived( |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 108 | rvh(), |
| 109 | ChromeViewHostMsg_TranslateLanguageDetermined( |
[email protected] | 27eff89 | 2013-05-21 16:40:52 | [diff] [blame] | 110 | 0, details, page_translatable)); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 111 | } |
| 112 | |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 113 | void SimulateOnPageTranslated(int routing_id, |
| 114 | const std::string& source_lang, |
| 115 | const std::string& target_lang, |
| 116 | TranslateErrors::Type error) { |
[email protected] | 2fa6318d | 2013-12-02 22:10:56 | [diff] [blame] | 117 | content::RenderViewHostTester::TestOnMessageReceived( |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 118 | 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] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 129 | 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] | 4f822f02 | 2012-12-20 19:11:42 | [diff] [blame] | 149 | InfoBarService* infobar_service() { |
| 150 | return InfoBarService::FromWebContents(web_contents()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | // Returns the translate infobar if there is 1 infobar and it is a translate |
| 154 | // infobar. |
| 155 | TranslateInfoBarDelegate* GetTranslateInfoBar() { |
[email protected] | a608d17 | 2013-04-23 22:27:37 | [diff] [blame] | 156 | return (infobar_service()->infobar_count() == 1) ? |
[email protected] | 39308cb | 2013-12-06 03:01:48 | [diff] [blame] | 157 | infobar_service()->infobar_at(0)->delegate()-> |
| 158 | AsTranslateInfoBarDelegate() : NULL; |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 159 | } |
| 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] | 39308cb | 2013-12-06 03:01:48 | [diff] [blame] | 168 | infobar_service()->RemoveInfoBar(infobar_service()->infobar_at(0)); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 169 | 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] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 180 | void ExpireTranslateScriptImmediately() { |
[email protected] | 140930a | 2013-06-21 16:11:09 | [diff] [blame] | 181 | TranslateManager::GetInstance()->SetTranslateScriptExpirationDelay(0); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 182 | } |
| 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] | 39308cb | 2013-12-06 03:01:48 | [diff] [blame] | 191 | infobar_service()->RemoveInfoBar(infobar_service()->infobar_at(0)); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 192 | return true; |
| 193 | } |
| 194 | |
[email protected] | b637bcd | 2013-03-28 12:50:32 | [diff] [blame] | 195 | void ReloadAndWait(bool successful_reload) { |
[email protected] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 196 | NavEntryCommittedObserver nav_observer(web_contents()); |
[email protected] | b637bcd | 2013-03-28 12:50:32 | [diff] [blame] | 197 | if (successful_reload) |
| 198 | Reload(); |
| 199 | else |
| 200 | FailedReload(); |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 201 | |
| 202 | // Ensures it is really handled a reload. |
| 203 | const content::LoadCommittedDetails& nav_details = |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 204 | nav_observer.load_committed_details(); |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 205 | 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] | b3a2509 | 2013-05-28 22:08:16 | [diff] [blame] | 210 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 211 | } |
| 212 | |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 213 | 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] | 39308cb | 2013-12-06 03:01:48 | [diff] [blame] | 218 | content::Details<InfoBar::RemovedDetails>(details)->first->delegate()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 219 | } |
| 220 | |
[email protected] | 116129e0 | 2012-11-21 17:26:27 | [diff] [blame] | 221 | MOCK_METHOD1(OnPreferenceChanged, void(const std::string&)); |
[email protected] | a6a7ced | 2012-11-01 17:24:18 | [diff] [blame] | 222 | |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 223 | protected: |
| 224 | virtual void SetUp() { |
[email protected] | bc18603 | 2013-12-09 19:15:23 | [diff] [blame] | 225 | // 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] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 230 | // Access the TranslateManager singleton so it is created before we call |
[email protected] | 292ccc9 | 2012-10-17 12:46:02 | [diff] [blame] | 231 | // ChromeRenderViewHostTestHarness::SetUp() to match what's done in Chrome, |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 232 | // 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] | bb1c86cd | 2014-01-29 18:22:18 | [diff] [blame] | 238 | TranslateService::Initialize(); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 239 | TranslateManager::GetInstance()->ClearTranslateScript(); |
| 240 | TranslateManager::GetInstance()-> |
[email protected] | 140930a | 2013-06-21 16:11:09 | [diff] [blame] | 241 | SetTranslateScriptExpirationDelay(60 * 60 * 1000); |
[email protected] | 9ad06a1 | 2013-05-10 14:49:53 | [diff] [blame] | 242 | TranslateManager::GetInstance()->set_translate_max_reload_attemps(0); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 243 | |
[email protected] | 292ccc9 | 2012-10-17 12:46:02 | [diff] [blame] | 244 | ChromeRenderViewHostTestHarness::SetUp(); |
[email protected] | 4f822f02 | 2012-12-20 19:11:42 | [diff] [blame] | 245 | InfoBarService::CreateForWebContents(web_contents()); |
[email protected] | 292ccc9 | 2012-10-17 12:46:02 | [diff] [blame] | 246 | TranslateTabHelper::CreateForWebContents(web_contents()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 247 | |
| 248 | notification_registrar_.Add(this, |
| 249 | chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, |
[email protected] | 4f822f02 | 2012-12-20 19:11:42 | [diff] [blame] | 250 | content::Source<InfoBarService>(infobar_service())); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | virtual void TearDown() { |
| 254 | process()->sink().ClearMessages(); |
| 255 | |
| 256 | notification_registrar_.Remove(this, |
| 257 | chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, |
[email protected] | 4f822f02 | 2012-12-20 19:11:42 | [diff] [blame] | 258 | content::Source<InfoBarService>(infobar_service())); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 259 | |
[email protected] | 292ccc9 | 2012-10-17 12:46:02 | [diff] [blame] | 260 | ChromeRenderViewHostTestHarness::TearDown(); |
[email protected] | bc18603 | 2013-12-09 19:15:23 | [diff] [blame] | 261 | TestingBrowserProcess::DeleteInstance(); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 262 | } |
| 263 | |
[email protected] | bb1c86cd | 2014-01-29 18:22:18 | [diff] [blame] | 264 | 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] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 270 | void SimulateTranslateScriptURLFetch(bool success) { |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 271 | net::TestURLFetcher* fetcher = |
| 272 | url_fetcher_factory_.GetFetcherByID(TranslateScript::kFetcherId); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 273 | 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] | 7da697a | 2013-07-12 08:09:46 | [diff] [blame] | 284 | bool success, |
| 285 | const std::vector<std::string>& languages, |
| 286 | bool use_alpha_languages, |
| 287 | const std::vector<std::string>& alpha_languages) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 288 | 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] | 4df61c70 | 2013-05-28 10:55:30 | [diff] [blame] | 294 | data = base::StringPrintf( |
| 295 | "%s{\"sl\": {\"bla\": \"bla\"}, \"%s\": {", |
| 296 | TranslateLanguageList::kLanguageListCallbackName, |
| 297 | TranslateLanguageList::kTargetLanguagesKey); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 298 | const char* comma = ""; |
| 299 | for (size_t i = 0; i < languages.size(); ++i) { |
| 300 | data += base::StringPrintf( |
[email protected] | 2a4e1ae | 2013-04-15 14:41:36 | [diff] [blame] | 301 | "%s\"%s\": \"UnusedFullName\"", comma, languages[i].c_str()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 302 | if (i == 0) |
| 303 | comma = ","; |
| 304 | } |
[email protected] | 7da697a | 2013-07-12 08:09:46 | [diff] [blame] | 305 | |
| 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] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 318 | data += "}})"; |
| 319 | } |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 320 | net::TestURLFetcher* fetcher = |
| 321 | url_fetcher_factory_.GetFetcherByID(TranslateLanguageList::kFetcherId); |
[email protected] | 7da697a | 2013-07-12 08:09:46 | [diff] [blame] | 322 | 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] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | void SetPrefObserverExpectation(const char* path) { |
[email protected] | 116129e0 | 2012-11-21 17:26:27 | [diff] [blame] | 331 | EXPECT_CALL(*this, OnPreferenceChanged(std::string(path))); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 332 | } |
| 333 | |
[email protected] | 116129e0 | 2012-11-21 17:26:27 | [diff] [blame] | 334 | PrefChangeRegistrar::NamedChangeCallback pref_callback_; |
| 335 | |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 336 | private: |
| 337 | content::NotificationRegistrar notification_registrar_; |
[email protected] | 8f2d8bdd | 2012-06-19 23:44:05 | [diff] [blame] | 338 | net::TestURLFetcherFactory url_fetcher_factory_; |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 339 | |
| 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] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 344 | DISALLOW_COPY_AND_ASSIGN(TranslateManagerBrowserTest); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 345 | }; |
| 346 | |
[email protected] | e625b760 | 2013-10-28 09:24:56 | [diff] [blame] | 347 | class MockTranslateBubbleFactory : public TranslateBubbleFactory { |
| 348 | public: |
| 349 | MockTranslateBubbleFactory() { |
| 350 | } |
| 351 | |
| 352 | virtual void ShowImplementation( |
| 353 | BrowserWindow* window, |
| 354 | content::WebContents* web_contents, |
[email protected] | 6d37ab2 | 2013-12-03 11:18:30 | [diff] [blame] | 355 | TranslateBubbleModel::ViewState view_state, |
| 356 | TranslateErrors::Type error_type) OVERRIDE { |
[email protected] | e625b760 | 2013-10-28 09:24:56 | [diff] [blame] | 357 | 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] | 3155c527 | 2014-01-21 12:38:12 | [diff] [blame] | 365 | translate_tab_helper->GetLanguageState().original_language(); |
[email protected] | e625b760 | 2013-10-28 09:24:56 | [diff] [blame] | 366 | 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] | 83fa875a | 2013-12-12 17:05:28 | [diff] [blame] | 371 | target_language)); |
[email protected] | e625b760 | 2013-10-28 09:24:56 | [diff] [blame] | 372 | 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] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 384 | namespace { |
| 385 | |
| 386 | class TestRenderViewContextMenu : public RenderViewContextMenu { |
| 387 | public: |
| 388 | static TestRenderViewContextMenu* CreateContextMenu( |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 389 | content::WebContents* web_contents) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 390 | content::ContextMenuParams params; |
[email protected] | a1221aea | 2013-11-07 01:31:30 | [diff] [blame] | 391 | params.media_type = blink::WebContextMenuData::MediaTypeNone; |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 392 | params.x = 0; |
| 393 | params.y = 0; |
[email protected] | 6462f8111 | 2013-08-23 23:00:37 | [diff] [blame] | 394 | params.has_image_contents = true; |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 395 | 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] | a1221aea | 2013-11-07 01:31:30 | [diff] [blame] | 404 | params.edit_flags = blink::WebContextMenuData::CanTranslate; |
[email protected] | a09d53ce | 2014-01-31 00:46:42 | [diff] [blame^] | 405 | return new TestRenderViewContextMenu(web_contents->GetMainFrame(), params); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | bool IsItemPresent(int id) { |
| 409 | return menu_model_.GetIndexOfCommandId(id) != -1; |
| 410 | } |
| 411 | |
[email protected] | b94584a | 2013-02-07 03:02:08 | [diff] [blame] | 412 | virtual void PlatformInit() OVERRIDE { } |
| 413 | virtual void PlatformCancel() OVERRIDE { } |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 414 | virtual bool GetAcceleratorForCommandId( |
| 415 | int command_id, |
[email protected] | b94584a | 2013-02-07 03:02:08 | [diff] [blame] | 416 | ui::Accelerator* accelerator) OVERRIDE { return false; } |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 417 | |
| 418 | private: |
[email protected] | a09d53ce | 2014-01-31 00:46:42 | [diff] [blame^] | 419 | TestRenderViewContextMenu(content::RenderFrameHost* render_frame_host, |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 420 | const content::ContextMenuParams& params) |
[email protected] | a09d53ce | 2014-01-31 00:46:42 | [diff] [blame^] | 421 | : RenderViewContextMenu(render_frame_host, params) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | DISALLOW_COPY_AND_ASSIGN(TestRenderViewContextMenu); |
| 425 | }; |
| 426 | |
| 427 | } // namespace |
| 428 | |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 429 | TEST_F(TranslateManagerBrowserTest, NormalTranslate) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 430 | 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] | 2ebd22d | 2013-01-07 17:04:27 | [diff] [blame] | 435 | EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE, |
| 436 | infobar->infobar_type()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 437 | |
| 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] | 2ebd22d | 2013-01-07 17:04:27 | [diff] [blame] | 445 | EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATING, infobar->infobar_type()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 446 | |
| 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] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 459 | SimulateOnPageTranslated("fr", "en"); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 460 | |
| 461 | // The after translate infobar should be showing. |
| 462 | infobar = GetTranslateInfoBar(); |
| 463 | ASSERT_TRUE(infobar != NULL); |
[email protected] | 2ebd22d | 2013-01-07 17:04:27 | [diff] [blame] | 464 | EXPECT_EQ(TranslateInfoBarDelegate::AFTER_TRANSLATE, infobar->infobar_type()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 465 | |
[email protected] | 9f94348 | 2012-09-21 01:05:13 | [diff] [blame] | 466 | // Simulate changing the original language and translating. |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 467 | process()->sink().ClearMessages(); |
[email protected] | 9f94348 | 2012-09-21 01:05:13 | [diff] [blame] | 468 | std::string new_original_lang = infobar->language_code_at(0); |
[email protected] | 3e1c839 | 2013-10-31 09:19:01 | [diff] [blame] | 469 | infobar->UpdateOriginalLanguageIndex(0); |
[email protected] | 9f94348 | 2012-09-21 01:05:13 | [diff] [blame] | 470 | infobar->Translate(); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 471 | 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] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 475 | SimulateOnPageTranslated(new_original_lang, "en"); |
[email protected] | 2fa6318d | 2013-12-02 22:10:56 | [diff] [blame] | 476 | infobar = GetTranslateInfoBar(); |
| 477 | ASSERT_TRUE(infobar != NULL); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 478 | |
[email protected] | 9f94348 | 2012-09-21 01:05:13 | [diff] [blame] | 479 | // Simulate changing the target language and translating. |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 480 | process()->sink().ClearMessages(); |
[email protected] | 9f94348 | 2012-09-21 01:05:13 | [diff] [blame] | 481 | std::string new_target_lang = infobar->language_code_at(1); |
[email protected] | 3e1c839 | 2013-10-31 09:19:01 | [diff] [blame] | 482 | infobar->UpdateTargetLanguageIndex(1); |
[email protected] | 9f94348 | 2012-09-21 01:05:13 | [diff] [blame] | 483 | infobar->Translate(); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 484 | 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] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 488 | SimulateOnPageTranslated(new_original_lang, new_target_lang); |
[email protected] | 2fa6318d | 2013-12-02 22:10:56 | [diff] [blame] | 489 | infobar = GetTranslateInfoBar(); |
| 490 | ASSERT_TRUE(infobar != NULL); |
| 491 | EXPECT_EQ(new_target_lang, infobar->target_language_code()); |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 492 | |
[email protected] | b022b949 | 2013-11-26 19:40:04 | [diff] [blame] | 493 | // Reloading should trigger translation iff Always Translate is on. |
[email protected] | b637bcd | 2013-03-28 12:50:32 | [diff] [blame] | 494 | ReloadAndWait(true); |
[email protected] | 2fa6318d | 2013-12-02 22:10:56 | [diff] [blame] | 495 | infobar = GetTranslateInfoBar(); |
| 496 | ASSERT_TRUE(infobar != NULL); |
[email protected] | b022b949 | 2013-11-26 19:40:04 | [diff] [blame] | 497 | EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE, |
| 498 | infobar->infobar_type()); |
| 499 | infobar->UpdateTargetLanguageIndex(1); |
| 500 | infobar->ToggleAlwaysTranslate(); |
| 501 | ReloadAndWait(true); |
[email protected] | 2fa6318d | 2013-12-02 22:10:56 | [diff] [blame] | 502 | infobar = GetTranslateInfoBar(); |
| 503 | ASSERT_TRUE(infobar != NULL); |
[email protected] | b022b949 | 2013-11-26 19:40:04 | [diff] [blame] | 504 | EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATING, infobar->infobar_type()); |
| 505 | EXPECT_EQ(new_target_lang, infobar->target_language_code()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 506 | } |
| 507 | |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 508 | TEST_F(TranslateManagerBrowserTest, TranslateScriptNotAvailable) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 509 | 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] | 2ebd22d | 2013-01-07 17:04:27 | [diff] [blame] | 514 | EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE, |
| 515 | infobar->infobar_type()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 516 | |
| 517 | // Simulate clicking translate. |
| 518 | process()->sink().ClearMessages(); |
| 519 | infobar->Translate(); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 520 | 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] | 2ebd22d | 2013-01-07 17:04:27 | [diff] [blame] | 528 | EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, |
| 529 | infobar->infobar_type()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 530 | } |
| 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] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 534 | TEST_F(TranslateManagerBrowserTest, TranslateUnknownLanguage) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 535 | // 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] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 544 | TestRenderViewContextMenu::CreateContextMenu(web_contents())); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 545 | menu->Init(); |
[email protected] | 96bf34e | 2013-03-13 04:22:14 | [diff] [blame] | 546 | menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 547 | |
| 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] | 2ebd22d | 2013-01-07 17:04:27 | [diff] [blame] | 553 | EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, |
| 554 | infobar->infobar_type()); |
[email protected] | cacaa24 | 2013-07-19 22:00:36 | [diff] [blame] | 555 | EXPECT_TRUE(infobar->is_error()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 556 | 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] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 561 | SimulateOnPageTranslated("fr", "en"); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 562 | |
| 563 | // The after translate infobar should be showing. |
| 564 | infobar = GetTranslateInfoBar(); |
| 565 | ASSERT_TRUE(infobar != NULL); |
[email protected] | 2ebd22d | 2013-01-07 17:04:27 | [diff] [blame] | 566 | EXPECT_EQ(TranslateInfoBarDelegate::AFTER_TRANSLATE, infobar->infobar_type()); |
[email protected] | 9f94348 | 2012-09-21 01:05:13 | [diff] [blame] | 567 | EXPECT_EQ("fr", infobar->original_language_code()); |
| 568 | EXPECT_EQ("en", infobar->target_language_code()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 569 | |
| 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] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 573 | menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents())); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 574 | menu->Init(); |
[email protected] | 96bf34e | 2013-03-13 04:22:14 | [diff] [blame] | 575 | menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0); |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 576 | SimulateOnPageTranslated(1, "en", "en", TranslateErrors::IDENTICAL_LANGUAGES); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 577 | infobar = GetTranslateInfoBar(); |
| 578 | ASSERT_TRUE(infobar != NULL); |
[email protected] | 2ebd22d | 2013-01-07 17:04:27 | [diff] [blame] | 579 | EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, |
| 580 | infobar->infobar_type()); |
| 581 | EXPECT_EQ(TranslateErrors::IDENTICAL_LANGUAGES, infobar->error_type()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 582 | |
| 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] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 586 | menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents())); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 587 | menu->Init(); |
[email protected] | 96bf34e | 2013-03-13 04:22:14 | [diff] [blame] | 588 | menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0); |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 589 | SimulateOnPageTranslated(2, std::string(), "en", |
| 590 | TranslateErrors::UNKNOWN_LANGUAGE); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 591 | infobar = GetTranslateInfoBar(); |
| 592 | ASSERT_TRUE(infobar != NULL); |
[email protected] | 2ebd22d | 2013-01-07 17:04:27 | [diff] [blame] | 593 | EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, |
| 594 | infobar->infobar_type()); |
| 595 | EXPECT_EQ(TranslateErrors::UNKNOWN_LANGUAGE, infobar->error_type()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 596 | } |
| 597 | |
[email protected] | f274d37 | 2013-07-22 13:08:50 | [diff] [blame] | 598 | // Tests that we show/don't show an info-bar for the languages. |
| 599 | TEST_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] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 609 | |
| 610 | GURL url("http://www.google.com"); |
[email protected] | f274d37 | 2013-07-22 13:08:50 | [diff] [blame] | 611 | for (size_t i = 0; i < languages.size(); ++i) { |
| 612 | std::string lang = languages[i]; |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 613 | SCOPED_TRACE(::testing::Message() << "Iteration " << i << |
[email protected] | f274d37 | 2013-07-22 13:08:50 | [diff] [blame] | 614 | " language=" << lang); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 615 | |
| 616 | // We should not have a translate infobar. |
| 617 | TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 618 | ASSERT_TRUE(infobar == NULL); |
| 619 | |
[email protected] | f274d37 | 2013-07-22 13:08:50 | [diff] [blame] | 620 | SimulateNavigation(url, lang, true); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 621 | |
| 622 | // Verify we have/don't have an info-bar as expected. |
| 623 | infobar = GetTranslateInfoBar(); |
[email protected] | f274d37 | 2013-07-22 13:08:50 | [diff] [blame] | 624 | bool expected = TranslateManager::IsSupportedLanguage(lang) && |
| 625 | lang != "en"; |
| 626 | EXPECT_EQ(expected, infobar != NULL); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 627 | |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 628 | if (infobar != NULL) |
| 629 | EXPECT_TRUE(CloseTranslateInfoBar()); |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | // Test the fetching of languages from the translate server |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 634 | TEST_F(TranslateManagerBrowserTest, FetchLanguagesFromTranslateServer) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 635 | 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] | 462e909 | 2013-06-14 08:39:38 | [diff] [blame] | 638 | server_languages.push_back("ak"); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 639 | server_languages.push_back("ab"); |
| 640 | server_languages.push_back("en-CA"); |
[email protected] | 462e909 | 2013-06-14 08:39:38 | [diff] [blame] | 641 | server_languages.push_back("zh"); |
| 642 | server_languages.push_back("yi"); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 643 | server_languages.push_back("fr-FR"); |
[email protected] | 462e909 | 2013-06-14 08:39:38 | [diff] [blame] | 644 | server_languages.push_back("xx"); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 645 | |
[email protected] | 7da697a | 2013-07-12 08:09:46 | [diff] [blame] | 646 | std::vector<std::string> alpha_languages; |
| 647 | alpha_languages.push_back("aa"); |
| 648 | alpha_languages.push_back("yi"); |
| 649 | |
[email protected] | 296ff316 | 2013-06-20 18:14:23 | [diff] [blame] | 650 | // First, get the default languages list. Note that calling |
| 651 | // GetSupportedLanguages() invokes RequestLanguageList() internally. |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 652 | 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] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 657 | // Check that we still get the defaults until the URLFetch has completed. |
| 658 | std::vector<std::string> current_supported_languages; |
| 659 | TranslateManager::GetSupportedLanguages(¤t_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] | 7da697a | 2013-07-12 08:09:46 | [diff] [blame] | 663 | SimulateSupportedLanguagesURLFetch(false, std::vector<std::string>(), |
| 664 | true, std::vector<std::string>()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 665 | current_supported_languages.clear(); |
| 666 | TranslateManager::GetSupportedLanguages(¤t_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] | 7da697a | 2013-07-12 08:09:46 | [diff] [blame] | 670 | SimulateSupportedLanguagesURLFetch(true, server_languages, |
| 671 | true, alpha_languages); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 672 | current_supported_languages.clear(); |
| 673 | TranslateManager::GetSupportedLanguages(¤t_supported_languages); |
[email protected] | 462e909 | 2013-06-14 08:39:38 | [diff] [blame] | 674 | // "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] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 676 | // Not sure we need to guarantee the order of languages, so we find them. |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 677 | for (size_t i = 0; i < server_languages.size(); ++i) { |
[email protected] | 7da697a | 2013-07-12 08:09:46 | [diff] [blame] | 678 | const std::string& lang = server_languages[i]; |
| 679 | if (lang == "xx") |
[email protected] | 462e909 | 2013-06-14 08:39:38 | [diff] [blame] | 680 | continue; |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 681 | EXPECT_NE(current_supported_languages.end(), |
| 682 | std::find(current_supported_languages.begin(), |
| 683 | current_supported_languages.end(), |
[email protected] | 7da697a | 2013-07-12 08:09:46 | [diff] [blame] | 684 | 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] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 689 | } |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 690 | } |
| 691 | |
[email protected] | 7da697a | 2013-07-12 08:09:46 | [diff] [blame] | 692 | // Test the fetching of languages from the translate server without 'al' |
| 693 | // parameter. |
| 694 | TEST_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] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 705 | |
[email protected] | 7da697a | 2013-07-12 08:09:46 | [diff] [blame] | 706 | 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(¤t_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] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 733 | } |
| 734 | |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 735 | // Tests auto-translate on page. |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 736 | TEST_F(TranslateManagerBrowserTest, AutoTranslateOnNavigate) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 737 | 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] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 745 | SimulateOnPageTranslated("fr", "en"); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 746 | |
| 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] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 768 | TEST_F(TranslateManagerBrowserTest, MultipleOnPageContents) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 769 | SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 770 | |
| 771 | // Simulate clicking 'Nope' (don't translate). |
| 772 | EXPECT_TRUE(DenyTranslation()); |
[email protected] | a608d17 | 2013-04-23 22:27:37 | [diff] [blame] | 773 | EXPECT_EQ(0U, infobar_service()->infobar_count()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 774 | |
| 775 | // Send a new PageContents, we should not show an infobar. |
| 776 | SimulateOnTranslateLanguageDetermined("fr", true); |
[email protected] | a608d17 | 2013-04-23 22:27:37 | [diff] [blame] | 777 | EXPECT_EQ(0U, infobar_service()->infobar_count()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 778 | |
| 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] | a608d17 | 2013-04-23 22:27:37 | [diff] [blame] | 782 | EXPECT_EQ(0U, infobar_service()->infobar_count()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 783 | SimulateOnTranslateLanguageDetermined("fr", true); |
[email protected] | a608d17 | 2013-04-23 22:27:37 | [diff] [blame] | 784 | EXPECT_EQ(0U, infobar_service()->infobar_count()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 785 | } |
| 786 | |
[email protected] | b637bcd | 2013-03-28 12:50:32 | [diff] [blame] | 787 | // 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] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 789 | TEST_F(TranslateManagerBrowserTest, Reload) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 790 | SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 791 | |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 792 | EXPECT_TRUE(CloseTranslateInfoBar()); |
| 793 | |
[email protected] | 2fa6318d | 2013-12-02 22:10:56 | [diff] [blame] | 794 | // Reload should bring back the infobar if the reload succeeds. |
[email protected] | b637bcd | 2013-03-28 12:50:32 | [diff] [blame] | 795 | ReloadAndWait(true); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 796 | EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
[email protected] | b637bcd | 2013-03-28 12:50:32 | [diff] [blame] | 797 | EXPECT_TRUE(CloseTranslateInfoBar()); |
| 798 | |
[email protected] | 2fa6318d | 2013-12-02 22:10:56 | [diff] [blame] | 799 | // ...But not show it if the reload fails. |
[email protected] | b637bcd | 2013-03-28 12:50:32 | [diff] [blame] | 800 | ReloadAndWait(false); |
[email protected] | 2fa6318d | 2013-12-02 22:10:56 | [diff] [blame] | 801 | EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
[email protected] | 9ad06a1 | 2013-05-10 14:49:53 | [diff] [blame] | 802 | |
[email protected] | 2fa6318d | 2013-12-02 22:10:56 | [diff] [blame] | 803 | // If we set reload attempts to a high value, we will not see the infobar |
| 804 | // immediately. |
[email protected] | 9ad06a1 | 2013-05-10 14:49:53 | [diff] [blame] | 805 | TranslateManager::GetInstance()->set_translate_max_reload_attemps(100); |
| 806 | ReloadAndWait(true); |
| 807 | EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 808 | } |
| 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] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 812 | TEST_F(TranslateManagerBrowserTest, ReloadFromLocationBar) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 813 | GURL url("http://www.google.fr"); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 814 | SimulateNavigation(url, "fr", true); |
| 815 | |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 816 | 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] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 820 | NavEntryCommittedObserver nav_observer(web_contents()); |
| 821 | web_contents()->GetController().LoadURL(url, content::Referrer(), |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 822 | content::PAGE_TRANSITION_TYPED, |
| 823 | std::string()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 824 | 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] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 829 | nav_observer.load_committed_details(); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 830 | 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] | b3a2509 | 2013-05-28 22:08:16 | [diff] [blame] | 835 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 836 | EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 837 | } |
| 838 | |
| 839 | // Tests that a closed translate infobar does not reappear when navigating |
| 840 | // in-page. |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 841 | TEST_F(TranslateManagerBrowserTest, CloseInfoBarInPageNavigation) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 842 | SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 843 | |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 844 | EXPECT_TRUE(CloseTranslateInfoBar()); |
| 845 | |
| 846 | // Navigate in page, no infobar should be shown. |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 847 | SimulateNavigation(GURL("http://www.google.fr/#ref1"), "fr", true); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 848 | 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] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 857 | TEST_F(TranslateManagerBrowserTest, CloseInfoBarInSubframeNavigation) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 858 | SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 859 | |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 860 | 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] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 877 | // Tests that denying translation is sticky when navigating in page. |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 878 | TEST_F(TranslateManagerBrowserTest, DenyTranslateInPageNavigation) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 879 | 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] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 895 | TEST_F(TranslateManagerBrowserTest, TranslateCloseInfoBarInPageNavigation) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 896 | 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] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 904 | SimulateOnPageTranslated("fr", "en"); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 905 | |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 906 | 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] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 922 | TEST_F(TranslateManagerBrowserTest, TranslateInPageNavigation) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 923 | 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] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 929 | SimulateTranslateScriptURLFetch(true); |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 930 | SimulateOnPageTranslated("fr", "en"); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 931 | // 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] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 947 | TEST_F(TranslateManagerBrowserTest, CLDReportsUnsupportedPageLanguage) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 948 | // 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] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 958 | TEST_F(TranslateManagerBrowserTest, ServerReportsUnsupportedLanguage) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 959 | 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] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 967 | SimulateOnPageTranslated("qbz", "en"); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 968 | |
| 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] | 2ebd22d | 2013-01-07 17:04:27 | [diff] [blame] | 973 | EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, |
| 974 | infobar->infobar_type()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 975 | |
| 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] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 992 | TEST_F(TranslateManagerBrowserTest, UnsupportedUILanguage) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 993 | std::string original_lang = g_browser_process->GetApplicationLocale(); |
[email protected] | bb1c86cd | 2014-01-29 18:22:18 | [diff] [blame] | 994 | SetApplicationLocale("qbz"); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 995 | |
| 996 | // Make sure that the accept language list only contains unsupported languages |
| 997 | Profile* profile = |
[email protected] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 998 | Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 999 | 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] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 1011 | TestRenderViewContextMenu::CreateContextMenu(web_contents())); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1012 | menu->Init(); |
| 1013 | EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 1014 | EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 1015 | |
[email protected] | bb1c86cd | 2014-01-29 18:22:18 | [diff] [blame] | 1016 | SetApplicationLocale(original_lang); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | // Tests that the first supported accept language is selected |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 1020 | TEST_F(TranslateManagerBrowserTest, TranslateAcceptLanguage) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1021 | // Set locate to non-existant language |
| 1022 | std::string original_lang = g_browser_process->GetApplicationLocale(); |
[email protected] | bb1c86cd | 2014-01-29 18:22:18 | [diff] [blame] | 1023 | SetApplicationLocale("qbz"); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1024 | |
| 1025 | // Set Qbz and French as the only accepted languages |
| 1026 | Profile* profile = |
[email protected] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 1027 | Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1028 | 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] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 1050 | TEST_F(TranslateManagerBrowserTest, TranslateEnabledPref) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1051 | // Make sure the pref allows translate. |
| 1052 | Profile* profile = |
[email protected] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 1053 | Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1054 | PrefService* prefs = profile->GetPrefs(); |
| 1055 | prefs->SetBoolean(prefs::kEnableTranslate, true); |
| 1056 | |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1057 | 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] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 1080 | TEST_F(TranslateManagerBrowserTest, NeverTranslateLanguagePref) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1081 | 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] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 1089 | Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1090 | PrefService* prefs = profile->GetPrefs(); |
| 1091 | PrefChangeRegistrar registrar; |
| 1092 | registrar.Init(prefs); |
[email protected] | 032ae93a | 2013-08-30 09:15:03 | [diff] [blame] | 1093 | registrar.Add(TranslatePrefs::kPrefTranslateBlockedLanguages, |
[email protected] | 116129e0 | 2012-11-21 17:26:27 | [diff] [blame] | 1094 | pref_callback_); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1095 | TranslatePrefs translate_prefs(prefs); |
[email protected] | 5d37b2298 | 2013-06-25 06:07:00 | [diff] [blame] | 1096 | EXPECT_FALSE(translate_prefs.IsBlockedLanguage("fr")); |
[email protected] | f9db7e0 | 2013-06-17 10:29:43 | [diff] [blame] | 1097 | EXPECT_TRUE(translate_prefs.CanTranslateLanguage(profile, "fr")); |
[email protected] | 032ae93a | 2013-08-30 09:15:03 | [diff] [blame] | 1098 | SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateBlockedLanguages); |
[email protected] | 5d37b2298 | 2013-06-25 06:07:00 | [diff] [blame] | 1099 | translate_prefs.BlockLanguage("fr"); |
| 1100 | EXPECT_TRUE(translate_prefs.IsBlockedLanguage("fr")); |
[email protected] | f9db7e0 | 2013-06-17 10:29:43 | [diff] [blame] | 1101 | EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(url.host())); |
| 1102 | EXPECT_FALSE(translate_prefs.CanTranslateLanguage(profile, "fr")); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1103 | |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1104 | 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] | 032ae93a | 2013-08-30 09:15:03 | [diff] [blame] | 1113 | SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateBlockedLanguages); |
[email protected] | 5d37b2298 | 2013-06-25 06:07:00 | [diff] [blame] | 1114 | translate_prefs.UnblockLanguage("fr"); |
| 1115 | EXPECT_FALSE(translate_prefs.IsBlockedLanguage("fr")); |
[email protected] | f9db7e0 | 2013-06-17 10:29:43 | [diff] [blame] | 1116 | EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(url.host())); |
| 1117 | EXPECT_TRUE(translate_prefs.CanTranslateLanguage(profile, "fr")); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1118 | |
| 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] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 1127 | TEST_F(TranslateManagerBrowserTest, NeverTranslateSitePref) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1128 | 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] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 1137 | Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1138 | PrefService* prefs = profile->GetPrefs(); |
| 1139 | PrefChangeRegistrar registrar; |
| 1140 | registrar.Init(prefs); |
[email protected] | 116129e0 | 2012-11-21 17:26:27 | [diff] [blame] | 1141 | registrar.Add(TranslatePrefs::kPrefTranslateSiteBlacklist, pref_callback_); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1142 | TranslatePrefs translate_prefs(prefs); |
| 1143 | EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(host)); |
[email protected] | f9db7e0 | 2013-06-17 10:29:43 | [diff] [blame] | 1144 | EXPECT_TRUE(translate_prefs.CanTranslateLanguage(profile, "fr")); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1145 | SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateSiteBlacklist); |
| 1146 | translate_prefs.BlacklistSite(host); |
| 1147 | EXPECT_TRUE(translate_prefs.IsSiteBlacklisted(host)); |
[email protected] | f9db7e0 | 2013-06-17 10:29:43 | [diff] [blame] | 1148 | EXPECT_TRUE(translate_prefs.CanTranslateLanguage(profile, "fr")); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1149 | |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1150 | 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] | f9db7e0 | 2013-06-17 10:29:43 | [diff] [blame] | 1162 | EXPECT_TRUE(translate_prefs.CanTranslateLanguage(profile, "fr")); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1163 | |
| 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] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 1172 | TEST_F(TranslateManagerBrowserTest, AlwaysTranslateLanguagePref) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1173 | // Select always translate French to English. |
| 1174 | Profile* profile = |
[email protected] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 1175 | Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1176 | PrefService* prefs = profile->GetPrefs(); |
| 1177 | PrefChangeRegistrar registrar; |
| 1178 | registrar.Init(prefs); |
[email protected] | 116129e0 | 2012-11-21 17:26:27 | [diff] [blame] | 1179 | registrar.Add(TranslatePrefs::kPrefTranslateWhitelists, pref_callback_); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1180 | 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] | 2ebd22d | 2013-01-07 17:04:27 | [diff] [blame] | 1192 | EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATING, infobar->infobar_type()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1193 | 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] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 1210 | static_cast<TestingProfile*>(web_contents()->GetBrowserContext()); |
[email protected] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 1211 | test_profile->ForceIncognito(true); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1212 | 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] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 1216 | test_profile->ForceIncognito(false); // Get back to non incognito. |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1217 | |
| 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] | 2ebd22d | 2013-01-07 17:04:27 | [diff] [blame] | 1226 | EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE, |
| 1227 | infobar->infobar_type()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | // Context menu. |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 1231 | TEST_F(TranslateManagerBrowserTest, ContextMenu) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1232 | // Blacklist www.google.fr and French for translation. |
| 1233 | GURL url("http://www.google.fr"); |
| 1234 | Profile* profile = |
[email protected] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 1235 | Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1236 | TranslatePrefs translate_prefs(profile->GetPrefs()); |
[email protected] | 5d37b2298 | 2013-06-25 06:07:00 | [diff] [blame] | 1237 | translate_prefs.BlockLanguage("fr"); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1238 | translate_prefs.BlacklistSite(url.host()); |
[email protected] | 5d37b2298 | 2013-06-25 06:07:00 | [diff] [blame] | 1239 | EXPECT_TRUE(translate_prefs.IsBlockedLanguage("fr")); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1240 | 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] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 1246 | TestRenderViewContextMenu::CreateContextMenu(web_contents())); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1247 | 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] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 1253 | menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents())); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1254 | 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] | 96bf34e | 2013-03-13 04:22:14 | [diff] [blame] | 1259 | menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1260 | |
| 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] | 2ebd22d | 2013-01-07 17:04:27 | [diff] [blame] | 1265 | EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATING, infobar->infobar_type()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1266 | 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] | 5d37b2298 | 2013-06-25 06:07:00 | [diff] [blame] | 1275 | EXPECT_FALSE(translate_prefs.IsBlockedLanguage("fr")); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1276 | EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(url.host())); |
| 1277 | |
| 1278 | // Let's simulate the page being translated. |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 1279 | SimulateOnPageTranslated("fr", "en"); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1280 | |
| 1281 | // The translate menu should now be disabled. |
[email protected] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 1282 | menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents())); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1283 | 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] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 1296 | menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents())); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1297 | menu->Init(); |
| 1298 | EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); |
[email protected] | 96bf34e | 2013-03-13 04:22:14 | [diff] [blame] | 1299 | menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1300 | // 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] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 1311 | menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents())); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1312 | menu->Init(); |
| 1313 | EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 1314 | SimulateOnPageTranslated("de", "en"); |
[email protected] | 96bf34e | 2013-03-13 04:22:14 | [diff] [blame] | 1315 | menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1316 | // 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] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 1322 | menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents())); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1323 | menu->Init(); |
| 1324 | EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 1325 | EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 1326 | |
[email protected] | 8e7d0b47 | 2013-05-23 05:41:37 | [diff] [blame] | 1327 | // Test that the translate context menu is enabled even if the page is in an |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1328 | // unsupported language. |
| 1329 | SimulateNavigation(url, "qbz", true); |
[email protected] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 1330 | menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents())); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1331 | menu->Init(); |
| 1332 | EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); |
[email protected] | 8e7d0b47 | 2013-05-23 05:41:37 | [diff] [blame] | 1333 | EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1334 | } |
| 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] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 1339 | TEST_F(TranslateManagerBrowserTest, BeforeTranslateExtraButtons) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1340 | Profile* profile = |
[email protected] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 1341 | Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1342 | 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] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 1352 | static_cast<TestingProfile*>(web_contents()->GetBrowserContext()); |
[email protected] | bd30672 | 2012-07-11 20:43:59 | [diff] [blame] | 1353 | static_cast<extensions::TestExtensionSystem*>( |
| 1354 | extensions::ExtensionSystem::Get(test_profile))-> |
[email protected] | 98b6d94 | 2013-11-10 00:34:07 | [diff] [blame] | 1355 | CreateProcessManager(); |
[email protected] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 1356 | test_profile->ForceIncognito(true); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1357 | 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] | 2ebd22d | 2013-01-07 17:04:27 | [diff] [blame] | 1363 | EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE, |
| 1364 | infobar->infobar_type()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1365 | if (i < 7) { |
[email protected] | f1717965 | 2013-04-25 18:11:10 | [diff] [blame] | 1366 | EXPECT_FALSE(infobar->ShouldShowAlwaysTranslateShortcut()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1367 | infobar->Translate(); |
| 1368 | process()->sink().ClearMessages(); |
| 1369 | } else { |
[email protected] | f1717965 | 2013-04-25 18:11:10 | [diff] [blame] | 1370 | EXPECT_TRUE(infobar->ShouldShowAlwaysTranslateShortcut()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1371 | } |
| 1372 | if (i == 3) |
[email protected] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 1373 | test_profile->ForceIncognito(false); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1374 | } |
| 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] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 1389 | test_profile->ForceIncognito(true); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1390 | 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] | 2ebd22d | 2013-01-07 17:04:27 | [diff] [blame] | 1396 | EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE, |
| 1397 | infobar->infobar_type()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1398 | if (i < 7) { |
[email protected] | f1717965 | 2013-04-25 18:11:10 | [diff] [blame] | 1399 | EXPECT_FALSE(infobar->ShouldShowNeverTranslateShortcut()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1400 | infobar->TranslationDeclined(); |
| 1401 | } else { |
[email protected] | f1717965 | 2013-04-25 18:11:10 | [diff] [blame] | 1402 | EXPECT_TRUE(infobar->ShouldShowNeverTranslateShortcut()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1403 | } |
| 1404 | if (i == 3) |
[email protected] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 1405 | test_profile->ForceIncognito(false); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1406 | } |
| 1407 | // Simulate the user pressing "Never translate French". |
| 1408 | infobar->NeverTranslatePageLanguage(); |
[email protected] | 5d37b2298 | 2013-06-25 06:07:00 | [diff] [blame] | 1409 | EXPECT_TRUE(translate_prefs.IsBlockedLanguage("de")); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1410 | // 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] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 1418 | TEST_F(TranslateManagerBrowserTest, NonTranslatablePage) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1419 | SimulateNavigation(GURL("http://mail.google.fr"), "fr", false); |
| 1420 | |
| 1421 | // We should not have an infobar. |
| 1422 | EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 1423 | |
[email protected] | 54e4b52 | 2013-05-21 08:36:23 | [diff] [blame] | 1424 | // The context menu is enabled to allow users to force translation. |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1425 | scoped_ptr<TestRenderViewContextMenu> menu( |
[email protected] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 1426 | TestRenderViewContextMenu::CreateContextMenu(web_contents())); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1427 | menu->Init(); |
| 1428 | EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); |
[email protected] | 54e4b52 | 2013-05-21 08:36:23 | [diff] [blame] | 1429 | EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1430 | } |
| 1431 | |
| 1432 | // Tests that the script is expired and refetched as expected. |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 1433 | TEST_F(TranslateManagerBrowserTest, ScriptExpires) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1434 | ExpireTranslateScriptImmediately(); |
| 1435 | |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1436 | 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] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 1442 | SimulateOnPageTranslated("fr", "en"); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1443 | |
| 1444 | // A task should have been posted to clear the script, run it. |
[email protected] | b3a2509 | 2013-05-28 22:08:16 | [diff] [blame] | 1445 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1446 | |
| 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] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 1457 | ChromeViewMsg_TranslatePage::ID) == NULL); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1458 | // 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] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 1467 | |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 1468 | TEST_F(TranslateManagerBrowserTest, DownloadsAndHistoryNotTranslated) { |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 1469 | ASSERT_FALSE(TranslateManager::IsTranslatableURL( |
| 1470 | GURL(chrome::kChromeUIDownloadsURL))); |
| 1471 | ASSERT_FALSE(TranslateManager::IsTranslatableURL( |
| 1472 | GURL(chrome::kChromeUIHistoryURL))); |
| 1473 | } |
| 1474 | |
[email protected] | e625b760 | 2013-10-28 09:24:56 | [diff] [blame] | 1475 | #if !defined(OS_ANDROID) && !defined(OS_IOS) |
| 1476 | |
| 1477 | TEST_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 | |
| 1519 | TEST_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 | |
| 1552 | TEST_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] | 373b461 | 2012-12-15 23:26:41 | [diff] [blame] | 1584 | // Test is flaky on Win http://crbug.com/166334 |
| 1585 | #if defined(OS_WIN) |
| 1586 | #define MAYBE_PRE_TranslateSessionRestore DISABLED_PRE_TranslateSessionRestore |
| 1587 | #else |
| 1588 | #define MAYBE_PRE_TranslateSessionRestore PRE_TranslateSessionRestore |
| 1589 | #endif |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 1590 | // Test that session restore restores the translate infobar and other translate |
| 1591 | // settings. |
[email protected] | 373b461 | 2012-12-15 23:26:41 | [diff] [blame] | 1592 | IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, |
| 1593 | MAYBE_PRE_TranslateSessionRestore) { |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 1594 | SessionStartupPref pref(SessionStartupPref::LAST); |
| 1595 | SessionStartupPref::SetStartupPref(browser()->profile(), pref); |
| 1596 | |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 1597 | content::WebContents* current_web_contents = |
[email protected] | 47ae2337 | 2013-01-29 01:50:48 | [diff] [blame] | 1598 | browser()->tab_strip_model()->GetActiveWebContents(); |
[email protected] | f0235f6 | 2012-09-28 16:50:57 | [diff] [blame] | 1599 | TranslateTabHelper* translate_tab_helper = |
| 1600 | TranslateTabHelper::FromWebContents(current_web_contents); |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 1601 | content::Source<content::WebContents> source(current_web_contents); |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 1602 | |
[email protected] | 27eff89 | 2013-05-21 16:40:52 | [diff] [blame] | 1603 | ui_test_utils::WindowedNotificationObserverWithDetails< |
| 1604 | LanguageDetectionDetails> |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 1605 | fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, |
| 1606 | source); |
| 1607 | |
| 1608 | GURL french_url = ui_test_utils::GetTestUrl( |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 1609 | base::FilePath(), base::FilePath(FILE_PATH_LITERAL("french_page.html"))); |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 1610 | ui_test_utils::NavigateToURL(browser(), french_url); |
| 1611 | fr_language_detected_signal.Wait(); |
[email protected] | 27eff89 | 2013-05-21 16:40:52 | [diff] [blame] | 1612 | LanguageDetectionDetails details; |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 1613 | EXPECT_TRUE(fr_language_detected_signal.GetDetailsFor( |
[email protected] | 27eff89 | 2013-05-21 16:40:52 | [diff] [blame] | 1614 | source.map_key(), &details)); |
| 1615 | EXPECT_EQ("fr", details.adopted_language); |
[email protected] | 3155c527 | 2014-01-21 12:38:12 | [diff] [blame] | 1616 | EXPECT_EQ("fr", translate_tab_helper->GetLanguageState().original_language()); |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 1617 | } |
| 1618 | |
[email protected] | 373b461 | 2012-12-15 23:26:41 | [diff] [blame] | 1619 | #if defined (OS_WIN) |
| 1620 | #define MAYBE_TranslateSessionRestore DISABLED_TranslateSessionRestore |
| 1621 | #else |
| 1622 | #define MAYBE_TranslateSessionRestore TranslateSessionRestore |
| 1623 | #endif |
| 1624 | IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, MAYBE_TranslateSessionRestore) { |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 1625 | content::WebContents* current_web_contents = |
[email protected] | 47ae2337 | 2013-01-29 01:50:48 | [diff] [blame] | 1626 | browser()->tab_strip_model()->GetActiveWebContents(); |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 1627 | content::Source<content::WebContents> source(current_web_contents); |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 1628 | |
[email protected] | 27eff89 | 2013-05-21 16:40:52 | [diff] [blame] | 1629 | ui_test_utils::WindowedNotificationObserverWithDetails< |
| 1630 | LanguageDetectionDetails> |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 1631 | fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, |
| 1632 | source); |
| 1633 | fr_language_detected_signal.Wait(); |
| 1634 | } |