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()))
61 virtual void TearDown()
75 class NotificationConsumerTest : public TestWithMock
78 NotificationConsumerTest() = default;
79 ~NotificationConsumerTest() = default;
81 static void NSProviderDiscoveredCallbackEmpty(NSProvider *)
83 std::cout << __func__ << std::endl;
86 static void NSNotificationReceivedCallbackEmpty(NSMessage *)
88 std::cout << __func__ << std::endl;
91 static void NSSyncCallbackEmpty(NSSyncInfo *)
93 std::cout << __func__ << std::endl;
96 static void foundResourceEmpty(std::shared_ptr< OC::OCResource >)
98 std::cout << __func__ << std::endl;
101 static void SubscriptionAcceptedCallback(NSProvider *)
103 std::cout << __func__ << std::endl;
110 TestWithMock::SetUp();
112 if (g_isStartedStack == false)
114 OC::PlatformConfig cfg
116 OC::ServiceType::InProc,
120 OC::QualityOfService::LowQos
122 OC::OCPlatform::Configure(cfg);
126 OC::OCPlatform::stopPresence();
133 g_isStartedStack = true;
140 TestWithMock::TearDown();
145 TEST_F(NotificationConsumerTest, StartConsumerPositive)
147 NSConsumerConfig cfg;
148 cfg.discoverCb = NSProviderDiscoveredCallbackEmpty;
149 cfg.acceptedCb = SubscriptionAcceptedCallback;
150 cfg.messageCb = NSNotificationReceivedCallbackEmpty;
151 cfg.syncInfoCb = NSSyncCallbackEmpty;
152 EXPECT_EQ(NS_OK, NSStartConsumer(cfg));
155 TEST_F(NotificationConsumerTest, StopConsumerPositive)
157 EXPECT_EQ(NSStopConsumer(), NS_OK);
160 TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsumerFirst)
162 mocks.ExpectCallFunc(NSProviderDiscoveredCallbackEmpty).Do(
165 std::cout << "Call Discovered" << std::endl;
166 responseCon.notify_all();
169 NSConsumerConfig cfg;
170 cfg.discoverCb = NSProviderDiscoveredCallbackEmpty;
171 cfg.acceptedCb = SubscriptionAcceptedCallback;
172 cfg.messageCb = NSNotificationReceivedCallbackEmpty;
173 cfg.syncInfoCb = NSSyncCallbackEmpty;
174 NSStartConsumer(cfg);
176 g_providerSimul.setAccepter(1);
177 g_providerSimul.createNotificationResource();
179 std::unique_lock< std::mutex > lock{ mutexForCondition };
180 responseCon.wait_for(lock, g_waitForResponse);
183 g_providerSimul.deleteNotificationResource();
186 TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsumerAfter)
188 g_providerSimul.setAccepter(1);
189 g_providerSimul.createNotificationResource();
191 std::unique_lock< std::mutex > lock{ mutexForCondition };
192 responseCon.wait_for(lock, g_waitForResponse);
195 mocks.ExpectCallFunc(NSProviderDiscoveredCallbackEmpty).Do(
196 [this](NSProvider * provider)
198 std::cout << "Call Discovered" << std::endl;
199 g_provider = provider;
200 responseCon.notify_all();
203 NSConsumerConfig cfg;
204 cfg.discoverCb = NSProviderDiscoveredCallbackEmpty;
205 cfg.acceptedCb = SubscriptionAcceptedCallback;
206 cfg.messageCb = NSNotificationReceivedCallbackEmpty;
207 cfg.syncInfoCb = NSSyncCallbackEmpty;
208 NSStartConsumer(cfg);
210 std::unique_lock< std::mutex > lock{ mutexForCondition };
211 responseCon.wait_for(lock, g_waitForResponse);
215 TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenRescan)
217 g_providerSimul.setAccepter(1);
218 mocks.ExpectCallFunc(NSProviderDiscoveredCallbackEmpty).Do(
219 [this](NSProvider * provider)
221 std::cout << "Call Discovered" << std::endl;
222 g_provider = provider;
223 std::cout << g_provider->providerId << std::endl;
224 responseCon.notify_all();
229 std::unique_lock< std::mutex > lock{ mutexForCondition };
230 responseCon.wait_for(lock, g_waitForResponse);
235 TEST_F(NotificationConsumerTest, ExpectSubscribeSuccess)
237 mocks.ExpectCallFunc(SubscriptionAcceptedCallback).Do(
240 std::cout << "Income Accepted subscription : " << std::endl;
243 NSResult ret = NSSubscribe(g_provider);
244 std::unique_lock< std::mutex > lock{ mutexForCondition };
245 responseCon.wait_for(lock, g_waitForResponse);
247 EXPECT_EQ(NS_OK, ret);
250 TEST_F(NotificationConsumerTest, ExpectReceiveNotification)
253 std::string title = "title";
254 std::string msg = "msg";
256 mocks.ExpectCallFunc(NSNotificationReceivedCallbackEmpty).Do(
257 [](NSMessage * message)
259 std::cout << "Income Notification : " << message->messageId << std::endl;
262 g_providerSimul.notifyMessage(id, title, msg);
264 std::unique_lock< std::mutex > lock{ mutexForCondition };
265 responseCon.wait_for(lock, g_waitForResponse);
270 TEST_F(NotificationConsumerTest, ExpectReceiveNotificationWithAccepterisProvider)
273 std::string title = "title";
274 std::string msg = "msg";
276 g_providerSimul.setAccepter(0);
278 NSConsumerConfig cfg;
279 cfg.discoverCb = NSProviderDiscoveredCallbackEmpty;
280 cfg.acceptedCb = SubscriptionAcceptedCallback;
281 cfg.messageCb = NSNotificationReceivedCallbackEmpty;
282 cfg.syncInfoCb = NSSyncCallbackEmpty;
283 NSStartConsumer(cfg);
285 std::unique_lock< std::mutex > lock{ mutexForCondition };
286 responseCon.wait_for(lock, g_waitForResponse);
289 mocks.ExpectCallFunc(NSNotificationReceivedCallbackEmpty).Do(
290 [](NSMessage * message)
292 std::cout << "Income Notification : " << message->messageId << std::endl;
295 g_providerSimul.notifyMessage(id, title, msg);
297 std::unique_lock< std::mutex > lock{ mutexForCondition };
298 responseCon.wait_for(lock, g_waitForResponse);
300 // g_providerSimul.deleteNotificationResource();
304 TEST_F(NotificationConsumerTest, ExpectCallbackReadCheckWhenProviderNotifySync)
307 std::string title = "title";
308 std::string msg = "msg";
310 NSSyncType type = NS_SYNC_DELETED;
312 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
313 [](NSMessage * message)
315 std::cout << "Income Notification : " << message->messageId << std::endl;
318 mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
319 [& type](NSSyncInfo * sync)
321 std::cout << "Income SyncInfo : " << sync->messageId
322 << ", State : " << sync->state << std::endl;
327 g_providerSimul.notifyMessage(id, title, msg);
329 std::unique_lock< std::mutex > lock{ mutexForCondition };
330 responseCon.wait_for(lock, g_waitForResponse);
333 g_providerSimul.sendRead(id);
335 std::unique_lock< std::mutex > lock{ mutexForCondition };
336 responseCon.wait_for(lock, g_waitForResponse);
339 // g_providerSimul.deleteNotificationResource();
342 EXPECT_EQ(NS_SYNC_READ, type);
345 TEST_F(NotificationConsumerTest, ExpectCallbackDismissCheckWhenProviderNotifySync)
348 std::string title = "title";
349 std::string msg = "msg";
351 NSSyncType type = NS_SYNC_READ;
353 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
354 [](NSMessage * message)
356 std::cout << "Income Notification : " << message->messageId << std::endl;
359 mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
360 [& type](NSSyncInfo * sync)
362 std::cout << "Income Notification : " << sync->messageId
363 << ", State : " << sync->state << std::endl;
368 g_providerSimul.notifyMessage(id, title, msg);
370 std::unique_lock< std::mutex > lock{ mutexForCondition };
371 responseCon.wait_for(lock, g_waitForResponse);
374 g_providerSimul.sendDismiss(id);
376 std::unique_lock< std::mutex > lock{ mutexForCondition };
377 responseCon.wait_for(lock, g_waitForResponse);
380 // g_providerSimul.deleteNotificationResource();
383 EXPECT_EQ(NS_SYNC_DELETED, type);
386 TEST_F(NotificationConsumerTest, ExpectCallbackReadCheckWhenConsumerPostSync)
389 std::string title = "title";
390 std::string msg = "msg";
392 NSSyncType type = NS_SYNC_DELETED;
394 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
395 [](NSMessage * message)
397 std::cout << "Income Notification : " << message->messageId << std::endl;
398 NSConsumerSendSyncInfo(message->providerId, message->messageId, NS_SYNC_READ);
399 std::unique_lock< std::mutex > lock{ mutexForCondition };
400 responseCon.wait_for(lock, g_waitForResponse);
403 mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
404 [& type](NSSyncInfo * sync)
406 std::cout << "Income Notification : " << sync->messageId
407 << ", State : " << sync->state << std::endl;
412 g_providerSimul.notifyMessage(id, title, msg);
414 std::unique_lock< std::mutex > lock{ mutexForCondition };
415 responseCon.wait_for(lock, g_waitForResponse);
418 // g_providerSimul.deleteNotificationResource();
421 EXPECT_EQ(NS_SYNC_READ, type);
424 TEST_F(NotificationConsumerTest, ExpectCallbackDismissCheckWhenConsumerPostSync)
427 std::string title = "title";
428 std::string msg = "msg";
430 NSSyncType type = NS_SYNC_READ;
432 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
433 [](NSMessage * message)
435 std::cout << "Income Notification : " << message->messageId << std::endl;
436 NSConsumerSendSyncInfo(message->providerId, message->messageId, NS_SYNC_DELETED);
437 std::unique_lock< std::mutex > lock{ mutexForCondition };
438 responseCon.wait_for(lock, g_waitForResponse);
441 mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
442 [& type](NSSyncInfo * sync)
444 std::cout << "Income Notification : " << sync->messageId
445 << ", State : " << sync->state << std::endl;
450 g_providerSimul.notifyMessage(id, title, msg);
452 std::unique_lock< std::mutex > lock{ mutexForCondition };
453 responseCon.wait_for(lock, g_waitForResponse);
456 EXPECT_EQ(NS_SYNC_DELETED, type);
459 TEST_F(NotificationConsumerTest, ExpectUnsubscribeSuccess)
461 NSResult ret = NSUnsubscribe(g_provider);
462 std::unique_lock< std::mutex > lock{ mutexForCondition };
463 responseCon.wait_for(lock, g_waitForResponse);
465 g_providerSimul.deleteNotificationResource();
468 EXPECT_EQ(NS_OK, ret);