File: test_SensorImpls.cpp

package info (click to toggle)
hueplusplus 1.2.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,372 kB
  • sloc: cpp: 14,028; ansic: 70; makefile: 12
file content (392 lines) | stat: -rw-r--r-- 12,036 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/**
    \file test_SensorImpls.cpp
    Copyright Notice\n
    Copyright (C) 2020  Jan Rogall	- developer\n

    This file is part of hueplusplus.

    hueplusplus is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    hueplusplus is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with hueplusplus.  If not, see <http://www.gnu.org/licenses/>.
**/

#include <hueplusplus/CLIPSensors.h>
#include <hueplusplus/ZLLSensors.h>

#include <gtest/gtest.h>

#include "testhelper.h"

#include "mocks/mock_HttpHandler.h"

using namespace testing;
using namespace hueplusplus;
using namespace hueplusplus::sensors;

// Many sensor classes contain duplicate methods, with the type parameterized tests at least the test cases
// do not have to be duplicated
template <typename T>
class SensorImplTest : public Test
{
protected:
    std::shared_ptr<MockHttpHandler> handler;
    HueCommandAPI commands;
    nlohmann::json state;

protected:
    SensorImplTest()
        : handler(std::make_shared<MockHttpHandler>()),
          commands(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler),
          state({{"type", T::typeStr}, {"config", nlohmann::json::object()}, {"state", nlohmann::json::object()}})
    { }

    void expectConfigSet(const std::string& key, const nlohmann::json& value)
    {
        EXPECT_CALL(*handler,
            PUTJson("/api/" + getBridgeUsername() + "/sensors/1/config", nlohmann::json({{key, value}}), getBridgeIp(),
                getBridgePort()))
            .WillOnce(Return(nlohmann::json {{{"success", {{"/sensors/1/config/" + key, value}}}}}));
    }
    void expectStateSet(const std::string& key, const nlohmann::json& value)
    {
        EXPECT_CALL(*handler,
            PUTJson("/api/" + getBridgeUsername() + "/sensors/1/state", nlohmann::json({{key, value}}), getBridgeIp(),
                getBridgePort()))
            .WillOnce(Return(nlohmann::json {{{"success", {{"/sensors/1/state/" + key, value}}}}}));
    }

    T getSensor()
    {
        EXPECT_CALL(*handler, GETJson("/api/" + getBridgeUsername() + "/sensors/1", _, getBridgeIp(), getBridgePort()))
            .WillOnce(Return(state));
        return T(Sensor(1, commands, std::chrono::steady_clock::duration::max(), nullptr));
    }
};

// Sensors with shared methods

template <typename T>
class SensorOnTest : public SensorImplTest<T>
{ };
// Only need to test one CLIP type, because they share the basic methods
using SensorOnTypes
    = Types<CLIPSwitch, ZGPSwitch, ZLLSwitch, ZLLPresence, ZLLTemperature, ZLLLightLevel, DaylightSensor>;
TYPED_TEST_SUITE(SensorOnTest, SensorOnTypes);

template <typename T>
class SensorBatteryTest : public SensorImplTest<T>
{ };
using SensorBatteryTypes = Types<CLIPSwitch, ZLLSwitch, ZLLPresence, ZLLTemperature, ZLLLightLevel, DaylightSensor>;
TYPED_TEST_SUITE(SensorBatteryTest, SensorBatteryTypes);

template <typename T>
class SensorReachableTest : public SensorImplTest<T>
{ };
using SensorReachableTypes = Types<CLIPSwitch, ZLLSwitch, ZLLPresence, ZLLTemperature, ZLLLightLevel>;
TYPED_TEST_SUITE(SensorReachableTest, SensorReachableTypes);

template <typename T>
class SensorUpdateTest : public SensorImplTest<T>
{ };
using SensorUpdateTypes = Types<CLIPSwitch, ZLLSwitch, ZLLPresence, ZLLLightLevel, ZLLTemperature, DaylightSensor>;
TYPED_TEST_SUITE(SensorUpdateTest, SensorUpdateTypes);

template <typename T>
class SensorAlertTest : public SensorImplTest<T>
{ };
using SensorAlertTypes = Types<ZLLSwitch, ZLLPresence, ZLLTemperature>;
TYPED_TEST_SUITE(SensorAlertTest, SensorAlertTypes);

template <typename T>
class SensorButtonTest : public SensorImplTest<T>
{ };
using SensorButtonTypes = Types<CLIPSwitch, ZLLSwitch, ZGPSwitch>;
TYPED_TEST_SUITE(SensorButtonTest, SensorButtonTypes);

template <typename T>
class SensorTemperatureTest : public SensorImplTest<T>
{ };
using SensorTemperatureTypes = Types<CLIPTemperature, ZLLTemperature>;
TYPED_TEST_SUITE(SensorTemperatureTest, SensorTemperatureTypes);

