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