| 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/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "content/public/test/test_browser_context.h" |
| 12 #include "extensions/browser/event_listener_map.h" | 13 #include "extensions/browser/event_listener_map.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 15 |
| 15 namespace extensions { | 16 namespace extensions { |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 // A simple mock to keep track of listener additions and removals. | 20 // A simple mock to keep track of listener additions and removals. |
| 20 class MockEventRouterObserver : public EventRouter::Observer { | 21 class MockEventRouterObserver : public EventRouter::Observer { |
| 21 public: | 22 public: |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 TEST_F(EventRouterTest, GetBaseEventName) { | 61 TEST_F(EventRouterTest, GetBaseEventName) { |
| 61 // Normal event names are passed through unchanged. | 62 // Normal event names are passed through unchanged. |
| 62 EXPECT_EQ("foo.onBar", EventRouter::GetBaseEventName("foo.onBar")); | 63 EXPECT_EQ("foo.onBar", EventRouter::GetBaseEventName("foo.onBar")); |
| 63 | 64 |
| 64 // Sub-events are converted to the part before the slash. | 65 // Sub-events are converted to the part before the slash. |
| 65 EXPECT_EQ("foo.onBar", EventRouter::GetBaseEventName("foo.onBar/123")); | 66 EXPECT_EQ("foo.onBar", EventRouter::GetBaseEventName("foo.onBar/123")); |
| 66 } | 67 } |
| 67 | 68 |
| 68 // Tests adding and removing observers from EventRouter. | 69 // Tests adding and removing observers from EventRouter. |
| 69 TEST_F(EventRouterTest, EventRouterObserver) { | 70 TEST_F(EventRouterTest, EventRouterObserver) { |
| 70 EventRouter router(NULL, NULL); | 71 scoped_ptr<content::TestBrowserContext> browser_context( |
| 72 new content::TestBrowserContext()); |
| 73 EventRouter router(browser_context.get(), NULL); |
| 71 EventListener listener( | 74 EventListener listener( |
| 72 "event_name", "extension_id", NULL, scoped_ptr<base::DictionaryValue>()); | 75 "event_name", "extension_id", NULL, scoped_ptr<base::DictionaryValue>()); |
| 73 | 76 |
| 74 // Add/remove works without any observers. | 77 // Add/remove works without any observers. |
| 75 router.OnListenerAdded(&listener); | 78 router.OnListenerAdded(&listener); |
| 76 router.OnListenerRemoved(&listener); | 79 router.OnListenerRemoved(&listener); |
| 77 | 80 |
| 78 // Register observers that both match and don't match the event above. | 81 // Register observers that both match and don't match the event above. |
| 79 MockEventRouterObserver matching_observer; | 82 MockEventRouterObserver matching_observer; |
| 80 router.RegisterObserver(&matching_observer, "event_name"); | 83 router.RegisterObserver(&matching_observer, "event_name"); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 | 118 |
| 116 // Ditto for removing the listener. | 119 // Ditto for removing the listener. |
| 117 matching_observer.Reset(); | 120 matching_observer.Reset(); |
| 118 router.OnListenerRemoved(&sub_event_listener); | 121 router.OnListenerRemoved(&sub_event_listener); |
| 119 EXPECT_EQ(0, matching_observer.listener_added_count()); | 122 EXPECT_EQ(0, matching_observer.listener_added_count()); |
| 120 EXPECT_EQ(1, matching_observer.listener_removed_count()); | 123 EXPECT_EQ(1, matching_observer.listener_removed_count()); |
| 121 EXPECT_EQ("event_name/1", matching_observer.last_event_name()); | 124 EXPECT_EQ("event_name/1", matching_observer.last_event_name()); |
| 122 } | 125 } |
| 123 | 126 |
| 124 } // namespace extensions | 127 } // namespace extensions |
| OLD | NEW |