1 // Copyright 2012 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.
5 #include "base/memory/scoped_ptr.h"
6 #include "base/message_loop/message_loop.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "base/values.h"
9 #include "chrome/browser/extensions/blacklist.h"
10 #include "chrome/browser/extensions/extension_management.h"
11 #include "chrome/browser/extensions/standard_management_policy_provider.h"
12 #include "chrome/browser/extensions/test_extension_prefs.h"
13 #include "content/public/test/test_browser_thread.h"
14 #include "extensions/browser/extension_prefs.h"
15 #include "extensions/common/manifest_constants.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 namespace extensions
{
20 class StandardManagementPolicyProviderTest
: public testing::Test
{
22 StandardManagementPolicyProviderTest()
23 : ui_thread_(content::BrowserThread::UI
, &message_loop_
),
24 file_thread_(content::BrowserThread::FILE, &message_loop_
),
25 prefs_(message_loop_
.task_runner().get()),
26 settings_(new ExtensionManagement(prefs()->pref_service())),
27 provider_(settings_
.get()) {}
30 ExtensionPrefs
* prefs() {
31 return prefs_
.prefs();
34 scoped_refptr
<const Extension
> CreateExtension(Manifest::Location location
,
36 base::DictionaryValue values
;
37 values
.SetString(manifest_keys::kName
, "test");
38 values
.SetString(manifest_keys::kVersion
, "0.1");
40 scoped_refptr
<const Extension
> extension
= Extension::Create(
41 base::FilePath(), location
, values
, Extension::NO_FLAGS
, &error
);
42 CHECK(extension
.get()) << error
;
46 base::MessageLoop message_loop_
;
47 content::TestBrowserThread ui_thread_
;
48 content::TestBrowserThread file_thread_
;
50 TestExtensionPrefs prefs_
;
51 scoped_ptr
<ExtensionManagement
> settings_
;
53 StandardManagementPolicyProvider provider_
;
56 // Tests the behavior of the ManagementPolicy provider methods for an
57 // extension required by policy.
58 TEST_F(StandardManagementPolicyProviderTest
, RequiredExtension
) {
59 scoped_refptr
<const Extension
> extension
=
60 CreateExtension(Manifest::EXTERNAL_POLICY_DOWNLOAD
, true);
62 base::string16 error16
;
63 EXPECT_TRUE(provider_
.UserMayLoad(extension
.get(), &error16
));
64 EXPECT_EQ(base::string16(), error16
);
66 // We won't check the exact wording of the error, but it should say
68 EXPECT_FALSE(provider_
.UserMayModifySettings(extension
.get(), &error16
));
69 EXPECT_NE(base::string16(), error16
);
70 EXPECT_TRUE(provider_
.MustRemainEnabled(extension
.get(), &error16
));
71 EXPECT_NE(base::string16(), error16
);
74 // Tests the behavior of the ManagementPolicy provider methods for an
75 // extension required by policy.
76 TEST_F(StandardManagementPolicyProviderTest
, NotRequiredExtension
) {
77 scoped_refptr
<const Extension
> extension
=
78 CreateExtension(Manifest::INTERNAL
, false);
80 base::string16 error16
;
81 EXPECT_TRUE(provider_
.UserMayLoad(extension
.get(), &error16
));
82 EXPECT_EQ(base::string16(), error16
);
83 EXPECT_TRUE(provider_
.UserMayModifySettings(extension
.get(), &error16
));
84 EXPECT_EQ(base::string16(), error16
);
85 EXPECT_FALSE(provider_
.MustRemainEnabled(extension
.get(), &error16
));
86 EXPECT_EQ(base::string16(), error16
);
89 } // namespace extensions