| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/common/event_filter.h" | 5 #include "extensions/common/event_filter.h" |
| 6 | 6 |
| 7 #include <memory> |
| 7 #include <string> | 8 #include <string> |
| 8 #include <utility> | 9 #include <utility> |
| 9 | 10 |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "extensions/common/event_filtering_info.h" | 12 #include "extensions/common/event_filtering_info.h" |
| 13 #include "extensions/common/event_matcher.h" | 13 #include "extensions/common/event_matcher.h" |
| 14 #include "ipc/ipc_message.h" | 14 #include "ipc/ipc_message.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 16 |
| 17 using base::DictionaryValue; | 17 using base::DictionaryValue; |
| 18 using base::ListValue; | 18 using base::ListValue; |
| 19 using base::Value; | 19 using base::Value; |
| 20 | 20 |
| 21 namespace extensions { | 21 namespace extensions { |
| 22 | 22 |
| 23 class EventFilterUnittest : public testing::Test { | 23 class EventFilterUnittest : public testing::Test { |
| 24 public: | 24 public: |
| 25 EventFilterUnittest() { | 25 EventFilterUnittest() { |
| 26 google_event_.SetURL(GURL("http://google.com")); | 26 google_event_.SetURL(GURL("http://google.com")); |
| 27 yahoo_event_.SetURL(GURL("http://yahoo.com")); | 27 yahoo_event_.SetURL(GURL("http://yahoo.com")); |
| 28 random_url_event_.SetURL(GURL("http://www.something-else.com")); | 28 random_url_event_.SetURL(GURL("http://www.something-else.com")); |
| 29 empty_url_event_.SetURL(GURL()); | 29 empty_url_event_.SetURL(GURL()); |
| 30 } | 30 } |
| 31 | 31 |
| 32 protected: | 32 protected: |
| 33 scoped_ptr<base::Value> HostSuffixDict(const std::string& host_suffix) { | 33 std::unique_ptr<base::Value> HostSuffixDict(const std::string& host_suffix) { |
| 34 scoped_ptr<base::DictionaryValue> dict(new DictionaryValue()); | 34 std::unique_ptr<base::DictionaryValue> dict(new DictionaryValue()); |
| 35 dict->Set("hostSuffix", new base::StringValue(host_suffix)); | 35 dict->Set("hostSuffix", new base::StringValue(host_suffix)); |
| 36 return scoped_ptr<base::Value>(dict.release()); | 36 return std::unique_ptr<base::Value>(dict.release()); |
| 37 } | 37 } |
| 38 | 38 |
| 39 scoped_ptr<base::ListValue> ValueAsList(scoped_ptr<base::Value> value) { | 39 std::unique_ptr<base::ListValue> ValueAsList( |
| 40 scoped_ptr<base::ListValue> result(new base::ListValue()); | 40 std::unique_ptr<base::Value> value) { |
| 41 std::unique_ptr<base::ListValue> result(new base::ListValue()); |
| 41 result->Append(value.release()); | 42 result->Append(value.release()); |
| 42 return result; | 43 return result; |
| 43 } | 44 } |
| 44 | 45 |
| 45 scoped_ptr<EventMatcher> AllURLs() { | 46 std::unique_ptr<EventMatcher> AllURLs() { |
| 46 return scoped_ptr<EventMatcher>(new EventMatcher( | 47 return std::unique_ptr<EventMatcher>( |
| 47 scoped_ptr<DictionaryValue>(new DictionaryValue), MSG_ROUTING_NONE)); | 48 new EventMatcher(std::unique_ptr<DictionaryValue>(new DictionaryValue), |
| 49 MSG_ROUTING_NONE)); |
| 48 } | 50 } |
| 49 | 51 |
| 50 scoped_ptr<EventMatcher> HostSuffixMatcher(const std::string& host_suffix) { | 52 std::unique_ptr<EventMatcher> HostSuffixMatcher( |
| 53 const std::string& host_suffix) { |
| 51 return MatcherFromURLFilterList(ValueAsList(HostSuffixDict(host_suffix))); | 54 return MatcherFromURLFilterList(ValueAsList(HostSuffixDict(host_suffix))); |
| 52 } | 55 } |
| 53 | 56 |
| 54 scoped_ptr<EventMatcher> MatcherFromURLFilterList( | 57 std::unique_ptr<EventMatcher> MatcherFromURLFilterList( |
| 55 scoped_ptr<ListValue> url_filter_list) { | 58 std::unique_ptr<ListValue> url_filter_list) { |
| 56 scoped_ptr<DictionaryValue> filter_dict(new DictionaryValue); | 59 std::unique_ptr<DictionaryValue> filter_dict(new DictionaryValue); |
| 57 filter_dict->Set("url", url_filter_list.release()); | 60 filter_dict->Set("url", url_filter_list.release()); |
| 58 return scoped_ptr<EventMatcher>( | 61 return std::unique_ptr<EventMatcher>( |
| 59 new EventMatcher(std::move(filter_dict), MSG_ROUTING_NONE)); | 62 new EventMatcher(std::move(filter_dict), MSG_ROUTING_NONE)); |
| 60 } | 63 } |
| 61 | 64 |
| 62 EventFilter event_filter_; | 65 EventFilter event_filter_; |
| 63 EventFilteringInfo empty_event_; | 66 EventFilteringInfo empty_event_; |
| 64 EventFilteringInfo google_event_; | 67 EventFilteringInfo google_event_; |
| 65 EventFilteringInfo yahoo_event_; | 68 EventFilteringInfo yahoo_event_; |
| 66 EventFilteringInfo random_url_event_; | 69 EventFilteringInfo random_url_event_; |
| 67 EventFilteringInfo empty_url_event_; | 70 EventFilteringInfo empty_url_event_; |
| 68 }; | 71 }; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 info.SetURL(GURL("http://www.google.com")); | 131 info.SetURL(GURL("http://www.google.com")); |
| 129 int id = event_filter_.AddEventMatcher("event1", | 132 int id = event_filter_.AddEventMatcher("event1", |
| 130 HostSuffixMatcher("google.com")); | 133 HostSuffixMatcher("google.com")); |
| 131 std::set<int> matches = event_filter_.MatchEvent( | 134 std::set<int> matches = event_filter_.MatchEvent( |
| 132 "event1", info, MSG_ROUTING_NONE); | 135 "event1", info, MSG_ROUTING_NONE); |
| 133 ASSERT_EQ(1u, matches.size()); | 136 ASSERT_EQ(1u, matches.size()); |
| 134 ASSERT_EQ(1u, matches.count(id)); | 137 ASSERT_EQ(1u, matches.count(id)); |
| 135 } | 138 } |
| 136 | 139 |
| 137 TEST_F(EventFilterUnittest, TestMultipleURLFiltersMatchOnAny) { | 140 TEST_F(EventFilterUnittest, TestMultipleURLFiltersMatchOnAny) { |
| 138 scoped_ptr<base::ListValue> filters(new base::ListValue()); | 141 std::unique_ptr<base::ListValue> filters(new base::ListValue()); |
| 139 filters->Append(HostSuffixDict("google.com").release()); | 142 filters->Append(HostSuffixDict("google.com").release()); |
| 140 filters->Append(HostSuffixDict("yahoo.com").release()); | 143 filters->Append(HostSuffixDict("yahoo.com").release()); |
| 141 | 144 |
| 142 scoped_ptr<EventMatcher> matcher( | 145 std::unique_ptr<EventMatcher> matcher( |
| 143 MatcherFromURLFilterList(std::move(filters))); | 146 MatcherFromURLFilterList(std::move(filters))); |
| 144 int id = event_filter_.AddEventMatcher("event1", std::move(matcher)); | 147 int id = event_filter_.AddEventMatcher("event1", std::move(matcher)); |
| 145 | 148 |
| 146 { | 149 { |
| 147 std::set<int> matches = event_filter_.MatchEvent("event1", | 150 std::set<int> matches = event_filter_.MatchEvent("event1", |
| 148 google_event_, MSG_ROUTING_NONE); | 151 google_event_, MSG_ROUTING_NONE); |
| 149 ASSERT_EQ(1u, matches.size()); | 152 ASSERT_EQ(1u, matches.size()); |
| 150 ASSERT_EQ(1u, matches.count(id)); | 153 ASSERT_EQ(1u, matches.count(id)); |
| 151 } | 154 } |
| 152 { | 155 { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 int id1 = event_filter_.AddEventMatcher("event1", AllURLs()); | 206 int id1 = event_filter_.AddEventMatcher("event1", AllURLs()); |
| 204 int id2 = event_filter_.AddEventMatcher("event1", AllURLs()); | 207 int id2 = event_filter_.AddEventMatcher("event1", AllURLs()); |
| 205 int id3 = event_filter_.AddEventMatcher("event2", AllURLs()); | 208 int id3 = event_filter_.AddEventMatcher("event2", AllURLs()); |
| 206 | 209 |
| 207 ASSERT_EQ("event1", event_filter_.RemoveEventMatcher(id1)); | 210 ASSERT_EQ("event1", event_filter_.RemoveEventMatcher(id1)); |
| 208 ASSERT_EQ("event1", event_filter_.RemoveEventMatcher(id2)); | 211 ASSERT_EQ("event1", event_filter_.RemoveEventMatcher(id2)); |
| 209 ASSERT_EQ("event2", event_filter_.RemoveEventMatcher(id3)); | 212 ASSERT_EQ("event2", event_filter_.RemoveEventMatcher(id3)); |
| 210 } | 213 } |
| 211 | 214 |
| 212 TEST_F(EventFilterUnittest, InvalidURLFilterCantBeAdded) { | 215 TEST_F(EventFilterUnittest, InvalidURLFilterCantBeAdded) { |
| 213 scoped_ptr<base::ListValue> filter_list(new base::ListValue()); | 216 std::unique_ptr<base::ListValue> filter_list(new base::ListValue()); |
| 214 filter_list->Append(new base::ListValue()); // Should be a dict. | 217 filter_list->Append(new base::ListValue()); // Should be a dict. |
| 215 scoped_ptr<EventMatcher> matcher( | 218 std::unique_ptr<EventMatcher> matcher( |
| 216 MatcherFromURLFilterList(std::move(filter_list))); | 219 MatcherFromURLFilterList(std::move(filter_list))); |
| 217 int id1 = event_filter_.AddEventMatcher("event1", std::move(matcher)); | 220 int id1 = event_filter_.AddEventMatcher("event1", std::move(matcher)); |
| 218 EXPECT_TRUE(event_filter_.IsURLMatcherEmpty()); | 221 EXPECT_TRUE(event_filter_.IsURLMatcherEmpty()); |
| 219 ASSERT_EQ(-1, id1); | 222 ASSERT_EQ(-1, id1); |
| 220 } | 223 } |
| 221 | 224 |
| 222 TEST_F(EventFilterUnittest, EmptyListOfURLFiltersMatchesAllURLs) { | 225 TEST_F(EventFilterUnittest, EmptyListOfURLFiltersMatchesAllURLs) { |
| 223 scoped_ptr<base::ListValue> filter_list(new base::ListValue()); | 226 std::unique_ptr<base::ListValue> filter_list(new base::ListValue()); |
| 224 scoped_ptr<EventMatcher> matcher(MatcherFromURLFilterList( | 227 std::unique_ptr<EventMatcher> matcher( |
| 225 scoped_ptr<ListValue>(new ListValue))); | 228 MatcherFromURLFilterList(std::unique_ptr<ListValue>(new ListValue))); |
| 226 int id = event_filter_.AddEventMatcher("event1", std::move(matcher)); | 229 int id = event_filter_.AddEventMatcher("event1", std::move(matcher)); |
| 227 std::set<int> matches = event_filter_.MatchEvent("event1", | 230 std::set<int> matches = event_filter_.MatchEvent("event1", |
| 228 google_event_, MSG_ROUTING_NONE); | 231 google_event_, MSG_ROUTING_NONE); |
| 229 ASSERT_EQ(1u, matches.size()); | 232 ASSERT_EQ(1u, matches.size()); |
| 230 ASSERT_EQ(1u, matches.count(id)); | 233 ASSERT_EQ(1u, matches.count(id)); |
| 231 } | 234 } |
| 232 | 235 |
| 233 TEST_F(EventFilterUnittest, | 236 TEST_F(EventFilterUnittest, |
| 234 InternalURLMatcherShouldBeEmptyWhenThereAreNoEventMatchers) { | 237 InternalURLMatcherShouldBeEmptyWhenThereAreNoEventMatchers) { |
| 235 ASSERT_TRUE(event_filter_.IsURLMatcherEmpty()); | 238 ASSERT_TRUE(event_filter_.IsURLMatcherEmpty()); |
| 236 int id = event_filter_.AddEventMatcher("event1", | 239 int id = event_filter_.AddEventMatcher("event1", |
| 237 HostSuffixMatcher("google.com")); | 240 HostSuffixMatcher("google.com")); |
| 238 ASSERT_FALSE(event_filter_.IsURLMatcherEmpty()); | 241 ASSERT_FALSE(event_filter_.IsURLMatcherEmpty()); |
| 239 event_filter_.RemoveEventMatcher(id); | 242 event_filter_.RemoveEventMatcher(id); |
| 240 ASSERT_TRUE(event_filter_.IsURLMatcherEmpty()); | 243 ASSERT_TRUE(event_filter_.IsURLMatcherEmpty()); |
| 241 } | 244 } |
| 242 | 245 |
| 243 TEST_F(EventFilterUnittest, EmptyURLsShouldBeMatchedByEmptyURLFilters) { | 246 TEST_F(EventFilterUnittest, EmptyURLsShouldBeMatchedByEmptyURLFilters) { |
| 244 int id = event_filter_.AddEventMatcher("event1", AllURLs()); | 247 int id = event_filter_.AddEventMatcher("event1", AllURLs()); |
| 245 std::set<int> matches = event_filter_.MatchEvent( | 248 std::set<int> matches = event_filter_.MatchEvent( |
| 246 "event1", empty_url_event_, MSG_ROUTING_NONE); | 249 "event1", empty_url_event_, MSG_ROUTING_NONE); |
| 247 ASSERT_EQ(1u, matches.size()); | 250 ASSERT_EQ(1u, matches.size()); |
| 248 ASSERT_EQ(1u, matches.count(id)); | 251 ASSERT_EQ(1u, matches.count(id)); |
| 249 } | 252 } |
| 250 | 253 |
| 251 TEST_F(EventFilterUnittest, | 254 TEST_F(EventFilterUnittest, |
| 252 EmptyURLsShouldBeMatchedByEmptyURLFiltersWithAnEmptyItem) { | 255 EmptyURLsShouldBeMatchedByEmptyURLFiltersWithAnEmptyItem) { |
| 253 scoped_ptr<EventMatcher> matcher(MatcherFromURLFilterList(ValueAsList( | 256 std::unique_ptr<EventMatcher> matcher(MatcherFromURLFilterList( |
| 254 scoped_ptr<Value>(new DictionaryValue())))); | 257 ValueAsList(std::unique_ptr<Value>(new DictionaryValue())))); |
| 255 int id = event_filter_.AddEventMatcher("event1", std::move(matcher)); | 258 int id = event_filter_.AddEventMatcher("event1", std::move(matcher)); |
| 256 std::set<int> matches = event_filter_.MatchEvent( | 259 std::set<int> matches = event_filter_.MatchEvent( |
| 257 "event1", empty_url_event_, MSG_ROUTING_NONE); | 260 "event1", empty_url_event_, MSG_ROUTING_NONE); |
| 258 ASSERT_EQ(1u, matches.size()); | 261 ASSERT_EQ(1u, matches.size()); |
| 259 ASSERT_EQ(1u, matches.count(id)); | 262 ASSERT_EQ(1u, matches.count(id)); |
| 260 } | 263 } |
| 261 | 264 |
| 262 } // namespace extensions | 265 } // namespace extensions |
| OLD | NEW |