template <typename T>
class SensorLightLevelTest : public SensorImplTest<T>
{ };
using SensorLightLevelTypes = Types<CLIPLightLevel, ZLLLightLevel>;
TYPED_TEST_SUITE(SensorLightLevelTest, SensorLightLevelTypes);

template <typename T>
class SensorPresenceTest : public SensorImplTest<T>
{ };
using SensorPresenceTypes = Types<CLIPPresence, ZLLPresence>;
TYPED_TEST_SUITE(SensorPresenceTest, SensorPresenceTypes);

// Sensors with unique methods

class DaylightSensorTest : public SensorImplTest<DaylightSensor>
{ };

class ZLLPresenceTest : public SensorImplTest<ZLLPresence>
{ };

class CLIPSwitchTest : public SensorImplTest<CLIPSwitch>
{ };

class CLIPOpenCloseTest : public SensorImplTest<CLIPOpenClose>
{ };

class CLIPPresenceTest : public SensorImplTest<CLIPPresence>
{ };

class CLIPTemperatureTest : public SensorImplTest<CLIPTemperature>
{ };

class CLIPHumidityTest : public SensorImplTest<CLIPHumidity>
{ };

class CLIPGenericFlagTest : public SensorImplTest<CLIPGenericFlag>
{ };

class CLIPGenericStatusTest : public SensorImplTest<CLIPGenericStatus>
{ };

TYPED_TEST(SensorOnTest, on)
{
    this->state["config"]["on"] = false;
    EXPECT_FALSE(this->getSensor().isOn());
    this->state["config"]["on"] = true;
    EXPECT_TRUE(this->getSensor().isOn());

    this->expectConfigSet("on", false);
    this->getSensor().setOn(false);
}

TYPED_TEST(SensorBatteryTest, BatteryState)
{
    EXPECT_FALSE(this->getSensor().hasBatteryState());
    this->state["config"]["battery"] = 90;
    EXPECT_TRUE(this->getSensor().hasBatteryState());
    EXPECT_EQ(90, this->getSensor().getBatteryState());
}

TYPED_TEST(SensorReachableTest, Reachable)
{
    this->state["config"]["reachable"] = true;
    EXPECT_TRUE(this->getSensor().isReachable());
}

TYPED_TEST(SensorUpdateTest, getLastUpdated)
{
    time::AbsoluteTime none = this->getSensor().getLastUpdated();
    EXPECT_EQ(std::chrono::seconds(0), none.getBaseTime().time_since_epoch());

    const std::string timestamp = "2020-05-02T12:00:01";
    this->state["state"]["lastupdated"] = timestamp;
    time::AbsoluteTime time = time::AbsoluteTime::parseUTC(timestamp);
    EXPECT_EQ(time.getBaseTime(), this->getSensor().getLastUpdated().getBaseTime());
}

TYPED_TEST(SensorAlertTest, Alert)
{
    this->state["config"]["alert"] = "none";
    EXPECT_EQ(Alert::none, this->getSensor().getLastAlert());

    this->expectConfigSet("alert", "lselect");
    this->getSensor().sendAlert(Alert::lselect);
}

TYPED_TEST(SensorButtonTest, ButtonEvent)
{
    int code = 12;
    this->state["state"]["buttonevent"] = code;
    EXPECT_EQ(code, this->getSensor().getButtonEvent());
}

TYPED_TEST(SensorTemperatureTest, Temperature)
{
    int temperature = 1200;
    this->state["state"]["temperature"] = temperature;
    EXPECT_EQ(temperature, this->getSensor().getTemperature());
}

TYPED_TEST(SensorLightLevelTest, LightLevel)
{
    int lightLevel = 1200;
    this->state["state"] = {{"lightlevel", lightLevel}, {"dark", true}, {"daylight", false}};
    EXPECT_EQ(lightLevel, this->getSensor().getLightLevel());
    EXPECT_TRUE(this->getSensor().isDark());
    EXPECT_FALSE(this->getSensor().isDaylight());
}

TYPED_TEST(SensorLightLevelTest, DarkThreshold)
{
    int darkThreshold = 12000;
    this->state["config"]["tholddark"] = darkThreshold;
    EXPECT_EQ(darkThreshold, this->getSensor().getDarkThreshold());

    int newThreshold = 10;
    this->expectConfigSet("tholddark", newThreshold);
    this->getSensor().setDarkThreshold(newThreshold);
}

TYPED_TEST(SensorLightLevelTest, ThresholdOffset)
{
    int offset = 12000;
    this->state["config"]["tholdoffset"] = offset;
    EXPECT_EQ(offset, this->getSensor().getThresholdOffset());

    int newOffset = 10;
    this->expectConfigSet("tholdoffset", newOffset);
    this->getSensor().setThresholdOffset(newOffset);
}

