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

Side by Side Diff: chrome/browser/policy/policy_prefs_browsertest.cc

Issue 10918263: Converted policy_prefs_ui.py pyauto test to a browser_test. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 3 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
OLDNEW
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 <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
11 #include "base/file_path.h" 11 #include "base/file_path.h"
12 #include "base/file_util.h" 12 #include "base/file_util.h"
13 #include "base/json/json_reader.h" 13 #include "base/json/json_reader.h"
14 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/scoped_ptr.h"
15 #include "base/stl_util.h" 15 #include "base/stl_util.h"
16 #include "base/values.h" 16 #include "base/values.h"
17 #include "chrome/browser/browser_process.h" 17 #include "chrome/browser/browser_process.h"
18 #include "chrome/browser/policy/browser_policy_connector.h" 18 #include "chrome/browser/policy/browser_policy_connector.h"
19 #include "chrome/browser/policy/mock_configuration_policy_provider.h" 19 #include "chrome/browser/policy/mock_configuration_policy_provider.h"
20 #include "chrome/browser/policy/policy_map.h" 20 #include "chrome/browser/policy/policy_map.h"
21 #include "chrome/browser/prefs/pref_service.h" 21 #include "chrome/browser/prefs/pref_service.h"
22 #include "chrome/browser/profiles/profile.h" 22 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/browser/ui/browser.h" 23 #include "chrome/browser/ui/browser.h"
24 #include "chrome/browser/ui/browser_tabstrip.h"
24 #include "chrome/test/base/in_process_browser_test.h" 25 #include "chrome/test/base/in_process_browser_test.h"
25 #include "chrome/test/base/ui_test_utils.h" 26 #include "chrome/test/base/ui_test_utils.h"
27 #include "content/public/browser/web_contents.h"
28 #include "content/public/test/browser_test_utils.h"
26 #include "googleurl/src/gurl.h" 29 #include "googleurl/src/gurl.h"
27 #include "policy/policy_constants.h" 30 #include "policy/policy_constants.h"
28 #include "testing/gmock/include/gmock/gmock.h" 31 #include "testing/gmock/include/gmock/gmock.h"
29 #include "testing/gtest/include/gtest/gtest.h" 32 #include "testing/gtest/include/gtest/gtest.h"
30 33
31 #if defined(OS_MACOSX)
32 #include "base/base_paths.h"
33 #include "base/mac/foundation_util.h"
34 #include "base/path_service.h"
35 #include "chrome/common/chrome_constants.h"
36 #endif
37
38 using testing::Return; 34 using testing::Return;
39 35
40 namespace policy { 36 namespace policy {
41 37
42 namespace { 38 namespace {
43 39
40 const char* kSettingsPages[] = {
41 "chrome://settings-frame",
42 "chrome://settings-frame/searchEngines",
43 "chrome://settings-frame/passwords",
44 "chrome://settings-frame/autofill",
45 "chrome://settings-frame/content",
46 "chrome://settings-frame/homePageOverlay",
47 "chrome://settings-frame/languages",
48 #if defined(OS_CHROMEOS)
49 "chrome://settings-frame/accounts",
50 #endif
51 };
52
44 // Contains the testing details for a single policy, loaded from 53 // Contains the testing details for a single policy, loaded from
45 // chrome/test/data/policy/policy_test_cases.json. 54 // chrome/test/data/policy/policy_test_cases.json.
46 class PolicyTestCase { 55 class PolicyTestCase {
47 public: 56 public:
48 explicit PolicyTestCase(const std::string& name) 57 explicit PolicyTestCase(const std::string& name)
49 : name_(name), 58 : name_(name),
50 is_local_state_(false), 59 is_local_state_(false),
51 official_only_(false) {} 60 official_only_(false) {}
52 ~PolicyTestCase() {} 61 ~PolicyTestCase() {}
53 62
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 std::string pref_; 111 std::string pref_;
103 PolicyMap test_policy_; 112 PolicyMap test_policy_;
104 std::vector<GURL> settings_pages_; 113 std::vector<GURL> settings_pages_;
105 std::vector<std::string> supported_os_; 114 std::vector<std::string> supported_os_;
106 bool is_local_state_; 115 bool is_local_state_;
107 bool official_only_; 116 bool official_only_;
108 117
109 DISALLOW_COPY_AND_ASSIGN(PolicyTestCase); 118 DISALLOW_COPY_AND_ASSIGN(PolicyTestCase);
110 }; 119 };
111 120
112 } // namespace 121 // Parses all the test cases and makes then available in a map.
122 class TestCases {
123 public:
124 typedef std::map<std::string, PolicyTestCase*> TestCaseMap;
125 typedef TestCaseMap::const_iterator iterator;
113 126
114 class PolicyPrefsTest : public InProcessBrowserTest { 127 TestCases() {
115 protected: 128 test_cases_ = new std::map<std::string, PolicyTestCase*>();
116 PolicyPrefsTest() {}
117 virtual ~PolicyPrefsTest() {}
118
119 // Loads policy_test_cases.json and builds a map of test cases.
120 static void SetUpTestCase() {
121 #if defined(OS_MACOSX)
122 // Ugly hack to work around http://crbug.com/63183, since this uses the
123 // PathService from GetTestFilePath() above before BrowserTestBase() and
124 // InProcessBrowserTest() are invoked. Those ctors include similar hacks.
125 base::mac::SetOverrideAmIBundled(true);
126 FilePath chrome_path;
127 CHECK(PathService::Get(base::FILE_EXE, &chrome_path));
128 FilePath fixed_chrome_path =
129 chrome_path.DirName().Append(chrome::kBrowserProcessExecutablePath);
130 CHECK(PathService::Override(base::FILE_EXE, fixed_chrome_path));
131 #endif
132 129
133 FilePath path = ui_test_utils::GetTestFilePath( 130 FilePath path = ui_test_utils::GetTestFilePath(
134 FilePath(FILE_PATH_LITERAL("policy")), 131 FilePath(FILE_PATH_LITERAL("policy")),
135 FilePath(FILE_PATH_LITERAL("policy_test_cases.json"))); 132 FilePath(FILE_PATH_LITERAL("policy_test_cases.json")));
136 std::string json; 133 std::string json;
137 ASSERT_TRUE(file_util::ReadFileToString(path, &json)); 134 if (!file_util::ReadFileToString(path, &json)) {
135 ADD_FAILURE();
136 return;
137 }
138 int error_code = -1; 138 int error_code = -1;
139 std::string error_string; 139 std::string error_string;
140 base::DictionaryValue* dict = NULL;
140 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError( 141 scoped_ptr<base::Value> value(base::JSONReader::ReadAndReturnError(
141 json, base::JSON_ALLOW_TRAILING_COMMAS, &error_code, &error_string)); 142 json, base::JSON_PARSE_RFC, &error_code, &error_string));
142 ASSERT_TRUE(value.get()) 143 if (!value.get() || !value->GetAsDictionary(&dict)) {
143 << "Error parsing policy_test_cases.json: " << error_string; 144 ADD_FAILURE() << "Error parsing policy_test_cases.json: " << error_string;
144 base::DictionaryValue* dict = NULL; 145 return;
145 ASSERT_TRUE(value->GetAsDictionary(&dict)); 146 }
146 policy_test_cases_ = new std::map<std::string, PolicyTestCase*>();
147 const PolicyDefinitionList* list = GetChromePolicyDefinitionList(); 147 const PolicyDefinitionList* list = GetChromePolicyDefinitionList();
148 for (const PolicyDefinitionList::Entry* policy = list->begin; 148 for (const PolicyDefinitionList::Entry* policy = list->begin;
149 policy != list->end; ++policy) { 149 policy != list->end; ++policy) {
150 PolicyTestCase* test_case = GetTestCase(dict, policy->name); 150 PolicyTestCase* test_case = GetTestCase(dict, policy->name);
151 if (test_case) 151 if (test_case)
152 (*policy_test_cases_)[policy->name] = test_case; 152 (*test_cases_)[policy->name] = test_case;
153 } 153 }
154
155 #if defined(OS_MACOSX)
156 // Restore |chrome_path| so that the fix in InProcessBrowserTest() works.
157 CHECK(PathService::Override(base::FILE_EXE, chrome_path));
158 #endif
159 } 154 }
160 155
161 static void TearDownTestCase() { 156 ~TestCases() {
162 STLDeleteValues(policy_test_cases_); 157 STLDeleteValues(test_cases_);
163 delete policy_test_cases_; 158 delete test_cases_;
164 } 159 }
165 160
166 static PolicyTestCase* GetTestCase(const base::DictionaryValue* tests, 161 const PolicyTestCase* Get(const std::string& name) {
167 const std::string& name) { 162 iterator it = test_cases_->find(name);
163 return it == end() ? NULL : it->second;
164 }
165
166 const TestCaseMap& map() const { return *test_cases_; }
167 iterator begin() const { return test_cases_->begin(); }
168 iterator end() const { return test_cases_->end(); }
169
170 private:
171 PolicyTestCase* GetTestCase(const base::DictionaryValue* tests,
172 const std::string& name) {
168 const base::DictionaryValue* dict = NULL; 173 const base::DictionaryValue* dict = NULL;
169 if (!tests->GetDictionary(name, &dict)) 174 if (!tests->GetDictionary(name, &dict))
170 return NULL; 175 return NULL;
171 PolicyTestCase* test_case = new PolicyTestCase(name); 176 PolicyTestCase* test_case = new PolicyTestCase(name);
172 std::string pref; 177 std::string pref;
173 if (dict->GetString("pref", &pref)) 178 if (dict->GetString("pref", &pref))
174 test_case->set_pref(pref); 179 test_case->set_pref(pref);
175 const base::DictionaryValue* policy_dict = NULL; 180 const base::DictionaryValue* policy_dict = NULL;
176 if (dict->GetDictionary("test_policy", &policy_dict)) { 181 if (dict->GetDictionary("test_policy", &policy_dict)) {
177 PolicyMap policies; 182 PolicyMap policies;
(...skipping 17 matching lines...) Expand all
195 } 200 }
196 } 201 }
197 bool flag = false; 202 bool flag = false;
198 if (dict->GetBoolean("local_state", &flag)) 203 if (dict->GetBoolean("local_state", &flag))
199 test_case->set_local_state(flag); 204 test_case->set_local_state(flag);
200 if (dict->GetBoolean("official_only", &flag)) 205 if (dict->GetBoolean("official_only", &flag))
201 test_case->set_official_only(flag); 206 test_case->set_official_only(flag);
202 return test_case; 207 return test_case;
203 } 208 }
204 209
210 TestCaseMap* test_cases_;
211
212 DISALLOW_COPY_AND_ASSIGN(TestCases);
213 };
214
215 bool IsBannerVisible(Browser* browser) {
216 content::WebContents* contents = chrome::GetActiveWebContents(browser);
217 bool result = false;
218 EXPECT_TRUE(content::ExecuteJavaScriptAndExtractBool(
219 contents->GetRenderViewHost(),
220 std::wstring(),
221 L"var visible = false;"
222 L"var banners = document.querySelectorAll('.page-banner');"
223 L"for (var i = 0; i < banners.length; i++) {"
224 L" if (banners[i].parentElement.id == 'templates')"
225 L" continue;"
226 L" if (window.getComputedStyle(banners[i]).display != 'none')"
227 L" visible = true;"
228 L"}"
229 L"domAutomationController.send(visible);",
230 &result));
231 return result;
232 }
233
234 } // namespace
235
236 // A class of tests parameterized by a settings page URL.
237 class PolicyPrefsSettingsBannerTest
238 : public InProcessBrowserTest,
239 public testing::WithParamInterface<const char*> {};
240
241 // Base class for tests that change policies.
242 class PolicyBaseTest : public InProcessBrowserTest {
243 protected:
205 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE { 244 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE {
206 EXPECT_CALL(provider_, IsInitializationComplete()) 245 EXPECT_CALL(provider_, IsInitializationComplete())
207 .WillRepeatedly(Return(true)); 246 .WillRepeatedly(Return(true));
208 BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_); 247 BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_);
209 } 248 }
210 249
211 static std::map<std::string, PolicyTestCase*>* policy_test_cases_; 250 TestCases test_cases_;
212 MockConfigurationPolicyProvider provider_; 251 MockConfigurationPolicyProvider provider_;
213 }; 252 };
214 253
215 std::map<std::string, PolicyTestCase*>* PolicyPrefsTest::policy_test_cases_ = 0; 254 // A class of tests that change policy and don't need parameters.
255 class PolicyPrefsBannerTest : public PolicyBaseTest {};
216 256
217 IN_PROC_BROWSER_TEST_F(PolicyPrefsTest, AllPoliciesHaveATestCase) { 257 // A class of tests that change policy and are parameterized with a policy
258 // definition.
259 class PolicyPrefsTest
260 : public PolicyBaseTest,
261 public testing::WithParamInterface<PolicyDefinitionList::Entry> {};
262
263 TEST(PolicyPrefsTest, AllPoliciesHaveATestCase) {
218 // Verifies that all known policies have a test case in the JSON file. 264 // Verifies that all known policies have a test case in the JSON file.
219 // This test fails when a policy is added to 265 // This test fails when a policy is added to
220 // chrome/app/policy/policy_templates.json but a test case is not added to 266 // chrome/app/policy/policy_templates.json but a test case is not added to
221 // chrome/test/data/policy/policy_test_cases.json. 267 // chrome/test/data/policy/policy_test_cases.json.
268 TestCases test_cases;
222 const PolicyDefinitionList* list = GetChromePolicyDefinitionList(); 269 const PolicyDefinitionList* list = GetChromePolicyDefinitionList();
223 for (const PolicyDefinitionList::Entry* policy = list->begin; 270 for (const PolicyDefinitionList::Entry* policy = list->begin;
224 policy != list->end; ++policy) { 271 policy != list->end; ++policy) {
225 EXPECT_TRUE(ContainsKey(*policy_test_cases_, policy->name)) 272 EXPECT_TRUE(ContainsKey(test_cases.map(), policy->name))
226 << "Missing policy test case for: " << policy->name; 273 << "Missing policy test case for: " << policy->name;
227 } 274 }
228 } 275 }
229 276
230 IN_PROC_BROWSER_TEST_F(PolicyPrefsTest, PolicyToPrefsMapping) { 277 IN_PROC_BROWSER_TEST_P(PolicyPrefsSettingsBannerTest, NoPoliciesNoBanner) {
278 // Verifies that the banner isn't shown in the settings UI when no policies
279 // are set.
280 ui_test_utils::NavigateToURL(browser(), GURL(GetParam()));
281 EXPECT_FALSE(IsBannerVisible(browser()));
282 }
283
284 INSTANTIATE_TEST_CASE_P(PolicyPrefsSettingsBannerTestInstance,
285 PolicyPrefsSettingsBannerTest,
286 testing::ValuesIn(kSettingsPages));
287
288 IN_PROC_BROWSER_TEST_F(PolicyPrefsBannerTest, TogglePolicyTogglesBanner) {
289 // Verifies that the banner appears and disappears as policies are added and
290 // removed.
291 // |test_case| is just a particular policy that should trigger the banner
292 // on the main settings page.
293 const PolicyTestCase* test_case = test_cases_.Get("ShowHomeButton");
294 ASSERT_TRUE(test_case);
295 // No banner by default.
296 ui_test_utils::NavigateToURL(browser(), GURL(kSettingsPages[0]));
297 EXPECT_FALSE(IsBannerVisible(browser()));
298 // Adding a policy makes the banner show up.
299 provider_.UpdateChromePolicy(test_case->test_policy());
300 EXPECT_TRUE(IsBannerVisible(browser()));
301 // And removing it makes the banner go away.
302 const PolicyMap kNoPolicies;
303 provider_.UpdateChromePolicy(kNoPolicies);
304 EXPECT_FALSE(IsBannerVisible(browser()));
305 // Do it again, just in case.
306 provider_.UpdateChromePolicy(test_case->test_policy());
307 EXPECT_TRUE(IsBannerVisible(browser()));
308 provider_.UpdateChromePolicy(kNoPolicies);
309 EXPECT_FALSE(IsBannerVisible(browser()));
310 }
311
312 IN_PROC_BROWSER_TEST_P(PolicyPrefsTest, PolicyToPrefsMapping) {
231 // Verifies that policies make their corresponding preferences become managed, 313 // Verifies that policies make their corresponding preferences become managed,
232 // and that the user can't override that setting. 314 // and that the user can't override that setting.
233 const PolicyMap kNoPolicies; 315 const PolicyTestCase* test_case = test_cases_.Get(GetParam().name);
234 PrefService* profile_prefs = browser()->profile()->GetPrefs(); 316 ASSERT_TRUE(test_case);
235 PrefService* local_state = g_browser_process->local_state(); 317 if (!test_case->IsSupported() || test_case->pref().empty())
236 std::map<std::string, PolicyTestCase*>::iterator it; 318 return;
237 for (it = policy_test_cases_->begin(); 319 LOG(INFO) << "Testing policy: " << test_case->name();
238 it != policy_test_cases_->end(); ++it) { 320
239 PolicyTestCase* test_case = it->second; 321 PrefService* prefs = test_case->is_local_state() ?
240 if (!test_case->IsSupported() || test_case->pref().empty()) 322 g_browser_process->local_state() : browser()->profile()->GetPrefs();
241 continue; 323 // The preference must have been registered.
242 LOG(INFO) << "Testing policy: " << test_case->name(); 324 const PrefService::Preference* pref =
243 // Clear policies. 325 prefs->FindPreference(test_case->pref_name());
326 ASSERT_TRUE(pref);
327 prefs->ClearPref(test_case->pref_name());
328
329 // Verify that setting the policy overrides the pref.
330 EXPECT_TRUE(pref->IsDefaultValue());
331 EXPECT_TRUE(pref->IsUserModifiable());
332 EXPECT_FALSE(pref->IsUserControlled());
333 EXPECT_FALSE(pref->IsManaged());
334
335 provider_.UpdateChromePolicy(test_case->test_policy());
336 EXPECT_FALSE(pref->IsDefaultValue());
337 EXPECT_FALSE(pref->IsUserModifiable());
338 EXPECT_FALSE(pref->IsUserControlled());
339 EXPECT_TRUE(pref->IsManaged());
340 }
341
342 IN_PROC_BROWSER_TEST_P(PolicyPrefsTest, CheckAllPoliciesThatShowTheBanner) {
343 // Verifies that the banner appears for each policy that affects a control
344 // in the settings UI.
345 const PolicyTestCase* test_case = test_cases_.Get(GetParam().name);
346 ASSERT_TRUE(test_case);
347 if (!test_case->IsSupported() || test_case->settings_pages().empty())
348 return;
349 LOG(INFO) << "Testing policy: " << test_case->name();
350
351 const std::vector<GURL>& pages = test_case->settings_pages();
352 for (size_t i = 0; i < pages.size(); ++i) {
353 ui_test_utils::NavigateToURL(browser(), pages[i]);
354 EXPECT_FALSE(IsBannerVisible(browser()));
355 provider_.UpdateChromePolicy(test_case->test_policy());
356 EXPECT_TRUE(IsBannerVisible(browser()));
357 const PolicyMap kNoPolicies;
244 provider_.UpdateChromePolicy(kNoPolicies); 358 provider_.UpdateChromePolicy(kNoPolicies);
245 359 EXPECT_FALSE(IsBannerVisible(browser()));
246 PrefService* prefs =
247 test_case->is_local_state() ? local_state : profile_prefs;
248 // The preference must have been registered.
249 const PrefService::Preference* pref =
250 prefs->FindPreference(test_case->pref_name());
251 ASSERT_TRUE(pref);
252 prefs->ClearPref(test_case->pref_name());
253
254 // Verify that setting the policy overrides the pref.
255 EXPECT_TRUE(pref->IsDefaultValue());
256 EXPECT_TRUE(pref->IsUserModifiable());
257 EXPECT_FALSE(pref->IsUserControlled());
258 EXPECT_FALSE(pref->IsManaged());
259
260 provider_.UpdateChromePolicy(test_case->test_policy());
261 EXPECT_FALSE(pref->IsDefaultValue());
262 EXPECT_FALSE(pref->IsUserModifiable());
263 EXPECT_FALSE(pref->IsUserControlled());
264 EXPECT_TRUE(pref->IsManaged());
265 } 360 }
266 } 361 }
267 362
363 INSTANTIATE_TEST_CASE_P(
364 PolicyPrefsTestInstance,
365 PolicyPrefsTest,
366 testing::ValuesIn(GetChromePolicyDefinitionList()->begin,
367 GetChromePolicyDefinitionList()->end));
368
268 } // namespace policy 369 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/automation/testing_automation_provider.cc ('k') | chrome/test/data/policy/policy_test_cases.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698