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 NS_SELECTION_CONSUMER = 0,
51 NS_SELECTION_PROVIDER = 1
56 class TestWithMock: public testing::Test
62 virtual ~TestWithMock() noexcept(noexcept(std::declval<Test>().~Test()))
67 virtual void TearDown()
81 class NotificationConsumerTest : public TestWithMock
84 NotificationConsumerTest() = default;
85 ~NotificationConsumerTest() = default;
87 static void NSProviderDiscoveredCallbackEmpty(NSProvider *)
89 std::cout << __func__ << std::endl;
92 static void NSNotificationReceivedCallbackEmpty(NSMessage *)
94 std::cout << __func__ << std::endl;
97 static void NSSyncCallbackEmpty(NSSyncInfo *)
99 std::cout << __func__ << std::endl;
102 static void NSFoundResourceEmpty(std::shared_ptr< OC::OCResource >)
104 std::cout << __func__ << std::endl;
107 static void NSSubscriptionAcceptedCallback(NSProvider *)
109 std::cout << __func__ << std::endl;
116 TestWithMock::SetUp();
118 if (g_isStartedStack == false)
120 OC::PlatformConfig cfg
122 OC::ServiceType::InProc,
126 OC::QualityOfService::LowQos
128 OC::OCPlatform::Configure(cfg);
132 OC::OCPlatform::stopPresence();
139 g_isStartedStack = true;
146 TestWithMock::TearDown();
151 TEST_F(NotificationConsumerTest, StartConsumerPositive)
153 NSConsumerConfig cfg;
154 cfg.discoverCb = NSProviderDiscoveredCallbackEmpty;
155 cfg.acceptedCb = NSSubscriptionAcceptedCallback;
156 cfg.messageCb = NSNotificationReceivedCallbackEmpty;
157 cfg.syncInfoCb = NSSyncCallbackEmpty;
158 EXPECT_EQ(NS_OK, NSStartConsumer(cfg));
161 TEST_F(NotificationConsumerTest, StopConsumerPositive)
163 EXPECT_EQ(NSStopConsumer(), NS_OK);
166 TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsumerFirst)
168 mocks.ExpectCallFunc(NSProviderDiscoveredCallbackEmpty).Do(
171 std::cout << "Call Discovered" << std::endl;
172 responseCon.notify_all();
175 NSConsumerConfig cfg;
176 cfg.discoverCb = NSProviderDiscoveredCallbackEmpty;
177 cfg.acceptedCb = NSSubscriptionAcceptedCallback;
178 cfg.messageCb = NSNotificationReceivedCallbackEmpty;
179 cfg.syncInfoCb = NSSyncCallbackEmpty;
180 NSStartConsumer(cfg);
182 g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER);
183 g_providerSimul.createNotificationResource();
185 std::unique_lock< std::mutex > lock{ mutexForCondition };
186 responseCon.wait_for(lock, g_waitForResponse);
189 g_providerSimul.deleteNotificationResource();
192 TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsumerAfter)
194 g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER);
195 g_providerSimul.createNotificationResource();
197 std::unique_lock< std::mutex > lock{ mutexForCondition };
198 responseCon.wait_for(lock, g_waitForResponse);
201 mocks.ExpectCallFunc(NSProviderDiscoveredCallbackEmpty).Do(
202 [this](NSProvider * provider)
204 std::cout << "Call Discovered" << std::endl;
205 g_provider = provider;
206 responseCon.notify_all();
209 NSConsumerConfig cfg;
210 cfg.discoverCb = NSProviderDiscoveredCallbackEmpty;
211 cfg.acceptedCb = NSSubscriptionAcceptedCallback;
212 cfg.messageCb = NSNotificationReceivedCallbackEmpty;
213 cfg.syncInfoCb = NSSyncCallbackEmpty;
214 NSStartConsumer(cfg);
216 std::unique_lock< std::mutex > lock{ mutexForCondition };
217 responseCon.wait_for(lock, g_waitForResponse);
221 TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenRescan)
223 g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER);
224 mocks.ExpectCallFunc(NSProviderDiscoveredCallbackEmpty).Do(
225 [this](NSProvider * provider)
227 std::cout << "Call Discovered" << std::endl;
228 g_provider = provider;
229 std::cout << g_provider->providerId << std::endl;
230 responseCon.notify_all();
235 std::unique_lock< std::mutex > lock{ mutexForCondition };
236 responseCon.wait_for(lock, g_waitForResponse);
241 TEST_F(NotificationConsumerTest, ExpectSubscribeSuccess)
243 // mocks.ExpectCallFunc(NSSubscriptionAcceptedCallback).Do(
246 // std::cout << "Income Accepted subscription : " << std::endl;
249 NSResult ret = NSSubscribe(g_provider);
250 std::unique_lock< std::mutex > lock{ mutexForCondition };
251 responseCon.wait_for(lock, g_waitForResponse);
253 EXPECT_EQ(NS_OK, ret);
256 TEST_F(NotificationConsumerTest, ExpectReceiveNotification)
259 std::string title = "title";
260 std::string msg = "msg";
262 mocks.ExpectCallFunc(NSNotificationReceivedCallbackEmpty).Do(
263 [](NSMessage * message)
265 std::cout << "Income Notification : " << message->messageId << std::endl;
268 g_providerSimul.notifyMessage(id, title, msg);
270 std::unique_lock< std::mutex > lock{ mutexForCondition };
271 responseCon.wait_for(lock, g_waitForResponse);
276 TEST_F(NotificationConsumerTest, ExpectReceiveNotificationWithAccepterisProvider)
279 std::string title = "title";
280 std::string msg = "msg";
282 g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_PROVIDER);
284 NSConsumerConfig cfg;
285 cfg.discoverCb = NSProviderDiscoveredCallbackEmpty;
286 cfg.acceptedCb = NSSubscriptionAcceptedCallback;
287 cfg.messageCb = NSNotificationReceivedCallbackEmpty;
288 cfg.syncInfoCb = NSSyncCallbackEmpty;
289 NSStartConsumer(cfg);
291 std::unique_lock< std::mutex > lock{ mutexForCondition };
292 responseCon.wait_for(lock, g_waitForResponse);
295 mocks.ExpectCallFunc(NSNotificationReceivedCallbackEmpty).Do(
296 [](NSMessage * message)
298 std::cout << "Income Notification : " << message->messageId << std::endl;
301 g_providerSimul.notifyMessage(id, title, msg);
303 std::unique_lock< std::mutex > lock{ mutexForCondition };
304 responseCon.wait_for(lock, g_waitForResponse);
306 // g_providerSimul.deleteNotificationResource();
310 TEST_F(NotificationConsumerTest, ExpectCallbackReadCheckWhenProviderNotifySync)
313 std::string title = "title";
314 std::string msg = "msg";
316 NSSyncType type = NS_SYNC_DELETED;
318 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
319 [](NSMessage * message)
321 std::cout << "Income Notification : " << message->messageId << std::endl;
324 mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
325 [& type](NSSyncInfo * sync)
327 std::cout << "Income SyncInfo : " << sync->messageId
328 << ", State : " << sync->state << std::endl;
333 g_providerSimul.notifyMessage(id, title, msg);
335 std::unique_lock< std::mutex > lock{ mutexForCondition };
336 responseCon.wait_for(lock, g_waitForResponse);
339 g_providerSimul.sendRead(id);
341 std::unique_lock< std::mutex > lock{ mutexForCondition };
342 responseCon.wait_for(lock, g_waitForResponse);
345 // g_providerSimul.deleteNotificationResource();
348 EXPECT_EQ(NS_SYNC_READ, type);
351 TEST_F(NotificationConsumerTest, ExpectCallbackDismissCheckWhenProviderNotifySync)
354 std::string title = "title";
355 std::string msg = "msg";
357 NSSyncType type = NS_SYNC_READ;
359 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
360 [](NSMessage * message)
362 std::cout << "Income Notification : " << message->messageId << std::endl;
365 mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
366 [& type](NSSyncInfo * sync)
368 std::cout << "Income Notification : " << sync->messageId
369 << ", State : " << sync->state << std::endl;
374 g_providerSimul.notifyMessage(id, title, msg);
376 std::unique_lock< std::mutex > lock{ mutexForCondition };
377 responseCon.wait_for(lock, g_waitForResponse);
380 g_providerSimul.sendDismiss(id);
382 std::unique_lock< std::mutex > lock{ mutexForCondition };
383 responseCon.wait_for(lock, g_waitForResponse);
386 // g_providerSimul.deleteNotificationResource();
389 EXPECT_EQ(NS_SYNC_DELETED, type);
392 TEST_F(NotificationConsumerTest, ExpectCallbackReadCheckWhenConsumerPostSync)
395 std::string title = "title";
396 std::string msg = "msg";
398 NSSyncType type = NS_SYNC_DELETED;
400 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
401 [](NSMessage * message)
403 std::cout << "Income Notification : " << message->messageId << std::endl;
404 NSConsumerSendSyncInfo(message->providerId, message->messageId, NS_SYNC_READ);
405 std::unique_lock< std::mutex > lock{ mutexForCondition };
406 responseCon.wait_for(lock, g_waitForResponse);
409 mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
410 [& type](NSSyncInfo * sync)
412 std::cout << "Income Notification : " << sync->messageId
413 << ", State : " << sync->state << std::endl;
418 g_providerSimul.notifyMessage(id, title, msg);
420 std::unique_lock< std::mutex > lock{ mutexForCondition };
421 responseCon.wait_for(lock, g_waitForResponse);
424 // g_providerSimul.deleteNotificationResource();
427 EXPECT_EQ(NS_SYNC_READ, type);
430 TEST_F(NotificationConsumerTest, ExpectCallbackDismissCheckWhenConsumerPostSync)
433 std::string title = "title";
434 std::string msg = "msg";
436 NSSyncType type = NS_SYNC_READ;
438 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
439 [](NSMessage * message)
441 std::cout << "Income Notification : " << message->messageId << std::endl;
442 NSConsumerSendSyncInfo(message->providerId, message->messageId, NS_SYNC_DELETED);
443 std::unique_lock< std::mutex > lock{ mutexForCondition };
444 responseCon.wait_for(lock, g_waitForResponse);
447 mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
448 [& type](NSSyncInfo * sync)
450 std::cout << "Income Notification : " << sync->messageId
451 << ", State : " << sync->state << std::endl;
456 g_providerSimul.notifyMessage(id, title, msg);
458 std::unique_lock< std::mutex > lock{ mutexForCondition };
459 responseCon.wait_for(lock, g_waitForResponse);
462 EXPECT_EQ(NS_SYNC_DELETED, type);
465 TEST_F(NotificationConsumerTest, ExpectUnsubscribeSuccess)
467 NSResult ret = NSUnsubscribe(g_provider);
468 std::unique_lock< std::mutex > lock{ mutexForCondition };
469 responseCon.wait_for(lock, g_waitForResponse);
471 g_providerSimul.deleteNotificationResource();
474 EXPECT_EQ(NS_OK, ret);