TYPED_TEST(SensorPresenceTest, Presence)
{
    this->state["state"]["presence"] = true;
    EXPECT_TRUE(this->getSensor().getPresence());
}

TEST_F(DaylightSensorTest, Coordinates)
{
    const std::string lat = "000.0000N";
    const std::string lon = "000.0000E";
    EXPECT_CALL(*handler,
        PUTJson("/api/" + getBridgeUsername() + "/sensors/1/config", nlohmann::json({{"lat", lat}, {"long", lon}}),
            getBridgeIp(), getBridgePort()))
        .WillOnce(Return(nlohmann::json {
            {{"success", {{"/sensors/1/config/lat", lat}}}}, {{"success", {{"/sensors/1/config/long", lon}}}}}));
    getSensor().setCoordinates(lat, lon);
    state["config"]["configured"] = true;
    EXPECT_TRUE(getSensor().isConfigured());
}

TEST_F(DaylightSensorTest, SunriseOffset)
{
    int offset = 10;
    state["config"]["sunriseoffset"] = offset;
    EXPECT_EQ(offset, getSensor().getSunriseOffset());

    int newOffset = 20;
    expectConfigSet("sunriseoffset", newOffset);
    getSensor().setSunriseOffset(newOffset);
}

TEST_F(DaylightSensorTest, SunsetOffset)
{
    int offset = 10;
    state["config"]["sunsetoffset"] = offset;
    EXPECT_EQ(offset, getSensor().getSunsetOffset());

    int newOffset = 20;
    expectConfigSet("sunsetoffset", newOffset);
    getSensor().setSunsetOffset(newOffset);
}

TEST_F(DaylightSensorTest, isDaylight)
{
    state["state"]["daylight"] = true;
    EXPECT_TRUE(getSensor().isDaylight());
}

TEST_F(ZLLPresenceTest, Sensitivity)
{
    int sensitivity = 1000;
    state["config"]["sensitivity"] = sensitivity;
    int maxSensitivity = 10000;
    state["config"]["sensitivitymax"] = maxSensitivity;
    EXPECT_EQ(sensitivity, getSensor().getSensitivity());
    EXPECT_EQ(maxSensitivity, getSensor().getMaxSensitivity());

    int newSensitivity = 10;
    expectConfigSet("sensitivity", newSensitivity);
    this->getSensor().setSensitivity(newSensitivity);
}

TEST_F(CLIPSwitchTest, setBatteryState)
{
    int percent = 10;
    expectConfigSet("battery", percent);
    this->getSensor().setBatteryState(percent);
}

TEST_F(CLIPSwitchTest, URL)
{
    EXPECT_FALSE(getSensor().hasURL());
    const std::string url = "https://abc";
    state["config"]["url"] = url;
    EXPECT_TRUE(getSensor().hasURL());
    EXPECT_EQ(url, getSensor().getURL());

    std::string newUrl = "https://cde";
    expectConfigSet("url", newUrl);
    getSensor().setURL(newUrl);
}

TEST_F(CLIPSwitchTest, setButtonEvent)
{
    int code = 10;
    expectStateSet("buttonevent", code);
    this->getSensor().setButtonEvent(code);
}

TEST_F(CLIPOpenCloseTest, Open)
{
    state["state"]["open"] = true;
    EXPECT_TRUE(getSensor().isOpen());

    bool open = false;
    expectStateSet("open", open);
    getSensor().setOpen(open);
}

TEST_F(CLIPPresenceTest, setPresence)
{
    bool presence = false;
    expectStateSet("presence", presence);
    getSensor().setPresence(presence);
}

TEST_F(CLIPTemperatureTest, setPresence)
{
    int temperature = 1100;
    expectStateSet("temperature", temperature);
    getSensor().setTemperature(temperature);
}

TEST_F(CLIPHumidityTest, Humidity)
{
    int humidity = 100;
    state["state"]["humidity"] = humidity;
    EXPECT_EQ(humidity, getSensor().getHumidity());

    int newHumidity = 1100;
    expectStateSet("humidity", newHumidity);
    getSensor().setHumidity(newHumidity);
}

TEST_F(CLIPGenericFlagTest, Flag)
{
    state["state"]["flag"] = true;
    EXPECT_TRUE(getSensor().getFlag());
    expectStateSet("flag", false);
    getSensor().setFlag(false);
}

TEST_F(CLIPGenericStatusTest, Status)
{
    int status = 32;
    state["state"]["status"] = status;
    EXPECT_EQ(status, getSensor().getStatus());
    int newStatus = 52;
    expectStateSet("status", newStatus);
    getSensor().setStatus(newStatus);
}