Merge branch 'upstream' into tizen
[platform/upstream/iotivity.git] / service / scene-manager / unittests / RemoteSceneCollectionTest.cpp
1 //******************************************************************
2 //
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include <mutex>
22 #include <condition_variable>
23
24 #include "RemoteSceneList.h"
25
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
33 using namespace std;
34 using namespace OIC::Service;
35 using namespace OC;
36
37 constexpr int DEFAULT_WAITTIME = 2000;
38
39 class RemoteSceneCollectionTest : public TestWithMock
40 {
41 protected:
42     void SetUp()
43     {
44         TestWithMock::SetUp();
45
46         SceneList::getInstance()->getName();
47         createListServer();
48
49         RemoteSceneList::createInstance(pListResource, std::bind(
50             &RemoteSceneCollectionTest::onRemoteSceneListCreated, this,
51             placeholders::_1, placeholders::_2));
52
53         waitForCallback();
54     }
55
56     void createListServer()
57     {
58         std::vector< std::string > vecRT{ SCENE_LIST_RT };
59         std::vector< std::string > vecIF{ OC_RSRVD_INTERFACE_DEFAULT, OC::BATCH_INTERFACE };
60
61         pListResource = SceneUtils::createRCSResourceObject(
62             "coap://" + SceneUtils::getNetAddress() + SCENE_LIST_URI,
63             SCENE_CONNECTIVITY, vecRT, vecIF);
64     }
65
66     void waitForCallback(int waitingTime = DEFAULT_WAITTIME)
67     {
68         std::unique_lock< std::mutex > lock{ mutex };
69         cond.wait_for(lock, std::chrono::milliseconds{ waitingTime });
70     }
71
72 public:
73     RCSRemoteResourceObject::Ptr pListResource;
74     RemoteSceneList::Ptr pSceneList;
75     RemoteSceneCollection::Ptr pSceneCollection;
76     std::condition_variable cond;
77     std::mutex mutex;
78
79     void onRemoteSceneListCreated(RemoteSceneList::Ptr remoteSceneList, int)
80     {
81         pSceneList = std::move(remoteSceneList);
82         cond.notify_all();
83     }
84
85     void onRemoteSceneCollectionCreated(RemoteSceneCollection::Ptr remoteSceneCol, int)
86     {
87         pSceneCollection = remoteSceneCol;
88         cond.notify_all();
89     }
90
91     void onRemoteSceneCreated(RemoteScene::Ptr, int)
92     {
93         cond.notify_all();
94     }
95
96     void onSetName(int)
97     {
98         cond.notify_all();
99     }
100 };
101
102 TEST_F(RemoteSceneCollectionTest, addNewRemoteSceneCollection)
103 {
104     pSceneList->addNewSceneCollection(std::bind(
105         &RemoteSceneCollectionTest::onRemoteSceneCollectionCreated, this,
106         placeholders::_1, placeholders::_2));
107
108     waitForCallback();
109
110     pSceneCollection->addNewScene("Default", std::bind(
111         &RemoteSceneCollectionTest::onRemoteSceneCreated, this,
112         placeholders::_1, placeholders::_2));
113
114     waitForCallback();
115
116     EXPECT_NE(nullptr, pSceneCollection);
117 }
118
119 TEST_F(RemoteSceneCollectionTest, getRemoteSceneCollectionList)
120 {
121     std::vector< RemoteSceneCollection::Ptr > sceneCollections
122         = pSceneList->getRemoteSceneCollections();
123
124     bool getCollectionsOK = true;
125
126     if (!sceneCollections.empty())
127     {
128         for (const auto &it : sceneCollections)
129         {
130             if (it->getId() == "")
131             {
132                 getCollectionsOK = false;
133                 break;
134             }
135         }
136     }
137     else
138     {
139         getCollectionsOK = false;
140     }
141
142     ASSERT_TRUE(getCollectionsOK);
143 }
144
145 TEST_F(RemoteSceneCollectionTest, setAndGetSceneCollectionName)
146 {
147     pSceneList->addNewSceneCollection(std::bind(
148         &RemoteSceneCollectionTest::onRemoteSceneCollectionCreated, this,
149         placeholders::_1, placeholders::_2));
150
151     waitForCallback();
152
153     pSceneCollection->addNewScene("Default", std::bind(
154         &RemoteSceneCollectionTest::onRemoteSceneCreated, this,
155         placeholders::_1, placeholders::_2));
156
157     waitForCallback();
158
159     pSceneCollection->setName("Kitchen", std::bind(
160         &RemoteSceneCollectionTest::onSetName, this, placeholders::_1));
161
162     waitForCallback();
163
164     EXPECT_EQ("Kitchen", pSceneCollection->getName());
165 }