Commit ea91f4830bcfe29d0b970a270113fa8491b6cb58

Authored by Moritz Wirger
1 parent 4056d090

Add a whole load of tests so everything important is tested now

- add tests for ExtendedColorHueStrategy
- add tests for ExtendedColorTemperatureStrategy
- add missing test for HueLight
- add tests for IHttpHandler
- add tests for SimpleBrightnessStrategy
- add tests for SimpleColorHueStrategy
- add tests for SimpleColorTemperatureStrategy
hueplusplus/include/linHttpHandler.h 100644 โ†’ 100755
@@ -37,7 +37,7 @@ public: @@ -37,7 +37,7 @@ public:
37 //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1" 37 //! \param adr String that contains an ip or hostname in dotted decimal notation like "192.168.2.1"
38 //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80 38 //! \param port Optional integer that specifies the port to which the request is sent to. Default is 80
39 //! \return String containing the response of the host 39 //! \return String containing the response of the host
40 - std::string send(const std::string &msg, const std::string &adr, int port=80) const; 40 + virtual std::string send(const std::string &msg, const std::string &adr, int port=80) const;
41 41
42 //! \brief Function that sends a multicast request with the specified message. 42 //! \brief Function that sends a multicast request with the specified message.
43 //! 43 //!
@@ -46,7 +46,7 @@ public: @@ -46,7 +46,7 @@ public:
46 //! \param port Optional integer that specifies the port to which the request is sent. Default is 1900 46 //! \param port Optional integer that specifies the port to which the request is sent. Default is 1900
47 //! \param timeout Optional Integer that specifies the timeout of the request in seconds. Default is 5 47 //! \param timeout Optional Integer that specifies the timeout of the request in seconds. Default is 5
48 //! \return Vector containing strings of each answer received 48 //! \return Vector containing strings of each answer received
49 - std::vector<std::string> sendMulticast(const std::string &msg, const std::string &adr = "239.255.255.250", int port = 1900, int timeout = 5) const; 49 + virtual std::vector<std::string> sendMulticast(const std::string &msg, const std::string &adr = "239.255.255.250", int port = 1900, int timeout = 5) const;
50 }; 50 };
51 51
52 #endif 52 #endif
hueplusplus/test/CMakeLists.txt
@@ -34,9 +34,15 @@ endif() @@ -34,9 +34,15 @@ endif()
34 34
35 # define all test sources 35 # define all test sources
36 set(TEST_SOURCES 36 set(TEST_SOURCES
  37 + ${CMAKE_CURRENT_SOURCE_DIR}/test_ExtendedColorHueStrategy.cpp
  38 + ${CMAKE_CURRENT_SOURCE_DIR}/test_ExtendedColorTemperatureStrategy.cpp
37 ${CMAKE_CURRENT_SOURCE_DIR}/test_Hue.cpp 39 ${CMAKE_CURRENT_SOURCE_DIR}/test_Hue.cpp
38 ${CMAKE_CURRENT_SOURCE_DIR}/test_HueLight.cpp 40 ${CMAKE_CURRENT_SOURCE_DIR}/test_HueLight.cpp
  41 + ${CMAKE_CURRENT_SOURCE_DIR}/test_IHttpHandler.cpp
39 ${CMAKE_CURRENT_SOURCE_DIR}/test_Main.cpp 42 ${CMAKE_CURRENT_SOURCE_DIR}/test_Main.cpp
  43 + ${CMAKE_CURRENT_SOURCE_DIR}/test_SimpleBrightnessStrategy.cpp
  44 + ${CMAKE_CURRENT_SOURCE_DIR}/test_SimpleColorHueStrategy.cpp
  45 + ${CMAKE_CURRENT_SOURCE_DIR}/test_SimpleColorTemperatureStrategy.cpp
40 ${CMAKE_CURRENT_SOURCE_DIR}/test_UPnP.cpp 46 ${CMAKE_CURRENT_SOURCE_DIR}/test_UPnP.cpp
41 ) 47 )
42 48
hueplusplus/test/mocks/mock_HttpHandler.h
@@ -32,7 +32,6 @@ @@ -32,7 +32,6 @@
32 class MockHttpHandler : public IHttpHandler 32 class MockHttpHandler : public IHttpHandler
33 { 33 {
34 public: 34 public:
35 -  
36 MOCK_CONST_METHOD3( send, std::string(const std::string &msg, const std::string &adr, int port) ); 35 MOCK_CONST_METHOD3( send, std::string(const std::string &msg, const std::string &adr, int port) );
37 36
38 MOCK_CONST_METHOD3( sendGetHTTPBody, std::string(const std::string &msg, const std::string &adr, int port) ); 37 MOCK_CONST_METHOD3( sendGetHTTPBody, std::string(const std::string &msg, const std::string &adr, int port) );
hueplusplus/test/mocks/mock_linHttpHandler.h 0 โ†’ 100755
  1 +/**
  2 + \file mock_linHttpHandler.h
  3 + Copyright Notice\n
  4 + Copyright (C) 2017 Jan Rogall - developer\n
  5 + Copyright (C) 2017 Moritz Wirger - developer\n
  6 +
  7 + This program is free software; you can redistribute it and/or modify
  8 + it under the terms of the GNU General Public License as published by
  9 + the Free Software Foundation; either version 3 of the License, or
  10 + (at your option) any later version.
  11 + This program is distributed in the hope that it will be useful,
  12 + but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14 + GNU General Public License for more details.
  15 + You should have received a copy of the GNU General Public License
  16 + along with this program; if not, write to the Free Software Foundation,
  17 + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18 +**/
  19 +
  20 +#ifndef _MOCK_LIN_HTTPHANDLER_H
  21 +#define _MOCK_LIN_HTTPHANDLER_H
  22 +
  23 +#include <string>
  24 +#include <vector>
  25 +
  26 +#include <gmock/gmock.h>
  27 +
  28 +#include "../hueplusplus/include/linHttpHandler.h"
  29 +#include "../hueplusplus/include/json/json.h"
  30 +
  31 +//! Mock Class
  32 +class MockLinHttpHandler : public linHttpHandler
  33 +{
  34 +public:
  35 + MOCK_CONST_METHOD3( send, std::string(const std::string &msg, const std::string &adr, int port) );
  36 +
  37 + MOCK_CONST_METHOD4( sendMulticast, std::vector<std::string>(const std::string &msg, const std::string &adr, int port, int timeout) );
  38 +};
  39 +
  40 +#endif
hueplusplus/test/test_ExtendedColorHueStrategy.cpp 0 โ†’ 100755
  1 +#include <gtest/gtest.h>
  2 +#include <gmock/gmock.h>
  3 +
  4 +#include "../include/ExtendedColorHueStrategy.h"
  5 +#include "../include/json/json.h"
  6 +#include "mocks/mock_HttpHandler.h"
  7 +#include "mocks/mock_HueLight.h"
  8 +#include "testhelper.h"
  9 +
  10 +#include <memory>
  11 +#include <string>
  12 +
  13 +TEST(ExtendedColorHueStrategy, alertHueSaturation)
  14 +{
  15 + using namespace ::testing;
  16 + std::shared_ptr<MockHttpHandler> handler(std::make_shared<MockHttpHandler>());
  17 + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80))
  18 + .Times(AtLeast(1))
  19 + .WillRepeatedly(Return(Json::Value(Json::objectValue)));
  20 + MockHueLight test_light(handler);
  21 + EXPECT_CALL(test_light, refreshState())
  22 + .Times(AtLeast(1))
  23 + .WillRepeatedly(Return());
  24 +
  25 + test_light.getState()["state"]["colormode"] = "invalid";
  26 + EXPECT_EQ( false, ExtendedColorHueStrategy().alertHueSaturation(30000, 128, test_light) );
  27 +
  28 + EXPECT_CALL(test_light, setColorHueSaturation(_, _, 1))
  29 + .Times(AtLeast(2))
  30 + .WillOnce(Return(false))
  31 + .WillRepeatedly(Return(true));
  32 + test_light.getState()["state"]["colormode"] = "hs";
  33 + test_light.getState()["state"]["on"] = true;
  34 + test_light.getState()["state"]["sat"] = 100;
  35 + test_light.getState()["state"]["hue"] = 200;
  36 + EXPECT_EQ( false, ExtendedColorHueStrategy().alertHueSaturation(200, 100, test_light) );
  37 +
  38 + EXPECT_CALL(test_light, alert())
  39 + .Times(AtLeast(2))
  40 + .WillOnce(Return(false))
  41 + .WillRepeatedly(Return(true));
  42 + EXPECT_EQ( false, ExtendedColorHueStrategy().alertHueSaturation(200, 100, test_light) );
  43 +
  44 + EXPECT_EQ( true, ExtendedColorHueStrategy().alertHueSaturation(200, 100, test_light) );
  45 +
  46 + EXPECT_CALL(test_light, OffNoRefresh(_))
  47 + .Times(AtLeast(1))
  48 + .WillRepeatedly(Return(true));
  49 + test_light.getState()["state"]["on"] = false;
  50 + EXPECT_EQ( true, ExtendedColorHueStrategy().alertHueSaturation(200, 100, test_light) );
  51 +
  52 + EXPECT_CALL(test_light, setColorHueSaturation(_, _, 1))
  53 + .Times(AtLeast(2))
  54 + .WillOnce(Return(false))
  55 + .WillRepeatedly(Return(true));
  56 + test_light.getState()["state"]["colormode"] = "xy";
  57 + test_light.getState()["state"]["on"] = true;
  58 + test_light.getState()["state"]["xy"][0] = 0.1;
  59 + test_light.getState()["state"]["xy"][1] = 0.1;
  60 + EXPECT_EQ( false, ExtendedColorHueStrategy().alertHueSaturation(200, 100, test_light) );
  61 +
  62 + EXPECT_CALL(test_light, alert())
  63 + .Times(AtLeast(2))
  64 + .WillOnce(Return(false))
  65 + .WillRepeatedly(Return(true));
  66 + EXPECT_EQ( false, ExtendedColorHueStrategy().alertHueSaturation(200, 100, test_light) );
  67 +
  68 + EXPECT_CALL(test_light, setColorXY(_, _, 1))
  69 + .Times(AtLeast(2))
  70 + .WillRepeatedly(Return(true));
  71 + EXPECT_EQ( true, ExtendedColorHueStrategy().alertHueSaturation(200, 100, test_light) );
  72 +
  73 + EXPECT_CALL(test_light, OffNoRefresh(_))
  74 + .Times(AtLeast(1))
  75 + .WillRepeatedly(Return(true));
  76 + test_light.getState()["state"]["on"] = false;
  77 + EXPECT_EQ( true, ExtendedColorHueStrategy().alertHueSaturation(200, 100, test_light) );
  78 +
  79 + EXPECT_CALL(test_light, setColorHueSaturation(_, _, 1))
  80 + .Times(AtLeast(2))
  81 + .WillOnce(Return(false))
  82 + .WillRepeatedly(Return(true));
  83 + test_light.getState()["state"]["colormode"] = "ct";
  84 + test_light.getState()["state"]["on"] = true;
  85 + test_light.getState()["state"]["ct"] = 200;
  86 + EXPECT_EQ( false, ExtendedColorHueStrategy().alertHueSaturation(200, 100, test_light) );
  87 +
  88 + EXPECT_CALL(test_light, alert())
  89 + .Times(AtLeast(2))
  90 + .WillOnce(Return(false))
  91 + .WillRepeatedly(Return(true));
  92 + EXPECT_EQ( false, ExtendedColorHueStrategy().alertHueSaturation(200, 100, test_light) );
  93 +
  94 + EXPECT_CALL(test_light, setColorTemperature(_, 1))
  95 + .Times(AtLeast(2))
  96 + .WillRepeatedly(Return(true));
  97 + EXPECT_EQ( true, ExtendedColorHueStrategy().alertHueSaturation(200, 100, test_light) );
  98 +
  99 + EXPECT_CALL(test_light, OffNoRefresh(_))
  100 + .Times(AtLeast(1))
  101 + .WillRepeatedly(Return(true));
  102 + test_light.getState()["state"]["on"] = false;
  103 + EXPECT_EQ( true, ExtendedColorHueStrategy().alertHueSaturation(200, 100, test_light) );
  104 +}
  105 +
  106 +TEST(ExtendedColorHueStrategy, alertXY)
  107 +{
  108 + using namespace ::testing;
  109 + std::shared_ptr<MockHttpHandler> handler(std::make_shared<MockHttpHandler>());
  110 + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80))
  111 + .Times(AtLeast(1))
  112 + .WillRepeatedly(Return(Json::Value(Json::objectValue)));
  113 + MockHueLight test_light(handler);
  114 + EXPECT_CALL(test_light, refreshState())
  115 + .Times(AtLeast(1))
  116 + .WillRepeatedly(Return());
  117 +
  118 + test_light.getState()["state"]["colormode"] = "invalid";
  119 + EXPECT_EQ( false, ExtendedColorHueStrategy().alertXY(0.1, 0.1, test_light) );
  120 +
  121 + EXPECT_CALL(test_light, setColorXY(_, _, 1))
  122 + .Times(AtLeast(2))
  123 + .WillOnce(Return(false))
  124 + .WillRepeatedly(Return(true));
  125 + test_light.getState()["state"]["colormode"] = "hs";
  126 + test_light.getState()["state"]["on"] = true;
  127 + test_light.getState()["state"]["xy"][0] = 0.1;
  128 + test_light.getState()["state"]["xy"][1] = 0.1;
  129 + test_light.getState()["state"]["sat"] = 100;
  130 + test_light.getState()["state"]["hue"] = 200;
  131 + EXPECT_EQ( false, ExtendedColorHueStrategy().alertXY(0.1, 0.1, test_light) );
  132 +
  133 + EXPECT_CALL(test_light, alert())
  134 + .Times(AtLeast(2))
  135 + .WillOnce(Return(false))
  136 + .WillRepeatedly(Return(true));
  137 + EXPECT_EQ( false, ExtendedColorHueStrategy().alertXY(0.1, 0.1, test_light) );
  138 +
  139 + EXPECT_CALL(test_light, setColorHueSaturation(_, _, 1))
  140 + .Times(AtLeast(2))
  141 + .WillRepeatedly(Return(true));
  142 + EXPECT_EQ( true, ExtendedColorHueStrategy().alertXY(0.1, 0.1, test_light) );
  143 +
  144 + EXPECT_CALL(test_light, OffNoRefresh(_))
  145 + .Times(AtLeast(1))
  146 + .WillRepeatedly(Return(true));
  147 + test_light.getState()["state"]["on"] = false;
  148 + EXPECT_EQ( true, ExtendedColorHueStrategy().alertXY(0.1, 0.1, test_light) );
  149 +
  150 + EXPECT_CALL(test_light, setColorXY(_, _, 1))
  151 + .Times(AtLeast(2))
  152 + .WillOnce(Return(false))
  153 + .WillRepeatedly(Return(true));
  154 + test_light.getState()["state"]["colormode"] = "xy";
  155 + test_light.getState()["state"]["on"] = true;
  156 + EXPECT_EQ( false, ExtendedColorHueStrategy().alertXY(0.1, 0.1, test_light) );
  157 +
  158 + EXPECT_CALL(test_light, alert())
  159 + .Times(AtLeast(2))
  160 + .WillOnce(Return(false))
  161 + .WillRepeatedly(Return(true));
  162 + EXPECT_EQ( false, ExtendedColorHueStrategy().alertXY(0.1, 0.1, test_light) );
  163 +
  164 + EXPECT_EQ( true, ExtendedColorHueStrategy().alertXY(0.1, 0.1, test_light) );
  165 +
  166 + EXPECT_CALL(test_light, OffNoRefresh(_))
  167 + .Times(AtLeast(1))
  168 + .WillRepeatedly(Return(true));
  169 + test_light.getState()["state"]["on"] = false;
  170 + EXPECT_EQ( true, ExtendedColorHueStrategy().alertXY(0.1, 0.1, test_light) );
  171 +
  172 + EXPECT_CALL(test_light, setColorXY(_, _, 1))
  173 + .Times(AtLeast(2))
  174 + .WillOnce(Return(false))
  175 + .WillRepeatedly(Return(true));
  176 + test_light.getState()["state"]["colormode"] = "ct";
  177 + test_light.getState()["state"]["on"] = true;
  178 + test_light.getState()["state"]["ct"] = 200;
  179 + EXPECT_EQ( false, ExtendedColorHueStrategy().alertXY(0.1, 0.1, test_light) );
  180 +
  181 + EXPECT_CALL(test_light, alert())
  182 + .Times(AtLeast(2))
  183 + .WillOnce(Return(false))
  184 + .WillRepeatedly(Return(true));
  185 + EXPECT_EQ( false, ExtendedColorHueStrategy().alertXY(0.1, 0.1, test_light) );
  186 +
  187 + EXPECT_CALL(test_light, setColorTemperature(_, 1))
  188 + .Times(AtLeast(2))
  189 + .WillRepeatedly(Return(true));
  190 + EXPECT_EQ( true, ExtendedColorHueStrategy().alertXY(0.1, 0.1, test_light) );
  191 +
  192 + EXPECT_CALL(test_light, OffNoRefresh(_))
  193 + .Times(AtLeast(1))
  194 + .WillRepeatedly(Return(true));
  195 + test_light.getState()["state"]["on"] = false;
  196 + EXPECT_EQ( true, ExtendedColorHueStrategy().alertXY(0.1, 0.1, test_light) );
  197 +}
  198 +
  199 +TEST(ExtendedColorHueStrategy, alertRGB)
  200 +{
  201 + using namespace ::testing;
  202 + std::shared_ptr<MockHttpHandler> handler(std::make_shared<MockHttpHandler>());
  203 + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80))
  204 + .Times(AtLeast(1))
  205 + .WillRepeatedly(Return(Json::Value(Json::objectValue)));
  206 + MockHueLight test_light(handler);
  207 + EXPECT_CALL(test_light, refreshState())
  208 + .Times(AtLeast(1))
  209 + .WillRepeatedly(Return());
  210 +
  211 + test_light.getState()["state"]["colormode"] = "invalid";
  212 + EXPECT_EQ( false, ExtendedColorHueStrategy().alertRGB(128, 128, 128, test_light) );
  213 +
  214 + EXPECT_CALL(test_light, setColorRGB(_, _, _, 1))
  215 + .Times(AtLeast(2))
  216 + .WillOnce(Return(false))
  217 + .WillRepeatedly(Return(true));
  218 + test_light.getState()["state"]["colormode"] = "hs";
  219 + test_light.getState()["state"]["on"] = true;
  220 + test_light.getState()["state"]["sat"] = 100;
  221 + test_light.getState()["state"]["hue"] = 200;
  222 + EXPECT_EQ( false, ExtendedColorHueStrategy().alertRGB(128, 128, 128, test_light) );
  223 +
  224 + EXPECT_CALL(test_light, alert())
  225 + .Times(AtLeast(2))
  226 + .WillOnce(Return(false))
  227 + .WillRepeatedly(Return(true));
  228 + EXPECT_EQ( false, ExtendedColorHueStrategy().alertRGB(128, 128, 128, test_light) );
  229 +
  230 + EXPECT_CALL(test_light, setColorHueSaturation(_, _, 1))
  231 + .Times(AtLeast(2))
  232 + .WillRepeatedly(Return(true));
  233 + EXPECT_EQ( true, ExtendedColorHueStrategy().alertRGB(128, 128, 128, test_light) );
  234 +
  235 + EXPECT_CALL(test_light, OffNoRefresh(_))
  236 + .Times(AtLeast(1))
  237 + .WillRepeatedly(Return(true));
  238 + test_light.getState()["state"]["on"] = false;
  239 + EXPECT_EQ( true, ExtendedColorHueStrategy().alertRGB(128, 128, 128, test_light) );
  240 +
  241 + EXPECT_CALL(test_light, setColorRGB(_, _, _, 1))
  242 + .Times(AtLeast(2))
  243 + .WillOnce(Return(false))
  244 + .WillRepeatedly(Return(true));
  245 + test_light.getState()["state"]["colormode"] = "xy";
  246 + test_light.getState()["state"]["on"] = true;
  247 + test_light.getState()["state"]["xy"][0] = 0.1;
  248 + test_light.getState()["state"]["xy"][1] = 0.1;
  249 + EXPECT_EQ( false, ExtendedColorHueStrategy().alertRGB(128, 128, 128, test_light) );
  250 +
  251 + EXPECT_CALL(test_light, alert())
  252 + .Times(AtLeast(2))
  253 + .WillOnce(Return(false))
  254 + .WillRepeatedly(Return(true));
  255 + EXPECT_EQ( false, ExtendedColorHueStrategy().alertRGB(128, 128, 128, test_light) );
  256 +
  257 + EXPECT_CALL(test_light, setColorXY(_, _, 1))
  258 + .Times(AtLeast(2))
  259 + .WillRepeatedly(Return(true));
  260 + EXPECT_EQ( true, ExtendedColorHueStrategy().alertRGB(128, 128, 128, test_light) );
  261 +
  262 + EXPECT_CALL(test_light, OffNoRefresh(_))
  263 + .Times(AtLeast(1))
  264 + .WillRepeatedly(Return(true));
  265 + test_light.getState()["state"]["on"] = false;
  266 + EXPECT_EQ( true, ExtendedColorHueStrategy().alertRGB(128, 128, 128, test_light) );
  267 +
  268 + EXPECT_CALL(test_light, setColorRGB(_, _, _, 1))
  269 + .Times(AtLeast(2))
  270 + .WillOnce(Return(false))
  271 + .WillRepeatedly(Return(true));
  272 + test_light.getState()["state"]["colormode"] = "ct";
  273 + test_light.getState()["state"]["on"] = true;
  274 + test_light.getState()["state"]["ct"] = 200;
  275 + EXPECT_EQ( false, ExtendedColorHueStrategy().alertRGB(128, 128, 128, test_light) );
  276 +
  277 + EXPECT_CALL(test_light, alert())
  278 + .Times(AtLeast(2))
  279 + .WillOnce(Return(false))
  280 + .WillRepeatedly(Return(true));
  281 + EXPECT_EQ( false, ExtendedColorHueStrategy().alertRGB(128, 128, 128, test_light) );
  282 +
  283 + EXPECT_CALL(test_light, setColorTemperature(_, 1))
  284 + .Times(AtLeast(2))
  285 + .WillRepeatedly(Return(true));
  286 + EXPECT_EQ( true, ExtendedColorHueStrategy().alertRGB(128, 128, 128, test_light) );
  287 +
  288 + EXPECT_CALL(test_light, OffNoRefresh(_))
  289 + .Times(AtLeast(1))
  290 + .WillRepeatedly(Return(true));
  291 + test_light.getState()["state"]["on"] = false;
  292 + EXPECT_EQ( true, ExtendedColorHueStrategy().alertRGB(128, 128, 128, test_light) );
  293 +}
