Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(449)

Side by Side Diff: chrome/browser/extensions/api/declarative/declarative_rule_unittest.cc

Issue 11547033: Implement declarativeContent API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 "chrome/browser/extensions/api/declarative/declarative_rule.h"
6
7 #include "base/message_loop.h"
8 #include "base/test/values_test_util.h"
9 #include "base/values.h"
10 #include "chrome/common/extensions/matcher/url_matcher_constants.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace extensions {
15
16 using json_schema_compiler::any::Any;
17 using base::ParseJson;
18
19 struct RecordingCondition {
20 typedef int MatchData;
21
22 URLMatcherConditionFactory* factory;
23 scoped_ptr<base::Value> value;
24
25 URLMatcherConditionSet::ID url_matcher_condition_set_id() const {
26 return 1;
27 }
28
29 static scoped_ptr<RecordingCondition> Create(
30 URLMatcherConditionFactory* url_matcher_condition_factory,
31 const base::Value& condition,
32 std::string* error) {
33 const base::DictionaryValue* dict;
34 if (error_on_key &&
35 condition.GetAsDictionary(&dict) &&
36 dict->HasKey(error_on_key)) {
37 *error = "Found error key";
38 return scoped_ptr<RecordingCondition>();
39 }
40
41 scoped_ptr<RecordingCondition> result(new RecordingCondition());
42 result->factory = url_matcher_condition_factory;
43 result->value.reset(condition.DeepCopy());
44 return result.Pass();
45 }
46
47 static const char* error_on_key;
48 };
49 const char* RecordingCondition::error_on_key = NULL;
50 typedef DeclarativeConditionSet<RecordingCondition> RecordingConditionSet;
51
52 TEST(DeclarativeConditionTest, ErrorConditionSet) {
53 URLMatcher matcher;
54 RecordingConditionSet::AnyVector conditions;
55 conditions.push_back(make_linked_ptr(new Any(ParseJson("{\"key\": 1}"))));
56 conditions.push_back(make_linked_ptr(new Any(ParseJson("{\"bad_key\": 2}"))));
57 RecordingCondition::error_on_key = "bad_key";
58
59 std::string error;
60 scoped_ptr<RecordingConditionSet> result =
61 RecordingConditionSet::Create(matcher.condition_factory(),
62 conditions, &error);
63 EXPECT_EQ("Found error key", error);
64 ASSERT_FALSE(result);
65 }
66
67 TEST(DeclarativeConditionTest, CreateConditionSet) {
68 URLMatcher matcher;
69 RecordingConditionSet::AnyVector conditions;
70 conditions.push_back(make_linked_ptr(new Any(ParseJson("{\"key\": 1}"))));
71 conditions.push_back(make_linked_ptr(new Any(ParseJson("[\"val1\", 2]"))));
72 RecordingCondition::error_on_key = NULL;
73
74 // Test insertion
75 std::string error;
76 scoped_ptr<RecordingConditionSet> result =
77 RecordingConditionSet::Create(matcher.condition_factory(),
78 conditions, &error);
79 EXPECT_EQ("", error);
80 ASSERT_TRUE(result);
81 EXPECT_EQ(2u, result->conditions().size());
82
83 EXPECT_EQ(matcher.condition_factory(), result->conditions()[0]->factory);
84 EXPECT_TRUE(ParseJson("{\"key\": 1}")->Equals(
85 result->conditions()[0]->value.get()));
86 }
87
88 struct FulfillableCondition {
89 typedef int MatchData;
90
91 int condition_set_id;
92 int max_value;
93
94 URLMatcherConditionSet::ID url_matcher_condition_set_id() const {
95 return condition_set_id;
96 }
97
98 scoped_refptr<URLMatcherConditionSet> url_matcher_condition_set() const {
99 return new URLMatcherConditionSet(condition_set_id,
100 URLMatcherConditionSet::Conditions());
101 }
102
103 bool IsFulfilled(int match_data) {
104 return match_data <= max_value;
105 }
106
107 static scoped_ptr<FulfillableCondition> Create(
108 URLMatcherConditionFactory* url_matcher_condition_factory,
109 const base::Value& condition,
110 std::string* error) {
111 scoped_ptr<FulfillableCondition> result(new FulfillableCondition());
112 const base::ListValue* list;
113 if (!condition.GetAsList(&list)) {
114 *error = "Expected list";
115 return result.Pass();
116 }
117 if (!list->GetInteger(0, &result->condition_set_id))
118 *error = "Expected integer at [0]";
119 if (!list->GetInteger(1, &result->max_value))
120 *error = "Expected integer at [1]";
121 return result.Pass();
122 }
123 };
124
125 TEST(DeclarativeConditionTest, FulfilConditionSet) {
126 typedef DeclarativeConditionSet<FulfillableCondition> FulfillableConditionSet;
127 FulfillableConditionSet::AnyVector conditions;
128 conditions.push_back(make_linked_ptr(new Any(ParseJson("[1, 3]"))));
129 conditions.push_back(make_linked_ptr(new Any(ParseJson("[2, 5]"))));
130 conditions.push_back(make_linked_ptr(new Any(ParseJson("[3, 1]"))));
131
132 // Test insertion
133 std::string error;
134 scoped_ptr<FulfillableConditionSet> result =
135 FulfillableConditionSet::Create(NULL, conditions, &error);
136 ASSERT_EQ("", error);
137 ASSERT_TRUE(result);
138 EXPECT_EQ(3u, result->conditions().size());
139
140 EXPECT_TRUE(result->IsFulfilled(1, 3));
141 EXPECT_FALSE(result->IsFulfilled(1, 4));
142 EXPECT_TRUE(result->IsFulfilled(2, 4));
143 EXPECT_FALSE(result->IsFulfilled(3, 2));
144
145 // Check the condition sets:
146 URLMatcherConditionSet::Vector condition_sets;
147 result->GetURLMatcherConditionSets(&condition_sets);
148 ASSERT_EQ(3U, condition_sets.size());
149 EXPECT_EQ(1, condition_sets[0]->id());
150 EXPECT_EQ(2, condition_sets[1]->id());
151 EXPECT_EQ(3, condition_sets[2]->id());
152 }
153
154 // DeclarativeAction
155
156 struct SummingAction {
157 typedef int* ApplyInfo;
158
159 int increment;
160 int min_priority;
161
162 static scoped_ptr<SummingAction> Create(const base::Value& action,
163 std::string* error,
164 bool* bad_message) {
165 scoped_ptr<SummingAction> result(new SummingAction());
166 const base::DictionaryValue* dict = NULL;
167 EXPECT_TRUE(action.GetAsDictionary(&dict));
168 if (dict->HasKey("error")) {
169 EXPECT_TRUE(dict->GetString("error", error));
170 return result.Pass();
171 }
172 if (dict->HasKey("bad")) {
173 *bad_message = true;
174 return result.Pass();
175 }
176
177 EXPECT_TRUE(dict->GetInteger("value", &result->increment));
178 dict->GetInteger("priority", &result->min_priority);
179 return result.Pass();
180 }
181
182 void Apply(const std::string& extension_id, const base::Time& install_time,
183 int* sum) {
184 *sum += increment;
185 }
186
187 int GetMinimumPriority() const {
188 return min_priority;
189 }
190 };
191 typedef DeclarativeActionSet<SummingAction> SummingActionSet;
192
193 TEST(DeclarativeActionTest, ErrorActionSet) {
194 SummingActionSet::AnyVector actions;
195 actions.push_back(make_linked_ptr(new Any(ParseJson("{\"value\": 1}"))));
196 actions.push_back(make_linked_ptr(new Any(ParseJson(
197 "{\"error\": \"the error\"}"))));
198
199 std::string error;
200 bool bad = false;
201 scoped_ptr<SummingActionSet> result =
202 SummingActionSet::Create(actions, &error, &bad);
203 EXPECT_EQ("the error", error);
204 EXPECT_FALSE(bad);
205 EXPECT_FALSE(result);
206
207 actions.clear();
208 actions.push_back(make_linked_ptr(new Any(ParseJson("{\"value\": 1}"))));
209 actions.push_back(make_linked_ptr(new Any(ParseJson("{\"bad\": 3}"))));
210 result = SummingActionSet::Create(actions, &error, &bad);
211 EXPECT_EQ("", error);
212 EXPECT_TRUE(bad);
213 EXPECT_FALSE(result);
214 }
215
216 TEST(DeclarativeActionTest, ApplyActionSet) {
217 SummingActionSet::AnyVector actions;
218 actions.push_back(make_linked_ptr(new Any(ParseJson(
219 "{\"value\": 1,"
220 " \"priority\": 5}"))));
221 actions.push_back(make_linked_ptr(new Any(ParseJson("{\"value\": 2}"))));
222
223 // Test insertion
224 std::string error;
225 bool bad = false;
226 scoped_ptr<SummingActionSet> result =
227 SummingActionSet::Create(actions, &error, &bad);
228 EXPECT_EQ("", error);
229 EXPECT_FALSE(bad);
230 ASSERT_TRUE(result);
231 EXPECT_EQ(2u, result->actions().size());
232
233 int sum = 0;
234 result->Apply("ext_id", base::Time(), &sum);
235 EXPECT_EQ(3, sum);
236 EXPECT_EQ(5, result->GetMinimumPriority());
237 }
238
239 TEST(DeclarativeRuleTest, Create) {
240 typedef DeclarativeRule<FulfillableCondition, SummingAction> Rule;
241 linked_ptr<Rule::JsonRule> json_rule(new Rule::JsonRule);
242 ASSERT_TRUE(Rule::JsonRule::Populate(
243 *ParseJson("{ \n"
244 " \"id\": \"rule1\", \n"
245 " \"conditions\": [ \n"
246 " [1, 3], \n"
247 " [2, 5], \n"
248 " ], \n"
249 " \"actions\": [ \n"
250 " { \n"
251 " \"value\": 2 \n"
252 " } \n"
253 " ], \n"
254 " \"priority\": 200 \n"
255 "}"),
256 json_rule.get()));
257
258 const char kExtensionId[] = "ext1";
259
260 base::Time install_time = base::Time::Now();
261
262 URLMatcher matcher;
263 std::string error;
264 scoped_ptr<Rule> rule(Rule::Create(matcher.condition_factory(), kExtensionId,
265 install_time, json_rule, NULL, &error));
266 ASSERT_TRUE(rule.get());
267 EXPECT_EQ("", error);
268
269 EXPECT_EQ(kExtensionId, rule->id().first);
270 EXPECT_EQ("rule1", rule->id().second);
271
272 EXPECT_EQ(200, rule->priority());
273
274 const Rule::ConditionSet& condition_set = rule->conditions();
275 const Rule::ConditionSet::Conditions conditions =
276 condition_set.conditions();
277 ASSERT_EQ(2u, conditions.size());
278 EXPECT_EQ(3, conditions[0]->max_value);
279 EXPECT_EQ(5, conditions[1]->max_value);
280
281 const Rule::ActionSet& action_set = rule->actions();
282 const Rule::ActionSet::Actions& actions = action_set.actions();
283 ASSERT_EQ(1u, actions.size());
284 EXPECT_EQ(2, actions[0]->increment);
285
286 int sum = 0;
287 rule->Apply(&sum);
288 EXPECT_EQ(2, sum);
289 }
290
291 bool AtLeastOneConditionAndAction(
292 DeclarativeConditionSet<FulfillableCondition>* conditions,
293 DeclarativeActionSet<SummingAction>* actions,
294 std::string* error) {
295 if (conditions->conditions().size() < 1) {
296 *error = "No conditions";
297 return false;
298 }
299 if (actions->actions().size() < 1) {
300 *error = "No actions";
301 return false;
302 }
303 return true;
304 }
305
306 TEST(DeclarativeRuleTest, CheckConsistency) {
307 typedef DeclarativeRule<FulfillableCondition, SummingAction> Rule;
308 URLMatcher matcher;
309 std::string error;
310 linked_ptr<Rule::JsonRule> json_rule(new Rule::JsonRule);
311 const char kExtensionId[] = "ext1";
312
313 ASSERT_TRUE(Rule::JsonRule::Populate(
314 *ParseJson("{ \n"
315 " \"id\": \"rule1\", \n"
316 " \"conditions\": [ \n"
317 " [1, 3], \n"
318 " [2, 5], \n"
319 " ], \n"
320 " \"actions\": [ \n"
321 " { \n"
322 " \"value\": 2 \n"
323 " } \n"
324 " ], \n"
325 " \"priority\": 200 \n"
326 "}"),
327 json_rule.get()));
328 scoped_ptr<Rule> rule(
329 Rule::Create(matcher.condition_factory(), kExtensionId, base::Time(),
330 json_rule, &AtLeastOneConditionAndAction, &error));
331 EXPECT_TRUE(rule);
332 EXPECT_EQ("", error);
333
334 ASSERT_TRUE(Rule::JsonRule::Populate(
335 *ParseJson("{ \n"
336 " \"id\": \"rule1\", \n"
337 " \"conditions\": [ \n"
338 " ], \n"
339 " \"actions\": [ \n"
340 " { \n"
341 " \"value\": 2 \n"
342 " } \n"
343 " ], \n"
344 " \"priority\": 200 \n"
345 "}"),
346 json_rule.get()));
347 rule = Rule::Create(matcher.condition_factory(), kExtensionId, base::Time(),
348 json_rule, &AtLeastOneConditionAndAction, &error);
349 EXPECT_FALSE(rule);
350 EXPECT_EQ("No conditions", error);
351 }
352
353 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698