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 virtual void SetUpInProcessBrowserTestFixture() OVERRIDE; |
| 84 void UpdateProviderPolicy(const policy::PolicyMap& policy); |
| 85 |
| 86 void VerifyPolicies(const std::vector<std::vector<std::string> > expected); |
| 87 |
| 88 policy::MockConfigurationPolicyProvider provider_; |
| 89 |
| 90 DISALLOW_COPY_AND_ASSIGN(PolicyUITest); |
| 91 }; |
| 92 |
| 93 PolicyUITest::PolicyUITest() { |
| 94 } |
| 95 |
| 96 PolicyUITest::~PolicyUITest() { |
| 97 } |
| 98 |
| 99 void PolicyUITest::SetUpInProcessBrowserTestFixture() { |
| 100 EXPECT_CALL(provider_, IsInitializationComplete()) |
| 101 .WillRepeatedly(Return(true)); |
| 102 policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_); |
| 103 } |
| 104 |
| 105 void PolicyUITest::UpdateProviderPolicy(const policy::PolicyMap& policy) { |
| 106 provider_.UpdateChromePolicy(policy); |
| 107 base::RunLoop loop; |
| 108 loop.RunUntilIdle(); |
| 109 } |
| 110 |
| 111 void PolicyUITest::VerifyPolicies( |
| 112 const std::vector<std::vector<std::string> > expected_policies) { |
| 113 ui_test_utils::NavigateToURL(browser(), GURL("chrome://policy")); |
| 114 |
| 115 // Retrieve the text contents of the policy table cells for all policies. |
| 116 const std::string javascript = |
| 117 "var entries = document.querySelectorAll(" |
| 118 " 'table#policy-table > tbody');" |
| 119 "var policies = [];" |
| 120 "for (var i = 0; i < entries.length; ++i) {" |
| 121 " var items = entries[i].querySelectorAll('tr > td');" |
| 122 " var values = [];" |
| 123 " for (var j = 0; j < items.length; ++j) {" |
| 124 " var item = items[j];" |
| 125 " var children = item.getElementsByTagName('div');" |
| 126 " if (children.length == 1)" |
| 127 " item = children[0];" |
| 128 " children = item.getElementsByTagName('span');" |
| 129 " if (children.length == 1)" |
| 130 " item = children[0];" |
| 131 " values.push(item.textContent);" |
| 132 " }" |
| 133 " policies.push(values);" |
| 134 "}" |
| 135 "domAutomationController.send(JSON.stringify(policies));"; |
| 136 content::WebContents* contents = |
| 137 browser()->tab_strip_model()->GetActiveWebContents(); |
| 138 std::string json; |
| 139 ASSERT_TRUE(content::ExecuteScriptAndExtractString(contents, javascript, |
| 140 &json)); |
| 141 scoped_ptr<base::Value> value_ptr(base::JSONReader::Read(json)); |
| 142 const base::ListValue* actual_policies = NULL; |
| 143 ASSERT_TRUE(value_ptr.get()); |
| 144 ASSERT_TRUE(value_ptr->GetAsList(&actual_policies)); |
| 145 |
| 146 // Verify that the cells contain the expected strings for all policies. |
| 147 ASSERT_EQ(expected_policies.size(), actual_policies->GetSize()); |
| 148 for (size_t i = 0; i < expected_policies.size(); ++i) { |
| 149 const std::vector<std::string> expected_policy = expected_policies[i]; |
| 150 const base::ListValue* actual_policy; |
| 151 ASSERT_TRUE(actual_policies->GetList(i, &actual_policy)); |
| 152 ASSERT_EQ(expected_policy.size(), actual_policy->GetSize()); |
| 153 for (size_t j = 0; j < expected_policy.size(); ++j) { |
| 154 std::string value; |
| 155 ASSERT_TRUE(actual_policy->GetString(j, &value)); |
| 156 EXPECT_EQ(expected_policy[j], value); |
| 157 } |
| 158 } |
| 159 } |
| 160 |
| 161 IN_PROC_BROWSER_TEST_F(PolicyUITest, SendDefinitions) { |
| 162 // Verifies that policy definitions are sent to the UI and processed there |
| 163 // correctly by checking that the policy table contains all known policies in |
| 164 // the correct order. |
| 165 |
| 166 // Expect that the policy table contains all known policies in alphabetical |
| 167 // order and none of the policies have a set value. |
| 168 std::vector<std::vector<std::string> > expected_policies; |
| 169 const policy::PolicyDefinitionList* policies = |
| 170 policy::GetChromePolicyDefinitionList(); |
| 171 for (const policy::PolicyDefinitionList::Entry* policy = policies->begin; |
| 172 policy != policies->end; ++policy) { |
| 173 expected_policies.push_back( |
| 174 PopulateExpectedPolicy(policy->name, "", NULL, false)); |
| 175 } |
| 176 |
| 177 // Retrieve the contents of the policy table from the UI and verify that it |
| 178 // matches the expectation. |
| 179 VerifyPolicies(expected_policies); |
| 180 } |
| 181 |
| 182 IN_PROC_BROWSER_TEST_F(PolicyUITest, SendPolicyValues) { |
| 183 // Verifies that policy values are sent to the UI and processed there |
| 184 // correctly by setting the values of four known and one unknown policy and |
| 185 // checking that the policy table contains the policy names, values and |
| 186 // metadata in the correct order. |
| 187 policy::PolicyMap values; |
| 188 std::map<std::string, std::string> expected_values; |
| 189 |
| 190 // Set the values of four existing policies. |
| 191 base::ListValue* restore_on_startup_urls = new base::ListValue; |
| 192 restore_on_startup_urls->Append(base::Value::CreateStringValue("aaa")); |
| 193 restore_on_startup_urls->Append(base::Value::CreateStringValue("bbb")); |
| 194 restore_on_startup_urls->Append(base::Value::CreateStringValue("ccc")); |
| 195 values.Set(policy::key::kRestoreOnStartupURLs, |
| 196 policy::POLICY_LEVEL_MANDATORY, |
| 197 policy::POLICY_SCOPE_USER, |
| 198 restore_on_startup_urls); |
| 199 expected_values[policy::key::kRestoreOnStartupURLs] = "aaa,bbb,ccc"; |
| 200 values.Set(policy::key::kHomepageLocation, |
| 201 policy::POLICY_LEVEL_MANDATORY, |
| 202 policy::POLICY_SCOPE_MACHINE, |
| 203 base::Value::CreateStringValue("http://google.com")); |
| 204 expected_values[policy::key::kHomepageLocation] = "http://google.com"; |
| 205 values.Set(policy::key::kRestoreOnStartup, |
| 206 policy::POLICY_LEVEL_RECOMMENDED, |
| 207 policy::POLICY_SCOPE_USER, |
| 208 base::Value::CreateIntegerValue(4)); |
| 209 expected_values[policy::key::kRestoreOnStartup] = "4"; |
| 210 values.Set(policy::key::kShowHomeButton, |
| 211 policy::POLICY_LEVEL_RECOMMENDED, |
| 212 policy::POLICY_SCOPE_MACHINE, |
| 213 base::Value::CreateBooleanValue(true)); |
| 214 expected_values[policy::key::kShowHomeButton] = "true"; |
| 215 // Set the value of a policy that does not exist. |
| 216 const std::string kUnknownPolicy = "NoSuchThing"; |
| 217 values.Set(kUnknownPolicy, |
| 218 policy::POLICY_LEVEL_MANDATORY, |
| 219 policy::POLICY_SCOPE_USER, |
| 220 base::Value::CreateBooleanValue(true)); |
| 221 expected_values[kUnknownPolicy] = "true"; |
| 222 UpdateProviderPolicy(values); |
| 223 |
| 224 // Expect that the policy table contains, in order: |
| 225 // * All known policies whose value has been set, in alphabetical order. |
| 226 // * The unknown policy. |
| 227 // * All known policies whose value has not been set, in alphabetical order. |
| 228 std::vector<std::vector<std::string> > expected_policies; |
| 229 size_t first_unset_position = 0; |
| 230 const policy::PolicyDefinitionList* policies = |
| 231 policy::GetChromePolicyDefinitionList(); |
| 232 for (const policy::PolicyDefinitionList::Entry* policy = policies->begin; |
| 233 policy != policies->end; ++policy) { |
| 234 std::map<std::string, std::string>::const_iterator it = |
| 235 expected_values.find(policy->name); |
| 236 const std::string value = it == expected_values.end() ? "" : it->second; |
| 237 const policy::PolicyMap::Entry* metadata = values.Get(policy->name); |
| 238 expected_policies.insert( |
| 239 metadata ? expected_policies.begin() + first_unset_position++ : |
| 240 expected_policies.end(), |
| 241 PopulateExpectedPolicy(policy->name, value, metadata, false)); |
| 242 } |
| 243 expected_policies.insert( |
| 244 expected_policies.begin() + first_unset_position++, |
| 245 PopulateExpectedPolicy(kUnknownPolicy, |
| 246 expected_values[kUnknownPolicy], |
| 247 values.Get(kUnknownPolicy), |
| 248 true)); |
| 249 |
| 250 // Retrieve the contents of the policy table from the UI and verify that it |
| 251 // matches the expectation. |
| 252 VerifyPolicies(expected_policies); |
| 253 } |
OLD | NEW |