OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <algorithm> | 5 #include <algorithm> |
6 #include <map> | 6 #include <map> |
7 #include <sstream> | 7 #include <sstream> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 "chrome://settings-frame/passwords", | 46 "chrome://settings-frame/passwords", |
47 "chrome://settings-frame/autofill", | 47 "chrome://settings-frame/autofill", |
48 "chrome://settings-frame/content", | 48 "chrome://settings-frame/content", |
49 "chrome://settings-frame/homePageOverlay", | 49 "chrome://settings-frame/homePageOverlay", |
50 "chrome://settings-frame/languages", | 50 "chrome://settings-frame/languages", |
51 #if defined(OS_CHROMEOS) | 51 #if defined(OS_CHROMEOS) |
52 "chrome://settings-frame/accounts", | 52 "chrome://settings-frame/accounts", |
53 #endif | 53 #endif |
54 }; | 54 }; |
55 | 55 |
56 // Contains the details of one test case verifying the behavior of controlled | 56 // Contains the details of a single test case verifying that the controlled |
57 // setting indicators in the settings UI for a policy, part of the data loaded | 57 // setting indicators for a pref affected by a policy work correctly. This is |
58 // from chrome/test/data/policy/policy_test_cases.json. | 58 // part of the data loaded from chrome/test/data/policy/policy_test_cases.json. |
59 class IndicatorTestCase { | 59 class IndicatorTestCase { |
60 public: | 60 public: |
61 IndicatorTestCase(const base::DictionaryValue& policy, | 61 IndicatorTestCase(const base::DictionaryValue& policy, |
62 const std::string& value, | 62 const std::string& value, |
63 bool readonly) | 63 bool readonly) |
64 : policy_(policy.DeepCopy()), value_(value), readonly_(readonly) {} | 64 : policy_(policy.DeepCopy()), value_(value), readonly_(readonly) {} |
65 ~IndicatorTestCase() {} | 65 ~IndicatorTestCase() {} |
66 | 66 |
67 const base::DictionaryValue& policy() const { return *policy_; } | 67 const base::DictionaryValue& policy() const { return *policy_; } |
| 68 |
68 const std::string& value() const { return value_; } | 69 const std::string& value() const { return value_; } |
| 70 |
69 bool readonly() const { return readonly_; } | 71 bool readonly() const { return readonly_; } |
70 | 72 |
71 private: | 73 private: |
72 scoped_ptr<base::DictionaryValue> policy_; | 74 scoped_ptr<base::DictionaryValue> policy_; |
73 std::string value_; | 75 std::string value_; |
74 bool readonly_; | 76 bool readonly_; |
75 | 77 |
76 DISALLOW_COPY_AND_ASSIGN(IndicatorTestCase); | 78 DISALLOW_COPY_AND_ASSIGN(IndicatorTestCase); |
77 }; | 79 }; |
78 | 80 |
79 // Contains the testing details for a single policy, loaded from | 81 // Contains the testing details for a single pref affected by a policy. This is |
80 // chrome/test/data/policy/policy_test_cases.json. | 82 // part of the data loaded from chrome/test/data/policy/policy_test_cases.json. |
81 class PolicyTestCase { | 83 class PrefMapping { |
82 public: | 84 public: |
83 explicit PolicyTestCase(const std::string& name) | 85 PrefMapping(const std::string& pref, |
84 : name_(name), | 86 bool is_local_state, |
85 can_be_recommended_(false), | 87 const std::string& indicator_test_setup_js, |
86 is_local_state_(false), | 88 const std::string& indicator_selector) |
87 official_only_(false) {} | 89 : pref_(pref), |
88 ~PolicyTestCase() {} | 90 is_local_state_(is_local_state), |
| 91 indicator_selector_(indicator_selector) { |
| 92 indicator_test_setup_js_ = ASCIIToWide(indicator_test_setup_js); |
| 93 } |
| 94 ~PrefMapping() {} |
89 | 95 |
90 const std::string& name() const { return name_; } | 96 const std::string& pref() const { return pref_; } |
91 | 97 |
92 void set_pref(const std::string& pref) { pref_ = pref; } | 98 bool is_local_state() const { return is_local_state_; } |
93 const std::string& pref() const { return pref_; } | |
94 const char* pref_name() const { return pref_.c_str(); } | |
95 | |
96 bool can_be_recommended() const { return can_be_recommended_; } | |
97 void set_can_be_recommended(bool can_be_recommended) { | |
98 can_be_recommended_ = can_be_recommended; | |
99 } | |
100 | |
101 const PolicyMap& test_policy() const { return test_policy_; } | |
102 void set_test_policy(const PolicyMap& policy) { | |
103 test_policy_.CopyFrom(policy); | |
104 } | |
105 | 99 |
106 const std::wstring& indicator_test_setup_js() const { | 100 const std::wstring& indicator_test_setup_js() const { |
107 return indicator_test_setup_js_; | 101 return indicator_test_setup_js_; |
108 } | 102 } |
109 void set_indicator_test_setup_js(const std::string& indicator_test_setup_js) { | |
110 indicator_test_setup_js_ = ASCIIToWide(indicator_test_setup_js); | |
111 } | |
112 | 103 |
113 const std::string& indicator_selector() const { return indicator_selector_; } | 104 const std::string& indicator_selector() const { |
114 void set_indicator_selector(const std::string& indicator_selector) { | 105 return indicator_selector_; |
115 indicator_selector_ = indicator_selector; | |
116 } | 106 } |
117 | 107 |
118 const ScopedVector<IndicatorTestCase>& indicator_test_cases() const { | 108 const ScopedVector<IndicatorTestCase>& indicator_test_cases() const { |
119 return indicator_test_cases_; | 109 return indicator_test_cases_; |
120 } | 110 } |
121 void AddIndicatorTestCase(IndicatorTestCase* test_case) { | 111 void AddIndicatorTestCase(IndicatorTestCase* test_case) { |
122 indicator_test_cases_.push_back(test_case); | 112 indicator_test_cases_.push_back(test_case); |
123 } | 113 } |
124 | 114 |
125 const std::vector<GURL>& settings_pages() const { return settings_pages_; } | 115 private: |
126 void AddSettingsPage(const GURL& url) { settings_pages_.push_back(url); } | 116 std::string pref_; |
| 117 bool is_local_state_; |
| 118 std::wstring indicator_test_setup_js_; |
| 119 std::string indicator_selector_; |
| 120 ScopedVector<IndicatorTestCase> indicator_test_cases_; |
| 121 |
| 122 DISALLOW_COPY_AND_ASSIGN(PrefMapping); |
| 123 }; |
| 124 |
| 125 // Contains the testing details for a single policy. This is part of the data |
| 126 // loaded from chrome/test/data/policy/policy_test_cases.json. |
| 127 class PolicyTestCase { |
| 128 public: |
| 129 PolicyTestCase(const std::string& name, |
| 130 bool is_official_only, |
| 131 bool can_be_recommended) |
| 132 : name_(name), |
| 133 is_official_only_(is_official_only), |
| 134 can_be_recommended_(can_be_recommended) {} |
| 135 ~PolicyTestCase() {} |
| 136 |
| 137 const std::string& name() const { return name_; } |
| 138 |
| 139 bool is_official_only() const { return is_official_only_; } |
| 140 |
| 141 bool can_be_recommended() const { return can_be_recommended_; } |
127 | 142 |
128 bool IsOsSupported() const { | 143 bool IsOsSupported() const { |
129 #if defined(OS_WIN) | 144 #if defined(OS_WIN) |
130 const std::string os("win"); | 145 const std::string os("win"); |
131 #elif defined(OS_MACOSX) | 146 #elif defined(OS_MACOSX) |
132 const std::string os("mac"); | 147 const std::string os("mac"); |
133 #elif defined(OS_CHROMEOS) | 148 #elif defined(OS_CHROMEOS) |
134 const std::string os("chromeos"); | 149 const std::string os("chromeos"); |
135 #elif defined(OS_LINUX) | 150 #elif defined(OS_LINUX) |
136 const std::string os("linux"); | 151 const std::string os("linux"); |
137 #else | 152 #else |
138 #error "Unknown platform" | 153 #error "Unknown platform" |
139 #endif | 154 #endif |
140 return std::find(supported_os_.begin(), supported_os_.end(), os) != | 155 return std::find(supported_os_.begin(), supported_os_.end(), os) != |
141 supported_os_.end(); | 156 supported_os_.end(); |
142 } | 157 } |
143 | |
144 void AddSupportedOs(const std::string& os) { supported_os_.push_back(os); } | 158 void AddSupportedOs(const std::string& os) { supported_os_.push_back(os); } |
145 | 159 |
146 bool is_local_state() const { return is_local_state_; } | |
147 void set_local_state(bool flag) { is_local_state_ = flag; } | |
148 | |
149 bool is_official_only() const { return official_only_; } | |
150 void set_official_only(bool flag) { official_only_ = flag; } | |
151 | |
152 bool IsSupported() const { | 160 bool IsSupported() const { |
153 #if !defined(OFFICIAL_BUILD) | 161 #if !defined(OFFICIAL_BUILD) |
154 if (is_official_only()) | 162 if (is_official_only()) |
155 return false; | 163 return false; |
156 #endif | 164 #endif |
157 return IsOsSupported(); | 165 return IsOsSupported(); |
158 } | 166 } |
159 | 167 |
| 168 const PolicyMap& test_policy() const { return test_policy_; } |
| 169 void SetTestPolicy(const PolicyMap& policy) { |
| 170 test_policy_.CopyFrom(policy); |
| 171 } |
| 172 |
| 173 const std::vector<GURL>& settings_pages() const { return settings_pages_; } |
| 174 void AddSettingsPage(const GURL& url) { settings_pages_.push_back(url); } |
| 175 |
| 176 const ScopedVector<PrefMapping>& pref_mappings() const { |
| 177 return pref_mappings_; |
| 178 } |
| 179 void AddPrefMapping(PrefMapping* pref_mapping) { |
| 180 pref_mappings_.push_back(pref_mapping); |
| 181 } |
| 182 |
160 private: | 183 private: |
161 std::string name_; | 184 std::string name_; |
162 std::string pref_; | 185 bool is_official_only_; |
163 bool can_be_recommended_; | 186 bool can_be_recommended_; |
| 187 std::vector<std::string> supported_os_; |
164 PolicyMap test_policy_; | 188 PolicyMap test_policy_; |
165 std::wstring indicator_test_setup_js_; | |
166 std::string indicator_selector_; | |
167 ScopedVector<IndicatorTestCase> indicator_test_cases_; | |
168 std::vector<GURL> settings_pages_; | 189 std::vector<GURL> settings_pages_; |
169 std::vector<std::string> supported_os_; | 190 ScopedVector<PrefMapping> pref_mappings_; |
170 bool is_local_state_; | |
171 bool official_only_; | |
172 | 191 |
173 DISALLOW_COPY_AND_ASSIGN(PolicyTestCase); | 192 DISALLOW_COPY_AND_ASSIGN(PolicyTestCase); |
174 }; | 193 }; |
175 | 194 |
176 // Parses all the test cases and makes then available in a map. | 195 // Parses all policy test cases and makes then available in a map. |
177 class TestCases { | 196 class PolicyTestCases { |
178 public: | 197 public: |
179 typedef std::map<std::string, PolicyTestCase*> TestCaseMap; | 198 typedef std::map<std::string, PolicyTestCase*> PolicyTestCaseMap; |
180 typedef TestCaseMap::const_iterator iterator; | 199 typedef PolicyTestCaseMap::const_iterator iterator; |
181 | 200 |
182 TestCases() { | 201 PolicyTestCases() { |
183 test_cases_ = new std::map<std::string, PolicyTestCase*>(); | 202 policy_test_cases_ = new std::map<std::string, PolicyTestCase*>(); |
184 | 203 |
185 FilePath path = ui_test_utils::GetTestFilePath( | 204 FilePath path = ui_test_utils::GetTestFilePath( |
186 FilePath(FILE_PATH_LITERAL("policy")), | 205 FilePath(FILE_PATH_LITERAL("policy")), |
187 FilePath(FILE_PATH_LITERAL("policy_test_cases.json"))); | 206 FilePath(FILE_PATH_LITERAL("policy_test_cases.json"))); |
188 std::string json; | 207 std::string json; |
189 if (!file_util::ReadFileToString(path, &json)) { | 208 if (!file_util::ReadFileToString(path, &json)) { |
190 ADD_FAILURE(); | 209 ADD_FAILURE(); |
191 return; | 210 return; |
192 } | 211 } |
193 int error_code = -1; | 212 int error_code = -1; |
194 std::string error_string; | 213 std::string error_string; |
195 base::DictionaryValue* dict = NULL; | 214 base::DictionaryValue* dict = NULL; |
196 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError( | 215 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError( |
197 json, base::JSON_PARSE_RFC, &error_code, &error_string)); | 216 json, base::JSON_PARSE_RFC, &error_code, &error_string)); |
198 if (!value.get() || !value->GetAsDictionary(&dict)) { | 217 if (!value.get() || !value->GetAsDictionary(&dict)) { |
199 ADD_FAILURE() << "Error parsing policy_test_cases.json: " << error_string; | 218 ADD_FAILURE() << "Error parsing policy_test_cases.json: " << error_string; |
200 return; | 219 return; |
201 } | 220 } |
202 const PolicyDefinitionList* list = GetChromePolicyDefinitionList(); | 221 const PolicyDefinitionList* list = GetChromePolicyDefinitionList(); |
203 for (const PolicyDefinitionList::Entry* policy = list->begin; | 222 for (const PolicyDefinitionList::Entry* policy = list->begin; |
204 policy != list->end; ++policy) { | 223 policy != list->end; ++policy) { |
205 PolicyTestCase* test_case = GetTestCase(dict, policy->name); | 224 PolicyTestCase* policy_test_case = GetPolicyTestCase(dict, policy->name); |
206 if (test_case) | 225 if (policy_test_case) |
207 (*test_cases_)[policy->name] = test_case; | 226 (*policy_test_cases_)[policy->name] = policy_test_case; |
208 } | 227 } |
209 } | 228 } |
210 | 229 |
211 ~TestCases() { | 230 ~PolicyTestCases() { |
212 STLDeleteValues(test_cases_); | 231 STLDeleteValues(policy_test_cases_); |
213 delete test_cases_; | 232 delete policy_test_cases_; |
214 } | 233 } |
215 | 234 |
216 const PolicyTestCase* Get(const std::string& name) { | 235 const PolicyTestCase* Get(const std::string& name) { |
217 iterator it = test_cases_->find(name); | 236 iterator it = policy_test_cases_->find(name); |
218 return it == end() ? NULL : it->second; | 237 return it == end() ? NULL : it->second; |
219 } | 238 } |
220 | 239 |
221 const TestCaseMap& map() const { return *test_cases_; } | 240 const PolicyTestCaseMap& map() const { return *policy_test_cases_; } |
222 iterator begin() const { return test_cases_->begin(); } | 241 iterator begin() const { return policy_test_cases_->begin(); } |
223 iterator end() const { return test_cases_->end(); } | 242 iterator end() const { return policy_test_cases_->end(); } |
224 | 243 |
225 private: | 244 private: |
226 PolicyTestCase* GetTestCase(const base::DictionaryValue* tests, | 245 PolicyTestCase* GetPolicyTestCase(const base::DictionaryValue* tests, |
227 const std::string& name) { | 246 const std::string& name) { |
228 const base::DictionaryValue* dict = NULL; | 247 const base::DictionaryValue* policy_test_dict = NULL; |
229 if (!tests->GetDictionary(name, &dict)) | 248 if (!tests->GetDictionary(name, &policy_test_dict)) |
230 return NULL; | 249 return NULL; |
231 PolicyTestCase* test_case = new PolicyTestCase(name); | 250 bool is_official_only = false; |
232 std::string pref; | 251 policy_test_dict->GetBoolean("official_only", &is_official_only); |
233 if (dict->GetString("pref", &pref)) | 252 bool can_be_recommended = false; |
234 test_case->set_pref(pref); | 253 policy_test_dict->GetBoolean("can_be_recommended", &can_be_recommended); |
235 bool flag = false; | 254 PolicyTestCase* policy_test_case = |
236 if (dict->GetBoolean("can_be_recommended", &flag)) | 255 new PolicyTestCase(name, is_official_only, can_be_recommended); |
237 test_case->set_can_be_recommended(flag); | 256 const base::ListValue* os_list = NULL; |
238 const base::DictionaryValue* policy_dict = NULL; | 257 if (policy_test_dict->GetList("os", &os_list)) { |
239 if (dict->GetDictionary("test_policy", &policy_dict)) { | 258 for (size_t i = 0; i < os_list->GetSize(); ++i) { |
240 PolicyMap policies; | 259 std::string os; |
241 policies.LoadFrom(policy_dict, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); | 260 if (os_list->GetString(i, &os)) |
242 test_case->set_test_policy(policies); | 261 policy_test_case->AddSupportedOs(os); |
243 } | |
244 std::string indicator_test_setup_js; | |
245 if (dict->GetString("indicator_test_setup_js", &indicator_test_setup_js)) | |
246 test_case->set_indicator_test_setup_js(indicator_test_setup_js); | |
247 std::string indicator_selector; | |
248 if (dict->GetString("indicator_selector", &indicator_selector)) | |
249 test_case->set_indicator_selector(indicator_selector); | |
250 const base::ListValue* indicator_tests = NULL; | |
251 if (dict->GetList("indicator_tests", &indicator_tests)) { | |
252 for (size_t i = 0; i < indicator_tests->GetSize(); ++i) { | |
253 const base::DictionaryValue* indicator_test_dict = NULL; | |
254 const base::DictionaryValue* policy = NULL; | |
255 if (!indicator_tests->GetDictionary(i, &indicator_test_dict) || | |
256 !indicator_test_dict->GetDictionary("policy", &policy)) { | |
257 continue; | |
258 } | |
259 std::string value; | |
260 indicator_test_dict->GetString("value", &value); | |
261 bool readonly = false; | |
262 indicator_test_dict->GetBoolean("readonly", &readonly); | |
263 test_case->AddIndicatorTestCase( | |
264 new IndicatorTestCase(*policy, value, readonly)); | |
265 } | 262 } |
266 } | 263 } |
| 264 const base::DictionaryValue* policy_dict = NULL; |
| 265 if (policy_test_dict->GetDictionary("test_policy", &policy_dict)) { |
| 266 PolicyMap policy; |
| 267 policy.LoadFrom(policy_dict, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); |
| 268 policy_test_case->SetTestPolicy(policy); |
| 269 } |
267 const base::ListValue* settings_pages = NULL; | 270 const base::ListValue* settings_pages = NULL; |
268 if (dict->GetList("settings_pages", &settings_pages)) { | 271 if (policy_test_dict->GetList("settings_pages", &settings_pages)) { |
269 for (size_t i = 0; i < settings_pages->GetSize(); ++i) { | 272 for (size_t i = 0; i < settings_pages->GetSize(); ++i) { |
270 std::string page; | 273 std::string page; |
271 if (settings_pages->GetString(i, &page)) | 274 if (settings_pages->GetString(i, &page)) |
272 test_case->AddSettingsPage(GURL(page)); | 275 policy_test_case->AddSettingsPage(GURL(page)); |
273 } | 276 } |
274 } | 277 } |
275 const base::ListValue* supported_os = NULL; | 278 const base::ListValue* pref_mappings = NULL; |
276 if (dict->GetList("os", &supported_os)) { | 279 if (policy_test_dict->GetList("pref_mappings", &pref_mappings)) { |
277 for (size_t i = 0; i < supported_os->GetSize(); ++i) { | 280 for (size_t i = 0; i < pref_mappings->GetSize(); ++i) { |
278 std::string os; | 281 const base::DictionaryValue* pref_mapping_dict = NULL; |
279 if (supported_os->GetString(i, &os)) | 282 std::string pref; |
280 test_case->AddSupportedOs(os); | 283 if (!pref_mappings->GetDictionary(i, &pref_mapping_dict) || |
| 284 !pref_mapping_dict->GetString("pref", &pref)) { |
| 285 ADD_FAILURE() << "Malformed pref_mappings entry in " |
| 286 << "policy_test_cases.json."; |
| 287 continue; |
| 288 } |
| 289 bool is_local_state = false; |
| 290 pref_mapping_dict->GetBoolean("local_state", &is_local_state); |
| 291 std::string indicator_test_setup_js; |
| 292 pref_mapping_dict->GetString("indicator_test_setup_js", |
| 293 &indicator_test_setup_js); |
| 294 std::string indicator_selector; |
| 295 pref_mapping_dict->GetString("indicator_selector", &indicator_selector); |
| 296 PrefMapping* pref_mapping = new PrefMapping( |
| 297 pref, is_local_state, indicator_test_setup_js, indicator_selector); |
| 298 const base::ListValue* indicator_tests = NULL; |
| 299 if (pref_mapping_dict->GetList("indicator_tests", &indicator_tests)) { |
| 300 for (size_t i = 0; i < indicator_tests->GetSize(); ++i) { |
| 301 const base::DictionaryValue* indicator_test_dict = NULL; |
| 302 const base::DictionaryValue* policy = NULL; |
| 303 if (!indicator_tests->GetDictionary(i, &indicator_test_dict) || |
| 304 !indicator_test_dict->GetDictionary("policy", &policy)) { |
| 305 ADD_FAILURE() << "Malformed indicator_tests entry in " |
| 306 << "policy_test_cases.json."; |
| 307 continue; |
| 308 } |
| 309 std::string value; |
| 310 indicator_test_dict->GetString("value", &value); |
| 311 bool readonly = false; |
| 312 indicator_test_dict->GetBoolean("readonly", &readonly); |
| 313 pref_mapping->AddIndicatorTestCase( |
| 314 new IndicatorTestCase(*policy, value, readonly)); |
| 315 } |
| 316 } |
| 317 policy_test_case->AddPrefMapping(pref_mapping); |
281 } | 318 } |
282 } | 319 } |
283 if (dict->GetBoolean("local_state", &flag)) | 320 return policy_test_case; |
284 test_case->set_local_state(flag); | |
285 if (dict->GetBoolean("official_only", &flag)) | |
286 test_case->set_official_only(flag); | |
287 return test_case; | |
288 } | 321 } |
289 | 322 |
290 TestCaseMap* test_cases_; | 323 PolicyTestCaseMap* policy_test_cases_; |
291 | 324 |
292 DISALLOW_COPY_AND_ASSIGN(TestCases); | 325 DISALLOW_COPY_AND_ASSIGN(PolicyTestCases); |
293 }; | 326 }; |
294 | 327 |
295 bool IsBannerVisible(Browser* browser) { | 328 bool IsBannerVisible(Browser* browser) { |
296 content::WebContents* contents = chrome::GetActiveWebContents(browser); | 329 content::WebContents* contents = chrome::GetActiveWebContents(browser); |
297 bool result = false; | 330 bool result = false; |
298 EXPECT_TRUE(content::ExecuteJavaScriptAndExtractBool( | 331 EXPECT_TRUE(content::ExecuteJavaScriptAndExtractBool( |
299 contents->GetRenderViewHost(), | 332 contents->GetRenderViewHost(), |
300 std::wstring(), | 333 std::wstring(), |
301 L"var visible = false;" | 334 L"var visible = false;" |
302 L"var banners = document.querySelectorAll('.page-banner');" | 335 L"var banners = document.querySelectorAll('.page-banner');" |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 | 414 |
382 // Base class for tests that change policies. | 415 // Base class for tests that change policies. |
383 class PolicyBaseTest : public InProcessBrowserTest { | 416 class PolicyBaseTest : public InProcessBrowserTest { |
384 protected: | 417 protected: |
385 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { | 418 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { |
386 EXPECT_CALL(provider_, IsInitializationComplete()) | 419 EXPECT_CALL(provider_, IsInitializationComplete()) |
387 .WillRepeatedly(Return(true)); | 420 .WillRepeatedly(Return(true)); |
388 BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_); | 421 BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_); |
389 } | 422 } |
390 | 423 |
391 TestCases test_cases_; | 424 PolicyTestCases policy_test_cases_; |
392 MockConfigurationPolicyProvider provider_; | 425 MockConfigurationPolicyProvider provider_; |
393 }; | 426 }; |
394 | 427 |
395 // A class of tests that change policy and don't need parameters. | 428 // A class of tests that change policy and don't need parameters. |
396 class PolicyPrefsBannerTest : public PolicyBaseTest {}; | 429 class PolicyPrefsBannerTest : public PolicyBaseTest {}; |
397 | 430 |
398 // A class of tests that change policy and are parameterized with a policy | 431 // A class of tests that change policy and are parameterized with a policy |
399 // definition. | 432 // definition. |
400 class PolicyPrefsTest | 433 class PolicyPrefsTest |
401 : public PolicyBaseTest, | 434 : public PolicyBaseTest, |
402 public testing::WithParamInterface<PolicyDefinitionList::Entry> {}; | 435 public testing::WithParamInterface<PolicyDefinitionList::Entry> {}; |
403 | 436 |
404 TEST(PolicyPrefsTest, AllPoliciesHaveATestCase) { | 437 TEST(PolicyPrefsTest, AllPoliciesHaveATestCase) { |
405 // Verifies that all known policies have a test case in the JSON file. | 438 // Verifies that all known policies have a test case in the JSON file. |
406 // This test fails when a policy is added to | 439 // This test fails when a policy is added to |
407 // chrome/app/policy/policy_templates.json but a test case is not added to | 440 // chrome/app/policy/policy_templates.json but a test case is not added to |
408 // chrome/test/data/policy/policy_test_cases.json. | 441 // chrome/test/data/policy/policy_test_cases.json. |
409 TestCases test_cases; | 442 PolicyTestCases policy_test_cases; |
410 const PolicyDefinitionList* list = GetChromePolicyDefinitionList(); | 443 const PolicyDefinitionList* list = GetChromePolicyDefinitionList(); |
411 for (const PolicyDefinitionList::Entry* policy = list->begin; | 444 for (const PolicyDefinitionList::Entry* policy = list->begin; |
412 policy != list->end; ++policy) { | 445 policy != list->end; ++policy) { |
413 EXPECT_TRUE(ContainsKey(test_cases.map(), policy->name)) | 446 EXPECT_TRUE(ContainsKey(policy_test_cases.map(), policy->name)) |
414 << "Missing policy test case for: " << policy->name; | 447 << "Missing policy test case for: " << policy->name; |
415 } | 448 } |
416 } | 449 } |
417 | 450 |
418 IN_PROC_BROWSER_TEST_P(PolicyPrefsSettingsBannerTest, NoPoliciesNoBanner) { | 451 IN_PROC_BROWSER_TEST_P(PolicyPrefsSettingsBannerTest, NoPoliciesNoBanner) { |
419 // Verifies that the banner isn't shown in the settings UI when no policies | 452 // Verifies that the banner isn't shown in the settings UI when no policies |
420 // are set. | 453 // are set. |
421 ui_test_utils::NavigateToURL(browser(), GURL(GetParam())); | 454 ui_test_utils::NavigateToURL(browser(), GURL(GetParam())); |
422 EXPECT_FALSE(IsBannerVisible(browser())); | 455 EXPECT_FALSE(IsBannerVisible(browser())); |
423 } | 456 } |
424 | 457 |
425 INSTANTIATE_TEST_CASE_P(PolicyPrefsSettingsBannerTestInstance, | 458 INSTANTIATE_TEST_CASE_P(PolicyPrefsSettingsBannerTestInstance, |
426 PolicyPrefsSettingsBannerTest, | 459 PolicyPrefsSettingsBannerTest, |
427 testing::ValuesIn(kSettingsPages)); | 460 testing::ValuesIn(kSettingsPages)); |
428 | 461 |
429 IN_PROC_BROWSER_TEST_F(PolicyPrefsBannerTest, TogglePolicyTogglesBanner) { | 462 IN_PROC_BROWSER_TEST_F(PolicyPrefsBannerTest, TogglePolicyTogglesBanner) { |
430 // Verifies that the banner appears and disappears as policies are added and | 463 // Verifies that the banner appears and disappears as policies are added and |
431 // removed. | 464 // removed. |
432 // |test_case| is just a particular policy that should trigger the banner | 465 // |test_case| is just a particular policy that should trigger the banner |
433 // on the main settings page. | 466 // on the main settings page. |
434 const PolicyTestCase* test_case = test_cases_.Get("ShowHomeButton"); | 467 const PolicyTestCase* test_case = policy_test_cases_.Get("ShowHomeButton"); |
435 ASSERT_TRUE(test_case); | 468 ASSERT_TRUE(test_case); |
436 // No banner by default. | 469 // No banner by default. |
437 ui_test_utils::NavigateToURL(browser(), GURL(kSettingsPages[0])); | 470 ui_test_utils::NavigateToURL(browser(), GURL(kSettingsPages[0])); |
438 EXPECT_FALSE(IsBannerVisible(browser())); | 471 EXPECT_FALSE(IsBannerVisible(browser())); |
439 // Adding a policy makes the banner show up. | 472 // Adding a policy makes the banner show up. |
440 provider_.UpdateChromePolicy(test_case->test_policy()); | 473 provider_.UpdateChromePolicy(test_case->test_policy()); |
441 EXPECT_TRUE(IsBannerVisible(browser())); | 474 EXPECT_TRUE(IsBannerVisible(browser())); |
442 // And removing it makes the banner go away. | 475 // And removing it makes the banner go away. |
443 const PolicyMap kNoPolicies; | 476 const PolicyMap kNoPolicies; |
444 provider_.UpdateChromePolicy(kNoPolicies); | 477 provider_.UpdateChromePolicy(kNoPolicies); |
445 EXPECT_FALSE(IsBannerVisible(browser())); | 478 EXPECT_FALSE(IsBannerVisible(browser())); |
446 // Do it again, just in case. | 479 // Do it again, just in case. |
447 provider_.UpdateChromePolicy(test_case->test_policy()); | 480 provider_.UpdateChromePolicy(test_case->test_policy()); |
448 EXPECT_TRUE(IsBannerVisible(browser())); | 481 EXPECT_TRUE(IsBannerVisible(browser())); |
449 provider_.UpdateChromePolicy(kNoPolicies); | 482 provider_.UpdateChromePolicy(kNoPolicies); |
450 EXPECT_FALSE(IsBannerVisible(browser())); | 483 EXPECT_FALSE(IsBannerVisible(browser())); |
451 } | 484 } |
452 | 485 |
453 IN_PROC_BROWSER_TEST_P(PolicyPrefsTest, PolicyToPrefsMapping) { | 486 IN_PROC_BROWSER_TEST_P(PolicyPrefsTest, PolicyToPrefsMapping) { |
454 // Verifies that policies make their corresponding preferences become managed, | 487 // Verifies that policies make their corresponding preferences become managed, |
455 // and that the user can't override that setting. | 488 // and that the user can't override that setting. |
456 const PolicyTestCase* test_case = test_cases_.Get(GetParam().name); | 489 const PolicyTestCase* test_case = policy_test_cases_.Get(GetParam().name); |
457 ASSERT_TRUE(test_case); | 490 ASSERT_TRUE(test_case); |
458 if (!test_case->IsSupported() || test_case->pref().empty()) | 491 const ScopedVector<PrefMapping>& pref_mappings = test_case->pref_mappings(); |
| 492 if (!test_case->IsSupported() || pref_mappings.empty()) |
459 return; | 493 return; |
460 LOG(INFO) << "Testing policy: " << test_case->name(); | 494 LOG(INFO) << "Testing policy: " << test_case->name(); |
461 | 495 |
462 PrefService* prefs = test_case->is_local_state() ? | 496 for (ScopedVector<PrefMapping>::const_iterator |
463 g_browser_process->local_state() : browser()->profile()->GetPrefs(); | 497 pref_mapping = pref_mappings.begin(); |
464 // The preference must have been registered. | 498 pref_mapping != pref_mappings.end(); |
465 const PrefService::Preference* pref = | 499 ++pref_mapping) { |
466 prefs->FindPreference(test_case->pref_name()); | 500 PrefService* prefs = (*pref_mapping)->is_local_state() ? |
467 ASSERT_TRUE(pref); | 501 g_browser_process->local_state() : browser()->profile()->GetPrefs(); |
468 prefs->ClearPref(test_case->pref_name()); | 502 // The preference must have been registered. |
| 503 const PrefService::Preference* pref = |
| 504 prefs->FindPreference((*pref_mapping)->pref().c_str()); |
| 505 ASSERT_TRUE(pref); |
| 506 prefs->ClearPref((*pref_mapping)->pref().c_str()); |
469 | 507 |
470 // Verify that setting the policy overrides the pref. | 508 // Verify that setting the policy overrides the pref. |
471 EXPECT_TRUE(pref->IsDefaultValue()); | 509 const PolicyMap kNoPolicies; |
472 EXPECT_TRUE(pref->IsUserModifiable()); | 510 provider_.UpdateChromePolicy(kNoPolicies); |
473 EXPECT_FALSE(pref->IsUserControlled()); | 511 EXPECT_TRUE(pref->IsDefaultValue()); |
474 EXPECT_FALSE(pref->IsManaged()); | 512 EXPECT_TRUE(pref->IsUserModifiable()); |
| 513 EXPECT_FALSE(pref->IsUserControlled()); |
| 514 EXPECT_FALSE(pref->IsManaged()); |
475 | 515 |
476 provider_.UpdateChromePolicy(test_case->test_policy()); | 516 provider_.UpdateChromePolicy(test_case->test_policy()); |
477 EXPECT_FALSE(pref->IsDefaultValue()); | 517 EXPECT_FALSE(pref->IsDefaultValue()); |
478 EXPECT_FALSE(pref->IsUserModifiable()); | 518 EXPECT_FALSE(pref->IsUserModifiable()); |
479 EXPECT_FALSE(pref->IsUserControlled()); | 519 EXPECT_FALSE(pref->IsUserControlled()); |
480 EXPECT_TRUE(pref->IsManaged()); | 520 EXPECT_TRUE(pref->IsManaged()); |
| 521 } |
481 } | 522 } |
482 | 523 |
483 IN_PROC_BROWSER_TEST_P(PolicyPrefsTest, CheckAllPoliciesThatShowTheBanner) { | 524 IN_PROC_BROWSER_TEST_P(PolicyPrefsTest, CheckAllPoliciesThatShowTheBanner) { |
484 // Verifies that the banner appears for each policy that affects a control | 525 // Verifies that the banner appears for each policy that affects a control |
485 // in the settings UI. | 526 // in the settings UI. |
486 const PolicyTestCase* test_case = test_cases_.Get(GetParam().name); | 527 const PolicyTestCase* test_case = policy_test_cases_.Get(GetParam().name); |
487 ASSERT_TRUE(test_case); | 528 ASSERT_TRUE(test_case); |
488 if (!test_case->IsSupported() || test_case->settings_pages().empty()) | 529 if (!test_case->IsSupported() || test_case->settings_pages().empty()) |
489 return; | 530 return; |
490 LOG(INFO) << "Testing policy: " << test_case->name(); | 531 LOG(INFO) << "Testing policy: " << test_case->name(); |
491 | 532 |
492 const std::vector<GURL>& pages = test_case->settings_pages(); | 533 const std::vector<GURL>& pages = test_case->settings_pages(); |
493 for (size_t i = 0; i < pages.size(); ++i) { | 534 for (size_t i = 0; i < pages.size(); ++i) { |
494 ui_test_utils::NavigateToURL(browser(), pages[i]); | 535 ui_test_utils::NavigateToURL(browser(), pages[i]); |
495 EXPECT_FALSE(IsBannerVisible(browser())); | 536 EXPECT_FALSE(IsBannerVisible(browser())); |
496 provider_.UpdateChromePolicy(test_case->test_policy()); | 537 provider_.UpdateChromePolicy(test_case->test_policy()); |
497 EXPECT_TRUE(IsBannerVisible(browser())); | 538 EXPECT_TRUE(IsBannerVisible(browser())); |
498 const PolicyMap kNoPolicies; | 539 const PolicyMap kNoPolicies; |
499 provider_.UpdateChromePolicy(kNoPolicies); | 540 provider_.UpdateChromePolicy(kNoPolicies); |
500 EXPECT_FALSE(IsBannerVisible(browser())); | 541 EXPECT_FALSE(IsBannerVisible(browser())); |
501 } | 542 } |
502 } | 543 } |
503 | 544 |
504 IN_PROC_BROWSER_TEST_P(PolicyPrefsTest, CheckPolicyIndicators) { | 545 IN_PROC_BROWSER_TEST_P(PolicyPrefsTest, CheckPolicyIndicators) { |
505 // Verifies that controlled setting indicators correctly show whether a pref's | 546 // Verifies that controlled setting indicators correctly show whether a pref's |
506 // value is recommended or enforced by a corresponding policy. | 547 // value is recommended or enforced by a corresponding policy. |
507 const PolicyTestCase* policy_test_case = test_cases_.Get(GetParam().name); | 548 const PolicyTestCase* policy_test_case = |
| 549 policy_test_cases_.Get(GetParam().name); |
508 ASSERT_TRUE(policy_test_case); | 550 ASSERT_TRUE(policy_test_case); |
509 const ScopedVector<IndicatorTestCase>& indicator_test_cases = | 551 const ScopedVector<PrefMapping>& pref_mappings = |
510 policy_test_case->indicator_test_cases(); | 552 policy_test_case->pref_mappings(); |
511 if (!policy_test_case->IsSupported() || indicator_test_cases.empty()) | 553 if (!policy_test_case->IsSupported() || pref_mappings.empty()) |
| 554 return; |
| 555 bool has_indicator_tests = false; |
| 556 for (ScopedVector<PrefMapping>::const_iterator |
| 557 pref_mapping = pref_mappings.begin(); |
| 558 pref_mapping != pref_mappings.end(); |
| 559 ++pref_mapping) { |
| 560 if (!(*pref_mapping)->indicator_test_cases().empty()) { |
| 561 has_indicator_tests = true; |
| 562 break; |
| 563 } |
| 564 } |
| 565 if (!has_indicator_tests) |
512 return; | 566 return; |
513 LOG(INFO) << "Testing policy: " << policy_test_case->name(); | 567 LOG(INFO) << "Testing policy: " << policy_test_case->name(); |
514 | 568 |
515 PrefService* prefs = policy_test_case->is_local_state() ? | 569 for (ScopedVector<PrefMapping>::const_iterator |
516 g_browser_process->local_state() : browser()->profile()->GetPrefs(); | 570 pref_mapping = pref_mappings.begin(); |
517 // The preference must have been registered. | 571 pref_mapping != pref_mappings.end(); |
518 const PrefService::Preference* pref = | 572 ++pref_mapping) { |
519 prefs->FindPreference(policy_test_case->pref_name()); | 573 const ScopedVector<IndicatorTestCase>& |
520 ASSERT_TRUE(pref); | 574 indicator_test_cases = (*pref_mapping)->indicator_test_cases(); |
| 575 if (indicator_test_cases.empty()) |
| 576 continue; |
| 577 PrefService* prefs = (*pref_mapping)->is_local_state() ? |
| 578 g_browser_process->local_state() : browser()->profile()->GetPrefs(); |
| 579 // The preference must have been registered. |
| 580 const PrefService::Preference* pref = |
| 581 prefs->FindPreference((*pref_mapping)->pref().c_str()); |
| 582 ASSERT_TRUE(pref); |
521 | 583 |
522 ui_test_utils::NavigateToURL(browser(), GURL(kSettingsPages[0])); | 584 ui_test_utils::NavigateToURL(browser(), GURL(kSettingsPages[0])); |
523 if (!policy_test_case->indicator_test_setup_js().empty()) { | 585 if (!(*pref_mapping)->indicator_test_setup_js().empty()) { |
524 ASSERT_TRUE(content::ExecuteJavaScript( | 586 ASSERT_TRUE(content::ExecuteJavaScript( |
525 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), L"", | 587 chrome::GetActiveWebContents(browser())->GetRenderViewHost(), L"", |
526 policy_test_case->indicator_test_setup_js())); | 588 (*pref_mapping)->indicator_test_setup_js())); |
527 } | 589 } |
528 | 590 |
529 std::string indicator_selector = policy_test_case->indicator_selector(); | 591 std::string indicator_selector = (*pref_mapping)->indicator_selector(); |
530 if (indicator_selector.empty()) | 592 if (indicator_selector.empty()) |
531 indicator_selector = "[pref=\"" + policy_test_case->pref() + "\"]"; | 593 indicator_selector = "[pref=\"" + (*pref_mapping)->pref() + "\"]"; |
532 for (ScopedVector<IndicatorTestCase>::const_iterator | 594 for (ScopedVector<IndicatorTestCase>::const_iterator |
533 indicator_test_case = indicator_test_cases.begin(); | 595 indicator_test_case = indicator_test_cases.begin(); |
534 indicator_test_case != indicator_test_cases.end(); | 596 indicator_test_case != indicator_test_cases.end(); |
535 ++indicator_test_case) { | 597 ++indicator_test_case) { |
536 // Check that no controlled setting indicator is visible when no value is | 598 // Check that no controlled setting indicator is visible when no value is |
537 // set by policy. | 599 // set by policy. |
538 PolicyMap policies; | 600 PolicyMap policies; |
539 provider_.UpdateChromePolicy(policies); | 601 provider_.UpdateChromePolicy(policies); |
540 VerifyControlledSettingIndicators(browser(), indicator_selector, | 602 VerifyControlledSettingIndicators(browser(), indicator_selector, |
541 "", "", false); | 603 "", "", false); |
542 // Check that the appropriate controlled setting indicator is shown when a | 604 // Check that the appropriate controlled setting indicator is shown when a |
543 // value is enforced by policy. | 605 // value is enforced by policy. |
544 policies.LoadFrom(&(*indicator_test_case)->policy(), | 606 policies.LoadFrom(&(*indicator_test_case)->policy(), |
545 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); | 607 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER); |
546 provider_.UpdateChromePolicy(policies); | 608 provider_.UpdateChromePolicy(policies); |
547 VerifyControlledSettingIndicators(browser(), indicator_selector, | 609 VerifyControlledSettingIndicators(browser(), indicator_selector, |
548 (*indicator_test_case)->value(), | 610 (*indicator_test_case)->value(), |
549 "policy", | 611 "policy", |
550 (*indicator_test_case)->readonly()); | 612 (*indicator_test_case)->readonly()); |
551 if (!policy_test_case->can_be_recommended()) | 613 if (!policy_test_case->can_be_recommended()) |
552 continue; | 614 continue; |
553 // Check that the appropriate controlled setting indicator is shown when a | 615 // Check that the appropriate controlled setting indicator is shown when a |
554 // value is recommended by policy and the user has not overridden the | 616 // value is recommended by policy and the user has not overridden the |
555 // recommendation. | 617 // recommendation. |
556 policies.LoadFrom(&(*indicator_test_case)->policy(), | 618 policies.LoadFrom(&(*indicator_test_case)->policy(), |
557 POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER); | 619 POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER); |
558 provider_.UpdateChromePolicy(policies); | 620 provider_.UpdateChromePolicy(policies); |
559 VerifyControlledSettingIndicators(browser(), indicator_selector, | 621 VerifyControlledSettingIndicators(browser(), indicator_selector, |
560 (*indicator_test_case)->value(), | 622 (*indicator_test_case)->value(), |
561 "recommended", | 623 "recommended", |
562 (*indicator_test_case)->readonly()); | 624 (*indicator_test_case)->readonly()); |
563 // Check that the appropriate controlled setting indicator is shown when a | 625 // Check that the appropriate controlled setting indicator is shown when a |
564 // value is recommended by policy and the user has overriddent the | 626 // value is recommended by policy and the user has overriddent the |
565 // recommendation. | 627 // recommendation. |
566 prefs->Set(policy_test_case->pref_name(), *pref->GetValue()); | 628 prefs->Set((*pref_mapping)->pref().c_str(), *pref->GetValue()); |
567 VerifyControlledSettingIndicators(browser(), indicator_selector, | 629 VerifyControlledSettingIndicators(browser(), indicator_selector, |
568 (*indicator_test_case)->value(), | 630 (*indicator_test_case)->value(), |
569 "hasRecommendation", | 631 "hasRecommendation", |
570 (*indicator_test_case)->readonly()); | 632 (*indicator_test_case)->readonly()); |
571 prefs->ClearPref(policy_test_case->pref_name()); | 633 prefs->ClearPref((*pref_mapping)->pref().c_str()); |
| 634 } |
572 } | 635 } |
573 } | 636 } |
574 | 637 |
575 INSTANTIATE_TEST_CASE_P( | 638 INSTANTIATE_TEST_CASE_P( |
576 PolicyPrefsTestInstance, | 639 PolicyPrefsTestInstance, |
577 PolicyPrefsTest, | 640 PolicyPrefsTest, |
578 testing::ValuesIn(GetChromePolicyDefinitionList()->begin, | 641 testing::ValuesIn(GetChromePolicyDefinitionList()->begin, |
579 GetChromePolicyDefinitionList()->end)); | 642 GetChromePolicyDefinitionList()->end)); |
580 | 643 |
581 } // namespace policy | 644 } // namespace policy |
OLD | NEW |