Tizen 2.0 Release
[framework/web/wrt-commons.git] / tests / dpl / 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/fast_delegate.h>
31 #include <dpl/log/log.h>
32
33 DECLARE_GENERIC_EVENT_0(TestEvent)
34
35 class TestListener: public DPL::Event::EventListener<TestEvent>
36 {
37 public:
38     explicit TestListener() : m_dummyVar(0) { }
39     void OnEventReceived(const TestEvent &) { m_dummyVar = 1; }
40     int GetDummyVar() const { return m_dummyVar; }
41     void ZeroDummyVar() { m_dummyVar = 0; }
42
43 private:
44     int m_dummyVar;
45 };
46
47 class TestEventSupport
48     : public DPL::Event::EventSupport<TestEvent>
49 {
50 public:
51     void TestEmitEvent() { EmitEvent(TestEvent()); }
52 };
53
54 DECLARE_GENERIC_EVENT_0(QuitEvent)
55
56 class QuitController
57     : public DPL::Event::Controller<DPL::TypeListDecl<QuitEvent>::Type>,
58       public DPL::ApplicationExt
59 {
60 public:
61     QuitController() : DPL::ApplicationExt(1, NULL, "test-app") { Touch(); }
62
63 protected:
64     virtual void OnEventReceived(const QuitEvent &) { Quit(); }
65 };
66
67 RUNNER_TEST(EventSupport_DestroyBeforeProcessing)
68 {
69     QuitController quitter;
70     quitter.PostTimedEvent(QuitEvent(), 1.0);
71
72     TestListener eventListener;
73     {
74         TestEventSupport eventSupport;
75         eventSupport.AddListener(&eventListener);
76         eventSupport.TestEmitEvent();
77         eventSupport.RemoveListener(&eventListener);
78     }
79     eventListener.ZeroDummyVar();
80
81     quitter.Exec();
82     RUNNER_ASSERT(eventListener.GetDummyVar() == 0);
83 }
84
85 int g_delegateTest;
86
87 void OnDelegateTest(const int &k);
88
89 void OnDelegateTest(const int &k)
90 {
91     LogInfo("Got delegate call");
92     g_delegateTest = k;
93 }
94
95 class DelegateTestSupport
96     : public DPL::Event::EventSupport<int>
97 {
98 public:
99     void Test()
100     {
101         EmitEvent(7);
102     }
103 };
104
105 RUNNER_TEST(EventSupport_BindDelegate)
106 {
107     g_delegateTest = 0;
108
109     DelegateTestSupport support;
110     support.AddListener(&OnDelegateTest);
111
112     QuitController quitter;
113     quitter.PostTimedEvent(QuitEvent(), 1.0);
114
115     support.Test();
116
117     quitter.Exec();
118
119     support.RemoveListener(&OnDelegateTest);
120
121     RUNNER_ASSERT(g_delegateTest == 7);
122 }