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

Side by Side Diff: chrome/browser/extensions/api/developer_private/developer_private_api_unittest.cc

Issue 2529083002: Make extensions developer mode adhere to policy (Closed)
Patch Set: Reviewers' comments Created 4 years 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
OLDNEW
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
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 std::unique_ptr<api::developer_private::ProfileInfo>
116 GetProfileConfiguration();
117
98 Browser* browser() { return browser_.get(); } 118 Browser* browser() { return browser_.get(); }
99 119
100 private: 120 private:
101 // ExtensionServiceTestBase: 121 // ExtensionServiceTestBase:
102 void SetUp() override; 122 void SetUp() override;
103 void TearDown() override; 123 void TearDown() override;
104 124
105 // The browser (and accompanying window). 125 // The browser (and accompanying window).
106 std::unique_ptr<TestBrowserWindow> browser_window_; 126 std::unique_ptr<TestBrowserWindow> browser_window_;
107 std::unique_ptr<Browser> browser_; 127 std::unique_ptr<Browser> browser_;
108 128
109 std::vector<std::unique_ptr<TestExtensionDir>> test_extension_dirs_; 129 std::vector<std::unique_ptr<TestExtensionDir>> test_extension_dirs_;
130 policy::MockConfigurationPolicyProvider mock_policy_provider_;
110 131
111 DISALLOW_COPY_AND_ASSIGN(DeveloperPrivateApiUnitTest); 132 DISALLOW_COPY_AND_ASSIGN(DeveloperPrivateApiUnitTest);
112 }; 133 };
113 134
114 bool DeveloperPrivateApiUnitTest::RunFunction( 135 bool DeveloperPrivateApiUnitTest::RunFunction(
115 const scoped_refptr<UIThreadExtensionFunction>& function, 136 const scoped_refptr<UIThreadExtensionFunction>& function,
116 const base::ListValue& args) { 137 const base::ListValue& args) {
117 return extension_function_test_utils::RunFunction( 138 return extension_function_test_utils::RunFunction(
118 function.get(), args.CreateDeepCopy(), browser(), 139 function.get(), args.CreateDeepCopy(), browser(),
119 extension_function_test_utils::NONE); 140 extension_function_test_utils::NONE);
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
235 } 256 }
236 257
237 if (response->override_flags != expected_flags) { 258 if (response->override_flags != expected_flags) {
238 return testing::AssertionFailure() << "Expected flags: " << 259 return testing::AssertionFailure() << "Expected flags: " <<
239 expected_flags << ", found flags: " << response->override_flags; 260 expected_flags << ", found flags: " << response->override_flags;
240 } 261 }
241 262
242 return testing::AssertionSuccess(); 263 return testing::AssertionSuccess();
243 } 264 }
244 265
266 void DeveloperPrivateApiUnitTest::UpdateProfileConfigurationDevMode(
267 bool dev_mode) {
268 scoped_refptr<UIThreadExtensionFunction> function(
269 new api::DeveloperPrivateUpdateProfileConfigurationFunction());
270 std::unique_ptr<base::ListValue> args =
271 ListBuilder()
272 .Append(DictionaryBuilder()
273 .SetBoolean("inDeveloperMode", dev_mode)
274 .Build())
275 .Build();
276 EXPECT_TRUE(RunFunction(function, *args)) << function->GetError();
277 }
278
279 std::unique_ptr<api::developer_private::ProfileInfo>
280 DeveloperPrivateApiUnitTest::GetProfileConfiguration() {
281 scoped_refptr<UIThreadExtensionFunction> function(
282 new api::DeveloperPrivateGetProfileConfigurationFunction());
283 base::ListValue args;
284 EXPECT_TRUE(RunFunction(function, args)) << function->GetError();
285
286 EXPECT_TRUE(function->GetResultList());
Andrew T Wilson (Slow) 2016/12/04 14:54:54 ASSERT_TRUE() because your test will crash on the
pmarko 2016/12/07 16:12:11 Good point - had have to change the method type to
287 EXPECT_EQ(1u, function->GetResultList()->GetSize());
Andrew T Wilson (Slow) 2016/12/04 14:54:54 I'd probably also ASSERT_EQ or ASSERT_GE here beca
Andrew T Wilson (Slow) 2016/12/04 14:54:54 I'd probably also ASSERT_EQ or ASSERT_GE here beca
pmarko 2016/12/07 16:12:11 Done - taking ASSERT_EQ because the API is suppose
288 const base::Value* response_value = nullptr;
289 function->GetResultList()->Get(0u, &response_value);
290 std::unique_ptr<api::developer_private::ProfileInfo> response =
291 api::developer_private::ProfileInfo::FromValue(*response_value);
292 return response;
293 }
294
245 void DeveloperPrivateApiUnitTest::SetUp() { 295 void DeveloperPrivateApiUnitTest::SetUp() {
246 ExtensionServiceTestBase::SetUp(); 296 ExtensionServiceTestBase::SetUp();
247 InitializeEmptyExtensionService(); 297
298 // By not specifying a pref_file filepath, we get a
299 // sync_preferences::TestingPrefServiceSyncable
300 // - see BuildTestingProfile in extension_service_test_base.cc.
301 ExtensionServiceInitParams init_params = CreateDefaultInitParams();
302 init_params.pref_file.clear();
303 InitializeExtensionService(init_params);
248 304
249 browser_window_.reset(new TestBrowserWindow()); 305 browser_window_.reset(new TestBrowserWindow());
250 Browser::CreateParams params(profile()); 306 Browser::CreateParams params(profile());
251 params.type = Browser::TYPE_TABBED; 307 params.type = Browser::TYPE_TABBED;
252 params.window = browser_window_.get(); 308 params.window = browser_window_.get();
253 browser_.reset(new Browser(params)); 309 browser_.reset(new Browser(params));
254 310
255 // Allow the API to be created. 311 // Allow the API to be created.
256 EventRouterFactory::GetInstance()->SetTestingFactory(profile(), 312 EventRouterFactory::GetInstance()->SetTestingFactory(profile(),
257 &BuildEventRouter); 313 &BuildEventRouter);
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after
569 ListBuilder() 625 ListBuilder()
570 .Append( 626 .Append(
571 DictionaryBuilder().Set("extensionId", extension->id()).Build()) 627 DictionaryBuilder().Set("extensionId", extension->id()).Build())
572 .Build(); 628 .Build();
573 function = new api::DeveloperPrivateDeleteExtensionErrorsFunction(); 629 function = new api::DeveloperPrivateDeleteExtensionErrorsFunction();
574 EXPECT_TRUE(RunFunction(function, *args)) << function->GetError(); 630 EXPECT_TRUE(RunFunction(function, *args)) << function->GetError();
575 // No more errors! 631 // No more errors!
576 EXPECT_TRUE(error_console->GetErrorsForExtension(extension->id()).empty()); 632 EXPECT_TRUE(error_console->GetErrorsForExtension(extension->id()).empty());
577 } 633 }
578 634
635 // Test developerPrivate.updateProfileConfiguration
636 // trying to turn on devMode when DeveloperToolsDisabled policy is active
637 TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateDevModeDisabledPolicy) {
638 testing_pref_service()->SetManagedPref(
639 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 GetProfileConfiguration();
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
654 // trying to turn on devMode
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 GetProfileConfiguration();
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698