| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "chrome/browser/extensions/api/developer_private/developer_private_api.
h" | 5 #include "chrome/browser/extensions/api/developer_private/developer_private_api.
h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "chrome/browser/extensions/error_console/error_console.h" | 14 #include "chrome/browser/extensions/error_console/error_console.h" |
| 15 #include "chrome/browser/extensions/extension_function_test_utils.h" | 15 #include "chrome/browser/extensions/extension_function_test_utils.h" |
| 16 #include "chrome/browser/extensions/extension_service.h" | 16 #include "chrome/browser/extensions/extension_service.h" |
| 17 #include "chrome/browser/extensions/extension_service_test_base.h" | 17 #include "chrome/browser/extensions/extension_service_test_base.h" |
| 18 #include "chrome/browser/extensions/extension_util.h" | 18 #include "chrome/browser/extensions/extension_util.h" |
| 19 #include "chrome/browser/extensions/scripting_permissions_modifier.h" | 19 #include "chrome/browser/extensions/scripting_permissions_modifier.h" |
| 20 #include "chrome/browser/extensions/test_extension_dir.h" | 20 #include "chrome/browser/extensions/test_extension_dir.h" |
| 21 #include "chrome/browser/extensions/test_extension_system.h" | 21 #include "chrome/browser/extensions/test_extension_system.h" |
| 22 #include "chrome/browser/extensions/unpacked_installer.h" | 22 #include "chrome/browser/extensions/unpacked_installer.h" |
| 23 #include "chrome/browser/ui/browser.h" | 23 #include "chrome/browser/ui/browser.h" |
| 24 #include "chrome/common/extensions/api/developer_private.h" | 24 #include "chrome/common/extensions/api/developer_private.h" |
| 25 #include "chrome/common/pref_names.h" | 25 #include "chrome/common/pref_names.h" |
| 26 #include "chrome/test/base/test_browser_window.h" | 26 #include "chrome/test/base/test_browser_window.h" |
| 27 #include "components/crx_file/id_util.h" | 27 #include "components/crx_file/id_util.h" |
| 28 #include "components/policy/core/browser/browser_policy_connector.h" |
| 29 #include "components/policy/core/common/mock_configuration_policy_provider.h" |
| 30 #include "components/policy/core/common/policy_map.h" |
| 31 #include "components/policy/core/common/policy_service_impl.h" |
| 32 #include "components/policy/core/common/policy_types.h" |
| 33 #include "components/policy/policy_constants.h" |
| 34 #include "components/sync_preferences/testing_pref_service_syncable.h" |
| 28 #include "content/public/test/web_contents_tester.h" | 35 #include "content/public/test/web_contents_tester.h" |
| 29 #include "extensions/browser/event_router_factory.h" | 36 #include "extensions/browser/event_router_factory.h" |
| 30 #include "extensions/browser/extension_error_test_util.h" | 37 #include "extensions/browser/extension_error_test_util.h" |
| 31 #include "extensions/browser/extension_prefs.h" | 38 #include "extensions/browser/extension_prefs.h" |
| 32 #include "extensions/browser/extension_registry.h" | 39 #include "extensions/browser/extension_registry.h" |
| 33 #include "extensions/browser/extension_system.h" | 40 #include "extensions/browser/extension_system.h" |
| 34 #include "extensions/browser/test_extension_registry_observer.h" | 41 #include "extensions/browser/test_extension_registry_observer.h" |
| 35 #include "extensions/common/extension.h" | 42 #include "extensions/common/extension.h" |
| 36 #include "extensions/common/extension_builder.h" | 43 #include "extensions/common/extension_builder.h" |
| 37 #include "extensions/common/extension_set.h" | 44 #include "extensions/common/extension_set.h" |
| 38 #include "extensions/common/feature_switch.h" | 45 #include "extensions/common/feature_switch.h" |
| 39 #include "extensions/common/manifest_constants.h" | 46 #include "extensions/common/manifest_constants.h" |
| 40 #include "extensions/common/test_util.h" | 47 #include "extensions/common/test_util.h" |
| 41 #include "extensions/common/value_builder.h" | 48 #include "extensions/common/value_builder.h" |
| 42 | 49 |
| 50 using testing::Return; |
| 51 using testing::_; |
| 52 |
| 43 namespace extensions { | 53 namespace extensions { |
| 44 | 54 |
| 45 namespace { | 55 namespace { |
| 46 | 56 |
| 47 std::unique_ptr<KeyedService> BuildAPI(content::BrowserContext* context) { | 57 std::unique_ptr<KeyedService> BuildAPI(content::BrowserContext* context) { |
| 48 return base::MakeUnique<DeveloperPrivateAPI>(context); | 58 return base::MakeUnique<DeveloperPrivateAPI>(context); |
| 49 } | 59 } |
| 50 | 60 |
| 51 std::unique_ptr<KeyedService> BuildEventRouter( | 61 std::unique_ptr<KeyedService> BuildEventRouter( |
| 52 content::BrowserContext* profile) { | 62 content::BrowserContext* profile) { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 // Tests modifying the extension's configuration. | 98 // Tests modifying the extension's configuration. |
| 89 void TestExtensionPrefSetting(const base::Callback<bool()>& has_pref, | 99 void TestExtensionPrefSetting(const base::Callback<bool()>& has_pref, |
| 90 const std::string& key, | 100 const std::string& key, |
| 91 const std::string& extension_id); | 101 const std::string& extension_id); |
| 92 | 102 |
| 93 testing::AssertionResult TestPackExtensionFunction( | 103 testing::AssertionResult TestPackExtensionFunction( |
| 94 const base::ListValue& args, | 104 const base::ListValue& args, |
| 95 api::developer_private::PackStatus expected_status, | 105 api::developer_private::PackStatus expected_status, |
| 96 int expected_flags); | 106 int expected_flags); |
| 97 | 107 |
| 108 // Execute the updateProfileConfiguration API call with a specified |
| 109 // dev_mode. This is done from the webui when the user checks the |
| 110 // "Developer Mode" checkbox. |
| 111 void UpdateProfileConfigurationDevMode(bool dev_mode); |
| 112 |
| 113 // Execute the getProfileConfiguration API and parse its result into a |
| 114 // ProfileInfo structure for further verification in the calling test. |
| 115 // Will reset the profile_info unique_ptr. |
| 116 // Uses ASSERT_* inside - callers should use ASSERT_NO_FATAL_FAILURE. |
| 117 void GetProfileConfiguration( |
| 118 std::unique_ptr<api::developer_private::ProfileInfo>* profile_info); |
| 119 |
| 98 Browser* browser() { return browser_.get(); } | 120 Browser* browser() { return browser_.get(); } |
| 99 | 121 |
| 100 private: | 122 private: |
| 101 // ExtensionServiceTestBase: | 123 // ExtensionServiceTestBase: |
| 102 void SetUp() override; | 124 void SetUp() override; |
| 103 void TearDown() override; | 125 void TearDown() override; |
| 104 | 126 |
| 105 // The browser (and accompanying window). | 127 // The browser (and accompanying window). |
| 106 std::unique_ptr<TestBrowserWindow> browser_window_; | 128 std::unique_ptr<TestBrowserWindow> browser_window_; |
| 107 std::unique_ptr<Browser> browser_; | 129 std::unique_ptr<Browser> browser_; |
| 108 | 130 |
| 109 std::vector<std::unique_ptr<TestExtensionDir>> test_extension_dirs_; | 131 std::vector<std::unique_ptr<TestExtensionDir>> test_extension_dirs_; |
| 132 policy::MockConfigurationPolicyProvider mock_policy_provider_; |
| 110 | 133 |
| 111 DISALLOW_COPY_AND_ASSIGN(DeveloperPrivateApiUnitTest); | 134 DISALLOW_COPY_AND_ASSIGN(DeveloperPrivateApiUnitTest); |
| 112 }; | 135 }; |
| 113 | 136 |
| 114 bool DeveloperPrivateApiUnitTest::RunFunction( | 137 bool DeveloperPrivateApiUnitTest::RunFunction( |
| 115 const scoped_refptr<UIThreadExtensionFunction>& function, | 138 const scoped_refptr<UIThreadExtensionFunction>& function, |
| 116 const base::ListValue& args) { | 139 const base::ListValue& args) { |
| 117 return extension_function_test_utils::RunFunction( | 140 return extension_function_test_utils::RunFunction( |
| 118 function.get(), args.CreateDeepCopy(), browser(), | 141 function.get(), args.CreateDeepCopy(), browser(), |
| 119 extension_function_test_utils::NONE); | 142 extension_function_test_utils::NONE); |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 } | 258 } |
| 236 | 259 |
| 237 if (response->override_flags != expected_flags) { | 260 if (response->override_flags != expected_flags) { |
| 238 return testing::AssertionFailure() << "Expected flags: " << | 261 return testing::AssertionFailure() << "Expected flags: " << |
| 239 expected_flags << ", found flags: " << response->override_flags; | 262 expected_flags << ", found flags: " << response->override_flags; |
| 240 } | 263 } |
| 241 | 264 |
| 242 return testing::AssertionSuccess(); | 265 return testing::AssertionSuccess(); |
| 243 } | 266 } |
| 244 | 267 |
| 268 void DeveloperPrivateApiUnitTest::UpdateProfileConfigurationDevMode( |
| 269 bool dev_mode) { |
| 270 scoped_refptr<UIThreadExtensionFunction> function( |
| 271 new api::DeveloperPrivateUpdateProfileConfigurationFunction()); |
| 272 std::unique_ptr<base::ListValue> args = |
| 273 ListBuilder() |
| 274 .Append(DictionaryBuilder() |
| 275 .SetBoolean("inDeveloperMode", dev_mode) |
| 276 .Build()) |
| 277 .Build(); |
| 278 EXPECT_TRUE(RunFunction(function, *args)) << function->GetError(); |
| 279 } |
| 280 |
| 281 void DeveloperPrivateApiUnitTest::GetProfileConfiguration( |
| 282 std::unique_ptr<api::developer_private::ProfileInfo>* profile_info) { |
| 283 scoped_refptr<UIThreadExtensionFunction> function( |
| 284 new api::DeveloperPrivateGetProfileConfigurationFunction()); |
| 285 base::ListValue args; |
| 286 EXPECT_TRUE(RunFunction(function, args)) << function->GetError(); |
| 287 |
| 288 ASSERT_TRUE(function->GetResultList()); |
| 289 ASSERT_EQ(1u, function->GetResultList()->GetSize()); |
| 290 const base::Value* response_value = nullptr; |
| 291 function->GetResultList()->Get(0u, &response_value); |
| 292 *profile_info = |
| 293 api::developer_private::ProfileInfo::FromValue(*response_value); |
| 294 } |
| 295 |
| 245 void DeveloperPrivateApiUnitTest::SetUp() { | 296 void DeveloperPrivateApiUnitTest::SetUp() { |
| 246 ExtensionServiceTestBase::SetUp(); | 297 ExtensionServiceTestBase::SetUp(); |
| 247 InitializeEmptyExtensionService(); | 298 |
| 299 // By not specifying a pref_file filepath, we get a |
| 300 // sync_preferences::TestingPrefServiceSyncable |
| 301 // - see BuildTestingProfile in extension_service_test_base.cc. |
| 302 ExtensionServiceInitParams init_params = CreateDefaultInitParams(); |
| 303 init_params.pref_file.clear(); |
| 304 InitializeExtensionService(init_params); |
| 248 | 305 |
| 249 browser_window_.reset(new TestBrowserWindow()); | 306 browser_window_.reset(new TestBrowserWindow()); |
| 250 Browser::CreateParams params(profile()); | 307 Browser::CreateParams params(profile()); |
| 251 params.type = Browser::TYPE_TABBED; | 308 params.type = Browser::TYPE_TABBED; |
| 252 params.window = browser_window_.get(); | 309 params.window = browser_window_.get(); |
| 253 browser_.reset(new Browser(params)); | 310 browser_.reset(new Browser(params)); |
| 254 | 311 |
| 255 // Allow the API to be created. | 312 // Allow the API to be created. |
| 256 EventRouterFactory::GetInstance()->SetTestingFactory(profile(), | 313 EventRouterFactory::GetInstance()->SetTestingFactory(profile(), |
| 257 &BuildEventRouter); | 314 &BuildEventRouter); |
| (...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 569 ListBuilder() | 626 ListBuilder() |
| 570 .Append( | 627 .Append( |
| 571 DictionaryBuilder().Set("extensionId", extension->id()).Build()) | 628 DictionaryBuilder().Set("extensionId", extension->id()).Build()) |
| 572 .Build(); | 629 .Build(); |
| 573 function = new api::DeveloperPrivateDeleteExtensionErrorsFunction(); | 630 function = new api::DeveloperPrivateDeleteExtensionErrorsFunction(); |
| 574 EXPECT_TRUE(RunFunction(function, *args)) << function->GetError(); | 631 EXPECT_TRUE(RunFunction(function, *args)) << function->GetError(); |
| 575 // No more errors! | 632 // No more errors! |
| 576 EXPECT_TRUE(error_console->GetErrorsForExtension(extension->id()).empty()); | 633 EXPECT_TRUE(error_console->GetErrorsForExtension(extension->id()).empty()); |
| 577 } | 634 } |
| 578 | 635 |
| 636 // Test developerPrivate.updateProfileConfiguration: Try to turn on devMode |
| 637 // when DeveloperToolsDisabled policy is active. |
| 638 TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateDevModeDisabledPolicy) { |
| 639 testing_pref_service()->SetManagedPref(prefs::kExtensionsUIDeveloperMode, |
| 640 new base::FundamentalValue(false)); |
| 641 |
| 642 UpdateProfileConfigurationDevMode(true); |
| 643 |
| 644 EXPECT_FALSE( |
| 645 profile()->GetPrefs()->GetBoolean(prefs::kExtensionsUIDeveloperMode)); |
| 646 |
| 647 std::unique_ptr<api::developer_private::ProfileInfo> profile_info; |
| 648 ASSERT_NO_FATAL_FAILURE(GetProfileConfiguration(&profile_info)); |
| 649 EXPECT_FALSE(profile_info->in_developer_mode); |
| 650 EXPECT_TRUE(profile_info->is_developer_mode_controlled_by_policy); |
| 651 } |
| 652 |
| 653 // Test developerPrivate.updateProfileConfiguration: Try to turn on devMode |
| 654 // (without DeveloperToolsDisabled policy). |
| 655 TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateDevMode) { |
| 656 EXPECT_FALSE( |
| 657 profile()->GetPrefs()->GetBoolean(prefs::kExtensionsUIDeveloperMode)); |
| 658 |
| 659 UpdateProfileConfigurationDevMode(true); |
| 660 |
| 661 EXPECT_TRUE( |
| 662 profile()->GetPrefs()->GetBoolean(prefs::kExtensionsUIDeveloperMode)); |
| 663 |
| 664 std::unique_ptr<api::developer_private::ProfileInfo> profile_info; |
| 665 ASSERT_NO_FATAL_FAILURE(GetProfileConfiguration(&profile_info)); |
| 666 EXPECT_TRUE(profile_info->in_developer_mode); |
| 667 EXPECT_FALSE(profile_info->is_developer_mode_controlled_by_policy); |
| 668 } |
| 669 |
| 579 } // namespace extensions | 670 } // namespace extensions |
| OLD | NEW |