hueplusplus/test/test_ExtendedColorTemperatureStrategy.cpp 0 โ†’ 100755
  1 +#include <gtest/gtest.h>
  2 +#include <gmock/gmock.h>
  3 +
  4 +#include "../include/ExtendedColorTemperatureStrategy.h"
  5 +#include "../include/json/json.h"
  6 +#include "mocks/mock_HttpHandler.h"
  7 +#include "mocks/mock_HueLight.h"
  8 +#include "testhelper.h"
  9 +
  10 +#include <memory>
  11 +#include <string>
  12 +
  13 +TEST(ExtendedColorTemperatureStrategy, setColorTemperature)
  14 +{
  15 + using namespace ::testing;
  16 + std::shared_ptr<MockHttpHandler> handler(std::make_shared<MockHttpHandler>());
  17 + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80))
  18 + .Times(AtLeast(1))
  19 + .WillRepeatedly(Return(Json::Value(Json::objectValue)));
  20 + MockHueLight test_light(handler);
  21 + EXPECT_CALL(test_light, refreshState())
  22 + .Times(AtLeast(1))
  23 + .WillRepeatedly(Return());
  24 + Json::Value prep_ret;
  25 + prep_ret = Json::Value(Json::arrayValue);
  26 + prep_ret[0] = Json::Value(Json::objectValue);
  27 + prep_ret[0]["success"] = Json::Value(Json::objectValue);
  28 + prep_ret[0]["success"]["/lights/1/state/transitiontime"] = 6;
  29 + prep_ret[1] = Json::Value(Json::objectValue);
  30 + prep_ret[1]["success"] = Json::Value(Json::objectValue);
  31 + prep_ret[1]["success"]["/lights/1/state/on"] = true;
  32 + prep_ret[2] = Json::Value(Json::objectValue);
  33 + prep_ret[2]["success"] = Json::Value(Json::objectValue);
  34 + prep_ret[2]["success"]["/lights/1/state/ct"] = 155;
  35 + EXPECT_CALL(test_light, SendPutRequest(_, "/state"))
  36 + .Times(1)
  37 + .WillOnce(Return(prep_ret));
  38 +
  39 + test_light.getState()["state"]["colormode"] = "ct";
  40 + test_light.getState()["state"]["on"] = true;
  41 + test_light.getState()["state"]["ct"] = 200;
  42 + EXPECT_EQ( true, ExtendedColorTemperatureStrategy().setColorTemperature(200, 4, test_light) );
  43 +
  44 + test_light.getState()["state"]["on"] = false;
  45 + EXPECT_EQ( true, ExtendedColorTemperatureStrategy().setColorTemperature(155, 6, test_light) );
  46 +
  47 + prep_ret[2]["success"]["/lights/1/state/ct"] = 153;
  48 + EXPECT_CALL(test_light, SendPutRequest(_, "/state"))
  49 + .Times(1)
  50 + .WillOnce(Return(prep_ret));
  51 + EXPECT_EQ( true, ExtendedColorTemperatureStrategy().setColorTemperature(0, 6, test_light) );
  52 +
  53 + prep_ret[2]["success"]["/lights/1/state/ct"] = 500;
  54 + EXPECT_CALL(test_light, SendPutRequest(_, "/state"))
  55 + .Times(1)
  56 + .WillOnce(Return(prep_ret));
  57 + EXPECT_EQ( true, ExtendedColorTemperatureStrategy().setColorTemperature(600, 6, test_light) );
  58 +}
  59 +
  60 +TEST(ExtendedColorTemperatureStrategy, alertTemperature)
  61 +{
  62 + using namespace ::testing;
  63 + std::shared_ptr<MockHttpHandler> handler(std::make_shared<MockHttpHandler>());
  64 + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80))
  65 + .Times(AtLeast(1))
  66 + .WillRepeatedly(Return(Json::Value(Json::objectValue)));
  67 + MockHueLight test_light(handler);
  68 + EXPECT_CALL(test_light, refreshState())
  69 + .Times(AtLeast(1))
  70 + .WillRepeatedly(Return());
  71 +
  72 + test_light.getState()["state"]["colormode"] = "invalid";
  73 + EXPECT_EQ( false, ExtendedColorTemperatureStrategy().alertTemperature(400, test_light) );
  74 +
  75 + EXPECT_CALL(test_light, setColorTemperature(_, _))
  76 + .Times(AtLeast(2))
  77 + .WillOnce(Return(false))
  78 + .WillRepeatedly(Return(true));
  79 + test_light.getState()["state"]["colormode"] = "hs";
  80 + test_light.getState()["state"]["on"] = true;
  81 + test_light.getState()["state"]["ct"] = 200;
  82 + test_light.getState()["state"]["sat"] = 100;
  83 + test_light.getState()["state"]["hue"] = 200;
  84 + EXPECT_EQ( false, ExtendedColorTemperatureStrategy().alertTemperature(400, test_light) );
  85 +
  86 + EXPECT_CALL(test_light, alert())
  87 + .Times(AtLeast(2))
  88 + .WillOnce(Return(false))
  89 + .WillRepeatedly(Return(true));
  90 + EXPECT_EQ( false, ExtendedColorTemperatureStrategy().alertTemperature(400, test_light) );
  91 +
  92 + EXPECT_CALL(test_light, setColorHueSaturation(_, _, 1))
  93 + .Times(AtLeast(2))
  94 + .WillRepeatedly(Return(true));
  95 + EXPECT_EQ( true, ExtendedColorTemperatureStrategy().alertTemperature(400, test_light) );
  96 +
  97 + EXPECT_CALL(test_light, OffNoRefresh(_))
  98 + .Times(AtLeast(1))
  99 + .WillRepeatedly(Return(true));
  100 + test_light.getState()["state"]["on"] = false;
  101 + EXPECT_EQ( true, ExtendedColorTemperatureStrategy().alertTemperature(400, test_light) );
  102 +
  103 + EXPECT_CALL(test_light, setColorTemperature(_, _))
  104 + .Times(AtLeast(2))
  105 + .WillOnce(Return(false))
  106 + .WillRepeatedly(Return(true));
  107 + test_light.getState()["state"]["colormode"] = "xy";
  108 + test_light.getState()["state"]["on"] = true;
  109 + test_light.getState()["state"]["xy"][0] = 0.1;
  110 + test_light.getState()["state"]["xy"][1] = 0.1;
  111 + EXPECT_EQ( false, ExtendedColorTemperatureStrategy().alertTemperature(400, test_light) );
  112 +
  113 + EXPECT_CALL(test_light, alert())
  114 + .Times(AtLeast(2))
  115 + .WillOnce(Return(false))
  116 + .WillRepeatedly(Return(true));
  117 + EXPECT_EQ( false, ExtendedColorTemperatureStrategy().alertTemperature(400, test_light) );
  118 +
  119 + EXPECT_CALL(test_light, setColorXY(_, _, 1))
  120 + .Times(AtLeast(2))
  121 + .WillRepeatedly(Return(true));
  122 + EXPECT_EQ( true, ExtendedColorTemperatureStrategy().alertTemperature(400, test_light) );
  123 +
  124 + test_light.getState()["state"]["on"] = false;
  125 + EXPECT_EQ( true, ExtendedColorTemperatureStrategy().alertTemperature(400, test_light) );
  126 +
  127 + EXPECT_CALL(test_light, setColorTemperature(_, _))
  128 + .Times(AtLeast(2))
  129 + .WillOnce(Return(false))
  130 + .WillRepeatedly(Return(true));
  131 + test_light.getState()["state"]["colormode"] = "ct";
  132 + test_light.getState()["state"]["on"] = true;
  133 + test_light.getState()["state"]["on"] = true;
  134 + test_light.getState()["state"]["ct"] = 200;
  135 + EXPECT_EQ( false, ExtendedColorTemperatureStrategy().alertTemperature(400, test_light) );
  136 +
  137 + EXPECT_CALL(test_light, alert())
  138 + .Times(AtLeast(2))
  139 + .WillOnce(Return(false))
  140 + .WillRepeatedly(Return(true));
  141 + EXPECT_EQ( false, ExtendedColorTemperatureStrategy().alertTemperature(400, test_light) );
  142 +
  143 + EXPECT_EQ( true, ExtendedColorTemperatureStrategy().alertTemperature(400, test_light) );
  144 +
  145 + test_light.getState()["state"]["on"] = false;
  146 + EXPECT_EQ( true, ExtendedColorTemperatureStrategy().alertTemperature(400, test_light) );
  147 +}
