Revert "[Tizen] Appendix log for ttrace + Print keycode and timestamp"
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-SignalDelegate.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
18 // EXTERNAL INCLUDES
19 #include <stdlib.h>
20
21 #include <iostream>
22
23 // INTERNAL INCLUDES
24 #include <dali-test-suite-utils.h>
25 #include <dali/devel-api/signals/signal-delegate.h>
26 #include <dali/public-api/dali-core.h>
27
28 using namespace Dali;
29
30 void utc_dali_signal_delegate_startup(void)
31 {
32   test_return_value = TET_UNDEF;
33 }
34
35 void utc_dali_signal_delegate_cleanup(void)
36 {
37   test_return_value = TET_PASS;
38 }
39
40 // Test infrastructure:
41
42 static bool gSignalReceived = false;
43
44 /**
45  * This object allows us to test member function connection.
46  */
47 class SignalDelegateTestClass : public Dali::ConnectionTracker
48 {
49 public:
50   SignalDelegateTestClass(Actor connectActor, std::string connectSignal)
51   {
52     mSignalDelegate = new SignalDelegate(connectActor, connectSignal);
53   }
54
55   void ConnectToInternalMember()
56   {
57     mSignalDelegate->Connect(this, &SignalDelegateTestClass::SignalHandlerMemberFunction);
58   }
59
60   bool IsConnected()
61   {
62     return mSignalDelegate->IsConnected();
63   }
64
65 private:
66   void SignalHandlerMemberFunction()
67   {
68     tet_infoline("Got signal in member function\n");
69     gSignalReceived = true;
70   }
71
72   SignalDelegate* mSignalDelegate;
73 };
74
75 /**
76  * A connection tracker is required when connecting a signal delegate to a functor.
77  */
78 class TestConnectionTrackerObject : public ConnectionTracker
79 {
80 };
81
82 /**
83  * This functor is used to test the signal delegate's connect ( to functor ) method.
84  */
85 struct SignalDelegateTestFunctor
86 {
87   SignalDelegateTestFunctor()
88   {
89   }
90
91   void operator()()
92   {
93     gSignalReceived = true;
94   }
95 };
96
97 // Test cases:
98
99 int UtcDaliSignalDelegateIsConnectedP(void)
100 {
101   TestApplication application;
102   tet_infoline(" UtcDaliSignalDelegateIsConnectedP");
103
104   // Set up an actor with a signal to connect to.
105   Actor       connectActor  = Actor::New();
106   std::string connectSignal = "onScene";
107
108   // Create the test class (this will create the delegate, but not connect to it yet.
109   SignalDelegateTestClass testObject(connectActor, connectSignal);
110
111   // Tell the test class to connect the delegate to it's internal member.
112   // Note: It is at this point that the delegate internally makes the connection.
113   testObject.ConnectToInternalMember();
114
115   DALI_TEST_CHECK(testObject.IsConnected());
116
117   END_TEST;
118 }
119
120 int UtcDaliSignalDelegateIsConnectedN(void)
121 {
122   TestApplication application;
123   tet_infoline(" UtcDaliSignalDelegateIsConnectedN");
124
125   // Set up an actor with a signal to connect to.
126   Actor       connectActor  = Actor::New();
127   std::string connectSignal = "onScene";
128
129   // Create the test class (this will create the delegate, but not connect to it yet.
130   SignalDelegateTestClass testObject(connectActor, connectSignal);
131
132   DALI_TEST_CHECK(!testObject.IsConnected());
133
134   END_TEST;
135 }
136
137 int UtcDaliSignalDelegateConnectToMemberP(void)
138 {
139   TestApplication application;
140   tet_infoline(" UtcDaliSignalDelegateConnectToMemberP");
141
142   // Set up an actor with a signal to connect to.
143   Actor       connectActor  = Actor::New();
144   std::string connectSignal = "onScene";
145
146   gSignalReceived = false;
147
148   // Create the test class (this will create the delegate, but not connect to it yet.
149   SignalDelegateTestClass testObject(connectActor, connectSignal);
150
151   // Tell the test class to connect the delegate to it's internal member.
152   // Note: It is at this point that the delegate internally makes the connection.
153   testObject.ConnectToInternalMember();
154
155   // Add the actor to the scene to trigger it's "onScene" signal.
156   // If the delegate connected correctly, this will call the member
157   // function in the test object and set a global flag.
158   application.GetScene().Add(connectActor);
159
160   // Check the global flag to confirm the signal was received.
161   DALI_TEST_CHECK(gSignalReceived);
162
163   END_TEST;
164 }
165
166 int UtcDaliSignalDelegateConnectToMemberN(void)
167 {
168   TestApplication application;
169   tet_infoline(" UtcDaliSignalDelegateConnectToMemberN");
170
171   // Set up an actor with a signal to connect to.
172   Actor       connectActor  = Actor::New();
173   std::string connectSignal = "onScene";
174
175   gSignalReceived = false;
176
177   // Create the test class (this will create the delegate, but not connect to it yet.
178   SignalDelegateTestClass testObject(connectActor, connectSignal);
179
180   // Tell the test class to connect the delegate to it's internal member.
181   // Note: It is at this point that the delegate internally makes the connection.
182   testObject.ConnectToInternalMember();
183
184   // Check the global flag to confirm the signal was not received.
185   DALI_TEST_CHECK(!gSignalReceived);
186
187   END_TEST;
188 }
189
190 int UtcDaliSignalDelegateConnectToFunctorP(void)
191 {
192   TestApplication application;
193   tet_infoline(" UtcDaliSignalDelegateConnectToFunctorP");
194
195   // Set up an actor with a signal to connect to.
196   Actor       connectActor  = Actor::New();
197   std::string connectSignal = "onScene";
198
199   // Initialise the signal delegate with the actor to connect to and it's signal.
200   SignalDelegate signalDelegate(connectActor, connectSignal);
201
202   // We need a connection tracker object to associated with the connection.
203   // This could normally be "this", but since we are not within a class, we pass
204   // in an external one.
205   TestConnectionTrackerObject* testTracker = new TestConnectionTrackerObject();
206
207   // Check the signal delegate currently has no connection.
208   DALI_TEST_CHECK(!signalDelegate.IsConnected());
209
210   // Tell the signal delegate to connect to the given functor (via a functor delegate).
211   // Note: It is at this point that the delegate internally makes the connection.
212   signalDelegate.Connect(testTracker, FunctorDelegate::New(SignalDelegateTestFunctor()));
213
214   // Check the signal delegate has made the connection.
215   DALI_TEST_CHECK(signalDelegate.IsConnected());
216
217   // Add the actor to the scene to trigger it's "onScene" signal.
218   // If the delegate connected correctly, this will call the () operator of our
219   // passed-in functor, the functor will in turn set a global flag.
220   application.GetScene().Add(connectActor);
221
222   // Check the global flag to confirm the signal was received.
223   DALI_TEST_CHECK(gSignalReceived);
224
225   END_TEST;
226 }
227
228 int UtcDaliSignalDelegateConnectToFunctorN(void)
229 {
230   TestApplication application;
231   tet_infoline(" UtcDaliSignalDelegateConnectToFunctorN");
232
233   // Set up an actor with a signal to connect to.
234   Actor       connectActor  = Actor::New();
235   std::string connectSignal = "onScene";
236
237   // Initialise the signal delegate with the actor to connect to and it's signal.
238   SignalDelegate signalDelegate(connectActor, connectSignal);
239
240   // We need a connection tracker object to associated with the connection.
241   // This could normally be "this", but since we are not within a class, we pass
242   // in an external one.
243   TestConnectionTrackerObject* testTracker = new TestConnectionTrackerObject();
244
245   // Check the signal delegate currently has no connection.
246   DALI_TEST_CHECK(!signalDelegate.IsConnected());
247
248   // Tell the signal delegate to connect to the given functor (via a functor delegate).
249   // Note: It is at this point that the delegate internally makes the connection.
250   signalDelegate.Connect(testTracker, FunctorDelegate::New(SignalDelegateTestFunctor()));
251
252   // Check the signal delegate has made the connection.
253   DALI_TEST_CHECK(signalDelegate.IsConnected());
254
255   // Check the global flag to confirm the signal was received.
256   DALI_TEST_CHECK(!gSignalReceived);
257
258   END_TEST;
259 }