From 91594d4748aa6cf25992da1f53c7199e149d5ae3 Mon Sep 17 00:00:00 2001
From: Jojo-1000 <33495614+Jojo-1000@users.noreply.github.com>
Date: Tue, 14 Apr 2020 18:28:05 +0200
Subject: [PATCH] Add tests for Group getters.
---
include/hueplusplus/Group.h | 2 +-
src/Group.cpp | 4 ++--
src/Hue.cpp | 2 +-
test/CMakeLists.txt | 1 +
test/test_Group.cpp | 198 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 203 insertions(+), 4 deletions(-)
create mode 100644 test/test_Group.cpp
diff --git a/include/hueplusplus/Group.h b/include/hueplusplus/Group.h
index c6c9bf5..98a28f8 100644
--- a/include/hueplusplus/Group.h
+++ b/include/hueplusplus/Group.h
@@ -36,7 +36,7 @@ namespace hueplusplus
class Group
{
public:
- Group(int id, const HueCommandAPI& commands);
+ Group(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration);
virtual ~Group() = default;
diff --git a/src/Group.cpp b/src/Group.cpp
index 44b498a..b096d4d 100644
--- a/src/Group.cpp
+++ b/src/Group.cpp
@@ -2,8 +2,8 @@
#include "hueplusplus/HueExceptionMacro.h"
-hueplusplus::Group::Group(int id, const HueCommandAPI& commands)
- : id(id), state("/groups/" + std::to_string(id), commands, std::chrono::seconds(10)), commands(commands)
+hueplusplus::Group::Group(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration)
+ : id(id), state("/groups/" + std::to_string(id), commands, refreshDuration), commands(commands)
{
state.Refresh();
}
diff --git a/src/Hue.cpp b/src/Hue.cpp
index e8d7acc..d1525a8 100644
--- a/src/Hue.cpp
+++ b/src/Hue.cpp
@@ -304,7 +304,7 @@ Group& Hue::getGroup(int id)
std::cerr << "Error in Hue getGroup(): group with id " << id << " is not valid\n";
throw HueException(CURRENT_FILE_INFO, "Group id is not valid");
}
- return groups.emplace(id, Group(id, commands)).first->second;
+ return groups.emplace(id, Group(id, commands, stateCache.GetRefreshDuration())).first->second;
}
bool Hue::groupExists(int id)
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index d077247..fe61852 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -34,6 +34,7 @@ set(TEST_SOURCES
test_BaseHttpHandler.cpp
test_ExtendedColorHueStrategy.cpp
test_ExtendedColorTemperatureStrategy.cpp
+ test_Group.cpp
test_Hue.cpp
test_HueLight.cpp
test_HueCommandAPI.cpp
diff --git a/test/test_Group.cpp b/test/test_Group.cpp
new file mode 100644
index 0000000..e40094a
--- /dev/null
+++ b/test/test_Group.cpp
@@ -0,0 +1,198 @@
+/**
+ \file test_Group.cpp
+ Copyright Notice\n
+ Copyright (C) 2020 Jan Rogall - developer\n
+ Copyright (C) 2020 Moritz Wirger - 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 .
+**/
+
+#include
+
+#include "testhelper.h"
+
+#include "hueplusplus/Group.h"
+#include "mocks/mock_HttpHandler.h"
+
+using namespace hueplusplus;
+using namespace testing;
+
+class GroupTest : public Test
+{
+protected:
+ const std::string groupName = "Group 1";
+ const std::string type = "Room";
+ const std::string roomType = "Bedroom";
+ const bool on = true;
+ const int bri = 254;
+ const int hue = 10000;
+ const int sat = 254;
+ const std::string effect = "none";
+ const float x = 0.5f;
+ const float y = 0.6f;
+ const int ct = 250;
+ const std::string alert = "none";
+ const std::string colormode = "ct";
+ const bool any_on = true;
+ const bool all_on = false;
+
+ std::shared_ptr handler;
+ HueCommandAPI commands;
+ nlohmann::json groupState;
+
+protected:
+ GroupTest()
+ : handler(std::make_shared()),
+ commands(getBridgeIp(), getBridgePort(), getBridgeUsername(), handler),
+ groupState({{"name", groupName}, {"type", type}, {"class", roomType}, {"lights", {"1", "2", "4"}},
+ {"action",
+ {{"on", on}, {"bri", bri}, {"hue", hue}, {"sat", sat}, {"effect", effect},
+ {"xy", nlohmann::json::array({x, y})}, {"ct", ct}, {"alert", alert}, {"colormode", colormode}}},
+ {"state", {{"any_on", any_on}, {"all_on", all_on}}}})
+ {}
+
+ void expectGetState(int id)
+ {
+ EXPECT_CALL(*handler,
+ GETJson("/api/" + getBridgeUsername() + "/groups/" + std::to_string(id), nlohmann::json::object(),
+ getBridgeIp(), getBridgePort()))
+ .WillOnce(Return(groupState));
+ }
+};
+
+TEST_F(GroupTest, Construtor)
+{
+ {
+ const int id = 12;
+ expectGetState(id);
+ Group group(id, commands, std::chrono::seconds(0));
+ EXPECT_EQ(id, group.getId());
+ Mock::VerifyAndClearExpectations(handler.get());
+ }
+ {
+ const int id = 0;
+ expectGetState(id);
+ Group group(id, commands, std::chrono::seconds(0));
+ EXPECT_EQ(id, group.getId());
+ Mock::VerifyAndClearExpectations(handler.get());
+ }
+}
+
+TEST_F(GroupTest, getName)
+{
+ const int id = 1;
+ expectGetState(id);
+ Group group(id, commands, std::chrono::seconds(0));
+ EXPECT_EQ(groupName, Const(group).getName());
+}
+
+TEST_F(GroupTest, getType)
+{
+ const int id = 1;
+ expectGetState(id);
+ Group group(id, commands, std::chrono::seconds(0));
+ EXPECT_EQ(type, Const(group).getType());
+}
+
+TEST_F(GroupTest, getLightIds)
+{
+ const int id = 1;
+ expectGetState(id);
+ Group group(id, commands, std::chrono::seconds(0));
+ EXPECT_EQ(std::vector({1, 2, 4}), Const(group).getLightIds());
+}
+
+TEST_F(GroupTest, getRoomType)
+{
+ const int id = 1;
+ expectGetState(id);
+ Group group(id, commands, std::chrono::seconds(0));
+ EXPECT_EQ(roomType, Const(group).getRoomType());
+}
+
+TEST_F(GroupTest, getAllOn)
+{
+ const int id = 1;
+ expectGetState(id);
+ Group group(id, commands, std::chrono::steady_clock::duration::max());
+ EXPECT_EQ(all_on, group.getAllOn());
+ EXPECT_EQ(all_on, Const(group).getAllOn());
+}
+
+TEST_F(GroupTest, getAnyOn)
+{
+ const int id = 1;
+ expectGetState(id);
+ Group group(id, commands, std::chrono::steady_clock::duration::max());
+ EXPECT_EQ(any_on, group.getAnyOn());
+ EXPECT_EQ(any_on, Const(group).getAnyOn());
+}
+
+TEST_F(GroupTest, getActionOn)
+{
+ const int id = 1;
+ expectGetState(id);
+ Group group(id, commands, std::chrono::steady_clock::duration::max());
+ EXPECT_EQ(on, group.getActionOn());
+ EXPECT_EQ(on, Const(group).getActionOn());
+}
+
+TEST_F(GroupTest, getActionHueSaturation)
+{
+ const int id = 1;
+ expectGetState(id);
+ Group group(id, commands, std::chrono::steady_clock::duration::max());
+ std::pair hueSat{hue, sat};
+ EXPECT_EQ(hueSat, group.getActionHueSaturation());
+ EXPECT_EQ(hueSat, Const(group).getActionHueSaturation());
+}
+
+TEST_F(GroupTest, getActionBrightness)
+{
+ const int id = 1;
+ expectGetState(id);
+ Group group(id, commands, std::chrono::steady_clock::duration::max());
+ EXPECT_EQ(bri, group.getActionBrightness());
+ EXPECT_EQ(bri, Const(group).getActionBrightness());
+}
+
+TEST_F(GroupTest, getActionColorTemperature)
+{
+ const int id = 1;
+ expectGetState(id);
+ Group group(id, commands, std::chrono::steady_clock::duration::max());
+ EXPECT_EQ(ct, group.getActionColorTemperature());
+ EXPECT_EQ(ct, Const(group).getActionColorTemperature());
+}
+
+TEST_F(GroupTest, getActionColorXY)
+{
+ const int id = 1;
+ expectGetState(id);
+ Group group(id, commands, std::chrono::steady_clock::duration::max());
+ std::pair xy{x, y};
+ EXPECT_EQ(xy, group.getActionColorXY());
+ EXPECT_EQ(xy, Const(group).getActionColorXY());
+}
+
+TEST_F(GroupTest, getActionColorMode)
+{
+ const int id = 1;
+ expectGetState(id);
+ Group group(id, commands, std::chrono::steady_clock::duration::max());
+ EXPECT_EQ(colormode, group.getActionColorMode());
+ EXPECT_EQ(colormode, Const(group).getActionColorMode());
+}
--
libgit2 0.21.4