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>
29 #include "NSConsumerSimulator.h"
32 #include "NSProviderInterface.h"
36 std::atomic_bool g_isStartedStack(false);
38 std::chrono::milliseconds g_waitForResponse(500);
40 std::condition_variable responseCon;
41 std::mutex mutexForCondition;
43 NSConsumerSimulator g_consumerSimul;
44 NSConsumer * g_consumer;
48 class TestWithMock: public testing::Test
54 virtual ~TestWithMock() noexcept(noexcept(std::declval<Test>().~Test())) {}
56 virtual void TearDown() {
69 class NotificationProviderTest : public TestWithMock
72 NotificationProviderTest() = default;
73 ~NotificationProviderTest() = default;
75 static void NSRequestedSubscribeCallbackEmpty(NSConsumer *)
77 std::cout << __func__ << std::endl;
80 static void NSSyncCallbackEmpty(NSSync *)
82 std::cout << __func__ << std::endl;
85 static void NSMessageCallbackFromConsumerEmpty(
86 const std::string &, const std::string &, const std::string &)
88 std::cout << __func__ << std::endl;
91 static void NSSyncCallbackFromConsumerEmpty(int, const std::string &)
93 std::cout << __func__ << std::endl;
100 TestWithMock::SetUp();
102 if (g_isStartedStack == false)
104 OC::PlatformConfig cfg
106 OC::ServiceType::InProc,
110 OC::QualityOfService::LowQos
112 OC::OCPlatform::Configure(cfg);
116 OC::OCPlatform::stopPresence();
123 g_isStartedStack = true;
130 TestWithMock::TearDown();
135 TEST_F(NotificationProviderTest, StartProviderPositive)
137 NSResult ret = NSStartProvider(NS_ACCEPTER_PROVIDER,
138 NSRequestedSubscribeCallbackEmpty,
139 NSSyncCallbackEmpty);
141 std::unique_lock< std::mutex > lock{ mutexForCondition };
142 responseCon.wait_for(lock, g_waitForResponse);
144 EXPECT_EQ(ret, NS_OK);
147 TEST_F(NotificationProviderTest, StopProviderPositive)
149 NSResult ret = NSStopProvider();
151 std::unique_lock< std::mutex > lock{ mutexForCondition };
152 responseCon.wait_for(lock, g_waitForResponse);
154 EXPECT_EQ(ret, NS_OK);
157 TEST_F(NotificationProviderTest, ExpectCallbackWhenReceiveSubscribeRequestWithAccepterProvider)
159 mocks.ExpectCallFunc(NSRequestedSubscribeCallbackEmpty).Do(
160 [](NSConsumer * consumer)
162 std::cout << "NSRequestedSubscribeCallback" << std::endl;
163 g_consumer = consumer;
164 responseCon.notify_all();
167 NSStartProvider(NS_ACCEPTER_PROVIDER,
168 NSRequestedSubscribeCallbackEmpty, NSSyncCallbackEmpty);
171 std::unique_lock< std::mutex > lock{ mutexForCondition };
172 responseCon.wait_for(lock, g_waitForResponse);
175 g_consumerSimul.setCallback(NSMessageCallbackFromConsumerEmpty,
176 NSSyncCallbackFromConsumerEmpty);
177 g_consumerSimul.findProvider();
179 std::unique_lock< std::mutex > lock{ mutexForCondition };
180 responseCon.wait_for(lock, std::chrono::milliseconds(1000));
183 TEST_F(NotificationProviderTest, NeverCallNotifyOnConsumerByAcceptIsFalse)
185 bool expectTrue = true;
187 mocks.OnCallFunc(NSMessageCallbackFromConsumerEmpty).Do(
188 [& expectTrue](const std::string &id, const std::string&, const std::string&)
190 if (id == "NeverCallNotifyOnConsumerByAcceptIsFalse")
192 std::cout << "This function never call" << std::endl;
197 NSAccept(g_consumer, false);
199 NSMessage * msg = new NSMessage();
200 msg->mId = strdup(std::string("NeverCallNotifyOnConsumerByAcceptIsFalse").c_str());
201 msg->mTitle = strdup(std::string("Title").c_str());
202 msg->mContentText = strdup(std::string("ContentText").c_str());
203 NSSendNotification(msg);
205 std::unique_lock< std::mutex > lock{ mutexForCondition };
206 responseCon.wait_for(lock, g_waitForResponse);
209 std::unique_lock< std::mutex > lock{ mutexForCondition };
210 responseCon.wait_for(lock, std::chrono::milliseconds(1000));
212 EXPECT_EQ(expectTrue, true);
215 TEST_F(NotificationProviderTest, ExpectCallNotifyOnConsumerByAcceptIsTrue)
217 mocks.ExpectCallFunc(NSMessageCallbackFromConsumerEmpty).Do(
218 [](const std::string &id, const std::string&, const std::string&)
220 if (id == "ExpectCallNotifyOnConsumerByAcceptIsTrue")
222 std::cout << "ExpectCallNotifyOnConsumerByAcceptIsTrue" << std::endl;
223 responseCon.notify_all();
227 NSAccept(g_consumer, true);
229 NSMessage * msg = new NSMessage();
230 msg->mId = strdup(std::string("ExpectCallNotifyOnConsumerByAcceptIsTrue").c_str());
231 msg->mTitle = strdup(std::string("Title").c_str());
232 msg->mContentText = strdup(std::string("ContentText").c_str());
233 NSSendNotification(msg);
235 std::unique_lock< std::mutex > lock{ mutexForCondition };
236 responseCon.wait_for(lock, g_waitForResponse);
239 std::unique_lock< std::mutex > lock{ mutexForCondition };
240 responseCon.wait_for(lock, g_waitForResponse);
243 //TEST_F(NotificationProviderTest, ExpectCallbackSyncOnReadToConsumer)
246 // std::string id = "ExpectCallNotifyOnConsumerByAcceptIsTrue";
247 // mocks.ExpectCallFunc(NSSyncCallbackFromConsumerEmpty).Do(
248 // [& id](int type, const std::string & syncId)
250 // std::cout << "NSSyncCallbackEmpty" << std::endl;
251 // if (syncId == id &&
252 // type == Notification_Read)
254 // std::cout << "ExpectCallbackSyncOnReadFromConsumer" << std::endl;
255 // responseCon.notify_all();
259 // NSMessage * msg = new NSMessage();
260 // msg->mId = strdup(std::string("ExpectCallNotifyOnConsumerByAcceptIsTrue").c_str());
261 // msg->mTitle = strdup(std::string("Title").c_str());
262 // msg->mContentText = strdup(std::string("ContentText").c_str());
263 // NSProviderReadCheck(msg);
264 // std::unique_lock< std::mutex > lock{ mutexForCondition };
265 // responseCon.wait_for(lock, std::chrono::milliseconds(5000));
268 //TEST_F(NotificationProviderTest, ExpectCallbackSyncOnReadFromConsumer)
271 // std::string id("ExpectCallNotifyOnConsumerByAcceptIsTrue");
272 // mocks.ExpectCallFunc(NSSyncCallbackEmpty).Do(
273 // [& id](NSSync * sync)
275 // std::cout << "NSSyncCallbackEmpty" << std::endl;
276 // if (sync->mMessageId == id && sync->mState == Notification_Read)
278 // std::cout << "ExpectCallbackSyncOnReadFromConsumer" << std::endl;
279 // responseCon.notify_all();
283 // g_consumerSimul.syncToProvider(type, std::string(id));
284 // std::unique_lock< std::mutex > lock{ mutexForCondition };
285 // responseCon.wait_for(lock, std::chrono::milliseconds(5000));