Delete chrome.mediaGalleriesPrivate because the functionality unique to it has since...
[chromium-blink-merge.git] / chrome / common / extensions / manifest_handlers / automation_unittest.cc
blob4d8c7ba568e25649a8867e77475a809c18f33e10
1 // Copyright 2014 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/strings/utf_string_conversions.h"
6 #include "chrome/common/extensions/features/feature_channel.h"
7 #include "chrome/common/extensions/manifest_handlers/automation.h"
8 #include "chrome/common/extensions/manifest_tests/chrome_manifest_test.h"
9 #include "chrome/grit/generated_resources.h"
10 #include "extensions/common/error_utils.h"
11 #include "extensions/common/manifest_constants.h"
12 #include "extensions/common/permissions/permissions_data.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "ui/base/l10n/l10n_util.h"
16 namespace extensions {
18 class AutomationManifestTest : public ChromeManifestTest {
19 public:
20 AutomationManifestTest() : channel_(chrome::VersionInfo::CHANNEL_UNKNOWN) {}
22 protected:
23 AutomationInfo* GetAutomationInfo(scoped_refptr<Extension> extension) {
24 return static_cast<AutomationInfo*>(
25 extension->GetManifestData(manifest_keys::kAutomation));
28 private:
29 ScopedCurrentChannel channel_;
32 TEST_F(AutomationManifestTest, AsBooleanFalse) {
33 scoped_refptr<Extension> extension =
34 LoadAndExpectSuccess("automation_boolean_false.json");
35 ASSERT_TRUE(extension.get());
37 std::vector<base::string16> warnings =
38 extension->permissions_data()->GetPermissionMessageStrings();
39 EXPECT_EQ(0u, warnings.size());
41 const AutomationInfo* info = AutomationInfo::Get(extension.get());
42 ASSERT_FALSE(info);
45 TEST_F(AutomationManifestTest, AsBooleanTrue) {
46 scoped_refptr<Extension> extension =
47 LoadAndExpectSuccess("automation_boolean_true.json");
48 ASSERT_TRUE(extension.get());
50 std::vector<base::string16> warnings =
51 extension->permissions_data()->GetPermissionMessageStrings();
52 ASSERT_EQ(1u, warnings.size());
53 EXPECT_EQ("Read and change your data on www.google.com",
54 base::UTF16ToUTF8(warnings[0]));
56 const AutomationInfo* info = AutomationInfo::Get(extension.get());
57 ASSERT_TRUE(info);
59 EXPECT_FALSE(info->desktop);
60 EXPECT_FALSE(info->interact);
61 EXPECT_TRUE(info->matches.is_empty());
64 TEST_F(AutomationManifestTest, InteractTrue) {
65 scoped_refptr<Extension> extension =
66 LoadAndExpectSuccess("automation_interact_true.json");
67 ASSERT_TRUE(extension.get());
69 std::vector<base::string16> warnings =
70 extension->permissions_data()->GetPermissionMessageStrings();
71 ASSERT_EQ(1u, warnings.size());
72 EXPECT_EQ("Read and change your data on www.google.com",
73 base::UTF16ToUTF8(warnings[0]));
75 const AutomationInfo* info = AutomationInfo::Get(extension.get());
76 ASSERT_TRUE(info);
78 EXPECT_FALSE(info->desktop);
79 EXPECT_TRUE(info->interact);
80 EXPECT_TRUE(info->matches.is_empty());
83 TEST_F(AutomationManifestTest, Matches) {
84 scoped_refptr<Extension> extension = LoadAndExpectWarning(
85 "automation_matches.json",
86 ErrorUtils::FormatErrorMessage(
87 automation_errors::kErrorInvalidMatch,
88 "www.badpattern.com",
89 URLPattern::GetParseResultString(
90 URLPattern::PARSE_ERROR_MISSING_SCHEME_SEPARATOR)));
91 ASSERT_TRUE(extension.get());
93 std::vector<base::string16> warnings =
94 extension->permissions_data()->GetPermissionMessageStrings();
95 ASSERT_EQ(1u, warnings.size());
96 EXPECT_EQ("Read your data on www.google.com and www.twitter.com",
97 base::UTF16ToUTF8(warnings[0]));
99 const AutomationInfo* info = AutomationInfo::Get(extension.get());
100 ASSERT_TRUE(info);
102 EXPECT_FALSE(info->desktop);
103 EXPECT_FALSE(info->interact);
104 EXPECT_FALSE(info->matches.is_empty());
106 EXPECT_TRUE(info->matches.MatchesURL(GURL("http://www.google.com/")));
107 EXPECT_TRUE(info->matches.MatchesURL(GURL("http://www.google.com")));
108 EXPECT_TRUE(info->matches.MatchesURL(GURL("http://www.twitter.com/")));
109 EXPECT_TRUE(info->matches.MatchesURL(GURL("http://www.twitter.com")));
111 EXPECT_FALSE(info->matches.MatchesURL(GURL("http://www.bing.com/")));
112 EXPECT_FALSE(info->matches.MatchesURL(GURL("http://www.bing.com")));
115 TEST_F(AutomationManifestTest, MatchesAndPermissions) {
116 scoped_refptr<Extension> extension =
117 LoadAndExpectSuccess("automation_matches_and_permissions.json");
118 ASSERT_TRUE(extension.get());
120 std::vector<base::string16> warnings =
121 extension->permissions_data()->GetPermissionMessageStrings();
122 ASSERT_EQ(2u, warnings.size());
123 EXPECT_EQ("Read and change your data on www.google.com",
124 base::UTF16ToUTF8(warnings[0]));
125 EXPECT_EQ("Read your data on www.twitter.com",
126 base::UTF16ToUTF8(warnings[1]));
128 const AutomationInfo* info = AutomationInfo::Get(extension.get());
129 ASSERT_TRUE(info);
131 EXPECT_FALSE(info->desktop);
132 EXPECT_FALSE(info->interact);
133 EXPECT_FALSE(info->matches.is_empty());
135 EXPECT_TRUE(info->matches.MatchesURL(GURL("http://www.twitter.com/")));
136 EXPECT_TRUE(info->matches.MatchesURL(GURL("http://www.twitter.com")));
139 TEST_F(AutomationManifestTest, EmptyMatches) {
140 scoped_refptr<Extension> extension =
141 LoadAndExpectWarning("automation_empty_matches.json",
142 automation_errors::kErrorNoMatchesProvided);
143 ASSERT_TRUE(extension.get());
145 std::vector<base::string16> warnings =
146 extension->permissions_data()->GetPermissionMessageStrings();
147 EXPECT_EQ(0u, warnings.size());
149 const AutomationInfo* info = AutomationInfo::Get(extension.get());
150 ASSERT_TRUE(info);
152 EXPECT_FALSE(info->desktop);
153 EXPECT_FALSE(info->interact);
154 EXPECT_TRUE(info->matches.is_empty());
157 TEST_F(AutomationManifestTest, NoValidMatches) {
158 std::string error;
159 scoped_refptr<Extension> extension =
160 LoadExtension(ManifestData("automation_no_valid_matches.json"), &error);
161 ASSERT_TRUE(extension.get());
162 EXPECT_EQ("", error);
163 EXPECT_EQ(2u, extension->install_warnings().size());
164 EXPECT_EQ(ErrorUtils::FormatErrorMessage(
165 automation_errors::kErrorInvalidMatch,
166 "www.badpattern.com",
167 URLPattern::GetParseResultString(
168 URLPattern::PARSE_ERROR_MISSING_SCHEME_SEPARATOR)),
169 extension->install_warnings()[0].message);
170 EXPECT_EQ(automation_errors::kErrorNoMatchesProvided,
171 extension->install_warnings()[1].message);
173 std::vector<base::string16> warnings =
174 extension->permissions_data()->GetPermissionMessageStrings();
175 ASSERT_EQ(0u, warnings.size());
177 const AutomationInfo* info = AutomationInfo::Get(extension.get());
178 ASSERT_TRUE(info);
180 EXPECT_FALSE(info->desktop);
181 EXPECT_FALSE(info->interact);
182 EXPECT_TRUE(info->matches.is_empty());
185 TEST_F(AutomationManifestTest, DesktopFalse) {
186 scoped_refptr<Extension> extension =
187 LoadAndExpectSuccess("automation_desktop_false.json");
188 ASSERT_TRUE(extension.get());
190 std::vector<base::string16> warnings =
191 extension->permissions_data()->GetPermissionMessageStrings();
192 ASSERT_EQ(1u, warnings.size());
193 EXPECT_EQ("Read and change your data on www.google.com",
194 base::UTF16ToUTF8(warnings[0]));
196 const AutomationInfo* info = AutomationInfo::Get(extension.get());
197 ASSERT_TRUE(info);
199 EXPECT_FALSE(info->desktop);
200 EXPECT_FALSE(info->interact);
201 EXPECT_TRUE(info->matches.is_empty());
204 TEST_F(AutomationManifestTest, DesktopTrue) {
205 scoped_refptr<Extension> extension =
206 LoadAndExpectSuccess("automation_desktop_true.json");
207 ASSERT_TRUE(extension.get());
209 std::vector<base::string16> warnings =
210 extension->permissions_data()->GetPermissionMessageStrings();
211 ASSERT_EQ(1u, warnings.size());
212 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_FULL_ACCESS),
213 warnings[0]);
215 const AutomationInfo* info = AutomationInfo::Get(extension.get());
216 ASSERT_TRUE(info);
218 EXPECT_TRUE(info->desktop);
219 EXPECT_TRUE(info->interact);
220 EXPECT_TRUE(info->matches.is_empty());
223 TEST_F(AutomationManifestTest, Desktop_InteractTrue) {
224 scoped_refptr<Extension> extension =
225 LoadAndExpectSuccess("automation_desktop_interact_true.json");
226 ASSERT_TRUE(extension.get());
227 std::vector<base::string16> warnings =
228 extension->permissions_data()->GetPermissionMessageStrings();
229 ASSERT_EQ(1u, warnings.size());
230 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_FULL_ACCESS),
231 warnings[0]);
233 const AutomationInfo* info = AutomationInfo::Get(extension.get());
234 ASSERT_TRUE(info);
236 EXPECT_TRUE(info->desktop);
237 EXPECT_TRUE(info->interact);
238 EXPECT_TRUE(info->matches.is_empty());
241 TEST_F(AutomationManifestTest, Desktop_InteractFalse) {
242 scoped_refptr<Extension> extension =
243 LoadAndExpectWarning("automation_desktop_interact_false.json",
244 automation_errors::kErrorDesktopTrueInteractFalse);
245 ASSERT_TRUE(extension.get());
247 std::vector<base::string16> warnings =
248 extension->permissions_data()->GetPermissionMessageStrings();
249 ASSERT_EQ(1u, warnings.size());
250 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_FULL_ACCESS),
251 warnings[0]);
253 const AutomationInfo* info = AutomationInfo::Get(extension.get());
254 ASSERT_TRUE(info);
256 EXPECT_TRUE(info->desktop);
257 EXPECT_TRUE(info->interact);
258 EXPECT_TRUE(info->matches.is_empty());
261 TEST_F(AutomationManifestTest, Desktop_MatchesSpecified) {
262 scoped_refptr<Extension> extension = LoadAndExpectWarning(
263 "automation_desktop_matches_specified.json",
264 automation_errors::kErrorDesktopTrueMatchesSpecified);
265 ASSERT_TRUE(extension.get());
267 std::vector<base::string16> warnings =
268 extension->permissions_data()->GetPermissionMessageStrings();
269 ASSERT_EQ(1u, warnings.size());
270 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_FULL_ACCESS),
271 warnings[0]);
273 const AutomationInfo* info = AutomationInfo::Get(extension.get());
274 ASSERT_TRUE(info);
276 EXPECT_TRUE(info->desktop);
277 EXPECT_TRUE(info->interact);
278 EXPECT_TRUE(info->matches.is_empty());
281 TEST_F(AutomationManifestTest, AllHostsInteractFalse) {
282 scoped_refptr<Extension> extension =
283 LoadAndExpectSuccess("automation_all_hosts_interact_false.json");
284 ASSERT_TRUE(extension.get());
286 std::vector<base::string16> warnings =
287 extension->permissions_data()->GetPermissionMessageStrings();
288 ASSERT_EQ(1u, warnings.size());
289 EXPECT_EQ(l10n_util::GetStringUTF16(
290 IDS_EXTENSION_PROMPT_WARNING_ALL_HOSTS_READ_ONLY),
291 warnings[0]);
293 const AutomationInfo* info = AutomationInfo::Get(extension.get());
294 ASSERT_TRUE(info);
296 EXPECT_FALSE(info->desktop);
297 EXPECT_FALSE(info->interact);
298 EXPECT_FALSE(info->matches.is_empty());
299 EXPECT_TRUE(info->matches.MatchesAllURLs());
302 TEST_F(AutomationManifestTest, AllHostsInteractTrue) {
303 scoped_refptr<Extension> extension =
304 LoadAndExpectSuccess("automation_all_hosts_interact_true.json");
305 ASSERT_TRUE(extension.get());
307 std::vector<base::string16> warnings =
308 extension->permissions_data()->GetPermissionMessageStrings();
309 ASSERT_EQ(1u, warnings.size());
310 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_ALL_HOSTS),
311 warnings[0]);
313 const AutomationInfo* info = AutomationInfo::Get(extension.get());
314 ASSERT_TRUE(info);
316 EXPECT_FALSE(info->desktop);
317 EXPECT_TRUE(info->interact);
318 EXPECT_FALSE(info->matches.is_empty());
319 EXPECT_TRUE(info->matches.MatchesAllURLs());
321 } // namespace extensions