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

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

Issue 1845253002: Add unit test for Site Settings Handler. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address feedback Created 4 years, 8 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
« no previous file with comments | « chrome/browser/ui/webui/settings/site_settings_handler.h ('k') | chrome/chrome_tests_unit.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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
19 const char kCallbackId[] = "test-callback-id";
20
21 }
22
23 namespace settings {
24
25 class SiteSettingsHandlerTest : public testing::Test {
26 public:
27 SiteSettingsHandlerTest() {}
28
29 Profile* profile() { return &profile_; }
30 content::TestWebUI* web_ui() { return &web_ui_; }
31
32 void ValidateDefault(bool expected_default, size_t expected_total_calls) {
33 EXPECT_EQ(expected_total_calls, web_ui()->call_data().size());
34
35 const content::TestWebUI::CallData& data = *web_ui()->call_data().back();
36 EXPECT_EQ("cr.webUIResponse", data.function_name());
37
38 std::string callback_id;
39 ASSERT_TRUE(data.arg1()->GetAsString(&callback_id));
40 EXPECT_EQ(kCallbackId, callback_id);
41
42 bool success = false;
43 ASSERT_TRUE(data.arg2()->GetAsBoolean(&success));
44 ASSERT_TRUE(success);
45
46 bool enabled;
47 ASSERT_TRUE(data.arg3()->GetAsBoolean(&enabled));
48 EXPECT_EQ(expected_default, enabled);
49 }
50
51 void ValidateOrigin(
52 const std::string& expected_origin,
53 const std::string& expected_embedding,
54 const std::string& expected_setting,
55 const std::string& expected_source,
56 size_t expected_total_calls) {
57 EXPECT_EQ(expected_total_calls, web_ui()->call_data().size());
58
59 const content::TestWebUI::CallData& data = *web_ui()->call_data().back();
60 EXPECT_EQ("cr.webUIResponse", data.function_name());
61
62 std::string callback_id;
63 ASSERT_TRUE(data.arg1()->GetAsString(&callback_id));
64 EXPECT_EQ(kCallbackId, callback_id);
65 bool success = false;
66 ASSERT_TRUE(data.arg2()->GetAsBoolean(&success));
67 ASSERT_TRUE(success);
68
69 const base::ListValue* exceptions;
70 ASSERT_TRUE(data.arg3()->GetAsList(&exceptions));
71 EXPECT_EQ(1U, exceptions->GetSize());
72 const base::DictionaryValue* exception;
73 ASSERT_TRUE(exceptions->GetDictionary(0, &exception));
74 std::string origin, embedding_origin, setting, source;
75 ASSERT_TRUE(exception->GetString(site_settings::kOrigin, &origin));
76 ASSERT_EQ(expected_origin, origin);
77 ASSERT_TRUE(exception->GetString(
78 site_settings::kEmbeddingOrigin, &embedding_origin));
79 ASSERT_EQ(expected_embedding, embedding_origin);
80 ASSERT_TRUE(exception->GetString(site_settings::kSetting, &setting));
81 ASSERT_EQ(expected_setting, setting);
82 ASSERT_TRUE(exception->GetString(site_settings::kSource, &source));
83 ASSERT_EQ(expected_source, source);
84 }
85
86 void ValidateNoOrigin(size_t expected_total_calls) {
87 EXPECT_EQ(expected_total_calls, web_ui()->call_data().size());
88
89 const content::TestWebUI::CallData& data = *web_ui()->call_data().back();
90 EXPECT_EQ("cr.webUIResponse", data.function_name());
91
92 std::string callback_id;
93 ASSERT_TRUE(data.arg1()->GetAsString(&callback_id));
94 EXPECT_EQ(kCallbackId, callback_id);
95
96 bool success = false;
97 ASSERT_TRUE(data.arg2()->GetAsBoolean(&success));
98 ASSERT_TRUE(success);
99
100 const base::ListValue* exceptions;
101 ASSERT_TRUE(data.arg3()->GetAsList(&exceptions));
102 EXPECT_EQ(0U, exceptions->GetSize());
103 }
104
105 private:
106 content::TestBrowserThreadBundle thread_bundle_;
107 TestingProfile profile_;
108 content::TestWebUI web_ui_;
109 };
110
111 TEST_F(SiteSettingsHandlerTest, GetAndSetDefault) {
112 SiteSettingsHandler handler(profile());
113 handler.set_web_ui(web_ui());
114
115 ContentSettingsType type = CONTENT_SETTINGS_TYPE_NOTIFICATIONS;
116
117 // Test the JS -> C++ -> JS callback path for getting and setting defaults.
118 base::ListValue getArgs;
119 getArgs.Append(new base::StringValue(kCallbackId));
120 getArgs.Append(new base::FundamentalValue(type));
121 handler.HandleGetDefaultValueForContentType(&getArgs);
122 ValidateDefault(true, 1U);
123
124 // Set the default to 'Blocked'.
125 base::ListValue setArgs;
126 setArgs.Append(new base::FundamentalValue(type));
127 setArgs.Append(new base::FundamentalValue(CONTENT_SETTING_BLOCK));
128 handler.HandleSetDefaultValueForContentType(&setArgs);
129
130 EXPECT_EQ(2U, web_ui()->call_data().size());
131
132 // Verify that the default has been set to 'Blocked'.
133 handler.HandleGetDefaultValueForContentType(&getArgs);
134 ValidateDefault(false, 3U);
135 }
136
137 TEST_F(SiteSettingsHandlerTest, Origins) {
138 SiteSettingsHandler handler(profile());
139 handler.set_web_ui(web_ui());
140
141 ContentSettingsType type = CONTENT_SETTINGS_TYPE_NOTIFICATIONS;
142
143 // Test the JS -> C++ -> JS callback path for configuring origins, by setting
144 // Google.com to blocked.
145 base::ListValue setArgs;
146 std::string google("http://www.google.com");
147 setArgs.Append(new base::StringValue(google)); // Primary pattern.
148 setArgs.Append(new base::StringValue(google)); // Secondary pattern.
149 setArgs.Append(new base::FundamentalValue(type));
150 setArgs.Append(new base::FundamentalValue(CONTENT_SETTING_BLOCK));
151 handler.HandleSetCategoryPermissionForOrigin(&setArgs);
152 EXPECT_EQ(1U, web_ui()->call_data().size());
153
154 // Verify the change was successful.
155 base::ListValue listArgs;
156 listArgs.Append(new base::StringValue(kCallbackId));
157 listArgs.Append(new base::FundamentalValue(type));
158 handler.HandleGetExceptionList(&listArgs);
159 ValidateOrigin(google, google, "block", "preference", 2U);
160
161 // Reset things back to how they were.
162 base::ListValue resetArgs;
163 resetArgs.Append(new base::StringValue(google));
164 resetArgs.Append(new base::StringValue(google));
165 resetArgs.Append(new base::FundamentalValue(type));
166 handler.HandleResetCategoryPermissionForOrigin(&resetArgs);
167 EXPECT_EQ(3U, web_ui()->call_data().size());
168
169 // Verify the reset was successful.
170 handler.HandleGetExceptionList(&listArgs);
171 ValidateNoOrigin(4U);
172 }
173
174 } // namespace settings
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/settings/site_settings_handler.h ('k') | chrome/chrome_tests_unit.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698