hueplusplus/test/test_HueLight.cpp 100644 โ†’ 100755
@@ -838,3 +838,18 @@ TEST_F(HueLightTest, setColorLoop) @@ -838,3 +838,18 @@ TEST_F(HueLightTest, setColorLoop)
838 EXPECT_EQ(false, test_light_2.setColorLoop(false)); 838 EXPECT_EQ(false, test_light_2.setColorLoop(false));
839 EXPECT_EQ(false, test_light_3.setColorLoop(true)); 839 EXPECT_EQ(false, test_light_3.setColorLoop(true));
840 } 840 }
  841 +
  842 +TEST_F(HueLightTest, refreshState)
  843 +{
  844 + using namespace ::testing;
  845 + test_bridge.getLight(1);
  846 + test_bridge.getLight(2);
  847 + test_bridge.getLight(3);
  848 +
  849 + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80))
  850 + .Times(2)
  851 + .WillRepeatedly(Return(Json::Value(Json::objectValue)));
  852 +
  853 + const HueLight ctest_light_1 = test_bridge.getLight(1);
  854 + HueLight test_light_1 = test_bridge.getLight(1);
  855 +}
hueplusplus/test/test_IHttpHandler.cpp 0 โ†’ 100755
  1 +#include <gtest/gtest.h>
  2 +#include <gmock/gmock.h>
  3 +
  4 +#include "../include/json/json.h"
  5 +#include "mocks/mock_linHttpHandler.h"
  6 +#include "testhelper.h"
  7 +
  8 +#include <memory>
  9 +#include <string>
  10 +
  11 +TEST(IHttpHandler, sendGetHTTPBody)
  12 +{
  13 + using namespace ::testing;
  14 + MockLinHttpHandler handler;
  15 +
  16 + EXPECT_CALL(handler, send("testmsg", "192.168.2.1", 90))
  17 + .Times(AtLeast(2))
  18 + .WillOnce(Return(""))
  19 + .WillRepeatedly(Return("\r\n\r\ntestreply"));
  20 +
  21 + EXPECT_THROW(handler.sendGetHTTPBody("testmsg", "192.168.2.1", 90), std::runtime_error);
  22 + EXPECT_EQ("testreply", handler.sendGetHTTPBody("testmsg", "192.168.2.1", 90));
  23 +}
  24 +
  25 +TEST(IHttpHandler, sendHTTPRequest)
  26 +{
  27 + using namespace ::testing;
  28 + MockLinHttpHandler handler;
  29 +
  30 + EXPECT_CALL(handler, send("GET UrI HTTP/1.0\r\nContent-Type: text/html\r\nContent-Length: 4\r\n\r\nbody\r\n\r\n", "192.168.2.1", 90))
  31 + .Times(AtLeast(2))
  32 + .WillOnce(Return(""))
  33 + .WillRepeatedly(Return("\r\n\r\ntestreply"));
  34 +
  35 + EXPECT_THROW(handler.sendHTTPRequest("GET", "UrI", "text/html", "body", "192.168.2.1", 90), std::runtime_error);
  36 + EXPECT_EQ("testreply", handler.sendHTTPRequest("GET", "UrI", "text/html", "body", "192.168.2.1", 90));
  37 +}
  38 +
  39 +TEST(IHttpHandler, GETString)
  40 +{
  41 + using namespace ::testing;
  42 + MockLinHttpHandler handler;
  43 +
  44 + EXPECT_CALL(handler, send("GET UrI HTTP/1.0\r\nContent-Type: text/html\r\nContent-Length: 4\r\n\r\nbody\r\n\r\n", "192.168.2.1", 90))
  45 + .Times(AtLeast(2))
  46 + .WillOnce(Return(""))
  47 + .WillRepeatedly(Return("\r\n\r\ntestreply"));
  48 +
  49 + EXPECT_THROW(handler.GETString("UrI", "text/html", "body", "192.168.2.1", 90), std::runtime_error);
  50 + EXPECT_EQ("testreply", handler.GETString("UrI", "text/html", "body", "192.168.2.1", 90));
  51 +}
  52 +
  53 +TEST(IHttpHandler, POSTString)
  54 +{
  55 + using namespace ::testing;
  56 + MockLinHttpHandler handler;
  57 +
  58 + EXPECT_CALL(handler, send("POST UrI HTTP/1.0\r\nContent-Type: text/html\r\nContent-Length: 4\r\n\r\nbody\r\n\r\n", "192.168.2.1", 90))
  59 + .Times(AtLeast(2))
  60 + .WillOnce(Return(""))
  61 + .WillRepeatedly(Return("\r\n\r\ntestreply"));
  62 +
  63 + EXPECT_THROW(handler.POSTString("UrI", "text/html", "body", "192.168.2.1", 90), std::runtime_error);
  64 + EXPECT_EQ("testreply", handler.POSTString("UrI", "text/html", "body", "192.168.2.1", 90));
  65 +}
  66 +
  67 +TEST(IHttpHandler, PUTString)
  68 +{
  69 + using namespace ::testing;
  70 + MockLinHttpHandler handler;
  71 +
  72 + EXPECT_CALL(handler, send("PUT UrI HTTP/1.0\r\nContent-Type: text/html\r\nContent-Length: 4\r\n\r\nbody\r\n\r\n", "192.168.2.1", 90))
  73 + .Times(AtLeast(2))
  74 + .WillOnce(Return(""))
  75 + .WillRepeatedly(Return("\r\n\r\ntestreply"));
  76 +
  77 + EXPECT_THROW(handler.PUTString("UrI", "text/html", "body", "192.168.2.1", 90), std::runtime_error);
  78 + EXPECT_EQ("testreply", handler.PUTString("UrI", "text/html", "body", "192.168.2.1", 90));
  79 +}
  80 +
  81 +TEST(IHttpHandler, DELETEString)
  82 +{
  83 + using namespace ::testing;
  84 + MockLinHttpHandler handler;
  85 +
  86 + EXPECT_CALL(handler, send("DELETE UrI HTTP/1.0\r\nContent-Type: text/html\r\nContent-Length: 4\r\n\r\nbody\r\n\r\n", "192.168.2.1", 90))
  87 + .Times(AtLeast(2))
  88 + .WillOnce(Return(""))
  89 + .WillRepeatedly(Return("\r\n\r\ntestreply"));
  90 +
  91 + EXPECT_THROW(handler.DELETEString("UrI", "text/html", "body", "192.168.2.1", 90), std::runtime_error);
  92 + EXPECT_EQ("testreply", handler.DELETEString("UrI", "text/html", "body", "192.168.2.1", 90));
  93 +}
  94 +
  95 +TEST(IHttpHandler, GETJson)
  96 +{
  97 + using namespace ::testing;
  98 + MockLinHttpHandler handler;
  99 +
  100 + Json::Value testval;
  101 + testval["test"] = 100;
  102 + std::string expected_call = "GET UrI HTTP/1.0\r\nContent-Type: application/json\r\nContent-Length: ";
  103 + expected_call.append(std::to_string(testval.toStyledString().size()));
  104 + expected_call.append("\r\n\r\n");
  105 + expected_call.append(testval.toStyledString());
  106 + expected_call.append("\r\n\r\n");
  107 +
  108 + EXPECT_CALL(handler, send(expected_call, "192.168.2.1", 90))
  109 + .Times(AtLeast(2))
  110 + .WillOnce(Return(""))
  111 + .WillOnce(Return("\r\n\r\n"))
  112 + .WillRepeatedly(Return("\r\n\r\n{\"test\" : \"whatever\"}"));
  113 + Json::Value expected;
  114 + expected["test"] = "whatever";
  115 +
  116 + EXPECT_THROW(handler.GETJson("UrI", testval, "192.168.2.1", 90), std::runtime_error);
  117 + EXPECT_THROW(handler.GETJson("UrI", testval, "192.168.2.1", 90), std::runtime_error);
  118 + EXPECT_EQ(expected, handler.GETJson("UrI", testval, "192.168.2.1", 90));
  119 +}
  120 +
  121 +TEST(IHttpHandler, POSTJson)
  122 +{
  123 + using namespace ::testing;
  124 + MockLinHttpHandler handler;
  125 +
  126 + Json::Value testval;
  127 + testval["test"] = 100;
  128 + std::string expected_call = "POST UrI HTTP/1.0\r\nContent-Type: application/json\r\nContent-Length: ";
  129 + expected_call.append(std::to_string(testval.toStyledString().size()));
  130 + expected_call.append("\r\n\r\n");
  131 + expected_call.append(testval.toStyledString());
  132 + expected_call.append("\r\n\r\n");
  133 +
  134 + EXPECT_CALL(handler, send(expected_call, "192.168.2.1", 90))
  135 + .Times(AtLeast(2))
  136 + .WillOnce(Return(""))
  137 + .WillOnce(Return("\r\n\r\n"))
  138 + .WillRepeatedly(Return("\r\n\r\n{\"test\" : \"whatever\"}"));
  139 + Json::Value expected;
  140 + expected["test"] = "whatever";
  141 +
  142 + EXPECT_THROW(handler.POSTJson("UrI", testval, "192.168.2.1", 90), std::runtime_error);
  143 + EXPECT_THROW(handler.POSTJson("UrI", testval, "192.168.2.1", 90), std::runtime_error);
  144 + EXPECT_EQ(expected, handler.POSTJson("UrI", testval, "192.168.2.1", 90));
  145 +}
  146 +
  147 +TEST(IHttpHandler, PUTJson)
  148 +{
  149 + using namespace ::testing;
  150 + MockLinHttpHandler handler;
  151 +
  152 + Json::Value testval;
  153 + testval["test"] = 100;
  154 + std::string expected_call = "PUT UrI HTTP/1.0\r\nContent-Type: application/json\r\nContent-Length: ";
  155 + expected_call.append(std::to_string(testval.toStyledString().size()));
  156 + expected_call.append("\r\n\r\n");
  157 + expected_call.append(testval.toStyledString());
  158 + expected_call.append("\r\n\r\n");
  159 +
  160 + EXPECT_CALL(handler, send(expected_call, "192.168.2.1", 90))
  161 + .Times(AtLeast(2))
  162 + .WillOnce(Return(""))
  163 + .WillOnce(Return("\r\n\r\n"))
  164 + .WillRepeatedly(Return("\r\n\r\n{\"test\" : \"whatever\"}"));
  165 + Json::Value expected;
  166 + expected["test"] = "whatever";
  167 +
  168 + EXPECT_THROW(handler.PUTJson("UrI", testval, "192.168.2.1", 90), std::runtime_error);
  169 + EXPECT_THROW(handler.PUTJson("UrI", testval, "192.168.2.1", 90), std::runtime_error);
  170 + EXPECT_EQ(expected, handler.PUTJson("UrI", testval, "192.168.2.1", 90));
  171 +}
  172 +
  173 +TEST(IHttpHandler, DELETEJson)
  174 +{
  175 + using namespace ::testing;
  176 + MockLinHttpHandler handler;
  177 +
  178 + Json::Value testval;
  179 + testval["test"] = 100;
  180 + std::string expected_call = "DELETE UrI HTTP/1.0\r\nContent-Type: application/json\r\nContent-Length: ";
  181 + expected_call.append(std::to_string(testval.toStyledString().size()));
  182 + expected_call.append("\r\n\r\n");
  183 + expected_call.append(testval.toStyledString());
  184 + expected_call.append("\r\n\r\n");
  185 +
  186 + EXPECT_CALL(handler, send(expected_call, "192.168.2.1", 90))
  187 + .Times(AtLeast(2))
  188 + .WillOnce(Return(""))
  189 + .WillOnce(Return("\r\n\r\n"))
  190 + .WillRepeatedly(Return("\r\n\r\n{\"test\" : \"whatever\"}"));
  191 + Json::Value expected;
  192 + expected["test"] = "whatever";
  193 +
  194 + EXPECT_THROW(handler.DELETEJson("UrI", testval, "192.168.2.1", 90), std::runtime_error);
  195 + EXPECT_THROW(handler.DELETEJson("UrI", testval, "192.168.2.1", 90), std::runtime_error);
  196 + EXPECT_EQ(expected, handler.DELETEJson("UrI", testval, "192.168.2.1", 90));
  197 +}
