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, StartConsumerPositive)
153 EXPECT_EQ(NS_OK, NSStartConsumer(cfg));
154 std::unique_lock< std::mutex > lock{ mutexForCondition };
155 responseCon.wait_for(lock, g_waitForResponse);
158 TEST_F(NotificationConsumerTest, StopConsumerPositive)
160 EXPECT_EQ(NSStopConsumer(), NS_OK);
163 TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsumerFirst)
165 mocks.ExpectCallFunc(NSProviderChangedCallback).Do(
166 [this](NSProvider *, NSProviderState)
168 std::cout << "Call Discovered" << std::endl;
169 responseCon.notify_all();
172 NSStartConsumer(cfg);
174 g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER);
175 g_providerSimul.createNotificationResource();
177 std::unique_lock< std::mutex > lock{ mutexForCondition };
178 responseCon.wait_for(lock, g_waitForResponse);
181 g_providerSimul.deleteNotificationResource();
184 TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsumerAfter)
186 g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER);
187 g_providerSimul.createNotificationResource();
189 std::unique_lock< std::mutex > lock{ mutexForCondition };
190 responseCon.wait_for(lock, g_waitForResponse);
193 mocks.ExpectCallFunc(NSProviderChangedCallback).Do(
194 [this](NSProvider * provider, NSProviderState)
196 std::cout << "Call Discovered" << std::endl;
197 g_provider = provider;
198 responseCon.notify_all();
201 NSStartConsumer(cfg);
203 std::unique_lock< std::mutex > lock{ mutexForCondition };
204 responseCon.wait_for(lock, g_waitForResponse);
208 TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenRescan)
210 g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER);
211 mocks.ExpectCallFunc(NSProviderChangedCallback).Do(
212 [this](NSProvider * provider, NSProviderState)
214 std::cout << "Call Discovered" << std::endl;
215 g_provider = provider;
216 std::cout << g_provider->providerId << std::endl;
217 responseCon.notify_all();
222 std::unique_lock< std::mutex > lock{ mutexForCondition };
223 responseCon.wait_for(lock, g_waitForResponse);
228 TEST_F(NotificationConsumerTest, ExpectSubscribeSuccess)
230 // mocks.ExpectCallFunc(NSSubscriptionAcceptedCallback).Do(
233 // std::cout << "Income Accepted subscription : " << std::endl;
236 NSResult ret = NSSubscribe(g_provider->providerId);
237 std::unique_lock< std::mutex > lock{ mutexForCondition };
238 responseCon.wait_for(lock, g_waitForResponse);
240 EXPECT_EQ(NS_OK, ret);
243 TEST_F(NotificationConsumerTest, ExpectReceiveNotification)
246 std::string title = "title";
247 std::string msg = "msg";
249 mocks.ExpectCallFunc(NSNotificationReceivedCallbackEmpty).Do(
250 [](NSMessage * message)
252 std::cout << "Income Notification : " << message->messageId << std::endl;
255 g_providerSimul.notifyMessage(id, title, msg);
257 std::unique_lock< std::mutex > lock{ mutexForCondition };
258 responseCon.wait_for(lock, g_waitForResponse);
263 TEST_F(NotificationConsumerTest, ExpectReceiveNotificationWithAccepterisProvider)
266 std::string title = "title";
267 std::string msg = "msg";
269 g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_PROVIDER);
271 NSStartConsumer(cfg);
273 std::unique_lock< std::mutex > lock{ mutexForCondition };
274 responseCon.wait_for(lock, g_waitForResponse);
277 mocks.ExpectCallFunc(NSNotificationReceivedCallbackEmpty).Do(
278 [](NSMessage * message)
280 std::cout << "Income Notification : " << message->messageId << std::endl;
283 g_providerSimul.notifyMessage(id, title, msg);
285 std::unique_lock< std::mutex > lock{ mutexForCondition };
286 responseCon.wait_for(lock, g_waitForResponse);
288 // g_providerSimul.deleteNotificationResource();
292 TEST_F(NotificationConsumerTest, ExpectCallbackReadCheckWhenProviderNotifySync)
295 std::string title = "title";
296 std::string msg = "msg";
298 NSSyncType type = NS_SYNC_DELETED;
300 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
301 [](NSMessage * message)
303 std::cout << "Income Notification : " << message->messageId << std::endl;
306 mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
307 [& type](NSSyncInfo * sync)
309 std::cout << "Income SyncInfo : " << sync->messageId
310 << ", State : " << sync->state << std::endl;
315 g_providerSimul.notifyMessage(id, title, msg);
317 std::unique_lock< std::mutex > lock{ mutexForCondition };
318 responseCon.wait_for(lock, g_waitForResponse);
321 g_providerSimul.sendRead(id);
323 std::unique_lock< std::mutex > lock{ mutexForCondition };
324 responseCon.wait_for(lock, g_waitForResponse);
327 // g_providerSimul.deleteNotificationResource();
330 EXPECT_EQ(NS_SYNC_READ, type);
333 TEST_F(NotificationConsumerTest, ExpectCallbackDismissCheckWhenProviderNotifySync)
336 std::string title = "title";
337 std::string msg = "msg";
339 NSSyncType type = NS_SYNC_READ;
341 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
342 [](NSMessage * message)
344 std::cout << "Income Notification : " << message->messageId << std::endl;
347 mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
348 [& type](NSSyncInfo * sync)
350 std::cout << "Income Notification : " << sync->messageId
351 << ", State : " << sync->state << std::endl;
356 g_providerSimul.notifyMessage(id, title, msg);
358 std::unique_lock< std::mutex > lock{ mutexForCondition };
359 responseCon.wait_for(lock, g_waitForResponse);
362 g_providerSimul.sendDismiss(id);
364 std::unique_lock< std::mutex > lock{ mutexForCondition };
365 responseCon.wait_for(lock, g_waitForResponse);
368 // g_providerSimul.deleteNotificationResource();
371 EXPECT_EQ(NS_SYNC_DELETED, type);
374 TEST_F(NotificationConsumerTest, ExpectCallbackReadCheckWhenConsumerPostSync)
377 std::string title = "title";
378 std::string msg = "msg";
380 NSSyncType type = NS_SYNC_DELETED;
382 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
383 [](NSMessage * message)
385 std::cout << "Income Notification : " << message->messageId << std::endl;
386 NSConsumerSendSyncInfo(message->providerId, message->messageId, NS_SYNC_READ);
387 std::unique_lock< std::mutex > lock{ mutexForCondition };
388 responseCon.wait_for(lock, g_waitForResponse);
391 mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
392 [& type](NSSyncInfo * sync)
394 std::cout << "Income Notification : " << sync->messageId
395 << ", State : " << sync->state << std::endl;
400 g_providerSimul.notifyMessage(id, title, msg);
402 std::unique_lock< std::mutex > lock{ mutexForCondition };
403 responseCon.wait_for(lock, g_waitForResponse);
406 // g_providerSimul.deleteNotificationResource();
409 EXPECT_EQ(NS_SYNC_READ, type);
412 TEST_F(NotificationConsumerTest, ExpectCallbackDismissCheckWhenConsumerPostSync)
415 std::string title = "title";
416 std::string msg = "msg";
418 NSSyncType type = NS_SYNC_READ;
420 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
421 [](NSMessage * message)
423 std::cout << "Income Notification : " << message->messageId << std::endl;
424 NSConsumerSendSyncInfo(message->providerId, message->messageId, NS_SYNC_DELETED);
425 std::unique_lock< std::mutex > lock{ mutexForCondition };
426 responseCon.wait_for(lock, g_waitForResponse);
429 mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
430 [& type](NSSyncInfo * sync)
432 std::cout << "Income Notification : " << sync->messageId
433 << ", State : " << sync->state << std::endl;
438 g_providerSimul.notifyMessage(id, title, msg);
440 std::unique_lock< std::mutex > lock{ mutexForCondition };
441 responseCon.wait_for(lock, g_waitForResponse);
444 g_providerSimul.deleteNotificationResource();
447 EXPECT_EQ(NS_SYNC_DELETED, type);
450 //TEST_F(NotificationConsumerTest, ExpectUnsubscribeSuccess)
452 // NSResult ret = NSUnsubscribe(g_provider->providerId);
453 // std::unique_lock< std::mutex > lock{ mutexForCondition };
454 // responseCon.wait_for(lock, g_waitForResponse);
456 // g_providerSimul.deleteNotificationResource();
459 // EXPECT_EQ(NS_OK, ret);