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 settings { | |
| 18 | |
| 19 class SiteSettingsHandlerTest : public testing::Test { | |
| 20 public: | |
| 21 SiteSettingsHandlerTest() {} | |
| 22 | |
| 23 void SetUp() override { | |
| 24 profile_.reset(new TestingProfile()); | |
| 25 } | |
| 26 | |
| 27 Profile* profile() { return profile_.get(); } | |
| 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("test-callback-id", callback_id); | |
|
michaelpg
2016/04/01 09:08:28
opt. nit: make the callback id a member or static
Finnur
2016/04/05 10:22:31
Done.
| |
| 39 | |
|
michaelpg
2016/04/01 09:08:28
should you check the call was successful via arg2(
Finnur
2016/04/05 10:22:31
Done.
| |
| 40 bool enabled; | |
| 41 ASSERT_TRUE(data.arg3()->GetAsBoolean(&enabled)); | |
| 42 EXPECT_EQ(expected_default, enabled); | |
| 43 } | |
| 44 | |
| 45 void ValidateOrigin( | |
| 46 const std::string& expected_origin, | |
| 47 const std::string& expected_embedding, | |
| 48 const std::string& expected_setting, | |
| 49 const std::string& expected_source, | |
| 50 size_t expected_total_calls) { | |
| 51 EXPECT_EQ(expected_total_calls, web_ui()->call_data().size()); | |
| 52 | |
| 53 const content::TestWebUI::CallData& data = *web_ui()->call_data().back(); | |
| 54 EXPECT_EQ("cr.webUIResponse", data.function_name()); | |
| 55 | |
| 56 std::string callback_id; | |
| 57 ASSERT_TRUE(data.arg1()->GetAsString(&callback_id)); | |
| 58 EXPECT_EQ("test-callback-id", callback_id); | |
| 59 | |
| 60 const base::ListValue* exceptions; | |
| 61 ASSERT_TRUE(data.arg3()->GetAsList(&exceptions)); | |
| 62 EXPECT_EQ(1U, exceptions->GetSize()); | |
| 63 const base::DictionaryValue* exception; | |
| 64 ASSERT_TRUE(exceptions->GetDictionary(0, &exception)); | |
| 65 std::string origin, embedding_origin, setting, source; | |
| 66 ASSERT_TRUE(exception->GetString(site_settings::kOrigin, &origin)); | |
| 67 ASSERT_EQ(expected_origin, origin); | |
| 68 ASSERT_TRUE(exception->GetString( | |
| 69 site_settings::kEmbeddingOrigin, &embedding_origin)); | |
| 70 ASSERT_EQ(expected_embedding, embedding_origin); | |
| 71 ASSERT_TRUE(exception->GetString(site_settings::kSetting, &setting)); | |
| 72 ASSERT_EQ(expected_setting, setting); | |
| 73 ASSERT_TRUE(exception->GetString(site_settings::kSource, &source)); | |
| 74 ASSERT_EQ(expected_source, source); | |
| 75 } | |
| 76 | |
| 77 void ValidateNoOrigin(size_t expected_total_calls) { | |
| 78 EXPECT_EQ(expected_total_calls, web_ui()->call_data().size()); | |
| 79 | |
| 80 const content::TestWebUI::CallData& data = *web_ui()->call_data().back(); | |
| 81 EXPECT_EQ("cr.webUIResponse", data.function_name()); | |
| 82 | |
| 83 std::string callback_id; | |
| 84 ASSERT_TRUE(data.arg1()->GetAsString(&callback_id)); | |
| 85 EXPECT_EQ("test-callback-id", callback_id); | |
| 86 | |
| 87 const base::ListValue* exceptions; | |
| 88 ASSERT_TRUE(data.arg3()->GetAsList(&exceptions)); | |
| 89 EXPECT_EQ(0U, exceptions->GetSize()); | |
| 90 } | |
| 91 | |
| 92 private: | |
| 93 content::TestBrowserThreadBundle thread_bundle_; | |
| 94 scoped_ptr<TestingProfile> profile_; | |
|
michaelpg
2016/04/01 09:08:28
can this just be a "TestingProfile profile_" membe
Finnur
2016/04/05 10:22:31
Done.
| |
| 95 content::TestWebUI web_ui_; | |
| 96 }; | |
| 97 | |
| 98 TEST_F(SiteSettingsHandlerTest, GetAndSetDefault) { | |
| 99 scoped_ptr<SiteSettingsHandler> handler; | |
|
michaelpg
2016/04/01 09:08:28
SiteSettingsHandler handler(profile());
Finnur
2016/04/05 10:22:31
Done.
| |
| 100 handler.reset(new SiteSettingsHandler(profile())); | |
| 101 handler->set_web_ui(web_ui()); | |
| 102 | |
| 103 ContentSettingsType type = CONTENT_SETTINGS_TYPE_NOTIFICATIONS; | |
| 104 | |
| 105 // Test the JS -> C++ -> JS callback path for getting and setting defaults. | |
| 106 base::ListValue getArgs; | |
| 107 getArgs.Append(new base::StringValue("test-callback-id")); | |
| 108 getArgs.Append(new base::FundamentalValue(type)); | |
| 109 handler->HandleGetDefaultValueForContentType(&getArgs); | |
| 110 ValidateDefault(true, 1U); | |
|
Finnur
2016/04/05 21:24:37
This validates that we've had 1 call into web_ui (
| |
| 111 | |
| 112 // Set the default to 'Blocked'. | |
| 113 base::ListValue setArgs; | |
| 114 setArgs.Append(new base::FundamentalValue(type)); | |
| 115 setArgs.Append(new base::FundamentalValue(CONTENT_SETTING_BLOCK)); | |
| 116 handler->HandleSetDefaultValueForContentType(&setArgs); | |
| 117 | |
| 118 EXPECT_EQ(2U, web_ui()->call_data().size()); | |
|
michaelpg
2016/04/01 09:08:28
is this the change event? why not verify the callb
Finnur
2016/04/05 10:22:31
Umm, I'm not sure which callback you are referring
michaelpg
2016/04/05 19:33:36
so where does this 2nd call into webui come from?
Finnur
2016/04/05 21:24:37
This validates that we've had 2 calls from JS into
michaelpg
2016/04/05 23:16:05
So, you're verifying that Get calls WebUI (via the
| |
| 119 | |
| 120 // Verify that the default has been set to 'Blocked'. | |
| 121 handler->HandleGetDefaultValueForContentType(&getArgs); | |
| 122 ValidateDefault(false, 3U); | |
| 123 } | |
| 124 | |
| 125 TEST_F(SiteSettingsHandlerTest, Origins) { | |
| 126 scoped_ptr<SiteSettingsHandler> handler; | |
|
michaelpg
2016/04/01 09:08:28
SiteSettingsHandler handler(profile());
Finnur
2016/04/05 10:22:31
Done.
| |
| 127 handler.reset(new SiteSettingsHandler(profile())); | |
| 128 handler->set_web_ui(web_ui()); | |
| 129 | |
| 130 ContentSettingsType type = CONTENT_SETTINGS_TYPE_NOTIFICATIONS; | |
| 131 | |
| 132 // Test the JS -> C++ -> JS callback path for configuring origins, by setting | |
| 133 // Google.com to blocked. | |
| 134 base::ListValue setArgs; | |
| 135 std::string google("http://www.google.com"); | |
| 136 setArgs.Append(new base::StringValue(google)); // Primary pattern. | |
| 137 setArgs.Append(new base::StringValue(google)); // Secondary pattern. | |
| 138 setArgs.Append(new base::FundamentalValue(type)); | |
| 139 setArgs.Append(new base::FundamentalValue(CONTENT_SETTING_BLOCK)); | |
| 140 handler->HandleSetCategoryPermissionForOrigin(&setArgs); | |
| 141 EXPECT_EQ(1U, web_ui()->call_data().size()); | |
| 142 | |
| 143 // Verify the change was successful. | |
| 144 base::ListValue listArgs; | |
| 145 listArgs.Append(new base::StringValue("test-callback-id")); | |
| 146 listArgs.Append(new base::FundamentalValue(type)); | |
| 147 handler->HandleGetExceptionList(&listArgs); | |
| 148 ValidateOrigin(google, google, "block", "preference", 2U); | |
| 149 | |
| 150 // Reset things back to how they were. | |
| 151 base::ListValue resetArgs; | |
| 152 resetArgs.Append(new base::StringValue(google)); | |
| 153 resetArgs.Append(new base::StringValue(google)); | |
| 154 resetArgs.Append(new base::FundamentalValue(type)); | |
| 155 handler->HandleResetCategoryPermissionForOrigin(&resetArgs); | |
| 156 EXPECT_EQ(3U, web_ui()->call_data().size()); | |
| 157 | |
| 158 // Verify the reset was successful. | |
| 159 handler->HandleGetExceptionList(&listArgs); | |
| 160 ValidateNoOrigin(4U); | |
| 161 } | |
| 162 | |
| 163 } // namespace settings | |
| OLD | NEW |