tizen 2.3.1 release
[framework/web/wearable/wrt-commons.git] / tests / event / test_event_support.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file        test_event_support.cpp
18  * @author      Piotr Marcinkiewicz (p.marcinkiew@samsung.com)
19  * @author      Pawel Sikorski (p.sikorski@samsung.com)
20  * @version     1.0
21  * @brief       This file contains test for event support
22  */
23
24 #include <dpl/test/test_runner.h>
25 #include <dpl/generic_event.h>
26 #include <dpl/event/event_listener.h>
27 #include <dpl/event/event_support.h>
28 #include <dpl/application.h>
29 #include <dpl/event/controller.h>
30 #include <dpl/log/log.h>
31
32 DECLARE_GENERIC_EVENT_0(TestEvent)
33
34 class TestListener : public DPL::Event::EventListener<TestEvent>
35 {
36   public:
37     explicit TestListener() : m_dummyVar(0) { }
38     void OnEventReceived(const TestEvent &)
39     {
40         m_dummyVar = 1;
41     }
42     int GetDummyVar() const
43     {
44         return m_dummyVar;
45     }
46     void ZeroDummyVar()
47     {
48         m_dummyVar = 0;
49     }
50
51   private:
52     int m_dummyVar;
53 };
54
55 class TestEventSupport :
56     public DPL::Event::EventSupport<TestEvent>
57 {
58   public:
59     void TestEmitEvent()
60     {
61         EmitEvent(TestEvent());
62     }
63 };
64
65 DECLARE_GENERIC_EVENT_0(QuitEvent)
66
67 class QuitController :
68     public DPL::Event::Controller<DPL::TypeListDecl<QuitEvent>::Type>,
69     public DPL::ApplicationExt
70 {
71   public:
72     QuitController() : DPL::ApplicationExt(1, NULL, "test-app")
73     {
74         Touch();
75     }
76
77   protected:
78     virtual void OnEventReceived(const QuitEvent &)
79     {
80         Quit();
81     }
82 };
83
84 /*
85 Name: EventSupport_DestroyBeforeProcessing
86 Description: tests if remoign listener is full successfull
87 Expected: dummy var should be affected by explicit call of ZeroDummyVar(),
88  but no by emitting event after removing listener
89 */
90 RUNNER_TEST(EventSupport_DestroyBeforeProcessing)
91 {
92     QuitController quitter;
93     quitter.PostTimedEvent(QuitEvent(), 1.0);
94
95     TestListener eventListener;
96     {
97         TestEventSupport eventSupport;
98         eventSupport.AddListener(&eventListener);
99         eventSupport.TestEmitEvent();
100         eventSupport.RemoveListener(&eventListener);
101     }
102     eventListener.ZeroDummyVar();
103
104     quitter.Exec();
105     RUNNER_ASSERT(eventListener.GetDummyVar() == 0);
106 }
107
108 int g_delegateTest;
109
110 void OnDelegateTest(const int &k);
111
112 void OnDelegateTest(const int &k)
113 {
114     LogDebug("Got delegate call");
115     g_delegateTest = k;
116 }
117
118 class DelegateTestSupport :
119     public DPL::Event::EventSupport<int>
120 {
121   public:
122     void Test()
123     {
124         EmitEvent(7);
125     }
126 };
127
128 /*
129 Name: EventSupport_BindDelegate
130 Description: tests if event support derived class successfully propagates
131  event to registered listener
132 Expected: value of event should be passed to listener
133 */
134 RUNNER_TEST(EventSupport_BindDelegate)
135 {
136     g_delegateTest = 0;
137
138     DelegateTestSupport support;
139     support.AddListener(&OnDelegateTest);
140
141     QuitController quitter;
142     quitter.PostTimedEvent(QuitEvent(), 1.0);
143
144     support.Test();
145
146     quitter.Exec();
147
148     support.RemoveListener(&OnDelegateTest);
149
150     RUNNER_ASSERT(g_delegateTest == 7);
151 }