OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 | 5 |
6 #include <algorithm> | 6 #include <algorithm> |
7 #include <set> | 7 #include <set> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/prefs/public/pref_change_registrar.h" | 12 #include "base/prefs/public/pref_change_registrar.h" |
| 13 #include "base/prefs/public/pref_observer.h" |
13 #include "base/stringprintf.h" | 14 #include "base/stringprintf.h" |
14 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
15 #include "base/values.h" | 16 #include "base/values.h" |
16 #include "chrome/app/chrome_command_ids.h" | 17 #include "chrome/app/chrome_command_ids.h" |
17 #include "chrome/browser/extensions/test_extension_system.h" | 18 #include "chrome/browser/extensions/test_extension_system.h" |
18 #include "chrome/browser/infobars/infobar.h" | 19 #include "chrome/browser/infobars/infobar.h" |
19 #include "chrome/browser/infobars/infobar_tab_helper.h" | 20 #include "chrome/browser/infobars/infobar_tab_helper.h" |
20 #include "chrome/browser/prefs/pref_service.h" | 21 #include "chrome/browser/prefs/pref_service.h" |
21 #include "chrome/browser/prefs/session_startup_pref.h" | 22 #include "chrome/browser/prefs/session_startup_pref.h" |
22 #include "chrome/browser/tab_contents/render_view_context_menu.h" | 23 #include "chrome/browser/tab_contents/render_view_context_menu.h" |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 } | 86 } |
86 | 87 |
87 private: | 88 private: |
88 content::LoadCommittedDetails details_; | 89 content::LoadCommittedDetails details_; |
89 content::NotificationRegistrar registrar_; | 90 content::NotificationRegistrar registrar_; |
90 | 91 |
91 DISALLOW_COPY_AND_ASSIGN(NavEntryCommittedObserver); | 92 DISALLOW_COPY_AND_ASSIGN(NavEntryCommittedObserver); |
92 }; | 93 }; |
93 | 94 |
94 class TranslateManagerTest : public ChromeRenderViewHostTestHarness, | 95 class TranslateManagerTest : public ChromeRenderViewHostTestHarness, |
95 public content::NotificationObserver { | 96 public content::NotificationObserver, |
| 97 public PrefObserver { |
96 public: | 98 public: |
97 TranslateManagerTest() | 99 TranslateManagerTest() |
98 : ui_thread_(BrowserThread::UI, &message_loop_) { | 100 : ui_thread_(BrowserThread::UI, &message_loop_) { |
99 } | 101 } |
100 | 102 |
101 // Simulates navigating to a page and getting the page contents and language | 103 // Simulates navigating to a page and getting the page contents and language |
102 // for that navigation. | 104 // for that navigation. |
103 void SimulateNavigation(const GURL& url, | 105 void SimulateNavigation(const GURL& url, |
104 const std::string& lang, | 106 const std::string& lang, |
105 bool page_translatable) { | 107 bool page_translatable) { |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 } | 209 } |
208 | 210 |
209 virtual void Observe(int type, | 211 virtual void Observe(int type, |
210 const content::NotificationSource& source, | 212 const content::NotificationSource& source, |
211 const content::NotificationDetails& details) { | 213 const content::NotificationDetails& details) { |
212 DCHECK_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, type); | 214 DCHECK_EQ(chrome::NOTIFICATION_TAB_CONTENTS_INFOBAR_REMOVED, type); |
213 removed_infobars_.insert( | 215 removed_infobars_.insert( |
214 content::Details<InfoBarRemovedDetails>(details)->first); | 216 content::Details<InfoBarRemovedDetails>(details)->first); |
215 } | 217 } |
216 | 218 |
| 219 MOCK_METHOD2(OnPreferenceChanged, void(PrefServiceBase*, const std::string&)); |
| 220 |
217 protected: | 221 protected: |
218 virtual void SetUp() { | 222 virtual void SetUp() { |
219 WebKit::initialize(webkit_platform_support_.Get()); | 223 WebKit::initialize(webkit_platform_support_.Get()); |
220 // Access the TranslateManager singleton so it is created before we call | 224 // Access the TranslateManager singleton so it is created before we call |
221 // ChromeRenderViewHostTestHarness::SetUp() to match what's done in Chrome, | 225 // ChromeRenderViewHostTestHarness::SetUp() to match what's done in Chrome, |
222 // where the TranslateManager is created before the WebContents. This | 226 // where the TranslateManager is created before the WebContents. This |
223 // matters as they both register for similar events and we want the | 227 // matters as they both register for similar events and we want the |
224 // notifications to happen in the same sequence (TranslateManager first, | 228 // notifications to happen in the same sequence (TranslateManager first, |
225 // WebContents second). Also clears the translate script so it is fetched | 229 // WebContents second). Also clears the translate script so it is fetched |
226 // everytime and sets the expiration delay to a large value by default (in | 230 // everytime and sets the expiration delay to a large value by default (in |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 data += "}})"; | 288 data += "}})"; |
285 } | 289 } |
286 fetcher->set_url(fetcher->GetOriginalURL()); | 290 fetcher->set_url(fetcher->GetOriginalURL()); |
287 fetcher->set_status(status); | 291 fetcher->set_status(status); |
288 fetcher->set_response_code(success ? 200 : 500); | 292 fetcher->set_response_code(success ? 200 : 500); |
289 fetcher->SetResponseString(data); | 293 fetcher->SetResponseString(data); |
290 fetcher->delegate()->OnURLFetchComplete(fetcher); | 294 fetcher->delegate()->OnURLFetchComplete(fetcher); |
291 } | 295 } |
292 | 296 |
293 void SetPrefObserverExpectation(const char* path) { | 297 void SetPrefObserverExpectation(const char* path) { |
294 EXPECT_CALL( | 298 EXPECT_CALL(*this, OnPreferenceChanged(_, std::string(path))); |
295 pref_observer_, | |
296 Observe(int(chrome::NOTIFICATION_PREF_CHANGED), | |
297 _, | |
298 Property(&content::Details<std::string>::ptr, Pointee(path)))); | |
299 } | 299 } |
300 | 300 |
301 content::MockNotificationObserver pref_observer_; | |
302 | |
303 private: | 301 private: |
304 content::NotificationRegistrar notification_registrar_; | 302 content::NotificationRegistrar notification_registrar_; |
305 net::TestURLFetcherFactory url_fetcher_factory_; | 303 net::TestURLFetcherFactory url_fetcher_factory_; |
306 content::TestBrowserThread ui_thread_; | 304 content::TestBrowserThread ui_thread_; |
307 content::RenderViewTest::RendererWebKitPlatformSupportImplNoSandbox | 305 content::RenderViewTest::RendererWebKitPlatformSupportImplNoSandbox |
308 webkit_platform_support_; | 306 webkit_platform_support_; |
309 | 307 |
310 // The infobars that have been removed. | 308 // The infobars that have been removed. |
311 // WARNING: the pointers point to deleted objects, use only for comparison. | 309 // WARNING: the pointers point to deleted objects, use only for comparison. |
312 std::set<InfoBarDelegate*> removed_infobars_; | 310 std::set<InfoBarDelegate*> removed_infobars_; |
(...skipping 774 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1087 | 1085 |
1088 // An infobar should be shown. | 1086 // An infobar should be shown. |
1089 EXPECT_TRUE(GetTranslateInfoBar() != NULL); | 1087 EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
1090 | 1088 |
1091 // Select never translate this language. | 1089 // Select never translate this language. |
1092 Profile* profile = | 1090 Profile* profile = |
1093 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 1091 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
1094 PrefService* prefs = profile->GetPrefs(); | 1092 PrefService* prefs = profile->GetPrefs(); |
1095 PrefChangeRegistrar registrar; | 1093 PrefChangeRegistrar registrar; |
1096 registrar.Init(prefs); | 1094 registrar.Init(prefs); |
1097 registrar.Add(TranslatePrefs::kPrefTranslateLanguageBlacklist, | 1095 registrar.Add(TranslatePrefs::kPrefTranslateLanguageBlacklist, this); |
1098 &pref_observer_); | |
1099 TranslatePrefs translate_prefs(prefs); | 1096 TranslatePrefs translate_prefs(prefs); |
1100 EXPECT_FALSE(translate_prefs.IsLanguageBlacklisted("fr")); | 1097 EXPECT_FALSE(translate_prefs.IsLanguageBlacklisted("fr")); |
1101 EXPECT_TRUE(translate_prefs.CanTranslate(prefs, "fr", url)); | 1098 EXPECT_TRUE(translate_prefs.CanTranslate(prefs, "fr", url)); |
1102 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateLanguageBlacklist); | 1099 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateLanguageBlacklist); |
1103 translate_prefs.BlacklistLanguage("fr"); | 1100 translate_prefs.BlacklistLanguage("fr"); |
1104 EXPECT_TRUE(translate_prefs.IsLanguageBlacklisted("fr")); | 1101 EXPECT_TRUE(translate_prefs.IsLanguageBlacklisted("fr")); |
1105 EXPECT_FALSE(translate_prefs.CanTranslate(prefs, "fr", url)); | 1102 EXPECT_FALSE(translate_prefs.CanTranslate(prefs, "fr", url)); |
1106 | 1103 |
1107 // Close the infobar. | 1104 // Close the infobar. |
1108 EXPECT_TRUE(CloseTranslateInfoBar()); | 1105 EXPECT_TRUE(CloseTranslateInfoBar()); |
(...skipping 26 matching lines...) Expand all Loading... |
1135 | 1132 |
1136 // An infobar should be shown. | 1133 // An infobar should be shown. |
1137 EXPECT_TRUE(GetTranslateInfoBar() != NULL); | 1134 EXPECT_TRUE(GetTranslateInfoBar() != NULL); |
1138 | 1135 |
1139 // Select never translate this site. | 1136 // Select never translate this site. |
1140 Profile* profile = | 1137 Profile* profile = |
1141 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 1138 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
1142 PrefService* prefs = profile->GetPrefs(); | 1139 PrefService* prefs = profile->GetPrefs(); |
1143 PrefChangeRegistrar registrar; | 1140 PrefChangeRegistrar registrar; |
1144 registrar.Init(prefs); | 1141 registrar.Init(prefs); |
1145 registrar.Add(TranslatePrefs::kPrefTranslateSiteBlacklist, | 1142 registrar.Add(TranslatePrefs::kPrefTranslateSiteBlacklist, this); |
1146 &pref_observer_); | |
1147 TranslatePrefs translate_prefs(prefs); | 1143 TranslatePrefs translate_prefs(prefs); |
1148 EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(host)); | 1144 EXPECT_FALSE(translate_prefs.IsSiteBlacklisted(host)); |
1149 EXPECT_TRUE(translate_prefs.CanTranslate(prefs, "fr", url)); | 1145 EXPECT_TRUE(translate_prefs.CanTranslate(prefs, "fr", url)); |
1150 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateSiteBlacklist); | 1146 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateSiteBlacklist); |
1151 translate_prefs.BlacklistSite(host); | 1147 translate_prefs.BlacklistSite(host); |
1152 EXPECT_TRUE(translate_prefs.IsSiteBlacklisted(host)); | 1148 EXPECT_TRUE(translate_prefs.IsSiteBlacklisted(host)); |
1153 EXPECT_FALSE(translate_prefs.CanTranslate(prefs, "fr", url)); | 1149 EXPECT_FALSE(translate_prefs.CanTranslate(prefs, "fr", url)); |
1154 | 1150 |
1155 // Close the infobar. | 1151 // Close the infobar. |
1156 EXPECT_TRUE(CloseTranslateInfoBar()); | 1152 EXPECT_TRUE(CloseTranslateInfoBar()); |
(...skipping 18 matching lines...) Expand all Loading... |
1175 } | 1171 } |
1176 | 1172 |
1177 // Tests the "Always translate this language" pref. | 1173 // Tests the "Always translate this language" pref. |
1178 TEST_F(TranslateManagerTest, AlwaysTranslateLanguagePref) { | 1174 TEST_F(TranslateManagerTest, AlwaysTranslateLanguagePref) { |
1179 // Select always translate French to English. | 1175 // Select always translate French to English. |
1180 Profile* profile = | 1176 Profile* profile = |
1181 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 1177 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
1182 PrefService* prefs = profile->GetPrefs(); | 1178 PrefService* prefs = profile->GetPrefs(); |
1183 PrefChangeRegistrar registrar; | 1179 PrefChangeRegistrar registrar; |
1184 registrar.Init(prefs); | 1180 registrar.Init(prefs); |
1185 registrar.Add(TranslatePrefs::kPrefTranslateWhitelists, | 1181 registrar.Add(TranslatePrefs::kPrefTranslateWhitelists, this); |
1186 &pref_observer_); | |
1187 TranslatePrefs translate_prefs(prefs); | 1182 TranslatePrefs translate_prefs(prefs); |
1188 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateWhitelists); | 1183 SetPrefObserverExpectation(TranslatePrefs::kPrefTranslateWhitelists); |
1189 translate_prefs.WhitelistLanguagePair("fr", "en"); | 1184 translate_prefs.WhitelistLanguagePair("fr", "en"); |
1190 | 1185 |
1191 // Load a page in French. | 1186 // Load a page in French. |
1192 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); | 1187 SimulateNavigation(GURL("http://www.google.fr"), "fr", true); |
1193 | 1188 |
1194 // It should have triggered an automatic translation to English. | 1189 // It should have triggered an automatic translation to English. |
1195 | 1190 |
1196 // The translating infobar should be showing. | 1191 // The translating infobar should be showing. |
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1518 | 1513 |
1519 IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, TranslateSessionRestore) { | 1514 IN_PROC_BROWSER_TEST_F(InProcessBrowserTest, TranslateSessionRestore) { |
1520 WebContents* current_web_contents = chrome::GetActiveWebContents(browser()); | 1515 WebContents* current_web_contents = chrome::GetActiveWebContents(browser()); |
1521 content::Source<WebContents> source(current_web_contents); | 1516 content::Source<WebContents> source(current_web_contents); |
1522 | 1517 |
1523 ui_test_utils::WindowedNotificationObserverWithDetails<std::string> | 1518 ui_test_utils::WindowedNotificationObserverWithDetails<std::string> |
1524 fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, | 1519 fr_language_detected_signal(chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, |
1525 source); | 1520 source); |
1526 fr_language_detected_signal.Wait(); | 1521 fr_language_detected_signal.Wait(); |
1527 } | 1522 } |
OLD | NEW |