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 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 | 212 |
213 class SummingAction : public base::RefCounted<SummingAction> { | 213 class SummingAction : public base::RefCounted<SummingAction> { |
214 public: | 214 public: |
215 typedef int ApplyInfo; | 215 typedef int ApplyInfo; |
216 | 216 |
217 SummingAction(int increment, int min_priority) | 217 SummingAction(int increment, int min_priority) |
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 HostID& host_id, |
222 const Extension* extension, | 223 const Extension* extension, |
223 const base::Value& action, | 224 const base::Value& action, |
224 std::string* error, | 225 std::string* error, |
225 bool* bad_message) { | 226 bool* bad_message) { |
226 int increment = 0; | 227 int increment = 0; |
227 int min_priority = 0; | 228 int min_priority = 0; |
228 const base::DictionaryValue* dict = NULL; | 229 const base::DictionaryValue* dict = NULL; |
229 EXPECT_TRUE(action.GetAsDictionary(&dict)); | 230 EXPECT_TRUE(action.GetAsDictionary(&dict)); |
230 if (dict->HasKey("error")) { | 231 if (dict->HasKey("error")) { |
231 EXPECT_TRUE(dict->GetString("error", error)); | 232 EXPECT_TRUE(dict->GetString("error", error)); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 typedef DeclarativeActionSet<SummingAction> SummingActionSet; | 264 typedef DeclarativeActionSet<SummingAction> SummingActionSet; |
264 | 265 |
265 TEST(DeclarativeActionTest, ErrorActionSet) { | 266 TEST(DeclarativeActionTest, ErrorActionSet) { |
266 SummingActionSet::AnyVector actions; | 267 SummingActionSet::AnyVector actions; |
267 actions.push_back(ScopedToLinkedPtr(ParseJson("{\"value\": 1}"))); | 268 actions.push_back(ScopedToLinkedPtr(ParseJson("{\"value\": 1}"))); |
268 actions.push_back(ScopedToLinkedPtr(ParseJson("{\"error\": \"the error\"}"))); | 269 actions.push_back(ScopedToLinkedPtr(ParseJson("{\"error\": \"the error\"}"))); |
269 | 270 |
270 std::string error; | 271 std::string error; |
271 bool bad = false; | 272 bool bad = false; |
272 scoped_ptr<SummingActionSet> result = | 273 scoped_ptr<SummingActionSet> result = |
273 SummingActionSet::Create(NULL, NULL, actions, &error, &bad); | 274 SummingActionSet::Create(NULL, HostID(), NULL, actions, &error, &bad); |
274 EXPECT_EQ("the error", error); | 275 EXPECT_EQ("the error", error); |
275 EXPECT_FALSE(bad); | 276 EXPECT_FALSE(bad); |
276 EXPECT_FALSE(result); | 277 EXPECT_FALSE(result); |
277 | 278 |
278 actions.clear(); | 279 actions.clear(); |
279 actions.push_back(ScopedToLinkedPtr(ParseJson("{\"value\": 1}"))); | 280 actions.push_back(ScopedToLinkedPtr(ParseJson("{\"value\": 1}"))); |
280 actions.push_back(ScopedToLinkedPtr(ParseJson("{\"bad\": 3}"))); | 281 actions.push_back(ScopedToLinkedPtr(ParseJson("{\"bad\": 3}"))); |
281 result = SummingActionSet::Create(NULL, NULL, actions, &error, &bad); | 282 result = SummingActionSet::Create( |
| 283 NULL, HostID(), NULL, actions, &error, &bad); |
282 EXPECT_EQ("", error); | 284 EXPECT_EQ("", error); |
283 EXPECT_TRUE(bad); | 285 EXPECT_TRUE(bad); |
284 EXPECT_FALSE(result); | 286 EXPECT_FALSE(result); |
285 } | 287 } |
286 | 288 |
287 TEST(DeclarativeActionTest, ApplyActionSet) { | 289 TEST(DeclarativeActionTest, ApplyActionSet) { |
288 SummingActionSet::AnyVector actions; | 290 SummingActionSet::AnyVector actions; |
289 actions.push_back(ScopedToLinkedPtr(ParseJson( | 291 actions.push_back(ScopedToLinkedPtr(ParseJson( |
290 "{\"value\": 1," | 292 "{\"value\": 1," |
291 " \"priority\": 5}"))); | 293 " \"priority\": 5}"))); |
292 actions.push_back(ScopedToLinkedPtr(ParseJson("{\"value\": 2}"))); | 294 actions.push_back(ScopedToLinkedPtr(ParseJson("{\"value\": 2}"))); |
293 | 295 |
294 // Test insertion | 296 // Test insertion |
295 std::string error; | 297 std::string error; |
296 bool bad = false; | 298 bool bad = false; |
297 scoped_ptr<SummingActionSet> result = | 299 scoped_ptr<SummingActionSet> result = |
298 SummingActionSet::Create(NULL, NULL, actions, &error, &bad); | 300 SummingActionSet::Create(NULL, HostID(), NULL, actions, &error, &bad); |
299 EXPECT_EQ("", error); | 301 EXPECT_EQ("", error); |
300 EXPECT_FALSE(bad); | 302 EXPECT_FALSE(bad); |
301 ASSERT_TRUE(result); | 303 ASSERT_TRUE(result); |
302 EXPECT_EQ(2u, result->actions().size()); | 304 EXPECT_EQ(2u, result->actions().size()); |
303 | 305 |
304 int sum = 0; | 306 int sum = 0; |
305 result->Apply("ext_id", base::Time(), &sum); | 307 result->Apply("ext_id", base::Time(), &sum); |
306 EXPECT_EQ(3, sum); | 308 EXPECT_EQ(3, sum); |
307 EXPECT_EQ(5, result->GetMinimumPriority()); | 309 EXPECT_EQ(5, result->GetMinimumPriority()); |
308 } | 310 } |
(...skipping 20 matching lines...) Expand all Loading... |
329 const char kExtensionId[] = "ext1"; | 331 const char kExtensionId[] = "ext1"; |
330 scoped_refptr<Extension> extension = ExtensionBuilder() | 332 scoped_refptr<Extension> extension = ExtensionBuilder() |
331 .SetManifest(SimpleManifest()) | 333 .SetManifest(SimpleManifest()) |
332 .SetID(kExtensionId) | 334 .SetID(kExtensionId) |
333 .Build(); | 335 .Build(); |
334 | 336 |
335 base::Time install_time = base::Time::Now(); | 337 base::Time install_time = base::Time::Now(); |
336 | 338 |
337 URLMatcher matcher; | 339 URLMatcher matcher; |
338 std::string error; | 340 std::string error; |
339 scoped_ptr<Rule> rule(Rule::Create(matcher.condition_factory(), | 341 scoped_ptr<Rule> rule( |
340 NULL, | 342 Rule::Create(matcher.condition_factory(), |
341 extension.get(), | 343 NULL, |
342 install_time, | 344 HostID(HostID::EXTENSIONS, extension.get()->id()), |
343 json_rule, | 345 extension.get(), |
344 Rule::ConsistencyChecker(), | 346 install_time, |
345 &error)); | 347 json_rule, |
| 348 Rule::ConsistencyChecker(), |
| 349 &error)); |
346 EXPECT_EQ("", error); | 350 EXPECT_EQ("", error); |
347 ASSERT_TRUE(rule.get()); | 351 ASSERT_TRUE(rule.get()); |
348 | 352 |
349 EXPECT_EQ(kExtensionId, rule->id().first); | 353 EXPECT_EQ(kExtensionId, rule->id().first); |
350 EXPECT_EQ("rule1", rule->id().second); | 354 EXPECT_EQ("rule1", rule->id().second); |
351 | 355 |
352 EXPECT_EQ(200, rule->priority()); | 356 EXPECT_EQ(200, rule->priority()); |
353 | 357 |
354 const Rule::ConditionSet& condition_set = rule->conditions(); | 358 const Rule::ConditionSet& condition_set = rule->conditions(); |
355 const Rule::ConditionSet::Conditions& conditions = | 359 const Rule::ConditionSet::Conditions& conditions = |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
398 " {\"url_id\": 2, \"max\": 5}, \n" | 402 " {\"url_id\": 2, \"max\": 5}, \n" |
399 " ], \n" | 403 " ], \n" |
400 " \"actions\": [ \n" | 404 " \"actions\": [ \n" |
401 " { \n" | 405 " { \n" |
402 " \"value\": 2 \n" | 406 " \"value\": 2 \n" |
403 " } \n" | 407 " } \n" |
404 " ], \n" | 408 " ], \n" |
405 " \"priority\": 200 \n" | 409 " \"priority\": 200 \n" |
406 "}"), | 410 "}"), |
407 json_rule.get())); | 411 json_rule.get())); |
408 scoped_ptr<Rule> rule(Rule::Create(matcher.condition_factory(), | 412 scoped_ptr<Rule> rule( |
409 NULL, | 413 Rule::Create(matcher.condition_factory(), |
410 extension.get(), | 414 NULL, |
411 base::Time(), | 415 HostID(HostID::EXTENSIONS, extension.get()->id()), |
412 json_rule, | 416 extension.get(), |
413 base::Bind(AtLeastOneCondition), | 417 base::Time(), |
414 &error)); | 418 json_rule, |
| 419 base::Bind(AtLeastOneCondition), |
| 420 &error)); |
415 EXPECT_TRUE(rule); | 421 EXPECT_TRUE(rule); |
416 EXPECT_EQ("", error); | 422 EXPECT_EQ("", error); |
417 | 423 |
418 ASSERT_TRUE(Rule::JsonRule::Populate( | 424 ASSERT_TRUE(Rule::JsonRule::Populate( |
419 *ParseJson("{ \n" | 425 *ParseJson("{ \n" |
420 " \"id\": \"rule1\", \n" | 426 " \"id\": \"rule1\", \n" |
421 " \"conditions\": [ \n" | 427 " \"conditions\": [ \n" |
422 " ], \n" | 428 " ], \n" |
423 " \"actions\": [ \n" | 429 " \"actions\": [ \n" |
424 " { \n" | 430 " { \n" |
425 " \"value\": 2 \n" | 431 " \"value\": 2 \n" |
426 " } \n" | 432 " } \n" |
427 " ], \n" | 433 " ], \n" |
428 " \"priority\": 200 \n" | 434 " \"priority\": 200 \n" |
429 "}"), | 435 "}"), |
430 json_rule.get())); | 436 json_rule.get())); |
431 rule = Rule::Create(matcher.condition_factory(), | 437 rule = Rule::Create(matcher.condition_factory(), |
432 NULL, | 438 NULL, |
| 439 HostID(HostID::EXTENSIONS, extension.get()->id()), |
433 extension.get(), | 440 extension.get(), |
434 base::Time(), | 441 base::Time(), |
435 json_rule, | 442 json_rule, |
436 base::Bind(AtLeastOneCondition), | 443 base::Bind(AtLeastOneCondition), |
437 &error); | 444 &error); |
438 EXPECT_FALSE(rule); | 445 EXPECT_FALSE(rule); |
439 EXPECT_EQ("No conditions", error); | 446 EXPECT_EQ("No conditions", error); |
440 } | 447 } |
441 | 448 |
442 } // namespace extensions | 449 } // namespace extensions |
OLD | NEW |