Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / chrome / common / extensions / manifest_handlers / automation_unittest.cc
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.
4
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"
15
16 namespace extensions {
17
18 class AutomationManifestTest : public ChromeManifestTest {
19  public:
20   AutomationManifestTest() : channel_(chrome::VersionInfo::CHANNEL_UNKNOWN) {}
21
22  protected:
23   AutomationInfo* GetAutomationInfo(scoped_refptr<Extension> extension) {
24     return static_cast<AutomationInfo*>(
25         extension->GetManifestData(manifest_keys::kAutomation));
26   }
27
28  private:
29   ScopedCurrentChannel channel_;
30 };
31
32 TEST_F(AutomationManifestTest, AsBooleanFalse) {
33   scoped_refptr<Extension> extension =
34       LoadAndExpectSuccess("automation_boolean_false.json");
35   ASSERT_TRUE(extension.get());
36
37   std::vector<base::string16> warnings =
38       extension->permissions_data()->GetPermissionMessageStrings();
39   EXPECT_EQ(0u, warnings.size());
40
41   const AutomationInfo* info = AutomationInfo::Get(extension.get());
42   ASSERT_FALSE(info);
43 }
44
45 TEST_F(AutomationManifestTest, AsBooleanTrue) {
46   scoped_refptr<Extension> extension =
47       LoadAndExpectSuccess("automation_boolean_true.json");
48   ASSERT_TRUE(extension.get());
49
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]));
55
56   const AutomationInfo* info = AutomationInfo::Get(extension.get());
57   ASSERT_TRUE(info);
58
59   EXPECT_FALSE(info->desktop);
60   EXPECT_FALSE(info->interact);
61   EXPECT_TRUE(info->matches.is_empty());
62 }
63
64 TEST_F(AutomationManifestTest, InteractTrue) {
65   scoped_refptr<Extension> extension =
66       LoadAndExpectSuccess("automation_interact_true.json");
67   ASSERT_TRUE(extension.get());
68
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]));
74
75   const AutomationInfo* info = AutomationInfo::Get(extension.get());
76   ASSERT_TRUE(info);
77
78   EXPECT_FALSE(info->desktop);
79   EXPECT_TRUE(info->interact);
80   EXPECT_TRUE(info->matches.is_empty());
81 }
82
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());
92
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]));
98
99   const AutomationInfo* info = AutomationInfo::Get(extension.get());
100   ASSERT_TRUE(info);
101
102   EXPECT_FALSE(info->desktop);
103   EXPECT_FALSE(info->interact);
104   EXPECT_FALSE(info->matches.is_empty());
105
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")));
110
111   EXPECT_FALSE(info->matches.MatchesURL(GURL("http://www.bing.com/")));
112   EXPECT_FALSE(info->matches.MatchesURL(GURL("http://www.bing.com")));
113 }
114
115 TEST_F(AutomationManifestTest, MatchesAndPermissions) {
116   scoped_refptr<Extension> extension =
117       LoadAndExpectSuccess("automation_matches_and_permissions.json");
118   ASSERT_TRUE(extension.get());
119
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]));
127
128   const AutomationInfo* info = AutomationInfo::Get(extension.get());
129   ASSERT_TRUE(info);
130
131   EXPECT_FALSE(info->desktop);
132   EXPECT_FALSE(info->interact);
133   EXPECT_FALSE(info->matches.is_empty());
134
135   EXPECT_TRUE(info->matches.MatchesURL(GURL("http://www.twitter.com/")));
136   EXPECT_TRUE(info->matches.MatchesURL(GURL("http://www.twitter.com")));
137 }
138
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());
144
145   std::vector<base::string16> warnings =
146       extension->permissions_data()->GetPermissionMessageStrings();
147   EXPECT_EQ(0u, warnings.size());
148
149   const AutomationInfo* info = AutomationInfo::Get(extension.get());
150   ASSERT_TRUE(info);
151
152   EXPECT_FALSE(info->desktop);
153   EXPECT_FALSE(info->interact);
154   EXPECT_TRUE(info->matches.is_empty());
155 }
156
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);
172
173   std::vector<base::string16> warnings =
174       extension->permissions_data()->GetPermissionMessageStrings();
175   ASSERT_EQ(0u, warnings.size());
176
177   const AutomationInfo* info = AutomationInfo::Get(extension.get());
178   ASSERT_TRUE(info);
179
180   EXPECT_FALSE(info->desktop);
181   EXPECT_FALSE(info->interact);
182   EXPECT_TRUE(info->matches.is_empty());
183 }
184
185 TEST_F(AutomationManifestTest, DesktopFalse) {
186   scoped_refptr<Extension> extension =
187       LoadAndExpectSuccess("automation_desktop_false.json");
188   ASSERT_TRUE(extension.get());
189
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]));
195
196   const AutomationInfo* info = AutomationInfo::Get(extension.get());
197   ASSERT_TRUE(info);
198
199   EXPECT_FALSE(info->desktop);
200   EXPECT_FALSE(info->interact);
201   EXPECT_TRUE(info->matches.is_empty());
202 }
203
204 TEST_F(AutomationManifestTest, DesktopTrue) {
205   scoped_refptr<Extension> extension =
206       LoadAndExpectSuccess("automation_desktop_true.json");
207   ASSERT_TRUE(extension.get());
208
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]);
214
215   const AutomationInfo* info = AutomationInfo::Get(extension.get());
216   ASSERT_TRUE(info);
217
218   EXPECT_TRUE(info->desktop);
219   EXPECT_TRUE(info->interact);
220   EXPECT_TRUE(info->matches.is_empty());
221 }
222
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]);
232
233   const AutomationInfo* info = AutomationInfo::Get(extension.get());
234   ASSERT_TRUE(info);
235
236   EXPECT_TRUE(info->desktop);
237   EXPECT_TRUE(info->interact);
238   EXPECT_TRUE(info->matches.is_empty());
239 }
240
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());
246
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]);
252
253   const AutomationInfo* info = AutomationInfo::Get(extension.get());
254   ASSERT_TRUE(info);
255
256   EXPECT_TRUE(info->desktop);
257   EXPECT_TRUE(info->interact);
258   EXPECT_TRUE(info->matches.is_empty());
259 }
260
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());
266
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]);
272
273   const AutomationInfo* info = AutomationInfo::Get(extension.get());
274   ASSERT_TRUE(info);
275
276   EXPECT_TRUE(info->desktop);
277   EXPECT_TRUE(info->interact);
278   EXPECT_TRUE(info->matches.is_empty());
279 }
280
281 TEST_F(AutomationManifestTest, AllHostsInteractFalse) {
282   scoped_refptr<Extension> extension =
283       LoadAndExpectSuccess("automation_all_hosts_interact_false.json");
284   ASSERT_TRUE(extension.get());
285
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]);
292
293   const AutomationInfo* info = AutomationInfo::Get(extension.get());
294   ASSERT_TRUE(info);
295
296   EXPECT_FALSE(info->desktop);
297   EXPECT_FALSE(info->interact);
298   EXPECT_FALSE(info->matches.is_empty());
299   EXPECT_TRUE(info->matches.MatchesAllURLs());
300 }
301
302 TEST_F(AutomationManifestTest, AllHostsInteractTrue) {
303   scoped_refptr<Extension> extension =
304       LoadAndExpectSuccess("automation_all_hosts_interact_true.json");
305   ASSERT_TRUE(extension.get());
306
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]);
312
313   const AutomationInfo* info = AutomationInfo::Get(extension.get());
314   ASSERT_TRUE(info);
315
316   EXPECT_FALSE(info->desktop);
317   EXPECT_TRUE(info->interact);
318   EXPECT_FALSE(info->matches.is_empty());
319   EXPECT_TRUE(info->matches.MatchesAllURLs());
320 }
321 }  // namespace extensions