Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / common / extensions / permissions / settings_override_permission_unittest.cc
blob606175963c7a9e022f71b249f3220bff5d133d3b
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 "chrome/common/extensions/permissions/settings_override_permission.h"
12 #include "extensions/common/manifest_constants.h"
13 #include "extensions/common/permissions/permission_set.h"
14 #include "extensions/common/permissions/permissions_data.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace extensions {
19 namespace {
21 class SettingsOverridePermissionTest : public ExtensionManifestTest {
22 protected:
23 enum Flags {
24 kHomepage = 1,
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());
56 ext_manifest.Set(
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());
69 EXPECT_TRUE(permission_set->HasAPIPermission(APIPermission::kHomepage));
70 std::vector<base::string16> warnings =
71 PermissionsData::GetPermissionMessageStrings(extension.get());
72 ASSERT_EQ(1u, warnings.size());
73 EXPECT_EQ("Change your home page to: google.com/",
74 base::UTF16ToUTF8(warnings[0]));
76 EXPECT_FALSE(permission_set->HasAPIPermission(APIPermission::kStartupPages));
77 EXPECT_FALSE(permission_set->HasAPIPermission(
78 APIPermission::kSearchProvider));
81 TEST_F(SettingsOverridePermissionTest, SartupPages) {
82 scoped_refptr<Extension> extension(GetPermissionSet(kStartupPages));
83 scoped_refptr<const PermissionSet> permission_set(
84 extension->GetActivePermissions());
86 EXPECT_TRUE(permission_set->HasAPIPermission(APIPermission::kStartupPages));
87 std::vector<base::string16> warnings =
88 PermissionsData::GetPermissionMessageStrings(extension.get());
89 ASSERT_EQ(1u, warnings.size());
90 EXPECT_EQ("Change your start page to: startup.com/startup.html",
91 base::UTF16ToUTF8(warnings[0]));
93 EXPECT_FALSE(permission_set->HasAPIPermission(APIPermission::kHomepage));
94 EXPECT_FALSE(permission_set->HasAPIPermission(
95 APIPermission::kSearchProvider));
98 TEST_F(SettingsOverridePermissionTest, SearchSettings) {
99 scoped_refptr<Extension> extension(GetPermissionSet(kSearchProvider));
100 scoped_refptr<const PermissionSet> permission_set(
101 extension->GetActivePermissions());
103 EXPECT_TRUE(permission_set->HasAPIPermission(APIPermission::kSearchProvider));
104 std::vector<base::string16> warnings =
105 PermissionsData::GetPermissionMessageStrings(extension.get());
106 ASSERT_EQ(1u, warnings.size());
107 EXPECT_EQ("Change your search settings to: google.com",
108 base::UTF16ToUTF8(warnings[0]));
110 EXPECT_FALSE(permission_set->HasAPIPermission(APIPermission::kHomepage));
111 EXPECT_FALSE(permission_set->HasAPIPermission(APIPermission::kStartupPages));
114 TEST_F(SettingsOverridePermissionTest, All) {
115 scoped_refptr<Extension> extension(GetPermissionSet(
116 kSearchProvider | kStartupPages | kHomepage));
117 scoped_refptr<const PermissionSet> permission_set(
118 extension->GetActivePermissions());
120 EXPECT_TRUE(permission_set->HasAPIPermission(APIPermission::kSearchProvider));
121 EXPECT_TRUE(permission_set->HasAPIPermission(APIPermission::kHomepage));
122 EXPECT_TRUE(permission_set->HasAPIPermission(APIPermission::kStartupPages));
125 TEST_F(SettingsOverridePermissionTest, Some) {
126 scoped_refptr<Extension> extension(GetPermissionSet(
127 kSearchProvider | kHomepage));
128 scoped_refptr<const PermissionSet> permission_set(
129 extension->GetActivePermissions());
132 EXPECT_TRUE(permission_set->HasAPIPermission(APIPermission::kSearchProvider));
133 EXPECT_TRUE(permission_set->HasAPIPermission(APIPermission::kHomepage));
134 EXPECT_FALSE(permission_set->HasAPIPermission(APIPermission::kStartupPages));
137 } // namespace
139 } // namespace extensions