| 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" |
| 11 #include "testing/gmock/include/gmock/gmock.h" | 11 #include "testing/gmock/include/gmock/gmock.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 namespace extensions { | 14 namespace extensions { |
| 15 | 15 |
| 16 using json_schema_compiler::any::Any; | |
| 17 using base::test::ParseJson; | 16 using base::test::ParseJson; |
| 18 | 17 |
| 18 namespace { |
| 19 |
| 20 template<typename T> |
| 21 linked_ptr<T> ScopedToLinkedPtr(scoped_ptr<T> ptr) { |
| 22 return linked_ptr<T>(ptr.release()); |
| 23 } |
| 24 |
| 25 } // namespace |
| 26 |
| 19 struct RecordingCondition { | 27 struct RecordingCondition { |
| 20 typedef int MatchData; | 28 typedef int MatchData; |
| 21 | 29 |
| 22 URLMatcherConditionFactory* factory; | 30 URLMatcherConditionFactory* factory; |
| 23 scoped_ptr<base::Value> value; | 31 scoped_ptr<base::Value> value; |
| 24 | 32 |
| 25 void GetURLMatcherConditionSets( | 33 void GetURLMatcherConditionSets( |
| 26 URLMatcherConditionSet::Vector* condition_sets) const { | 34 URLMatcherConditionSet::Vector* condition_sets) const { |
| 27 // No condition sets. | 35 // No condition sets. |
| 28 } | 36 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 45 result->factory = url_matcher_condition_factory; | 53 result->factory = url_matcher_condition_factory; |
| 46 result->value.reset(condition.DeepCopy()); | 54 result->value.reset(condition.DeepCopy()); |
| 47 return result.Pass(); | 55 return result.Pass(); |
| 48 } | 56 } |
| 49 }; | 57 }; |
| 50 typedef DeclarativeConditionSet<RecordingCondition> RecordingConditionSet; | 58 typedef DeclarativeConditionSet<RecordingCondition> RecordingConditionSet; |
| 51 | 59 |
| 52 TEST(DeclarativeConditionTest, ErrorConditionSet) { | 60 TEST(DeclarativeConditionTest, ErrorConditionSet) { |
| 53 URLMatcher matcher; | 61 URLMatcher matcher; |
| 54 RecordingConditionSet::AnyVector conditions; | 62 RecordingConditionSet::AnyVector conditions; |
| 55 conditions.push_back(make_linked_ptr(new Any(ParseJson("{\"key\": 1}")))); | 63 conditions.push_back(ScopedToLinkedPtr(ParseJson("{\"key\": 1}"))); |
| 56 conditions.push_back(make_linked_ptr(new Any(ParseJson("{\"bad_key\": 2}")))); | 64 conditions.push_back(ScopedToLinkedPtr(ParseJson("{\"bad_key\": 2}"))); |
| 57 | 65 |
| 58 std::string error; | 66 std::string error; |
| 59 scoped_ptr<RecordingConditionSet> result = | 67 scoped_ptr<RecordingConditionSet> result = |
| 60 RecordingConditionSet::Create(matcher.condition_factory(), | 68 RecordingConditionSet::Create(matcher.condition_factory(), |
| 61 conditions, &error); | 69 conditions, &error); |
| 62 EXPECT_EQ("Found error key", error); | 70 EXPECT_EQ("Found error key", error); |
| 63 ASSERT_FALSE(result); | 71 ASSERT_FALSE(result); |
| 64 } | 72 } |
| 65 | 73 |
| 66 TEST(DeclarativeConditionTest, CreateConditionSet) { | 74 TEST(DeclarativeConditionTest, CreateConditionSet) { |
| 67 URLMatcher matcher; | 75 URLMatcher matcher; |
| 68 RecordingConditionSet::AnyVector conditions; | 76 RecordingConditionSet::AnyVector conditions; |
| 69 conditions.push_back(make_linked_ptr(new Any(ParseJson("{\"key\": 1}")))); | 77 conditions.push_back(ScopedToLinkedPtr(ParseJson("{\"key\": 1}"))); |
| 70 conditions.push_back(make_linked_ptr(new Any(ParseJson("[\"val1\", 2]")))); | 78 conditions.push_back(ScopedToLinkedPtr(ParseJson("[\"val1\", 2]"))); |
| 71 | 79 |
| 72 // Test insertion | 80 // Test insertion |
| 73 std::string error; | 81 std::string error; |
| 74 scoped_ptr<RecordingConditionSet> result = | 82 scoped_ptr<RecordingConditionSet> result = |
| 75 RecordingConditionSet::Create(matcher.condition_factory(), | 83 RecordingConditionSet::Create(matcher.condition_factory(), |
| 76 conditions, &error); | 84 conditions, &error); |
| 77 EXPECT_EQ("", error); | 85 EXPECT_EQ("", error); |
| 78 ASSERT_TRUE(result); | 86 ASSERT_TRUE(result); |
| 79 EXPECT_EQ(2u, result->conditions().size()); | 87 EXPECT_EQ(2u, result->conditions().size()); |
| 80 | 88 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 result->condition_set_id, | 142 result->condition_set_id, |
| 135 URLMatcherConditionSet::Conditions()); | 143 URLMatcherConditionSet::Conditions()); |
| 136 } | 144 } |
| 137 return result.Pass(); | 145 return result.Pass(); |
| 138 } | 146 } |
| 139 }; | 147 }; |
| 140 | 148 |
| 141 TEST(DeclarativeConditionTest, FulfillConditionSet) { | 149 TEST(DeclarativeConditionTest, FulfillConditionSet) { |
| 142 typedef DeclarativeConditionSet<FulfillableCondition> FulfillableConditionSet; | 150 typedef DeclarativeConditionSet<FulfillableCondition> FulfillableConditionSet; |
| 143 FulfillableConditionSet::AnyVector conditions; | 151 FulfillableConditionSet::AnyVector conditions; |
| 144 conditions.push_back(make_linked_ptr(new Any(ParseJson( | 152 conditions.push_back(ScopedToLinkedPtr(ParseJson( |
| 145 "{\"url_id\": 1, \"max\": 3}")))); | 153 "{\"url_id\": 1, \"max\": 3}"))); |
| 146 conditions.push_back(make_linked_ptr(new Any(ParseJson( | 154 conditions.push_back(ScopedToLinkedPtr(ParseJson( |
| 147 "{\"url_id\": 2, \"max\": 5}")))); | 155 "{\"url_id\": 2, \"max\": 5}"))); |
| 148 conditions.push_back(make_linked_ptr(new Any(ParseJson( | 156 conditions.push_back(ScopedToLinkedPtr(ParseJson( |
| 149 "{\"url_id\": 3, \"max\": 1}")))); | 157 "{\"url_id\": 3, \"max\": 1}"))); |
| 150 conditions.push_back(make_linked_ptr(new Any(ParseJson( | 158 conditions.push_back(ScopedToLinkedPtr(ParseJson( |
| 151 "{\"max\": -5}")))); // No url. | 159 "{\"max\": -5}"))); // No url. |
| 152 | 160 |
| 153 // Test insertion | 161 // Test insertion |
| 154 std::string error; | 162 std::string error; |
| 155 scoped_ptr<FulfillableConditionSet> result = | 163 scoped_ptr<FulfillableConditionSet> result = |
| 156 FulfillableConditionSet::Create(NULL, conditions, &error); | 164 FulfillableConditionSet::Create(NULL, conditions, &error); |
| 157 ASSERT_EQ("", error); | 165 ASSERT_EQ("", error); |
| 158 ASSERT_TRUE(result); | 166 ASSERT_TRUE(result); |
| 159 EXPECT_EQ(4u, result->conditions().size()); | 167 EXPECT_EQ(4u, result->conditions().size()); |
| 160 | 168 |
| 161 std::set<URLMatcherConditionSet::ID> url_matches; | 169 std::set<URLMatcherConditionSet::ID> url_matches; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 } | 231 } |
| 224 | 232 |
| 225 int GetMinimumPriority() const { | 233 int GetMinimumPriority() const { |
| 226 return min_priority; | 234 return min_priority; |
| 227 } | 235 } |
| 228 }; | 236 }; |
| 229 typedef DeclarativeActionSet<SummingAction> SummingActionSet; | 237 typedef DeclarativeActionSet<SummingAction> SummingActionSet; |
| 230 | 238 |
| 231 TEST(DeclarativeActionTest, ErrorActionSet) { | 239 TEST(DeclarativeActionTest, ErrorActionSet) { |
| 232 SummingActionSet::AnyVector actions; | 240 SummingActionSet::AnyVector actions; |
| 233 actions.push_back(make_linked_ptr(new Any(ParseJson("{\"value\": 1}")))); | 241 actions.push_back(ScopedToLinkedPtr(ParseJson("{\"value\": 1}"))); |
| 234 actions.push_back(make_linked_ptr(new Any(ParseJson( | 242 actions.push_back(ScopedToLinkedPtr(ParseJson("{\"error\": \"the error\"}"))); |
| 235 "{\"error\": \"the error\"}")))); | |
| 236 | 243 |
| 237 std::string error; | 244 std::string error; |
| 238 bool bad = false; | 245 bool bad = false; |
| 239 scoped_ptr<SummingActionSet> result = | 246 scoped_ptr<SummingActionSet> result = |
| 240 SummingActionSet::Create(actions, &error, &bad); | 247 SummingActionSet::Create(actions, &error, &bad); |
| 241 EXPECT_EQ("the error", error); | 248 EXPECT_EQ("the error", error); |
| 242 EXPECT_FALSE(bad); | 249 EXPECT_FALSE(bad); |
| 243 EXPECT_FALSE(result); | 250 EXPECT_FALSE(result); |
| 244 | 251 |
| 245 actions.clear(); | 252 actions.clear(); |
| 246 actions.push_back(make_linked_ptr(new Any(ParseJson("{\"value\": 1}")))); | 253 actions.push_back(ScopedToLinkedPtr(ParseJson("{\"value\": 1}"))); |
| 247 actions.push_back(make_linked_ptr(new Any(ParseJson("{\"bad\": 3}")))); | 254 actions.push_back(ScopedToLinkedPtr(ParseJson("{\"bad\": 3}"))); |
| 248 result = SummingActionSet::Create(actions, &error, &bad); | 255 result = SummingActionSet::Create(actions, &error, &bad); |
| 249 EXPECT_EQ("", error); | 256 EXPECT_EQ("", error); |
| 250 EXPECT_TRUE(bad); | 257 EXPECT_TRUE(bad); |
| 251 EXPECT_FALSE(result); | 258 EXPECT_FALSE(result); |
| 252 } | 259 } |
| 253 | 260 |
| 254 TEST(DeclarativeActionTest, ApplyActionSet) { | 261 TEST(DeclarativeActionTest, ApplyActionSet) { |
| 255 SummingActionSet::AnyVector actions; | 262 SummingActionSet::AnyVector actions; |
| 256 actions.push_back(make_linked_ptr(new Any(ParseJson( | 263 actions.push_back(ScopedToLinkedPtr(ParseJson( |
| 257 "{\"value\": 1," | 264 "{\"value\": 1," |
| 258 " \"priority\": 5}")))); | 265 " \"priority\": 5}"))); |
| 259 actions.push_back(make_linked_ptr(new Any(ParseJson("{\"value\": 2}")))); | 266 actions.push_back(ScopedToLinkedPtr(ParseJson("{\"value\": 2}"))); |
| 260 | 267 |
| 261 // Test insertion | 268 // Test insertion |
| 262 std::string error; | 269 std::string error; |
| 263 bool bad = false; | 270 bool bad = false; |
| 264 scoped_ptr<SummingActionSet> result = | 271 scoped_ptr<SummingActionSet> result = |
| 265 SummingActionSet::Create(actions, &error, &bad); | 272 SummingActionSet::Create(actions, &error, &bad); |
| 266 EXPECT_EQ("", error); | 273 EXPECT_EQ("", error); |
| 267 EXPECT_FALSE(bad); | 274 EXPECT_FALSE(bad); |
| 268 ASSERT_TRUE(result); | 275 ASSERT_TRUE(result); |
| 269 EXPECT_EQ(2u, result->actions().size()); | 276 EXPECT_EQ(2u, result->actions().size()); |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 378 " \"priority\": 200 \n" | 385 " \"priority\": 200 \n" |
| 379 "}"), | 386 "}"), |
| 380 json_rule.get())); | 387 json_rule.get())); |
| 381 rule = Rule::Create(matcher.condition_factory(), kExtensionId, base::Time(), | 388 rule = Rule::Create(matcher.condition_factory(), kExtensionId, base::Time(), |
| 382 json_rule, &AtLeastOneCondition, &error); | 389 json_rule, &AtLeastOneCondition, &error); |
| 383 EXPECT_FALSE(rule); | 390 EXPECT_FALSE(rule); |
| 384 EXPECT_EQ("No conditions", error); | 391 EXPECT_EQ("No conditions", error); |
| 385 } | 392 } |
| 386 | 393 |
| 387 } // namespace extensions | 394 } // namespace extensions |
| OLD | NEW |