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

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: Fix and improve api unittest 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 void UpdateProfileConfigurationDevMode(bool devMode);
Devlin 2016/12/02 20:47:39 nit: add descriptive function comment nit: dev_mod
pmarko 2016/12/07 16:12:11 Done.
109
110 std::unique_ptr<api::developer_private::ProfileInfo>
Devlin 2016/12/02 20:47:39 nit: add comment
pmarko 2016/12/07 16:12:11 Done.
111 GetProfileConfiguration();
112
98 Browser* browser() { return browser_.get(); } 113 Browser* browser() { return browser_.get(); }
99 114
100 private: 115 private:
101 // ExtensionServiceTestBase: 116 // ExtensionServiceTestBase:
102 void SetUp() override; 117 void SetUp() override;
103 void TearDown() override; 118 void TearDown() override;
104 119
105 // The browser (and accompanying window). 120 // The browser (and accompanying window).
106 std::unique_ptr<TestBrowserWindow> browser_window_; 121 std::unique_ptr<TestBrowserWindow> browser_window_;
107 std::unique_ptr<Browser> browser_; 122 std::unique_ptr<Browser> browser_;
108 123
109 std::vector<std::unique_ptr<TestExtensionDir>> test_extension_dirs_; 124 std::vector<std::unique_ptr<TestExtensionDir>> test_extension_dirs_;
125 policy::MockConfigurationPolicyProvider mock_policy_provider_;
110 126
111 DISALLOW_COPY_AND_ASSIGN(DeveloperPrivateApiUnitTest); 127 DISALLOW_COPY_AND_ASSIGN(DeveloperPrivateApiUnitTest);
112 }; 128 };
113 129
114 bool DeveloperPrivateApiUnitTest::RunFunction( 130 bool DeveloperPrivateApiUnitTest::RunFunction(
115 const scoped_refptr<UIThreadExtensionFunction>& function, 131 const scoped_refptr<UIThreadExtensionFunction>& function,
116 const base::ListValue& args) { 132 const base::ListValue& args) {
117 return extension_function_test_utils::RunFunction( 133 return extension_function_test_utils::RunFunction(
118 function.get(), args.CreateDeepCopy(), browser(), 134 function.get(), args.CreateDeepCopy(), browser(),
119 extension_function_test_utils::NONE); 135 extension_function_test_utils::NONE);
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
235 } 251 }
236 252
237 if (response->override_flags != expected_flags) { 253 if (response->override_flags != expected_flags) {
238 return testing::AssertionFailure() << "Expected flags: " << 254 return testing::AssertionFailure() << "Expected flags: " <<
239 expected_flags << ", found flags: " << response->override_flags; 255 expected_flags << ", found flags: " << response->override_flags;
240 } 256 }
241 257
242 return testing::AssertionSuccess(); 258 return testing::AssertionSuccess();
243 } 259 }
244 260
261 void DeveloperPrivateApiUnitTest::UpdateProfileConfigurationDevMode(
262 bool devMode) {
Devlin 2016/12/02 20:47:39 (here too, dev_mode)
pmarko 2016/12/07 16:12:11 Done.
263 scoped_refptr<UIThreadExtensionFunction> function(
264 new api::DeveloperPrivateUpdateProfileConfigurationFunction());
265 std::unique_ptr<base::ListValue> args =
266 ListBuilder()
267 .Append(DictionaryBuilder()
268 .SetBoolean("inDeveloperMode", devMode)
269 .Build())
270 .Build();
271 EXPECT_TRUE(RunFunction(function, *args)) << function->GetError();
272 }
273
274 std::unique_ptr<api::developer_private::ProfileInfo>
275 DeveloperPrivateApiUnitTest::GetProfileConfiguration() {
276 scoped_refptr<UIThreadExtensionFunction> function(
277 new api::DeveloperPrivateGetProfileConfigurationFunction());
278 base::ListValue args;
279 EXPECT_TRUE(RunFunction(function, args)) << function->GetError();
280
281 EXPECT_TRUE(function->GetResultList());
282 EXPECT_EQ(1u, function->GetResultList()->GetSize());
283 const base::Value* response_value = nullptr;
284 function->GetResultList()->Get(0u, &response_value);
285 std::unique_ptr<api::developer_private::ProfileInfo> response =
286 api::developer_private::ProfileInfo::FromValue(*response_value);
287 return response;
288 }
289
245 void DeveloperPrivateApiUnitTest::SetUp() { 290 void DeveloperPrivateApiUnitTest::SetUp() {
246 ExtensionServiceTestBase::SetUp(); 291 ExtensionServiceTestBase::SetUp();
247 InitializeEmptyExtensionService(); 292
293 // By not specifying a pref_file filepath, we get a
294 // sync_preferences::TestingPrefServiceSyncable
295 // - see BuildTestingProfile in extension_service_test_base.cc.
296 ExtensionServiceInitParams init_params = CreateDefaultInitParams();
297 init_params.pref_file.clear();
298 InitializeExtensionService(init_params);
248 299
249 browser_window_.reset(new TestBrowserWindow()); 300 browser_window_.reset(new TestBrowserWindow());
250 Browser::CreateParams params(profile()); 301 Browser::CreateParams params(profile());
251 params.type = Browser::TYPE_TABBED; 302 params.type = Browser::TYPE_TABBED;
252 params.window = browser_window_.get(); 303 params.window = browser_window_.get();
253 browser_.reset(new Browser(params)); 304 browser_.reset(new Browser(params));
254 305
255 // Allow the API to be created. 306 // Allow the API to be created.
256 EventRouterFactory::GetInstance()->SetTestingFactory(profile(), 307 EventRouterFactory::GetInstance()->SetTestingFactory(profile(),
257 &BuildEventRouter); 308 &BuildEventRouter);
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after
569 ListBuilder() 620 ListBuilder()
570 .Append( 621 .Append(
571 DictionaryBuilder().Set("extensionId", extension->id()).Build()) 622 DictionaryBuilder().Set("extensionId", extension->id()).Build())
572 .Build(); 623 .Build();
573 function = new api::DeveloperPrivateDeleteExtensionErrorsFunction(); 624 function = new api::DeveloperPrivateDeleteExtensionErrorsFunction();
574 EXPECT_TRUE(RunFunction(function, *args)) << function->GetError(); 625 EXPECT_TRUE(RunFunction(function, *args)) << function->GetError();
575 // No more errors! 626 // No more errors!
576 EXPECT_TRUE(error_console->GetErrorsForExtension(extension->id()).empty()); 627 EXPECT_TRUE(error_console->GetErrorsForExtension(extension->id()).empty());
577 } 628 }
578 629
630 // Test developerPrivate.updateProfileConfiguration
631 // trying to turn on devMode when DeveloperToolsDisabled policy is active
632 TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateDevModeDisabledPolicy) {
633 testing_pref_service()->SetManagedPref(
634 prefs::kExtensionsUIDeveloperMode,
635 new base::FundamentalValue(false));
636
637 UpdateProfileConfigurationDevMode(true);
638
639 EXPECT_FALSE(
640 profile()->GetPrefs()->GetBoolean(prefs::kExtensionsUIDeveloperMode));
641
642 std::unique_ptr<api::developer_private::ProfileInfo> profile_info =
643 GetProfileConfiguration();
644 EXPECT_FALSE(profile_info->in_developer_mode);
645 EXPECT_TRUE(profile_info->is_developer_mode_controlled_by_policy);
646 }
647
648 // Test developerPrivate.updateProfileConfiguration
649 // trying to turn on devMode
650 TEST_F(DeveloperPrivateApiUnitTest, DeveloperPrivateDevMode) {
651 EXPECT_FALSE(
652 profile()->GetPrefs()->GetBoolean(prefs::kExtensionsUIDeveloperMode));
653
654 UpdateProfileConfigurationDevMode(true);
655
656 EXPECT_TRUE(
657 profile()->GetPrefs()->GetBoolean(prefs::kExtensionsUIDeveloperMode));
658
659 std::unique_ptr<api::developer_private::ProfileInfo> profile_info =
660 GetProfileConfiguration();
661 EXPECT_TRUE(profile_info->in_developer_mode);
662 EXPECT_FALSE(profile_info->is_developer_mode_controlled_by_policy);
663 }
664
579 } // namespace extensions 665 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698