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