Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(96)

Side by Side Diff: chrome/browser/ui/webui/settings/site_settings_handler_unittest.cc

Issue 1988463002: MD Settings: Convert C++ handlers to be JavaScript-lifecycle aware. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
33 // Get the default value for an arbitrary content type to AllowJavascript.
34 base::ListValue list_args;
35 list_args.Append(new base::StringValue("get-exceptions-callback-id"));
36 list_args.AppendDouble(CONTENT_SETTINGS_TYPE_FULLSCREEN);
37 handler()->HandleGetDefaultValueForContentType(&list_args);
Dan Beam 2016/05/18 00:23:30 this is OK, but maybe just ... call handler()->All
tommycli 2016/05/18 21:12:12 Done.
38
39 web_ui()->ClearTrackedCalls();
40 }
29 41
30 Profile* profile() { return &profile_; } 42 Profile* profile() { return &profile_; }
31 content::TestWebUI* web_ui() { return &web_ui_; } 43 content::TestWebUI* web_ui() { return &web_ui_; }
44 SiteSettingsHandler* handler() { return &handler_; }
32 45
33 void ValidateDefault(bool expected_default, size_t expected_total_calls) { 46 void ValidateDefault(bool expected_default, size_t expected_total_calls) {
34 EXPECT_EQ(expected_total_calls, web_ui()->call_data().size()); 47 EXPECT_EQ(expected_total_calls, web_ui()->call_data().size());
35 48
36 const content::TestWebUI::CallData& data = *web_ui()->call_data().back(); 49 const content::TestWebUI::CallData& data = *web_ui()->call_data().back();
37 EXPECT_EQ("cr.webUIResponse", data.function_name()); 50 EXPECT_EQ("cr.webUIResponse", data.function_name());
38 51
39 std::string callback_id; 52 std::string callback_id;
40 ASSERT_TRUE(data.arg1()->GetAsString(&callback_id)); 53 ASSERT_TRUE(data.arg1()->GetAsString(&callback_id));
41 EXPECT_EQ(kCallbackId, callback_id); 54 EXPECT_EQ(kCallbackId, callback_id);
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 132
120 bool valid; 133 bool valid;
121 ASSERT_TRUE(data.arg3()->GetAsBoolean(&valid)); 134 ASSERT_TRUE(data.arg3()->GetAsBoolean(&valid));
122 EXPECT_EQ(expected_validity, valid); 135 EXPECT_EQ(expected_validity, valid);
123 } 136 }
124 137
125 private: 138 private:
126 content::TestBrowserThreadBundle thread_bundle_; 139 content::TestBrowserThreadBundle thread_bundle_;
127 TestingProfile profile_; 140 TestingProfile profile_;
128 content::TestWebUI web_ui_; 141 content::TestWebUI web_ui_;
142 SiteSettingsHandler handler_;
129 }; 143 };
130 144
131 TEST_F(SiteSettingsHandlerTest, GetAndSetDefault) { 145 TEST_F(SiteSettingsHandlerTest, GetAndSetDefault) {
132 SiteSettingsHandler handler(profile());
133 handler.set_web_ui(web_ui());
134
135 ContentSettingsType type = CONTENT_SETTINGS_TYPE_NOTIFICATIONS; 146 ContentSettingsType type = CONTENT_SETTINGS_TYPE_NOTIFICATIONS;
136 147
137 // Test the JS -> C++ -> JS callback path for getting and setting defaults. 148 // Test the JS -> C++ -> JS callback path for getting and setting defaults.
138 base::ListValue getArgs; 149 base::ListValue getArgs;
139 getArgs.Append(new base::StringValue(kCallbackId)); 150 getArgs.Append(new base::StringValue(kCallbackId));
140 getArgs.Append(new base::FundamentalValue(type)); 151 getArgs.Append(new base::FundamentalValue(type));
141 handler.HandleGetDefaultValueForContentType(&getArgs); 152 handler()->HandleGetDefaultValueForContentType(&getArgs);
142 ValidateDefault(true, 1U); 153 ValidateDefault(true, 1U);
143 154
144 // Set the default to 'Blocked'. 155 // Set the default to 'Blocked'.
145 base::ListValue setArgs; 156 base::ListValue setArgs;
146 setArgs.Append(new base::FundamentalValue(type)); 157 setArgs.Append(new base::FundamentalValue(type));
147 setArgs.Append(new base::StringValue("block")); 158 setArgs.Append(new base::StringValue("block"));
148 handler.HandleSetDefaultValueForContentType(&setArgs); 159 handler()->HandleSetDefaultValueForContentType(&setArgs);
149 160
150 EXPECT_EQ(2U, web_ui()->call_data().size()); 161 EXPECT_EQ(2U, web_ui()->call_data().size());
151 162
152 // Verify that the default has been set to 'Blocked'. 163 // Verify that the default has been set to 'Blocked'.
153 handler.HandleGetDefaultValueForContentType(&getArgs); 164 handler()->HandleGetDefaultValueForContentType(&getArgs);
154 ValidateDefault(false, 3U); 165 ValidateDefault(false, 3U);
155 } 166 }
156 167
157 TEST_F(SiteSettingsHandlerTest, Origins) { 168 TEST_F(SiteSettingsHandlerTest, Origins) {
158 SiteSettingsHandler handler(profile());
159 handler.set_web_ui(web_ui());
160
161 ContentSettingsType type = CONTENT_SETTINGS_TYPE_NOTIFICATIONS; 169 ContentSettingsType type = CONTENT_SETTINGS_TYPE_NOTIFICATIONS;
162 170
163 // Test the JS -> C++ -> JS callback path for configuring origins, by setting 171 // Test the JS -> C++ -> JS callback path for configuring origins, by setting
164 // Google.com to blocked. 172 // Google.com to blocked.
165 base::ListValue setArgs; 173 base::ListValue setArgs;
166 std::string google("http://www.google.com"); 174 std::string google("http://www.google.com");
167 setArgs.Append(new base::StringValue(google)); // Primary pattern. 175 setArgs.Append(new base::StringValue(google)); // Primary pattern.
168 setArgs.Append(new base::StringValue(google)); // Secondary pattern. 176 setArgs.Append(new base::StringValue(google)); // Secondary pattern.
169 setArgs.Append(new base::FundamentalValue(type)); 177 setArgs.Append(new base::FundamentalValue(type));
170 setArgs.Append(new base::StringValue("block")); 178 setArgs.Append(new base::StringValue("block"));
171 handler.HandleSetCategoryPermissionForOrigin(&setArgs); 179 handler()->HandleSetCategoryPermissionForOrigin(&setArgs);
172 EXPECT_EQ(1U, web_ui()->call_data().size()); 180 EXPECT_EQ(1U, web_ui()->call_data().size());
173 181
174 // Verify the change was successful. 182 // Verify the change was successful.
175 base::ListValue listArgs; 183 base::ListValue listArgs;
176 listArgs.Append(new base::StringValue(kCallbackId)); 184 listArgs.Append(new base::StringValue(kCallbackId));
177 listArgs.Append(new base::FundamentalValue(type)); 185 listArgs.Append(new base::FundamentalValue(type));
178 handler.HandleGetExceptionList(&listArgs); 186 handler()->HandleGetExceptionList(&listArgs);
179 ValidateOrigin(google, google, "block", "preference", 2U); 187 ValidateOrigin(google, google, "block", "preference", 2U);
180 188
181 // Reset things back to how they were. 189 // Reset things back to how they were.
182 base::ListValue resetArgs; 190 base::ListValue resetArgs;
183 resetArgs.Append(new base::StringValue(google)); 191 resetArgs.Append(new base::StringValue(google));
184 resetArgs.Append(new base::StringValue(google)); 192 resetArgs.Append(new base::StringValue(google));
185 resetArgs.Append(new base::FundamentalValue(type)); 193 resetArgs.Append(new base::FundamentalValue(type));
186 handler.HandleResetCategoryPermissionForOrigin(&resetArgs); 194 handler()->HandleResetCategoryPermissionForOrigin(&resetArgs);
187 EXPECT_EQ(3U, web_ui()->call_data().size()); 195 EXPECT_EQ(3U, web_ui()->call_data().size());
188 196
189 // Verify the reset was successful. 197 // Verify the reset was successful.
190 handler.HandleGetExceptionList(&listArgs); 198 handler()->HandleGetExceptionList(&listArgs);
191 ValidateNoOrigin(4U); 199 ValidateNoOrigin(4U);
192 } 200 }
193 201
194 TEST_F(SiteSettingsHandlerTest, Patterns) { 202 TEST_F(SiteSettingsHandlerTest, Patterns) {
195 SiteSettingsHandler handler(profile());
196 handler.set_web_ui(web_ui());
197
198 base::ListValue args; 203 base::ListValue args;
199 std::string pattern("[*.]google.com"); 204 std::string pattern("[*.]google.com");
200 args.Append(new base::StringValue(kCallbackId)); 205 args.Append(new base::StringValue(kCallbackId));
201 args.Append(new base::StringValue(pattern)); 206 args.Append(new base::StringValue(pattern));
202 handler.HandleIsPatternValid(&args); 207 handler()->HandleIsPatternValid(&args);
203 ValidatePattern(true, 1U); 208 ValidatePattern(true, 1U);
204 209
205 base::ListValue invalid; 210 base::ListValue invalid;
206 std::string bad_pattern(";"); 211 std::string bad_pattern(";");
207 invalid.Append(new base::StringValue(kCallbackId)); 212 invalid.Append(new base::StringValue(kCallbackId));
208 invalid.Append(new base::StringValue(bad_pattern)); 213 invalid.Append(new base::StringValue(bad_pattern));
209 handler.HandleIsPatternValid(&invalid); 214 handler()->HandleIsPatternValid(&invalid);
210 ValidatePattern(false, 2U); 215 ValidatePattern(false, 2U);
211 } 216 }
212 217
213 } // namespace settings 218 } // namespace settings
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/settings/site_settings_handler.cc ('k') | chrome/browser/ui/webui/settings/system_handler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698