From 07482c7ba63fb7006a17f3682b231329acd563f4 Mon Sep 17 00:00:00 2001 From: Rami Jung Date: Sun, 2 Aug 2015 16:10:58 +0900 Subject: [PATCH] [Resource-Encapsulation] improving unittest - adding more functions - modification for existing functions Change-Id: I898221305bac3c2e2779ecca9f836a8fac57f627 Signed-off-by: Rami Jung Reviewed-on: https://gerrit.iotivity.org/gerrit/2041 Tested-by: jenkins-iotivity Reviewed-by: Madan Lanka --- .../unittests/RCSResourceObjectTest.cpp | 81 +++++++++++++--------- 1 file changed, 47 insertions(+), 34 deletions(-) diff --git a/service/resource-encapsulation/src/serverBuilder/unittests/RCSResourceObjectTest.cpp b/service/resource-encapsulation/src/serverBuilder/unittests/RCSResourceObjectTest.cpp index 64b0fd7..33cfaa6 100755 --- a/service/resource-encapsulation/src/serverBuilder/unittests/RCSResourceObjectTest.cpp +++ b/service/resource-encapsulation/src/serverBuilder/unittests/RCSResourceObjectTest.cpp @@ -587,50 +587,47 @@ public: } }; -TEST_F(AttributeUpdatedListenerTest, AddListenerReturnsTrueIfListenerIsCalled) +class FunctionsForAttributeUpdatedListener { - bool called=false; +public: + virtual void fCalled(const OIC::Service::RCSResourceAttributes::Value&, + const OIC::Service::RCSResourceAttributes::Value&)=0; + virtual void fNotCalled(const OIC::Service::RCSResourceAttributes::Value&, + const OIC::Service::RCSResourceAttributes::Value&)=0; +}; + +TEST_F(AttributeUpdatedListenerTest, AddListenerRunsAddedFunction) +{ + FunctionsForAttributeUpdatedListener *ptrMock = + mocks.Mock(); server->setAttribute(KEY, 0); - OCRepresentation ocRep = createOCRepresentation(); - server->addAttributeUpdatedListener(KEY, - [&called](const OIC::Service::RCSResourceAttributes::Value&, - const OIC::Service::RCSResourceAttributes::Value& ) - { - called=true; - } ); + mocks.ExpectCall(ptrMock, FunctionsForAttributeUpdatedListener::fCalled); - handler(ResourceObjectHandlingRequestTest::createRequest(OC_REST_PUT, ocRep)); + server->addAttributeUpdatedListener(KEY, + (std::bind(&FunctionsForAttributeUpdatedListener::fCalled, ptrMock, _1, _2))); - ASSERT_TRUE(called); + handler(createRequest(OC_REST_PUT, createOCRepresentation())); } -TEST_F(AttributeUpdatedListenerTest, AddListenerisChangedAccordingToLastAddedFunction) +TEST_F(AttributeUpdatedListenerTest, AddListenerRunsAccordingToLastAddedFunction) { - int called=0, expected=100; - string myKey("key"); // 'myKey' and 'constexpr char KEY[]' should have same value + FunctionsForAttributeUpdatedListener *ptrMock = + mocks.Mock(); - server->setAttribute(KEY,0); - OCRepresentation ocRep = createOCRepresentation(); + string duplicateKEY(KEY); + server->setAttribute(KEY, 0); - server->addAttributeUpdatedListener(myKey, - [&called](const OIC::Service::RCSResourceAttributes::Value&, - const OIC::Service::RCSResourceAttributes::Value&) - { - called=10; - } ); + mocks.ExpectCall(ptrMock, FunctionsForAttributeUpdatedListener::fCalled); + mocks.NeverCall(ptrMock, FunctionsForAttributeUpdatedListener::fNotCalled); + server->addAttributeUpdatedListener(duplicateKEY, + (std::bind(&FunctionsForAttributeUpdatedListener::fNotCalled, ptrMock, _1, _2))); server->addAttributeUpdatedListener(KEY, - [&called](const OIC::Service::RCSResourceAttributes::Value&, - const OIC::Service::RCSResourceAttributes::Value&) - { - called=100; - } ); + (std::bind(&FunctionsForAttributeUpdatedListener::fCalled, ptrMock, _1, _2))); - handler(ResourceObjectHandlingRequestTest::createRequest(OC_REST_PUT, ocRep)); - - ASSERT_EQ(expected, called); + handler(createRequest(OC_REST_PUT, createOCRepresentation())); } TEST_F(AttributeUpdatedListenerTest, RemoveListenerReturnsTrueIfListenerIsNotAdded) @@ -640,13 +637,29 @@ TEST_F(AttributeUpdatedListenerTest, RemoveListenerReturnsTrueIfListenerIsNotAdd TEST_F(AttributeUpdatedListenerTest, RemoveListenerReturnsTrueIfListenerIsAdded) { + FunctionsForAttributeUpdatedListener *ptrMock = + mocks.Mock(); + server->addAttributeUpdatedListener(KEY, - [](const OIC::Service::RCSResourceAttributes::Value&, - const OIC::Service::RCSResourceAttributes::Value&) - { - } ); + (std::bind(&FunctionsForAttributeUpdatedListener::fNotCalled, ptrMock, _1, _2))); ASSERT_TRUE(server->removeAttributeUpdatedListener(KEY)); } +TEST_F(AttributeUpdatedListenerTest, RemoveListenerNeverRunsRemovedFunc) +{ + FunctionsForAttributeUpdatedListener *ptrMock = + mocks.Mock(); + + mocks.NeverCall(ptrMock, FunctionsForAttributeUpdatedListener::fNotCalled); + + server->setAttribute(KEY, 0); + server->addAttributeUpdatedListener(KEY, + (std::bind(&FunctionsForAttributeUpdatedListener::fNotCalled, ptrMock, _1, _2))); + server->removeAttributeUpdatedListener(KEY); + + handler(createRequest(OC_REST_PUT, createOCRepresentation())); +} + + -- 2.7.4