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
57 class TestWithMock: public testing::Test
63 virtual ~TestWithMock() noexcept(noexcept(std::declval<Test>().~Test()))
68 virtual void TearDown()
82 class NotificationConsumerTest : public TestWithMock
85 NotificationConsumerTest() = default;
86 ~NotificationConsumerTest() = default;
88 static void NSNotificationReceivedCallbackEmpty(NSMessage *)
90 std::cout << __func__ << std::endl;
93 static void NSSyncCallbackEmpty(NSSyncInfo *)
95 std::cout << __func__ << std::endl;
98 static void NSFoundResourceEmpty(std::shared_ptr< OC::OCResource >)
100 std::cout << __func__ << std::endl;
103 static void NSProviderChangedCallback(NSProvider *, NSProviderState)
105 std::cout << __func__ << std::endl;
112 TestWithMock::SetUp();
114 if (g_isStartedStack == false)
116 OC::PlatformConfig occfg
118 OC::ServiceType::InProc,
122 OC::QualityOfService::LowQos
124 OC::OCPlatform::Configure(occfg);
128 OC::OCPlatform::stopPresence();
135 g_isStartedStack = true;
137 cfg.changedCb = NSProviderChangedCallback;
138 cfg.messageCb = NSNotificationReceivedCallbackEmpty;
139 cfg.syncInfoCb = NSSyncCallbackEmpty;
146 TestWithMock::TearDown();
151 TEST_F(NotificationConsumerTest, StartConsumerNegativeNonSetChangedCB)
153 cfg.changedCb = NULL;
154 cfg.messageCb = NSNotificationReceivedCallbackEmpty;
155 cfg.syncInfoCb = NSSyncCallbackEmpty;
157 EXPECT_EQ(NS_ERROR, NSStartConsumer(cfg));
158 std::unique_lock< std::mutex > lock{ mutexForCondition };
159 responseCon.wait_for(lock, g_waitForResponse);
162 TEST_F(NotificationConsumerTest, StartConsumerNegativeNonSetNotiReceiveCB)
164 cfg.changedCb = NSProviderChangedCallback;
165 cfg.messageCb = NULL;
166 cfg.syncInfoCb = NSSyncCallbackEmpty;
168 EXPECT_EQ(NS_ERROR, NSStartConsumer(cfg));
169 std::unique_lock< std::mutex > lock{ mutexForCondition };
170 responseCon.wait_for(lock, g_waitForResponse);
173 TEST_F(NotificationConsumerTest, StartConsumerNegativeNonSetSyncCB)
175 cfg.changedCb = NSProviderChangedCallback;
176 cfg.messageCb = NSNotificationReceivedCallbackEmpty;
177 cfg.syncInfoCb = NULL;
179 EXPECT_EQ(NS_ERROR, NSStartConsumer(cfg));
180 std::unique_lock< std::mutex > lock{ mutexForCondition };
181 responseCon.wait_for(lock, g_waitForResponse);
184 TEST_F(NotificationConsumerTest, StartConsumerPositive)
186 cfg.changedCb = NSProviderChangedCallback;
187 cfg.messageCb = NSNotificationReceivedCallbackEmpty;
188 cfg.syncInfoCb = NSSyncCallbackEmpty;
190 EXPECT_EQ(NS_OK, NSStartConsumer(cfg));
191 std::unique_lock< std::mutex > lock{ mutexForCondition };
192 responseCon.wait_for(lock, g_waitForResponse);
195 TEST_F(NotificationConsumerTest, StopConsumerPositive)
197 EXPECT_EQ(NSStopConsumer(), NS_OK);
200 TEST_F(NotificationConsumerTest, StopConsumerNegative)
202 EXPECT_EQ(NSStopConsumer(), NS_ERROR);
205 TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsumerFirst)
207 NSProviderState revState = NS_STOPPED;
208 mocks.OnCallFunc(NSProviderChangedCallback).Do(
209 [this, & revState](NSProvider *, NSProviderState state)
211 std::cout << "Call Discovered" << std::endl;
213 responseCon.notify_all();
216 NSStartConsumer(cfg);
218 g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER);
219 g_providerSimul.createNotificationResource();
221 std::unique_lock< std::mutex > lock{ mutexForCondition };
222 responseCon.wait_for(lock, g_waitForResponse);
225 g_providerSimul.deleteNotificationResource();
227 EXPECT_EQ(NS_DISCOVERED, revState);
230 TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsumerAfter)
232 g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER);
233 g_providerSimul.createNotificationResource();
235 std::unique_lock< std::mutex > lock{ mutexForCondition };
236 responseCon.wait_for(lock, g_waitForResponse);
239 NSProviderState revState = NS_STOPPED;
240 mocks.OnCallFunc(NSProviderChangedCallback).Do(
241 [this, & revState](NSProvider * provider, NSProviderState state)
243 std::cout << "Call Discovered" << std::endl;
245 g_provider = provider;
247 responseCon.notify_all();
250 NSStartConsumer(cfg);
252 std::unique_lock< std::mutex > lock{ mutexForCondition };
253 responseCon.wait_for(lock, g_waitForResponse);
255 EXPECT_EQ(NS_DISCOVERED, revState);
258 TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenRescan)
260 g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER);
261 NSProviderState revState = NS_STOPPED;
262 mocks.OnCallFunc(NSProviderChangedCallback).Do(
263 [this, &revState](NSProvider * provider, NSProviderState state)
265 std::cout << "Call Discovered" << std::endl;
267 g_provider = provider;
268 std::cout << g_provider->providerId << std::endl;
269 responseCon.notify_all();
274 std::unique_lock< std::mutex > lock{ mutexForCondition };
275 responseCon.wait_for(lock, g_waitForResponse);
277 EXPECT_EQ(NS_DISCOVERED, revState);
280 TEST_F(NotificationConsumerTest, ExpectSubscribeSuccess)
282 NSProviderState revState = NS_DENY;
283 mocks.OnCallFunc(NSProviderChangedCallback).Do(
284 [this, & revState](NSProvider * , NSProviderState state)
286 std::cout << "Income Changed Callback : " << state << std::endl;
288 responseCon.notify_all();
291 NSResult ret = NSSubscribe(g_provider->providerId);
292 std::unique_lock< std::mutex > lock{ mutexForCondition };
293 responseCon.wait_for(lock, g_waitForResponse);
295 EXPECT_EQ(NS_ALLOW, revState);
296 EXPECT_EQ(NS_OK, ret);
299 TEST_F(NotificationConsumerTest, ExpectReceiveNotification)
302 std::string title = "title";
303 std::string msg = "msg";
305 mocks.ExpectCallFunc(NSNotificationReceivedCallbackEmpty).Do(
306 [this](NSMessage * message)
308 std::cout << "Income Notification : " << message->messageId << std::endl;
311 g_providerSimul.notifyMessage(id, title, msg);
313 std::unique_lock< std::mutex > lock{ mutexForCondition };
314 responseCon.wait_for(lock, g_waitForResponse);
319 TEST_F(NotificationConsumerTest, ExpectReceiveSubAllowWithAccepterisProvider)
321 g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_PROVIDER);
322 NSProviderState revState = NS_DENY;
324 mocks.OnCallFunc(NSProviderChangedCallback).Do(
325 [this, & revState](NSProvider * , NSProviderState state)
327 std::cout << "Income Changed Callback : " << state << std::endl;
329 responseCon.notify_all();
332 NSStartConsumer(cfg);
333 std::unique_lock< std::mutex > lock{ mutexForCondition };
334 responseCon.wait_for(lock, g_waitForResponse);
336 EXPECT_EQ(NS_ALLOW, revState);
339 TEST_F(NotificationConsumerTest, ExpectReceiveNotificationWithAccepterisProvider)
342 std::string title = "title";
343 std::string msg = "msg";
346 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
347 [this, & id, & revId](NSMessage * message)
349 std::cout << "Income Notification : " << message->messageId << std::endl;
350 revId = message->messageId;
353 g_providerSimul.notifyMessage(id, title, msg);
355 std::unique_lock< std::mutex > lock{ mutexForCondition };
356 responseCon.wait_for(lock, g_waitForResponse);
358 EXPECT_EQ(id, revId);
361 TEST_F(NotificationConsumerTest, ExpectCallbackReadCheckWhenProviderNotifySync)
364 std::string title = "title";
365 std::string msg = "msg";
366 NSSyncType type = NS_SYNC_DELETED;
368 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
369 [this](NSMessage * message)
371 std::cout << "Income Notification : " << message->messageId << std::endl;
374 mocks.OnCallFunc(NSSyncCallbackEmpty).Do(
375 [& type, this](NSSyncInfo * sync)
377 std::cout << "Income SyncInfo : " << sync->messageId
378 << ", State : " << sync->state << std::endl;
382 g_providerSimul.notifyMessage(id, title, msg);
384 std::unique_lock< std::mutex > lock{ mutexForCondition };
385 responseCon.wait_for(lock, g_waitForResponse);
388 g_providerSimul.sendRead(id);
390 std::unique_lock< std::mutex > lock{ mutexForCondition };
391 responseCon.wait_for(lock, g_waitForResponse);
394 EXPECT_EQ(NS_SYNC_READ, type);
397 TEST_F(NotificationConsumerTest, ExpectCallbackDismissCheckWhenProviderNotifySync)
400 std::string title = "title";
401 std::string msg = "msg";
402 NSSyncType type = NS_SYNC_READ;
404 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
405 [this](NSMessage * message)
407 std::cout << "Income Notification : " << message->messageId << std::endl;
410 mocks.OnCallFunc(NSSyncCallbackEmpty).Do(
411 [& type, this](NSSyncInfo * sync)
413 std::cout << "Income Notification : " << sync->messageId
414 << ", 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.sendDismiss(id);
426 std::unique_lock< std::mutex > lock{ mutexForCondition };
427 responseCon.wait_for(lock, g_waitForResponse);
430 EXPECT_EQ(NS_SYNC_DELETED, type);
433 TEST_F(NotificationConsumerTest, ExpectCallbackReadCheckWhenConsumerPostSync)
436 std::string title = "title";
437 std::string msg = "msg";
438 NSSyncType type = NS_SYNC_DELETED;
440 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
441 [this](NSMessage * message)
443 std::cout << "Income Notification : " << message->messageId << std::endl;
444 NSConsumerSendSyncInfo(message->providerId, message->messageId, NS_SYNC_READ);
445 std::unique_lock< std::mutex > lock{ mutexForCondition };
446 responseCon.wait_for(lock, g_waitForResponse);
449 mocks.OnCallFunc(NSSyncCallbackEmpty).Do(
450 [& type, this](NSSyncInfo * sync)
452 std::cout << "Income Notification : " << sync->messageId
453 << ", State : " << sync->state << std::endl;
457 g_providerSimul.notifyMessage(id, title, msg);
459 std::unique_lock< std::mutex > lock{ mutexForCondition };
460 responseCon.wait_for(lock, g_waitForResponse);
463 EXPECT_EQ(NS_SYNC_READ, type);
466 TEST_F(NotificationConsumerTest, ExpectCallbackDismissCheckWhenConsumerPostSync)
469 std::string title = "title";
470 std::string msg = "msg";
471 NSSyncType state = NS_SYNC_READ;
473 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
474 [this](NSMessage * message)
476 std::cout << "Income Notification : " << message->messageId << std::endl;
477 NSConsumerSendSyncInfo(message->providerId, message->messageId, NS_SYNC_DELETED);
478 std::unique_lock< std::mutex > lock{ mutexForCondition };
479 responseCon.wait_for(lock, g_waitForResponse);
482 mocks.OnCallFunc(NSSyncCallbackEmpty).Do(
483 [& state, this](NSSyncInfo * sync)
485 std::cout << "Income Notification : " << sync->messageId
486 << ", State : " << sync->state << std::endl;
490 g_providerSimul.notifyMessage(id, title, msg);
492 std::unique_lock< std::mutex > lock{ mutexForCondition };
493 responseCon.wait_for(lock, g_waitForResponse);
496 EXPECT_EQ(NS_SYNC_DELETED, state);
497 // g_providerSimul.deleteNotificationResource();
501 TEST_F(NotificationConsumerTest, ExpectCallbackDeletedProvider)
503 NSProviderState type = NS_ALLOW;
504 mocks.OnCallFunc(NSProviderChangedCallback).Do(
505 [& type, this](NSProvider * , NSProviderState state)
507 std::cout << "Income Changed Callback : " << state << std::endl;
508 //EXPECT_EQ(state, NS_STOPPED);
510 responseCon.notify_all();
513 g_providerSimul.deleteNotificationResource();
515 std::unique_lock< std::mutex > lock{ mutexForCondition };
516 responseCon.wait_for(lock, g_waitForResponse);
518 EXPECT_EQ(type, NS_STOPPED);
522 //TEST_F(NotificationConsumerTest, ExpectUnsubscribeSuccess)
524 // NSResult ret = NSUnsubscribe(g_provider->providerId);
525 // std::unique_lock< std::mutex > lock{ mutexForCondition };
526 // responseCon.wait_for(lock, g_waitForResponse);
528 // g_providerSimul.deleteNotificationResource();
531 // EXPECT_EQ(NS_OK, ret);