1 // Copyright 2013 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 // These tests make sure SettingsOverridePermission values are set correctly.
7 #include "base/basictypes.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/values.h"
10 #include "chrome/common/extensions/manifest_tests/extension_manifest_test.h"
11 #include "extensions/common/manifest_constants.h"
12 #include "extensions/common/permissions/permission_set.h"
13 #include "extensions/common/permissions/permissions_data.h"
14 #include "extensions/common/permissions/settings_override_permission.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace extensions
{
21 class SettingsOverridePermissionTest
: public ExtensionManifestTest
{
25 kStartupPages
= 1 << 1,
26 kSearchProvider
= 1 << 2,
29 scoped_refptr
<Extension
> GetPermissionSet(uint32 flags
) {
30 base::DictionaryValue ext_manifest
;
31 ext_manifest
.SetString(manifest_keys::kName
, "test");
32 ext_manifest
.SetString(manifest_keys::kVersion
, "0.1");
33 ext_manifest
.SetInteger(manifest_keys::kManifestVersion
, 2);
35 scoped_ptr
<base::DictionaryValue
> settings_override(
36 new base::DictionaryValue
);
37 if (flags
& kHomepage
)
38 settings_override
->SetString("homepage", "http://www.google.com");
39 if (flags
& kStartupPages
) {
40 scoped_ptr
<base::ListValue
> startup_pages(new base::ListValue
);
41 startup_pages
->AppendString("http://startup.com/startup.html");
42 settings_override
->Set("startup_pages", startup_pages
.release());
44 if (flags
& kSearchProvider
) {
45 scoped_ptr
<base::DictionaryValue
> search_provider(
46 new base::DictionaryValue
);
47 search_provider
->SetString("search_url", "http://google.com/search.html");
48 search_provider
->SetString("name", "test");
49 search_provider
->SetString("keyword", "lock");
50 search_provider
->SetString("encoding", "UTF-8");
51 search_provider
->SetBoolean("is_default", true);
52 search_provider
->SetString("favicon_url",
53 "http://wikipedia.org/wiki/Favicon");
54 settings_override
->Set("search_provider", search_provider
.release());
57 manifest_keys::kSettingsOverride
, settings_override
.release());
59 Manifest
manifest(&ext_manifest
, "test");
60 return LoadAndExpectSuccess(manifest
);
64 TEST_F(SettingsOverridePermissionTest
, HomePage
) {
65 scoped_refptr
<Extension
> extension(GetPermissionSet(kHomepage
));
66 scoped_refptr
<const PermissionSet
> permission_set(
67 extension
->GetActivePermissions());
70 EXPECT_TRUE(permission_set
->HasAPIPermission(APIPermission::kHomepage
));
71 std::vector
<base::string16
> warnings
=
72 PermissionsData::GetPermissionMessageStrings(extension
.get());
73 ASSERT_EQ(1u, warnings
.size());
74 EXPECT_EQ("Change your home page to: google.com/",
75 base::UTF16ToUTF8(warnings
[0]));
77 EXPECT_FALSE(permission_set
->HasAPIPermission(APIPermission::kStartupPages
));
78 EXPECT_FALSE(permission_set
->HasAPIPermission(
79 APIPermission::kSearchProvider
));
81 EXPECT_FALSE(permission_set
->HasAPIPermission(APIPermission::kHomepage
));
82 EXPECT_FALSE(permission_set
->HasAPIPermission(APIPermission::kStartupPages
));
84 permission_set
->HasAPIPermission(APIPermission::kSearchProvider
));
88 TEST_F(SettingsOverridePermissionTest
, StartupPages
) {
89 scoped_refptr
<Extension
> extension(GetPermissionSet(kStartupPages
));
90 scoped_refptr
<const PermissionSet
> permission_set(
91 extension
->GetActivePermissions());
94 EXPECT_TRUE(permission_set
->HasAPIPermission(APIPermission::kStartupPages
));
95 std::vector
<base::string16
> warnings
=
96 PermissionsData::GetPermissionMessageStrings(extension
.get());
97 ASSERT_EQ(1u, warnings
.size());
98 EXPECT_EQ("Change your start page to: startup.com/startup.html",
99 base::UTF16ToUTF8(warnings
[0]));
101 EXPECT_FALSE(permission_set
->HasAPIPermission(APIPermission::kHomepage
));
102 EXPECT_FALSE(permission_set
->HasAPIPermission(
103 APIPermission::kSearchProvider
));
105 EXPECT_FALSE(permission_set
->HasAPIPermission(APIPermission::kHomepage
));
106 EXPECT_FALSE(permission_set
->HasAPIPermission(APIPermission::kStartupPages
));
108 permission_set
->HasAPIPermission(APIPermission::kSearchProvider
));
112 TEST_F(SettingsOverridePermissionTest
, SearchSettings
) {
113 scoped_refptr
<Extension
> extension(GetPermissionSet(kSearchProvider
));
114 scoped_refptr
<const PermissionSet
> permission_set(
115 extension
->GetActivePermissions());
118 EXPECT_TRUE(permission_set
->HasAPIPermission(APIPermission::kSearchProvider
));
119 std::vector
<base::string16
> warnings
=
120 PermissionsData::GetPermissionMessageStrings(extension
.get());
121 ASSERT_EQ(1u, warnings
.size());
122 EXPECT_EQ("Change your search settings to: google.com",
123 base::UTF16ToUTF8(warnings
[0]));
125 EXPECT_FALSE(permission_set
->HasAPIPermission(APIPermission::kHomepage
));
126 EXPECT_FALSE(permission_set
->HasAPIPermission(APIPermission::kStartupPages
));
128 EXPECT_FALSE(permission_set
->HasAPIPermission(APIPermission::kHomepage
));
129 EXPECT_FALSE(permission_set
->HasAPIPermission(APIPermission::kStartupPages
));
131 permission_set
->HasAPIPermission(APIPermission::kSearchProvider
));
135 TEST_F(SettingsOverridePermissionTest
, All
) {
136 scoped_refptr
<Extension
> extension(GetPermissionSet(
137 kSearchProvider
| kStartupPages
| kHomepage
));
138 scoped_refptr
<const PermissionSet
> permission_set(
139 extension
->GetActivePermissions());
142 EXPECT_TRUE(permission_set
->HasAPIPermission(APIPermission::kSearchProvider
));
143 EXPECT_TRUE(permission_set
->HasAPIPermission(APIPermission::kHomepage
));
144 EXPECT_TRUE(permission_set
->HasAPIPermission(APIPermission::kStartupPages
));
146 EXPECT_FALSE(permission_set
->HasAPIPermission(APIPermission::kHomepage
));
147 EXPECT_FALSE(permission_set
->HasAPIPermission(APIPermission::kStartupPages
));
149 permission_set
->HasAPIPermission(APIPermission::kSearchProvider
));
153 TEST_F(SettingsOverridePermissionTest
, Some
) {
154 scoped_refptr
<Extension
> extension(GetPermissionSet(
155 kSearchProvider
| kHomepage
));
156 scoped_refptr
<const PermissionSet
> permission_set(
157 extension
->GetActivePermissions());
160 EXPECT_TRUE(permission_set
->HasAPIPermission(APIPermission::kSearchProvider
));
161 EXPECT_TRUE(permission_set
->HasAPIPermission(APIPermission::kHomepage
));
162 EXPECT_FALSE(permission_set
->HasAPIPermission(APIPermission::kStartupPages
));
164 EXPECT_FALSE(permission_set
->HasAPIPermission(APIPermission::kHomepage
));
165 EXPECT_FALSE(permission_set
->HasAPIPermission(APIPermission::kStartupPages
));
167 permission_set
->HasAPIPermission(APIPermission::kSearchProvider
));
173 } // namespace extensions