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