1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/public/test/test_browser_thread_bundle.h"
6 #include "testing/gtest/include/gtest/gtest.h"
7 #include "xwalk/application/browser/application_event_manager.h"
8 #include "xwalk/application/browser/application_event_router.h"
9 #include "xwalk/application/browser/event_observer.h"
10 #include "xwalk/application/browser/application_system.h"
11 #include "xwalk/runtime/browser/runtime_context.h"
14 namespace application {
18 const char kMockEvent0[] = "MOCK_EVENT_0";
19 const char kMockEvent1[] = "MOCK_EVENT_1";
20 const char kMockAppId0[] = "mock_app_0";
22 std::vector<std::string> g_call_sequence;
24 class MockEventObserver : public EventObserver {
26 explicit MockEventObserver(ApplicationEventManager* manager)
27 : EventObserver(manager) {
30 virtual void Observe(const std::string& app_id,
31 scoped_refptr<Event> event) OVERRIDE {
32 if (event->name() == kMockEvent0 || event->name() == kMockEvent1) {
33 HandleEvent(app_id, event);
39 void HandleEvent(const std::string& app_id, scoped_refptr<Event> event) {
40 std::string call_info = __FUNCTION__;
41 call_info += "," + app_id + "," + event->name();
42 g_call_sequence.push_back(call_info);
48 class ApplicationEventRouterTest : public testing::Test {
50 virtual void SetUp() OVERRIDE {
51 runtime_context_.reset(new xwalk::RuntimeContext);
52 system_ = ApplicationSystem::Create(runtime_context_.get());
53 router_.reset(new ApplicationEventRouter(kMockAppId0));
54 event_manager_ = system_->event_manager();
57 void SendEventToApp(const std::string& event_name) {
58 scoped_refptr<Event> event = Event::CreateEvent(
59 event_name, scoped_ptr<base::ListValue>(new base::ListValue()));
60 router_->ProcessEvent(event);
63 int GetObserverCount(const std::string& event_name) {
64 ApplicationEventRouter::ObserverListMap::iterator it =
65 router_->observers_.find(event_name);
66 if (it == router_->observers_.end() || !it->second->might_have_observers())
68 ObserverList<EventObserver>::Iterator ob_it(*it->second);
70 while (ob_it.GetNext() != NULL)
75 int GetRegisteredEventCount() {
76 return router_->observers_.size();
80 ApplicationEventManager* event_manager_;
81 scoped_ptr<ApplicationEventRouter> router_;
84 scoped_ptr<xwalk::RuntimeContext> runtime_context_;
85 scoped_ptr<ApplicationSystem> system_;
86 content::TestBrowserThreadBundle thread_bundle_;
89 TEST_F(ApplicationEventRouterTest, AttachObserver) {
90 MockEventObserver observer(event_manager_);
92 router_->AttachObserver(kMockEvent0, &observer);
93 ASSERT_EQ(GetObserverCount(kMockEvent0), 1);
95 router_->AttachObserver(kMockEvent1, &observer);
96 ASSERT_EQ(GetRegisteredEventCount(), 2);
97 ASSERT_EQ(GetObserverCount(kMockEvent1), 1);
100 TEST_F(ApplicationEventRouterTest, DetachObserverFromEvent) {
101 MockEventObserver observer1(event_manager_);
102 MockEventObserver observer2(event_manager_);
104 router_->AttachObserver(kMockEvent0, &observer1);
105 ASSERT_EQ(GetObserverCount(kMockEvent0), 1);
106 router_->AttachObserver(kMockEvent0, &observer2);
107 ASSERT_EQ(GetObserverCount(kMockEvent0), 2);
109 router_->DetachObserver(kMockEvent0, &observer1);
110 ASSERT_EQ(GetObserverCount(kMockEvent0), 1);
112 // Removing non-exist observer from router must not crash.
113 router_->DetachObserver(kMockEvent0, &observer1);
114 ASSERT_EQ(GetObserverCount(kMockEvent0), 1);
116 router_->DetachObserver(kMockEvent0, &observer2);
117 ASSERT_EQ(GetObserverCount(kMockEvent0), 0);
118 ASSERT_EQ(GetRegisteredEventCount(), 0);
121 TEST_F(ApplicationEventRouterTest, DetachObservers) {
122 MockEventObserver observer1(event_manager_);
123 MockEventObserver observer2(event_manager_);
124 MockEventObserver observer3(event_manager_);
125 router_->AttachObserver(kMockEvent0, &observer1);
126 router_->AttachObserver(kMockEvent1, &observer1);
127 router_->AttachObserver(kMockEvent1, &observer2);
128 router_->AttachObserver(kMockEvent0, &observer3);
129 router_->AttachObserver(kMockEvent1, &observer3);
130 ASSERT_EQ(GetObserverCount(kMockEvent0), 2);
131 ASSERT_EQ(GetObserverCount(kMockEvent1), 3);
133 router_->DetachObserver(&observer1);
134 ASSERT_EQ(GetObserverCount(kMockEvent0), 1);
135 ASSERT_EQ(GetObserverCount(kMockEvent1), 2);
137 router_->DetachAllObservers();
138 ASSERT_EQ(GetObserverCount(kMockEvent0), 0);
139 ASSERT_EQ(GetObserverCount(kMockEvent1), 0);
140 ASSERT_EQ(GetRegisteredEventCount(), 0);
143 // Dispatch event which has multiple observers, all observers should be
145 TEST_F(ApplicationEventRouterTest, EventDispatch) {
146 MockEventObserver observer1(event_manager_);
147 MockEventObserver observer2(event_manager_);
148 MockEventObserver observer3(event_manager_);
149 g_call_sequence.clear();
150 router_->AttachObserver(kMockEvent0, &observer1);
151 router_->AttachObserver(kMockEvent0, &observer2);
152 router_->AttachObserver(kMockEvent0, &observer3);
154 SendEventToApp(kMockEvent0);
155 ASSERT_EQ(g_call_sequence.size(), 3);
158 } // namespace application