| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/settings/site_settings_handler.h" | 5 #include "chrome/browser/ui/webui/settings/site_settings_handler.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "chrome/browser/ui/webui/site_settings_helper.h" | 9 #include "chrome/browser/ui/webui/site_settings_helper.h" |
| 10 #include "chrome/test/base/testing_profile.h" | 10 #include "chrome/test/base/testing_profile.h" |
| 11 #include "components/content_settings/core/common/content_settings.h" | 11 #include "components/content_settings/core/common/content_settings.h" |
| 12 #include "components/content_settings/core/common/content_settings_types.h" | 12 #include "components/content_settings/core/common/content_settings_types.h" |
| 13 #include "content/public/browser/web_ui_data_source.h" | 13 #include "content/public/browser/web_ui_data_source.h" |
| 14 #include "content/public/test/test_browser_thread_bundle.h" | 14 #include "content/public/test/test_browser_thread_bundle.h" |
| 15 #include "content/public/test/test_web_ui.h" | 15 #include "content/public/test/test_web_ui.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 const char kCallbackId[] = "test-callback-id"; | 20 const char kCallbackId[] = "test-callback-id"; |
| 21 | 21 |
| 22 } | 22 } |
| 23 | 23 |
| 24 namespace settings { | 24 namespace settings { |
| 25 | 25 |
| 26 class SiteSettingsHandlerTest : public testing::Test { | 26 class SiteSettingsHandlerTest : public testing::Test { |
| 27 public: | 27 public: |
| 28 SiteSettingsHandlerTest() {} | 28 SiteSettingsHandlerTest() : handler_(&profile_) {} |
| 29 |
| 30 void SetUp() override { |
| 31 handler()->set_web_ui(web_ui()); |
| 32 handler()->AllowJavascript(); |
| 33 web_ui()->ClearTrackedCalls(); |
| 34 } |
| 29 | 35 |
| 30 Profile* profile() { return &profile_; } | 36 Profile* profile() { return &profile_; } |
| 31 content::TestWebUI* web_ui() { return &web_ui_; } | 37 content::TestWebUI* web_ui() { return &web_ui_; } |
| 38 SiteSettingsHandler* handler() { return &handler_; } |
| 32 | 39 |
| 33 void ValidateDefault(bool expected_default, size_t expected_total_calls) { | 40 void ValidateDefault(bool expected_default, size_t expected_total_calls) { |
| 34 EXPECT_EQ(expected_total_calls, web_ui()->call_data().size()); | 41 EXPECT_EQ(expected_total_calls, web_ui()->call_data().size()); |
| 35 | 42 |
| 36 const content::TestWebUI::CallData& data = *web_ui()->call_data().back(); | 43 const content::TestWebUI::CallData& data = *web_ui()->call_data().back(); |
| 37 EXPECT_EQ("cr.webUIResponse", data.function_name()); | 44 EXPECT_EQ("cr.webUIResponse", data.function_name()); |
| 38 | 45 |
| 39 std::string callback_id; | 46 std::string callback_id; |
| 40 ASSERT_TRUE(data.arg1()->GetAsString(&callback_id)); | 47 ASSERT_TRUE(data.arg1()->GetAsString(&callback_id)); |
| 41 EXPECT_EQ(kCallbackId, callback_id); | 48 EXPECT_EQ(kCallbackId, callback_id); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 | 126 |
| 120 bool valid; | 127 bool valid; |
| 121 ASSERT_TRUE(data.arg3()->GetAsBoolean(&valid)); | 128 ASSERT_TRUE(data.arg3()->GetAsBoolean(&valid)); |
| 122 EXPECT_EQ(expected_validity, valid); | 129 EXPECT_EQ(expected_validity, valid); |
| 123 } | 130 } |
| 124 | 131 |
| 125 private: | 132 private: |
| 126 content::TestBrowserThreadBundle thread_bundle_; | 133 content::TestBrowserThreadBundle thread_bundle_; |
| 127 TestingProfile profile_; | 134 TestingProfile profile_; |
| 128 content::TestWebUI web_ui_; | 135 content::TestWebUI web_ui_; |
| 136 SiteSettingsHandler handler_; |
| 129 }; | 137 }; |
| 130 | 138 |
| 131 TEST_F(SiteSettingsHandlerTest, GetAndSetDefault) { | 139 TEST_F(SiteSettingsHandlerTest, GetAndSetDefault) { |
| 132 SiteSettingsHandler handler(profile()); | |
| 133 handler.set_web_ui(web_ui()); | |
| 134 | |
| 135 ContentSettingsType type = CONTENT_SETTINGS_TYPE_NOTIFICATIONS; | 140 ContentSettingsType type = CONTENT_SETTINGS_TYPE_NOTIFICATIONS; |
| 136 | 141 |
| 137 // Test the JS -> C++ -> JS callback path for getting and setting defaults. | 142 // Test the JS -> C++ -> JS callback path for getting and setting defaults. |
| 138 base::ListValue getArgs; | 143 base::ListValue getArgs; |
| 139 getArgs.Append(new base::StringValue(kCallbackId)); | 144 getArgs.Append(new base::StringValue(kCallbackId)); |
| 140 getArgs.Append(new base::FundamentalValue(type)); | 145 getArgs.Append(new base::FundamentalValue(type)); |
| 141 handler.HandleGetDefaultValueForContentType(&getArgs); | 146 handler()->HandleGetDefaultValueForContentType(&getArgs); |
| 142 ValidateDefault(true, 1U); | 147 ValidateDefault(true, 1U); |
| 143 | 148 |
| 144 // Set the default to 'Blocked'. | 149 // Set the default to 'Blocked'. |
| 145 base::ListValue setArgs; | 150 base::ListValue setArgs; |
| 146 setArgs.Append(new base::FundamentalValue(type)); | 151 setArgs.Append(new base::FundamentalValue(type)); |
| 147 setArgs.Append(new base::StringValue("block")); | 152 setArgs.Append(new base::StringValue("block")); |
| 148 handler.HandleSetDefaultValueForContentType(&setArgs); | 153 handler()->HandleSetDefaultValueForContentType(&setArgs); |
| 149 | 154 |
| 150 EXPECT_EQ(2U, web_ui()->call_data().size()); | 155 EXPECT_EQ(2U, web_ui()->call_data().size()); |
| 151 | 156 |
| 152 // Verify that the default has been set to 'Blocked'. | 157 // Verify that the default has been set to 'Blocked'. |
| 153 handler.HandleGetDefaultValueForContentType(&getArgs); | 158 handler()->HandleGetDefaultValueForContentType(&getArgs); |
| 154 ValidateDefault(false, 3U); | 159 ValidateDefault(false, 3U); |
| 155 } | 160 } |
| 156 | 161 |
| 157 TEST_F(SiteSettingsHandlerTest, Origins) { | 162 TEST_F(SiteSettingsHandlerTest, Origins) { |
| 158 SiteSettingsHandler handler(profile()); | |
| 159 handler.set_web_ui(web_ui()); | |
| 160 | |
| 161 ContentSettingsType type = CONTENT_SETTINGS_TYPE_NOTIFICATIONS; | 163 ContentSettingsType type = CONTENT_SETTINGS_TYPE_NOTIFICATIONS; |
| 162 | 164 |
| 163 // Test the JS -> C++ -> JS callback path for configuring origins, by setting | 165 // Test the JS -> C++ -> JS callback path for configuring origins, by setting |
| 164 // Google.com to blocked. | 166 // Google.com to blocked. |
| 165 base::ListValue setArgs; | 167 base::ListValue setArgs; |
| 166 std::string google("http://www.google.com"); | 168 std::string google("http://www.google.com"); |
| 167 setArgs.Append(new base::StringValue(google)); // Primary pattern. | 169 setArgs.Append(new base::StringValue(google)); // Primary pattern. |
| 168 setArgs.Append(new base::StringValue(google)); // Secondary pattern. | 170 setArgs.Append(new base::StringValue(google)); // Secondary pattern. |
| 169 setArgs.Append(new base::FundamentalValue(type)); | 171 setArgs.Append(new base::FundamentalValue(type)); |
| 170 setArgs.Append(new base::StringValue("block")); | 172 setArgs.Append(new base::StringValue("block")); |
| 171 handler.HandleSetCategoryPermissionForOrigin(&setArgs); | 173 handler()->HandleSetCategoryPermissionForOrigin(&setArgs); |
| 172 EXPECT_EQ(1U, web_ui()->call_data().size()); | 174 EXPECT_EQ(1U, web_ui()->call_data().size()); |
| 173 | 175 |
| 174 // Verify the change was successful. | 176 // Verify the change was successful. |
| 175 base::ListValue listArgs; | 177 base::ListValue listArgs; |
| 176 listArgs.Append(new base::StringValue(kCallbackId)); | 178 listArgs.Append(new base::StringValue(kCallbackId)); |
| 177 listArgs.Append(new base::FundamentalValue(type)); | 179 listArgs.Append(new base::FundamentalValue(type)); |
| 178 handler.HandleGetExceptionList(&listArgs); | 180 handler()->HandleGetExceptionList(&listArgs); |
| 179 ValidateOrigin(google, google, "block", "preference", 2U); | 181 ValidateOrigin(google, google, "block", "preference", 2U); |
| 180 | 182 |
| 181 // Reset things back to how they were. | 183 // Reset things back to how they were. |
| 182 base::ListValue resetArgs; | 184 base::ListValue resetArgs; |
| 183 resetArgs.Append(new base::StringValue(google)); | 185 resetArgs.Append(new base::StringValue(google)); |
| 184 resetArgs.Append(new base::StringValue(google)); | 186 resetArgs.Append(new base::StringValue(google)); |
| 185 resetArgs.Append(new base::FundamentalValue(type)); | 187 resetArgs.Append(new base::FundamentalValue(type)); |
| 186 handler.HandleResetCategoryPermissionForOrigin(&resetArgs); | 188 handler()->HandleResetCategoryPermissionForOrigin(&resetArgs); |
| 187 EXPECT_EQ(3U, web_ui()->call_data().size()); | 189 EXPECT_EQ(3U, web_ui()->call_data().size()); |
| 188 | 190 |
| 189 // Verify the reset was successful. | 191 // Verify the reset was successful. |
| 190 handler.HandleGetExceptionList(&listArgs); | 192 handler()->HandleGetExceptionList(&listArgs); |
| 191 ValidateNoOrigin(4U); | 193 ValidateNoOrigin(4U); |
| 192 } | 194 } |
| 193 | 195 |
| 194 TEST_F(SiteSettingsHandlerTest, Patterns) { | 196 TEST_F(SiteSettingsHandlerTest, Patterns) { |
| 195 SiteSettingsHandler handler(profile()); | |
| 196 handler.set_web_ui(web_ui()); | |
| 197 | |
| 198 base::ListValue args; | 197 base::ListValue args; |
| 199 std::string pattern("[*.]google.com"); | 198 std::string pattern("[*.]google.com"); |
| 200 args.Append(new base::StringValue(kCallbackId)); | 199 args.Append(new base::StringValue(kCallbackId)); |
| 201 args.Append(new base::StringValue(pattern)); | 200 args.Append(new base::StringValue(pattern)); |
| 202 handler.HandleIsPatternValid(&args); | 201 handler()->HandleIsPatternValid(&args); |
| 203 ValidatePattern(true, 1U); | 202 ValidatePattern(true, 1U); |
| 204 | 203 |
| 205 base::ListValue invalid; | 204 base::ListValue invalid; |
| 206 std::string bad_pattern(";"); | 205 std::string bad_pattern(";"); |
| 207 invalid.Append(new base::StringValue(kCallbackId)); | 206 invalid.Append(new base::StringValue(kCallbackId)); |
| 208 invalid.Append(new base::StringValue(bad_pattern)); | 207 invalid.Append(new base::StringValue(bad_pattern)); |
| 209 handler.HandleIsPatternValid(&invalid); | 208 handler()->HandleIsPatternValid(&invalid); |
| 210 ValidatePattern(false, 2U); | 209 ValidatePattern(false, 2U); |
| 211 } | 210 } |
| 212 | 211 |
| 213 } // namespace settings | 212 } // namespace settings |
| OLD | NEW |