Chromium Code Reviews| Index: extensions/browser/event_router_unittest.cc |
| diff --git a/extensions/browser/event_router_unittest.cc b/extensions/browser/event_router_unittest.cc |
| index ff5d5db0d1ab022e226715f0ab72451416430ac6..22d78d3aa5a603e6440cd99574fda35d2d3668e7 100644 |
| --- a/extensions/browser/event_router_unittest.cc |
| +++ b/extensions/browser/event_router_unittest.cc |
| @@ -10,10 +10,14 @@ |
| #include "base/compiler_specific.h" |
| #include "base/macros.h" |
| #include "base/memory/scoped_ptr.h" |
| +#include "base/test/histogram_tester.h" |
| #include "base/values.h" |
| #include "content/public/browser/notification_service.h" |
| +#include "content/public/test/test_browser_thread_bundle.h" |
| #include "extensions/browser/event_listener_map.h" |
| #include "extensions/browser/extensions_test.h" |
| +#include "extensions/common/extension_builder.h" |
| +#include "extensions/common/test_util.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| namespace extensions { |
| @@ -81,6 +85,23 @@ scoped_ptr<EventListener> CreateEventListenerForURL( |
| event_name, listener_url, process, make_scoped_ptr(filter)); |
| } |
| +scoped_refptr<Extension> CreateExtension(bool component, bool persistent) { |
| + scoped_ptr<base::DictionaryValue> manifest = |
| + make_scoped_ptr(new base::DictionaryValue()); |
| + manifest->SetString("name", "foo"); |
| + manifest->SetString("version", "1.0.0"); |
| + manifest->SetInteger("manifest_version", 2); |
| + manifest->SetString("background.page", "background.html"); |
| + manifest->SetBoolean("background.persistent", persistent); |
| + |
| + ExtensionBuilder builder; |
| + builder.SetManifest(std::move(manifest)); |
| + if (component) |
| + builder.SetLocation(Manifest::Location::COMPONENT); |
| + |
| + return builder.Build(); |
| +} |
| + |
| } // namespace |
| class EventRouterTest : public ExtensionsTest { |
| @@ -92,8 +113,50 @@ class EventRouterTest : public ExtensionsTest { |
| // Tests adding and removing observers from EventRouter. |
| void RunEventRouterObserverTest(const EventListenerConstructor& constructor); |
| + // Tests that the correct counts are recorded for the Extensions.Events |
| + // histograms. |
| + void ExpectHistogramCounts(int dispatch_count, |
| + int component_count, |
| + int persistent_count, |
| + int suspended_count, |
| + int component_suspended_count, |
| + int running_count) { |
| + if (dispatch_count) { |
| + histogram_tester_.ExpectBucketCount("Extensions.Events.Dispatch", |
| + events::HistogramValue::FOR_TEST, |
| + dispatch_count); |
| + } |
| + if (component_count) { |
| + histogram_tester_.ExpectBucketCount( |
| + "Extensions.Events.DispatchToComponent", |
| + events::HistogramValue::FOR_TEST, component_count); |
| + } |
| + if (persistent_count) { |
| + histogram_tester_.ExpectBucketCount( |
| + "Extensions.Events.DispatchWithPersistentBackgroundPage", |
| + events::HistogramValue::FOR_TEST, persistent_count); |
| + } |
| + if (suspended_count) { |
| + histogram_tester_.ExpectBucketCount( |
| + "Extensions.Events.DispatchWithSuspendedEventPage", |
| + events::HistogramValue::FOR_TEST, suspended_count); |
| + } |
| + if (component_suspended_count) { |
| + histogram_tester_.ExpectBucketCount( |
| + "Extensions.Events.DispatchToComponentWithSuspendedEventPage", |
| + events::HistogramValue::FOR_TEST, component_suspended_count); |
| + } |
| + if (running_count) { |
| + histogram_tester_.ExpectBucketCount( |
| + "Extensions.Events.DispatchWithRunningEventPage", |
| + events::HistogramValue::FOR_TEST, running_count); |
| + } |
| + } |
| + |
| private: |
| scoped_ptr<content::NotificationService> notification_service_; |
| + content::TestBrowserThreadBundle thread_bundle_; |
| + base::HistogramTester histogram_tester_; |
| DISALLOW_COPY_AND_ASSIGN(EventRouterTest); |
| }; |
| @@ -171,4 +234,40 @@ TEST_F(EventRouterTest, EventRouterObserverForURLs) { |
| base::Bind(&CreateEventListenerForURL, GURL("http://google.com/path"))); |
| } |
| +TEST_F(EventRouterTest, TestReportEvent) { |
| + EventRouter router(browser_context(), NULL); |
| + scoped_refptr<Extension> normal = test_util::CreateEmptyExtension("id1"); |
| + router.ReportEvent(events::HistogramValue::FOR_TEST, normal.get(), |
| + false /** did_enqueue */); |
| + ExpectHistogramCounts(1 /** Dispatch */, 0 /** DispatchToComponent */, |
| + 0 /** DispatchWithPersistentBackgroundPage */, |
| + 0 /** DispatchWithSuspendedEventPage */, |
| + 0 /** DispatchToComponentWithSuspendedEventPage */, |
| + 0 /** DispatchWithRunningEventPage */); |
| + |
| + scoped_refptr<Extension> component = CreateExtension(true, true); |
| + router.ReportEvent(events::HistogramValue::FOR_TEST, component.get(), |
| + false /** did_enqueue */); |
| + ExpectHistogramCounts(2, 1, 1, 0, 0, 0); |
| + |
| + scoped_refptr<Extension> persistent = CreateExtension(false, true); |
| + router.ReportEvent(events::HistogramValue::FOR_TEST, persistent.get(), |
| + false /** did_enqueue */); |
| + ExpectHistogramCounts(3, 1, 2, 0, 0, 0); |
| + |
| + scoped_refptr<Extension> event = CreateExtension(false, false); |
| + router.ReportEvent(events::HistogramValue::FOR_TEST, event.get(), |
| + false /** did_enqueue */); |
| + router.ReportEvent(events::HistogramValue::FOR_TEST, event.get(), |
| + true /** did_enqueue */); |
| + ExpectHistogramCounts(5, 1, 2, 1, 0, 1); |
|
benwells
2016/01/20 23:23:36
Nit: Seems like having ExpectHistogramCounts(4, 1,
|
| + |
| + scoped_refptr<Extension> component_event = CreateExtension(true, false); |
| + router.ReportEvent(events::HistogramValue::FOR_TEST, component_event.get(), |
| + false /** did_enqueue */); |
| + router.ReportEvent(events::HistogramValue::FOR_TEST, component_event.get(), |
| + true /** did_enqueue */); |
| + ExpectHistogramCounts(7, 3, 2, 2, 1, 2); |
| +} |
| + |
| } // namespace extensions |