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.
4
5// These tests make sure SettingsOverridePermission values are set correctly.
6
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/chrome_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"
16
17namespace extensions {
18
19namespace {
20
21class SettingsOverridePermissionTest : public ChromeManifestTest {
22 protected:
23  enum Flags {
24    kHomepage = 1,
25    kStartupPages = 1 << 1,
26    kSearchProvider = 1 << 2,
27  };
28
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);
34
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());
43    }
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());
55    }
56    ext_manifest.Set(
57        manifest_keys::kSettingsOverride, settings_override.release());
58
59    ManifestData manifest(&ext_manifest, "test");
60    return LoadAndExpectSuccess(manifest);
61  }
62};
63
64TEST_F(SettingsOverridePermissionTest, HomePage) {
65  scoped_refptr<Extension> extension(GetPermissionSet(kHomepage));
66  scoped_refptr<const PermissionSet> permission_set(
67      extension->permissions_data()->active_permissions());
68
69#if defined(OS_WIN)
70  EXPECT_TRUE(permission_set->HasAPIPermission(APIPermission::kHomepage));
71  std::vector<base::string16> warnings =
72      extension->permissions_data()->GetPermissionMessageStrings();
73  ASSERT_EQ(1u, warnings.size());
74  EXPECT_EQ("Change your home page to: google.com/",
75            base::UTF16ToUTF8(warnings[0]));
76
77  EXPECT_FALSE(permission_set->HasAPIPermission(APIPermission::kStartupPages));
78  EXPECT_FALSE(permission_set->HasAPIPermission(
79      APIPermission::kSearchProvider));
80#else
81  EXPECT_FALSE(permission_set->HasAPIPermission(APIPermission::kHomepage));
82  EXPECT_FALSE(permission_set->HasAPIPermission(APIPermission::kStartupPages));
83  EXPECT_FALSE(
84      permission_set->HasAPIPermission(APIPermission::kSearchProvider));
85#endif
86}
87
88TEST_F(SettingsOverridePermissionTest, StartupPages) {
89  scoped_refptr<Extension> extension(GetPermissionSet(kStartupPages));
90  scoped_refptr<const PermissionSet> permission_set(
91      extension->permissions_data()->active_permissions());
92
93#if defined(OS_WIN)
94  EXPECT_TRUE(permission_set->HasAPIPermission(APIPermission::kStartupPages));
95  std::vector<base::string16> warnings =
96      extension->permissions_data()->GetPermissionMessageStrings();
97  ASSERT_EQ(1u, warnings.size());
98  EXPECT_EQ("Change your start page to: startup.com/startup.html",
99            base::UTF16ToUTF8(warnings[0]));
100
101  EXPECT_FALSE(permission_set->HasAPIPermission(APIPermission::kHomepage));
102  EXPECT_FALSE(permission_set->HasAPIPermission(
103      APIPermission::kSearchProvider));
104#else
105  EXPECT_FALSE(permission_set->HasAPIPermission(APIPermission::kHomepage));
106  EXPECT_FALSE(permission_set->HasAPIPermission(APIPermission::kStartupPages));
107  EXPECT_FALSE(
108      permission_set->HasAPIPermission(APIPermission::kSearchProvider));
109#endif
110}
111
112TEST_F(SettingsOverridePermissionTest, SearchSettings) {
113  scoped_refptr<Extension> extension(GetPermissionSet(kSearchProvider));
114  scoped_refptr<const PermissionSet> permission_set(
115      extension->permissions_data()->active_permissions());
116
117#if defined(OS_WIN)
118  EXPECT_TRUE(permission_set->HasAPIPermission(APIPermission::kSearchProvider));
119  std::vector<base::string16> warnings =
120      extension->permissions_data()->GetPermissionMessageStrings();
121  ASSERT_EQ(1u, warnings.size());
122  EXPECT_EQ("Change your search settings to: google.com",
123            base::UTF16ToUTF8(warnings[0]));
124
125  EXPECT_FALSE(permission_set->HasAPIPermission(APIPermission::kHomepage));
126  EXPECT_FALSE(permission_set->HasAPIPermission(APIPermission::kStartupPages));
127#else
128  EXPECT_FALSE(permission_set->HasAPIPermission(APIPermission::kHomepage));
129  EXPECT_FALSE(permission_set->HasAPIPermission(APIPermission::kStartupPages));
130  EXPECT_FALSE(
131      permission_set->HasAPIPermission(APIPermission::kSearchProvider));
132#endif
133}
134
135TEST_F(SettingsOverridePermissionTest, All) {
136  scoped_refptr<Extension> extension(GetPermissionSet(
137      kSearchProvider | kStartupPages | kHomepage));
138  scoped_refptr<const PermissionSet> permission_set(
139      extension->permissions_data()->active_permissions());
140
141#if defined(OS_WIN)
142  EXPECT_TRUE(permission_set->HasAPIPermission(APIPermission::kSearchProvider));
143  EXPECT_TRUE(permission_set->HasAPIPermission(APIPermission::kHomepage));
144  EXPECT_TRUE(permission_set->HasAPIPermission(APIPermission::kStartupPages));
145#else
146  EXPECT_FALSE(permission_set->HasAPIPermission(APIPermission::kHomepage));
147  EXPECT_FALSE(permission_set->HasAPIPermission(APIPermission::kStartupPages));
148  EXPECT_FALSE(
149      permission_set->HasAPIPermission(APIPermission::kSearchProvider));
150#endif
151}
152
153TEST_F(SettingsOverridePermissionTest, Some) {
154  scoped_refptr<Extension> extension(GetPermissionSet(
155      kSearchProvider | kHomepage));
156  scoped_refptr<const PermissionSet> permission_set(
157      extension->permissions_data()->active_permissions());
158
159#if defined(OS_WIN)
160  EXPECT_TRUE(permission_set->HasAPIPermission(APIPermission::kSearchProvider));
161  EXPECT_TRUE(permission_set->HasAPIPermission(APIPermission::kHomepage));
162  EXPECT_FALSE(permission_set->HasAPIPermission(APIPermission::kStartupPages));
163#else
164  EXPECT_FALSE(permission_set->HasAPIPermission(APIPermission::kHomepage));
165  EXPECT_FALSE(permission_set->HasAPIPermission(APIPermission::kStartupPages));
166  EXPECT_FALSE(
167      permission_set->HasAPIPermission(APIPermission::kSearchProvider));
168#endif
169}
170
171}  // namespace
172
173}  // namespace extensions
174