| 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/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/test/values_test_util.h" | 8 #include "base/test/values_test_util.h" |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "chrome/common/extensions/matcher/url_matcher_constants.h" | 10 #include "chrome/common/extensions/matcher/url_matcher_constants.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 typedef int MatchData; | 28 typedef int MatchData; |
| 29 | 29 |
| 30 URLMatcherConditionFactory* factory; | 30 URLMatcherConditionFactory* factory; |
| 31 scoped_ptr<base::Value> value; | 31 scoped_ptr<base::Value> value; |
| 32 | 32 |
| 33 void GetURLMatcherConditionSets( | 33 void GetURLMatcherConditionSets( |
| 34 URLMatcherConditionSet::Vector* condition_sets) const { | 34 URLMatcherConditionSet::Vector* condition_sets) const { |
| 35 // No condition sets. | 35 // No condition sets. |
| 36 } | 36 } |
| 37 | 37 |
| 38 bool has_url_matcher_condition_set() const { | |
| 39 return false; | |
| 40 } | |
| 41 | |
| 42 static scoped_ptr<RecordingCondition> Create( | 38 static scoped_ptr<RecordingCondition> Create( |
| 43 URLMatcherConditionFactory* url_matcher_condition_factory, | 39 URLMatcherConditionFactory* url_matcher_condition_factory, |
| 44 const base::Value& condition, | 40 const base::Value& condition, |
| 45 std::string* error) { | 41 std::string* error) { |
| 46 const base::DictionaryValue* dict = NULL; | 42 const base::DictionaryValue* dict = NULL; |
| 47 if (condition.GetAsDictionary(&dict) && dict->HasKey("bad_key")) { | 43 if (condition.GetAsDictionary(&dict) && dict->HasKey("bad_key")) { |
| 48 *error = "Found error key"; | 44 *error = "Found error key"; |
| 49 return scoped_ptr<RecordingCondition>(); | 45 return scoped_ptr<RecordingCondition>(); |
| 50 } | 46 } |
| 51 | 47 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 EXPECT_EQ("", error); | 81 EXPECT_EQ("", error); |
| 86 ASSERT_TRUE(result); | 82 ASSERT_TRUE(result); |
| 87 EXPECT_EQ(2u, result->conditions().size()); | 83 EXPECT_EQ(2u, result->conditions().size()); |
| 88 | 84 |
| 89 EXPECT_EQ(matcher.condition_factory(), result->conditions()[0]->factory); | 85 EXPECT_EQ(matcher.condition_factory(), result->conditions()[0]->factory); |
| 90 EXPECT_TRUE(ParseJson("{\"key\": 1}")->Equals( | 86 EXPECT_TRUE(ParseJson("{\"key\": 1}")->Equals( |
| 91 result->conditions()[0]->value.get())); | 87 result->conditions()[0]->value.get())); |
| 92 } | 88 } |
| 93 | 89 |
| 94 struct FulfillableCondition { | 90 struct FulfillableCondition { |
| 95 typedef int MatchData; | 91 struct MatchData { |
| 92 int value; |
| 93 const std::set<URLMatcherConditionSet::ID>& url_matches; |
| 94 }; |
| 96 | 95 |
| 97 scoped_refptr<URLMatcherConditionSet> condition_set; | 96 scoped_refptr<URLMatcherConditionSet> condition_set; |
| 98 int condition_set_id; | 97 int condition_set_id; |
| 99 int max_value; | 98 int max_value; |
| 100 | 99 |
| 101 URLMatcherConditionSet::ID url_matcher_condition_set_id() const { | 100 URLMatcherConditionSet::ID url_matcher_condition_set_id() const { |
| 102 return condition_set_id; | 101 return condition_set_id; |
| 103 } | 102 } |
| 104 | 103 |
| 105 bool has_url_matcher_condition_set() const { | |
| 106 return true; | |
| 107 } | |
| 108 | |
| 109 scoped_refptr<URLMatcherConditionSet> url_matcher_condition_set() const { | 104 scoped_refptr<URLMatcherConditionSet> url_matcher_condition_set() const { |
| 110 return condition_set; | 105 return condition_set; |
| 111 } | 106 } |
| 112 | 107 |
| 113 void GetURLMatcherConditionSets( | 108 void GetURLMatcherConditionSets( |
| 114 URLMatcherConditionSet::Vector* condition_sets) const { | 109 URLMatcherConditionSet::Vector* condition_sets) const { |
| 115 if (condition_set) | 110 if (condition_set) |
| 116 condition_sets->push_back(condition_set); | 111 condition_sets->push_back(condition_set); |
| 117 } | 112 } |
| 118 | 113 |
| 119 bool IsFulfilled(const std::set<URLMatcherConditionSet::ID>& url_matches, | 114 bool IsFulfilled(const MatchData& match_data) const { |
| 120 int match_data) const { | 115 if (condition_set_id != -1 && |
| 121 if (condition_set_id != -1 && !ContainsKey(url_matches, condition_set_id)) | 116 !ContainsKey(match_data.url_matches, condition_set_id)) |
| 122 return false; | 117 return false; |
| 123 return match_data <= max_value; | 118 return match_data.value <= max_value; |
| 124 } | 119 } |
| 125 | 120 |
| 126 static scoped_ptr<FulfillableCondition> Create( | 121 static scoped_ptr<FulfillableCondition> Create( |
| 127 URLMatcherConditionFactory* url_matcher_condition_factory, | 122 URLMatcherConditionFactory* /*url_matcher_condition_factory*/, |
| 128 const base::Value& condition, | 123 const base::Value& condition, |
| 129 std::string* error) { | 124 std::string* error) { |
| 130 scoped_ptr<FulfillableCondition> result(new FulfillableCondition()); | 125 scoped_ptr<FulfillableCondition> result(new FulfillableCondition()); |
| 131 const base::DictionaryValue* dict; | 126 const base::DictionaryValue* dict; |
| 132 if (!condition.GetAsDictionary(&dict)) { | 127 if (!condition.GetAsDictionary(&dict)) { |
| 133 *error = "Expected dict"; | 128 *error = "Expected dict"; |
| 134 return result.Pass(); | 129 return result.Pass(); |
| 135 } | 130 } |
| 136 if (!dict->GetInteger("url_id", &result->condition_set_id)) | 131 if (!dict->GetInteger("url_id", &result->condition_set_id)) |
| 137 result->condition_set_id = -1; | 132 result->condition_set_id = -1; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 160 | 155 |
| 161 // Test insertion | 156 // Test insertion |
| 162 std::string error; | 157 std::string error; |
| 163 scoped_ptr<FulfillableConditionSet> result = | 158 scoped_ptr<FulfillableConditionSet> result = |
| 164 FulfillableConditionSet::Create(NULL, conditions, &error); | 159 FulfillableConditionSet::Create(NULL, conditions, &error); |
| 165 ASSERT_EQ("", error); | 160 ASSERT_EQ("", error); |
| 166 ASSERT_TRUE(result); | 161 ASSERT_TRUE(result); |
| 167 EXPECT_EQ(4u, result->conditions().size()); | 162 EXPECT_EQ(4u, result->conditions().size()); |
| 168 | 163 |
| 169 std::set<URLMatcherConditionSet::ID> url_matches; | 164 std::set<URLMatcherConditionSet::ID> url_matches; |
| 170 EXPECT_FALSE(result->IsFulfilled(1, url_matches, 0)) | 165 FulfillableCondition::MatchData match_data = { 0, url_matches }; |
| 166 EXPECT_FALSE(result->IsFulfilled(1, match_data)) |
| 171 << "Testing an ID that's not in url_matches forwards to the Condition, " | 167 << "Testing an ID that's not in url_matches forwards to the Condition, " |
| 172 << "which doesn't match."; | 168 << "which doesn't match."; |
| 173 EXPECT_FALSE(result->IsFulfilled(-1, url_matches, 0)) | 169 EXPECT_FALSE(result->IsFulfilled(-1, match_data)) |
| 174 << "Testing the 'no ID' value tries to match the 4th condition, but " | 170 << "Testing the 'no ID' value tries to match the 4th condition, but " |
| 175 << "its max is too low."; | 171 << "its max is too low."; |
| 176 EXPECT_TRUE(result->IsFulfilled(-1, url_matches, -5)) | 172 match_data.value = -5; |
| 173 EXPECT_TRUE(result->IsFulfilled(-1, match_data)) |
| 177 << "Testing the 'no ID' value tries to match the 4th condition, and " | 174 << "Testing the 'no ID' value tries to match the 4th condition, and " |
| 178 << "this value is low enough."; | 175 << "this value is low enough."; |
| 179 | 176 |
| 180 url_matches.insert(1); | 177 url_matches.insert(1); |
| 181 EXPECT_TRUE(result->IsFulfilled(1, url_matches, 3)) | 178 match_data.value = 3; |
| 179 EXPECT_TRUE(result->IsFulfilled(1, match_data)) |
| 182 << "Tests a condition with a url matcher, for a matching value."; | 180 << "Tests a condition with a url matcher, for a matching value."; |
| 183 EXPECT_FALSE(result->IsFulfilled(1, url_matches, 4)) | 181 match_data.value = 4; |
| 182 EXPECT_FALSE(result->IsFulfilled(1, match_data)) |
| 184 << "Tests a condition with a url matcher, for a non-matching value " | 183 << "Tests a condition with a url matcher, for a non-matching value " |
| 185 << "that would match a different condition."; | 184 << "that would match a different condition."; |
| 186 url_matches.insert(2); | 185 url_matches.insert(2); |
| 187 EXPECT_TRUE(result->IsFulfilled(2, url_matches, 4)) | 186 EXPECT_TRUE(result->IsFulfilled(2, match_data)) |
| 188 << "Tests with 2 elements in the match set."; | 187 << "Tests with 2 elements in the match set."; |
| 189 | 188 |
| 190 // Check the condition sets: | 189 // Check the condition sets: |
| 191 URLMatcherConditionSet::Vector condition_sets; | 190 URLMatcherConditionSet::Vector condition_sets; |
| 192 result->GetURLMatcherConditionSets(&condition_sets); | 191 result->GetURLMatcherConditionSets(&condition_sets); |
| 193 ASSERT_EQ(3U, condition_sets.size()); | 192 ASSERT_EQ(3U, condition_sets.size()); |
| 194 EXPECT_EQ(1, condition_sets[0]->id()); | 193 EXPECT_EQ(1, condition_sets[0]->id()); |
| 195 EXPECT_EQ(2, condition_sets[1]->id()); | 194 EXPECT_EQ(2, condition_sets[1]->id()); |
| 196 EXPECT_EQ(3, condition_sets[2]->id()); | 195 EXPECT_EQ(3, condition_sets[2]->id()); |
| 197 } | 196 } |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 385 " \"priority\": 200 \n" | 384 " \"priority\": 200 \n" |
| 386 "}"), | 385 "}"), |
| 387 json_rule.get())); | 386 json_rule.get())); |
| 388 rule = Rule::Create(matcher.condition_factory(), kExtensionId, base::Time(), | 387 rule = Rule::Create(matcher.condition_factory(), kExtensionId, base::Time(), |
| 389 json_rule, &AtLeastOneCondition, &error); | 388 json_rule, &AtLeastOneCondition, &error); |
| 390 EXPECT_FALSE(rule); | 389 EXPECT_FALSE(rule); |
| 391 EXPECT_EQ("No conditions", error); | 390 EXPECT_EQ("No conditions", error); |
| 392 } | 391 } |
| 393 | 392 |
| 394 } // namespace extensions | 393 } // namespace extensions |
| OLD | NEW |