Chromium Code Reviews| Index: chrome/browser/extensions/api/declarative/declarative_rule_unittest.cc |
| diff --git a/chrome/browser/extensions/api/declarative/declarative_rule_unittest.cc b/chrome/browser/extensions/api/declarative/declarative_rule_unittest.cc |
| index 5cadb74588932395790706defd11a7cd6258a4d3..db79f10cc860578dd35b2dc13db9590631807d5d 100644 |
| --- a/chrome/browser/extensions/api/declarative/declarative_rule_unittest.cc |
| +++ b/chrome/browser/extensions/api/declarative/declarative_rule_unittest.cc |
| @@ -4,6 +4,7 @@ |
| #include "chrome/browser/extensions/api/declarative/declarative_rule.h" |
| +#include "base/basictypes.h" |
| #include "base/message_loop.h" |
| #include "base/test/values_test_util.h" |
| #include "base/values.h" |
| @@ -24,23 +25,90 @@ linked_ptr<T> ScopedToLinkedPtr(scoped_ptr<T> ptr) { |
| } // namespace |
| +struct MultipleFactoriesCondition { |
|
vabr (Chromium)
2013/01/22 08:34:53
I don't think that the layout of this struct confo
|
| + typedef int MatchData; |
| + |
| + static const char* kKey; |
| + |
| + int number_of_factories_to_consider; |
| + |
| + int GetURLMatcherConditionSets( |
| + URLMatcherConditionSet::Vector* /*condition_sets*/, |
| + int index) const { |
| + if (0 <= index && index < number_of_factories_to_consider) |
| + return index + 1; |
| + return -1; |
| + } |
| + |
| + static scoped_ptr<MultipleFactoriesCondition> Create( |
| + const std::vector<URLMatcherConditionFactory*>& /*factories*/, |
| + const base::Value& condition, |
| + std::string* error) { |
| + const base::DictionaryValue* dict = NULL; |
| + int value; |
| + if (!condition.GetAsDictionary(&dict) || !dict->HasKey(kKey) || |
| + !dict->GetInteger(kKey, &value)) { |
| + *error = "Invalid condition description."; |
| + return scoped_ptr<MultipleFactoriesCondition>(); |
| + } |
| + |
| + scoped_ptr<MultipleFactoriesCondition> result( |
| + new MultipleFactoriesCondition()); |
| + result->number_of_factories_to_consider = value; |
| + return result.Pass(); |
| + } |
| +}; |
| +const char* MultipleFactoriesCondition::kKey = "numberOfFactoriesToConsider"; |
| +typedef DeclarativeConditionSet<MultipleFactoriesCondition> |
| + MultipleFactoriesConditionSet; |
| + |
| +namespace { |
| + |
| +void TestCorrectIndexHandling(int number_of_factories) { |
| + MultipleFactoriesConditionSet::AnyVector conditions; |
| + for (int i = 0; i <= number_of_factories; ++i) { |
| + scoped_ptr<DictionaryValue> dict(new DictionaryValue); |
| + dict->SetInteger(MultipleFactoriesCondition::kKey, i); |
| + conditions.push_back(ScopedToLinkedPtr(dict.Pass())); |
| + } |
| + std::string error; |
| + std::vector<URLMatcherConditionFactory*> null_factories(number_of_factories); |
| + scoped_ptr<MultipleFactoriesConditionSet> result = |
| + MultipleFactoriesConditionSet::Create(null_factories, conditions, &error); |
| + EXPECT_TRUE(error.empty()); |
| + ASSERT_TRUE(result); |
| + |
| + for (int i = 0; i < number_of_factories; ++i) { |
| + EXPECT_EQ(i + 1, result->GetURLMatcherConditionSets(NULL, i)); |
| + } |
| + EXPECT_EQ(-1, result->GetURLMatcherConditionSets(NULL, number_of_factories)); |
| +} |
| + |
| +} // namespace |
| + |
| +TEST(DeclarativeConditionTest, GetURLMatcherConditionSets) { |
| + static const int numbers_of_factories[] = { 0, 1, 2, 5, 20 }; |
| + for (size_t i = 0; i < arraysize(numbers_of_factories); ++i) { |
| + TestCorrectIndexHandling(numbers_of_factories[i]); |
| + } |
| +} |
| + |
| struct RecordingCondition { |
| typedef int MatchData; |
| - URLMatcherConditionFactory* factory; |
| + std::vector<URLMatcherConditionFactory*> factories; |
| scoped_ptr<base::Value> value; |
| - void GetURLMatcherConditionSets( |
| - URLMatcherConditionSet::Vector* condition_sets) const { |
| + int GetURLMatcherConditionSets( |
| + URLMatcherConditionSet::Vector* condition_sets, |
| + int index) const { |
| // No condition sets. |
| - } |
| - |
| - bool has_url_matcher_condition_set() const { |
| - return false; |
| + return -1; |
| } |
| static scoped_ptr<RecordingCondition> Create( |
| - URLMatcherConditionFactory* url_matcher_condition_factory, |
| + const std::vector<URLMatcherConditionFactory*>& |
| + url_matcher_condition_factories, |
| const base::Value& condition, |
| std::string* error) { |
| const base::DictionaryValue* dict = NULL; |
| @@ -50,7 +118,7 @@ struct RecordingCondition { |
| } |
| scoped_ptr<RecordingCondition> result(new RecordingCondition()); |
| - result->factory = url_matcher_condition_factory; |
| + result->factories = url_matcher_condition_factories; |
| result->value.reset(condition.DeepCopy()); |
| return result.Pass(); |
| } |
| @@ -64,9 +132,10 @@ TEST(DeclarativeConditionTest, ErrorConditionSet) { |
| conditions.push_back(ScopedToLinkedPtr(ParseJson("{\"bad_key\": 2}"))); |
| std::string error; |
| + std::vector<URLMatcherConditionFactory*> factories; |
| + factories.push_back(matcher.condition_factory()); |
| scoped_ptr<RecordingConditionSet> result = |
| - RecordingConditionSet::Create(matcher.condition_factory(), |
| - conditions, &error); |
| + RecordingConditionSet::Create(factories, conditions, &error); |
| EXPECT_EQ("Found error key", error); |
| ASSERT_FALSE(result); |
| } |
| @@ -79,14 +148,16 @@ TEST(DeclarativeConditionTest, CreateConditionSet) { |
| // Test insertion |
| std::string error; |
| + std::vector<URLMatcherConditionFactory*> factories; |
| + factories.push_back(matcher.condition_factory()); |
| scoped_ptr<RecordingConditionSet> result = |
| - RecordingConditionSet::Create(matcher.condition_factory(), |
| - conditions, &error); |
| + RecordingConditionSet::Create(factories, conditions, &error); |
| EXPECT_EQ("", error); |
| ASSERT_TRUE(result); |
| EXPECT_EQ(2u, result->conditions().size()); |
| - EXPECT_EQ(matcher.condition_factory(), result->conditions()[0]->factory); |
| + EXPECT_EQ(1u, result->conditions()[0]->factories.size()); |
| + EXPECT_EQ(matcher.condition_factory(), result->conditions()[0]->factories[0]); |
| EXPECT_TRUE(ParseJson("{\"key\": 1}")->Equals( |
| result->conditions()[0]->value.get())); |
| } |
| @@ -102,18 +173,16 @@ struct FulfillableCondition { |
| return condition_set_id; |
| } |
| - bool has_url_matcher_condition_set() const { |
| - return true; |
| - } |
| - |
| scoped_refptr<URLMatcherConditionSet> url_matcher_condition_set() const { |
| return condition_set; |
| } |
| - void GetURLMatcherConditionSets( |
| - URLMatcherConditionSet::Vector* condition_sets) const { |
| - if (condition_set) |
| + int GetURLMatcherConditionSets( |
| + URLMatcherConditionSet::Vector* condition_sets, |
| + int index) const { |
| + if (index == 0 && condition_set) |
| condition_sets->push_back(condition_set); |
| + return -1; |
| } |
| bool IsFulfilled(const std::set<URLMatcherConditionSet::ID>& url_matches, |
| @@ -124,7 +193,8 @@ struct FulfillableCondition { |
| } |
| static scoped_ptr<FulfillableCondition> Create( |
| - URLMatcherConditionFactory* url_matcher_condition_factory, |
| + const std::vector<URLMatcherConditionFactory*>& |
| + url_matcher_condition_factories, |
| const base::Value& condition, |
| std::string* error) { |
| scoped_ptr<FulfillableCondition> result(new FulfillableCondition()); |
| @@ -160,8 +230,9 @@ TEST(DeclarativeConditionTest, FulfillConditionSet) { |
| // Test insertion |
| std::string error; |
| + std::vector<URLMatcherConditionFactory*> factories; |
| scoped_ptr<FulfillableConditionSet> result = |
| - FulfillableConditionSet::Create(NULL, conditions, &error); |
| + FulfillableConditionSet::Create(factories, conditions, &error); |
| ASSERT_EQ("", error); |
| ASSERT_TRUE(result); |
| EXPECT_EQ(4u, result->conditions().size()); |
| @@ -189,7 +260,7 @@ TEST(DeclarativeConditionTest, FulfillConditionSet) { |
| // Check the condition sets: |
| URLMatcherConditionSet::Vector condition_sets; |
| - result->GetURLMatcherConditionSets(&condition_sets); |
| + result->GetURLMatcherConditionSets(&condition_sets, 0); |
| ASSERT_EQ(3U, condition_sets.size()); |
| EXPECT_EQ(1, condition_sets[0]->id()); |
| EXPECT_EQ(2, condition_sets[1]->id()); |
| @@ -305,8 +376,10 @@ TEST(DeclarativeRuleTest, Create) { |
| base::Time install_time = base::Time::Now(); |
| URLMatcher matcher; |
| + std::vector<URLMatcherConditionFactory*> factories; |
| + factories.push_back(matcher.condition_factory()); |
| std::string error; |
| - scoped_ptr<Rule> rule(Rule::Create(matcher.condition_factory(), kExtensionId, |
| + scoped_ptr<Rule> rule(Rule::Create(factories, kExtensionId, |
| install_time, json_rule, NULL, &error)); |
| EXPECT_EQ("", error); |
| ASSERT_TRUE(rule.get()); |
| @@ -350,6 +423,8 @@ TEST(DeclarativeRuleTest, CheckConsistency) { |
| std::string error; |
| linked_ptr<Rule::JsonRule> json_rule(new Rule::JsonRule); |
| const char kExtensionId[] = "ext1"; |
| + std::vector<URLMatcherConditionFactory*> factories; |
| + factories.push_back(matcher.condition_factory()); |
| ASSERT_TRUE(Rule::JsonRule::Populate( |
| *ParseJson("{ \n" |
| @@ -366,9 +441,8 @@ TEST(DeclarativeRuleTest, CheckConsistency) { |
| " \"priority\": 200 \n" |
| "}"), |
| json_rule.get())); |
| - scoped_ptr<Rule> rule( |
| - Rule::Create(matcher.condition_factory(), kExtensionId, base::Time(), |
| - json_rule, &AtLeastOneCondition, &error)); |
| + scoped_ptr<Rule> rule(Rule::Create(factories, kExtensionId, base::Time(), |
| + json_rule, &AtLeastOneCondition, &error)); |
| EXPECT_TRUE(rule); |
| EXPECT_EQ("", error); |
| @@ -385,7 +459,7 @@ TEST(DeclarativeRuleTest, CheckConsistency) { |
| " \"priority\": 200 \n" |
| "}"), |
| json_rule.get())); |
| - rule = Rule::Create(matcher.condition_factory(), kExtensionId, base::Time(), |
| + rule = Rule::Create(factories, kExtensionId, base::Time(), |
| json_rule, &AtLeastOneCondition, &error); |
| EXPECT_FALSE(rule); |
| EXPECT_EQ("No conditions", error); |