Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / content_settings / content_setting_image_model_unittest.cc
blob10b63bd5c66e431423b3bb337dcd84132452474a
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_factory.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 "components/content_settings/core/browser/host_content_settings_map.h"
15 #include "content/public/browser/notification_observer.h"
16 #include "content/public/browser/notification_registrar.h"
17 #include "content/public/browser/notification_service.h"
18 #include "content/public/test/test_renderer_host.h"
19 #include "net/cookies/cookie_options.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 namespace {
24 // Forward all NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED to the specified
25 // ContentSettingImageModel.
26 class NotificationForwarder : public content::NotificationObserver {
27 public:
28 explicit NotificationForwarder(ContentSettingImageModel* model)
29 : model_(model) {
30 registrar_.Add(this,
31 chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED,
32 content::NotificationService::AllSources());
34 ~NotificationForwarder() override {}
36 void clear() {
37 registrar_.RemoveAll();
40 void Observe(int type,
41 const content::NotificationSource& source,
42 const content::NotificationDetails& details) override {
43 if (type == chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED) {
44 model_->UpdateFromWebContents(
45 content::Source<content::WebContents>(source).ptr());
49 private:
50 content::NotificationRegistrar registrar_;
51 ContentSettingImageModel* model_;
53 DISALLOW_COPY_AND_ASSIGN(NotificationForwarder);
56 class ContentSettingImageModelTest : public ChromeRenderViewHostTestHarness {
59 TEST_F(ContentSettingImageModelTest, UpdateFromWebContents) {
60 TabSpecificContentSettings::CreateForWebContents(web_contents());
61 TabSpecificContentSettings* content_settings =
62 TabSpecificContentSettings::FromWebContents(web_contents());
63 scoped_ptr<ContentSettingImageModel> content_setting_image_model(
64 ContentSettingImageModel::CreateContentSettingImageModel(
65 CONTENT_SETTINGS_TYPE_IMAGES));
66 EXPECT_FALSE(content_setting_image_model->is_visible());
67 EXPECT_TRUE(content_setting_image_model->icon().IsEmpty());
68 EXPECT_TRUE(content_setting_image_model->get_tooltip().empty());
70 content_settings->OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES);
71 content_setting_image_model->UpdateFromWebContents(web_contents());
73 EXPECT_TRUE(content_setting_image_model->is_visible());
74 EXPECT_FALSE(content_setting_image_model->icon().IsEmpty());
75 EXPECT_FALSE(content_setting_image_model->get_tooltip().empty());
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());
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/")));
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 HostContentSettingsMapFactory::GetForProfile(profile())
100 ->SetDefaultContentSetting(CONTENT_SETTINGS_TYPE_COOKIES,
101 CONTENT_SETTING_BLOCK);
102 scoped_ptr<ContentSettingImageModel> content_setting_image_model(
103 ContentSettingImageModel::CreateContentSettingImageModel(
104 CONTENT_SETTINGS_TYPE_COOKIES));
105 EXPECT_FALSE(content_setting_image_model->is_visible());
106 EXPECT_TRUE(content_setting_image_model->icon().IsEmpty());
107 EXPECT_TRUE(content_setting_image_model->get_tooltip().empty());
109 net::CookieOptions options;
110 content_settings->OnCookieChanged(GURL("http://google.com"),
111 GURL("http://google.com"),
112 "A=B",
113 options,
114 false);
115 content_setting_image_model->UpdateFromWebContents(web_contents());
116 EXPECT_TRUE(content_setting_image_model->is_visible());
117 EXPECT_FALSE(content_setting_image_model->icon().IsEmpty());
118 EXPECT_FALSE(content_setting_image_model->get_tooltip().empty());
121 // Regression test for http://crbug.com/161854.
122 TEST_F(ContentSettingImageModelTest, NULLTabSpecificContentSettings) {
123 scoped_ptr<ContentSettingImageModel> content_setting_image_model(
124 ContentSettingImageModel::CreateContentSettingImageModel(
125 CONTENT_SETTINGS_TYPE_IMAGES));
126 NotificationForwarder forwarder(content_setting_image_model.get());
127 // Should not crash.
128 TabSpecificContentSettings::CreateForWebContents(web_contents());
129 forwarder.clear();
132 } // namespace