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 5 #include "base/strings/utf_string_conversions.h" 6 #include "chrome/browser/chrome_notification_types.h" 7 #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/prerender/prerender_manager.h" 10 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/browser/ui/content_settings/content_setting_image_model.h" 12 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 13 #include "chrome/test/base/testing_profile.h" 14 #include "content/public/browser/notification_observer.h" 15 #include "content/public/browser/notification_registrar.h" 16 #include "content/public/browser/notification_service.h" 17 #include "content/public/test/test_renderer_host.h" 18 #include "net/cookies/cookie_options.h" 19 #include "testing/gtest/include/gtest/gtest.h" 20 21 namespace { 22 23 // Forward all NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED to the specified 24 // ContentSettingImageModel. 25 class NotificationForwarder : public content::NotificationObserver { 26 public: 27 explicit NotificationForwarder(ContentSettingImageModel* model) 28 : model_(model) { 29 registrar_.Add(this, 30 chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED, 31 content::NotificationService::AllSources()); 32 } 33 virtual ~NotificationForwarder() {} 34 35 void clear() { 36 registrar_.RemoveAll(); 37 } 38 39 virtual void Observe(int type, 40 const content::NotificationSource& source, 41 const content::NotificationDetails& details) OVERRIDE { 42 if (type == chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED) { 43 model_->UpdateFromWebContents( 44 content::Source<content::WebContents>(source).ptr()); 45 } 46 } 47 48 private: 49 content::NotificationRegistrar registrar_; 50 ContentSettingImageModel* model_; 51 52 DISALLOW_COPY_AND_ASSIGN(NotificationForwarder); 53 }; 54 55 class ContentSettingImageModelTest : public ChromeRenderViewHostTestHarness { 56 }; 57 58 TEST_F(ContentSettingImageModelTest, UpdateFromWebContents) { 59 TabSpecificContentSettings::CreateForWebContents(web_contents()); 60 TabSpecificContentSettings* content_settings = 61 TabSpecificContentSettings::FromWebContents(web_contents()); 62 scoped_ptr<ContentSettingImageModel> content_setting_image_model( 63 ContentSettingImageModel::CreateContentSettingImageModel( 64 CONTENT_SETTINGS_TYPE_IMAGES)); 65 EXPECT_FALSE(content_setting_image_model->is_visible()); 66 EXPECT_EQ(0, content_setting_image_model->get_icon()); 67 EXPECT_TRUE(content_setting_image_model->get_tooltip().empty()); 68 69 content_settings->OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES, 70 std::string()); 71 content_setting_image_model->UpdateFromWebContents(web_contents()); 72 73 EXPECT_TRUE(content_setting_image_model->is_visible()); 74 EXPECT_NE(0, content_setting_image_model->get_icon()); 75 EXPECT_FALSE(content_setting_image_model->get_tooltip().empty()); 76 } 77 78 TEST_F(ContentSettingImageModelTest, RPHUpdateFromWebContents) { 79 TabSpecificContentSettings::CreateForWebContents(web_contents()); 80 scoped_ptr<ContentSettingImageModel> content_setting_image_model( 81 ContentSettingImageModel::CreateContentSettingImageModel( 82 CONTENT_SETTINGS_TYPE_PROTOCOL_HANDLERS)); 83 content_setting_image_model->UpdateFromWebContents(web_contents()); 84 EXPECT_FALSE(content_setting_image_model->is_visible()); 85 86 TabSpecificContentSettings* content_settings = 87 TabSpecificContentSettings::FromWebContents(web_contents()); 88 content_settings->set_pending_protocol_handler( 89 ProtocolHandler::CreateProtocolHandler( 90 "mailto", GURL("http://www.google.com/"), ASCIIToUTF16("Handler"))); 91 content_setting_image_model->UpdateFromWebContents(web_contents()); 92 EXPECT_TRUE(content_setting_image_model->is_visible()); 93 } 94 95 TEST_F(ContentSettingImageModelTest, CookieAccessed) { 96 TabSpecificContentSettings::CreateForWebContents(web_contents()); 97 TabSpecificContentSettings* content_settings = 98 TabSpecificContentSettings::FromWebContents(web_contents()); 99 profile()->GetHostContentSettingsMap()->SetDefaultContentSetting( 100 CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK); 101 scoped_ptr<ContentSettingImageModel> content_setting_image_model( 102 ContentSettingImageModel::CreateContentSettingImageModel( 103 CONTENT_SETTINGS_TYPE_COOKIES)); 104 EXPECT_FALSE(content_setting_image_model->is_visible()); 105 EXPECT_EQ(0, content_setting_image_model->get_icon()); 106 EXPECT_TRUE(content_setting_image_model->get_tooltip().empty()); 107 108 net::CookieOptions options; 109 content_settings->OnCookieChanged(GURL("http://google.com"), 110 GURL("http://google.com"), 111 "A=B", 112 options, 113 false); 114 content_setting_image_model->UpdateFromWebContents(web_contents()); 115 EXPECT_TRUE(content_setting_image_model->is_visible()); 116 EXPECT_NE(0, content_setting_image_model->get_icon()); 117 EXPECT_FALSE(content_setting_image_model->get_tooltip().empty()); 118 } 119 120 // Regression test for http://crbug.com/161854. 121 TEST_F(ContentSettingImageModelTest, NULLTabSpecificContentSettings) { 122 scoped_ptr<ContentSettingImageModel> content_setting_image_model( 123 ContentSettingImageModel::CreateContentSettingImageModel( 124 CONTENT_SETTINGS_TYPE_IMAGES)); 125 NotificationForwarder forwarder(content_setting_image_model.get()); 126 // Should not crash. 127 TabSpecificContentSettings::CreateForWebContents(web_contents()); 128 forwarder.clear(); 129 } 130 131 } // namespace 132