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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include <UnitTestHelper.h>
23 #include <PresenceSubscriber.h>
24 #include <RCSException.h>
26 #include <OCPlatform.h>
28 using namespace OIC::Service;
30 typedef OCStackResult (*SubscribePresence1)(OC::OCPlatform::OCPresenceHandle&,
31 const std::string&, OCConnectivityType, SubscribeCallback);
32 typedef OCStackResult (*SubscribePresence2)(OC::OCPlatform::OCPresenceHandle&,
33 const std::string&, const std::string&, OCConnectivityType, SubscribeCallback);
35 const std::string HOST{ "host" };
36 const OCConnectivityType CONTYPE{ };
39 class PresenceSubscriberNonMemberTest: public TestWithMock
45 TEST_F(PresenceSubscriberNonMemberTest, OCPlatformSubscribePresenceWillBeCalled)
47 mocks.ExpectCallFuncOverload(
48 static_cast< SubscribePresence1 >(OC::OCPlatform::subscribePresence))
49 .With(_, HOST,CONTYPE, _).Return(OC_STACK_OK);
51 subscribePresence(handle, HOST, CONTYPE, SubscribeCallback());
54 TEST_F(PresenceSubscriberNonMemberTest, SubscribePresenceThrowsIfResultIsNotOK)
56 mocks.ExpectCallFuncOverload(
57 static_cast< SubscribePresence1>(OC::OCPlatform::subscribePresence))
58 .Return(OC_STACK_ERROR);
60 ASSERT_THROW(subscribePresence(handle, "", CONTYPE, SubscribeCallback()), PlatformException);
63 TEST_F(PresenceSubscriberNonMemberTest, OCPlatformUnsubscribePresenceWillBeCalled)
65 mocks.ExpectCallFuncOverload(OC::OCPlatform::unsubscribePresence).Return(OC_STACK_OK);
67 unsubscribePresence(handle);
70 TEST_F(PresenceSubscriberNonMemberTest, UnsubscribePresenceThrowIfResultIsNotOK)
72 mocks.ExpectCallFuncOverload(OC::OCPlatform::unsubscribePresence).Return(OC_STACK_ERROR);
74 ASSERT_THROW(unsubscribePresence(handle), PlatformException);
77 class PresenceSubscriberTest: public TestWithMock
81 mocks.OnCallFuncOverload(
82 static_cast< SubscribePresence1 >(OC::OCPlatform::subscribePresence)).Do(
84 [](OC::OCPlatform::OCPresenceHandle& handle, const std::string&,
85 OCConnectivityType, SubscribeCallback) -> OCStackResult
87 handle = reinterpret_cast<OC::OCPlatform::OCPresenceHandle>(1);
92 mocks.OnCallFunc(OC::OCPlatform::unsubscribePresence).Return(OC_STACK_OK);
97 TEST_F(PresenceSubscriberTest, IsNotSubscribingWhenCreatedWithDefaultConstructor)
99 PresenceSubscriber subscriber;
100 ASSERT_FALSE(subscriber.isSubscribing());
103 TEST_F(PresenceSubscriberTest, ConstructorCallOCPlatformSubscribe)
105 mocks.ExpectCallFuncOverload(
106 static_cast< SubscribePresence1 >(OC::OCPlatform::subscribePresence))
107 .With(_, HOST, CONTYPE, _).Return(OC_STACK_OK);
109 PresenceSubscriber subscriber{ HOST, CONTYPE, SubscribeCallback() };
112 TEST_F(PresenceSubscriberTest, ConstructorWithResourceTypeCallOCPlatformSubscribe)
114 const std::string resType { "resType" };
116 mocks.ExpectCallFuncOverload(
117 static_cast< SubscribePresence2 >(OC::OCPlatform::subscribePresence))
118 .With(_, HOST, resType, CONTYPE, _).Return(OC_STACK_OK);
120 PresenceSubscriber subscriber{ HOST, resType, CONTYPE, SubscribeCallback() };
123 TEST_F(PresenceSubscriberTest, ConstructorThrowsIfResultIsNotOK)
125 mocks.ExpectCallFuncOverload(
126 static_cast< SubscribePresence1 >(OC::OCPlatform::subscribePresence))
127 .Return(OC_STACK_ERROR);
129 ASSERT_THROW(PresenceSubscriber(HOST, CONTYPE, SubscribeCallback()), PlatformException);
132 TEST_F(PresenceSubscriberTest, IsSubscribingIfConstructedWithoutException)
134 PresenceSubscriber subscriber{ HOST, CONTYPE, SubscribeCallback() };
136 ASSERT_TRUE(subscriber.isSubscribing());
139 TEST_F(PresenceSubscriberTest, IsSubscribingOfMovedSubscriberReturnsFalse)
141 PresenceSubscriber subscriber{ HOST, CONTYPE, SubscribeCallback() };
143 PresenceSubscriber newSubscriber{ std::move(subscriber) };
145 ASSERT_FALSE(subscriber.isSubscribing());
148 TEST_F(PresenceSubscriberTest, IsSubscribingOfMovedSubscriberWithAssignmentReturnsFalse)
150 PresenceSubscriber subscriber{ HOST, CONTYPE, SubscribeCallback() };
152 PresenceSubscriber newSubscriber;
154 newSubscriber = std::move(subscriber);
156 ASSERT_FALSE(subscriber.isSubscribing());
159 TEST_F(PresenceSubscriberTest, UnsubscribeWillBeCalledWhenSubscriberIsDestoryed)
161 mocks.ExpectCallFunc(OC::OCPlatform::unsubscribePresence).Return(OC_STACK_OK);
163 PresenceSubscriber subscriber{ HOST, CONTYPE, SubscribeCallback() };