Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 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 <vector> | 5 #include <vector> |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/command_line.h" | |
| 8 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| 10 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 11 #include "base/values.h" | 12 #include "base/values.h" |
| 12 #include "chrome/browser/extensions/extension_service.h" | 13 #include "chrome/browser/extensions/extension_service.h" |
| 13 #include "chrome/browser/extensions/test_extension_system.h" | 14 #include "chrome/browser/extensions/test_extension_system.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/ui/browser.h" | 16 #include "chrome/browser/ui/browser.h" |
| 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 17 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 18 #include "chrome/common/chrome_switches.h" | |
| 17 #include "chrome/common/url_constants.h" | 19 #include "chrome/common/url_constants.h" |
| 18 #include "chrome/test/base/in_process_browser_test.h" | 20 #include "chrome/test/base/in_process_browser_test.h" |
| 19 #include "chrome/test/base/ui_test_utils.h" | 21 #include "chrome/test/base/ui_test_utils.h" |
| 20 #include "components/policy/core/browser/browser_policy_connector.h" | 22 #include "components/policy/core/browser/browser_policy_connector.h" |
| 21 #include "components/policy/core/common/external_data_fetcher.h" | 23 #include "components/policy/core/common/external_data_fetcher.h" |
| 22 #include "components/policy/core/common/mock_configuration_policy_provider.h" | 24 #include "components/policy/core/common/mock_configuration_policy_provider.h" |
| 23 #include "components/policy/core/common/policy_map.h" | 25 #include "components/policy/core/common/policy_map.h" |
| 24 #include "components/policy/core/common/policy_types.h" | 26 #include "components/policy/core/common/policy_types.h" |
| 25 #include "components/policy/core/common/schema.h" | 27 #include "components/policy/core/common/schema.h" |
| 26 #include "content/public/browser/web_contents.h" | 28 #include "content/public/browser/web_contents.h" |
| 27 #include "content/public/test/browser_test_utils.h" | 29 #include "content/public/test/browser_test_utils.h" |
| 28 #include "extensions/common/extension_builder.h" | 30 #include "extensions/common/extension_builder.h" |
| 29 #include "grit/components_strings.h" | 31 #include "grit/components_strings.h" |
| 30 #include "policy/policy_constants.h" | 32 #include "policy/policy_constants.h" |
| 31 #include "testing/gmock/include/gmock/gmock.h" | 33 #include "testing/gmock/include/gmock/gmock.h" |
| 32 #include "testing/gtest/include/gtest/gtest.h" | 34 #include "testing/gtest/include/gtest/gtest.h" |
| 33 #include "ui/base/l10n/l10n_util.h" | 35 #include "ui/base/l10n/l10n_util.h" |
| 34 #include "url/gurl.h" | 36 #include "url/gurl.h" |
| 35 | 37 |
| 36 using testing::Return; | 38 using testing::Return; |
| 37 using testing::_; | 39 using testing::_; |
| 38 | 40 |
| 39 namespace { | 41 namespace { |
| 40 | 42 |
| 43 struct UrlForFlag { | |
|
bartfab (slow)
2015/09/23 17:17:13
Nit: This is nifty but a bit over-engineered. A Bo
fhorschig
2015/09/25 08:27:15
Done.
| |
| 44 bool has_flag; | |
| 45 const char* url; | |
| 46 }; | |
| 47 | |
| 48 const UrlForFlag kUrlsForFlags[] = { | |
| 49 { true, chrome::kChromeUIPolicyInternalsURL}, | |
|
bartfab (slow)
2015/09/23 17:17:13
Nit: s/}/ }/
fhorschig
2015/09/25 08:27:15
Done.
| |
| 50 { false, chrome::kChromeUIPolicyURL} | |
|
bartfab (slow)
2015/09/23 17:17:13
Nit: s/}/ }/
fhorschig
2015/09/25 08:27:15
Done.
| |
| 51 }; | |
| 52 | |
| 41 std::vector<std::string> PopulateExpectedPolicy( | 53 std::vector<std::string> PopulateExpectedPolicy( |
| 42 const std::string& name, | 54 const std::string& name, |
| 43 const std::string& value, | 55 const std::string& value, |
| 44 const std::string& source, | 56 const std::string& source, |
| 45 const policy::PolicyMap::Entry* metadata, | 57 const policy::PolicyMap::Entry* metadata, |
| 46 bool unknown) { | 58 bool unknown) { |
| 47 std::vector<std::string> expected_policy; | 59 std::vector<std::string> expected_policy; |
| 48 | 60 |
| 49 // Populate expected scope. | 61 // Populate expected scope. |
| 50 if (metadata) { | 62 if (metadata) { |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 81 expected_policy.push_back(l10n_util::GetStringUTF8(IDS_POLICY_UNSET)); | 93 expected_policy.push_back(l10n_util::GetStringUTF8(IDS_POLICY_UNSET)); |
| 82 | 94 |
| 83 // Populate expected expanded policy value. | 95 // Populate expected expanded policy value. |
| 84 expected_policy.push_back(value); | 96 expected_policy.push_back(value); |
| 85 | 97 |
| 86 return expected_policy; | 98 return expected_policy; |
| 87 } | 99 } |
| 88 | 100 |
| 89 } // namespace | 101 } // namespace |
| 90 | 102 |
| 91 class PolicyUITest : public InProcessBrowserTest { | 103 class PolicyUITest |
| 104 : public InProcessBrowserTest, | |
| 105 public testing::WithParamInterface<UrlForFlag> { | |
| 92 public: | 106 public: |
| 93 PolicyUITest(); | 107 PolicyUITest(); |
| 94 ~PolicyUITest() override; | 108 ~PolicyUITest() override; |
| 95 | 109 |
| 110 // InProcessBrowserTest implementation. | |
|
bartfab (slow)
2015/09/23 17:17:13
Nit: The new style is simply // InProcessBrowserTe
fhorschig
2015/09/25 08:27:15
Done.
| |
| 111 void SetUpCommandLine(base::CommandLine* command_line) override; | |
| 112 | |
| 113 const char* url() const { return test_condition.url; } | |
|
bartfab (slow)
2015/09/23 17:17:13
Nit: Always pass around URLs as GURL instances.
fhorschig
2015/09/25 08:27:15
Done.
| |
| 114 | |
| 96 protected: | 115 protected: |
| 97 // InProcessBrowserTest implementation. | 116 // InProcessBrowserTest implementation. |
| 98 void SetUpInProcessBrowserTestFixture() override; | 117 void SetUpInProcessBrowserTestFixture() override; |
| 99 | 118 |
| 100 void UpdateProviderPolicy(const policy::PolicyMap& policy); | 119 void UpdateProviderPolicy(const policy::PolicyMap& policy); |
| 101 | 120 |
| 102 void VerifyPolicies(const std::vector<std::vector<std::string> >& expected); | 121 void VerifyPolicies(const std::vector<std::vector<std::string> >& expected); |
| 103 | 122 |
| 104 protected: | 123 protected: |
| 105 policy::MockConfigurationPolicyProvider provider_; | 124 policy::MockConfigurationPolicyProvider provider_; |
| 106 | 125 |
| 107 private: | 126 private: |
| 127 UrlForFlag test_condition; | |
|
bartfab (slow)
2015/09/23 17:17:13
Nit 1: Add _ suffix to member name.
Nit 2: Add bla
fhorschig
2015/09/25 08:27:15
Done.
| |
| 108 DISALLOW_COPY_AND_ASSIGN(PolicyUITest); | 128 DISALLOW_COPY_AND_ASSIGN(PolicyUITest); |
| 109 }; | 129 }; |
| 110 | 130 |
| 111 PolicyUITest::PolicyUITest() { | 131 PolicyUITest::PolicyUITest() { |
| 132 test_condition = GetParam(); | |
| 112 } | 133 } |
| 113 | 134 |
| 114 PolicyUITest::~PolicyUITest() { | 135 PolicyUITest::~PolicyUITest() { |
| 115 } | 136 } |
| 116 | 137 |
| 138 void PolicyUITest::SetUpCommandLine(base::CommandLine* command_line) { | |
| 139 InProcessBrowserTest::SetUpCommandLine(command_line); | |
| 140 if (test_condition.has_flag) | |
| 141 command_line->AppendSwitch(switches::kEnableMaterialDesignPolicyPage); | |
| 142 } | |
| 143 | |
| 117 void PolicyUITest::SetUpInProcessBrowserTestFixture() { | 144 void PolicyUITest::SetUpInProcessBrowserTestFixture() { |
| 118 EXPECT_CALL(provider_, IsInitializationComplete(_)) | 145 EXPECT_CALL(provider_, IsInitializationComplete(_)) |
| 119 .WillRepeatedly(Return(true)); | 146 .WillRepeatedly(Return(true)); |
| 120 policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_); | 147 policy::BrowserPolicyConnector::SetPolicyProviderForTesting(&provider_); |
| 121 } | 148 } |
| 122 | 149 |
| 123 void PolicyUITest::UpdateProviderPolicy(const policy::PolicyMap& policy) { | 150 void PolicyUITest::UpdateProviderPolicy(const policy::PolicyMap& policy) { |
| 124 provider_.UpdateChromePolicy(policy); | 151 provider_.UpdateChromePolicy(policy); |
| 125 base::RunLoop loop; | 152 base::RunLoop loop; |
| 126 loop.RunUntilIdle(); | 153 loop.RunUntilIdle(); |
| 127 } | 154 } |
| 128 | 155 |
| 129 void PolicyUITest::VerifyPolicies( | 156 void PolicyUITest::VerifyPolicies( |
| 130 const std::vector<std::vector<std::string> >& expected_policies) { | 157 const std::vector<std::vector<std::string> >& expected_policies) { |
| 131 ui_test_utils::NavigateToURL(browser(), GURL("chrome://policy")); | 158 ui_test_utils::NavigateToURL(browser(), GURL(url())); |
| 132 | 159 |
| 133 // Retrieve the text contents of the policy table cells for all policies. | 160 // Retrieve the text contents of the policy table cells for all policies. |
| 134 const std::string javascript = | 161 const std::string javascript = |
| 135 "var entries = document.querySelectorAll(" | 162 "var entries = document.querySelectorAll(" |
| 136 " 'section.policy-table-section > * > tbody');" | 163 " 'section.policy-table-section > * > tbody');" |
| 137 "var policies = [];" | 164 "var policies = [];" |
| 138 "for (var i = 0; i < entries.length; ++i) {" | 165 "for (var i = 0; i < entries.length; ++i) {" |
| 139 " var items = entries[i].querySelectorAll('tr > td');" | 166 " var items = entries[i].querySelectorAll('tr > td');" |
| 140 " var values = [];" | 167 " var values = [];" |
| 141 " for (var j = 0; j < items.length; ++j) {" | 168 " for (var j = 0; j < items.length; ++j) {" |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 169 ASSERT_TRUE(actual_policies->GetList(i, &actual_policy)); | 196 ASSERT_TRUE(actual_policies->GetList(i, &actual_policy)); |
| 170 ASSERT_EQ(expected_policy.size(), actual_policy->GetSize()); | 197 ASSERT_EQ(expected_policy.size(), actual_policy->GetSize()); |
| 171 for (size_t j = 0; j < expected_policy.size(); ++j) { | 198 for (size_t j = 0; j < expected_policy.size(); ++j) { |
| 172 std::string value; | 199 std::string value; |
| 173 ASSERT_TRUE(actual_policy->GetString(j, &value)); | 200 ASSERT_TRUE(actual_policy->GetString(j, &value)); |
| 174 EXPECT_EQ(expected_policy[j], value); | 201 EXPECT_EQ(expected_policy[j], value); |
| 175 } | 202 } |
| 176 } | 203 } |
| 177 } | 204 } |
| 178 | 205 |
| 179 IN_PROC_BROWSER_TEST_F(PolicyUITest, SendPolicyNames) { | 206 IN_PROC_BROWSER_TEST_P(PolicyUITest, SendPolicyNames) { |
| 180 // Verifies that the names of known policies are sent to the UI and processed | 207 // Verifies that the names of known policies are sent to the UI and processed |
| 181 // there correctly by checking that the policy table contains all policies in | 208 // there correctly by checking that the policy table contains all policies in |
| 182 // the correct order. | 209 // the correct order. |
| 183 | 210 |
| 184 // Expect that the policy table contains all known policies in alphabetical | 211 // Expect that the policy table contains all known policies in alphabetical |
| 185 // order and none of the policies have a set value. | 212 // order and none of the policies have a set value. |
| 186 std::vector<std::vector<std::string> > expected_policies; | 213 std::vector<std::vector<std::string> > expected_policies; |
| 187 policy::Schema chrome_schema = | 214 policy::Schema chrome_schema = |
| 188 policy::Schema::Wrap(policy::GetChromeSchemaData()); | 215 policy::Schema::Wrap(policy::GetChromeSchemaData()); |
| 189 ASSERT_TRUE(chrome_schema.valid()); | 216 ASSERT_TRUE(chrome_schema.valid()); |
| 190 for (policy::Schema::Iterator it = chrome_schema.GetPropertiesIterator(); | 217 for (policy::Schema::Iterator it = chrome_schema.GetPropertiesIterator(); |
| 191 !it.IsAtEnd(); it.Advance()) { | 218 !it.IsAtEnd(); it.Advance()) { |
| 192 expected_policies.push_back( | 219 expected_policies.push_back( |
| 193 PopulateExpectedPolicy( | 220 PopulateExpectedPolicy( |
| 194 it.key(), std::string(), std::string(), nullptr, false)); | 221 it.key(), std::string(), std::string(), nullptr, false)); |
| 195 } | 222 } |
| 196 | 223 |
| 197 // Retrieve the contents of the policy table from the UI and verify that it | 224 // Retrieve the contents of the policy table from the UI and verify that it |
| 198 // matches the expectation. | 225 // matches the expectation. |
| 199 VerifyPolicies(expected_policies); | 226 VerifyPolicies(expected_policies); |
| 200 } | 227 } |
| 201 | 228 |
| 202 IN_PROC_BROWSER_TEST_F(PolicyUITest, SendPolicyValues) { | 229 IN_PROC_BROWSER_TEST_P(PolicyUITest, SendPolicyValues) { |
| 203 // Verifies that policy values are sent to the UI and processed there | 230 // Verifies that policy values are sent to the UI and processed there |
| 204 // correctly by setting the values of four known and one unknown policy and | 231 // correctly by setting the values of four known and one unknown policy and |
| 205 // checking that the policy table contains the policy names, values and | 232 // checking that the policy table contains the policy names, values and |
| 206 // metadata in the correct order. | 233 // metadata in the correct order. |
| 207 policy::PolicyMap values; | 234 policy::PolicyMap values; |
| 208 std::map<std::string, std::string> expected_values; | 235 std::map<std::string, std::string> expected_values; |
| 209 | 236 |
| 210 // Set the values of four existing policies. | 237 // Set the values of four existing policies. |
| 211 base::ListValue* restore_on_startup_urls = new base::ListValue; | 238 base::ListValue* restore_on_startup_urls = new base::ListValue; |
| 212 restore_on_startup_urls->Append(new base::StringValue("aaa")); | 239 restore_on_startup_urls->Append(new base::StringValue("aaa")); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 280 expected_values[kUnknownPolicy], | 307 expected_values[kUnknownPolicy], |
| 281 "Platform", | 308 "Platform", |
| 282 values.Get(kUnknownPolicy), | 309 values.Get(kUnknownPolicy), |
| 283 true)); | 310 true)); |
| 284 | 311 |
| 285 // Retrieve the contents of the policy table from the UI and verify that it | 312 // Retrieve the contents of the policy table from the UI and verify that it |
| 286 // matches the expectation. | 313 // matches the expectation. |
| 287 VerifyPolicies(expected_policies); | 314 VerifyPolicies(expected_policies); |
| 288 } | 315 } |
| 289 | 316 |
| 290 IN_PROC_BROWSER_TEST_F(PolicyUITest, ExtensionLoadAndSendPolicy) { | 317 IN_PROC_BROWSER_TEST_P(PolicyUITest, ExtensionLoadAndSendPolicy) { |
| 291 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIPolicyURL)); | 318 ui_test_utils::NavigateToURL(browser(), GURL(url())); |
| 292 base::ScopedTempDir temp_dir_; | 319 base::ScopedTempDir temp_dir_; |
| 293 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 320 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 294 | 321 |
| 295 const std::string newly_added_policy_name = "new_policy"; | 322 const std::string newly_added_policy_name = "new_policy"; |
| 296 std::string json_data = "{\"type\": \"object\",\"properties\": {\"" + | 323 std::string json_data = "{\"type\": \"object\",\"properties\": {\"" + |
| 297 newly_added_policy_name + | 324 newly_added_policy_name + |
| 298 "\": { \"type\": \"string\"}}}"; | 325 "\": { \"type\": \"string\"}}}"; |
| 299 | 326 |
| 300 const std::string schema_file = "schema.json"; | 327 const std::string schema_file = "schema.json"; |
| 301 base::FilePath schema_path = temp_dir_.path().AppendASCII(schema_file); | 328 base::FilePath schema_path = temp_dir_.path().AppendASCII(schema_file); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 333 PopulateExpectedPolicy( | 360 PopulateExpectedPolicy( |
| 334 it.key(), std::string(), std::string(), nullptr, false)); | 361 it.key(), std::string(), std::string(), nullptr, false)); |
| 335 } | 362 } |
| 336 // Add newly added policy to expected policy list. | 363 // Add newly added policy to expected policy list. |
| 337 expected_policies.push_back(PopulateExpectedPolicy( | 364 expected_policies.push_back(PopulateExpectedPolicy( |
| 338 newly_added_policy_name, std::string(), std::string(), nullptr, false)); | 365 newly_added_policy_name, std::string(), std::string(), nullptr, false)); |
| 339 | 366 |
| 340 // Verify if policy UI includes policy that extension have. | 367 // Verify if policy UI includes policy that extension have. |
| 341 VerifyPolicies(expected_policies); | 368 VerifyPolicies(expected_policies); |
| 342 } | 369 } |
| 370 | |
| 371 INSTANTIATE_TEST_CASE_P(WithFlagPolicyUITest, | |
| 372 PolicyUITest, | |
| 373 testing::Values(kUrlsForFlags[0])); | |
| 374 | |
| 375 INSTANTIATE_TEST_CASE_P(NoFlagPolicyUITest, | |
| 376 PolicyUITest, | |
| 377 testing::Values(kUrlsForFlags[1])); | |
| OLD | NEW |