Chromium Code Reviews| 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 #include "base/auto_reset.h" | 5 #include "base/auto_reset.h" | 
| 6 #include "base/command_line.h" | 6 #include "base/command_line.h" | 
| 7 #include "base/utf_string_conversions.h" | |
| 7 #include "chrome/browser/content_settings/host_content_settings_map.h" | 8 #include "chrome/browser/content_settings/host_content_settings_map.h" | 
| 8 #include "chrome/browser/content_settings/tab_specific_content_settings.h" | 9 #include "chrome/browser/content_settings/tab_specific_content_settings.h" | 
| 10 #include "chrome/browser/custom_handlers/protocol_handler_registry.h" | |
| 9 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" | 
| 10 #include "chrome/browser/ui/content_settings/content_setting_bubble_model.h" | 12 #include "chrome/browser/ui/content_settings/content_setting_bubble_model.h" | 
| 11 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 13 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 
| 12 #include "chrome/browser/ui/tab_contents/test_tab_contents.h" | 14 #include "chrome/browser/ui/tab_contents/test_tab_contents.h" | 
| 13 #include "chrome/common/chrome_switches.h" | 15 #include "chrome/common/chrome_switches.h" | 
| 16 #include "chrome/common/content_settings.h" | |
| 14 #include "chrome/test/base/testing_profile.h" | 17 #include "chrome/test/base/testing_profile.h" | 
| 15 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" | 
| 16 #include "content/public/test/test_browser_thread.h" | 19 #include "content/public/test/test_browser_thread.h" | 
| 17 #include "content/public/test/web_contents_tester.h" | 20 #include "content/public/test/web_contents_tester.h" | 
| 18 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" | 
| 19 | 22 | 
| 20 using content::BrowserThread; | 23 using content::BrowserThread; | 
| 21 using content::WebContentsTester; | 24 using content::WebContentsTester; | 
| 22 | 25 | 
| 23 class ContentSettingBubbleModelTest : public TabContentsTestHarness { | 26 class ContentSettingBubbleModelTest : public TabContentsTestHarness { | 
| 24 protected: | 27 protected: | 
| 25 ContentSettingBubbleModelTest() | 28 ContentSettingBubbleModelTest() | 
| 26 : ui_thread_(BrowserThread::UI, MessageLoop::current()) { | 29 : ui_thread_(BrowserThread::UI, MessageLoop::current()) { | 
| 27 } | 30 } | 
| 28 | 31 | 
| 32 void StartIOThread() { | |
| 33 io_thread_.reset(new content::TestBrowserThread(BrowserThread::IO)); | |
| 34 io_thread_->StartIOThread(); | |
| 35 } | |
| 36 | |
| 37 virtual void TearDown() { | |
| 38 // This will delete the TestingProfile on the UI thread. | |
| 39 TabContentsTestHarness::TearDown(); | |
| 40 | |
| 41 // Finish off deleting the ProtocolHandlerRegistry, which must be done on | |
| 42 // the IO thread. | |
| 43 if (io_thread_.get()) { | |
| 44 io_thread_->Stop(); | |
| 45 io_thread_.reset(NULL); | |
| 46 } | |
| 47 } | |
| 48 | |
| 29 void CheckGeolocationBubble(size_t expected_domains, | 49 void CheckGeolocationBubble(size_t expected_domains, | 
| 30 bool expect_clear_link, | 50 bool expect_clear_link, | 
| 31 bool expect_reload_hint) { | 51 bool expect_reload_hint) { | 
| 32 scoped_ptr<ContentSettingBubbleModel> content_setting_bubble_model( | 52 scoped_ptr<ContentSettingBubbleModel> content_setting_bubble_model( | 
| 33 ContentSettingBubbleModel::CreateContentSettingBubbleModel( | 53 ContentSettingBubbleModel::CreateContentSettingBubbleModel( | 
| 34 NULL, tab_contents(), profile(), | 54 NULL, tab_contents(), profile(), | 
| 35 CONTENT_SETTINGS_TYPE_GEOLOCATION)); | 55 CONTENT_SETTINGS_TYPE_GEOLOCATION)); | 
| 36 const ContentSettingBubbleModel::BubbleContent& bubble_content = | 56 const ContentSettingBubbleModel::BubbleContent& bubble_content = | 
| 37 content_setting_bubble_model->bubble_content(); | 57 content_setting_bubble_model->bubble_content(); | 
| 38 EXPECT_TRUE(bubble_content.title.empty()); | 58 EXPECT_TRUE(bubble_content.title.empty()); | 
| 39 EXPECT_TRUE(bubble_content.radio_group.radio_items.empty()); | 59 EXPECT_TRUE(bubble_content.radio_group.radio_items.empty()); | 
| 40 EXPECT_TRUE(bubble_content.popup_items.empty()); | 60 EXPECT_TRUE(bubble_content.popup_items.empty()); | 
| 41 EXPECT_EQ(expected_domains, bubble_content.domain_lists.size()); | 61 EXPECT_EQ(expected_domains, bubble_content.domain_lists.size()); | 
| 42 EXPECT_NE(expect_clear_link || expect_reload_hint, | 62 EXPECT_NE(expect_clear_link || expect_reload_hint, | 
| 43 bubble_content.custom_link.empty()); | 63 bubble_content.custom_link.empty()); | 
| 44 EXPECT_EQ(expect_clear_link, bubble_content.custom_link_enabled); | 64 EXPECT_EQ(expect_clear_link, bubble_content.custom_link_enabled); | 
| 45 EXPECT_FALSE(bubble_content.manage_link.empty()); | 65 EXPECT_FALSE(bubble_content.manage_link.empty()); | 
| 46 } | 66 } | 
| 47 | 67 | 
| 48 content::TestBrowserThread ui_thread_; | 68 content::TestBrowserThread ui_thread_; | 
| 69 scoped_ptr<content::TestBrowserThread> io_thread_; | |
| 49 }; | 70 }; | 
| 50 | 71 | 
| 51 TEST_F(ContentSettingBubbleModelTest, ImageRadios) { | 72 TEST_F(ContentSettingBubbleModelTest, ImageRadios) { | 
| 52 TabSpecificContentSettings* content_settings = | 73 TabSpecificContentSettings* content_settings = | 
| 53 tab_contents()->content_settings(); | 74 tab_contents()->content_settings(); | 
| 54 content_settings->OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, | 75 content_settings->OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, | 
| 55 std::string()); | 76 std::string()); | 
| 56 | 77 | 
| 57 scoped_ptr<ContentSettingBubbleModel> content_setting_bubble_model( | 78 scoped_ptr<ContentSettingBubbleModel> content_setting_bubble_model( | 
| 58 ContentSettingBubbleModel::CreateContentSettingBubbleModel( | 79 ContentSettingBubbleModel::CreateContentSettingBubbleModel( | 
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 213 std::string file_url("file:///tmp/test.html"); | 234 std::string file_url("file:///tmp/test.html"); | 
| 214 NavigateAndCommit(GURL(file_url)); | 235 NavigateAndCommit(GURL(file_url)); | 
| 215 scoped_ptr<ContentSettingBubbleModel> content_setting_bubble_model( | 236 scoped_ptr<ContentSettingBubbleModel> content_setting_bubble_model( | 
| 216 ContentSettingBubbleModel::CreateContentSettingBubbleModel( | 237 ContentSettingBubbleModel::CreateContentSettingBubbleModel( | 
| 217 NULL, tab_contents(), profile(), | 238 NULL, tab_contents(), profile(), | 
| 218 CONTENT_SETTINGS_TYPE_IMAGES)); | 239 CONTENT_SETTINGS_TYPE_IMAGES)); | 
| 219 std::string title = | 240 std::string title = | 
| 220 content_setting_bubble_model->bubble_content().radio_group.radio_items[0]; | 241 content_setting_bubble_model->bubble_content().radio_group.radio_items[0]; | 
| 221 ASSERT_NE(std::string::npos, title.find(file_url)); | 242 ASSERT_NE(std::string::npos, title.find(file_url)); | 
| 222 } | 243 } | 
| 244 | |
| 245 TEST_F(ContentSettingBubbleModelTest, RegisterProtocolHandler) { | |
| 246 const GURL page_url("http://toplevel.example/"); | |
| 247 NavigateAndCommit(page_url); | |
| 248 TabSpecificContentSettings* content_settings = | |
| 249 tab_contents()->content_settings(); | |
| 250 content_settings->set_pending_protocol_handler( | |
| 251 ProtocolHandler::CreateProtocolHandler("mailto", | |
| 252 GURL("http://www.toplevel.example/"), ASCIIToUTF16("Handler"))); | |
| 253 | |
| 254 scoped_ptr<ContentSettingBubbleModel> content_setting_bubble_model( | |
| 255 ContentSettingBubbleModel::CreateContentSettingBubbleModel( | |
| 256 NULL, tab_contents(), profile(), | |
| 257 CONTENT_SETTINGS_TYPE_PROTOCOL_HANDLERS)); | |
| 258 const ContentSettingBubbleModel::BubbleContent& bubble_content = | |
| 259 content_setting_bubble_model->bubble_content(); | |
| 260 EXPECT_FALSE(bubble_content.title.empty()); | |
| 261 EXPECT_FALSE(bubble_content.radio_group.radio_items.empty()); | |
| 262 EXPECT_TRUE(bubble_content.popup_items.empty()); | |
| 263 EXPECT_TRUE(bubble_content.domain_lists.empty()); | |
| 264 EXPECT_TRUE(bubble_content.custom_link.empty()); | |
| 265 EXPECT_FALSE(bubble_content.custom_link_enabled); | |
| 266 EXPECT_FALSE(bubble_content.manage_link.empty()); | |
| 267 } | |
| 268 | |
| 269 class FakeDelegate : public ProtocolHandlerRegistry::Delegate { | |
| 270 public: | |
| 271 virtual void RegisterExternalHandler(const std::string& protocol) { | |
| 272 // Overrides in order to not register the handler with the | |
| 273 // ChildProcessSecurityPolicy. That has persistent and unalterable | |
| 274 // side effects on other tests. | |
| 275 } | |
| 276 | |
| 277 virtual ShellIntegration::DefaultProtocolClientWorker* CreateShellWorker( | |
| 278 ShellIntegration::DefaultWebClientObserver* observer, | |
| 279 const std::string& protocol) { | |
| 280 LOG(INFO) << "CreateShellWorker"; | |
| 281 return NULL; | |
| 282 } | |
| 283 | |
| 284 virtual ProtocolHandlerRegistry::DefaultClientObserver* CreateShellObserver( | |
| 285 ProtocolHandlerRegistry* registry) { | |
| 286 return NULL; | |
| 287 } | |
| 288 | |
| 289 virtual void RegisterWithOSAsDefaultClient( | |
| 290 const std::string& protocol, | |
| 291 ProtocolHandlerRegistry* registry) { | |
| 292 LOG(INFO) << "Register With OS"; | |
| 
 
Nico
2013/03/13 17:31:57
What's the point of LOG(INFO) statements in tests?
 
 | |
| 293 } | |
| 294 }; | |
| 295 | |
| 296 TEST_F(ContentSettingBubbleModelTest, RPHAllow) { | |
| 297 StartIOThread(); | |
| 298 profile()->CreateProtocolHandlerRegistry(new FakeDelegate); | |
| 299 | |
| 300 const GURL page_url("http://toplevel.example/"); | |
| 301 NavigateAndCommit(page_url); | |
| 302 TabSpecificContentSettings* content_settings = | |
| 303 tab_contents()->content_settings(); | |
| 304 ProtocolHandler test_handler = ProtocolHandler::CreateProtocolHandler( | |
| 305 "mailto", GURL("http://www.toplevel.example/"), | |
| 306 ASCIIToUTF16("Handler")); | |
| 307 content_settings->set_pending_protocol_handler(test_handler); | |
| 308 | |
| 309 scoped_ptr<ContentSettingBubbleModel> content_setting_bubble_model( | |
| 310 ContentSettingBubbleModel::CreateContentSettingBubbleModel( | |
| 311 NULL, tab_contents(), profile(), | |
| 312 CONTENT_SETTINGS_TYPE_PROTOCOL_HANDLERS)); | |
| 313 | |
| 314 { | |
| 315 ProtocolHandler handler = | |
| 316 profile()->GetProtocolHandlerRegistry()->GetHandlerFor("mailto"); | |
| 317 EXPECT_TRUE(handler.IsEmpty()); | |
| 318 EXPECT_EQ(CONTENT_SETTING_DEFAULT, | |
| 319 content_settings->pending_protocol_handler_setting()); | |
| 320 } | |
| 321 | |
| 322 // "0" is the "Allow" radio button. | |
| 323 content_setting_bubble_model->OnRadioClicked(0); | |
| 324 { | |
| 325 ProtocolHandler handler = | |
| 326 profile()->GetProtocolHandlerRegistry()->GetHandlerFor("mailto"); | |
| 327 ASSERT_FALSE(handler.IsEmpty()); | |
| 328 EXPECT_EQ(ASCIIToUTF16("Handler"), handler.title()); | |
| 329 EXPECT_EQ(CONTENT_SETTING_ALLOW, | |
| 330 content_settings->pending_protocol_handler_setting()); | |
| 331 } | |
| 332 | |
| 333 // "1" is the "Deny" radio button. | |
| 334 content_setting_bubble_model->OnRadioClicked(1); | |
| 335 { | |
| 336 ProtocolHandler handler = | |
| 337 profile()->GetProtocolHandlerRegistry()->GetHandlerFor("mailto"); | |
| 338 EXPECT_TRUE(handler.IsEmpty()); | |
| 339 EXPECT_EQ(CONTENT_SETTING_BLOCK, | |
| 340 content_settings->pending_protocol_handler_setting()); | |
| 341 } | |
| 342 | |
| 343 // "2" is the "Ignore button. | |
| 344 content_setting_bubble_model->OnRadioClicked(2); | |
| 345 { | |
| 346 ProtocolHandler handler = | |
| 347 profile()->GetProtocolHandlerRegistry()->GetHandlerFor("mailto"); | |
| 348 EXPECT_TRUE(handler.IsEmpty()); | |
| 349 EXPECT_EQ(CONTENT_SETTING_DEFAULT, | |
| 350 content_settings->pending_protocol_handler_setting()); | |
| 351 EXPECT_TRUE(profile()->GetProtocolHandlerRegistry()->IsIgnored( | |
| 352 test_handler)); | |
| 353 } | |
| 354 | |
| 355 // "0" is the "Allow" radio button. | |
| 356 content_setting_bubble_model->OnRadioClicked(0); | |
| 357 { | |
| 358 ProtocolHandler handler = | |
| 359 profile()->GetProtocolHandlerRegistry()->GetHandlerFor("mailto"); | |
| 360 ASSERT_FALSE(handler.IsEmpty()); | |
| 361 EXPECT_EQ(ASCIIToUTF16("Handler"), handler.title()); | |
| 362 EXPECT_EQ(CONTENT_SETTING_ALLOW, | |
| 363 content_settings->pending_protocol_handler_setting()); | |
| 364 EXPECT_FALSE(profile()->GetProtocolHandlerRegistry()->IsIgnored( | |
| 365 test_handler)); | |
| 366 } | |
| 367 | |
| 368 // This must be done on the UI thread. | |
| 369 profile()->GetProtocolHandlerRegistry()->Finalize(); | |
| 370 } | |
| OLD | NEW |