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/manifest_handlers/automation.h"
7 #include "chrome/common/extensions/manifest_tests/extension_manifest_test.h"
8 #include "extensions/common/error_utils.h"
9 #include "extensions/common/manifest_constants.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace extensions {
14 class AutomationManifestTest : public ExtensionManifestTest {
16 AutomationManifestTest() : channel_(chrome::VersionInfo::CHANNEL_UNKNOWN) {}
19 AutomationInfo* GetAutomationInfo(scoped_refptr<Extension> extension) {
20 return static_cast<AutomationInfo*>(
21 extension->GetManifestData(manifest_keys::kAutomation));
25 ScopedCurrentChannel channel_;
28 TEST_F(AutomationManifestTest, AsBooleanFalse) {
29 scoped_refptr<Extension> extension =
30 LoadAndExpectSuccess("automation_boolean_false.json");
31 ASSERT_TRUE(extension.get());
33 const AutomationInfo* info = AutomationInfo::Get(extension.get());
37 TEST_F(AutomationManifestTest, AsBooleanTrue) {
38 scoped_refptr<Extension> extension =
39 LoadAndExpectSuccess("automation_boolean_true.json");
40 ASSERT_TRUE(extension.get());
42 const AutomationInfo* info = AutomationInfo::Get(extension.get());
45 EXPECT_FALSE(info->desktop);
46 EXPECT_FALSE(info->interact);
47 EXPECT_FALSE(info->specified_matches);
48 EXPECT_TRUE(info->matches.is_empty());
51 TEST_F(AutomationManifestTest, InteractTrue) {
52 scoped_refptr<Extension> extension =
53 LoadAndExpectSuccess("automation_interact_true.json");
54 ASSERT_TRUE(extension.get());
56 const AutomationInfo* info = AutomationInfo::Get(extension.get());
59 EXPECT_FALSE(info->desktop);
60 EXPECT_TRUE(info->interact);
61 EXPECT_FALSE(info->specified_matches);
62 EXPECT_TRUE(info->matches.is_empty());
65 TEST_F(AutomationManifestTest, Matches) {
66 scoped_refptr<Extension> extension = LoadAndExpectWarning(
67 "automation_matches.json",
68 ErrorUtils::FormatErrorMessage(
69 automation_errors::kErrorInvalidMatch,
71 URLPattern::GetParseResultString(
72 URLPattern::PARSE_ERROR_MISSING_SCHEME_SEPARATOR)));
73 ASSERT_TRUE(extension.get());
75 const AutomationInfo* info = AutomationInfo::Get(extension.get());
78 EXPECT_FALSE(info->desktop);
79 EXPECT_FALSE(info->interact);
80 EXPECT_TRUE(info->specified_matches);
81 EXPECT_FALSE(info->matches.is_empty());
83 EXPECT_TRUE(info->matches.MatchesURL(GURL("http://www.google.com/")));
84 EXPECT_TRUE(info->matches.MatchesURL(GURL("http://www.google.com")));
85 EXPECT_TRUE(info->matches.MatchesURL(GURL("http://www.twitter.com/")));
86 EXPECT_TRUE(info->matches.MatchesURL(GURL("http://www.twitter.com")));
88 EXPECT_FALSE(info->matches.MatchesURL(GURL("http://www.bing.com/")));
89 EXPECT_FALSE(info->matches.MatchesURL(GURL("http://www.bing.com")));
92 TEST_F(AutomationManifestTest, EmptyMatches) {
93 scoped_refptr<Extension> extension =
94 LoadAndExpectWarning("automation_empty_matches.json",
95 automation_errors::kErrorNoMatchesProvided);
96 ASSERT_TRUE(extension.get());
98 const AutomationInfo* info = AutomationInfo::Get(extension.get());
101 EXPECT_FALSE(info->desktop);
102 EXPECT_FALSE(info->interact);
103 EXPECT_TRUE(info->specified_matches);
104 EXPECT_TRUE(info->matches.is_empty());
107 TEST_F(AutomationManifestTest, NoValidMatches) {
109 scoped_refptr<Extension> extension =
110 LoadExtension(Manifest("automation_no_valid_matches.json"), &error);
111 ASSERT_TRUE(extension.get());
112 EXPECT_EQ("", error);
113 EXPECT_EQ(2u, extension->install_warnings().size());
114 EXPECT_EQ(ErrorUtils::FormatErrorMessage(
115 automation_errors::kErrorInvalidMatch,
116 "www.badpattern.com",
117 URLPattern::GetParseResultString(
118 URLPattern::PARSE_ERROR_MISSING_SCHEME_SEPARATOR)),
119 extension->install_warnings()[0].message);
120 EXPECT_EQ(automation_errors::kErrorNoMatchesProvided,
121 extension->install_warnings()[1].message);
123 const AutomationInfo* info = AutomationInfo::Get(extension.get());
126 EXPECT_FALSE(info->desktop);
127 EXPECT_FALSE(info->interact);
128 EXPECT_TRUE(info->specified_matches);
129 EXPECT_TRUE(info->matches.is_empty());
132 TEST_F(AutomationManifestTest, DesktopFalse) {
133 scoped_refptr<Extension> extension =
134 LoadAndExpectSuccess("automation_desktop_false.json");
135 ASSERT_TRUE(extension.get());
137 const AutomationInfo* info = AutomationInfo::Get(extension.get());
140 EXPECT_FALSE(info->desktop);
141 EXPECT_FALSE(info->interact);
142 EXPECT_FALSE(info->specified_matches);
143 EXPECT_TRUE(info->matches.is_empty());
146 TEST_F(AutomationManifestTest, DesktopTrue) {
147 scoped_refptr<Extension> extension =
148 LoadAndExpectSuccess("automation_desktop_true.json");
149 ASSERT_TRUE(extension.get());
151 const AutomationInfo* info = AutomationInfo::Get(extension.get());
154 EXPECT_TRUE(info->desktop);
155 EXPECT_TRUE(info->interact);
156 EXPECT_FALSE(info->specified_matches);
157 EXPECT_TRUE(info->matches.is_empty());
160 TEST_F(AutomationManifestTest, Desktop_InteractTrue) {
161 scoped_refptr<Extension> extension =
162 LoadAndExpectSuccess("automation_desktop_interact_true.json");
163 ASSERT_TRUE(extension.get());
165 const AutomationInfo* info = AutomationInfo::Get(extension.get());
168 EXPECT_TRUE(info->desktop);
169 EXPECT_TRUE(info->interact);
170 EXPECT_FALSE(info->specified_matches);
171 EXPECT_TRUE(info->matches.is_empty());
174 TEST_F(AutomationManifestTest, Desktop_InteractFalse) {
175 scoped_refptr<Extension> extension =
176 LoadAndExpectWarning("automation_desktop_interact_false.json",
177 automation_errors::kErrorDesktopTrueInteractFalse);
178 ASSERT_TRUE(extension.get());
180 const AutomationInfo* info = AutomationInfo::Get(extension.get());
183 EXPECT_TRUE(info->desktop);
184 EXPECT_TRUE(info->interact);
185 EXPECT_FALSE(info->specified_matches);
186 EXPECT_TRUE(info->matches.is_empty());
189 TEST_F(AutomationManifestTest, Desktop_MatchesSpecified) {
190 scoped_refptr<Extension> extension = LoadAndExpectWarning(
191 "automation_desktop_matches_specified.json",
192 automation_errors::kErrorDesktopTrueMatchesSpecified);
193 ASSERT_TRUE(extension.get());
195 const AutomationInfo* info = AutomationInfo::Get(extension.get());
198 EXPECT_TRUE(info->desktop);
199 EXPECT_TRUE(info->interact);
200 EXPECT_FALSE(info->specified_matches);
201 EXPECT_TRUE(info->matches.is_empty());
204 } // namespace extensions