1 // Copyright (c) 2010 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 "chrome/browser/content_setting_image_model.h"
6 #include "chrome/browser/content_settings/host_content_settings_map.h"
7 #include "chrome/browser/prerender/prerender_manager.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/test/testing_profile.h"
10 #include "content/browser/browser_thread.h"
11 #include "content/browser/renderer_host/test_render_view_host.h"
12 #include "content/browser/tab_contents/test_tab_contents.h"
13 #include "net/base/cookie_options.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 class ContentSettingImageModelTest : public RenderViewHostTestHarness {
17 public:
ContentSettingImageModelTest()18 ContentSettingImageModelTest()
19 : RenderViewHostTestHarness(),
20 ui_thread_(BrowserThread::UI, &message_loop_) {}
21
22 private:
23 BrowserThread ui_thread_;
24
25 DISALLOW_COPY_AND_ASSIGN(ContentSettingImageModelTest);
26 };
27
TEST_F(ContentSettingImageModelTest,UpdateFromTabContents)28 TEST_F(ContentSettingImageModelTest, UpdateFromTabContents) {
29 TestTabContents tab_contents(profile_.get(), NULL);
30 TabSpecificContentSettings* content_settings =
31 tab_contents.GetTabSpecificContentSettings();
32 scoped_ptr<ContentSettingImageModel> content_setting_image_model(
33 ContentSettingImageModel::CreateContentSettingImageModel(
34 CONTENT_SETTINGS_TYPE_IMAGES));
35 EXPECT_FALSE(content_setting_image_model->is_visible());
36 EXPECT_EQ(0, content_setting_image_model->get_icon());
37 EXPECT_TRUE(content_setting_image_model->get_tooltip().empty());
38
39 content_settings->OnContentBlocked(CONTENT_SETTINGS_TYPE_IMAGES,
40 std::string());
41 content_setting_image_model->UpdateFromTabContents(&tab_contents);
42
43 EXPECT_TRUE(content_setting_image_model->is_visible());
44 EXPECT_NE(0, content_setting_image_model->get_icon());
45 EXPECT_FALSE(content_setting_image_model->get_tooltip().empty());
46 }
47
TEST_F(ContentSettingImageModelTest,CookieAccessed)48 TEST_F(ContentSettingImageModelTest, CookieAccessed) {
49 TestTabContents tab_contents(profile_.get(), NULL);
50 TabSpecificContentSettings* content_settings =
51 tab_contents.GetTabSpecificContentSettings();
52 profile_->GetHostContentSettingsMap()->SetDefaultContentSetting(
53 CONTENT_SETTINGS_TYPE_COOKIES, CONTENT_SETTING_BLOCK);
54 scoped_ptr<ContentSettingImageModel> content_setting_image_model(
55 ContentSettingImageModel::CreateContentSettingImageModel(
56 CONTENT_SETTINGS_TYPE_COOKIES));
57 EXPECT_FALSE(content_setting_image_model->is_visible());
58 EXPECT_EQ(0, content_setting_image_model->get_icon());
59 EXPECT_TRUE(content_setting_image_model->get_tooltip().empty());
60
61 net::CookieOptions options;
62 content_settings->OnCookieChanged(
63 GURL("http://google.com"), "A=B", options, false);
64 content_setting_image_model->UpdateFromTabContents(&tab_contents);
65 EXPECT_TRUE(content_setting_image_model->is_visible());
66 EXPECT_NE(0, content_setting_image_model->get_icon());
67 EXPECT_FALSE(content_setting_image_model->get_tooltip().empty());
68 }
69
TEST_F(ContentSettingImageModelTest,Prerender)70 TEST_F(ContentSettingImageModelTest, Prerender) {
71 prerender::PrerenderManager::SetMode(
72 prerender::PrerenderManager::PRERENDER_MODE_ENABLED);
73 TestTabContents tab_contents(profile_.get(), NULL);
74 scoped_ptr<ContentSettingImageModel> content_setting_image_model(
75 ContentSettingImageModel::CreateContentSettingImageModel(
76 CONTENT_SETTINGS_TYPE_PRERENDER));
77 EXPECT_FALSE(content_setting_image_model->is_visible());
78 EXPECT_NE(0, content_setting_image_model->get_icon());
79 EXPECT_FALSE(content_setting_image_model->get_tooltip().empty());
80
81 // Make the tab_contents prerendered
82 tab_contents.profile()->GetPrerenderManager()->MarkTabContentsAsPrerendered(
83 &tab_contents);
84 content_setting_image_model->UpdateFromTabContents(&tab_contents);
85 EXPECT_TRUE(content_setting_image_model->is_visible());
86 EXPECT_NE(0, content_setting_image_model->get_icon());
87 EXPECT_FALSE(content_setting_image_model->get_tooltip().empty());
88 }
89