| 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 "extensions/browser/api/declarative/declarative_rule.h" | 5 #include "extensions/browser/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" |
| 11 #include "components/url_matcher/url_matcher_constants.h" | 11 #include "components/url_matcher/url_matcher_constants.h" |
| 12 #include "extensions/common/extension_builder.h" | 12 #include "extensions/common/extension_builder.h" |
| 13 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 15 |
| 16 using base::test::ParseJson; | 16 using base::test::ParseJson; |
| 17 using url_matcher::URLMatcher; | 17 using url_matcher::URLMatcher; |
| 18 using url_matcher::URLMatcherConditionFactory; | 18 using url_matcher::URLMatcherConditionFactory; |
| 19 using url_matcher::URLMatcherConditionSet; | 19 using url_matcher::URLMatcherConditionSet; |
| 20 | 20 |
| 21 namespace extensions { | 21 namespace extensions { |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 template<typename T> | |
| 26 linked_ptr<T> ScopedToLinkedPtr(scoped_ptr<T> ptr) { | |
| 27 return linked_ptr<T>(ptr.release()); | |
| 28 } | |
| 29 | |
| 30 scoped_ptr<base::DictionaryValue> SimpleManifest() { | 25 scoped_ptr<base::DictionaryValue> SimpleManifest() { |
| 31 return DictionaryBuilder() | 26 return DictionaryBuilder() |
| 32 .Set("name", "extension") | 27 .Set("name", "extension") |
| 33 .Set("manifest_version", 2) | 28 .Set("manifest_version", 2) |
| 34 .Set("version", "1.0") | 29 .Set("version", "1.0") |
| 35 .Build(); | 30 .Build(); |
| 36 } | 31 } |
| 37 | 32 |
| 38 } // namespace | 33 } // namespace |
| 39 | 34 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 63 result->factory = url_matcher_condition_factory; | 58 result->factory = url_matcher_condition_factory; |
| 64 result->value.reset(condition.DeepCopy()); | 59 result->value.reset(condition.DeepCopy()); |
| 65 return result; | 60 return result; |
| 66 } | 61 } |
| 67 }; | 62 }; |
| 68 typedef DeclarativeConditionSet<RecordingCondition> RecordingConditionSet; | 63 typedef DeclarativeConditionSet<RecordingCondition> RecordingConditionSet; |
| 69 | 64 |
| 70 TEST(DeclarativeConditionTest, ErrorConditionSet) { | 65 TEST(DeclarativeConditionTest, ErrorConditionSet) { |
| 71 URLMatcher matcher; | 66 URLMatcher matcher; |
| 72 RecordingConditionSet::Values conditions; | 67 RecordingConditionSet::Values conditions; |
| 73 conditions.push_back(ScopedToLinkedPtr(ParseJson("{\"key\": 1}"))); | 68 conditions.push_back(ParseJson("{\"key\": 1}")); |
| 74 conditions.push_back(ScopedToLinkedPtr(ParseJson("{\"bad_key\": 2}"))); | 69 conditions.push_back(ParseJson("{\"bad_key\": 2}")); |
| 75 | 70 |
| 76 std::string error; | 71 std::string error; |
| 77 scoped_ptr<RecordingConditionSet> result = RecordingConditionSet::Create( | 72 scoped_ptr<RecordingConditionSet> result = RecordingConditionSet::Create( |
| 78 NULL, matcher.condition_factory(), conditions, &error); | 73 NULL, matcher.condition_factory(), conditions, &error); |
| 79 EXPECT_EQ("Found error key", error); | 74 EXPECT_EQ("Found error key", error); |
| 80 ASSERT_FALSE(result); | 75 ASSERT_FALSE(result); |
| 81 } | 76 } |
| 82 | 77 |
| 83 TEST(DeclarativeConditionTest, CreateConditionSet) { | 78 TEST(DeclarativeConditionTest, CreateConditionSet) { |
| 84 URLMatcher matcher; | 79 URLMatcher matcher; |
| 85 RecordingConditionSet::Values conditions; | 80 RecordingConditionSet::Values conditions; |
| 86 conditions.push_back(ScopedToLinkedPtr(ParseJson("{\"key\": 1}"))); | 81 conditions.push_back(ParseJson("{\"key\": 1}")); |
| 87 conditions.push_back(ScopedToLinkedPtr(ParseJson("[\"val1\", 2]"))); | 82 conditions.push_back(ParseJson("[\"val1\", 2]")); |
| 88 | 83 |
| 89 // Test insertion | 84 // Test insertion |
| 90 std::string error; | 85 std::string error; |
| 91 scoped_ptr<RecordingConditionSet> result = RecordingConditionSet::Create( | 86 scoped_ptr<RecordingConditionSet> result = RecordingConditionSet::Create( |
| 92 NULL, matcher.condition_factory(), conditions, &error); | 87 NULL, matcher.condition_factory(), conditions, &error); |
| 93 EXPECT_EQ("", error); | 88 EXPECT_EQ("", error); |
| 94 ASSERT_TRUE(result); | 89 ASSERT_TRUE(result); |
| 95 EXPECT_EQ(2u, result->conditions().size()); | 90 EXPECT_EQ(2u, result->conditions().size()); |
| 96 | 91 |
| 97 EXPECT_EQ(matcher.condition_factory(), result->conditions()[0]->factory); | 92 EXPECT_EQ(matcher.condition_factory(), result->conditions()[0]->factory); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 result->condition_set_id, | 145 result->condition_set_id, |
| 151 URLMatcherConditionSet::Conditions()); | 146 URLMatcherConditionSet::Conditions()); |
| 152 } | 147 } |
| 153 return result; | 148 return result; |
| 154 } | 149 } |
| 155 }; | 150 }; |
| 156 | 151 |
| 157 TEST(DeclarativeConditionTest, FulfillConditionSet) { | 152 TEST(DeclarativeConditionTest, FulfillConditionSet) { |
| 158 typedef DeclarativeConditionSet<FulfillableCondition> FulfillableConditionSet; | 153 typedef DeclarativeConditionSet<FulfillableCondition> FulfillableConditionSet; |
| 159 FulfillableConditionSet::Values conditions; | 154 FulfillableConditionSet::Values conditions; |
| 160 conditions.push_back(ScopedToLinkedPtr(ParseJson( | 155 conditions.push_back(ParseJson("{\"url_id\": 1, \"max\": 3}")); |
| 161 "{\"url_id\": 1, \"max\": 3}"))); | 156 conditions.push_back(ParseJson("{\"url_id\": 2, \"max\": 5}")); |
| 162 conditions.push_back(ScopedToLinkedPtr(ParseJson( | 157 conditions.push_back(ParseJson("{\"url_id\": 3, \"max\": 1}")); |
| 163 "{\"url_id\": 2, \"max\": 5}"))); | 158 conditions.push_back(ParseJson("{\"max\": -5}")); // No url. |
| 164 conditions.push_back(ScopedToLinkedPtr(ParseJson( | |
| 165 "{\"url_id\": 3, \"max\": 1}"))); | |
| 166 conditions.push_back(ScopedToLinkedPtr(ParseJson( | |
| 167 "{\"max\": -5}"))); // No url. | |
| 168 | 159 |
| 169 // Test insertion | 160 // Test insertion |
| 170 std::string error; | 161 std::string error; |
| 171 scoped_ptr<FulfillableConditionSet> result = | 162 scoped_ptr<FulfillableConditionSet> result = |
| 172 FulfillableConditionSet::Create(NULL, NULL, conditions, &error); | 163 FulfillableConditionSet::Create(NULL, NULL, conditions, &error); |
| 173 ASSERT_EQ("", error); | 164 ASSERT_EQ("", error); |
| 174 ASSERT_TRUE(result); | 165 ASSERT_TRUE(result); |
| 175 EXPECT_EQ(4u, result->conditions().size()); | 166 EXPECT_EQ(4u, result->conditions().size()); |
| 176 | 167 |
| 177 std::set<URLMatcherConditionSet::ID> url_matches; | 168 std::set<URLMatcherConditionSet::ID> url_matches; |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 friend class base::RefCounted<SummingAction>; | 248 friend class base::RefCounted<SummingAction>; |
| 258 virtual ~SummingAction() {} | 249 virtual ~SummingAction() {} |
| 259 | 250 |
| 260 int increment_; | 251 int increment_; |
| 261 int min_priority_; | 252 int min_priority_; |
| 262 }; | 253 }; |
| 263 typedef DeclarativeActionSet<SummingAction> SummingActionSet; | 254 typedef DeclarativeActionSet<SummingAction> SummingActionSet; |
| 264 | 255 |
| 265 TEST(DeclarativeActionTest, ErrorActionSet) { | 256 TEST(DeclarativeActionTest, ErrorActionSet) { |
| 266 SummingActionSet::Values actions; | 257 SummingActionSet::Values actions; |
| 267 actions.push_back(ScopedToLinkedPtr(ParseJson("{\"value\": 1}"))); | 258 actions.push_back(ParseJson("{\"value\": 1}")); |
| 268 actions.push_back(ScopedToLinkedPtr(ParseJson("{\"error\": \"the error\"}"))); | 259 actions.push_back(ParseJson("{\"error\": \"the error\"}")); |
| 269 | 260 |
| 270 std::string error; | 261 std::string error; |
| 271 bool bad = false; | 262 bool bad = false; |
| 272 scoped_ptr<SummingActionSet> result = | 263 scoped_ptr<SummingActionSet> result = |
| 273 SummingActionSet::Create(NULL, NULL, actions, &error, &bad); | 264 SummingActionSet::Create(NULL, NULL, actions, &error, &bad); |
| 274 EXPECT_EQ("the error", error); | 265 EXPECT_EQ("the error", error); |
| 275 EXPECT_FALSE(bad); | 266 EXPECT_FALSE(bad); |
| 276 EXPECT_FALSE(result); | 267 EXPECT_FALSE(result); |
| 277 | 268 |
| 278 actions.clear(); | 269 actions.clear(); |
| 279 actions.push_back(ScopedToLinkedPtr(ParseJson("{\"value\": 1}"))); | 270 actions.push_back(ParseJson("{\"value\": 1}")); |
| 280 actions.push_back(ScopedToLinkedPtr(ParseJson("{\"bad\": 3}"))); | 271 actions.push_back(ParseJson("{\"bad\": 3}")); |
| 281 result = SummingActionSet::Create(NULL, NULL, actions, &error, &bad); | 272 result = SummingActionSet::Create(NULL, NULL, actions, &error, &bad); |
| 282 EXPECT_EQ("", error); | 273 EXPECT_EQ("", error); |
| 283 EXPECT_TRUE(bad); | 274 EXPECT_TRUE(bad); |
| 284 EXPECT_FALSE(result); | 275 EXPECT_FALSE(result); |
| 285 } | 276 } |
| 286 | 277 |
| 287 TEST(DeclarativeActionTest, ApplyActionSet) { | 278 TEST(DeclarativeActionTest, ApplyActionSet) { |
| 288 SummingActionSet::Values actions; | 279 SummingActionSet::Values actions; |
| 289 actions.push_back(ScopedToLinkedPtr(ParseJson( | 280 actions.push_back( |
| 290 "{\"value\": 1," | 281 ParseJson("{\"value\": 1," |
| 291 " \"priority\": 5}"))); | 282 " \"priority\": 5}")); |
| 292 actions.push_back(ScopedToLinkedPtr(ParseJson("{\"value\": 2}"))); | 283 actions.push_back(ParseJson("{\"value\": 2}")); |
| 293 | 284 |
| 294 // Test insertion | 285 // Test insertion |
| 295 std::string error; | 286 std::string error; |
| 296 bool bad = false; | 287 bool bad = false; |
| 297 scoped_ptr<SummingActionSet> result = | 288 scoped_ptr<SummingActionSet> result = |
| 298 SummingActionSet::Create(NULL, NULL, actions, &error, &bad); | 289 SummingActionSet::Create(NULL, NULL, actions, &error, &bad); |
| 299 EXPECT_EQ("", error); | 290 EXPECT_EQ("", error); |
| 300 EXPECT_FALSE(bad); | 291 EXPECT_FALSE(bad); |
| 301 ASSERT_TRUE(result); | 292 ASSERT_TRUE(result); |
| 302 EXPECT_EQ(2u, result->actions().size()); | 293 EXPECT_EQ(2u, result->actions().size()); |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 433 extension.get(), | 424 extension.get(), |
| 434 base::Time(), | 425 base::Time(), |
| 435 json_rule, | 426 json_rule, |
| 436 base::Bind(AtLeastOneCondition), | 427 base::Bind(AtLeastOneCondition), |
| 437 &error); | 428 &error); |
| 438 EXPECT_FALSE(rule); | 429 EXPECT_FALSE(rule); |
| 439 EXPECT_EQ("No conditions", error); | 430 EXPECT_EQ("No conditions", error); |
| 440 } | 431 } |
| 441 | 432 |
| 442 } // namespace extensions | 433 } // namespace extensions |
| OLD | NEW |