hueplusplus/test/test_SimpleBrightnessStrategy.cpp 0 โ†’ 100755
  1 +#include <gtest/gtest.h>
  2 +#include <gmock/gmock.h>
  3 +
  4 +#include "../include/SimpleBrightnessStrategy.h"
  5 +#include "../include/json/json.h"
  6 +#include "mocks/mock_HttpHandler.h"
  7 +#include "mocks/mock_HueLight.h"
  8 +#include "testhelper.h"
  9 +
  10 +#include <iostream>
  11 +#include <memory>
  12 +#include <string>
  13 +
  14 +TEST(SimpleBrightnessStrategy, setBrightness)
  15 +{
  16 + using namespace ::testing;
  17 + std::shared_ptr<MockHttpHandler> handler(std::make_shared<MockHttpHandler>());
  18 + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80))
  19 + .Times(AtLeast(1))
  20 + .WillRepeatedly(Return(Json::Value(Json::objectValue)));
  21 + MockHueLight test_light(handler);
  22 + EXPECT_CALL(test_light, refreshState())
  23 + .Times(AtLeast(1))
  24 + .WillRepeatedly(Return());
  25 + EXPECT_CALL(test_light, OffNoRefresh(_))
  26 + .Times(AtLeast(1))
  27 + .WillRepeatedly(Return(true));
  28 + Json::Value prep_ret;
  29 + prep_ret = Json::Value(Json::arrayValue);
  30 + prep_ret[0] = Json::Value(Json::objectValue);
  31 + prep_ret[0]["success"] = Json::Value(Json::objectValue);
  32 + prep_ret[0]["success"]["/lights/1/state/transitiontime"] = 6;
  33 + prep_ret[1] = Json::Value(Json::objectValue);
  34 + prep_ret[1]["success"] = Json::Value(Json::objectValue);
  35 + prep_ret[1]["success"]["/lights/1/state/on"] = true;
  36 + prep_ret[2] = Json::Value(Json::objectValue);
  37 + prep_ret[2]["success"] = Json::Value(Json::objectValue);
  38 + prep_ret[2]["success"]["/lights/1/state/bri"] = 50;
  39 + EXPECT_CALL(test_light, SendPutRequest(_, "/state"))
  40 + .Times(1)
  41 + .WillOnce(Return(prep_ret));
  42 +
  43 + test_light.getState()["state"]["on"] = true;
  44 + EXPECT_EQ( true, SimpleBrightnessStrategy().setBrightness(0, 4, test_light) );
  45 + test_light.getState()["state"]["on"] = false;
  46 + EXPECT_EQ( true, SimpleBrightnessStrategy().setBrightness(0, 4, test_light) );
  47 +
  48 + test_light.getState()["state"]["bri"] = 0;
  49 + EXPECT_EQ( true, SimpleBrightnessStrategy().setBrightness(50, 6, test_light) );
  50 + test_light.getState()["state"]["on"] = true;
  51 + test_light.getState()["state"]["bri"] = 50;
  52 + EXPECT_EQ( true, SimpleBrightnessStrategy().setBrightness(50, 6, test_light) );
  53 +
  54 + prep_ret[2]["success"]["/lights/1/state/bri"] = 254;
  55 + EXPECT_CALL(test_light, SendPutRequest(_, "/state"))
  56 + .Times(1)
  57 + .WillOnce(Return(prep_ret));
  58 + test_light.getState()["state"]["on"] = false;
  59 + EXPECT_EQ( true, SimpleBrightnessStrategy().setBrightness(255, 6, test_light) );
  60 +}
  61 +
  62 +TEST(SimpleBrightnessStrategy, getBrightness)
  63 +{
  64 + using namespace ::testing;
  65 + std::shared_ptr<MockHttpHandler> handler(std::make_shared<MockHttpHandler>());
  66 + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80))
  67 + .Times(AtLeast(1))
  68 + .WillRepeatedly(Return(Json::Value(Json::objectValue)));
  69 + MockHueLight test_light(handler);
  70 + EXPECT_CALL(test_light, refreshState())
  71 + .Times(AtLeast(1))
  72 + .WillRepeatedly(Return());
  73 +
  74 + test_light.getState()["state"]["bri"] = 200;
  75 + EXPECT_EQ( 200, SimpleBrightnessStrategy().getBrightness(test_light) );
  76 + test_light.getState()["state"]["bri"] = 0;
  77 + EXPECT_EQ( 0, SimpleBrightnessStrategy().getBrightness(static_cast<const HueLight>(test_light)) );
  78 +}
