1 // Copyright (c) 2011 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 <memory>
6 
7 #include "components/content_settings/core/common/content_settings_pattern.h"
8 #include "components/content_settings/core/test/content_settings_mock_provider.h"
9 #include "components/content_settings/core/test/content_settings_test_utils.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "url/gurl.h"
12 
13 namespace content_settings {
14 
TEST(ContentSettingsProviderTest,Mock)15 TEST(ContentSettingsProviderTest, Mock) {
16   ContentSettingsPattern pattern =
17       ContentSettingsPattern::FromString("[*.]youtube.com");
18   GURL url("http://www.youtube.com");
19 
20   MockProvider mock_provider(false);
21   mock_provider.SetWebsiteSetting(
22       pattern, pattern, ContentSettingsType::PLUGINS, "java_plugin",
23       std::make_unique<base::Value>(CONTENT_SETTING_BLOCK));
24 
25   EXPECT_EQ(CONTENT_SETTING_BLOCK,
26             TestUtils::GetContentSetting(&mock_provider, url, url,
27                                          ContentSettingsType::PLUGINS,
28                                          "java_plugin", false));
29   std::unique_ptr<base::Value> value_ptr(TestUtils::GetContentSettingValue(
30       &mock_provider, url, url, ContentSettingsType::PLUGINS, "java_plugin",
31       false));
32   int int_value = -1;
33   value_ptr->GetAsInteger(&int_value);
34   EXPECT_EQ(CONTENT_SETTING_BLOCK, IntToContentSetting(int_value));
35 
36   EXPECT_EQ(CONTENT_SETTING_DEFAULT,
37             TestUtils::GetContentSetting(&mock_provider, url, url,
38                                          ContentSettingsType::PLUGINS,
39                                          "flash_plugin", false));
40   EXPECT_EQ(nullptr, TestUtils::GetContentSettingValue(
41                          &mock_provider, url, url, ContentSettingsType::PLUGINS,
42                          "flash_plugin", false));
43   EXPECT_EQ(CONTENT_SETTING_DEFAULT,
44             TestUtils::GetContentSetting(&mock_provider, url, url,
45                                          ContentSettingsType::GEOLOCATION,
46                                          std::string(), false));
47   EXPECT_EQ(nullptr,
48             TestUtils::GetContentSettingValue(&mock_provider, url, url,
49                                               ContentSettingsType::GEOLOCATION,
50                                               std::string(), false));
51 
52   bool owned = mock_provider.SetWebsiteSetting(
53       pattern, pattern, ContentSettingsType::PLUGINS, "java_plugin",
54       std::make_unique<base::Value>(CONTENT_SETTING_ALLOW));
55   EXPECT_TRUE(owned);
56   EXPECT_EQ(CONTENT_SETTING_ALLOW,
57             TestUtils::GetContentSetting(&mock_provider, url, url,
58                                          ContentSettingsType::PLUGINS,
59                                          "java_plugin", false));
60 
61   mock_provider.set_read_only(true);
62   std::unique_ptr<base::Value> value(new base::Value(CONTENT_SETTING_BLOCK));
63   owned = mock_provider.SetWebsiteSetting(pattern, pattern,
64                                           ContentSettingsType::PLUGINS,
65                                           "java_plugin", std::move(value));
66   EXPECT_FALSE(owned);
67   EXPECT_EQ(CONTENT_SETTING_ALLOW,
68             TestUtils::GetContentSetting(&mock_provider, url, url,
69                                          ContentSettingsType::PLUGINS,
70                                          "java_plugin", false));
71 
72   EXPECT_TRUE(mock_provider.read_only());
73 
74   mock_provider.set_read_only(false);
75   owned = mock_provider.SetWebsiteSetting(
76       pattern, pattern, ContentSettingsType::PLUGINS, "java_plugin",
77       std::make_unique<base::Value>(CONTENT_SETTING_BLOCK));
78   EXPECT_TRUE(owned);
79   EXPECT_EQ(CONTENT_SETTING_BLOCK,
80             TestUtils::GetContentSetting(&mock_provider, url, url,
81                                          ContentSettingsType::PLUGINS,
82                                          "java_plugin", false));
83 }
84 
85 }  // namespace content_settings
86