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/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/test/histogram_tester.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" | |
17 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
18 | 22 |
19 namespace extensions { | 23 namespace extensions { |
20 | 24 |
21 namespace { | 25 namespace { |
22 | 26 |
23 // A simple mock to keep track of listener additions and removals. | 27 // A simple mock to keep track of listener additions and removals. |
24 class MockEventRouterObserver : public EventRouter::Observer { | 28 class MockEventRouterObserver : public EventRouter::Observer { |
25 public: | 29 public: |
26 MockEventRouterObserver() | 30 MockEventRouterObserver() |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
74 | 78 |
75 scoped_ptr<EventListener> CreateEventListenerForURL( | 79 scoped_ptr<EventListener> CreateEventListenerForURL( |
76 const GURL& listener_url, | 80 const GURL& listener_url, |
77 const std::string& event_name, | 81 const std::string& event_name, |
78 content::RenderProcessHost* process, | 82 content::RenderProcessHost* process, |
79 base::DictionaryValue* filter) { | 83 base::DictionaryValue* filter) { |
80 return EventListener::ForURL( | 84 return EventListener::ForURL( |
81 event_name, listener_url, process, make_scoped_ptr(filter)); | 85 event_name, listener_url, process, make_scoped_ptr(filter)); |
82 } | 86 } |
83 | 87 |
88 scoped_refptr<Extension> CreateExtension(bool component, bool persistent) { | |
89 scoped_ptr<base::DictionaryValue> manifest = | |
90 make_scoped_ptr(new base::DictionaryValue()); | |
91 manifest->SetString("name", "foo"); | |
92 manifest->SetString("version", "1.0.0"); | |
93 manifest->SetInteger("manifest_version", 2); | |
94 manifest->SetString("background.page", "background.html"); | |
95 manifest->SetBoolean("background.persistent", persistent); | |
96 | |
97 ExtensionBuilder builder; | |
98 builder.SetManifest(std::move(manifest)); | |
99 if (component) | |
100 builder.SetLocation(Manifest::Location::COMPONENT); | |
101 | |
102 return builder.Build(); | |
103 } | |
104 | |
84 } // namespace | 105 } // namespace |
85 | 106 |
86 class EventRouterTest : public ExtensionsTest { | 107 class EventRouterTest : public ExtensionsTest { |
87 public: | 108 public: |
88 EventRouterTest() | 109 EventRouterTest() |
89 : notification_service_(content::NotificationService::Create()) {} | 110 : notification_service_(content::NotificationService::Create()) {} |
90 | 111 |
91 protected: | 112 protected: |
92 // Tests adding and removing observers from EventRouter. | 113 // Tests adding and removing observers from EventRouter. |
93 void RunEventRouterObserverTest(const EventListenerConstructor& constructor); | 114 void RunEventRouterObserverTest(const EventListenerConstructor& constructor); |
94 | 115 |
116 // Tests that the correct counts are recorded for the Extensions.Events | |
117 // histograms. | |
118 void ExpectHistogramCounts(int dispatch_count, | |
119 int component_count, | |
120 int persistent_count, | |
121 int suspended_count, | |
122 int component_suspended_count, | |
123 int running_count) { | |
124 if (dispatch_count) { | |
125 histogram_tester_.ExpectBucketCount("Extensions.Events.Dispatch", | |
126 events::HistogramValue::FOR_TEST, | |
127 dispatch_count); | |
128 } | |
129 if (component_count) { | |
130 histogram_tester_.ExpectBucketCount( | |
131 "Extensions.Events.DispatchToComponent", | |
132 events::HistogramValue::FOR_TEST, component_count); | |
133 } | |
134 if (persistent_count) { | |
135 histogram_tester_.ExpectBucketCount( | |
136 "Extensions.Events.DispatchWithPersistentBackgroundPage", | |
137 events::HistogramValue::FOR_TEST, persistent_count); | |
138 } | |
139 if (suspended_count) { | |
140 histogram_tester_.ExpectBucketCount( | |
141 "Extensions.Events.DispatchWithSuspendedEventPage", | |
142 events::HistogramValue::FOR_TEST, suspended_count); | |
143 } | |
144 if (component_suspended_count) { | |
145 histogram_tester_.ExpectBucketCount( | |
146 "Extensions.Events.DispatchToComponentWithSuspendedEventPage", | |
147 events::HistogramValue::FOR_TEST, component_suspended_count); | |
148 } | |
149 if (running_count) { | |
150 histogram_tester_.ExpectBucketCount( | |
151 "Extensions.Events.DispatchWithRunningEventPage", | |
152 events::HistogramValue::FOR_TEST, running_count); | |
153 } | |
154 } | |
155 | |
95 private: | 156 private: |
96 scoped_ptr<content::NotificationService> notification_service_; | 157 scoped_ptr<content::NotificationService> notification_service_; |
158 content::TestBrowserThreadBundle thread_bundle_; | |
159 base::HistogramTester histogram_tester_; | |
97 | 160 |
98 DISALLOW_COPY_AND_ASSIGN(EventRouterTest); | 161 DISALLOW_COPY_AND_ASSIGN(EventRouterTest); |
99 }; | 162 }; |
100 | 163 |
101 TEST_F(EventRouterTest, GetBaseEventName) { | 164 TEST_F(EventRouterTest, GetBaseEventName) { |
102 // Normal event names are passed through unchanged. | 165 // Normal event names are passed through unchanged. |
103 EXPECT_EQ("foo.onBar", EventRouter::GetBaseEventName("foo.onBar")); | 166 EXPECT_EQ("foo.onBar", EventRouter::GetBaseEventName("foo.onBar")); |
104 | 167 |
105 // Sub-events are converted to the part before the slash. | 168 // Sub-events are converted to the part before the slash. |
106 EXPECT_EQ("foo.onBar", EventRouter::GetBaseEventName("foo.onBar/123")); | 169 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) { | 227 TEST_F(EventRouterTest, EventRouterObserverForExtensions) { |
165 RunEventRouterObserverTest( | 228 RunEventRouterObserverTest( |
166 base::Bind(&CreateEventListenerForExtension, "extension_id")); | 229 base::Bind(&CreateEventListenerForExtension, "extension_id")); |
167 } | 230 } |
168 | 231 |
169 TEST_F(EventRouterTest, EventRouterObserverForURLs) { | 232 TEST_F(EventRouterTest, EventRouterObserverForURLs) { |
170 RunEventRouterObserverTest( | 233 RunEventRouterObserverTest( |
171 base::Bind(&CreateEventListenerForURL, GURL("http://google.com/path"))); | 234 base::Bind(&CreateEventListenerForURL, GURL("http://google.com/path"))); |
172 } | 235 } |
173 | 236 |
237 TEST_F(EventRouterTest, TestReportEvent) { | |
238 EventRouter router(browser_context(), NULL); | |
239 scoped_refptr<Extension> normal = test_util::CreateEmptyExtension("id1"); | |
240 router.ReportEvent(events::HistogramValue::FOR_TEST, normal.get(), | |
241 false /** did_enqueue */); | |
242 ExpectHistogramCounts(1 /** Dispatch */, 0 /** DispatchToComponent */, | |
243 0 /** DispatchWithPersistentBackgroundPage */, | |
244 0 /** DispatchWithSuspendedEventPage */, | |
245 0 /** DispatchToComponentWithSuspendedEventPage */, | |
246 0 /** DispatchWithRunningEventPage */); | |
247 | |
248 scoped_refptr<Extension> component = CreateExtension(true, true); | |
249 router.ReportEvent(events::HistogramValue::FOR_TEST, component.get(), | |
250 false /** did_enqueue */); | |
251 ExpectHistogramCounts(2, 1, 1, 0, 0, 0); | |
252 | |
253 scoped_refptr<Extension> persistent = CreateExtension(false, true); | |
254 router.ReportEvent(events::HistogramValue::FOR_TEST, persistent.get(), | |
255 false /** did_enqueue */); | |
256 ExpectHistogramCounts(3, 1, 2, 0, 0, 0); | |
257 | |
258 scoped_refptr<Extension> event = CreateExtension(false, false); | |
259 router.ReportEvent(events::HistogramValue::FOR_TEST, event.get(), | |
260 false /** did_enqueue */); | |
261 router.ReportEvent(events::HistogramValue::FOR_TEST, event.get(), | |
262 true /** did_enqueue */); | |
263 ExpectHistogramCounts(5, 1, 2, 1, 0, 1); | |
benwells
2016/01/20 23:23:36
Nit: Seems like having ExpectHistogramCounts(4, 1,
| |
264 | |
265 scoped_refptr<Extension> component_event = CreateExtension(true, false); | |
266 router.ReportEvent(events::HistogramValue::FOR_TEST, component_event.get(), | |
267 false /** did_enqueue */); | |
268 router.ReportEvent(events::HistogramValue::FOR_TEST, component_event.get(), | |
269 true /** did_enqueue */); | |
270 ExpectHistogramCounts(7, 3, 2, 2, 1, 2); | |
271 } | |
272 | |
174 } // namespace extensions | 273 } // namespace extensions |
OLD | NEW |