1 //******************************************************************
3 // Copyright 2015 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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 #include "gtest/gtest.h"
23 #include "HippoMocks/hippomocks.h"
25 #include "BrokerTypes.h"
26 #include "PrimitiveResource.h"
27 #include "ResponseStatement.h"
28 #include "OCPlatform.h"
29 #include "DevicePresence.h"
30 #include "ResourcePresence.h"
31 #include "UnitTestHelper.h"
33 using namespace testing;
34 using namespace OIC::Service;
37 typedef OCStackResult (*subscribePresenceSig1)(OC::OCPlatform::OCPresenceHandle&,
38 const std::string&, OCConnectivityType, SubscribeCallback);
40 class DevicePresenceTest : public TestWithMock
43 typedef std::function<void(OCStackResult,const unsigned int, const std::string&)> subscribeCallback;
44 DevicePresence * instance;
45 PrimitiveResource::Ptr pResource;
53 TestWithMock::SetUp();
54 instance = (DevicePresence*)new DevicePresence();
55 pResource = PrimitiveResource::Ptr(mocks.Mock< PrimitiveResource >(), [](PrimitiveResource*){});
56 cb = ([](BROKER_STATE)->OCStackResult{return OC_STACK_OK;});
62 TestWithMock::TearDown();
70 mocks.OnCall(pResource.get(), PrimitiveResource::getHost).Return(std::string());
71 mocks.OnCallFuncOverload(static_cast< subscribePresenceSig1 >(OC::OCPlatform::subscribePresence)).Return(OC_STACK_OK);
74 TEST_F(DevicePresenceTest,timeoutCB_TimeOverWhenIsSubscribe)
77 instance->initializeDevicePresence(pResource);
78 std::cout<<"wait while done timeout device presence\n";
79 sleep((BROKER_DEVICE_PRESENCE_TIMEROUT/1000)+1);
80 ASSERT_EQ(DEVICE_STATE::LOST_SIGNAL,instance->getDeviceState());
83 TEST_F(DevicePresenceTest,SubscribeCB_NormalHandlingIfMessageOC_STACK_OK)
85 mocks.OnCall(pResource.get(), PrimitiveResource::getHost).Return(std::string());
86 mocks.OnCallFuncOverload(static_cast< subscribePresenceSig1 >(OC::OCPlatform::subscribePresence)).Do(
87 [](OC::OCPlatform::OCPresenceHandle&,
88 const std::string&, OCConnectivityType, SubscribeCallback callback)->OCStackResult{
90 callback(OC_STACK_OK,0,std::string());
93 }).Return(OC_STACK_OK);
94 instance->initializeDevicePresence(pResource);
95 ASSERT_NE(DEVICE_STATE::LOST_SIGNAL,instance->getDeviceState());
98 TEST_F(DevicePresenceTest,initializeDevicePresence_NormalHandlingIfNormalResource)
103 ASSERT_NO_THROW(instance->initializeDevicePresence(pResource));
107 TEST_F(DevicePresenceTest,initializeDevicePresence_ErrorHandlingIfAbnormalResource)
111 mocks.OnCallFuncOverload(static_cast< subscribePresenceSig1 >(OC::OCPlatform::subscribePresence)).Return(OC_STACK_ERROR);
113 ASSERT_THROW(instance->initializeDevicePresence(pResource),PlatformException);
117 TEST_F(DevicePresenceTest,addPresenceResource_NormalHandlingIfNormalResource)
120 ResourcePresence * resource = (ResourcePresence *)new ResourcePresence();
121 instance->addPresenceResource(resource);
123 ASSERT_FALSE(instance->isEmptyResourcePresence());
127 TEST_F(DevicePresenceTest,isEmptyResourcePresence_NormalHandling)
132 ASSERT_TRUE(instance->isEmptyResourcePresence());
136 TEST_F(DevicePresenceTest,getAddress_NormalHandling)
141 instance->initializeDevicePresence(pResource);
142 instance->getAddress();
146 TEST_F(DevicePresenceTest,NormalHandlingWhenReceivedCallbackMessage)