OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/browser/api/declarative/declarative_rule.h" | 5 #include "extensions/browser/api/declarative/declarative_rule.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/test/values_test_util.h" | 9 #include "base/test/values_test_util.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 void GetURLMatcherConditionSets( | 46 void GetURLMatcherConditionSets( |
47 URLMatcherConditionSet::Vector* condition_sets) const { | 47 URLMatcherConditionSet::Vector* condition_sets) const { |
48 // No condition sets. | 48 // No condition sets. |
49 } | 49 } |
50 | 50 |
51 static scoped_ptr<RecordingCondition> Create( | 51 static scoped_ptr<RecordingCondition> Create( |
52 const Extension* extension, | 52 const Extension* extension, |
53 URLMatcherConditionFactory* url_matcher_condition_factory, | 53 URLMatcherConditionFactory* url_matcher_condition_factory, |
54 const base::Value& condition, | 54 const base::Value& condition, |
55 std::string* error) { | 55 std::string* error) { |
56 const base::DictionaryValue* dict = NULL; | 56 const base::DictionaryValue* dict = nullptr; |
57 if (condition.GetAsDictionary(&dict) && dict->HasKey("bad_key")) { | 57 if (condition.GetAsDictionary(&dict) && dict->HasKey("bad_key")) { |
58 *error = "Found error key"; | 58 *error = "Found error key"; |
59 return scoped_ptr<RecordingCondition>(); | 59 return scoped_ptr<RecordingCondition>(); |
60 } | 60 } |
61 | 61 |
62 scoped_ptr<RecordingCondition> result(new RecordingCondition()); | 62 scoped_ptr<RecordingCondition> result(new RecordingCondition()); |
63 result->factory = url_matcher_condition_factory; | 63 result->factory = url_matcher_condition_factory; |
64 result->value.reset(condition.DeepCopy()); | 64 result->value.reset(condition.DeepCopy()); |
65 return result.Pass(); | 65 return result.Pass(); |
66 } | 66 } |
67 }; | 67 }; |
68 typedef DeclarativeConditionSet<RecordingCondition> RecordingConditionSet; | 68 typedef DeclarativeConditionSet<RecordingCondition> RecordingConditionSet; |
69 | 69 |
70 TEST(DeclarativeConditionTest, ErrorConditionSet) { | 70 TEST(DeclarativeConditionTest, ErrorConditionSet) { |
71 URLMatcher matcher; | 71 URLMatcher matcher; |
72 RecordingConditionSet::AnyVector conditions; | 72 RecordingConditionSet::AnyVector conditions; |
73 conditions.push_back(ScopedToLinkedPtr(ParseJson("{\"key\": 1}"))); | 73 conditions.push_back(ScopedToLinkedPtr(ParseJson("{\"key\": 1}"))); |
74 conditions.push_back(ScopedToLinkedPtr(ParseJson("{\"bad_key\": 2}"))); | 74 conditions.push_back(ScopedToLinkedPtr(ParseJson("{\"bad_key\": 2}"))); |
75 | 75 |
76 std::string error; | 76 std::string error; |
77 scoped_ptr<RecordingConditionSet> result = RecordingConditionSet::Create( | 77 scoped_ptr<RecordingConditionSet> result = RecordingConditionSet::Create( |
78 NULL, matcher.condition_factory(), conditions, &error); | 78 nullptr, matcher.condition_factory(), conditions, &error); |
79 EXPECT_EQ("Found error key", error); | 79 EXPECT_EQ("Found error key", error); |
80 ASSERT_FALSE(result); | 80 ASSERT_FALSE(result); |
81 } | 81 } |
82 | 82 |
83 TEST(DeclarativeConditionTest, CreateConditionSet) { | 83 TEST(DeclarativeConditionTest, CreateConditionSet) { |
84 URLMatcher matcher; | 84 URLMatcher matcher; |
85 RecordingConditionSet::AnyVector conditions; | 85 RecordingConditionSet::AnyVector conditions; |
86 conditions.push_back(ScopedToLinkedPtr(ParseJson("{\"key\": 1}"))); | 86 conditions.push_back(ScopedToLinkedPtr(ParseJson("{\"key\": 1}"))); |
87 conditions.push_back(ScopedToLinkedPtr(ParseJson("[\"val1\", 2]"))); | 87 conditions.push_back(ScopedToLinkedPtr(ParseJson("[\"val1\", 2]"))); |
88 | 88 |
89 // Test insertion | 89 // Test insertion |
90 std::string error; | 90 std::string error; |
91 scoped_ptr<RecordingConditionSet> result = RecordingConditionSet::Create( | 91 scoped_ptr<RecordingConditionSet> result = RecordingConditionSet::Create( |
92 NULL, matcher.condition_factory(), conditions, &error); | 92 nullptr, matcher.condition_factory(), conditions, &error); |
93 EXPECT_EQ("", error); | 93 EXPECT_EQ("", error); |
94 ASSERT_TRUE(result); | 94 ASSERT_TRUE(result); |
95 EXPECT_EQ(2u, result->conditions().size()); | 95 EXPECT_EQ(2u, result->conditions().size()); |
96 | 96 |
97 EXPECT_EQ(matcher.condition_factory(), result->conditions()[0]->factory); | 97 EXPECT_EQ(matcher.condition_factory(), result->conditions()[0]->factory); |
98 EXPECT_TRUE(ParseJson("{\"key\": 1}")->Equals( | 98 EXPECT_TRUE(ParseJson("{\"key\": 1}")->Equals( |
99 result->conditions()[0]->value.get())); | 99 result->conditions()[0]->value.get())); |
100 } | 100 } |
101 | 101 |
102 struct FulfillableCondition { | 102 struct FulfillableCondition { |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 conditions.push_back(ScopedToLinkedPtr(ParseJson( | 162 conditions.push_back(ScopedToLinkedPtr(ParseJson( |
163 "{\"url_id\": 2, \"max\": 5}"))); | 163 "{\"url_id\": 2, \"max\": 5}"))); |
164 conditions.push_back(ScopedToLinkedPtr(ParseJson( | 164 conditions.push_back(ScopedToLinkedPtr(ParseJson( |
165 "{\"url_id\": 3, \"max\": 1}"))); | 165 "{\"url_id\": 3, \"max\": 1}"))); |
166 conditions.push_back(ScopedToLinkedPtr(ParseJson( | 166 conditions.push_back(ScopedToLinkedPtr(ParseJson( |
167 "{\"max\": -5}"))); // No url. | 167 "{\"max\": -5}"))); // No url. |
168 | 168 |
169 // Test insertion | 169 // Test insertion |
170 std::string error; | 170 std::string error; |
171 scoped_ptr<FulfillableConditionSet> result = | 171 scoped_ptr<FulfillableConditionSet> result = |
172 FulfillableConditionSet::Create(NULL, NULL, conditions, &error); | 172 FulfillableConditionSet::Create(nullptr, nullptr, conditions, &error); |
173 ASSERT_EQ("", error); | 173 ASSERT_EQ("", error); |
174 ASSERT_TRUE(result); | 174 ASSERT_TRUE(result); |
175 EXPECT_EQ(4u, result->conditions().size()); | 175 EXPECT_EQ(4u, result->conditions().size()); |
176 | 176 |
177 std::set<URLMatcherConditionSet::ID> url_matches; | 177 std::set<URLMatcherConditionSet::ID> url_matches; |
178 FulfillableCondition::MatchData match_data = { 0, url_matches }; | 178 FulfillableCondition::MatchData match_data = { 0, url_matches }; |
179 EXPECT_FALSE(result->IsFulfilled(1, match_data)) | 179 EXPECT_FALSE(result->IsFulfilled(1, match_data)) |
180 << "Testing an ID that's not in url_matches forwards to the Condition, " | 180 << "Testing an ID that's not in url_matches forwards to the Condition, " |
181 << "which doesn't match."; | 181 << "which doesn't match."; |
182 EXPECT_FALSE(result->IsFulfilled(-1, match_data)) | 182 EXPECT_FALSE(result->IsFulfilled(-1, match_data)) |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 : increment_(increment), min_priority_(min_priority) {} | 218 : increment_(increment), min_priority_(min_priority) {} |
219 | 219 |
220 static scoped_refptr<const SummingAction> Create( | 220 static scoped_refptr<const SummingAction> Create( |
221 content::BrowserContext* browser_context, | 221 content::BrowserContext* browser_context, |
222 const Extension* extension, | 222 const Extension* extension, |
223 const base::Value& action, | 223 const base::Value& action, |
224 std::string* error, | 224 std::string* error, |
225 bool* bad_message) { | 225 bool* bad_message) { |
226 int increment = 0; | 226 int increment = 0; |
227 int min_priority = 0; | 227 int min_priority = 0; |
228 const base::DictionaryValue* dict = NULL; | 228 const base::DictionaryValue* dict = nullptr; |
229 EXPECT_TRUE(action.GetAsDictionary(&dict)); | 229 EXPECT_TRUE(action.GetAsDictionary(&dict)); |
230 if (dict->HasKey("error")) { | 230 if (dict->HasKey("error")) { |
231 EXPECT_TRUE(dict->GetString("error", error)); | 231 EXPECT_TRUE(dict->GetString("error", error)); |
232 return scoped_refptr<const SummingAction>(NULL); | 232 return scoped_refptr<const SummingAction>(nullptr); |
233 } | 233 } |
234 if (dict->HasKey("bad")) { | 234 if (dict->HasKey("bad")) { |
235 *bad_message = true; | 235 *bad_message = true; |
236 return scoped_refptr<const SummingAction>(NULL); | 236 return scoped_refptr<const SummingAction>(nullptr); |
237 } | 237 } |
238 | 238 |
239 EXPECT_TRUE(dict->GetInteger("value", &increment)); | 239 EXPECT_TRUE(dict->GetInteger("value", &increment)); |
240 dict->GetInteger("priority", &min_priority); | 240 dict->GetInteger("priority", &min_priority); |
241 return scoped_refptr<const SummingAction>( | 241 return scoped_refptr<const SummingAction>( |
242 new SummingAction(increment, min_priority)); | 242 new SummingAction(increment, min_priority)); |
243 } | 243 } |
244 | 244 |
245 void Apply(const std::string& extension_id, | 245 void Apply(const std::string& extension_id, |
246 const base::Time& install_time, | 246 const base::Time& install_time, |
(...skipping 16 matching lines...) Expand all Loading... |
263 typedef DeclarativeActionSet<SummingAction> SummingActionSet; | 263 typedef DeclarativeActionSet<SummingAction> SummingActionSet; |
264 | 264 |
265 TEST(DeclarativeActionTest, ErrorActionSet) { | 265 TEST(DeclarativeActionTest, ErrorActionSet) { |
266 SummingActionSet::AnyVector actions; | 266 SummingActionSet::AnyVector actions; |
267 actions.push_back(ScopedToLinkedPtr(ParseJson("{\"value\": 1}"))); | 267 actions.push_back(ScopedToLinkedPtr(ParseJson("{\"value\": 1}"))); |
268 actions.push_back(ScopedToLinkedPtr(ParseJson("{\"error\": \"the error\"}"))); | 268 actions.push_back(ScopedToLinkedPtr(ParseJson("{\"error\": \"the error\"}"))); |
269 | 269 |
270 std::string error; | 270 std::string error; |
271 bool bad = false; | 271 bool bad = false; |
272 scoped_ptr<SummingActionSet> result = | 272 scoped_ptr<SummingActionSet> result = |
273 SummingActionSet::Create(NULL, NULL, actions, &error, &bad); | 273 SummingActionSet::Create(nullptr, nullptr, actions, &error, &bad); |
274 EXPECT_EQ("the error", error); | 274 EXPECT_EQ("the error", error); |
275 EXPECT_FALSE(bad); | 275 EXPECT_FALSE(bad); |
276 EXPECT_FALSE(result); | 276 EXPECT_FALSE(result); |
277 | 277 |
278 actions.clear(); | 278 actions.clear(); |
279 actions.push_back(ScopedToLinkedPtr(ParseJson("{\"value\": 1}"))); | 279 actions.push_back(ScopedToLinkedPtr(ParseJson("{\"value\": 1}"))); |
280 actions.push_back(ScopedToLinkedPtr(ParseJson("{\"bad\": 3}"))); | 280 actions.push_back(ScopedToLinkedPtr(ParseJson("{\"bad\": 3}"))); |
281 result = SummingActionSet::Create(NULL, NULL, actions, &error, &bad); | 281 result = SummingActionSet::Create(nullptr, nullptr, actions, &error, &bad); |
282 EXPECT_EQ("", error); | 282 EXPECT_EQ("", error); |
283 EXPECT_TRUE(bad); | 283 EXPECT_TRUE(bad); |
284 EXPECT_FALSE(result); | 284 EXPECT_FALSE(result); |
285 } | 285 } |
286 | 286 |
287 TEST(DeclarativeActionTest, ApplyActionSet) { | 287 TEST(DeclarativeActionTest, ApplyActionSet) { |
288 SummingActionSet::AnyVector actions; | 288 SummingActionSet::AnyVector actions; |
289 actions.push_back(ScopedToLinkedPtr(ParseJson( | 289 actions.push_back(ScopedToLinkedPtr(ParseJson( |
290 "{\"value\": 1," | 290 "{\"value\": 1," |
291 " \"priority\": 5}"))); | 291 " \"priority\": 5}"))); |
292 actions.push_back(ScopedToLinkedPtr(ParseJson("{\"value\": 2}"))); | 292 actions.push_back(ScopedToLinkedPtr(ParseJson("{\"value\": 2}"))); |
293 | 293 |
294 // Test insertion | 294 // Test insertion |
295 std::string error; | 295 std::string error; |
296 bool bad = false; | 296 bool bad = false; |
297 scoped_ptr<SummingActionSet> result = | 297 scoped_ptr<SummingActionSet> result = |
298 SummingActionSet::Create(NULL, NULL, actions, &error, &bad); | 298 SummingActionSet::Create(nullptr, nullptr, actions, &error, &bad); |
299 EXPECT_EQ("", error); | 299 EXPECT_EQ("", error); |
300 EXPECT_FALSE(bad); | 300 EXPECT_FALSE(bad); |
301 ASSERT_TRUE(result); | 301 ASSERT_TRUE(result); |
302 EXPECT_EQ(2u, result->actions().size()); | 302 EXPECT_EQ(2u, result->actions().size()); |
303 | 303 |
304 int sum = 0; | 304 int sum = 0; |
305 result->Apply("ext_id", base::Time(), &sum); | 305 result->Apply("ext_id", base::Time(), &sum); |
306 EXPECT_EQ(3, sum); | 306 EXPECT_EQ(3, sum); |
307 EXPECT_EQ(5, result->GetMinimumPriority()); | 307 EXPECT_EQ(5, result->GetMinimumPriority()); |
308 } | 308 } |
(...skipping 21 matching lines...) Expand all Loading... |
330 scoped_refptr<Extension> extension = ExtensionBuilder() | 330 scoped_refptr<Extension> extension = ExtensionBuilder() |
331 .SetManifest(SimpleManifest()) | 331 .SetManifest(SimpleManifest()) |
332 .SetID(kExtensionId) | 332 .SetID(kExtensionId) |
333 .Build(); | 333 .Build(); |
334 | 334 |
335 base::Time install_time = base::Time::Now(); | 335 base::Time install_time = base::Time::Now(); |
336 | 336 |
337 URLMatcher matcher; | 337 URLMatcher matcher; |
338 std::string error; | 338 std::string error; |
339 scoped_ptr<Rule> rule(Rule::Create(matcher.condition_factory(), | 339 scoped_ptr<Rule> rule(Rule::Create(matcher.condition_factory(), |
340 NULL, | 340 nullptr, |
341 extension.get(), | 341 extension.get(), |
342 install_time, | 342 install_time, |
343 json_rule, | 343 json_rule, |
344 Rule::ConsistencyChecker(), | 344 Rule::ConsistencyChecker(), |
345 &error)); | 345 &error)); |
346 EXPECT_EQ("", error); | 346 EXPECT_EQ("", error); |
347 ASSERT_TRUE(rule.get()); | 347 ASSERT_TRUE(rule.get()); |
348 | 348 |
349 EXPECT_EQ(kExtensionId, rule->id().first); | 349 EXPECT_EQ(kExtensionId, rule->id().first); |
350 EXPECT_EQ("rule1", rule->id().second); | 350 EXPECT_EQ("rule1", rule->id().second); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
399 " ], \n" | 399 " ], \n" |
400 " \"actions\": [ \n" | 400 " \"actions\": [ \n" |
401 " { \n" | 401 " { \n" |
402 " \"value\": 2 \n" | 402 " \"value\": 2 \n" |
403 " } \n" | 403 " } \n" |
404 " ], \n" | 404 " ], \n" |
405 " \"priority\": 200 \n" | 405 " \"priority\": 200 \n" |
406 "}"), | 406 "}"), |
407 json_rule.get())); | 407 json_rule.get())); |
408 scoped_ptr<Rule> rule(Rule::Create(matcher.condition_factory(), | 408 scoped_ptr<Rule> rule(Rule::Create(matcher.condition_factory(), |
409 NULL, | 409 nullptr, |
410 extension.get(), | 410 extension.get(), |
411 base::Time(), | 411 base::Time(), |
412 json_rule, | 412 json_rule, |
413 base::Bind(AtLeastOneCondition), | 413 base::Bind(AtLeastOneCondition), |
414 &error)); | 414 &error)); |
415 EXPECT_TRUE(rule); | 415 EXPECT_TRUE(rule); |
416 EXPECT_EQ("", error); | 416 EXPECT_EQ("", error); |
417 | 417 |
418 ASSERT_TRUE(Rule::JsonRule::Populate( | 418 ASSERT_TRUE(Rule::JsonRule::Populate( |
419 *ParseJson("{ \n" | 419 *ParseJson("{ \n" |
420 " \"id\": \"rule1\", \n" | 420 " \"id\": \"rule1\", \n" |
421 " \"conditions\": [ \n" | 421 " \"conditions\": [ \n" |
422 " ], \n" | 422 " ], \n" |
423 " \"actions\": [ \n" | 423 " \"actions\": [ \n" |
424 " { \n" | 424 " { \n" |
425 " \"value\": 2 \n" | 425 " \"value\": 2 \n" |
426 " } \n" | 426 " } \n" |
427 " ], \n" | 427 " ], \n" |
428 " \"priority\": 200 \n" | 428 " \"priority\": 200 \n" |
429 "}"), | 429 "}"), |
430 json_rule.get())); | 430 json_rule.get())); |
431 rule = Rule::Create(matcher.condition_factory(), | 431 rule = Rule::Create(matcher.condition_factory(), |
432 NULL, | 432 nullptr, |
433 extension.get(), | 433 extension.get(), |
434 base::Time(), | 434 base::Time(), |
435 json_rule, | 435 json_rule, |
436 base::Bind(AtLeastOneCondition), | 436 base::Bind(AtLeastOneCondition), |
437 &error); | 437 &error); |
438 EXPECT_FALSE(rule); | 438 EXPECT_FALSE(rule); |
439 EXPECT_EQ("No conditions", error); | 439 EXPECT_EQ("No conditions", error); |
440 } | 440 } |
441 | 441 |
442 } // namespace extensions | 442 } // namespace extensions |
OLD | NEW |