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 #include <utility> |
8 | 9 |
9 #include "base/bind.h" | 10 #include "base/bind.h" |
10 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.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" |
| 14 #include "base/test/histogram_tester.h" |
13 #include "base/values.h" | 15 #include "base/values.h" |
14 #include "content/public/browser/notification_service.h" | 16 #include "content/public/browser/notification_service.h" |
| 17 #include "content/public/test/test_browser_thread_bundle.h" |
15 #include "extensions/browser/event_listener_map.h" | 18 #include "extensions/browser/event_listener_map.h" |
16 #include "extensions/browser/extensions_test.h" | 19 #include "extensions/browser/extensions_test.h" |
| 20 #include "extensions/common/extension_builder.h" |
| 21 #include "extensions/common/test_util.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 // Creates an extension. If |component| is true, it is created as a component |
| 90 // extension. If |persistent| is true, it is created with a persistent |
| 91 // background page; otherwise it is created with an event page. |
| 92 scoped_refptr<Extension> CreateExtension(bool component, bool persistent) { |
| 93 ExtensionBuilder builder; |
| 94 scoped_ptr<base::DictionaryValue> manifest = |
| 95 make_scoped_ptr(new base::DictionaryValue()); |
| 96 manifest->SetString("name", "foo"); |
| 97 manifest->SetString("version", "1.0.0"); |
| 98 manifest->SetInteger("manifest_version", 2); |
| 99 manifest->SetString("background.page", "background.html"); |
| 100 manifest->SetBoolean("background.persistent", persistent); |
| 101 builder.SetManifest(std::move(manifest)); |
| 102 if (component) |
| 103 builder.SetLocation(Manifest::Location::COMPONENT); |
| 104 |
| 105 return builder.Build(); |
| 106 } |
| 107 |
84 } // namespace | 108 } // namespace |
85 | 109 |
86 class EventRouterTest : public ExtensionsTest { | 110 class EventRouterTest : public ExtensionsTest { |
87 public: | 111 public: |
88 EventRouterTest() | 112 EventRouterTest() |
89 : notification_service_(content::NotificationService::Create()) {} | 113 : notification_service_(content::NotificationService::Create()) {} |
90 | 114 |
91 protected: | 115 protected: |
92 // Tests adding and removing observers from EventRouter. | 116 // Tests adding and removing observers from EventRouter. |
93 void RunEventRouterObserverTest(const EventListenerConstructor& constructor); | 117 void RunEventRouterObserverTest(const EventListenerConstructor& constructor); |
94 | 118 |
| 119 // Tests that the correct counts are recorded for the Extensions.Events |
| 120 // histograms. |
| 121 void ExpectHistogramCounts(int dispatch_count, |
| 122 int component_count, |
| 123 int persistent_count, |
| 124 int suspended_count, |
| 125 int component_suspended_count, |
| 126 int running_count) { |
| 127 if (dispatch_count) { |
| 128 histogram_tester_.ExpectBucketCount("Extensions.Events.Dispatch", |
| 129 events::HistogramValue::FOR_TEST, |
| 130 dispatch_count); |
| 131 } |
| 132 if (component_count) { |
| 133 histogram_tester_.ExpectBucketCount( |
| 134 "Extensions.Events.DispatchToComponent", |
| 135 events::HistogramValue::FOR_TEST, component_count); |
| 136 } |
| 137 if (persistent_count) { |
| 138 histogram_tester_.ExpectBucketCount( |
| 139 "Extensions.Events.DispatchWithPersistentBackgroundPage", |
| 140 events::HistogramValue::FOR_TEST, persistent_count); |
| 141 } |
| 142 if (suspended_count) { |
| 143 histogram_tester_.ExpectBucketCount( |
| 144 "Extensions.Events.DispatchWithSuspendedEventPage", |
| 145 events::HistogramValue::FOR_TEST, suspended_count); |
| 146 } |
| 147 if (component_suspended_count) { |
| 148 histogram_tester_.ExpectBucketCount( |
| 149 "Extensions.Events.DispatchToComponentWithSuspendedEventPage", |
| 150 events::HistogramValue::FOR_TEST, component_suspended_count); |
| 151 } |
| 152 if (running_count) { |
| 153 histogram_tester_.ExpectBucketCount( |
| 154 "Extensions.Events.DispatchWithRunningEventPage", |
| 155 events::HistogramValue::FOR_TEST, running_count); |
| 156 } |
| 157 } |
| 158 |
95 private: | 159 private: |
96 scoped_ptr<content::NotificationService> notification_service_; | 160 scoped_ptr<content::NotificationService> notification_service_; |
| 161 content::TestBrowserThreadBundle thread_bundle_; |
| 162 base::HistogramTester histogram_tester_; |
97 | 163 |
98 DISALLOW_COPY_AND_ASSIGN(EventRouterTest); | 164 DISALLOW_COPY_AND_ASSIGN(EventRouterTest); |
99 }; | 165 }; |
100 | 166 |
101 TEST_F(EventRouterTest, GetBaseEventName) { | 167 TEST_F(EventRouterTest, GetBaseEventName) { |
102 // Normal event names are passed through unchanged. | 168 // Normal event names are passed through unchanged. |
103 EXPECT_EQ("foo.onBar", EventRouter::GetBaseEventName("foo.onBar")); | 169 EXPECT_EQ("foo.onBar", EventRouter::GetBaseEventName("foo.onBar")); |
104 | 170 |
105 // Sub-events are converted to the part before the slash. | 171 // Sub-events are converted to the part before the slash. |
106 EXPECT_EQ("foo.onBar", EventRouter::GetBaseEventName("foo.onBar/123")); | 172 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) { | 230 TEST_F(EventRouterTest, EventRouterObserverForExtensions) { |
165 RunEventRouterObserverTest( | 231 RunEventRouterObserverTest( |
166 base::Bind(&CreateEventListenerForExtension, "extension_id")); | 232 base::Bind(&CreateEventListenerForExtension, "extension_id")); |
167 } | 233 } |
168 | 234 |
169 TEST_F(EventRouterTest, EventRouterObserverForURLs) { | 235 TEST_F(EventRouterTest, EventRouterObserverForURLs) { |
170 RunEventRouterObserverTest( | 236 RunEventRouterObserverTest( |
171 base::Bind(&CreateEventListenerForURL, GURL("http://google.com/path"))); | 237 base::Bind(&CreateEventListenerForURL, GURL("http://google.com/path"))); |
172 } | 238 } |
173 | 239 |
| 240 TEST_F(EventRouterTest, TestReportEvent) { |
| 241 EventRouter router(browser_context(), NULL); |
| 242 scoped_refptr<Extension> normal = test_util::CreateEmptyExtension("id1"); |
| 243 router.ReportEvent(events::HistogramValue::FOR_TEST, normal.get(), |
| 244 false /** did_enqueue */); |
| 245 ExpectHistogramCounts(1 /** Dispatch */, 0 /** DispatchToComponent */, |
| 246 0 /** DispatchWithPersistentBackgroundPage */, |
| 247 0 /** DispatchWithSuspendedEventPage */, |
| 248 0 /** DispatchToComponentWithSuspendedEventPage */, |
| 249 0 /** DispatchWithRunningEventPage */); |
| 250 |
| 251 scoped_refptr<Extension> component = |
| 252 CreateExtension(true /** component */, true /** persistent */); |
| 253 router.ReportEvent(events::HistogramValue::FOR_TEST, component.get(), |
| 254 false /** did_enqueue */); |
| 255 ExpectHistogramCounts(2, 1, 1, 0, 0, 0); |
| 256 |
| 257 scoped_refptr<Extension> persistent = CreateExtension(false, true); |
| 258 router.ReportEvent(events::HistogramValue::FOR_TEST, persistent.get(), |
| 259 false /** did_enqueue */); |
| 260 ExpectHistogramCounts(3, 1, 2, 0, 0, 0); |
| 261 |
| 262 scoped_refptr<Extension> event = CreateExtension(false, false); |
| 263 router.ReportEvent(events::HistogramValue::FOR_TEST, event.get(), |
| 264 false /** did_enqueue */); |
| 265 ExpectHistogramCounts(4, 1, 2, 0, 0, 0); |
| 266 router.ReportEvent(events::HistogramValue::FOR_TEST, event.get(), |
| 267 true /** did_enqueue */); |
| 268 ExpectHistogramCounts(5, 1, 2, 1, 0, 1); |
| 269 |
| 270 scoped_refptr<Extension> component_event = CreateExtension(true, false); |
| 271 router.ReportEvent(events::HistogramValue::FOR_TEST, component_event.get(), |
| 272 false /** did_enqueue */); |
| 273 ExpectHistogramCounts(6, 2, 2, 1, 0, 2); |
| 274 router.ReportEvent(events::HistogramValue::FOR_TEST, component_event.get(), |
| 275 true /** did_enqueue */); |
| 276 ExpectHistogramCounts(7, 3, 2, 2, 1, 2); |
| 277 } |
| 278 |
174 } // namespace extensions | 279 } // namespace extensions |
OLD | NEW |