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));
156 TEST_F(NotificationConsumerTest, StopConsumerPositive)
158 EXPECT_EQ(NSStopConsumer(), NS_OK);
161 TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsumerFirst)
163 mocks.ExpectCallFunc(NSProviderChangedCallback).Do(
164 [this](NSProvider *, NSProviderState)
166 std::cout << "Call Discovered" << std::endl;
167 responseCon.notify_all();
170 NSStartConsumer(cfg);
172 g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER);
173 g_providerSimul.createNotificationResource();
175 std::unique_lock< std::mutex > lock{ mutexForCondition };
176 responseCon.wait_for(lock, g_waitForResponse);
179 g_providerSimul.deleteNotificationResource();
182 TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenStartedConsumerAfter)
184 g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER);
185 g_providerSimul.createNotificationResource();
187 std::unique_lock< std::mutex > lock{ mutexForCondition };
188 responseCon.wait_for(lock, g_waitForResponse);
191 mocks.ExpectCallFunc(NSProviderChangedCallback).Do(
192 [this](NSProvider * provider, NSProviderState)
194 std::cout << "Call Discovered" << std::endl;
195 g_provider = provider;
196 responseCon.notify_all();
199 NSStartConsumer(cfg);
201 std::unique_lock< std::mutex > lock{ mutexForCondition };
202 responseCon.wait_for(lock, g_waitForResponse);
206 TEST_F(NotificationConsumerTest, DiscoverProviderWithNonAccepterWhenRescan)
208 g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_CONSUMER);
209 mocks.ExpectCallFunc(NSProviderChangedCallback).Do(
210 [this](NSProvider * provider, NSProviderState)
212 std::cout << "Call Discovered" << std::endl;
213 g_provider = provider;
214 std::cout << g_provider->providerId << std::endl;
215 responseCon.notify_all();
220 std::unique_lock< std::mutex > lock{ mutexForCondition };
221 responseCon.wait_for(lock, g_waitForResponse);
226 TEST_F(NotificationConsumerTest, ExpectSubscribeSuccess)
228 // mocks.ExpectCallFunc(NSSubscriptionAcceptedCallback).Do(
231 // std::cout << "Income Accepted subscription : " << std::endl;
234 NSResult ret = NSSubscribe(g_provider);
235 std::unique_lock< std::mutex > lock{ mutexForCondition };
236 responseCon.wait_for(lock, g_waitForResponse);
238 EXPECT_EQ(NS_OK, ret);
241 TEST_F(NotificationConsumerTest, ExpectReceiveNotification)
244 std::string title = "title";
245 std::string msg = "msg";
247 mocks.ExpectCallFunc(NSNotificationReceivedCallbackEmpty).Do(
248 [](NSMessage * message)
250 std::cout << "Income Notification : " << message->messageId << std::endl;
253 g_providerSimul.notifyMessage(id, title, msg);
255 std::unique_lock< std::mutex > lock{ mutexForCondition };
256 responseCon.wait_for(lock, g_waitForResponse);
261 TEST_F(NotificationConsumerTest, ExpectReceiveNotificationWithAccepterisProvider)
264 std::string title = "title";
265 std::string msg = "msg";
267 g_providerSimul.setAccepter((int)NSSelector::NS_SELECTION_PROVIDER);
269 NSStartConsumer(cfg);
271 std::unique_lock< std::mutex > lock{ mutexForCondition };
272 responseCon.wait_for(lock, g_waitForResponse);
275 mocks.ExpectCallFunc(NSNotificationReceivedCallbackEmpty).Do(
276 [](NSMessage * message)
278 std::cout << "Income Notification : " << message->messageId << std::endl;
281 g_providerSimul.notifyMessage(id, title, msg);
283 std::unique_lock< std::mutex > lock{ mutexForCondition };
284 responseCon.wait_for(lock, g_waitForResponse);
286 // g_providerSimul.deleteNotificationResource();
290 TEST_F(NotificationConsumerTest, ExpectCallbackReadCheckWhenProviderNotifySync)
293 std::string title = "title";
294 std::string msg = "msg";
296 NSSyncType type = NS_SYNC_DELETED;
298 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
299 [](NSMessage * message)
301 std::cout << "Income Notification : " << message->messageId << std::endl;
304 mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
305 [& type](NSSyncInfo * sync)
307 std::cout << "Income SyncInfo : " << sync->messageId
308 << ", State : " << sync->state << std::endl;
313 g_providerSimul.notifyMessage(id, title, msg);
315 std::unique_lock< std::mutex > lock{ mutexForCondition };
316 responseCon.wait_for(lock, g_waitForResponse);
319 g_providerSimul.sendRead(id);
321 std::unique_lock< std::mutex > lock{ mutexForCondition };
322 responseCon.wait_for(lock, g_waitForResponse);
325 // g_providerSimul.deleteNotificationResource();
328 EXPECT_EQ(NS_SYNC_READ, type);
331 TEST_F(NotificationConsumerTest, ExpectCallbackDismissCheckWhenProviderNotifySync)
334 std::string title = "title";
335 std::string msg = "msg";
337 NSSyncType type = NS_SYNC_READ;
339 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
340 [](NSMessage * message)
342 std::cout << "Income Notification : " << message->messageId << std::endl;
345 mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
346 [& type](NSSyncInfo * sync)
348 std::cout << "Income Notification : " << sync->messageId
349 << ", State : " << sync->state << std::endl;
354 g_providerSimul.notifyMessage(id, title, msg);
356 std::unique_lock< std::mutex > lock{ mutexForCondition };
357 responseCon.wait_for(lock, g_waitForResponse);
360 g_providerSimul.sendDismiss(id);
362 std::unique_lock< std::mutex > lock{ mutexForCondition };
363 responseCon.wait_for(lock, g_waitForResponse);
366 // g_providerSimul.deleteNotificationResource();
369 EXPECT_EQ(NS_SYNC_DELETED, type);
372 TEST_F(NotificationConsumerTest, ExpectCallbackReadCheckWhenConsumerPostSync)
375 std::string title = "title";
376 std::string msg = "msg";
378 NSSyncType type = NS_SYNC_DELETED;
380 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
381 [](NSMessage * message)
383 std::cout << "Income Notification : " << message->messageId << std::endl;
384 NSConsumerSendSyncInfo(message->providerId, message->messageId, NS_SYNC_READ);
385 std::unique_lock< std::mutex > lock{ mutexForCondition };
386 responseCon.wait_for(lock, g_waitForResponse);
389 mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
390 [& type](NSSyncInfo * sync)
392 std::cout << "Income Notification : " << sync->messageId
393 << ", State : " << sync->state << std::endl;
398 g_providerSimul.notifyMessage(id, title, msg);
400 std::unique_lock< std::mutex > lock{ mutexForCondition };
401 responseCon.wait_for(lock, g_waitForResponse);
404 // g_providerSimul.deleteNotificationResource();
407 EXPECT_EQ(NS_SYNC_READ, type);
410 TEST_F(NotificationConsumerTest, ExpectCallbackDismissCheckWhenConsumerPostSync)
413 std::string title = "title";
414 std::string msg = "msg";
416 NSSyncType type = NS_SYNC_READ;
418 mocks.OnCallFunc(NSNotificationReceivedCallbackEmpty).Do(
419 [](NSMessage * message)
421 std::cout << "Income Notification : " << message->messageId << std::endl;
422 NSConsumerSendSyncInfo(message->providerId, message->messageId, NS_SYNC_DELETED);
423 std::unique_lock< std::mutex > lock{ mutexForCondition };
424 responseCon.wait_for(lock, g_waitForResponse);
427 mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
428 [& type](NSSyncInfo * sync)
430 std::cout << "Income Notification : " << sync->messageId
431 << ", State : " << sync->state << std::endl;
436 g_providerSimul.notifyMessage(id, title, msg);
438 std::unique_lock< std::mutex > lock{ mutexForCondition };
439 responseCon.wait_for(lock, g_waitForResponse);
442 EXPECT_EQ(NS_SYNC_DELETED, type);
445 TEST_F(NotificationConsumerTest, ExpectUnsubscribeSuccess)
447 NSResult ret = NSUnsubscribe(g_provider);
448 std::unique_lock< std::mutex > lock{ mutexForCondition };
449 responseCon.wait_for(lock, g_waitForResponse);
451 g_providerSimul.deleteNotificationResource();
454 EXPECT_EQ(NS_OK, ret);