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