Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / content_settings / content_setting_image_model_unittest.cc
blob3cf2aeb9ec23782763408050799ea705d2cdcd03
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.
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"
21 namespace {
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());
33 virtual ~NotificationForwarder() {}
35 void clear() {
36 registrar_.RemoveAll();
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());
48 private:
49 content::NotificationRegistrar registrar_;
50 ContentSettingImageModel* model_;
52 DISALLOW_COPY_AND_ASSIGN(NotificationForwarder);
55 class ContentSettingImageModelTest : public ChromeRenderViewHostTestHarness {
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());
69 content_settings->OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES);
70 content_setting_image_model->UpdateFromWebContents(web_contents());
72 EXPECT_TRUE(content_setting_image_model->is_visible());
73 EXPECT_NE(0, content_setting_image_model->get_icon());
74 EXPECT_FALSE(content_setting_image_model->get_tooltip().empty());
77 TEST_F(ContentSettingImageModelTest, RPHUpdateFromWebContents) {
78 TabSpecificContentSettings::CreateForWebContents(web_contents());
79 scoped_ptr<ContentSettingImageModel> content_setting_image_model(
80 ContentSettingImageModel::CreateContentSettingImageModel(
81 CONTENT_SETTINGS_TYPE_PROTOCOL_HANDLERS));
82 content_setting_image_model->UpdateFromWebContents(web_contents());
83 EXPECT_FALSE(content_setting_image_model->is_visible());
85 TabSpecificContentSettings* content_settings =
86 TabSpecificContentSettings::FromWebContents(web_contents());
87 content_settings->set_pending_protocol_handler(
88 ProtocolHandler::CreateProtocolHandler(
89 "mailto", GURL("http://www.google.com/"),
90 base::ASCIIToUTF16("Handler")));
91 content_setting_image_model->UpdateFromWebContents(web_contents());
92 EXPECT_TRUE(content_setting_image_model->is_visible());
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());
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());
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();
131 } // namespace