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 "chrome/common/extensions/features/feature_channel.h"
6 #include "chrome/common/extensions/manifest_handlers/automation.h"
7 #include "chrome/common/extensions/manifest_tests/chrome_manifest_test.h"
8 #include "chrome/grit/generated_resources.h"
9 #include "components/version_info/version_info.h"
10 #include "extensions/common/error_utils.h"
11 #include "extensions/common/manifest_constants.h"
12 #include "extensions/common/permissions/permission_message_test_util.h"
13 #include "extensions/common/permissions/permissions_data.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "ui/base/l10n/l10n_util.h"
17 namespace extensions
{
19 class AutomationManifestTest
: public ChromeManifestTest
{
21 AutomationManifestTest() : channel_(version_info::Channel::UNKNOWN
) {}
24 AutomationInfo
* GetAutomationInfo(scoped_refptr
<Extension
> extension
) {
25 return static_cast<AutomationInfo
*>(
26 extension
->GetManifestData(manifest_keys::kAutomation
));
30 ScopedCurrentChannel channel_
;
33 TEST_F(AutomationManifestTest
, AsBooleanFalse
) {
34 scoped_refptr
<Extension
> extension
=
35 LoadAndExpectSuccess("automation_boolean_false.json");
36 ASSERT_TRUE(extension
.get());
38 EXPECT_TRUE(VerifyNoPermissionMessages(extension
->permissions_data()));
40 const AutomationInfo
* info
= AutomationInfo::Get(extension
.get());
44 TEST_F(AutomationManifestTest
, AsBooleanTrue
) {
45 scoped_refptr
<Extension
> extension
=
46 LoadAndExpectSuccess("automation_boolean_true.json");
47 ASSERT_TRUE(extension
.get());
49 EXPECT_TRUE(VerifyOnePermissionMessage(
50 extension
->permissions_data(),
51 "Read and change your data on www.google.com"));
53 const AutomationInfo
* info
= AutomationInfo::Get(extension
.get());
56 EXPECT_FALSE(info
->desktop
);
57 EXPECT_FALSE(info
->interact
);
58 EXPECT_TRUE(info
->matches
.is_empty());
61 TEST_F(AutomationManifestTest
, InteractTrue
) {
62 scoped_refptr
<Extension
> extension
=
63 LoadAndExpectSuccess("automation_interact_true.json");
64 ASSERT_TRUE(extension
.get());
66 EXPECT_TRUE(VerifyOnePermissionMessage(
67 extension
->permissions_data(),
68 "Read and change your data on www.google.com"));
70 const AutomationInfo
* info
= AutomationInfo::Get(extension
.get());
73 EXPECT_FALSE(info
->desktop
);
74 EXPECT_TRUE(info
->interact
);
75 EXPECT_TRUE(info
->matches
.is_empty());
78 TEST_F(AutomationManifestTest
, Matches
) {
79 scoped_refptr
<Extension
> extension
= LoadAndExpectWarning(
80 "automation_matches.json",
81 ErrorUtils::FormatErrorMessage(
82 automation_errors::kErrorInvalidMatch
,
84 URLPattern::GetParseResultString(
85 URLPattern::PARSE_ERROR_MISSING_SCHEME_SEPARATOR
)));
86 ASSERT_TRUE(extension
.get());
88 EXPECT_TRUE(VerifyOnePermissionMessage(
89 extension
->permissions_data(),
90 "Read your data on www.google.com and www.twitter.com"));
92 const AutomationInfo
* info
= AutomationInfo::Get(extension
.get());
95 EXPECT_FALSE(info
->desktop
);
96 EXPECT_FALSE(info
->interact
);
97 EXPECT_FALSE(info
->matches
.is_empty());
99 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://www.google.com/")));
100 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://www.google.com")));
101 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://www.twitter.com/")));
102 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://www.twitter.com")));
104 EXPECT_FALSE(info
->matches
.MatchesURL(GURL("http://www.bing.com/")));
105 EXPECT_FALSE(info
->matches
.MatchesURL(GURL("http://www.bing.com")));
108 TEST_F(AutomationManifestTest
, MatchesAndPermissions
) {
109 scoped_refptr
<Extension
> extension
=
110 LoadAndExpectSuccess("automation_matches_and_permissions.json");
111 ASSERT_TRUE(extension
.get());
114 VerifyTwoPermissionMessages(extension
->permissions_data(),
115 "Read and change your data on www.google.com",
116 "Read your data on www.twitter.com", false));
118 const AutomationInfo
* info
= AutomationInfo::Get(extension
.get());
121 EXPECT_FALSE(info
->desktop
);
122 EXPECT_FALSE(info
->interact
);
123 EXPECT_FALSE(info
->matches
.is_empty());
125 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://www.twitter.com/")));
126 EXPECT_TRUE(info
->matches
.MatchesURL(GURL("http://www.twitter.com")));
129 TEST_F(AutomationManifestTest
, EmptyMatches
) {
130 scoped_refptr
<Extension
> extension
=
131 LoadAndExpectWarning("automation_empty_matches.json",
132 automation_errors::kErrorNoMatchesProvided
);
133 ASSERT_TRUE(extension
.get());
135 EXPECT_TRUE(VerifyNoPermissionMessages(extension
->permissions_data()));
137 const AutomationInfo
* info
= AutomationInfo::Get(extension
.get());
140 EXPECT_FALSE(info
->desktop
);
141 EXPECT_FALSE(info
->interact
);
142 EXPECT_TRUE(info
->matches
.is_empty());
145 TEST_F(AutomationManifestTest
, NoValidMatches
) {
147 scoped_refptr
<Extension
> extension
=
148 LoadExtension(ManifestData("automation_no_valid_matches.json"), &error
);
149 ASSERT_TRUE(extension
.get());
150 EXPECT_EQ("", error
);
151 EXPECT_EQ(2u, extension
->install_warnings().size());
152 EXPECT_EQ(ErrorUtils::FormatErrorMessage(
153 automation_errors::kErrorInvalidMatch
,
154 "www.badpattern.com",
155 URLPattern::GetParseResultString(
156 URLPattern::PARSE_ERROR_MISSING_SCHEME_SEPARATOR
)),
157 extension
->install_warnings()[0].message
);
158 EXPECT_EQ(automation_errors::kErrorNoMatchesProvided
,
159 extension
->install_warnings()[1].message
);
161 EXPECT_TRUE(VerifyNoPermissionMessages(extension
->permissions_data()));
163 const AutomationInfo
* info
= AutomationInfo::Get(extension
.get());
166 EXPECT_FALSE(info
->desktop
);
167 EXPECT_FALSE(info
->interact
);
168 EXPECT_TRUE(info
->matches
.is_empty());
171 TEST_F(AutomationManifestTest
, DesktopFalse
) {
172 scoped_refptr
<Extension
> extension
=
173 LoadAndExpectSuccess("automation_desktop_false.json");
174 ASSERT_TRUE(extension
.get());
176 EXPECT_TRUE(VerifyOnePermissionMessage(
177 extension
->permissions_data(),
178 "Read and change your data on www.google.com"));
180 const AutomationInfo
* info
= AutomationInfo::Get(extension
.get());
183 EXPECT_FALSE(info
->desktop
);
184 EXPECT_FALSE(info
->interact
);
185 EXPECT_TRUE(info
->matches
.is_empty());
188 TEST_F(AutomationManifestTest
, DesktopTrue
) {
189 scoped_refptr
<Extension
> extension
=
190 LoadAndExpectSuccess("automation_desktop_true.json");
191 ASSERT_TRUE(extension
.get());
193 EXPECT_TRUE(VerifyOnePermissionMessage(
194 extension
->permissions_data(),
195 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_FULL_ACCESS
)));
197 const AutomationInfo
* info
= AutomationInfo::Get(extension
.get());
200 EXPECT_TRUE(info
->desktop
);
201 EXPECT_TRUE(info
->interact
);
202 EXPECT_TRUE(info
->matches
.is_empty());
205 TEST_F(AutomationManifestTest
, Desktop_InteractTrue
) {
206 scoped_refptr
<Extension
> extension
=
207 LoadAndExpectSuccess("automation_desktop_interact_true.json");
208 ASSERT_TRUE(extension
.get());
210 EXPECT_TRUE(VerifyOnePermissionMessage(
211 extension
->permissions_data(),
212 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_FULL_ACCESS
)));
214 const AutomationInfo
* info
= AutomationInfo::Get(extension
.get());
217 EXPECT_TRUE(info
->desktop
);
218 EXPECT_TRUE(info
->interact
);
219 EXPECT_TRUE(info
->matches
.is_empty());
222 TEST_F(AutomationManifestTest
, Desktop_InteractFalse
) {
223 scoped_refptr
<Extension
> extension
=
224 LoadAndExpectWarning("automation_desktop_interact_false.json",
225 automation_errors::kErrorDesktopTrueInteractFalse
);
226 ASSERT_TRUE(extension
.get());
228 EXPECT_TRUE(VerifyOnePermissionMessage(
229 extension
->permissions_data(),
230 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_FULL_ACCESS
)));
232 const AutomationInfo
* info
= AutomationInfo::Get(extension
.get());
235 EXPECT_TRUE(info
->desktop
);
236 EXPECT_TRUE(info
->interact
);
237 EXPECT_TRUE(info
->matches
.is_empty());
240 TEST_F(AutomationManifestTest
, Desktop_MatchesSpecified
) {
241 scoped_refptr
<Extension
> extension
= LoadAndExpectWarning(
242 "automation_desktop_matches_specified.json",
243 automation_errors::kErrorDesktopTrueMatchesSpecified
);
244 ASSERT_TRUE(extension
.get());
246 EXPECT_TRUE(VerifyOnePermissionMessage(
247 extension
->permissions_data(),
248 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_FULL_ACCESS
)));
250 const AutomationInfo
* info
= AutomationInfo::Get(extension
.get());
253 EXPECT_TRUE(info
->desktop
);
254 EXPECT_TRUE(info
->interact
);
255 EXPECT_TRUE(info
->matches
.is_empty());
258 TEST_F(AutomationManifestTest
, AllHostsInteractFalse
) {
259 scoped_refptr
<Extension
> extension
=
260 LoadAndExpectSuccess("automation_all_hosts_interact_false.json");
261 ASSERT_TRUE(extension
.get());
263 EXPECT_TRUE(VerifyOnePermissionMessage(
264 extension
->permissions_data(),
265 l10n_util::GetStringUTF16(
266 IDS_EXTENSION_PROMPT_WARNING_ALL_HOSTS_READ_ONLY
)));
268 const AutomationInfo
* info
= AutomationInfo::Get(extension
.get());
271 EXPECT_FALSE(info
->desktop
);
272 EXPECT_FALSE(info
->interact
);
273 EXPECT_FALSE(info
->matches
.is_empty());
274 EXPECT_TRUE(info
->matches
.MatchesAllURLs());
277 TEST_F(AutomationManifestTest
, AllHostsInteractTrue
) {
278 scoped_refptr
<Extension
> extension
=
279 LoadAndExpectSuccess("automation_all_hosts_interact_true.json");
280 ASSERT_TRUE(extension
.get());
282 EXPECT_TRUE(VerifyOnePermissionMessage(
283 extension
->permissions_data(),
284 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_ALL_HOSTS
)));
286 const AutomationInfo
* info
= AutomationInfo::Get(extension
.get());
289 EXPECT_FALSE(info
->desktop
);
290 EXPECT_TRUE(info
->interact
);
291 EXPECT_FALSE(info
->matches
.is_empty());
292 EXPECT_TRUE(info
->matches
.MatchesAllURLs());
294 } // namespace extensions