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

Side by Side Diff: chrome/browser/ui/webui/policy_ui_browsertest.cc

Issue 12084065: Convert chrome://policy to new WebUI style (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased. Created 7 years, 10 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 | Annotate | Revision Log
« no previous file with comments | « chrome/browser/ui/webui/policy_ui.cc ('k') | chrome/browser/ui/webui/policy_ui_unittest.cc » ('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 (c) 2013 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 <vector>
6
7 #include "base/json/json_reader.h"
8 #include "base/run_loop.h"
9 #include "base/values.h"
10 #include "chrome/browser/policy/browser_policy_connector.h"
11 #include "chrome/browser/policy/mock_configuration_policy_provider.h"
12 #include "chrome/browser/policy/policy_map.h"
13 #include "chrome/browser/policy/policy_types.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/tabs/tab_strip_model.h"
16 #include "chrome/test/base/in_process_browser_test.h"
17 #include "chrome/test/base/ui_test_utils.h"
18 #include "content/public/browser/web_contents.h"
19 #include "content/public/test/browser_test_utils.h"
20 #include "googleurl/src/gurl.h"
21 #include "grit/generated_resources.h"
22 #include "policy/policy_constants.h"
23 #include "testing/gmock/include/gmock/gmock.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25 #include "ui/base/l10n/l10n_util.h"
26
27 using testing::AnyNumber;
28 using testing::Return;
29 using testing::_;
30
31 namespace {
32
33 std::vector<std::string> PopulateExpectedPolicy(
34 const std::string& name,
35 const std::string& value,
36 const policy::PolicyMap::Entry* metadata,
37 bool unknown) {
38 std::vector<std::string> expected_policy;
39
40 // Populate expected scope.
41 if (metadata) {
42 expected_policy.push_back(l10n_util::GetStringUTF8(
43 metadata->scope == policy::POLICY_SCOPE_MACHINE ?
44 IDS_POLICY_SCOPE_DEVICE : IDS_POLICY_SCOPE_USER));
45 } else {
46 expected_policy.push_back("");
47 }
48
49 // Populate expected level.
50 if (metadata) {
51 expected_policy.push_back(l10n_util::GetStringUTF8(
52 metadata->level == policy::POLICY_LEVEL_RECOMMENDED ?
53 IDS_POLICY_LEVEL_RECOMMENDED : IDS_POLICY_LEVEL_MANDATORY));
54 } else {
55 expected_policy.push_back("");
56 }
57
58 // Populate expected policy name.
59 expected_policy.push_back(name);
60
61 // Populate expected policy value.
62 expected_policy.push_back(value);
63
64 // Populate expected status.
65 if (unknown)
66 expected_policy.push_back(l10n_util::GetStringUTF8(IDS_POLICY_UNKNOWN));
67 else if (metadata)
68 expected_policy.push_back(l10n_util::GetStringUTF8(IDS_POLICY_OK));
69 else
70 expected_policy.push_back(l10n_util::GetStringUTF8(IDS_POLICY_UNSET));
71
72 // Populate expected expanded policy value.
73 expected_policy.push_back(value);
74
75 return expected_policy;
76 }
77
78 } // namespace
79
80 class PolicyUITest : public InProcessBrowserTest {
81 public:
82 PolicyUITest();
83 virtual ~PolicyUITest();
84
85 protected:
86 // InProcessBrowserTest implementation.
87 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE;
88
89 void UpdateProviderPolicy(const policy::PolicyMap& policy);
90
91 void VerifyPolicies(const std::vector<std::vector<std::string> > expected);
92
93 private:
94 policy::MockConfigurationPolicyProvider provider_;
95
96 DISALLOW_COPY_AND_ASSIGN(PolicyUITest);
97 };
98
99 PolicyUITest::PolicyUITest() {
100 }
101
102 PolicyUITest::~PolicyUITest() {
103 }
104
105 void PolicyUITest::SetUpInProcessBrowserTestFixture() {
106 EXPECT_CALL(provider_, IsInitializationComplete(_))
107 .WillRepeatedly(Return(true));
108 EXPECT_CALL(provider_, RegisterPolicyNamespace(_, _)).Times(AnyNumber());
109 policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
110 }
111
112 void PolicyUITest::UpdateProviderPolicy(const policy::PolicyMap& policy) {
113 provider_.UpdateChromePolicy(policy);
114 base::RunLoop loop;
115 loop.RunUntilIdle();
116 }
117
118 void PolicyUITest::VerifyPolicies(
119 const std::vector<std::vector<std::string> > expected_policies) {
120 ui_test_utils::NavigateToURL(browser(), GURL("chrome://policy"));
121
122 // Retrieve the text contents of the policy table cells for all policies.
123 const std::string javascript =
124 "var entries = document.querySelectorAll("
125 " 'table#policy-table > tbody');"
126 "var policies = [];"
127 "for (var i = 0; i < entries.length; ++i) {"
128 " var items = entries[i].querySelectorAll('tr > td');"
129 " var values = [];"
130 " for (var j = 0; j < items.length; ++j) {"
131 " var item = items[j];"
132 " var children = item.getElementsByTagName('div');"
133 " if (children.length == 1)"
134 " item = children[0];"
135 " children = item.getElementsByTagName('span');"
136 " if (children.length == 1)"
137 " item = children[0];"
138 " values.push(item.textContent);"
139 " }"
140 " policies.push(values);"
141 "}"
142 "domAutomationController.send(JSON.stringify(policies));";
143 content::WebContents* contents =
144 browser()->tab_strip_model()->GetActiveWebContents();
145 std::string json;
146 ASSERT_TRUE(content::ExecuteScriptAndExtractString(contents, javascript,
147 &json));
148 scoped_ptr<base::Value> value_ptr(base::JSONReader::Read(json));
149 const base::ListValue* actual_policies = NULL;
150 ASSERT_TRUE(value_ptr.get());
151 ASSERT_TRUE(value_ptr->GetAsList(&actual_policies));
152
153 // Verify that the cells contain the expected strings for all policies.
154 ASSERT_EQ(expected_policies.size(), actual_policies->GetSize());
155 for (size_t i = 0; i < expected_policies.size(); ++i) {
156 const std::vector<std::string> expected_policy = expected_policies[i];
157 const base::ListValue* actual_policy;
158 ASSERT_TRUE(actual_policies->GetList(i, &actual_policy));
159 ASSERT_EQ(expected_policy.size(), actual_policy->GetSize());
160 for (size_t j = 0; j < expected_policy.size(); ++j) {
161 std::string value;
162 ASSERT_TRUE(actual_policy->GetString(j, &value));
163 EXPECT_EQ(expected_policy[j], value);
164 }
165 }
166 }
167
168 IN_PROC_BROWSER_TEST_F(PolicyUITest, SendPolicyNames) {
169 // Verifies that the names of known policies are sent to the UI and processed
170 // there correctly by checking that the policy table contains all policies in
171 // the correct order.
172
173 // Expect that the policy table contains all known policies in alphabetical
174 // order and none of the policies have a set value.
175 std::vector<std::vector<std::string> > expected_policies;
176 const policy::PolicyDefinitionList* policies =
177 policy::GetChromePolicyDefinitionList();
178 for (const policy::PolicyDefinitionList::Entry* policy = policies->begin;
179 policy != policies->end; ++policy) {
180 expected_policies.push_back(
181 PopulateExpectedPolicy(policy->name, "", NULL, false));
182 }
183
184 // Retrieve the contents of the policy table from the UI and verify that it
185 // matches the expectation.
186 VerifyPolicies(expected_policies);
187 }
188
189 IN_PROC_BROWSER_TEST_F(PolicyUITest, SendPolicyValues) {
190 // Verifies that policy values are sent to the UI and processed there
191 // correctly by setting the values of four known and one unknown policy and
192 // checking that the policy table contains the policy names, values and
193 // metadata in the correct order.
194 policy::PolicyMap values;
195 std::map<std::string, std::string> expected_values;
196
197 // Set the values of four existing policies.
198 base::ListValue* restore_on_startup_urls = new base::ListValue;
199 restore_on_startup_urls->Append(base::Value::CreateStringValue("aaa"));
200 restore_on_startup_urls->Append(base::Value::CreateStringValue("bbb"));
201 restore_on_startup_urls->Append(base::Value::CreateStringValue("ccc"));
202 values.Set(policy::key::kRestoreOnStartupURLs,
203 policy::POLICY_LEVEL_MANDATORY,
204 policy::POLICY_SCOPE_USER,
205 restore_on_startup_urls);
206 expected_values[policy::key::kRestoreOnStartupURLs] = "aaa,bbb,ccc";
207 values.Set(policy::key::kHomepageLocation,
208 policy::POLICY_LEVEL_MANDATORY,
209 policy::POLICY_SCOPE_MACHINE,
210 base::Value::CreateStringValue("http://google.com"));
211 expected_values[policy::key::kHomepageLocation] = "http://google.com";
212 values.Set(policy::key::kRestoreOnStartup,
213 policy::POLICY_LEVEL_RECOMMENDED,
214 policy::POLICY_SCOPE_USER,
215 base::Value::CreateIntegerValue(4));
216 expected_values[policy::key::kRestoreOnStartup] = "4";
217 values.Set(policy::key::kShowHomeButton,
218 policy::POLICY_LEVEL_RECOMMENDED,
219 policy::POLICY_SCOPE_MACHINE,
220 base::Value::CreateBooleanValue(true));
221 expected_values[policy::key::kShowHomeButton] = "true";
222 // Set the value of a policy that does not exist.
223 const std::string kUnknownPolicy = "NoSuchThing";
224 values.Set(kUnknownPolicy,
225 policy::POLICY_LEVEL_MANDATORY,
226 policy::POLICY_SCOPE_USER,
227 base::Value::CreateBooleanValue(true));
228 expected_values[kUnknownPolicy] = "true";
229 UpdateProviderPolicy(values);
230
231 // Expect that the policy table contains, in order:
232 // * All known policies whose value has been set, in alphabetical order.
233 // * The unknown policy.
234 // * All known policies whose value has not been set, in alphabetical order.
235 std::vector<std::vector<std::string> > expected_policies;
236 size_t first_unset_position = 0;
237 const policy::PolicyDefinitionList* policies =
238 policy::GetChromePolicyDefinitionList();
239 for (const policy::PolicyDefinitionList::Entry* policy = policies->begin;
240 policy != policies->end; ++policy) {
241 std::map<std::string, std::string>::const_iterator it =
242 expected_values.find(policy->name);
243 const std::string value = it == expected_values.end() ? "" : it->second;
244 const policy::PolicyMap::Entry* metadata = values.Get(policy->name);
245 expected_policies.insert(
246 metadata ? expected_policies.begin() + first_unset_position++ :
247 expected_policies.end(),
248 PopulateExpectedPolicy(policy->name, value, metadata, false));
249 }
250 expected_policies.insert(
251 expected_policies.begin() + first_unset_position++,
252 PopulateExpectedPolicy(kUnknownPolicy,
253 expected_values[kUnknownPolicy],
254 values.Get(kUnknownPolicy),
255 true));
256
257 // Retrieve the contents of the policy table from the UI and verify that it
258 // matches the expectation.
259 VerifyPolicies(expected_policies);
260 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/policy_ui.cc ('k') | chrome/browser/ui/webui/policy_ui_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698