Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/ui/webui/settings/site_settings_handler.h" | |
| 6 | |
| 7 #include "base/memory/scoped_ptr.h" | |
| 8 #include "chrome/browser/ui/webui/site_settings_helper.h" | |
| 9 #include "chrome/test/base/testing_profile.h" | |
| 10 #include "components/content_settings/core/common/content_settings.h" | |
| 11 #include "components/content_settings/core/common/content_settings_types.h" | |
| 12 #include "content/public/browser/web_ui_data_source.h" | |
| 13 #include "content/public/test/test_browser_thread_bundle.h" | |
| 14 #include "content/public/test/test_web_ui.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 namespace { | |
| 18 const char kCallbackId[] = "test-callback-id"; | |
|
michaelpg
2016/04/05 19:33:36
nit: no indent
| |
| 19 } | |
| 20 | |
| 21 namespace settings { | |
| 22 | |
| 23 class SiteSettingsHandlerTest : public testing::Test { | |
| 24 public: | |
| 25 SiteSettingsHandlerTest() {} | |
| 26 | |
| 27 Profile* profile() { return &profile_; } | |
| 28 content::TestWebUI* web_ui() { return &web_ui_; } | |
| 29 | |
| 30 void ValidateDefault(bool expected_default, size_t expected_total_calls) { | |
| 31 EXPECT_EQ(expected_total_calls, web_ui()->call_data().size()); | |
| 32 | |
| 33 const content::TestWebUI::CallData& data = *web_ui()->call_data().back(); | |
| 34 EXPECT_EQ("cr.webUIResponse", data.function_name()); | |
| 35 | |
| 36 std::string callback_id; | |
| 37 ASSERT_TRUE(data.arg1()->GetAsString(&callback_id)); | |
| 38 EXPECT_EQ(kCallbackId, callback_id); | |
| 39 | |
| 40 bool success = false; | |
| 41 ASSERT_TRUE(data.arg2()->GetAsBoolean(&success)); | |
| 42 ASSERT_TRUE(success); | |
| 43 | |
| 44 bool enabled; | |
| 45 ASSERT_TRUE(data.arg3()->GetAsBoolean(&enabled)); | |
| 46 EXPECT_EQ(expected_default, enabled); | |
| 47 } | |
| 48 | |
| 49 void ValidateOrigin( | |
| 50 const std::string& expected_origin, | |
| 51 const std::string& expected_embedding, | |
| 52 const std::string& expected_setting, | |
| 53 const std::string& expected_source, | |
| 54 size_t expected_total_calls) { | |
| 55 EXPECT_EQ(expected_total_calls, web_ui()->call_data().size()); | |
| 56 | |
| 57 const content::TestWebUI::CallData& data = *web_ui()->call_data().back(); | |
| 58 EXPECT_EQ("cr.webUIResponse", data.function_name()); | |
| 59 | |
| 60 std::string callback_id; | |
| 61 ASSERT_TRUE(data.arg1()->GetAsString(&callback_id)); | |
| 62 EXPECT_EQ(kCallbackId, callback_id); | |
| 63 bool success = false; | |
| 64 ASSERT_TRUE(data.arg2()->GetAsBoolean(&success)); | |
| 65 ASSERT_TRUE(success); | |
| 66 | |
| 67 const base::ListValue* exceptions; | |
| 68 ASSERT_TRUE(data.arg3()->GetAsList(&exceptions)); | |
| 69 EXPECT_EQ(1U, exceptions->GetSize()); | |
| 70 const base::DictionaryValue* exception; | |
| 71 ASSERT_TRUE(exceptions->GetDictionary(0, &exception)); | |
| 72 std::string origin, embedding_origin, setting, source; | |
| 73 ASSERT_TRUE(exception->GetString(site_settings::kOrigin, &origin)); | |
| 74 ASSERT_EQ(expected_origin, origin); | |
| 75 ASSERT_TRUE(exception->GetString( | |
| 76 site_settings::kEmbeddingOrigin, &embedding_origin)); | |
| 77 ASSERT_EQ(expected_embedding, embedding_origin); | |
| 78 ASSERT_TRUE(exception->GetString(site_settings::kSetting, &setting)); | |
| 79 ASSERT_EQ(expected_setting, setting); | |
| 80 ASSERT_TRUE(exception->GetString(site_settings::kSource, &source)); | |
| 81 ASSERT_EQ(expected_source, source); | |
| 82 } | |
| 83 | |
| 84 void ValidateNoOrigin(size_t expected_total_calls) { | |
| 85 EXPECT_EQ(expected_total_calls, web_ui()->call_data().size()); | |
| 86 | |
| 87 const content::TestWebUI::CallData& data = *web_ui()->call_data().back(); | |
| 88 EXPECT_EQ("cr.webUIResponse", data.function_name()); | |
| 89 | |
| 90 std::string callback_id; | |
| 91 ASSERT_TRUE(data.arg1()->GetAsString(&callback_id)); | |
| 92 EXPECT_EQ(kCallbackId, callback_id); | |
| 93 | |
| 94 bool success = false; | |
| 95 ASSERT_TRUE(data.arg2()->GetAsBoolean(&success)); | |
| 96 ASSERT_TRUE(success); | |
| 97 | |
| 98 const base::ListValue* exceptions; | |
| 99 ASSERT_TRUE(data.arg3()->GetAsList(&exceptions)); | |
| 100 EXPECT_EQ(0U, exceptions->GetSize()); | |
| 101 } | |
| 102 | |
| 103 private: | |
| 104 content::TestBrowserThreadBundle thread_bundle_; | |
| 105 TestingProfile profile_; | |
| 106 content::TestWebUI web_ui_; | |
| 107 }; | |
| 108 | |
| 109 TEST_F(SiteSettingsHandlerTest, GetAndSetDefault) { | |
| 110 SiteSettingsHandler handler(profile()); | |
| 111 handler.set_web_ui(web_ui()); | |
| 112 | |
| 113 ContentSettingsType type = CONTENT_SETTINGS_TYPE_NOTIFICATIONS; | |
| 114 | |
| 115 // Test the JS -> C++ -> JS callback path for getting and setting defaults. | |
| 116 base::ListValue getArgs; | |
| 117 getArgs.Append(new base::StringValue(kCallbackId)); | |
| 118 getArgs.Append(new base::FundamentalValue(type)); | |
| 119 handler.HandleGetDefaultValueForContentType(&getArgs); | |
| 120 ValidateDefault(true, 1U); | |
| 121 | |
| 122 // Set the default to 'Blocked'. | |
| 123 base::ListValue setArgs; | |
| 124 setArgs.Append(new base::FundamentalValue(type)); | |
| 125 setArgs.Append(new base::FundamentalValue(CONTENT_SETTING_BLOCK)); | |
| 126 handler.HandleSetDefaultValueForContentType(&setArgs); | |
| 127 | |
| 128 EXPECT_EQ(2U, web_ui()->call_data().size()); | |
| 129 | |
| 130 // Verify that the default has been set to 'Blocked'. | |
| 131 handler.HandleGetDefaultValueForContentType(&getArgs); | |
| 132 ValidateDefault(false, 3U); | |
| 133 } | |
| 134 | |
| 135 TEST_F(SiteSettingsHandlerTest, Origins) { | |
| 136 SiteSettingsHandler handler(profile()); | |
| 137 handler.set_web_ui(web_ui()); | |
| 138 | |
| 139 ContentSettingsType type = CONTENT_SETTINGS_TYPE_NOTIFICATIONS; | |
| 140 | |
| 141 // Test the JS -> C++ -> JS callback path for configuring origins, by setting | |
| 142 // Google.com to blocked. | |
| 143 base::ListValue setArgs; | |
| 144 std::string google("http://www.google.com"); | |
| 145 setArgs.Append(new base::StringValue(google)); // Primary pattern. | |
| 146 setArgs.Append(new base::StringValue(google)); // Secondary pattern. | |
| 147 setArgs.Append(new base::FundamentalValue(type)); | |
| 148 setArgs.Append(new base::FundamentalValue(CONTENT_SETTING_BLOCK)); | |
| 149 handler.HandleSetCategoryPermissionForOrigin(&setArgs); | |
| 150 EXPECT_EQ(1U, web_ui()->call_data().size()); | |
| 151 | |
| 152 // Verify the change was successful. | |
| 153 base::ListValue listArgs; | |
| 154 listArgs.Append(new base::StringValue(kCallbackId)); | |
| 155 listArgs.Append(new base::FundamentalValue(type)); | |
| 156 handler.HandleGetExceptionList(&listArgs); | |
| 157 ValidateOrigin(google, google, "block", "preference", 2U); | |
| 158 | |
| 159 // Reset things back to how they were. | |
| 160 base::ListValue resetArgs; | |
| 161 resetArgs.Append(new base::StringValue(google)); | |
| 162 resetArgs.Append(new base::StringValue(google)); | |
| 163 resetArgs.Append(new base::FundamentalValue(type)); | |
| 164 handler.HandleResetCategoryPermissionForOrigin(&resetArgs); | |
| 165 EXPECT_EQ(3U, web_ui()->call_data().size()); | |
| 166 | |
| 167 // Verify the reset was successful. | |
| 168 handler.HandleGetExceptionList(&listArgs); | |
| 169 ValidateNoOrigin(4U); | |
| 170 } | |
| 171 | |
| 172 } // namespace settings | |
| OLD | NEW |