[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] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 26 | #include "chrome/browser/translate/translate_tab_helper.h" |
| 27 | #include "chrome/browser/ui/browser.h" |
[email protected] | 47ae2337 | 2013-01-29 01:50:48 | [diff] [blame] | 28 | #include "chrome/browser/ui/tabs/tab_strip_model.h" |
[email protected] | e625b760 | 2013-10-28 09:24:56 | [diff] [blame^] | 29 | #include "chrome/browser/ui/translate/translate_bubble_factory.h" |
| 30 | #include "chrome/browser/ui/translate/translate_bubble_model.h" |
| 31 | #include "chrome/browser/ui/translate/translate_bubble_model_impl.h" |
| 32 | #include "chrome/common/chrome_switches.h" |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 33 | #include "chrome/common/pref_names.h" |
| 34 | #include "chrome/common/render_messages.h" |
[email protected] | b2ed9c67 | 2013-06-06 23:34:00 | [diff] [blame] | 35 | #include "chrome/common/translate/language_detection_details.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] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 42 | #include "content/public/browser/navigation_details.h" |
| 43 | #include "content/public/browser/navigation_entry.h" |
| 44 | #include "content/public/browser/notification_details.h" |
| 45 | #include "content/public/browser/notification_registrar.h" |
| 46 | #include "content/public/browser/web_contents.h" |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 47 | #include "content/public/test/mock_render_process_host.h" |
[email protected] | b1e3f20 | 2012-06-04 14:45:50 | [diff] [blame] | 48 | #include "content/public/test/test_renderer_host.h" |
[email protected] | 8f2d8bdd | 2012-06-19 23:44:05 | [diff] [blame] | 49 | #include "net/url_request/test_url_fetcher_factory.h" |
[email protected] | 140930a | 2013-06-21 16:11:09 | [diff] [blame] | 50 | #include "net/url_request/url_fetcher_delegate.h" |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 51 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | 2255a933 | 2013-06-17 05:12:31 | [diff] [blame] | 52 | #include "third_party/WebKit/public/web/WebContextMenuData.h" |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 53 | |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 54 | using content::RenderViewHostTester; |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 55 | |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 56 | // An observer that keeps track of whether a navigation entry was committed. |
| 57 | class NavEntryCommittedObserver : public content::NotificationObserver { |
| 58 | public: |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 59 | explicit NavEntryCommittedObserver(content::WebContents* web_contents) { |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 60 | registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 61 | content::Source<content::NavigationController>( |
| 62 | &web_contents->GetController())); |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | virtual void Observe(int type, |
| 66 | const content::NotificationSource& source, |
[email protected] | b94584a | 2013-02-07 03:02:08 | [diff] [blame] | 67 | const content::NotificationDetails& details) OVERRIDE { |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 68 | DCHECK(type == content::NOTIFICATION_NAV_ENTRY_COMMITTED); |
| 69 | details_ = |
| 70 | *(content::Details<content::LoadCommittedDetails>(details).ptr()); |
| 71 | } |
| 72 | |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 73 | const content::LoadCommittedDetails& load_committed_details() const { |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 74 | return details_; |
| 75 | } |
| 76 | |
| 77 | private: |
| 78 | content::LoadCommittedDetails details_; |
| 79 | content::NotificationRegistrar registrar_; |
| 80 | |
| 81 | DISALLOW_COPY_AND_ASSIGN(NavEntryCommittedObserver); |
| 82 | }; |
| 83 | |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 84 | class TranslateManagerBrowserTest : public ChromeRenderViewHostTestHarness, |
| 85 | public content::NotificationObserver { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 86 | public: |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 87 | TranslateManagerBrowserTest() |
| 88 | : pref_callback_( |
| 89 | base::Bind(&TranslateManagerBrowserTest::OnPreferenceChanged, |
[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame] | 90 | base::Unretained(this))) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | // Simulates navigating to a page and getting the page contents and language |
| 94 | // for that navigation. |
| 95 | void SimulateNavigation(const GURL& url, |
| 96 | const std::string& lang, |
| 97 | bool page_translatable) { |
| 98 | NavigateAndCommit(url); |
| 99 | SimulateOnTranslateLanguageDetermined(lang, page_translatable); |
| 100 | } |
| 101 | |
| 102 | void SimulateOnTranslateLanguageDetermined(const std::string& lang, |
| 103 | bool page_translatable) { |
[email protected] | 27eff89 | 2013-05-21 16:40:52 | [diff] [blame] | 104 | LanguageDetectionDetails details; |
| 105 | details.adopted_language = lang; |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 106 | RenderViewHostTester::TestOnMessageReceived( |
| 107 | rvh(), |
| 108 | ChromeViewHostMsg_TranslateLanguageDetermined( |
[email protected] | 27eff89 | 2013-05-21 16:40:52 | [diff] [blame] | 109 | 0, details, page_translatable)); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 110 | } |
| 111 | |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 112 | void SimulateOnPageTranslated(int routing_id, |
| 113 | const std::string& source_lang, |
| 114 | const std::string& target_lang, |
| 115 | TranslateErrors::Type error) { |
| 116 | RenderViewHostTester::TestOnMessageReceived( |
| 117 | rvh(), |
| 118 | ChromeViewHostMsg_PageTranslated( |
| 119 | routing_id, 0, source_lang, target_lang, error)); |
| 120 | } |
| 121 | |
| 122 | void SimulateOnPageTranslated(const std::string& source_lang, |
| 123 | const std::string& target_lang) { |
| 124 | SimulateOnPageTranslated(0, source_lang, target_lang, |
| 125 | TranslateErrors::NONE); |
| 126 | } |
| 127 | |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 128 | bool GetTranslateMessage(int* page_id, |
| 129 | std::string* original_lang, |
| 130 | std::string* target_lang) { |
| 131 | const IPC::Message* message = |
| 132 | process()->sink().GetFirstMessageMatching( |
| 133 | ChromeViewMsg_TranslatePage::ID); |
| 134 | if (!message) |
| 135 | return false; |
| 136 | Tuple4<int, std::string, std::string, std::string> translate_param; |
| 137 | ChromeViewMsg_TranslatePage::Read(message, &translate_param); |
| 138 | if (page_id) |
| 139 | *page_id = translate_param.a; |
| 140 | // Ignore translate_param.b which is the script injected in the page. |
| 141 | if (original_lang) |
| 142 | *original_lang = translate_param.c; |
| 143 | if (target_lang) |
| 144 | *target_lang = translate_param.d; |
| 145 | return true; |
| 146 | } |
| 147 | |
[email protected] | 4f822f02 | 2012-12-20 19:11:42 | [diff] [blame] | 148 | InfoBarService* infobar_service() { |
| 149 | return InfoBarService::FromWebContents(web_contents()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | // Returns the translate infobar if there is 1 infobar and it is a translate |
| 153 | // infobar. |
| 154 | TranslateInfoBarDelegate* GetTranslateInfoBar() { |
[email protected] | a608d17 | 2013-04-23 22:27:37 | [diff] [blame] | 155 | return (infobar_service()->infobar_count() == 1) ? |
| 156 | infobar_service()->infobar_at(0)->AsTranslateInfoBarDelegate() : NULL; |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | // If there is 1 infobar and it is a translate infobar, closes it and returns |
| 160 | // true. Returns false otherwise. |
| 161 | bool CloseTranslateInfoBar() { |
| 162 | InfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 163 | if (!infobar) |
| 164 | return false; |
| 165 | infobar->InfoBarDismissed(); // Simulates closing the infobar. |
[email protected] | 4f822f02 | 2012-12-20 19:11:42 | [diff] [blame] | 166 | infobar_service()->RemoveInfoBar(infobar); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 167 | return true; |
| 168 | } |
| 169 | |
| 170 | // Checks whether |infobar| has been removed and clears the removed infobar |
| 171 | // list. |
| 172 | bool CheckInfoBarRemovedAndReset(InfoBarDelegate* delegate) { |
| 173 | bool found = removed_infobars_.count(delegate) != 0; |
| 174 | removed_infobars_.clear(); |
| 175 | return found; |
| 176 | } |
| 177 | |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 178 | void ExpireTranslateScriptImmediately() { |
[email protected] | 140930a | 2013-06-21 16:11:09 | [diff] [blame] | 179 | TranslateManager::GetInstance()->SetTranslateScriptExpirationDelay(0); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | // If there is 1 infobar and it is a translate infobar, deny translation and |
| 183 | // returns true. Returns false otherwise. |
| 184 | bool DenyTranslation() { |
| 185 | TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 186 | if (!infobar) |
| 187 | return false; |
| 188 | infobar->TranslationDeclined(); |
[email protected] | 4f822f02 | 2012-12-20 19:11:42 | [diff] [blame] | 189 | infobar_service()->RemoveInfoBar(infobar); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 190 | return true; |
| 191 | } |
| 192 | |
[email protected] | b637bcd | 2013-03-28 12:50:32 | [diff] [blame] | 193 | void ReloadAndWait(bool successful_reload) { |
[email protected] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 194 | NavEntryCommittedObserver nav_observer(web_contents()); |
[email protected] | b637bcd | 2013-03-28 12:50:32 | [diff] [blame] | 195 | if (successful_reload) |
| 196 | Reload(); |
| 197 | else |
| 198 | FailedReload(); |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 199 | |
| 200 | // Ensures it is really handled a reload. |
| 201 | const content::LoadCommittedDetails& nav_details = |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 202 | nav_observer.load_committed_details(); |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 203 | EXPECT_TRUE(nav_details.entry != NULL); // There was a navigation. |
| 204 | EXPECT_EQ(content::NAVIGATION_TYPE_EXISTING_PAGE, nav_details.type); |
| 205 | |
| 206 | // The TranslateManager class processes the navigation entry committed |
| 207 | // notification in a posted task; process that task. |
[email protected] | b3a2509 | 2013-05-28 22:08:16 | [diff] [blame] | 208 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 209 | } |
| 210 | |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 211 | virtual void Observe(int type, |
| 212 | const content::NotificationSource& source, |
| 213 | const content::NotificationDetails& details) { |
| 214 | DCHECK_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, type); |
| 215 | removed_infobars_.insert( |
| 216 | content::Details<InfoBarRemovedDetails>(details)->first); |
| 217 | } |
| 218 | |
[email protected] | 116129e0 | 2012-11-21 17:26:27 | [diff] [blame] | 219 | MOCK_METHOD1(OnPreferenceChanged, void(const std::string&)); |
[email protected] | a6a7ced | 2012-11-01 17:24:18 | [diff] [blame] | 220 | |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 221 | protected: |
| 222 | virtual void SetUp() { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 223 | // Access the TranslateManager singleton so it is created before we call |
[email protected] | 292ccc9 | 2012-10-17 12:46:02 | [diff] [blame] | 224 | // ChromeRenderViewHostTestHarness::SetUp() to match what's done in Chrome, |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 225 | // where the TranslateManager is created before the WebContents. This |
| 226 | // matters as they both register for similar events and we want the |
| 227 | // notifications to happen in the same sequence (TranslateManager first, |
| 228 | // WebContents second). Also clears the translate script so it is fetched |
| 229 | // everytime and sets the expiration delay to a large value by default (in |
| 230 | // case it was zeroed in a previous test). |
| 231 | TranslateManager::GetInstance()->ClearTranslateScript(); |
| 232 | TranslateManager::GetInstance()-> |
[email protected] | 140930a | 2013-06-21 16:11:09 | [diff] [blame] | 233 | SetTranslateScriptExpirationDelay(60 * 60 * 1000); |
[email protected] | 9ad06a1 | 2013-05-10 14:49:53 | [diff] [blame] | 234 | TranslateManager::GetInstance()->set_translate_max_reload_attemps(0); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 235 | |
[email protected] | 292ccc9 | 2012-10-17 12:46:02 | [diff] [blame] | 236 | ChromeRenderViewHostTestHarness::SetUp(); |
[email protected] | 4f822f02 | 2012-12-20 19:11:42 | [diff] [blame] | 237 | InfoBarService::CreateForWebContents(web_contents()); |
[email protected] | 292ccc9 | 2012-10-17 12:46:02 | [diff] [blame] | 238 | TranslateTabHelper::CreateForWebContents(web_contents()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 239 | |
| 240 | notification_registrar_.Add(this, |
| 241 | chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, |
[email protected] | 4f822f02 | 2012-12-20 19:11:42 | [diff] [blame] | 242 | content::Source<InfoBarService>(infobar_service())); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | virtual void TearDown() { |
| 246 | process()->sink().ClearMessages(); |
| 247 | |
| 248 | notification_registrar_.Remove(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 | |
[email protected] | 292ccc9 | 2012-10-17 12:46:02 | [diff] [blame] | 252 | ChromeRenderViewHostTestHarness::TearDown(); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | void SimulateTranslateScriptURLFetch(bool success) { |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 256 | net::TestURLFetcher* fetcher = |
| 257 | url_fetcher_factory_.GetFetcherByID(TranslateScript::kFetcherId); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 258 | ASSERT_TRUE(fetcher); |
| 259 | net::URLRequestStatus status; |
| 260 | status.set_status(success ? net::URLRequestStatus::SUCCESS : |
| 261 | net::URLRequestStatus::FAILED); |
| 262 | fetcher->set_url(fetcher->GetOriginalURL()); |
| 263 | fetcher->set_status(status); |
| 264 | fetcher->set_response_code(success ? 200 : 500); |
| 265 | fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 266 | } |
| 267 | |
| 268 | void SimulateSupportedLanguagesURLFetch( |
[email protected] | 7da697a | 2013-07-12 08:09:46 | [diff] [blame] | 269 | bool success, |
| 270 | const std::vector<std::string>& languages, |
| 271 | bool use_alpha_languages, |
| 272 | const std::vector<std::string>& alpha_languages) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 273 | net::URLRequestStatus status; |
| 274 | status.set_status(success ? net::URLRequestStatus::SUCCESS : |
| 275 | net::URLRequestStatus::FAILED); |
| 276 | |
| 277 | std::string data; |
| 278 | if (success) { |
[email protected] | 4df61c70 | 2013-05-28 10:55:30 | [diff] [blame] | 279 | data = base::StringPrintf( |
| 280 | "%s{\"sl\": {\"bla\": \"bla\"}, \"%s\": {", |
| 281 | TranslateLanguageList::kLanguageListCallbackName, |
| 282 | TranslateLanguageList::kTargetLanguagesKey); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 283 | const char* comma = ""; |
| 284 | for (size_t i = 0; i < languages.size(); ++i) { |
| 285 | data += base::StringPrintf( |
[email protected] | 2a4e1ae | 2013-04-15 14:41:36 | [diff] [blame] | 286 | "%s\"%s\": \"UnusedFullName\"", comma, languages[i].c_str()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 287 | if (i == 0) |
| 288 | comma = ","; |
| 289 | } |
[email protected] | 7da697a | 2013-07-12 08:09:46 | [diff] [blame] | 290 | |
| 291 | if (use_alpha_languages) { |
| 292 | data += base::StringPrintf("},\"%s\": {", |
| 293 | TranslateLanguageList::kAlphaLanguagesKey); |
| 294 | comma = ""; |
| 295 | for (size_t i = 0; i < alpha_languages.size(); ++i) { |
| 296 | data += base::StringPrintf("%s\"%s\": 1", comma, |
| 297 | alpha_languages[i].c_str()); |
| 298 | if (i == 0) |
| 299 | comma = ","; |
| 300 | } |
| 301 | } |
| 302 | |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 303 | data += "}})"; |
| 304 | } |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 305 | net::TestURLFetcher* fetcher = |
| 306 | url_fetcher_factory_.GetFetcherByID(TranslateLanguageList::kFetcherId); |
[email protected] | 7da697a | 2013-07-12 08:09:46 | [diff] [blame] | 307 | ASSERT_TRUE(fetcher != NULL); |
| 308 | fetcher->set_url(fetcher->GetOriginalURL()); |
| 309 | fetcher->set_status(status); |
| 310 | fetcher->set_response_code(success ? 200 : 500); |
| 311 | fetcher->SetResponseString(data); |
| 312 | fetcher->delegate()->OnURLFetchComplete(fetcher); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | void SetPrefObserverExpectation(const char* path) { |
[email protected] | 116129e0 | 2012-11-21 17:26:27 | [diff] [blame] | 316 | EXPECT_CALL(*this, OnPreferenceChanged(std::string(path))); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 317 | } |
| 318 | |
[email protected] | 116129e0 | 2012-11-21 17:26:27 | [diff] [blame] | 319 | PrefChangeRegistrar::NamedChangeCallback pref_callback_; |
| 320 | |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 321 | private: |
| 322 | content::NotificationRegistrar notification_registrar_; |
[email protected] | 8f2d8bdd | 2012-06-19 23:44:05 | [diff] [blame] | 323 | net::TestURLFetcherFactory url_fetcher_factory_; |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 324 | |
| 325 | // The infobars that have been removed. |
| 326 | // WARNING: the pointers point to deleted objects, use only for comparison. |
| 327 | std::set<InfoBarDelegate*> removed_infobars_; |
| 328 | |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 329 | DISALLOW_COPY_AND_ASSIGN(TranslateManagerBrowserTest); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 330 | }; |
| 331 | |
[email protected] | e625b760 | 2013-10-28 09:24:56 | [diff] [blame^] | 332 | class MockTranslateBubbleFactory : public TranslateBubbleFactory { |
| 333 | public: |
| 334 | MockTranslateBubbleFactory() { |
| 335 | } |
| 336 | |
| 337 | virtual void ShowImplementation( |
| 338 | BrowserWindow* window, |
| 339 | content::WebContents* web_contents, |
| 340 | TranslateBubbleModel::ViewState view_state) OVERRIDE { |
| 341 | if (model_) { |
| 342 | model_->SetViewState(view_state); |
| 343 | return; |
| 344 | } |
| 345 | |
| 346 | TranslateTabHelper* translate_tab_helper = |
| 347 | TranslateTabHelper::FromWebContents(web_contents); |
| 348 | std::string source_language = |
| 349 | translate_tab_helper->language_state().original_language(); |
| 350 | std::string target_language = TranslateManager::GetLanguageCode( |
| 351 | g_browser_process->GetApplicationLocale()); |
| 352 | scoped_ptr<TranslateUIDelegate> ui_delegate( |
| 353 | new TranslateUIDelegate(web_contents, |
| 354 | source_language, |
| 355 | target_language)); |
| 356 | model_.reset( |
| 357 | new TranslateBubbleModelImpl(view_state, ui_delegate.Pass())); |
| 358 | } |
| 359 | |
| 360 | TranslateBubbleModel* model() { return model_.get(); } |
| 361 | |
| 362 | private: |
| 363 | scoped_ptr<TranslateBubbleModel> model_; |
| 364 | |
| 365 | DISALLOW_COPY_AND_ASSIGN(MockTranslateBubbleFactory); |
| 366 | }; |
| 367 | |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 368 | namespace { |
| 369 | |
| 370 | class TestRenderViewContextMenu : public RenderViewContextMenu { |
| 371 | public: |
| 372 | static TestRenderViewContextMenu* CreateContextMenu( |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 373 | content::WebContents* web_contents) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 374 | content::ContextMenuParams params; |
| 375 | params.media_type = WebKit::WebContextMenuData::MediaTypeNone; |
| 376 | params.x = 0; |
| 377 | params.y = 0; |
[email protected] | 6462f8111 | 2013-08-23 23:00:37 | [diff] [blame] | 378 | params.has_image_contents = true; |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 379 | params.media_flags = 0; |
| 380 | params.spellcheck_enabled = false; |
| 381 | params.is_editable = false; |
| 382 | params.page_url = web_contents->GetController().GetActiveEntry()->GetURL(); |
| 383 | #if defined(OS_MACOSX) |
| 384 | params.writing_direction_default = 0; |
| 385 | params.writing_direction_left_to_right = 0; |
| 386 | params.writing_direction_right_to_left = 0; |
| 387 | #endif // OS_MACOSX |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 388 | params.edit_flags = WebKit::WebContextMenuData::CanTranslate; |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 389 | return new TestRenderViewContextMenu(web_contents, params); |
| 390 | } |
| 391 | |
| 392 | bool IsItemPresent(int id) { |
| 393 | return menu_model_.GetIndexOfCommandId(id) != -1; |
| 394 | } |
| 395 | |
[email protected] | b94584a | 2013-02-07 03:02:08 | [diff] [blame] | 396 | virtual void PlatformInit() OVERRIDE { } |
| 397 | virtual void PlatformCancel() OVERRIDE { } |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 398 | virtual bool GetAcceleratorForCommandId( |
| 399 | int command_id, |
[email protected] | b94584a | 2013-02-07 03:02:08 | [diff] [blame] | 400 | ui::Accelerator* accelerator) OVERRIDE { return false; } |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 401 | |
| 402 | private: |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 403 | TestRenderViewContextMenu(content::WebContents* web_contents, |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 404 | const content::ContextMenuParams& params) |
| 405 | : RenderViewContextMenu(web_contents, params) { |
| 406 | } |
| 407 | |
| 408 | DISALLOW_COPY_AND_ASSIGN(TestRenderViewContextMenu); |
| 409 | }; |
| 410 | |
| 411 | } // namespace |
| 412 | |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 413 | TEST_F(TranslateManagerBrowserTest, NormalTranslate) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 414 | SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 415 | |
| 416 | // We should have an infobar. |
| 417 | TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 418 | ASSERT_TRUE(infobar != NULL); |
[email protected] | 2ebd22d | 2013-01-07 17:04:27 | [diff] [blame] | 419 | EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE, |
| 420 | infobar->infobar_type()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 421 | |
| 422 | // Simulate clicking translate. |
| 423 | process()->sink().ClearMessages(); |
| 424 | infobar->Translate(); |
| 425 | |
| 426 | // The "Translating..." infobar should be showing. |
| 427 | infobar = GetTranslateInfoBar(); |
| 428 | ASSERT_TRUE(infobar != NULL); |
[email protected] | 2ebd22d | 2013-01-07 17:04:27 | [diff] [blame] | 429 | EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATING, infobar->infobar_type()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 430 | |
| 431 | // Simulate the translate script being retrieved (it only needs to be done |
| 432 | // once in the test as it is cached). |
| 433 | SimulateTranslateScriptURLFetch(true); |
| 434 | |
| 435 | // Test that we sent the right message to the renderer. |
| 436 | int page_id = 0; |
| 437 | std::string original_lang, target_lang; |
| 438 | EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 439 | EXPECT_EQ("fr", original_lang); |
| 440 | EXPECT_EQ("en", target_lang); |
| 441 | |
| 442 | // Simulate the render notifying the translation has been done. |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 443 | SimulateOnPageTranslated("fr", "en"); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 444 | |
| 445 | // The after translate infobar should be showing. |
| 446 | infobar = GetTranslateInfoBar(); |
| 447 | ASSERT_TRUE(infobar != NULL); |
[email protected] | 2ebd22d | 2013-01-07 17:04:27 | [diff] [blame] | 448 | EXPECT_EQ(TranslateInfoBarDelegate::AFTER_TRANSLATE, infobar->infobar_type()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 449 | |
[email protected] | 9f94348 | 2012-09-21 01:05:13 | [diff] [blame] | 450 | // Simulate changing the original language and translating. |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 451 | process()->sink().ClearMessages(); |
[email protected] | 9f94348 | 2012-09-21 01:05:13 | [diff] [blame] | 452 | std::string new_original_lang = infobar->language_code_at(0); |
| 453 | infobar->set_original_language_index(0); |
| 454 | infobar->Translate(); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 455 | EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 456 | EXPECT_EQ(new_original_lang, original_lang); |
| 457 | EXPECT_EQ("en", target_lang); |
| 458 | // Simulate the render notifying the translation has been done. |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 459 | SimulateOnPageTranslated(new_original_lang, "en"); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 460 | // infobar is now invalid. |
| 461 | TranslateInfoBarDelegate* new_infobar = GetTranslateInfoBar(); |
| 462 | ASSERT_TRUE(new_infobar != NULL); |
| 463 | infobar = new_infobar; |
| 464 | |
[email protected] | 9f94348 | 2012-09-21 01:05:13 | [diff] [blame] | 465 | // Simulate changing the target language and translating. |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 466 | process()->sink().ClearMessages(); |
[email protected] | 9f94348 | 2012-09-21 01:05:13 | [diff] [blame] | 467 | std::string new_target_lang = infobar->language_code_at(1); |
| 468 | infobar->set_target_language_index(1); |
| 469 | infobar->Translate(); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 470 | EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 471 | EXPECT_EQ(new_original_lang, original_lang); |
| 472 | EXPECT_EQ(new_target_lang, target_lang); |
| 473 | // Simulate the render notifying the translation has been done. |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 474 | SimulateOnPageTranslated(new_original_lang, new_target_lang); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 475 | // infobar is now invalid. |
| 476 | new_infobar = GetTranslateInfoBar(); |
| 477 | ASSERT_TRUE(new_infobar != NULL); |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 478 | |
| 479 | // Verify reload keeps the same settings. |
[email protected] | b637bcd | 2013-03-28 12:50:32 | [diff] [blame] | 480 | ReloadAndWait(true); |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 481 | new_infobar = GetTranslateInfoBar(); |
| 482 | ASSERT_TRUE(new_infobar != NULL); |
[email protected] | 9f94348 | 2012-09-21 01:05:13 | [diff] [blame] | 483 | ASSERT_EQ(new_target_lang, infobar->target_language_code()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 484 | } |
| 485 | |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 486 | TEST_F(TranslateManagerBrowserTest, TranslateScriptNotAvailable) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 487 | SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 488 | |
| 489 | // We should have an infobar. |
| 490 | TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 491 | ASSERT_TRUE(infobar != NULL); |
[email protected] | 2ebd22d | 2013-01-07 17:04:27 | [diff] [blame] | 492 | EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE, |
| 493 | infobar->infobar_type()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 494 | |
| 495 | // Simulate clicking translate. |
| 496 | process()->sink().ClearMessages(); |
| 497 | infobar->Translate(); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 498 | SimulateTranslateScriptURLFetch(false); |
| 499 | |
| 500 | // We should not have sent any message to translate to the renderer. |
| 501 | EXPECT_FALSE(GetTranslateMessage(NULL, NULL, NULL)); |
| 502 | |
| 503 | // And we should have an error infobar showing. |
| 504 | infobar = GetTranslateInfoBar(); |
| 505 | ASSERT_TRUE(infobar != NULL); |
[email protected] | 2ebd22d | 2013-01-07 17:04:27 | [diff] [blame] | 506 | EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, |
| 507 | infobar->infobar_type()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | // Ensures we deal correctly with pages for which the browser does not recognize |
| 511 | // the language (the translate server may or not detect the language). |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 512 | TEST_F(TranslateManagerBrowserTest, TranslateUnknownLanguage) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 513 | // Simulate navigating to a page ("und" is the string returned by the CLD for |
| 514 | // languages it does not recognize). |
| 515 | SimulateNavigation(GURL("http://www.google.mys"), "und", true); |
| 516 | |
| 517 | // We should not have an infobar as we don't know the language. |
| 518 | ASSERT_TRUE(GetTranslateInfoBar() == NULL); |
| 519 | |
| 520 | // Translate the page anyway throught the context menu. |
| 521 | scoped_ptr<TestRenderViewContextMenu> menu( |
[email protected] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 522 | TestRenderViewContextMenu::CreateContextMenu(web_contents())); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 523 | menu->Init(); |
[email protected] | 96bf34e | 2013-03-13 04:22:14 | [diff] [blame] | 524 | menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 525 | |
| 526 | // To test that bug #49018 if fixed, make sure we deal correctly with errors. |
| 527 | // Simulate a failure to fetch the translate script. |
| 528 | SimulateTranslateScriptURLFetch(false); |
| 529 | TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 530 | ASSERT_TRUE(infobar != NULL); |
[email protected] | 2ebd22d | 2013-01-07 17:04:27 | [diff] [blame] | 531 | EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, |
| 532 | infobar->infobar_type()); |
[email protected] | cacaa24 | 2013-07-19 22:00:36 | [diff] [blame] | 533 | EXPECT_TRUE(infobar->is_error()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 534 | infobar->MessageInfoBarButtonPressed(); |
| 535 | SimulateTranslateScriptURLFetch(true); // This time succeed. |
| 536 | |
| 537 | // Simulate the render notifying the translation has been done, the server |
| 538 | // having detected the page was in a known and supported language. |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 539 | SimulateOnPageTranslated("fr", "en"); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 540 | |
| 541 | // The after translate infobar should be showing. |
| 542 | infobar = GetTranslateInfoBar(); |
| 543 | ASSERT_TRUE(infobar != NULL); |
[email protected] | 2ebd22d | 2013-01-07 17:04:27 | [diff] [blame] | 544 | EXPECT_EQ(TranslateInfoBarDelegate::AFTER_TRANSLATE, infobar->infobar_type()); |
[email protected] | 9f94348 | 2012-09-21 01:05:13 | [diff] [blame] | 545 | EXPECT_EQ("fr", infobar->original_language_code()); |
| 546 | EXPECT_EQ("en", infobar->target_language_code()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 547 | |
| 548 | // Let's run the same steps but this time the server detects the page is |
| 549 | // already in English. |
| 550 | SimulateNavigation(GURL("http://www.google.com"), "und", true); |
[email protected] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 551 | menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents())); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 552 | menu->Init(); |
[email protected] | 96bf34e | 2013-03-13 04:22:14 | [diff] [blame] | 553 | menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0); |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 554 | SimulateOnPageTranslated(1, "en", "en", TranslateErrors::IDENTICAL_LANGUAGES); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 555 | infobar = GetTranslateInfoBar(); |
| 556 | ASSERT_TRUE(infobar != NULL); |
[email protected] | 2ebd22d | 2013-01-07 17:04:27 | [diff] [blame] | 557 | EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, |
| 558 | infobar->infobar_type()); |
| 559 | EXPECT_EQ(TranslateErrors::IDENTICAL_LANGUAGES, infobar->error_type()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 560 | |
| 561 | // Let's run the same steps again but this time the server fails to detect the |
| 562 | // page's language (it returns an empty string). |
| 563 | SimulateNavigation(GURL("http://www.google.com"), "und", true); |
[email protected] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 564 | menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents())); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 565 | menu->Init(); |
[email protected] | 96bf34e | 2013-03-13 04:22:14 | [diff] [blame] | 566 | menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0); |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 567 | SimulateOnPageTranslated(2, std::string(), "en", |
| 568 | TranslateErrors::UNKNOWN_LANGUAGE); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 569 | infobar = GetTranslateInfoBar(); |
| 570 | ASSERT_TRUE(infobar != NULL); |
[email protected] | 2ebd22d | 2013-01-07 17:04:27 | [diff] [blame] | 571 | EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, |
| 572 | infobar->infobar_type()); |
| 573 | EXPECT_EQ(TranslateErrors::UNKNOWN_LANGUAGE, infobar->error_type()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 574 | } |
| 575 | |
[email protected] | f274d37 | 2013-07-22 13:08:50 | [diff] [blame] | 576 | // Tests that we show/don't show an info-bar for the languages. |
| 577 | TEST_F(TranslateManagerBrowserTest, TestLanguages) { |
| 578 | std::vector<std::string> languages; |
| 579 | languages.push_back("en"); |
| 580 | languages.push_back("ja"); |
| 581 | languages.push_back("fr"); |
| 582 | languages.push_back("ht"); |
| 583 | languages.push_back("xx"); |
| 584 | languages.push_back("zh"); |
| 585 | languages.push_back("zh-CN"); |
| 586 | languages.push_back("und"); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 587 | |
| 588 | GURL url("http://www.google.com"); |
[email protected] | f274d37 | 2013-07-22 13:08:50 | [diff] [blame] | 589 | for (size_t i = 0; i < languages.size(); ++i) { |
| 590 | std::string lang = languages[i]; |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 591 | SCOPED_TRACE(::testing::Message() << "Iteration " << i << |
[email protected] | f274d37 | 2013-07-22 13:08:50 | [diff] [blame] | 592 | " language=" << lang); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 593 | |
| 594 | // We should not have a translate infobar. |
| 595 | TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 596 | ASSERT_TRUE(infobar == NULL); |
| 597 | |
[email protected] | f274d37 | 2013-07-22 13:08:50 | [diff] [blame] | 598 | SimulateNavigation(url, lang, true); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 599 | |
| 600 | // Verify we have/don't have an info-bar as expected. |
| 601 | infobar = GetTranslateInfoBar(); |
[email protected] | f274d37 | 2013-07-22 13:08:50 | [diff] [blame] | 602 | bool expected = TranslateManager::IsSupportedLanguage(lang) && |
| 603 | lang != "en"; |
| 604 | EXPECT_EQ(expected, infobar != NULL); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 605 | |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 606 | if (infobar != NULL) |
| 607 | EXPECT_TRUE(CloseTranslateInfoBar()); |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | // Test the fetching of languages from the translate server |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 612 | TEST_F(TranslateManagerBrowserTest, FetchLanguagesFromTranslateServer) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 613 | std::vector<std::string> server_languages; |
| 614 | // A list of languages to fake being returned by the translate server. |
| 615 | server_languages.push_back("aa"); |
[email protected] | 462e909 | 2013-06-14 08:39:38 | [diff] [blame] | 616 | server_languages.push_back("ak"); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 617 | server_languages.push_back("ab"); |
| 618 | server_languages.push_back("en-CA"); |
[email protected] | 462e909 | 2013-06-14 08:39:38 | [diff] [blame] | 619 | server_languages.push_back("zh"); |
| 620 | server_languages.push_back("yi"); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 621 | server_languages.push_back("fr-FR"); |
[email protected] | 462e909 | 2013-06-14 08:39:38 | [diff] [blame] | 622 | server_languages.push_back("xx"); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 623 | |
[email protected] | 7da697a | 2013-07-12 08:09:46 | [diff] [blame] | 624 | std::vector<std::string> alpha_languages; |
| 625 | alpha_languages.push_back("aa"); |
| 626 | alpha_languages.push_back("yi"); |
| 627 | |
[email protected] | 296ff316 | 2013-06-20 18:14:23 | [diff] [blame] | 628 | // First, get the default languages list. Note that calling |
| 629 | // GetSupportedLanguages() invokes RequestLanguageList() internally. |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 630 | std::vector<std::string> default_supported_languages; |
| 631 | TranslateManager::GetSupportedLanguages(&default_supported_languages); |
| 632 | // To make sure we got the defaults and don't confuse them with the mocks. |
| 633 | ASSERT_NE(default_supported_languages.size(), server_languages.size()); |
| 634 | |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 635 | // Check that we still get the defaults until the URLFetch has completed. |
| 636 | std::vector<std::string> current_supported_languages; |
| 637 | TranslateManager::GetSupportedLanguages(¤t_supported_languages); |
| 638 | EXPECT_EQ(default_supported_languages, current_supported_languages); |
| 639 | |
| 640 | // Also check that it didn't change if we failed the URL fetch. |
[email protected] | 7da697a | 2013-07-12 08:09:46 | [diff] [blame] | 641 | SimulateSupportedLanguagesURLFetch(false, std::vector<std::string>(), |
| 642 | true, std::vector<std::string>()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 643 | current_supported_languages.clear(); |
| 644 | TranslateManager::GetSupportedLanguages(¤t_supported_languages); |
| 645 | EXPECT_EQ(default_supported_languages, current_supported_languages); |
| 646 | |
| 647 | // Now check that we got the appropriate set of languages from the server. |
[email protected] | 7da697a | 2013-07-12 08:09:46 | [diff] [blame] | 648 | SimulateSupportedLanguagesURLFetch(true, server_languages, |
| 649 | true, alpha_languages); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 650 | current_supported_languages.clear(); |
| 651 | TranslateManager::GetSupportedLanguages(¤t_supported_languages); |
[email protected] | 462e909 | 2013-06-14 08:39:38 | [diff] [blame] | 652 | // "xx" can't be displayed in the Translate inforbar, so this is eliminated. |
| 653 | EXPECT_EQ(server_languages.size() - 1, current_supported_languages.size()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 654 | // 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] | 655 | for (size_t i = 0; i < server_languages.size(); ++i) { |
[email protected] | 7da697a | 2013-07-12 08:09:46 | [diff] [blame] | 656 | const std::string& lang = server_languages[i]; |
| 657 | if (lang == "xx") |
[email protected] | 462e909 | 2013-06-14 08:39:38 | [diff] [blame] | 658 | continue; |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 659 | EXPECT_NE(current_supported_languages.end(), |
| 660 | std::find(current_supported_languages.begin(), |
| 661 | current_supported_languages.end(), |
[email protected] | 7da697a | 2013-07-12 08:09:46 | [diff] [blame] | 662 | lang)); |
| 663 | bool is_alpha = std::find(alpha_languages.begin(), |
| 664 | alpha_languages.end(), |
| 665 | lang) != alpha_languages.end(); |
| 666 | EXPECT_EQ(TranslateManager::IsAlphaLanguage(lang), is_alpha); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 667 | } |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 668 | } |
| 669 | |
[email protected] | 7da697a | 2013-07-12 08:09:46 | [diff] [blame] | 670 | // Test the fetching of languages from the translate server without 'al' |
| 671 | // parameter. |
| 672 | TEST_F(TranslateManagerBrowserTest, |
| 673 | FetchLanguagesFromTranslateServerWithoutAlpha) { |
| 674 | std::vector<std::string> server_languages; |
| 675 | server_languages.push_back("aa"); |
| 676 | server_languages.push_back("ak"); |
| 677 | server_languages.push_back("ab"); |
| 678 | server_languages.push_back("en-CA"); |
| 679 | server_languages.push_back("zh"); |
| 680 | server_languages.push_back("yi"); |
| 681 | server_languages.push_back("fr-FR"); |
| 682 | server_languages.push_back("xx"); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 683 | |
[email protected] | 7da697a | 2013-07-12 08:09:46 | [diff] [blame] | 684 | std::vector<std::string> alpha_languages; |
| 685 | alpha_languages.push_back("aa"); |
| 686 | alpha_languages.push_back("yi"); |
| 687 | |
| 688 | // call GetSupportedLanguages to call RequestLanguageList internally. |
| 689 | std::vector<std::string> default_supported_languages; |
| 690 | TranslateManager::GetSupportedLanguages(&default_supported_languages); |
| 691 | |
| 692 | SimulateSupportedLanguagesURLFetch(true, server_languages, |
| 693 | false, alpha_languages); |
| 694 | |
| 695 | std::vector<std::string> current_supported_languages; |
| 696 | TranslateManager::GetSupportedLanguages(¤t_supported_languages); |
| 697 | |
| 698 | // "xx" can't be displayed in the Translate inforbar, so this is eliminated. |
| 699 | EXPECT_EQ(server_languages.size() - 1, current_supported_languages.size()); |
| 700 | |
| 701 | for (size_t i = 0; i < server_languages.size(); ++i) { |
| 702 | const std::string& lang = server_languages[i]; |
| 703 | if (lang == "xx") |
| 704 | continue; |
| 705 | EXPECT_NE(current_supported_languages.end(), |
| 706 | std::find(current_supported_languages.begin(), |
| 707 | current_supported_languages.end(), |
| 708 | lang)); |
| 709 | EXPECT_FALSE(TranslateManager::IsAlphaLanguage(lang)); |
| 710 | } |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 711 | } |
| 712 | |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 713 | // Tests auto-translate on page. |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 714 | TEST_F(TranslateManagerBrowserTest, AutoTranslateOnNavigate) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 715 | SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 716 | |
| 717 | // Simulate the user translating. |
| 718 | TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 719 | ASSERT_TRUE(infobar != NULL); |
| 720 | infobar->Translate(); |
| 721 | // Simulate the translate script being retrieved. |
| 722 | SimulateTranslateScriptURLFetch(true); |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 723 | SimulateOnPageTranslated("fr", "en"); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 724 | |
| 725 | // Now navigate to a new page in the same language. |
| 726 | process()->sink().ClearMessages(); |
| 727 | SimulateNavigation(GURL("http://news.google.fr"), "fr", true); |
| 728 | |
| 729 | // This should have automatically triggered a translation. |
| 730 | int page_id = 0; |
| 731 | std::string original_lang, target_lang; |
| 732 | EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 733 | EXPECT_EQ(1, page_id); |
| 734 | EXPECT_EQ("fr", original_lang); |
| 735 | EXPECT_EQ("en", target_lang); |
| 736 | |
| 737 | // Now navigate to a page in a different language. |
| 738 | process()->sink().ClearMessages(); |
| 739 | SimulateNavigation(GURL("http://news.google.es"), "es", true); |
| 740 | |
| 741 | // This should not have triggered a translate. |
| 742 | EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 743 | } |
| 744 | |
| 745 | // Tests that multiple OnPageContents do not cause multiple infobars. |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 746 | TEST_F(TranslateManagerBrowserTest, MultipleOnPageContents) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 747 | SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 748 | |
| 749 | // Simulate clicking 'Nope' (don't translate). |
| 750 | EXPECT_TRUE(DenyTranslation()); |
[email protected] | a608d17 | 2013-04-23 22:27:37 | [diff] [blame] | 751 | EXPECT_EQ(0U, infobar_service()->infobar_count()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 752 | |
| 753 | // Send a new PageContents, we should not show an infobar. |
| 754 | SimulateOnTranslateLanguageDetermined("fr", true); |
[email protected] | a608d17 | 2013-04-23 22:27:37 | [diff] [blame] | 755 | EXPECT_EQ(0U, infobar_service()->infobar_count()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 756 | |
| 757 | // Do the same steps but simulate closing the infobar this time. |
| 758 | SimulateNavigation(GURL("http://www.youtube.fr"), "fr", true); |
| 759 | EXPECT_TRUE(CloseTranslateInfoBar()); |
[email protected] | a608d17 | 2013-04-23 22:27:37 | [diff] [blame] | 760 | EXPECT_EQ(0U, infobar_service()->infobar_count()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 761 | SimulateOnTranslateLanguageDetermined("fr", true); |
[email protected] | a608d17 | 2013-04-23 22:27:37 | [diff] [blame] | 762 | EXPECT_EQ(0U, infobar_service()->infobar_count()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 763 | } |
| 764 | |
[email protected] | b637bcd | 2013-03-28 12:50:32 | [diff] [blame] | 765 | // Test that reloading the page brings back the infobar if the |
| 766 | // reload succeeded and does not bring it back the reload fails. |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 767 | TEST_F(TranslateManagerBrowserTest, Reload) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 768 | SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 769 | |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 770 | EXPECT_TRUE(CloseTranslateInfoBar()); |
| 771 | |
[email protected] | b637bcd | 2013-03-28 12:50:32 | [diff] [blame] | 772 | // Reload should bring back the infobar if the page succeds |
| 773 | ReloadAndWait(true); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 774 | EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
[email protected] | b637bcd | 2013-03-28 12:50:32 | [diff] [blame] | 775 | |
[email protected] | b637bcd | 2013-03-28 12:50:32 | [diff] [blame] | 776 | EXPECT_TRUE(CloseTranslateInfoBar()); |
| 777 | |
| 778 | // And not show it if the reload fails |
| 779 | ReloadAndWait(false); |
| 780 | EXPECT_EQ(NULL, GetTranslateInfoBar()); |
[email protected] | 9ad06a1 | 2013-05-10 14:49:53 | [diff] [blame] | 781 | |
| 782 | // Set reload attempts to a high value, we will not see the infobar |
| 783 | // immediatly. |
| 784 | TranslateManager::GetInstance()->set_translate_max_reload_attemps(100); |
| 785 | ReloadAndWait(true); |
| 786 | EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | // Test that reloading the page by way of typing again the URL in the |
| 790 | // location bar brings back the infobar. |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 791 | TEST_F(TranslateManagerBrowserTest, ReloadFromLocationBar) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 792 | GURL url("http://www.google.fr"); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 793 | SimulateNavigation(url, "fr", true); |
| 794 | |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 795 | EXPECT_TRUE(CloseTranslateInfoBar()); |
| 796 | |
| 797 | // Create a pending navigation and simulate a page load. That should be the |
| 798 | // equivalent of typing the URL again in the location bar. |
[email protected] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 799 | NavEntryCommittedObserver nav_observer(web_contents()); |
| 800 | web_contents()->GetController().LoadURL(url, content::Referrer(), |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 801 | content::PAGE_TRANSITION_TYPED, |
| 802 | std::string()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 803 | rvh_tester()->SendNavigate(0, url); |
| 804 | |
| 805 | // Test that we are really getting a same page navigation, the test would be |
| 806 | // useless if it was not the case. |
| 807 | const content::LoadCommittedDetails& nav_details = |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 808 | nav_observer.load_committed_details(); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 809 | EXPECT_TRUE(nav_details.entry != NULL); // There was a navigation. |
| 810 | EXPECT_EQ(content::NAVIGATION_TYPE_SAME_PAGE, nav_details.type); |
| 811 | |
| 812 | // The TranslateManager class processes the navigation entry committed |
| 813 | // notification in a posted task; process that task. |
[email protected] | b3a2509 | 2013-05-28 22:08:16 | [diff] [blame] | 814 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 815 | EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 816 | } |
| 817 | |
| 818 | // Tests that a closed translate infobar does not reappear when navigating |
| 819 | // in-page. |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 820 | TEST_F(TranslateManagerBrowserTest, CloseInfoBarInPageNavigation) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 821 | SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 822 | |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 823 | EXPECT_TRUE(CloseTranslateInfoBar()); |
| 824 | |
| 825 | // Navigate in page, no infobar should be shown. |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 826 | SimulateNavigation(GURL("http://www.google.fr/#ref1"), "fr", true); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 827 | EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 828 | |
| 829 | // Navigate out of page, a new infobar should show. |
| 830 | SimulateNavigation(GURL("http://www.google.fr/foot"), "fr", true); |
| 831 | EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 832 | } |
| 833 | |
| 834 | // Tests that a closed translate infobar does not reappear when navigating |
| 835 | // in a subframe. (http://crbug.com/48215) |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 836 | TEST_F(TranslateManagerBrowserTest, CloseInfoBarInSubframeNavigation) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 837 | SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 838 | |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 839 | EXPECT_TRUE(CloseTranslateInfoBar()); |
| 840 | |
| 841 | // Simulate a sub-frame auto-navigating. |
| 842 | rvh_tester()->SendNavigateWithTransition( |
| 843 | 1, GURL("http://pub.com"), content::PAGE_TRANSITION_AUTO_SUBFRAME); |
| 844 | EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 845 | |
| 846 | // Simulate the user navigating in a sub-frame. |
| 847 | rvh_tester()->SendNavigateWithTransition( |
| 848 | 2, GURL("http://pub.com"), content::PAGE_TRANSITION_MANUAL_SUBFRAME); |
| 849 | EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 850 | |
| 851 | // Navigate out of page, a new infobar should show. |
| 852 | SimulateNavigation(GURL("http://www.google.fr/foot"), "fr", true); |
| 853 | EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 854 | } |
| 855 | |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 856 | // Tests that denying translation is sticky when navigating in page. |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 857 | TEST_F(TranslateManagerBrowserTest, DenyTranslateInPageNavigation) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 858 | SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 859 | |
| 860 | // Simulate clicking 'Nope' (don't translate). |
| 861 | EXPECT_TRUE(DenyTranslation()); |
| 862 | |
| 863 | // Navigate in page, no infobar should be shown. |
| 864 | SimulateNavigation(GURL("http://www.google.fr/#ref1"), "fr", true); |
| 865 | EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 866 | |
| 867 | // Navigate out of page, a new infobar should show. |
| 868 | SimulateNavigation(GURL("http://www.google.fr/foot"), "fr", true); |
| 869 | EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 870 | } |
| 871 | |
| 872 | // Tests that after translating and closing the infobar, the infobar does not |
| 873 | // return when navigating in page. |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 874 | TEST_F(TranslateManagerBrowserTest, TranslateCloseInfoBarInPageNavigation) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 875 | SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 876 | |
| 877 | // Simulate the user translating. |
| 878 | TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 879 | ASSERT_TRUE(infobar != NULL); |
| 880 | infobar->Translate(); |
| 881 | // Simulate the translate script being retrieved. |
| 882 | SimulateTranslateScriptURLFetch(true); |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 883 | SimulateOnPageTranslated("fr", "en"); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 884 | |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 885 | EXPECT_TRUE(CloseTranslateInfoBar()); |
| 886 | |
| 887 | // Navigate in page, no infobar should be shown. |
| 888 | SimulateNavigation(GURL("http://www.google.fr/#ref1"), "fr", true); |
| 889 | EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 890 | |
| 891 | // Navigate out of page, a new infobar should show. |
| 892 | // Note that we navigate to a page in a different language so we don't trigger |
| 893 | // the auto-translate feature (it would translate the page automatically and |
| 894 | // the before translate inforbar would not be shown). |
| 895 | SimulateNavigation(GURL("http://www.google.de"), "de", true); |
| 896 | EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 897 | } |
| 898 | |
| 899 | // Tests that the after translate the infobar still shows when navigating |
| 900 | // in-page. |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 901 | TEST_F(TranslateManagerBrowserTest, TranslateInPageNavigation) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 902 | SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 903 | |
| 904 | // Simulate the user translating. |
| 905 | TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 906 | ASSERT_TRUE(infobar != NULL); |
| 907 | infobar->Translate(); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 908 | SimulateTranslateScriptURLFetch(true); |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 909 | SimulateOnPageTranslated("fr", "en"); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 910 | // The after translate infobar is showing. |
| 911 | infobar = GetTranslateInfoBar(); |
| 912 | ASSERT_TRUE(infobar != NULL); |
| 913 | |
| 914 | // Navigate out of page, a new infobar should show. |
| 915 | // See note in TranslateCloseInfoBarInPageNavigation test on why it is |
| 916 | // important to navigate to a page in a different language for this test. |
| 917 | SimulateNavigation(GURL("http://www.google.de"), "de", true); |
| 918 | // The old infobar is gone. |
| 919 | EXPECT_TRUE(CheckInfoBarRemovedAndReset(infobar)); |
| 920 | // And there is a new one. |
| 921 | EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 922 | } |
| 923 | |
| 924 | // Tests that no translate infobar is shown when navigating to a page in an |
| 925 | // unsupported language. |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 926 | TEST_F(TranslateManagerBrowserTest, CLDReportsUnsupportedPageLanguage) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 927 | // Simulate navigating to a page and getting an unsupported language. |
| 928 | SimulateNavigation(GURL("http://www.google.com"), "qbz", true); |
| 929 | |
| 930 | // No info-bar should be shown. |
| 931 | EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 932 | } |
| 933 | |
| 934 | // Tests that we deal correctly with unsupported languages returned by the |
| 935 | // server. |
| 936 | // The translation server might return a language we don't support. |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 937 | TEST_F(TranslateManagerBrowserTest, ServerReportsUnsupportedLanguage) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 938 | SimulateNavigation(GURL("http://mail.google.fr"), "fr", true); |
| 939 | TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 940 | ASSERT_TRUE(infobar != NULL); |
| 941 | process()->sink().ClearMessages(); |
| 942 | infobar->Translate(); |
| 943 | SimulateTranslateScriptURLFetch(true); |
| 944 | // Simulate the render notifying the translation has been done, but it |
| 945 | // reports a language we don't support. |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 946 | SimulateOnPageTranslated("qbz", "en"); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 947 | |
| 948 | // An error infobar should be showing to report that we don't support this |
| 949 | // language. |
| 950 | infobar = GetTranslateInfoBar(); |
| 951 | ASSERT_TRUE(infobar != NULL); |
[email protected] | 2ebd22d | 2013-01-07 17:04:27 | [diff] [blame] | 952 | EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATION_ERROR, |
| 953 | infobar->infobar_type()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 954 | |
| 955 | // This infobar should have a button (so the string should not be empty). |
| 956 | ASSERT_FALSE(infobar->GetMessageInfoBarButtonText().empty()); |
| 957 | |
| 958 | // Pressing the button on that infobar should revert to the original language. |
| 959 | process()->sink().ClearMessages(); |
| 960 | infobar->MessageInfoBarButtonPressed(); |
| 961 | const IPC::Message* message = |
| 962 | process()->sink().GetFirstMessageMatching( |
| 963 | ChromeViewMsg_RevertTranslation::ID); |
| 964 | EXPECT_TRUE(message != NULL); |
| 965 | // And it should have removed the infobar. |
| 966 | EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 967 | } |
| 968 | |
| 969 | // Tests that no translate infobar is shown and context menu is disabled, when |
| 970 | // Chrome is in a language that the translate server does not support. |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 971 | TEST_F(TranslateManagerBrowserTest, UnsupportedUILanguage) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 972 | std::string original_lang = g_browser_process->GetApplicationLocale(); |
| 973 | g_browser_process->SetApplicationLocale("qbz"); |
| 974 | |
| 975 | // Make sure that the accept language list only contains unsupported languages |
| 976 | Profile* profile = |
[email protected] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 977 | Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 978 | PrefService* prefs = profile->GetPrefs(); |
| 979 | prefs->SetString(prefs::kAcceptLanguages, "qbz"); |
| 980 | |
| 981 | // Simulate navigating to a page in a language supported by the translate |
| 982 | // server. |
| 983 | SimulateNavigation(GURL("http://www.google.com"), "en", true); |
| 984 | |
| 985 | // No info-bar should be shown. |
| 986 | EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 987 | |
| 988 | // And the context menu option should be disabled too. |
| 989 | scoped_ptr<TestRenderViewContextMenu> menu( |
[email protected] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 990 | TestRenderViewContextMenu::CreateContextMenu(web_contents())); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 991 | menu->Init(); |
| 992 | EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 993 | EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 994 | |
| 995 | g_browser_process->SetApplicationLocale(original_lang); |
| 996 | } |
| 997 | |
| 998 | // Tests that the first supported accept language is selected |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 999 | TEST_F(TranslateManagerBrowserTest, TranslateAcceptLanguage) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1000 | // Set locate to non-existant language |
| 1001 | std::string original_lang = g_browser_process->GetApplicationLocale(); |
| 1002 | g_browser_process->SetApplicationLocale("qbz"); |
| 1003 | |
| 1004 | // Set Qbz and French as the only accepted languages |
| 1005 | Profile* profile = |
[email protected] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 1006 | Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1007 | PrefService* prefs = profile->GetPrefs(); |
| 1008 | prefs->SetString(prefs::kAcceptLanguages, "qbz,fr"); |
| 1009 | |
| 1010 | // Go to a German page |
| 1011 | SimulateNavigation(GURL("http://google.de"), "de", true); |
| 1012 | |
| 1013 | // Expect the infobar to pop up |
| 1014 | EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 1015 | |
| 1016 | // Set Qbz and English-US as the only accepted languages to test the country |
| 1017 | // code removal code which was causing a crash as filed in Issue 90106, |
| 1018 | // a crash caused by a language with a country code that wasn't recognized. |
| 1019 | prefs->SetString(prefs::kAcceptLanguages, "qbz,en-us"); |
| 1020 | |
| 1021 | // Go to a German page |
| 1022 | SimulateNavigation(GURL("http://google.de"), "de", true); |
| 1023 | |
| 1024 | // Expect the infobar to pop up |
| 1025 | EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 1026 | } |
| 1027 | |
| 1028 | // Tests that the translate enabled preference is honored. |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 1029 | TEST_F(TranslateManagerBrowserTest, TranslateEnabledPref) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1030 | // Make sure the pref allows translate. |
| 1031 | Profile* profile = |
[email protected] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 1032 | Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1033 | PrefService* prefs = profile->GetPrefs(); |
| 1034 | prefs->SetBoolean(prefs::kEnableTranslate, true); |
| 1035 | |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1036 | SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 1037 | |
| 1038 | // An infobar should be shown. |
| 1039 | TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 1040 | EXPECT_TRUE(infobar != NULL); |
| 1041 | |
| 1042 | // Disable translate. |
| 1043 | prefs->SetBoolean(prefs::kEnableTranslate, false); |
| 1044 | |
| 1045 | // Navigate to a new page, that should close the previous infobar. |
| 1046 | GURL url("http://www.youtube.fr"); |
| 1047 | NavigateAndCommit(url); |
| 1048 | infobar = GetTranslateInfoBar(); |
| 1049 | EXPECT_TRUE(infobar == NULL); |
| 1050 | |
| 1051 | // Simulate getting the page contents and language, that should not trigger |
| 1052 | // a translate infobar. |
| 1053 | SimulateOnTranslateLanguageDetermined("fr", true); |
| 1054 | infobar = GetTranslateInfoBar(); |
| 1055 | EXPECT_TRUE(infobar == NULL); |
| 1056 | } |
| 1057 | |
| 1058 | // Tests the "Never translate <language>" pref. |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 1059 | TEST_F(TranslateManagerBrowserTest, NeverTranslateLanguagePref) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1060 | GURL url("http://www.google.fr"); |
| 1061 | SimulateNavigation(url, "fr", true); |
| 1062 | |
| 1063 | // An infobar should be shown. |
| 1064 | EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 1065 | |
| 1066 | // Select never translate this language. |
| 1067 | Profile* profile = |
[email protected] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 1068 | Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1069 | PrefService* prefs = profile->GetPrefs(); |
| 1070 | PrefChangeRegistrar registrar; |
| 1071 | registrar.Init(prefs); |
[email protected] | 032ae93a | 2013-08-30 09:15:03 | [diff] [blame] | 1072 | registrar.Add(TranslatePrefs::kPrefTranslateBlockedLanguages, |
[email protected] | 116129e0 | 2012-11-21 17:26:27 | [diff] [blame] | 1073 | pref_callback_); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1074 | TranslatePrefs translate_prefs(prefs); |
[email protected] | 5d37b2298 | 2013-06-25 06:07:00 | [diff] [blame] | 1075 | EXPECT_FALSE(translate_prefs.IsBlockedLanguage("fr")); |
[email protected] | f9db7e0 | 2013-06-17 10:29:43 | [diff] [blame] | 1076 | EXPECT_TRUE(translate_prefs.CanTranslateLanguage(profile, "fr")); |
[email protected] | 032ae93a | 2013-08-30 09:15:03 | [diff] [blame] | 1077 | SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateBlockedLanguages); |
[email protected] | 5d37b2298 | 2013-06-25 06:07:00 | [diff] [blame] | 1078 | translate_prefs.BlockLanguage("fr"); |
| 1079 | EXPECT_TRUE(translate_prefs.IsBlockedLanguage("fr")); |
[email protected] | f9db7e0 | 2013-06-17 10:29:43 | [diff] [blame] | 1080 | EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(url.host())); |
| 1081 | EXPECT_FALSE(translate_prefs.CanTranslateLanguage(profile, "fr")); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1082 | |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1083 | EXPECT_TRUE(CloseTranslateInfoBar()); |
| 1084 | |
| 1085 | // Navigate to a new page also in French. |
| 1086 | SimulateNavigation(GURL("http://wwww.youtube.fr"), "fr", true); |
| 1087 | |
| 1088 | // There should not be a translate infobar. |
| 1089 | EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 1090 | |
| 1091 | // Remove the language from the blacklist. |
[email protected] | 032ae93a | 2013-08-30 09:15:03 | [diff] [blame] | 1092 | SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateBlockedLanguages); |
[email protected] | 5d37b2298 | 2013-06-25 06:07:00 | [diff] [blame] | 1093 | translate_prefs.UnblockLanguage("fr"); |
| 1094 | EXPECT_FALSE(translate_prefs.IsBlockedLanguage("fr")); |
[email protected] | f9db7e0 | 2013-06-17 10:29:43 | [diff] [blame] | 1095 | EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(url.host())); |
| 1096 | EXPECT_TRUE(translate_prefs.CanTranslateLanguage(profile, "fr")); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1097 | |
| 1098 | // Navigate to a page in French. |
| 1099 | SimulateNavigation(url, "fr", true); |
| 1100 | |
| 1101 | // There should be a translate infobar. |
| 1102 | EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 1103 | } |
| 1104 | |
| 1105 | // Tests the "Never translate this site" pref. |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 1106 | TEST_F(TranslateManagerBrowserTest, NeverTranslateSitePref) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1107 | GURL url("http://www.google.fr"); |
| 1108 | std::string host(url.host()); |
| 1109 | SimulateNavigation(url, "fr", true); |
| 1110 | |
| 1111 | // An infobar should be shown. |
| 1112 | EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 1113 | |
| 1114 | // Select never translate this site. |
| 1115 | Profile* profile = |
[email protected] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 1116 | Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1117 | PrefService* prefs = profile->GetPrefs(); |
| 1118 | PrefChangeRegistrar registrar; |
| 1119 | registrar.Init(prefs); |
[email protected] | 116129e0 | 2012-11-21 17:26:27 | [diff] [blame] | 1120 | registrar.Add(TranslatePrefs::kPrefTranslateSiteBlacklist, pref_callback_); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1121 | TranslatePrefs translate_prefs(prefs); |
| 1122 | EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(host)); |
[email protected] | f9db7e0 | 2013-06-17 10:29:43 | [diff] [blame] | 1123 | EXPECT_TRUE(translate_prefs.CanTranslateLanguage(profile, "fr")); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1124 | SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateSiteBlacklist); |
| 1125 | translate_prefs.BlacklistSite(host); |
| 1126 | EXPECT_TRUE(translate_prefs.IsSiteBlacklisted(host)); |
[email protected] | f9db7e0 | 2013-06-17 10:29:43 | [diff] [blame] | 1127 | EXPECT_TRUE(translate_prefs.CanTranslateLanguage(profile, "fr")); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1128 | |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1129 | EXPECT_TRUE(CloseTranslateInfoBar()); |
| 1130 | |
| 1131 | // Navigate to a new page also on the same site. |
| 1132 | SimulateNavigation(GURL("http://www.google.fr/hello"), "fr", true); |
| 1133 | |
| 1134 | // There should not be a translate infobar. |
| 1135 | EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 1136 | |
| 1137 | // Remove the site from the blacklist. |
| 1138 | SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateSiteBlacklist); |
| 1139 | translate_prefs.RemoveSiteFromBlacklist(host); |
| 1140 | EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(host)); |
[email protected] | f9db7e0 | 2013-06-17 10:29:43 | [diff] [blame] | 1141 | EXPECT_TRUE(translate_prefs.CanTranslateLanguage(profile, "fr")); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1142 | |
| 1143 | // Navigate to a page in French. |
| 1144 | SimulateNavigation(url, "fr", true); |
| 1145 | |
| 1146 | // There should be a translate infobar. |
| 1147 | EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 1148 | } |
| 1149 | |
| 1150 | // Tests the "Always translate this language" pref. |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 1151 | TEST_F(TranslateManagerBrowserTest, AlwaysTranslateLanguagePref) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1152 | // Select always translate French to English. |
| 1153 | Profile* profile = |
[email protected] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 1154 | Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1155 | PrefService* prefs = profile->GetPrefs(); |
| 1156 | PrefChangeRegistrar registrar; |
| 1157 | registrar.Init(prefs); |
[email protected] | 116129e0 | 2012-11-21 17:26:27 | [diff] [blame] | 1158 | registrar.Add(TranslatePrefs::kPrefTranslateWhitelists, pref_callback_); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1159 | TranslatePrefs translate_prefs(prefs); |
| 1160 | SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateWhitelists); |
| 1161 | translate_prefs.WhitelistLanguagePair("fr", "en"); |
| 1162 | |
| 1163 | // Load a page in French. |
| 1164 | SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 1165 | |
| 1166 | // It should have triggered an automatic translation to English. |
| 1167 | |
| 1168 | // The translating infobar should be showing. |
| 1169 | TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 1170 | ASSERT_TRUE(infobar != NULL); |
[email protected] | 2ebd22d | 2013-01-07 17:04:27 | [diff] [blame] | 1171 | EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATING, infobar->infobar_type()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1172 | SimulateTranslateScriptURLFetch(true); |
| 1173 | int page_id = 0; |
| 1174 | std::string original_lang, target_lang; |
| 1175 | EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 1176 | EXPECT_EQ("fr", original_lang); |
| 1177 | EXPECT_EQ("en", target_lang); |
| 1178 | process()->sink().ClearMessages(); |
| 1179 | |
| 1180 | // Try another language, it should not be autotranslated. |
| 1181 | SimulateNavigation(GURL("http://www.google.es"), "es", true); |
| 1182 | EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 1183 | EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 1184 | EXPECT_TRUE(CloseTranslateInfoBar()); |
| 1185 | |
| 1186 | // Let's switch to incognito mode, it should not be autotranslated in that |
| 1187 | // case either. |
| 1188 | TestingProfile* test_profile = |
[email protected] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 1189 | static_cast<TestingProfile*>(web_contents()->GetBrowserContext()); |
[email protected] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 1190 | test_profile->ForceIncognito(true); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1191 | SimulateNavigation(GURL("http://www.youtube.fr"), "fr", true); |
| 1192 | EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 1193 | EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
| 1194 | EXPECT_TRUE(CloseTranslateInfoBar()); |
[email protected] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 1195 | test_profile->ForceIncognito(false); // Get back to non incognito. |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1196 | |
| 1197 | // Now revert the always translate pref and make sure we go back to expected |
| 1198 | // behavior, which is show a "before translate" infobar. |
| 1199 | SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateWhitelists); |
| 1200 | translate_prefs.RemoveLanguagePairFromWhitelist("fr", "en"); |
| 1201 | SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 1202 | EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 1203 | infobar = GetTranslateInfoBar(); |
| 1204 | ASSERT_TRUE(infobar != NULL); |
[email protected] | 2ebd22d | 2013-01-07 17:04:27 | [diff] [blame] | 1205 | EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE, |
| 1206 | infobar->infobar_type()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | // Context menu. |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 1210 | TEST_F(TranslateManagerBrowserTest, ContextMenu) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1211 | // Blacklist www.google.fr and French for translation. |
| 1212 | GURL url("http://www.google.fr"); |
| 1213 | Profile* profile = |
[email protected] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 1214 | Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1215 | TranslatePrefs translate_prefs(profile->GetPrefs()); |
[email protected] | 5d37b2298 | 2013-06-25 06:07:00 | [diff] [blame] | 1216 | translate_prefs.BlockLanguage("fr"); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1217 | translate_prefs.BlacklistSite(url.host()); |
[email protected] | 5d37b2298 | 2013-06-25 06:07:00 | [diff] [blame] | 1218 | EXPECT_TRUE(translate_prefs.IsBlockedLanguage("fr")); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1219 | EXPECT_TRUE(translate_prefs.IsSiteBlacklisted(url.host())); |
| 1220 | |
| 1221 | // Simulate navigating to a page in French. The translate menu should show but |
| 1222 | // should only be enabled when the page language has been received. |
| 1223 | NavigateAndCommit(url); |
| 1224 | scoped_ptr<TestRenderViewContextMenu> menu( |
[email protected] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 1225 | TestRenderViewContextMenu::CreateContextMenu(web_contents())); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1226 | menu->Init(); |
| 1227 | EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 1228 | EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 1229 | |
| 1230 | // Simulate receiving the language. |
| 1231 | SimulateOnTranslateLanguageDetermined("fr", true); |
[email protected] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 1232 | menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents())); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1233 | menu->Init(); |
| 1234 | EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 1235 | EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 1236 | |
| 1237 | // Use the menu to translate the page. |
[email protected] | 96bf34e | 2013-03-13 04:22:14 | [diff] [blame] | 1238 | menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1239 | |
| 1240 | // That should have triggered a translation. |
| 1241 | // The "translating..." infobar should be showing. |
| 1242 | TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 1243 | ASSERT_TRUE(infobar != NULL); |
[email protected] | 2ebd22d | 2013-01-07 17:04:27 | [diff] [blame] | 1244 | EXPECT_EQ(TranslateInfoBarDelegate::TRANSLATING, infobar->infobar_type()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1245 | SimulateTranslateScriptURLFetch(true); |
| 1246 | int page_id = 0; |
| 1247 | std::string original_lang, target_lang; |
| 1248 | EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 1249 | EXPECT_EQ("fr", original_lang); |
| 1250 | EXPECT_EQ("en", target_lang); |
| 1251 | process()->sink().ClearMessages(); |
| 1252 | |
| 1253 | // This should also have reverted the blacklisting of this site and language. |
[email protected] | 5d37b2298 | 2013-06-25 06:07:00 | [diff] [blame] | 1254 | EXPECT_FALSE(translate_prefs.IsBlockedLanguage("fr")); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1255 | EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(url.host())); |
| 1256 | |
| 1257 | // Let's simulate the page being translated. |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 1258 | SimulateOnPageTranslated("fr", "en"); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1259 | |
| 1260 | // The translate menu should now be disabled. |
[email protected] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 1261 | menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents())); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1262 | menu->Init(); |
| 1263 | EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 1264 | EXPECT_FALSE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 1265 | |
| 1266 | // Test that selecting translate in the context menu WHILE the page is being |
| 1267 | // translated does nothing (this could happen if autotranslate kicks-in and |
| 1268 | // the user selects the menu while the translation is being performed). |
| 1269 | SimulateNavigation(GURL("http://www.google.es"), "es", true); |
| 1270 | infobar = GetTranslateInfoBar(); |
| 1271 | ASSERT_TRUE(infobar != NULL); |
| 1272 | infobar->Translate(); |
| 1273 | EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 1274 | process()->sink().ClearMessages(); |
[email protected] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 1275 | menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents())); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1276 | menu->Init(); |
| 1277 | EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); |
[email protected] | 96bf34e | 2013-03-13 04:22:14 | [diff] [blame] | 1278 | menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1279 | // No message expected since the translation should have been ignored. |
| 1280 | EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 1281 | |
| 1282 | // Now test that selecting translate in the context menu AFTER the page has |
| 1283 | // been translated does nothing. |
| 1284 | SimulateNavigation(GURL("http://www.google.de"), "de", true); |
| 1285 | infobar = GetTranslateInfoBar(); |
| 1286 | ASSERT_TRUE(infobar != NULL); |
| 1287 | infobar->Translate(); |
| 1288 | EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 1289 | process()->sink().ClearMessages(); |
[email protected] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 1290 | menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents())); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1291 | menu->Init(); |
| 1292 | EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 1293 | SimulateOnPageTranslated("de", "en"); |
[email protected] | 96bf34e | 2013-03-13 04:22:14 | [diff] [blame] | 1294 | menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1295 | // No message expected since the translation should have been ignored. |
| 1296 | EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 1297 | |
| 1298 | // Test that the translate context menu is enabled when the page is in an |
| 1299 | // unknown language. |
| 1300 | SimulateNavigation(url, "und", true); |
[email protected] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 1301 | menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents())); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1302 | menu->Init(); |
| 1303 | EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 1304 | EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); |
| 1305 | |
[email protected] | 8e7d0b47 | 2013-05-23 05:41:37 | [diff] [blame] | 1306 | // 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] | 1307 | // unsupported language. |
| 1308 | SimulateNavigation(url, "qbz", true); |
[email protected] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 1309 | menu.reset(TestRenderViewContextMenu::CreateContextMenu(web_contents())); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1310 | menu->Init(); |
| 1311 | EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); |
[email protected] | 8e7d0b47 | 2013-05-23 05:41:37 | [diff] [blame] | 1312 | EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1313 | } |
| 1314 | |
| 1315 | // Tests that an extra always/never translate button is shown on the "before |
| 1316 | // translate" infobar when the translation is accepted/declined 3 times, |
| 1317 | // only when not in incognito mode. |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 1318 | TEST_F(TranslateManagerBrowserTest, BeforeTranslateExtraButtons) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1319 | Profile* profile = |
[email protected] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 1320 | Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1321 | TranslatePrefs translate_prefs(profile->GetPrefs()); |
| 1322 | translate_prefs.ResetTranslationAcceptedCount("fr"); |
| 1323 | translate_prefs.ResetTranslationDeniedCount("fr"); |
| 1324 | translate_prefs.ResetTranslationAcceptedCount("de"); |
| 1325 | translate_prefs.ResetTranslationDeniedCount("de"); |
| 1326 | |
| 1327 | // We'll do 4 times in incognito mode first to make sure the button is not |
| 1328 | // shown in that case, then 4 times in normal mode. |
| 1329 | TranslateInfoBarDelegate* infobar; |
| 1330 | TestingProfile* test_profile = |
[email protected] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 1331 | static_cast<TestingProfile*>(web_contents()->GetBrowserContext()); |
[email protected] | bd30672 | 2012-07-11 20:43:59 | [diff] [blame] | 1332 | static_cast<extensions::TestExtensionSystem*>( |
| 1333 | extensions::ExtensionSystem::Get(test_profile))-> |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1334 | CreateExtensionProcessManager(); |
[email protected] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 1335 | test_profile->ForceIncognito(true); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1336 | for (int i = 0; i < 8; ++i) { |
| 1337 | SCOPED_TRACE(::testing::Message() << "Iteration " << i << |
| 1338 | " incognito mode=" << test_profile->IsOffTheRecord()); |
| 1339 | SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 1340 | infobar = GetTranslateInfoBar(); |
| 1341 | ASSERT_TRUE(infobar != NULL); |
[email protected] | 2ebd22d | 2013-01-07 17:04:27 | [diff] [blame] | 1342 | EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE, |
| 1343 | infobar->infobar_type()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1344 | if (i < 7) { |
[email protected] | f1717965 | 2013-04-25 18:11:10 | [diff] [blame] | 1345 | EXPECT_FALSE(infobar->ShouldShowAlwaysTranslateShortcut()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1346 | infobar->Translate(); |
| 1347 | process()->sink().ClearMessages(); |
| 1348 | } else { |
[email protected] | f1717965 | 2013-04-25 18:11:10 | [diff] [blame] | 1349 | EXPECT_TRUE(infobar->ShouldShowAlwaysTranslateShortcut()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1350 | } |
| 1351 | if (i == 3) |
[email protected] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 1352 | test_profile->ForceIncognito(false); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1353 | } |
| 1354 | // Simulate the user pressing "Always translate French". |
| 1355 | infobar->AlwaysTranslatePageLanguage(); |
| 1356 | EXPECT_TRUE(translate_prefs.IsLanguagePairWhitelisted("fr", "en")); |
| 1357 | // Simulate the translate script being retrieved (it only needs to be done |
| 1358 | // once in the test as it is cached). |
| 1359 | SimulateTranslateScriptURLFetch(true); |
| 1360 | // That should have triggered a page translate. |
| 1361 | int page_id = 0; |
| 1362 | std::string original_lang, target_lang; |
| 1363 | EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 1364 | process()->sink().ClearMessages(); |
| 1365 | |
| 1366 | // Now test that declining the translation causes a "never translate" button |
| 1367 | // to be shown (in non incognito mode only). |
[email protected] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 1368 | test_profile->ForceIncognito(true); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1369 | for (int i = 0; i < 8; ++i) { |
| 1370 | SCOPED_TRACE(::testing::Message() << "Iteration " << i << |
| 1371 | " incognito mode=" << test_profile->IsOffTheRecord()); |
| 1372 | SimulateNavigation(GURL("http://www.google.de"), "de", true); |
| 1373 | infobar = GetTranslateInfoBar(); |
| 1374 | ASSERT_TRUE(infobar != NULL); |
[email protected] | 2ebd22d | 2013-01-07 17:04:27 | [diff] [blame] | 1375 | EXPECT_EQ(TranslateInfoBarDelegate::BEFORE_TRANSLATE, |
| 1376 | infobar->infobar_type()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1377 | if (i < 7) { |
[email protected] | f1717965 | 2013-04-25 18:11:10 | [diff] [blame] | 1378 | EXPECT_FALSE(infobar->ShouldShowNeverTranslateShortcut()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1379 | infobar->TranslationDeclined(); |
| 1380 | } else { |
[email protected] | f1717965 | 2013-04-25 18:11:10 | [diff] [blame] | 1381 | EXPECT_TRUE(infobar->ShouldShowNeverTranslateShortcut()); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1382 | } |
| 1383 | if (i == 3) |
[email protected] | 9819fd0 | 2013-08-22 10:49:39 | [diff] [blame] | 1384 | test_profile->ForceIncognito(false); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1385 | } |
| 1386 | // Simulate the user pressing "Never translate French". |
| 1387 | infobar->NeverTranslatePageLanguage(); |
[email protected] | 5d37b2298 | 2013-06-25 06:07:00 | [diff] [blame] | 1388 | EXPECT_TRUE(translate_prefs.IsBlockedLanguage("de")); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1389 | // No translation should have occured and the infobar should be gone. |
| 1390 | EXPECT_FALSE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 1391 | process()->sink().ClearMessages(); |
| 1392 | ASSERT_TRUE(GetTranslateInfoBar() == NULL); |
| 1393 | } |
| 1394 | |
| 1395 | // Tests that we don't show a translate infobar when a page instructs that it |
| 1396 | // should not be translated. |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 1397 | TEST_F(TranslateManagerBrowserTest, NonTranslatablePage) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1398 | SimulateNavigation(GURL("http://mail.google.fr"), "fr", false); |
| 1399 | |
| 1400 | // We should not have an infobar. |
| 1401 | EXPECT_TRUE(GetTranslateInfoBar() == NULL); |
| 1402 | |
[email protected] | 54e4b52 | 2013-05-21 08:36:23 | [diff] [blame] | 1403 | // The context menu is enabled to allow users to force translation. |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1404 | scoped_ptr<TestRenderViewContextMenu> menu( |
[email protected] | 921441c | 2012-10-19 22:16:01 | [diff] [blame] | 1405 | TestRenderViewContextMenu::CreateContextMenu(web_contents())); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1406 | menu->Init(); |
| 1407 | EXPECT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_TRANSLATE)); |
[email protected] | 54e4b52 | 2013-05-21 08:36:23 | [diff] [blame] | 1408 | EXPECT_TRUE(menu->IsCommandIdEnabled(IDC_CONTENT_CONTEXT_TRANSLATE)); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1409 | } |
| 1410 | |
| 1411 | // Tests that the script is expired and refetched as expected. |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 1412 | TEST_F(TranslateManagerBrowserTest, ScriptExpires) { |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1413 | ExpireTranslateScriptImmediately(); |
| 1414 | |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1415 | SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 1416 | TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 1417 | ASSERT_TRUE(infobar != NULL); |
| 1418 | process()->sink().ClearMessages(); |
| 1419 | infobar->Translate(); |
| 1420 | SimulateTranslateScriptURLFetch(true); |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 1421 | SimulateOnPageTranslated("fr", "en"); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1422 | |
| 1423 | // A task should have been posted to clear the script, run it. |
[email protected] | b3a2509 | 2013-05-28 22:08:16 | [diff] [blame] | 1424 | base::MessageLoop::current()->RunUntilIdle(); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1425 | |
| 1426 | // Do another navigation and translation. |
| 1427 | SimulateNavigation(GURL("http://www.google.es"), "es", true); |
| 1428 | infobar = GetTranslateInfoBar(); |
| 1429 | ASSERT_TRUE(infobar != NULL); |
| 1430 | process()->sink().ClearMessages(); |
| 1431 | infobar->Translate(); |
| 1432 | // If we don't simulate the URL fetch, the TranslateManager should be waiting |
| 1433 | // for the script and no message should have been sent to the renderer. |
| 1434 | EXPECT_TRUE( |
| 1435 | process()->sink().GetFirstMessageMatching( |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 1436 | ChromeViewMsg_TranslatePage::ID) == NULL); |
[email protected] | 2253b3b | 2012-06-03 22:39:15 | [diff] [blame] | 1437 | // Now simulate the URL fetch. |
| 1438 | SimulateTranslateScriptURLFetch(true); |
| 1439 | // Now the message should have been sent. |
| 1440 | int page_id = 0; |
| 1441 | std::string original_lang, target_lang; |
| 1442 | EXPECT_TRUE(GetTranslateMessage(&page_id, &original_lang, &target_lang)); |
| 1443 | EXPECT_EQ("es", original_lang); |
| 1444 | EXPECT_EQ("en", target_lang); |
| 1445 | } |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 1446 | |
[email protected] | 6775567 | 2013-04-08 23:25:32 | [diff] [blame] | 1447 | TEST_F(TranslateManagerBrowserTest, DownloadsAndHistoryNotTranslated) { |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 1448 | ASSERT_FALSE(TranslateManager::IsTranslatableURL( |
| 1449 | GURL(chrome::kChromeUIDownloadsURL))); |
| 1450 | ASSERT_FALSE(TranslateManager::IsTranslatableURL( |
| 1451 | GURL(chrome::kChromeUIHistoryURL))); |
| 1452 | } |
| 1453 | |
[email protected] | e625b760 | 2013-10-28 09:24:56 | [diff] [blame^] | 1454 | #if !defined(OS_ANDROID) && !defined(OS_IOS) |
| 1455 | |
| 1456 | TEST_F(TranslateManagerBrowserTest, BubbleNormalTranslate) { |
| 1457 | // Prepare for the bubble |
| 1458 | CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 1459 | command_line->AppendSwitch(switches::kEnableTranslateNewUX); |
| 1460 | MockTranslateBubbleFactory* factory = new MockTranslateBubbleFactory; |
| 1461 | scoped_ptr<TranslateBubbleFactory> factory_ptr(factory); |
| 1462 | TranslateBubbleFactory::SetFactory(factory); |
| 1463 | |
| 1464 | SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 1465 | |
| 1466 | // Check the bubble exists instead of the infobar. |
| 1467 | TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 1468 | ASSERT_TRUE(infobar == NULL); |
| 1469 | TranslateBubbleModel* bubble = factory->model(); |
| 1470 | ASSERT_TRUE(bubble != NULL); |
| 1471 | EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE, |
| 1472 | bubble->GetViewState()); |
| 1473 | |
| 1474 | // Simulate clicking translate. |
| 1475 | process()->sink().ClearMessages(); |
| 1476 | bubble->Translate(); |
| 1477 | |
| 1478 | // Check the bubble shows "Translating...". |
| 1479 | bubble = factory->model(); |
| 1480 | ASSERT_TRUE(bubble != NULL); |
| 1481 | EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_TRANSLATING, |
| 1482 | bubble->GetViewState()); |
| 1483 | |
| 1484 | // Simulate the translate script being retrieved (it only needs to be done |
| 1485 | // once in the test as it is cached). |
| 1486 | SimulateTranslateScriptURLFetch(true); |
| 1487 | |
| 1488 | // Simulate the render notifying the translation has been done. |
| 1489 | SimulateOnPageTranslated("fr", "en"); |
| 1490 | |
| 1491 | // Check the bubble shows "Translated." |
| 1492 | bubble = factory->model(); |
| 1493 | ASSERT_TRUE(bubble != NULL); |
| 1494 | EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_AFTER_TRANSLATE, |
| 1495 | bubble->GetViewState()); |
| 1496 | } |
| 1497 | |
| 1498 | TEST_F(TranslateManagerBrowserTest, BubbleTranslateScriptNotAvailable) { |
| 1499 | // Prepare for the bubble |
| 1500 | CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 1501 | command_line->AppendSwitch(switches::kEnableTranslateNewUX); |
| 1502 | MockTranslateBubbleFactory* factory = new MockTranslateBubbleFactory; |
| 1503 | scoped_ptr<TranslateBubbleFactory> factory_ptr(factory); |
| 1504 | TranslateBubbleFactory::SetFactory(factory); |
| 1505 | |
| 1506 | SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
| 1507 | |
| 1508 | // Check the bubble exists instead of the infobar. |
| 1509 | TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 1510 | ASSERT_TRUE(infobar == NULL); |
| 1511 | TranslateBubbleModel* bubble = factory->model(); |
| 1512 | ASSERT_TRUE(bubble != NULL); |
| 1513 | EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE, |
| 1514 | bubble->GetViewState()); |
| 1515 | |
| 1516 | // Simulate clicking translate. |
| 1517 | process()->sink().ClearMessages(); |
| 1518 | bubble->Translate(); |
| 1519 | SimulateTranslateScriptURLFetch(false); |
| 1520 | |
| 1521 | // We should not have sent any message to translate to the renderer. |
| 1522 | EXPECT_FALSE(GetTranslateMessage(NULL, NULL, NULL)); |
| 1523 | |
| 1524 | // And we should have an error infobar showing. |
| 1525 | bubble = factory->model(); |
| 1526 | ASSERT_TRUE(bubble != NULL); |
| 1527 | EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_ERROR, |
| 1528 | bubble->GetViewState()); |
| 1529 | } |
| 1530 | |
| 1531 | TEST_F(TranslateManagerBrowserTest, BubbleUnknownLanguage) { |
| 1532 | // Prepare for the bubble |
| 1533 | CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 1534 | command_line->AppendSwitch(switches::kEnableTranslateNewUX); |
| 1535 | MockTranslateBubbleFactory* factory = new MockTranslateBubbleFactory; |
| 1536 | scoped_ptr<TranslateBubbleFactory> factory_ptr(factory); |
| 1537 | TranslateBubbleFactory::SetFactory(factory); |
| 1538 | |
| 1539 | // Simulate navigating to a page ("und" is the string returned by the CLD for |
| 1540 | // languages it does not recognize). |
| 1541 | SimulateNavigation(GURL("http://www.google.mys"), "und", true); |
| 1542 | |
| 1543 | // We should not have a bubble as we don't know the language. |
| 1544 | ASSERT_TRUE(factory->model() == NULL); |
| 1545 | |
| 1546 | // Translate the page anyway throught the context menu. |
| 1547 | scoped_ptr<TestRenderViewContextMenu> menu( |
| 1548 | TestRenderViewContextMenu::CreateContextMenu(web_contents())); |
| 1549 | menu->Init(); |
| 1550 | menu->ExecuteCommand(IDC_CONTENT_CONTEXT_TRANSLATE, 0); |
| 1551 | |
| 1552 | // Check the bubble exists instead of the infobar. |
| 1553 | TranslateInfoBarDelegate* infobar = GetTranslateInfoBar(); |
| 1554 | ASSERT_TRUE(infobar == NULL); |
| 1555 | TranslateBubbleModel* bubble = factory->model(); |
| 1556 | ASSERT_TRUE(bubble != NULL); |
| 1557 | EXPECT_EQ(TranslateBubbleModel::VIEW_STATE_TRANSLATING, |
| 1558 | bubble->GetViewState()); |
| 1559 | } |
| 1560 | |
| 1561 | #endif // !defined(OS_ANDROID) && !defined(OS_IOS) |
| 1562 | |
[email protected] | 373b461 | 2012-12-15 23:26:41 | [diff] [blame] | 1563 | // Test is flaky on Win http://crbug.com/166334 |
| 1564 | #if defined(OS_WIN) |
| 1565 | #define MAYBE_PRE_TranslateSessionRestore DISABLED_PRE_TranslateSessionRestore |
| 1566 | #else |
| 1567 | #define MAYBE_PRE_TranslateSessionRestore PRE_TranslateSessionRestore |
| 1568 | #endif |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 1569 | // Test that session restore restores the translate infobar and other translate |
| 1570 | // settings. |
[email protected] | 373b461 | 2012-12-15 23:26:41 | [diff] [blame] | 1571 | IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, |
| 1572 | MAYBE_PRE_TranslateSessionRestore) { |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 1573 | SessionStartupPref pref(SessionStartupPref::LAST); |
| 1574 | SessionStartupPref::SetStartupPref(browser()->profile(), pref); |
| 1575 | |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 1576 | content::WebContents* current_web_contents = |
[email protected] | 47ae2337 | 2013-01-29 01:50:48 | [diff] [blame] | 1577 | browser()->tab_strip_model()->GetActiveWebContents(); |
[email protected] | f0235f6 | 2012-09-28 16:50:57 | [diff] [blame] | 1578 | TranslateTabHelper* translate_tab_helper = |
| 1579 | TranslateTabHelper::FromWebContents(current_web_contents); |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 1580 | content::Source<content::WebContents> source(current_web_contents); |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 1581 | |
[email protected] | 27eff89 | 2013-05-21 16:40:52 | [diff] [blame] | 1582 | ui_test_utils::WindowedNotificationObserverWithDetails< |
| 1583 | LanguageDetectionDetails> |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 1584 | fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, |
| 1585 | source); |
| 1586 | |
| 1587 | GURL french_url = ui_test_utils::GetTestUrl( |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 1588 | base::FilePath(), base::FilePath(FILE_PATH_LITERAL("french_page.html"))); |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 1589 | ui_test_utils::NavigateToURL(browser(), french_url); |
| 1590 | fr_language_detected_signal.Wait(); |
[email protected] | 27eff89 | 2013-05-21 16:40:52 | [diff] [blame] | 1591 | LanguageDetectionDetails details; |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 1592 | EXPECT_TRUE(fr_language_detected_signal.GetDetailsFor( |
[email protected] | 27eff89 | 2013-05-21 16:40:52 | [diff] [blame] | 1593 | source.map_key(), &details)); |
| 1594 | EXPECT_EQ("fr", details.adopted_language); |
[email protected] | f0235f6 | 2012-09-28 16:50:57 | [diff] [blame] | 1595 | EXPECT_EQ("fr", translate_tab_helper->language_state().original_language()); |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 1596 | } |
| 1597 | |
[email protected] | 373b461 | 2012-12-15 23:26:41 | [diff] [blame] | 1598 | #if defined (OS_WIN) |
| 1599 | #define MAYBE_TranslateSessionRestore DISABLED_TranslateSessionRestore |
| 1600 | #else |
| 1601 | #define MAYBE_TranslateSessionRestore TranslateSessionRestore |
| 1602 | #endif |
| 1603 | IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, MAYBE_TranslateSessionRestore) { |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 1604 | content::WebContents* current_web_contents = |
[email protected] | 47ae2337 | 2013-01-29 01:50:48 | [diff] [blame] | 1605 | browser()->tab_strip_model()->GetActiveWebContents(); |
[email protected] | 0502471 | 2013-07-31 09:46:29 | [diff] [blame] | 1606 | content::Source<content::WebContents> source(current_web_contents); |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 1607 | |
[email protected] | 27eff89 | 2013-05-21 16:40:52 | [diff] [blame] | 1608 | ui_test_utils::WindowedNotificationObserverWithDetails< |
| 1609 | LanguageDetectionDetails> |
[email protected] | f8a2e13 | 2012-08-31 18:16:20 | [diff] [blame] | 1610 | fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, |
| 1611 | source); |
| 1612 | fr_language_detected_signal.Wait(); |
| 1613 | } |