1 //******************************************************************
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include <gtest/gtest.h>
22 #include <HippoMocks/hippomocks.h>
25 #include <condition_variable>
32 #include "NSConsumerInterface.h"
34 #include "NSProviderSimulator.h"
38 NSProviderSimulator g_providerSimul;
39 NSProvider * g_provider;
41 std::atomic_bool g_isStartedStack(false);
43 std::chrono::milliseconds g_waitForResponse(500);
45 std::condition_variable responseCon;
46 std::mutex mutexForCondition;
50 class TestWithMock: public testing::Test
56 virtual ~TestWithMock() noexcept(noexcept(std::declval<Test>().~Test())) {}
58 virtual void TearDown() {
71 class NotificationConsumerTest : public TestWithMock
74 NotificationConsumerTest() = default;
75 ~NotificationConsumerTest() = default;
77 static void NSProviderDiscoveredCallbackEmpty(NSProvider *)
79 std::cout << __func__ << std::endl;
82 static void NSNotificationReceivedCallbackEmpty(NSProvider *, NSMessage *) { }
84 static void NSSyncCallbackEmpty(NSProvider *, NSSync *) { }
86 static void foundResourceEmpty(std::shared_ptr< OC::OCResource >) { }
92 TestWithMock::SetUp();
94 if (g_isStartedStack == false)
96 OC::PlatformConfig cfg
98 OC::ServiceType::InProc,
102 OC::QualityOfService::LowQos
104 OC::OCPlatform::Configure(cfg);
108 OC::OCPlatform::stopPresence();
115 g_isStartedStack = true;
122 TestWithMock::TearDown();
127 TEST_F(NotificationConsumerTest, StartConsumerPositive)
131 NSProviderDiscoveredCallbackEmpty,
132 NSNotificationReceivedCallbackEmpty,
133 NSSyncCallbackEmpty));
136 TEST_F(NotificationConsumerTest, StopConsumerPositive)
138 EXPECT_EQ(NSStopConsumer(), NS_OK);
141 TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsumerFirst)
143 mocks.ExpectCallFunc(NSProviderDiscoveredCallbackEmpty).Do(
144 [this, & responseCon](NSProvider *)
146 std::cout << "Call Discovered" << std::endl;
147 responseCon.notify_all();
151 NSProviderDiscoveredCallbackEmpty,
152 NSNotificationReceivedCallbackEmpty,
153 NSSyncCallbackEmpty);
155 g_providerSimul.setAccepter(1);
156 g_providerSimul.createNotificationResource();
158 std::unique_lock< std::mutex > lock{ mutexForCondition };
159 responseCon.wait_for(lock, g_waitForResponse);
162 g_providerSimul.deleteNotificationResource();
165 TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsumerAfter)
167 g_providerSimul.setAccepter(1);
168 g_providerSimul.createNotificationResource();
170 std::unique_lock< std::mutex > lock{ mutexForCondition };
171 responseCon.wait_for(lock, g_waitForResponse);
174 mocks.ExpectCallFunc(NSProviderDiscoveredCallbackEmpty).Do(
175 [this, & responseCon](NSProvider *)
177 std::cout << "Call Discovered" << std::endl;
178 responseCon.notify_all();
182 NSProviderDiscoveredCallbackEmpty,
183 NSNotificationReceivedCallbackEmpty,
184 NSSyncCallbackEmpty);
186 std::unique_lock< std::mutex > lock{ mutexForCondition };
187 responseCon.wait_for(lock, g_waitForResponse);
191 TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenRescan)
193 mocks.ExpectCallFunc(NSProviderDiscoveredCallbackEmpty)
195 [this, & responseCon](NSProvider * provider)
197 std::cout << "Call Discovered" << std::endl;
198 g_provider = provider;
199 responseCon.notify_all();
204 std::unique_lock< std::mutex > lock{ mutexForCondition };
205 responseCon.wait_for(lock, g_waitForResponse);
210 TEST_F(NotificationConsumerTest, ExpectSubscribeSuccess)
212 NSResult ret = NSSubscribe(g_provider);
213 std::unique_lock< std::mutex > lock{ mutexForCondition };
214 responseCon.wait_for(lock, g_waitForResponse);
216 EXPECT_EQ(NS_OK, ret);
219 TEST_F(NotificationConsumerTest, ExpectReceiveNotification)
221 std::string id = "id";
222 std::string title = "title";
223 std::string msg = "msg";
225 mocks.ExpectCallFunc(NSNotificationReceivedCallbackEmpty).Do(
226 [](NSProvider *, NSMessage * message)
228 std::cout << "Income Notification : " << message->mId << std::endl;
229 NSDropNSObject(message);
232 g_providerSimul.notifyMessage(id, title, msg);
234 std::unique_lock< std::mutex > lock{ mutexForCondition };
235 responseCon.wait_for(lock, g_waitForResponse);
239 TEST_F(NotificationConsumerTest, ExpectUnsubscribeSuccess)
241 NSResult ret = NSUnsubscribe(g_provider);
242 std::unique_lock< std::mutex > lock{ mutexForCondition };
243 responseCon.wait_for(lock, g_waitForResponse);
245 EXPECT_EQ(NS_OK, ret);
248 TEST_F(NotificationConsumerTest, ExpectReceiveNotificationWithAccepterisProvider)
250 std::string id = "ExpectReceiveNotificationWithAccepterisProvider";
251 std::string title = "title";
252 std::string msg = "msg";
254 g_providerSimul.setAccepter(0);
258 std::unique_lock< std::mutex > lock{ mutexForCondition };
259 responseCon.wait_for(lock, g_waitForResponse);
262 mocks.ExpectCallFunc(NSNotificationReceivedCallbackEmpty).Do(
263 [](NSProvider *, NSMessage * message)
265 std::cout << "Income Notification : " << message->mId << std::endl;
266 NSDropNSObject(message);
269 g_providerSimul.notifyMessage(id, title, msg);
271 std::unique_lock< std::mutex > lock{ mutexForCondition };
272 responseCon.wait_for(lock, g_waitForResponse);
274 // g_providerSimul.deleteNotificationResource();
278 TEST_F(NotificationConsumerTest, ExpectCallbackReadCheckWhenProviderNotifySync)
280 std::string id = "ExpectCallbackReadCheckWhenProviderNotifySync";
281 std::string title = "title";
282 std::string msg = "msg";
284 NSSyncTypes type = Notification_Dismiss;
286 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
287 [](NSProvider *, NSMessage * message)
289 std::cout << "Income Notification : " << message->mId << std::endl;
290 NSDropNSObject(message);
293 mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
294 [& type](NSProvider *, NSSync * sync)
296 std::cout << "Income Notification : " << sync->mMessageId
297 << ", State : " << sync->mState << std::endl;
302 g_providerSimul.notifyMessage(id, title, msg);
304 std::unique_lock< std::mutex > lock{ mutexForCondition };
305 responseCon.wait_for(lock, g_waitForResponse);
308 g_providerSimul.sendRead(id);
310 std::unique_lock< std::mutex > lock{ mutexForCondition };
311 responseCon.wait_for(lock, g_waitForResponse);
314 // g_providerSimul.deleteNotificationResource();
317 EXPECT_EQ(Notification_Read, type);
320 TEST_F(NotificationConsumerTest, ExpectCallbackDismissCheckWhenProviderNotifySync)
322 std::string id = "ExpectCallbackDismissCheckWhenProviderNotifySync";
323 std::string title = "title";
324 std::string msg = "msg";
326 NSSyncTypes type = Notification_Read;
328 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
329 [](NSProvider *, NSMessage * message)
331 std::cout << "Income Notification : " << message->mId << std::endl;
332 NSDropNSObject(message);
335 mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
336 [& type](NSProvider *, NSSync * sync)
338 std::cout << "Income Notification : " << sync->mMessageId
339 << ", State : " << sync->mState << std::endl;
344 g_providerSimul.notifyMessage(id, title, msg);
346 std::unique_lock< std::mutex > lock{ mutexForCondition };
347 responseCon.wait_for(lock, g_waitForResponse);
350 g_providerSimul.sendDismiss(id);
352 std::unique_lock< std::mutex > lock{ mutexForCondition };
353 responseCon.wait_for(lock, g_waitForResponse);
356 // g_providerSimul.deleteNotificationResource();
359 EXPECT_EQ(Notification_Dismiss, type);
362 TEST_F(NotificationConsumerTest, ExpectCallbackReadCheckWhenConsumerPostSync)
364 std::string id = "ExpectCallbackReadCheckWhenConsumerPostSync";
365 std::string title = "title";
366 std::string msg = "msg";
368 NSSyncTypes type = Notification_Dismiss;
370 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
371 [](NSProvider *, NSMessage * message)
373 std::cout << "Income Notification : " << message->mId << std::endl;
374 NSConsumerReadCheck(message);
375 std::unique_lock< std::mutex > lock{ mutexForCondition };
376 responseCon.wait_for(lock, g_waitForResponse);
379 mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
380 [& type](NSProvider *, NSSync * sync)
382 std::cout << "Income Notification : " << sync->mMessageId
383 << ", State : " << sync->mState << std::endl;
388 g_providerSimul.notifyMessage(id, title, msg);
390 std::unique_lock< std::mutex > lock{ mutexForCondition };
391 responseCon.wait_for(lock, g_waitForResponse);
394 // g_providerSimul.deleteNotificationResource();
397 EXPECT_EQ(Notification_Read, type);
400 TEST_F(NotificationConsumerTest, ExpectCallbackDismissCheckWhenConsumerPostSync)
402 std::string id = "ExpectCallbackDismissCheckWhenConsumerPostSync";
403 std::string title = "title";
404 std::string msg = "msg";
406 NSSyncTypes type = Notification_Read;
408 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
409 [](NSProvider *, NSMessage * message)
411 std::cout << "Income Notification : " << message->mId << std::endl;
412 NSConsumerDismissCheck(message);
413 std::unique_lock< std::mutex > lock{ mutexForCondition };
414 responseCon.wait_for(lock, g_waitForResponse);
417 mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
418 [& type](NSProvider *, NSSync * sync)
420 std::cout << "Income Notification : " << sync->mMessageId
421 << ", State : " << sync->mState << std::endl;
426 g_providerSimul.notifyMessage(id, title, msg);
428 std::unique_lock< std::mutex > lock{ mutexForCondition };
429 responseCon.wait_for(lock, g_waitForResponse);
432 g_providerSimul.deleteNotificationResource();
435 EXPECT_EQ(Notification_Dismiss, type);