Commit 91594d4748aa6cf25992da1f53c7199e149d5ae3
Committed by
Moritz Wirger
1 parent
c26cf227
Add tests for Group getters.
Showing
5 changed files
with
203 additions
and
4 deletions
include/hueplusplus/Group.h
src/Group.cpp
| ... | ... | @@ -2,8 +2,8 @@ |
| 2 | 2 | |
| 3 | 3 | #include "hueplusplus/HueExceptionMacro.h" |
| 4 | 4 | |
| 5 | -hueplusplus::Group::Group(int id, const HueCommandAPI& commands) | |
| 6 | - : id(id), state("/groups/" + std::to_string(id), commands, std::chrono::seconds(10)), commands(commands) | |
| 5 | +hueplusplus::Group::Group(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration) | |
| 6 | + : id(id), state("/groups/" + std::to_string(id), commands, refreshDuration), commands(commands) | |
| 7 | 7 | { |
| 8 | 8 | state.Refresh(); |
| 9 | 9 | } | ... | ... |
src/Hue.cpp
| ... | ... | @@ -304,7 +304,7 @@ Group& Hue::getGroup(int id) |
| 304 | 304 | std::cerr << "Error in Hue getGroup(): group with id " << id << " is not valid\n"; |
| 305 | 305 | throw HueException(CURRENT_FILE_INFO, "Group id is not valid"); |
| 306 | 306 | } |
| 307 | - return groups.emplace(id, Group(id, commands)).first->second; | |
| 307 | + return groups.emplace(id, Group(id, commands, stateCache.GetRefreshDuration())).first->second; | |
| 308 | 308 | } |
| 309 | 309 | |
| 310 | 310 | bool Hue::groupExists(int id) | ... | ... |
test/CMakeLists.txt
test/test_Group.cpp
0 → 100644
| 1 | +/** | |
| 2 | + \file test_Group.cpp | |
| 3 | + Copyright Notice\n | |
| 4 | + Copyright (C) 2020 Jan Rogall - developer\n | |
| 5 | + Copyright (C) 2020 Moritz Wirger - developer\n | |
| 6 | + | |
| 7 | + This file is part of hueplusplus. | |
| 8 | + | |
| 9 | + hueplusplus is free software: you can redistribute it and/or modify | |
| 10 | + it under the terms of the GNU Lesser General Public License as published by | |
| 11 | + the Free Software Foundation, either version 3 of the License, or | |
| 12 | + (at your option) any later version. | |
| 13 | + | |
| 14 | + hueplusplus is distributed in the hope that it will be useful, | |
| 15 | + but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 17 | + GNU Lesser General Public License for more details. | |
| 18 | + | |
| 19 | + You should have received a copy of the GNU Lesser General Public License | |
| 20 | + along with hueplusplus. If not, see <http://www.gnu.org/licenses/>. | |
| 21 | +**/ | |
| 22 | + | |
| 23 | +#include <gtest/gtest.h> | |
| 24 | + | |
| 25 | +#include "testhelper.h" | |
| 26 | + | |
| 27 | +#include "hueplusplus/Group.h" | |
| 28 | +#include "mocks/mock_HttpHandler.h" | |
| 29 | + | |
| 30 | +using namespace hueplusplus; | |
| 31 | +using namespace testing; | |
| 32 | + | |
| 33 | +class GroupTest : public Test | |
| 34 | +{ | |
| 35 | +protected: | |
| 36 | + const std::string groupName = "Group 1"; | |
| 37 | + const std::string type = "Room"; | |
| 38 | + const std::string roomType = "Bedroom"; | |
| 39 | + const bool on = true; | |
| 40 | + const int bri = 254; | |
| 41 | + const int hue = 10000; | |
| 42 | + const int sat = 254; | |
| 43 | + const std::string effect = "none"; | |
| 44 | + const float x = 0.5f; | |
| 45 | + const float y = 0.6f; | |
| 46 | + const int ct = 250; | |
| 47 | + const std::string alert = "none"; | |
| 48 | + const std::string colormode = "ct"; | |
| 49 | + const bool any_on = true; | |
| 50 | + const bool all_on = false; | |
| 51 | + | |
| 52 | + std::shared_ptr<MockHttpHandler> handler; | |
| 53 | + HueCommandAPI commands; | |
| 54 | + nlohmann::json groupState; | |
| 55 | + | |
| 56 | +protected: | |
| 57 | + GroupTest() | |
| 58 | + : handler(std::make_shared<MockHttpHandler>()), | |
| 59 | + commands(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler), | |
| 60 | + groupState({{"name", groupName}, {"type", type}, {"class", roomType}, {"lights", {"1", "2", "4"}}, | |
| 61 | + {"action", | |
| 62 | + {{"on", on}, {"bri", bri}, {"hue", hue}, {"sat", sat}, {"effect", effect}, | |
| 63 | + {"xy", nlohmann::json::array({x, y})}, {"ct", ct}, {"alert", alert}, {"colormode", colormode}}}, | |
| 64 | + {"state", {{"any_on", any_on}, {"all_on", all_on}}}}) | |
| 65 | + {} | |
| 66 | + | |
| 67 | + void expectGetState(int id) | |
| 68 | + { | |
| 69 | + EXPECT_CALL(*handler, | |
| 70 | + GETJson("/api/" + getBridgeUsername() + "/groups/" + std::to_string(id), nlohmann::json::object(), | |
| 71 | + getBridgeIp(), getBridgePort())) | |
| 72 | + .WillOnce(Return(groupState)); | |
| 73 | + } | |
| 74 | +}; | |
| 75 | + | |
| 76 | +TEST_F(GroupTest, Construtor) | |
| 77 | +{ | |
| 78 | + { | |
| 79 | + const int id = 12; | |
| 80 | + expectGetState(id); | |
| 81 | + Group group(id, commands, std::chrono::seconds(0)); | |
| 82 | + EXPECT_EQ(id, group.getId()); | |
| 83 | + Mock::VerifyAndClearExpectations(handler.get()); | |
| 84 | + } | |
| 85 | + { | |
| 86 | + const int id = 0; | |
| 87 | + expectGetState(id); | |
| 88 | + Group group(id, commands, std::chrono::seconds(0)); | |
| 89 | + EXPECT_EQ(id, group.getId()); | |
| 90 | + Mock::VerifyAndClearExpectations(handler.get()); | |
| 91 | + } | |
| 92 | +} | |
| 93 | + | |
| 94 | +TEST_F(GroupTest, getName) | |
| 95 | +{ | |
| 96 | + const int id = 1; | |
| 97 | + expectGetState(id); | |
| 98 | + Group group(id, commands, std::chrono::seconds(0)); | |
| 99 | + EXPECT_EQ(groupName, Const(group).getName()); | |
| 100 | +} | |
| 101 | + | |
| 102 | +TEST_F(GroupTest, getType) | |
| 103 | +{ | |
| 104 | + const int id = 1; | |
| 105 | + expectGetState(id); | |
| 106 | + Group group(id, commands, std::chrono::seconds(0)); | |
| 107 | + EXPECT_EQ(type, Const(group).getType()); | |
| 108 | +} | |
| 109 | + | |
| 110 | +TEST_F(GroupTest, getLightIds) | |
| 111 | +{ | |
| 112 | + const int id = 1; | |
| 113 | + expectGetState(id); | |
| 114 | + Group group(id, commands, std::chrono::seconds(0)); | |
| 115 | + EXPECT_EQ(std::vector<int>({1, 2, 4}), Const(group).getLightIds()); | |
| 116 | +} | |
| 117 | + | |
| 118 | +TEST_F(GroupTest, getRoomType) | |
| 119 | +{ | |
| 120 | + const int id = 1; | |
| 121 | + expectGetState(id); | |
| 122 | + Group group(id, commands, std::chrono::seconds(0)); | |
| 123 | + EXPECT_EQ(roomType, Const(group).getRoomType()); | |
| 124 | +} | |
| 125 | + | |
| 126 | +TEST_F(GroupTest, getAllOn) | |
| 127 | +{ | |
| 128 | + const int id = 1; | |
| 129 | + expectGetState(id); | |
| 130 | + Group group(id, commands, std::chrono::steady_clock::duration::max()); | |
| 131 | + EXPECT_EQ(all_on, group.getAllOn()); | |
| 132 | + EXPECT_EQ(all_on, Const(group).getAllOn()); | |
| 133 | +} | |
| 134 | + | |
| 135 | +TEST_F(GroupTest, getAnyOn) | |
| 136 | +{ | |
| 137 | + const int id = 1; | |
| 138 | + expectGetState(id); | |
| 139 | + Group group(id, commands, std::chrono::steady_clock::duration::max()); | |
| 140 | + EXPECT_EQ(any_on, group.getAnyOn()); | |
| 141 | + EXPECT_EQ(any_on, Const(group).getAnyOn()); | |
| 142 | +} | |
| 143 | + | |
| 144 | +TEST_F(GroupTest, getActionOn) | |
| 145 | +{ | |
| 146 | + const int id = 1; | |
| 147 | + expectGetState(id); | |
| 148 | + Group group(id, commands, std::chrono::steady_clock::duration::max()); | |
| 149 | + EXPECT_EQ(on, group.getActionOn()); | |
| 150 | + EXPECT_EQ(on, Const(group).getActionOn()); | |
| 151 | +} | |
| 152 | + | |
| 153 | +TEST_F(GroupTest, getActionHueSaturation) | |
| 154 | +{ | |
| 155 | + const int id = 1; | |
| 156 | + expectGetState(id); | |
| 157 | + Group group(id, commands, std::chrono::steady_clock::duration::max()); | |
| 158 | + std::pair<uint16_t, uint8_t> hueSat{hue, sat}; | |
| 159 | + EXPECT_EQ(hueSat, group.getActionHueSaturation()); | |
| 160 | + EXPECT_EQ(hueSat, Const(group).getActionHueSaturation()); | |
| 161 | +} | |
| 162 | + | |
| 163 | +TEST_F(GroupTest, getActionBrightness) | |
| 164 | +{ | |
| 165 | + const int id = 1; | |
| 166 | + expectGetState(id); | |
| 167 | + Group group(id, commands, std::chrono::steady_clock::duration::max()); | |
| 168 | + EXPECT_EQ(bri, group.getActionBrightness()); | |
| 169 | + EXPECT_EQ(bri, Const(group).getActionBrightness()); | |
| 170 | +} | |
| 171 | + | |
| 172 | +TEST_F(GroupTest, getActionColorTemperature) | |
| 173 | +{ | |
| 174 | + const int id = 1; | |
| 175 | + expectGetState(id); | |
| 176 | + Group group(id, commands, std::chrono::steady_clock::duration::max()); | |
| 177 | + EXPECT_EQ(ct, group.getActionColorTemperature()); | |
| 178 | + EXPECT_EQ(ct, Const(group).getActionColorTemperature()); | |
| 179 | +} | |
| 180 | + | |
| 181 | +TEST_F(GroupTest, getActionColorXY) | |
| 182 | +{ | |
| 183 | + const int id = 1; | |
| 184 | + expectGetState(id); | |
| 185 | + Group group(id, commands, std::chrono::steady_clock::duration::max()); | |
| 186 | + std::pair<float, float> xy{x, y}; | |
| 187 | + EXPECT_EQ(xy, group.getActionColorXY()); | |
| 188 | + EXPECT_EQ(xy, Const(group).getActionColorXY()); | |
| 189 | +} | |
| 190 | + | |
| 191 | +TEST_F(GroupTest, getActionColorMode) | |
| 192 | +{ | |
| 193 | + const int id = 1; | |
| 194 | + expectGetState(id); | |
| 195 | + Group group(id, commands, std::chrono::steady_clock::duration::max()); | |
| 196 | + EXPECT_EQ(colormode, group.getActionColorMode()); | |
| 197 | + EXPECT_EQ(colormode, Const(group).getActionColorMode()); | |
| 198 | +} | ... | ... |