Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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/event_router.h" | 5 #include "extensions/browser/event_router.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 11 #include "base/test/histogram_tester.h" | |
| 11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/values.h" | 14 #include "base/values.h" |
| 14 #include "content/public/browser/notification_service.h" | 15 #include "content/public/browser/notification_service.h" |
| 16 #include "content/public/test/test_browser_thread_bundle.h" | |
| 15 #include "extensions/browser/event_listener_map.h" | 17 #include "extensions/browser/event_listener_map.h" |
| 16 #include "extensions/browser/extensions_test.h" | 18 #include "extensions/browser/extensions_test.h" |
| 19 #include "extensions/common/extension_builder.h" | |
| 20 #include "extensions/common/test_util.h" | |
| 21 #include "extensions/common/value_builder.h" | |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 23 |
| 19 namespace extensions { | 24 namespace extensions { |
| 20 | 25 |
| 21 namespace { | 26 namespace { |
| 22 | 27 |
| 23 // A simple mock to keep track of listener additions and removals. | 28 // A simple mock to keep track of listener additions and removals. |
| 24 class MockEventRouterObserver : public EventRouter::Observer { | 29 class MockEventRouterObserver : public EventRouter::Observer { |
| 25 public: | 30 public: |
| 26 MockEventRouterObserver() | 31 MockEventRouterObserver() |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 74 | 79 |
| 75 scoped_ptr<EventListener> CreateEventListenerForURL( | 80 scoped_ptr<EventListener> CreateEventListenerForURL( |
| 76 const GURL& listener_url, | 81 const GURL& listener_url, |
| 77 const std::string& event_name, | 82 const std::string& event_name, |
| 78 content::RenderProcessHost* process, | 83 content::RenderProcessHost* process, |
| 79 base::DictionaryValue* filter) { | 84 base::DictionaryValue* filter) { |
| 80 return EventListener::ForURL( | 85 return EventListener::ForURL( |
| 81 event_name, listener_url, process, make_scoped_ptr(filter)); | 86 event_name, listener_url, process, make_scoped_ptr(filter)); |
| 82 } | 87 } |
| 83 | 88 |
| 89 scoped_ptr<base::DictionaryValue> CreateBackgroundManifest(bool persistent) { | |
| 90 scoped_ptr<base::DictionaryValue> manifest = | |
| 91 make_scoped_ptr(new base::DictionaryValue()); | |
| 92 manifest->SetString("background.page", "background.html"); | |
| 93 manifest->SetBoolean("background.persistent", persistent); | |
| 94 return manifest; | |
|
imcheng
2016/01/20 18:28:21
It should compile if you changed this to:
std::mo
mark a. foltz
2016/01/20 21:58:07
Thanks! That was the fix. It isn't clear to me y
| |
| 95 } | |
| 96 | |
| 84 } // namespace | 97 } // namespace |
| 85 | 98 |
| 86 class EventRouterTest : public ExtensionsTest { | 99 class EventRouterTest : public ExtensionsTest { |
| 87 public: | 100 public: |
| 88 EventRouterTest() | 101 EventRouterTest() |
| 89 : notification_service_(content::NotificationService::Create()) {} | 102 : notification_service_(content::NotificationService::Create()) {} |
| 90 | 103 |
| 91 protected: | 104 protected: |
| 92 // Tests adding and removing observers from EventRouter. | 105 // Tests adding and removing observers from EventRouter. |
| 93 void RunEventRouterObserverTest(const EventListenerConstructor& constructor); | 106 void RunEventRouterObserverTest(const EventListenerConstructor& constructor); |
| 94 | 107 |
| 108 // Tests that the correct counts are recorded for the Extensions.Events | |
| 109 // histograms. | |
| 110 void ExpectHistogramCounts(int dispatch_count, int component_count, | |
| 111 int persistent_count, int suspended_count, | |
| 112 int component_suspended_count, int running_count) { | |
| 113 if (dispatch_count) { | |
| 114 histogram_tester_.ExpectBucketCount("Extensions.Events.Dispatch", | |
| 115 events::HistogramValue::FOR_TEST, | |
| 116 dispatch_count); | |
| 117 } | |
| 118 if (component_count) { | |
| 119 histogram_tester_.ExpectBucketCount( | |
| 120 "Extensions.Events.DispatchToComponent", | |
| 121 events::HistogramValue::FOR_TEST, | |
| 122 component_count); | |
| 123 } | |
| 124 if (persistent_count) { | |
| 125 histogram_tester_.ExpectBucketCount( | |
| 126 "Extensions.Events.DispatchWithPersistentBackgroundPage", | |
| 127 events::HistogramValue::FOR_TEST, | |
| 128 persistent_count); | |
| 129 } | |
| 130 if (suspended_count) { | |
| 131 histogram_tester_.ExpectBucketCount( | |
| 132 "Extensions.Events.DispatchWithSuspendedEventPage", | |
| 133 events::HistogramValue::FOR_TEST, | |
| 134 suspended_count); | |
| 135 } | |
| 136 if (component_suspended_count) { | |
| 137 histogram_tester_.ExpectBucketCount( | |
| 138 "Extensions.Events.DispatchToComponentWithSuspendedEventPage", | |
| 139 events::HistogramValue::FOR_TEST, | |
| 140 component_suspended_count); | |
| 141 } | |
| 142 if (running_count) { | |
| 143 histogram_tester_.ExpectBucketCount( | |
| 144 "Extensions.Events.DispatchWithRunningEventPage", | |
| 145 events::HistogramValue::FOR_TEST, | |
| 146 running_count); | |
| 147 } | |
| 148 } | |
| 149 | |
| 95 private: | 150 private: |
| 96 scoped_ptr<content::NotificationService> notification_service_; | 151 scoped_ptr<content::NotificationService> notification_service_; |
| 152 content::TestBrowserThreadBundle thread_bundle_; | |
| 153 base::HistogramTester histogram_tester_; | |
| 97 | 154 |
| 98 DISALLOW_COPY_AND_ASSIGN(EventRouterTest); | 155 DISALLOW_COPY_AND_ASSIGN(EventRouterTest); |
| 99 }; | 156 }; |
| 100 | 157 |
| 101 TEST_F(EventRouterTest, GetBaseEventName) { | 158 TEST_F(EventRouterTest, GetBaseEventName) { |
| 102 // Normal event names are passed through unchanged. | 159 // Normal event names are passed through unchanged. |
| 103 EXPECT_EQ("foo.onBar", EventRouter::GetBaseEventName("foo.onBar")); | 160 EXPECT_EQ("foo.onBar", EventRouter::GetBaseEventName("foo.onBar")); |
| 104 | 161 |
| 105 // Sub-events are converted to the part before the slash. | 162 // Sub-events are converted to the part before the slash. |
| 106 EXPECT_EQ("foo.onBar", EventRouter::GetBaseEventName("foo.onBar/123")); | 163 EXPECT_EQ("foo.onBar", EventRouter::GetBaseEventName("foo.onBar/123")); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 164 TEST_F(EventRouterTest, EventRouterObserverForExtensions) { | 221 TEST_F(EventRouterTest, EventRouterObserverForExtensions) { |
| 165 RunEventRouterObserverTest( | 222 RunEventRouterObserverTest( |
| 166 base::Bind(&CreateEventListenerForExtension, "extension_id")); | 223 base::Bind(&CreateEventListenerForExtension, "extension_id")); |
| 167 } | 224 } |
| 168 | 225 |
| 169 TEST_F(EventRouterTest, EventRouterObserverForURLs) { | 226 TEST_F(EventRouterTest, EventRouterObserverForURLs) { |
| 170 RunEventRouterObserverTest( | 227 RunEventRouterObserverTest( |
| 171 base::Bind(&CreateEventListenerForURL, GURL("http://google.com/path"))); | 228 base::Bind(&CreateEventListenerForURL, GURL("http://google.com/path"))); |
| 172 } | 229 } |
| 173 | 230 |
| 231 TEST_F(EventRouterTest, TestReportEvent) { | |
| 232 EventRouter router(browser_context(), NULL); | |
| 233 // TODO: Create component extensions w/ and w/out lazy background page | |
| 234 // Use ExtensionBuilder. | |
| 235 scoped_refptr<Extension> normal = test_util::CreateEmptyExtension("id1"); | |
| 236 router.ReportEvent(events::HistogramValue::FOR_TEST, | |
| 237 normal.get(), | |
| 238 false /** did_enqueue */); | |
| 239 ExpectHistogramCounts(1 /** Dispatch */, | |
| 240 0 /** DispatchToComponent */, | |
| 241 0 /** DispatchWithPersistentBackgroundPage */, | |
| 242 0 /** DispatchWithSuspendedEventPage */, | |
| 243 0 /** DispatchToComponentWithSuspendedEventPage */, | |
| 244 0 /** DispatchWithRunningEventPage */); | |
| 245 | |
| 246 ExtensionBuilder component_builder; | |
| 247 scoped_refptr<Extension> component = component_builder. | |
| 248 SetLocation(Manifest::Location::COMPONENT).Build(); | |
| 249 router.ReportEvent(events::HistogramValue::FOR_TEST, | |
| 250 component.get(), | |
| 251 false /** did_enqueue */); | |
| 252 ExpectHistogramCounts(2, 1, 0, 0, 0, 0); | |
| 253 | |
| 254 ExtensionBuilder persistent_builder; | |
| 255 scoped_ptr<base::DictionaryValue> persistent_manifest = | |
| 256 CreateBackgroundManifest(true); | |
| 257 | |
| 258 scoped_refptr<Extension> persistent = persistent_builder. | |
| 259 SetManifest(persistent_manifest).Build(); | |
| 260 router.ReportEvent(events::HistogramValue::FOR_TEST, | |
| 261 persistent.get(), | |
| 262 false /** did_enqueue */); | |
| 263 ExpectHistogramCounts(3, 1, 1, 0, 0, 0); | |
| 264 | |
| 265 // ExtensionBuilder event_builder; | |
| 266 // scoped_refptr<Extension> event = event_builder. | |
| 267 // SetManifest(CreateBackgroundManifest(false)).Build(); | |
| 268 // router.ReportEvent(events::HistogramValue::FOR_TEST, | |
| 269 // event.get(), | |
| 270 // false /** did_enqueue */); | |
| 271 // router.ReportEvent(events::HistogramValue::FOR_TEST, | |
| 272 // event.get(), | |
| 273 // true /** did_enqueue */); | |
| 274 // ExpectHistogramCounts(4, 1, 1, 1, 0, 1); | |
| 275 | |
| 276 // ExtensionBuilder component_event_builder; | |
| 277 // scoped_refptr<Extension> component_event = component_event_builder. | |
| 278 // SetLocation(Manifest::Location::COMPONENT). | |
| 279 // SetManifest(CreateBackgroundManifest(false)).Build(); | |
| 280 // router.ReportEvent(events::HistogramValue::FOR_TEST, | |
| 281 // component_event.get(), | |
| 282 // false /** did_enqueue */); | |
| 283 // router.ReportEvent(events::HistogramValue::FOR_TEST, | |
| 284 // component_event.get(), | |
| 285 // true /** did_enqueue */); | |
| 286 // ExpectHistogramCounts(5, 2, 1, 2, 1, 2); | |
| 287 } | |
| 288 | |
| 174 } // namespace extensions | 289 } // namespace extensions |
| OLD | NEW |