hueplusplus/test/test_SimpleColorHueStrategy.cpp 0 โ†’ 100755
  1 +#include <gtest/gtest.h>
  2 +#include <gmock/gmock.h>
  3 +
  4 +#include "../include/SimpleColorHueStrategy.h"
  5 +#include "../include/json/json.h"
  6 +#include "mocks/mock_HttpHandler.h"
  7 +#include "mocks/mock_HueLight.h"
  8 +#include "testhelper.h"
  9 +
  10 +#include <memory>
  11 +#include <string>
  12 +
  13 +TEST(SimpleColorHueStrategy, setColorHue)
  14 +{
  15 + using namespace ::testing;
  16 + std::shared_ptr<MockHttpHandler> handler(std::make_shared<MockHttpHandler>());
  17 + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80))
  18 + .Times(AtLeast(1))
  19 + .WillRepeatedly(Return(Json::Value(Json::objectValue)));
  20 + MockHueLight test_light(handler);
  21 + EXPECT_CALL(test_light, refreshState())
  22 + .Times(AtLeast(1))
  23 + .WillRepeatedly(Return());
  24 + Json::Value prep_ret;
  25 + prep_ret = Json::Value(Json::arrayValue);
  26 + prep_ret[0] = Json::Value(Json::objectValue);
  27 + prep_ret[0]["success"] = Json::Value(Json::objectValue);
  28 + prep_ret[0]["success"]["/lights/1/state/transitiontime"] = 6;
  29 + prep_ret[1] = Json::Value(Json::objectValue);
  30 + prep_ret[1]["success"] = Json::Value(Json::objectValue);
  31 + prep_ret[1]["success"]["/lights/1/state/on"] = true;
  32 + prep_ret[2] = Json::Value(Json::objectValue);
  33 + prep_ret[2]["success"] = Json::Value(Json::objectValue);
  34 + prep_ret[2]["success"]["/lights/1/state/hue"] = 30500;
  35 + EXPECT_CALL(test_light, SendPutRequest(_, "/state"))
  36 + .Times(1)
  37 + .WillOnce(Return(prep_ret));
  38 +
  39 + test_light.getState()["state"]["on"] = true;
  40 + test_light.getState()["state"]["hue"] = 200;
  41 + test_light.getState()["state"]["colormode"] = "hs";
  42 + EXPECT_EQ( true, SimpleColorHueStrategy().setColorHue(200, 4, test_light) );
  43 +
  44 + test_light.getState()["state"]["on"] = false;
  45 + EXPECT_EQ( true, SimpleColorHueStrategy().setColorHue(30500, 6, test_light) );
  46 +}
  47 +
  48 +TEST(SimpleColorHueStrategy, setColorSaturation)
  49 +{
  50 + using namespace ::testing;
  51 + std::shared_ptr<MockHttpHandler> handler(std::make_shared<MockHttpHandler>());
  52 + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80))
  53 + .Times(AtLeast(1))
  54 + .WillRepeatedly(Return(Json::Value(Json::objectValue)));
  55 + MockHueLight test_light(handler);
  56 + EXPECT_CALL(test_light, refreshState())
  57 + .Times(AtLeast(1))
  58 + .WillRepeatedly(Return());
  59 + Json::Value prep_ret;
  60 + prep_ret = Json::Value(Json::arrayValue);
  61 + prep_ret[0] = Json::Value(Json::objectValue);
  62 + prep_ret[0]["success"] = Json::Value(Json::objectValue);
  63 + prep_ret[0]["success"]["/lights/1/state/transitiontime"] = 6;
  64 + prep_ret[1] = Json::Value(Json::objectValue);
  65 + prep_ret[1]["success"] = Json::Value(Json::objectValue);
  66 + prep_ret[1]["success"]["/lights/1/state/on"] = true;
  67 + prep_ret[2] = Json::Value(Json::objectValue);
  68 + prep_ret[2]["success"] = Json::Value(Json::objectValue);
  69 + prep_ret[2]["success"]["/lights/1/state/sat"] = 254;
  70 + EXPECT_CALL(test_light, SendPutRequest(_, "/state"))
  71 + .Times(1)
  72 + .WillOnce(Return(prep_ret));
  73 +
  74 + test_light.getState()["state"]["on"] = true;
  75 + test_light.getState()["state"]["sat"] = 100;
  76 + test_light.getState()["state"]["colormode"] = "hs";
  77 + EXPECT_EQ( true, SimpleColorHueStrategy().setColorSaturation(100, 4, test_light) );
  78 +
  79 + test_light.getState()["state"]["on"] = false;
  80 + EXPECT_EQ( true, SimpleColorHueStrategy().setColorSaturation(255, 6, test_light) );
  81 +}
  82 +
  83 +TEST(SimpleColorHueStrategy, setColorHueSaturation)
  84 +{
  85 + using namespace ::testing;
  86 + std::shared_ptr<MockHttpHandler> handler(std::make_shared<MockHttpHandler>());
  87 + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80))
  88 + .Times(AtLeast(1))
  89 + .WillRepeatedly(Return(Json::Value(Json::objectValue)));
  90 + MockHueLight test_light(handler);
  91 + EXPECT_CALL(test_light, refreshState())
  92 + .Times(AtLeast(1))
  93 + .WillRepeatedly(Return());
  94 + Json::Value prep_ret;
  95 + prep_ret = Json::Value(Json::arrayValue);
  96 + prep_ret[0] = Json::Value(Json::objectValue);
  97 + prep_ret[0]["success"] = Json::Value(Json::objectValue);
  98 + prep_ret[0]["success"]["/lights/1/state/transitiontime"] = 6;
  99 + prep_ret[1] = Json::Value(Json::objectValue);
  100 + prep_ret[1]["success"] = Json::Value(Json::objectValue);
  101 + prep_ret[1]["success"]["/lights/1/state/on"] = true;
  102 + prep_ret[2] = Json::Value(Json::objectValue);
  103 + prep_ret[2]["success"] = Json::Value(Json::objectValue);
  104 + prep_ret[2]["success"]["/lights/1/state/hue"] = 30500;
  105 + prep_ret[3] = Json::Value(Json::objectValue);
  106 + prep_ret[3]["success"] = Json::Value(Json::objectValue);
  107 + prep_ret[3]["success"]["/lights/1/state/sat"] = 254;
  108 + EXPECT_CALL(test_light, SendPutRequest(_, "/state"))
  109 + .Times(1)
  110 + .WillOnce(Return(prep_ret));
  111 +
  112 + test_light.getState()["state"]["on"] = true;
  113 + test_light.getState()["state"]["sat"] = 100;
  114 + test_light.getState()["state"]["hue"] = 200;
  115 + test_light.getState()["state"]["colormode"] = "hs";
  116 + EXPECT_EQ( true, SimpleColorHueStrategy().setColorHueSaturation(200, 100, 4, test_light) );
  117 +
  118 + test_light.getState()["state"]["on"] = false;
  119 + EXPECT_EQ( true, SimpleColorHueStrategy().setColorHueSaturation(30500, 255, 6, test_light) );
  120 +}
  121 +
  122 +TEST(SimpleColorHueStrategy, setColorXY)
  123 +{
  124 + using namespace ::testing;
  125 + std::shared_ptr<MockHttpHandler> handler(std::make_shared<MockHttpHandler>());
  126 + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80))
  127 + .Times(AtLeast(1))
  128 + .WillRepeatedly(Return(Json::Value(Json::objectValue)));
  129 + MockHueLight test_light(handler);
  130 + EXPECT_CALL(test_light, refreshState())
  131 + .Times(AtLeast(1))
  132 + .WillRepeatedly(Return());
  133 + Json::Value prep_ret;
  134 + prep_ret = Json::Value(Json::arrayValue);
  135 + prep_ret[0] = Json::Value(Json::objectValue);
  136 + prep_ret[0]["success"] = Json::Value(Json::objectValue);
  137 + prep_ret[0]["success"]["/lights/1/state/transitiontime"] = 6;
  138 + prep_ret[1] = Json::Value(Json::objectValue);
  139 + prep_ret[1]["success"] = Json::Value(Json::objectValue);
  140 + prep_ret[1]["success"]["/lights/1/state/on"] = true;
  141 + prep_ret[2] = Json::Value(Json::objectValue);
  142 + prep_ret[2]["success"] = Json::Value(Json::objectValue);
  143 + prep_ret[2]["success"]["/lights/1/state/xy"][0] = 0.2355;
  144 + prep_ret[2]["success"]["/lights/1/state/xy"][1] = 0.1234;
  145 + EXPECT_CALL(test_light, SendPutRequest(_, "/state"))
  146 + .Times(1)
  147 + .WillOnce(Return(prep_ret));
  148 +
  149 + test_light.getState()["state"]["on"] = true;
  150 + test_light.getState()["state"]["xy"][0] = 0.1;
  151 + test_light.getState()["state"]["xy"][1] = 0.1;
  152 + test_light.getState()["state"]["colormode"] = "xy";
  153 + EXPECT_EQ( true, SimpleColorHueStrategy().setColorXY(0.1, 0.1, 4, test_light) );
  154 +
  155 + test_light.getState()["state"]["on"] = false;
  156 + EXPECT_EQ( true, SimpleColorHueStrategy().setColorXY(0.2355, 0.1234, 6, test_light) );
  157 +}
  158 +
  159 +TEST(SimpleColorHueStrategy, setColorRGB)
  160 +{
  161 + using namespace ::testing;
  162 + std::shared_ptr<MockHttpHandler> handler(std::make_shared<MockHttpHandler>());
  163 + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80))
  164 + .Times(AtLeast(1))
  165 + .WillRepeatedly(Return(Json::Value(Json::objectValue)));
  166 + MockHueLight test_light(handler);
  167 + EXPECT_CALL(test_light, setColorXY(_, _, 4))
  168 + .Times(2)
  169 + .WillRepeatedly(Return(true));
  170 +
  171 + EXPECT_EQ( true, SimpleColorHueStrategy().setColorRGB(128, 128, 128, 4, test_light) );
  172 +
  173 + EXPECT_EQ( true, SimpleColorHueStrategy().setColorRGB(255, 255, 255, 4, test_light) );
  174 +
  175 + EXPECT_CALL(test_light, OffNoRefresh(4))
  176 + .Times(1)
  177 + .WillOnce(Return(true));
  178 + EXPECT_EQ( true, SimpleColorHueStrategy().setColorRGB(0, 0, 0, 4, test_light) );
  179 +}
  180 +
  181 +TEST(SimpleColorHueStrategy, setColorLoop)
  182 +{
  183 + using namespace ::testing;
  184 + std::shared_ptr<MockHttpHandler> handler(std::make_shared<MockHttpHandler>());
  185 + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80))
  186 + .Times(AtLeast(1))
  187 + .WillRepeatedly(Return(Json::Value(Json::objectValue)));
  188 + MockHueLight test_light(handler);
  189 + EXPECT_CALL(test_light, refreshState())
  190 + .Times(AtLeast(1))
  191 + .WillRepeatedly(Return());
  192 + Json::Value prep_ret;
  193 + prep_ret = Json::Value(Json::arrayValue);
  194 + prep_ret[0] = Json::Value(Json::objectValue);
  195 + prep_ret[0]["success"] = Json::Value(Json::objectValue);
  196 + prep_ret[0]["success"]["/lights/1/state/on"] = true;
  197 + prep_ret[1] = Json::Value(Json::objectValue);
  198 + prep_ret[1]["success"] = Json::Value(Json::objectValue);
  199 + prep_ret[1]["success"]["/lights/1/state/effect"] = "colorloop";
  200 + EXPECT_CALL(test_light, SendPutRequest(_, "/state"))
  201 + .Times(1)
  202 + .WillOnce(Return(prep_ret));
  203 +
  204 + test_light.getState()["state"]["on"] = true;
  205 + test_light.getState()["state"]["effect"] = "colorloop";
  206 + EXPECT_EQ( true, SimpleColorHueStrategy().setColorLoop(true, test_light) );
  207 +
  208 + test_light.getState()["state"]["on"] = false;
  209 + test_light.getState()["state"]["effect"] = "none";
  210 + EXPECT_EQ( true, SimpleColorHueStrategy().setColorLoop(true, test_light) );
  211 +}
  212 +
  213 +TEST(SimpleColorHueStrategy, alertHueSaturation)
  214 +{
  215 + using namespace ::testing;
  216 + std::shared_ptr<MockHttpHandler> handler(std::make_shared<MockHttpHandler>());
  217 + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80))
  218 + .Times(AtLeast(1))
  219 + .WillRepeatedly(Return(Json::Value(Json::objectValue)));
  220 + MockHueLight test_light(handler);
  221 + EXPECT_CALL(test_light, refreshState())
  222 + .Times(AtLeast(1))
  223 + .WillRepeatedly(Return());
  224 +
  225 + test_light.getState()["state"]["colormode"] = "invalid";
  226 + EXPECT_EQ( false, SimpleColorHueStrategy().alertHueSaturation(30000, 128, test_light) );
  227 +
  228 + EXPECT_CALL(test_light, setColorHueSaturation(_, _, 1))
  229 + .Times(AtLeast(2))
  230 + .WillOnce(Return(false))
  231 + .WillRepeatedly(Return(true));
  232 + test_light.getState()["state"]["colormode"] = "hs";
  233 + test_light.getState()["state"]["on"] = true;
  234 + test_light.getState()["state"]["sat"] = 100;
  235 + test_light.getState()["state"]["hue"] = 200;
  236 + EXPECT_EQ( false, SimpleColorHueStrategy().alertHueSaturation(200, 100, test_light) );
  237 +
  238 + EXPECT_CALL(test_light, alert())
  239 + .Times(AtLeast(2))
  240 + .WillOnce(Return(false))
  241 + .WillRepeatedly(Return(true));
  242 + EXPECT_EQ( false, SimpleColorHueStrategy().alertHueSaturation(200, 100, test_light) );
  243 +
  244 + EXPECT_EQ( true, SimpleColorHueStrategy().alertHueSaturation(200, 100, test_light) );
  245 +
  246 + EXPECT_CALL(test_light, OffNoRefresh(_))
  247 + .Times(AtLeast(1))
  248 + .WillRepeatedly(Return(true));
  249 + test_light.getState()["state"]["on"] = false;
  250 + EXPECT_EQ( true, SimpleColorHueStrategy().alertHueSaturation(200, 100, test_light) );
  251 +
  252 + EXPECT_CALL(test_light, setColorHueSaturation(_, _, 1))
  253 + .Times(AtLeast(2))
  254 + .WillOnce(Return(false))
  255 + .WillRepeatedly(Return(true));
  256 + test_light.getState()["state"]["colormode"] = "xy";
  257 + test_light.getState()["state"]["on"] = true;
  258 + test_light.getState()["state"]["xy"][0] = 0.1;
  259 + test_light.getState()["state"]["xy"][1] = 0.1;
  260 + EXPECT_EQ( false, SimpleColorHueStrategy().alertHueSaturation(200, 100, test_light) );
  261 +
  262 + EXPECT_CALL(test_light, alert())
  263 + .Times(AtLeast(2))
  264 + .WillOnce(Return(false))
  265 + .WillRepeatedly(Return(true));
  266 + EXPECT_EQ( false, SimpleColorHueStrategy().alertHueSaturation(200, 100, test_light) );
  267 +
  268 + EXPECT_CALL(test_light, setColorXY(_, _, 1))
  269 + .Times(AtLeast(2))
  270 + .WillRepeatedly(Return(true));
  271 + EXPECT_EQ( true, SimpleColorHueStrategy().alertHueSaturation(200, 100, test_light) );
  272 +
  273 + EXPECT_CALL(test_light, OffNoRefresh(_))
  274 + .Times(AtLeast(1))
  275 + .WillRepeatedly(Return(true));
  276 + test_light.getState()["state"]["on"] = false;
  277 + EXPECT_EQ( true, SimpleColorHueStrategy().alertHueSaturation(200, 100, test_light) );
  278 +}
  279 +
  280 +TEST(SimpleColorHueStrategy, alertXY)
  281 +{
  282 + using namespace ::testing;
  283 + std::shared_ptr<MockHttpHandler> handler(std::make_shared<MockHttpHandler>());
  284 + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80))
  285 + .Times(AtLeast(1))
  286 + .WillRepeatedly(Return(Json::Value(Json::objectValue)));
  287 + MockHueLight test_light(handler);
  288 + EXPECT_CALL(test_light, refreshState())
  289 + .Times(AtLeast(1))
  290 + .WillRepeatedly(Return());
  291 +
  292 + test_light.getState()["state"]["colormode"] = "invalid";
  293 + EXPECT_EQ( false, SimpleColorHueStrategy().alertXY(0.1, 0.1, test_light) );
  294 +
  295 + EXPECT_CALL(test_light, setColorXY(_, _, 1))
  296 + .Times(AtLeast(2))
  297 + .WillOnce(Return(false))
  298 + .WillRepeatedly(Return(true));
  299 + test_light.getState()["state"]["colormode"] = "hs";
  300 + test_light.getState()["state"]["on"] = true;
  301 + test_light.getState()["state"]["xy"][0] = 0.1;
  302 + test_light.getState()["state"]["xy"][1] = 0.1;
  303 + test_light.getState()["state"]["sat"] = 100;
  304 + test_light.getState()["state"]["hue"] = 200;
  305 + EXPECT_EQ( false, SimpleColorHueStrategy().alertXY(0.1, 0.1, test_light) );
  306 +
  307 + EXPECT_CALL(test_light, alert())
  308 + .Times(AtLeast(2))
  309 + .WillOnce(Return(false))
  310 + .WillRepeatedly(Return(true));
  311 + EXPECT_EQ( false, SimpleColorHueStrategy().alertXY(0.1, 0.1, test_light) );
  312 +
  313 + EXPECT_CALL(test_light, setColorHueSaturation(_, _, 1))
  314 + .Times(AtLeast(2))
  315 + .WillRepeatedly(Return(true));
  316 + EXPECT_EQ( true, SimpleColorHueStrategy().alertXY(0.1, 0.1, test_light) );
  317 +
  318 + EXPECT_CALL(test_light, OffNoRefresh(_))
  319 + .Times(AtLeast(1))
  320 + .WillRepeatedly(Return(true));
  321 + test_light.getState()["state"]["on"] = false;
  322 + EXPECT_EQ( true, SimpleColorHueStrategy().alertXY(0.1, 0.1, test_light) );
  323 +
  324 + EXPECT_CALL(test_light, setColorXY(_, _, 1))
  325 + .Times(AtLeast(2))
  326 + .WillOnce(Return(false))
  327 + .WillRepeatedly(Return(true));
  328 + test_light.getState()["state"]["colormode"] = "xy";
  329 + test_light.getState()["state"]["on"] = true;
  330 + EXPECT_EQ( false, SimpleColorHueStrategy().alertXY(0.1, 0.1, test_light) );
  331 +
  332 + EXPECT_CALL(test_light, alert())
  333 + .Times(AtLeast(2))
  334 + .WillOnce(Return(false))
  335 + .WillRepeatedly(Return(true));
  336 + EXPECT_EQ( false, SimpleColorHueStrategy().alertXY(0.1, 0.1, test_light) );
  337 +
  338 + EXPECT_EQ( true, SimpleColorHueStrategy().alertXY(0.1, 0.1, test_light) );
  339 +
  340 + EXPECT_CALL(test_light, OffNoRefresh(_))
  341 + .Times(AtLeast(1))
  342 + .WillRepeatedly(Return(true));
  343 + test_light.getState()["state"]["on"] = false;
  344 + EXPECT_EQ( true, SimpleColorHueStrategy().alertXY(0.1, 0.1, test_light) );
  345 +}
  346 +
  347 +TEST(SimpleColorHueStrategy, alertRGB)
  348 +{
  349 + using namespace ::testing;
  350 + std::shared_ptr<MockHttpHandler> handler(std::make_shared<MockHttpHandler>());
  351 + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80))
  352 + .Times(AtLeast(1))
  353 + .WillRepeatedly(Return(Json::Value(Json::objectValue)));
  354 + MockHueLight test_light(handler);
  355 + EXPECT_CALL(test_light, refreshState())
  356 + .Times(AtLeast(1))
  357 + .WillRepeatedly(Return());
  358 +
  359 + test_light.getState()["state"]["colormode"] = "invalid";
  360 + EXPECT_EQ( false, SimpleColorHueStrategy().alertRGB(128, 128, 128, test_light) );
  361 +
  362 + EXPECT_CALL(test_light, setColorRGB(_, _, _, 1))
  363 + .Times(AtLeast(2))
  364 + .WillOnce(Return(false))
  365 + .WillRepeatedly(Return(true));
  366 + test_light.getState()["state"]["colormode"] = "hs";
  367 + test_light.getState()["state"]["on"] = true;
  368 + test_light.getState()["state"]["sat"] = 100;
  369 + test_light.getState()["state"]["hue"] = 200;
  370 + EXPECT_EQ( false, SimpleColorHueStrategy().alertRGB(128, 128, 128, test_light) );
  371 +
  372 + EXPECT_CALL(test_light, alert())
  373 + .Times(AtLeast(2))
  374 + .WillOnce(Return(false))
  375 + .WillRepeatedly(Return(true));
  376 + EXPECT_EQ( false, SimpleColorHueStrategy().alertRGB(128, 128, 128, test_light) );
  377 +
  378 + EXPECT_CALL(test_light, setColorHueSaturation(_, _, 1))
  379 + .Times(AtLeast(2))
  380 + .WillRepeatedly(Return(true));
  381 + EXPECT_EQ( true, SimpleColorHueStrategy().alertRGB(128, 128, 128, test_light) );
  382 +
  383 + EXPECT_CALL(test_light, OffNoRefresh(_))
  384 + .Times(AtLeast(1))
  385 + .WillRepeatedly(Return(true));
  386 + test_light.getState()["state"]["on"] = false;
  387 + EXPECT_EQ( true, SimpleColorHueStrategy().alertRGB(128, 128, 128, test_light) );
  388 +
  389 + EXPECT_CALL(test_light, setColorRGB(_, _, _, 1))
  390 + .Times(AtLeast(2))
  391 + .WillOnce(Return(false))
  392 + .WillRepeatedly(Return(true));
  393 + test_light.getState()["state"]["colormode"] = "xy";
  394 + test_light.getState()["state"]["on"] = true;
  395 + test_light.getState()["state"]["xy"][0] = 0.1;
  396 + test_light.getState()["state"]["xy"][1] = 0.1;
  397 + EXPECT_EQ( false, SimpleColorHueStrategy().alertRGB(128, 128, 128, test_light) );
  398 +
  399 + EXPECT_CALL(test_light, alert())
  400 + .Times(AtLeast(2))
  401 + .WillOnce(Return(false))
  402 + .WillRepeatedly(Return(true));
  403 + EXPECT_EQ( false, SimpleColorHueStrategy().alertRGB(128, 128, 128, test_light) );
  404 +
  405 + EXPECT_CALL(test_light, setColorXY(_, _, 1))
  406 + .Times(AtLeast(2))
  407 + .WillRepeatedly(Return(true));
  408 + EXPECT_EQ( true, SimpleColorHueStrategy().alertRGB(128, 128, 128, test_light) );
  409 +
  410 + EXPECT_CALL(test_light, OffNoRefresh(_))
  411 + .Times(AtLeast(1))
  412 + .WillRepeatedly(Return(true));
  413 + test_light.getState()["state"]["on"] = false;
  414 + EXPECT_EQ( true, SimpleColorHueStrategy().alertRGB(128, 128, 128, test_light) );
  415 +}
  416 +
  417 +TEST(SimpleColorHueStrategy, getColorHueSaturation)
  418 +{
  419 + using namespace ::testing;
  420 + std::shared_ptr<MockHttpHandler> handler(std::make_shared<MockHttpHandler>());
  421 + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80))
  422 + .Times(AtLeast(1))
  423 + .WillRepeatedly(Return(Json::Value(Json::objectValue)));
  424 + MockHueLight test_light(handler);
  425 + EXPECT_CALL(test_light, refreshState())
  426 + .Times(AtLeast(1))
  427 + .WillRepeatedly(Return());
  428 +
  429 + test_light.getState()["state"]["hue"] = 5000;
  430 + test_light.getState()["state"]["sat"] = 128;
  431 + EXPECT_EQ( std::make_pair(static_cast<uint16_t>(5000), static_cast<uint8_t>(128)), SimpleColorHueStrategy().getColorHueSaturation(test_light) );
  432 + test_light.getState()["state"]["hue"] = 50000;
  433 + test_light.getState()["state"]["sat"] = 158;
  434 + EXPECT_EQ( std::make_pair(static_cast<uint16_t>(50000), static_cast<uint8_t>(158)), SimpleColorHueStrategy().getColorHueSaturation(static_cast<const HueLight>(test_light)) );
  435 +}
  436 +
  437 +TEST(SimpleColorHueStrategy, getColorXY)
  438 +{
  439 + using namespace ::testing;
  440 + std::shared_ptr<MockHttpHandler> handler(std::make_shared<MockHttpHandler>());
  441 + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80))
  442 + .Times(AtLeast(1))
  443 + .WillRepeatedly(Return(Json::Value(Json::objectValue)));
  444 + MockHueLight test_light(handler);
  445 + EXPECT_CALL(test_light, refreshState())
  446 + .Times(AtLeast(1))
  447 + .WillRepeatedly(Return());
  448 +
  449 + test_light.getState()["state"]["xy"][0] = 0.1234;
  450 + test_light.getState()["state"]["xy"][1] = 0.1234;
  451 + EXPECT_EQ( std::make_pair(static_cast<float>(0.1234), static_cast<float>(0.1234)), SimpleColorHueStrategy().getColorXY(test_light) );
  452 + test_light.getState()["state"]["xy"][0] = 0.12;
  453 + test_light.getState()["state"]["xy"][1] = 0.6458;
  454 + EXPECT_EQ( std::make_pair(static_cast<float>(0.12), static_cast<float>(0.6458)), SimpleColorHueStrategy().getColorXY(static_cast<const HueLight>(test_light)) );
  455 +}
