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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 #include <condition_variable>
24 #include "RemoteSceneList.h"
26 #include "UnitTestHelper.h"
27 #include "SceneCommons.h"
28 #include "SceneList.h"
29 #include "RCSResourceObject.h"
30 #include "RCSRemoteResourceObject.h"
31 #include "OCPlatform.h"
32 #include "RCSDiscoveryManager.h"
33 #include "RCSAddress.h"
36 using namespace OIC::Service;
39 constexpr int DEFAULT_WAITTIME = 2000;
41 SceneList* g_sceneList = SceneList::getInstance();
42 RCSRemoteResourceObject::Ptr pListResource = nullptr;
43 RemoteSceneList::Ptr pSceneList = nullptr;
44 RemoteSceneCollection::Ptr pSceneCollection = nullptr;
45 RemoteScene::Ptr pScene = nullptr;
47 void discoverSceneListServer()
49 if(pListResource == nullptr)
51 std::vector< std::string > vecRT{ SCENE_LIST_RT };
52 std::vector< std::string > vecIF{ OC_RSRVD_INTERFACE_DEFAULT, OC::BATCH_INTERFACE };
54 pListResource = SceneUtils::createRCSResourceObject(
55 "coap://" + SceneUtils::getNetAddress() + SCENE_LIST_URI,
56 SCENE_CONNECTIVITY, vecRT, vecIF);
60 class RemoteSceneCollectionTest : public TestWithMock
65 TestWithMock::SetUp();
67 g_sceneList->getName();
68 discoverSceneListServer();
70 if(pSceneList == nullptr){
71 RemoteSceneList::createInstance(pListResource, std::bind(
72 &RemoteSceneCollectionTest::onRemoteSceneListCreated, this,
73 placeholders::_1, placeholders::_2));
78 void waitForCallback(int waitingTime = DEFAULT_WAITTIME)
80 std::unique_lock< std::mutex > lock{ mutex };
81 cond.wait_for(lock, std::chrono::milliseconds{ waitingTime });
85 std::condition_variable cond;
88 void onRemoteSceneListCreated(RemoteSceneList::Ptr remoteSceneList, int)
90 pSceneList = std::move(remoteSceneList);
94 void onRemoteSceneCollectionCreated(RemoteSceneCollection::Ptr remoteSceneCol, int)
96 pSceneCollection = remoteSceneCol;
100 void onRemoteSceneCreated(RemoteScene::Ptr remoteScene, int)
102 pScene = remoteScene;
112 TEST_F(RemoteSceneCollectionTest, addNewRemoteSceneCollection)
114 if(pSceneCollection == nullptr)
116 pSceneList->addNewSceneCollection(std::bind(
117 &RemoteSceneCollectionTest::onRemoteSceneCollectionCreated, this,
118 placeholders::_1, placeholders::_2));
122 if(pScene == nullptr)
124 pSceneCollection->addNewScene("Default", std::bind(
125 &RemoteSceneCollectionTest::onRemoteSceneCreated, this,
126 placeholders::_1, placeholders::_2));
130 EXPECT_NE(nullptr, pSceneCollection);
133 TEST_F(RemoteSceneCollectionTest, getRemoteSceneCollectionList)
135 std::vector< RemoteSceneCollection::Ptr > sceneCollections
136 = pSceneList->getRemoteSceneCollections();
138 bool getCollectionsOK = true;
140 if (!sceneCollections.empty())
142 for (const auto &it : sceneCollections)
144 if (it->getId() == "")
146 getCollectionsOK = false;
153 getCollectionsOK = false;
156 ASSERT_TRUE(getCollectionsOK);
159 TEST_F(RemoteSceneCollectionTest, setAndGetSceneCollectionName)
161 if(pSceneCollection == nullptr)
163 pSceneList->addNewSceneCollection(std::bind(
164 &RemoteSceneCollectionTest::onRemoteSceneCollectionCreated, this,
165 placeholders::_1, placeholders::_2));
169 if(pScene == nullptr)
171 pSceneCollection->addNewScene("Default", std::bind(
172 &RemoteSceneCollectionTest::onRemoteSceneCreated, this,
173 placeholders::_1, placeholders::_2));
177 pSceneCollection->setName("Kitchen", std::bind(
178 &RemoteSceneCollectionTest::onSetName, this, placeholders::_1));
182 EXPECT_EQ("Kitchen", pSceneCollection->getName());