2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include <dali-test-suite-utils.h>
25 #include <dali/devel-api/signals/signal-delegate.h>
26 #include <dali/public-api/dali-core.h>
30 void utc_dali_signal_delegate_startup(void)
32 test_return_value = TET_UNDEF;
35 void utc_dali_signal_delegate_cleanup(void)
37 test_return_value = TET_PASS;
40 // Test infrastructure:
42 static bool gSignalReceived = false;
45 * This object allows us to test member function connection.
47 class SignalDelegateTestClass : public Dali::ConnectionTracker
50 SignalDelegateTestClass(Actor connectActor, std::string connectSignal)
52 mSignalDelegate = new SignalDelegate(connectActor, connectSignal);
55 void ConnectToInternalMember()
57 mSignalDelegate->Connect(this, &SignalDelegateTestClass::SignalHandlerMemberFunction);
62 return mSignalDelegate->IsConnected();
66 void SignalHandlerMemberFunction()
68 tet_infoline("Got signal in member function\n");
69 gSignalReceived = true;
72 SignalDelegate* mSignalDelegate;
76 * A connection tracker is required when connecting a signal delegate to a functor.
78 class TestConnectionTrackerObject : public ConnectionTracker
83 * This functor is used to test the signal delegate's connect ( to functor ) method.
85 struct SignalDelegateTestFunctor
87 SignalDelegateTestFunctor()
93 gSignalReceived = true;
99 int UtcDaliSignalDelegateIsConnectedP(void)
101 TestApplication application;
102 tet_infoline(" UtcDaliSignalDelegateIsConnectedP");
104 // Set up an actor with a signal to connect to.
105 Actor connectActor = Actor::New();
106 std::string connectSignal = "onScene";
108 // Create the test class (this will create the delegate, but not connect to it yet.
109 SignalDelegateTestClass testObject(connectActor, connectSignal);
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();
115 DALI_TEST_CHECK(testObject.IsConnected());
120 int UtcDaliSignalDelegateIsConnectedN(void)
122 TestApplication application;
123 tet_infoline(" UtcDaliSignalDelegateIsConnectedN");
125 // Set up an actor with a signal to connect to.
126 Actor connectActor = Actor::New();
127 std::string connectSignal = "onScene";
129 // Create the test class (this will create the delegate, but not connect to it yet.
130 SignalDelegateTestClass testObject(connectActor, connectSignal);
132 DALI_TEST_CHECK(!testObject.IsConnected());
137 int UtcDaliSignalDelegateConnectToMemberP(void)
139 TestApplication application;
140 tet_infoline(" UtcDaliSignalDelegateConnectToMemberP");
142 // Set up an actor with a signal to connect to.
143 Actor connectActor = Actor::New();
144 std::string connectSignal = "onScene";
146 gSignalReceived = false;
148 // Create the test class (this will create the delegate, but not connect to it yet.
149 SignalDelegateTestClass testObject(connectActor, connectSignal);
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();
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);
160 // Check the global flag to confirm the signal was received.
161 DALI_TEST_CHECK(gSignalReceived);
166 int UtcDaliSignalDelegateConnectToMemberN(void)
168 TestApplication application;
169 tet_infoline(" UtcDaliSignalDelegateConnectToMemberN");
171 // Set up an actor with a signal to connect to.
172 Actor connectActor = Actor::New();
173 std::string connectSignal = "onScene";
175 gSignalReceived = false;
177 // Create the test class (this will create the delegate, but not connect to it yet.
178 SignalDelegateTestClass testObject(connectActor, connectSignal);
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();
184 // Check the global flag to confirm the signal was not received.
185 DALI_TEST_CHECK(!gSignalReceived);
190 int UtcDaliSignalDelegateConnectToFunctorP(void)
192 TestApplication application;
193 tet_infoline(" UtcDaliSignalDelegateConnectToFunctorP");
195 // Set up an actor with a signal to connect to.
196 Actor connectActor = Actor::New();
197 std::string connectSignal = "onScene";
199 // Initialise the signal delegate with the actor to connect to and it's signal.
200 SignalDelegate signalDelegate(connectActor, connectSignal);
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();
207 // Check the signal delegate currently has no connection.
208 DALI_TEST_CHECK(!signalDelegate.IsConnected());
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()));
214 // Check the signal delegate has made the connection.
215 DALI_TEST_CHECK(signalDelegate.IsConnected());
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);
222 // Check the global flag to confirm the signal was received.
223 DALI_TEST_CHECK(gSignalReceived);
228 int UtcDaliSignalDelegateConnectToFunctorN(void)
230 TestApplication application;
231 tet_infoline(" UtcDaliSignalDelegateConnectToFunctorN");
233 // Set up an actor with a signal to connect to.
234 Actor connectActor = Actor::New();
235 std::string connectSignal = "onScene";
237 // Initialise the signal delegate with the actor to connect to and it's signal.
238 SignalDelegate signalDelegate(connectActor, connectSignal);
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();
245 // Check the signal delegate currently has no connection.
246 DALI_TEST_CHECK(!signalDelegate.IsConnected());
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()));
252 // Check the signal delegate has made the connection.
253 DALI_TEST_CHECK(signalDelegate.IsConnected());
255 // Check the global flag to confirm the signal was received.
256 DALI_TEST_CHECK(!gSignalReceived);