hueplusplus/test/test_SimpleColorTemperatureStrategy.cpp 0 โ†’ 100755
  1 +#include <gtest/gtest.h>
  2 +#include <gmock/gmock.h>
  3 +
  4 +#include "../include/SimpleColorTemperatureStrategy.h"
  5 +#include "../include/json/json.h"
  6 +#include "mocks/mock_HttpHandler.h"
  7 +#include "mocks/mock_HueLight.h"
  8 +#include "testhelper.h"
  9 +
  10 +#include <iostream>
  11 +#include <memory>
  12 +#include <string>
  13 +
  14 +TEST(SimpleColorTemperatureStrategy, setColorTemperature)
  15 +{
  16 + using namespace ::testing;
  17 + std::shared_ptr<MockHttpHandler> handler(std::make_shared<MockHttpHandler>());
  18 + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80))
  19 + .Times(AtLeast(1))
  20 + .WillRepeatedly(Return(Json::Value(Json::objectValue)));
  21 + MockHueLight test_light(handler);
  22 + EXPECT_CALL(test_light, refreshState())
  23 + .Times(AtLeast(1))
  24 + .WillRepeatedly(Return());
  25 + Json::Value prep_ret;
  26 + prep_ret = Json::Value(Json::arrayValue);
  27 + prep_ret[0] = Json::Value(Json::objectValue);
  28 + prep_ret[0]["success"] = Json::Value(Json::objectValue);
  29 + prep_ret[0]["success"]["/lights/1/state/transitiontime"] = 6;
  30 + prep_ret[1] = Json::Value(Json::objectValue);
  31 + prep_ret[1]["success"] = Json::Value(Json::objectValue);
  32 + prep_ret[1]["success"]["/lights/1/state/on"] = true;
  33 + prep_ret[2] = Json::Value(Json::objectValue);
  34 + prep_ret[2]["success"] = Json::Value(Json::objectValue);
  35 + prep_ret[2]["success"]["/lights/1/state/ct"] = 155;
  36 + EXPECT_CALL(test_light, SendPutRequest(_, "/state"))
  37 + .Times(1)
  38 + .WillOnce(Return(prep_ret));
  39 +
  40 + test_light.getState()["state"]["on"] = true;
  41 + test_light.getState()["state"]["ct"] = 200;
  42 + EXPECT_EQ( true, SimpleColorTemperatureStrategy().setColorTemperature(200, 4, test_light) );
  43 +
  44 + test_light.getState()["state"]["on"] = false;
  45 + EXPECT_EQ( true, SimpleColorTemperatureStrategy().setColorTemperature(155, 6, test_light) );
  46 +
  47 + prep_ret[2]["success"]["/lights/1/state/ct"] = 153;
  48 + EXPECT_CALL(test_light, SendPutRequest(_, "/state"))
  49 + .Times(1)
  50 + .WillOnce(Return(prep_ret));
  51 + EXPECT_EQ( true, SimpleColorTemperatureStrategy().setColorTemperature(0, 6, test_light) );
  52 +
  53 + prep_ret[2]["success"]["/lights/1/state/ct"] = 500;
  54 + EXPECT_CALL(test_light, SendPutRequest(_, "/state"))
  55 + .Times(1)
  56 + .WillOnce(Return(prep_ret));
  57 + EXPECT_EQ( true, SimpleColorTemperatureStrategy().setColorTemperature(600, 6, test_light) );
  58 +}
  59 +
  60 +TEST(SimpleColorTemperatureStrategy, alertTemperature)
  61 +{
  62 + using namespace ::testing;
  63 + std::shared_ptr<MockHttpHandler> handler(std::make_shared<MockHttpHandler>());
  64 + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80))
  65 + .Times(AtLeast(1))
  66 + .WillRepeatedly(Return(Json::Value(Json::objectValue)));
  67 + MockHueLight test_light(handler);
  68 + EXPECT_CALL(test_light, refreshState())
  69 + .Times(AtLeast(1))
  70 + .WillRepeatedly(Return());
  71 +
  72 + test_light.getState()["state"]["colormode"] = "invalid";
  73 + EXPECT_EQ( false, SimpleColorTemperatureStrategy().alertTemperature(400, test_light) );
  74 +
  75 + EXPECT_CALL(test_light, setColorTemperature(_, _))
  76 + .Times(AtLeast(2))
  77 + .WillOnce(Return(false))
  78 + .WillRepeatedly(Return(true));
  79 + test_light.getState()["state"]["colormode"] = "ct";
  80 + test_light.getState()["state"]["on"] = true;
  81 + test_light.getState()["state"]["ct"] = 200;
  82 + EXPECT_EQ( false, SimpleColorTemperatureStrategy().alertTemperature(400, test_light) );
  83 +
  84 + EXPECT_CALL(test_light, alert())
  85 + .Times(AtLeast(2))
  86 + .WillOnce(Return(false))
  87 + .WillRepeatedly(Return(true));
  88 + EXPECT_EQ( false, SimpleColorTemperatureStrategy().alertTemperature(400, test_light) );
  89 +
  90 + EXPECT_EQ( true, SimpleColorTemperatureStrategy().alertTemperature(400, test_light) );
  91 +
  92 + EXPECT_CALL(test_light, OffNoRefresh(_))
  93 + .Times(AtLeast(1))
  94 + .WillRepeatedly(Return(true));
  95 + test_light.getState()["state"]["on"] = false;
  96 + EXPECT_EQ( true, SimpleColorTemperatureStrategy().alertTemperature(400, test_light) );
  97 +}
  98 +
  99 +TEST(SimpleColorTemperatureStrategy, getColorTemperature)
  100 +{
  101 + using namespace ::testing;
  102 + std::shared_ptr<MockHttpHandler> handler(std::make_shared<MockHttpHandler>());
  103 + EXPECT_CALL(*handler, GETJson("/api/" + bridge_username + "/lights/1", Json::Value(Json::objectValue), bridge_ip, 80))
  104 + .Times(AtLeast(1))
  105 + .WillRepeatedly(Return(Json::Value(Json::objectValue)));
  106 + MockHueLight test_light(handler);
  107 + EXPECT_CALL(test_light, refreshState())
  108 + .Times(AtLeast(1))
  109 + .WillRepeatedly(Return());
  110 +
  111 + test_light.getState()["state"]["ct"] = 200;
  112 + EXPECT_EQ( 200, SimpleColorTemperatureStrategy().getColorTemperature(test_light) );
  113 + test_light.getState()["state"]["ct"] = 500;
  114 + EXPECT_EQ( 500, SimpleColorTemperatureStrategy().getColorTemperature(static_cast<const HueLight>(test_light)) );
  115 +}
hueplusplus/test/testhelper.h 100644 โ†’ 100755