| 1 |
/* |
| 2 |
* This program is free software; you can redistribute it and/or modify |
| 3 |
* it under the terms of the GNU General Public License as published by |
| 4 |
* the Free Software Foundation; either version 2 of the License, or |
| 5 |
* (at your option) any later version. |
| 6 |
* |
| 7 |
* This program is distributed in the hope that it will be useful, |
| 8 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 |
* GNU Library General Public License for more details. |
| 11 |
* |
| 12 |
* You should have received a copy of the GNU General Public License |
| 13 |
* along with this program; if not, write to the Free Software |
| 14 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 15 |
* |
| 16 |
* DeviceManagerTest.cpp |
| 17 |
* Test fixture for the DeviceManager class. |
| 18 |
* Copyright (C) 2005 Simon Newton |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <cppunit/extensions/HelperMacros.h> |
| 22 |
#include <string> |
| 23 |
#include <vector> |
| 24 |
|
| 25 |
#include "ola/Logging.h" |
| 26 |
#include "ola/DmxBuffer.h" |
| 27 |
#include "olad/Plugin.h" |
| 28 |
#include "olad/Port.h" |
| 29 |
#include "olad/PortBroker.h" |
| 30 |
#include "olad/Preferences.h" |
| 31 |
#include "olad/plugin_api/DeviceManager.h" |
| 32 |
#include "olad/plugin_api/PortManager.h" |
| 33 |
#include "olad/plugin_api/TestCommon.h" |
| 34 |
#include "olad/plugin_api/UniverseStore.h" |
| 35 |
#include "ola/testing/TestUtils.h" |
| 36 |
|
| 37 |
|
| 38 |
using ola::AbstractDevice; |
| 39 |
using ola::AbstractPlugin; |
| 40 |
using ola::DeviceManager; |
| 41 |
using ola::DmxBuffer; |
| 42 |
using ola::Port; |
| 43 |
using ola::PortManager; |
| 44 |
using ola::Universe; |
| 45 |
using ola::UniverseStore; |
| 46 |
using std::string; |
| 47 |
using std::vector; |
| 48 |
|
| 49 |
|
| 50 |
class DeviceManagerTest: public CppUnit::TestFixture { |
| 51 |
CPPUNIT_TEST_SUITE(DeviceManagerTest); |
| 52 |
CPPUNIT_TEST(testDeviceManager); |
| 53 |
CPPUNIT_TEST(testRestorePatchings); |
| 54 |
CPPUNIT_TEST(testRestorePriorities); |
| 55 |
CPPUNIT_TEST_SUITE_END(); |
| 56 |
|
| 57 |
public: |
| 58 |
void testDeviceManager(); |
| 59 |
void testRestorePatchings(); |
| 60 |
void testRestorePriorities(); |
| 61 |
}; |
| 62 |
|
| 63 |
|
| 64 |
CPPUNIT_TEST_SUITE_REGISTRATION(DeviceManagerTest); |
| 65 |
|
| 66 |
|
| 67 |
/* |
| 68 |
* Test that we can register devices and retrieve them. |
| 69 |
*/ |
| 70 |
void DeviceManagerTest::testDeviceManager() { |
| 71 |
DeviceManager manager(NULL, NULL); |
| 72 |
OLA_ASSERT_EQ(0u, manager.DeviceCount()); |
| 73 |
|
| 74 |
TestMockPlugin plugin(NULL, ola::OLA_PLUGIN_ARTNET); |
| 75 |
MockDevice orphaned_device(NULL, "orphaned device"); |
| 76 |
MockDevice device1(&plugin, "test device 1"); |
| 77 |
MockDevice device2(&plugin, "test device 2"); |
| 78 |
|
| 79 |
// can't register NULL |
| 80 |
OLA_ASSERT_FALSE(manager.RegisterDevice(NULL)); |
| 81 |
|
| 82 |
// Can't register a device with no unique id |
| 83 |
OLA_ASSERT_FALSE(manager.RegisterDevice(&orphaned_device)); |
| 84 |
|
| 85 |
// register a device |
| 86 |
OLA_ASSERT(manager.RegisterDevice(&device1)); |
| 87 |
// the second time must fail |
| 88 |
OLA_ASSERT_FALSE(manager.RegisterDevice(&device1)); |
| 89 |
|
| 90 |
// register a second device |
| 91 |
OLA_ASSERT(manager.RegisterDevice(&device2)); |
| 92 |
OLA_ASSERT_EQ(2u, manager.DeviceCount()); |
| 93 |
|
| 94 |
vector<ola::device_alias_pair> devices = manager.Devices(); |
| 95 |
OLA_ASSERT_EQ(1u, devices[0].alias); |
| 96 |
OLA_ASSERT_EQ(static_cast<AbstractDevice*>(&device1), devices[0].device); |
| 97 |
OLA_ASSERT_EQ(2u, devices[1].alias); |
| 98 |
OLA_ASSERT_EQ(static_cast<AbstractDevice*>(&device2), devices[1].device); |
| 99 |
|
| 100 |
// test fetching a device by alias |
| 101 |
OLA_ASSERT_EQ(static_cast<AbstractDevice*>(&device1), manager.GetDevice(1)); |
| 102 |
OLA_ASSERT_EQ(static_cast<AbstractDevice*>(&device2), manager.GetDevice(2)); |
| 103 |
OLA_ASSERT_EQ(static_cast<AbstractDevice*>(NULL), manager.GetDevice(3)); |
| 104 |
|
| 105 |
// test fetching a device by id |
| 106 |
ola::device_alias_pair result = manager.GetDevice(device1.UniqueId()); |
| 107 |
OLA_ASSERT_EQ(1u, result.alias); |
| 108 |
OLA_ASSERT_EQ(static_cast<AbstractDevice*>(&device1), result.device); |
| 109 |
result = manager.GetDevice(device2.UniqueId()); |
| 110 |
OLA_ASSERT_EQ(2u, result.alias); |
| 111 |
OLA_ASSERT_EQ(static_cast<AbstractDevice*>(&device2), result.device); |
| 112 |
result = manager.GetDevice("foo"); |
| 113 |
OLA_ASSERT_EQ(DeviceManager::MISSING_DEVICE_ALIAS, result.alias); |
| 114 |
OLA_ASSERT_EQ(static_cast<AbstractDevice*>(NULL), result.device); |
| 115 |
result = manager.GetDevice(""); |
| 116 |
OLA_ASSERT_EQ(DeviceManager::MISSING_DEVICE_ALIAS, result.alias); |
| 117 |
OLA_ASSERT_EQ(static_cast<AbstractDevice*>(NULL), result.device); |
| 118 |
|
| 119 |
// test unregistering null or non-registered device |
| 120 |
OLA_ASSERT_FALSE(manager.UnregisterDevice(NULL)); |
| 121 |
OLA_ASSERT_FALSE(manager.UnregisterDevice(&orphaned_device)); |
| 122 |
|
| 123 |
// unregistering the first device doesn't change the ID of the second |
| 124 |
OLA_ASSERT(manager.UnregisterDevice(&device1)); |
| 125 |
OLA_ASSERT_EQ(1u, manager.DeviceCount()); |
| 126 |
OLA_ASSERT_EQ(static_cast<AbstractDevice*>(NULL), manager.GetDevice(1)); |
| 127 |
OLA_ASSERT_EQ(static_cast<AbstractDevice*>(&device2), manager.GetDevice(2)); |
| 128 |
|
| 129 |
devices = manager.Devices(); |
| 130 |
OLA_ASSERT_EQ((size_t) 1, devices.size()); |
| 131 |
OLA_ASSERT_EQ(2u, devices[0].alias); |
| 132 |
OLA_ASSERT_EQ(static_cast<AbstractDevice*>(&device2), devices[0].device); |
| 133 |
|
| 134 |
// unregister by id |
| 135 |
OLA_ASSERT_FALSE(manager.UnregisterDevice(device1.UniqueId())); |
| 136 |
OLA_ASSERT(manager.UnregisterDevice(device2.UniqueId())); |
| 137 |
OLA_ASSERT_EQ(0u, manager.DeviceCount()); |
| 138 |
manager.UnregisterAllDevices(); |
| 139 |
|
| 140 |
// add one back and check that ids reset |
| 141 |
OLA_ASSERT(manager.RegisterDevice(&device1)); |
| 142 |
OLA_ASSERT_EQ(1u, manager.DeviceCount()); |
| 143 |
devices = manager.Devices(); |
| 144 |
OLA_ASSERT_EQ(1u, devices[0].alias); |
| 145 |
OLA_ASSERT_EQ(static_cast<AbstractDevice*>(&device1), devices[0].device); |
| 146 |
OLA_ASSERT_EQ(static_cast<AbstractDevice*>(&device1), manager.GetDevice(1)); |
| 147 |
result = manager.GetDevice(device1.UniqueId()); |
| 148 |
OLA_ASSERT_EQ(1u, result.alias); |
| 149 |
OLA_ASSERT_EQ(static_cast<AbstractDevice*>(&device1), result.device); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
/* |
| 154 |
* Check that we restore the port patchings |
| 155 |
*/ |
| 156 |
void DeviceManagerTest::testRestorePatchings() { |
| 157 |
ola::MemoryPreferencesFactory prefs_factory; |
| 158 |
UniverseStore uni_store(NULL, NULL); |
| 159 |
ola::PortBroker broker; |
| 160 |
PortManager port_manager(&uni_store, &broker); |
| 161 |
DeviceManager manager(&prefs_factory, &port_manager); |
| 162 |
OLA_ASSERT_EQ(0u, manager.DeviceCount()); |
| 163 |
|
| 164 |
ola::Preferences *prefs = prefs_factory.NewPreference("port"); |
| 165 |
OLA_ASSERT(prefs); |
| 166 |
// Use a hyphen to confirm we can parse these correctly |
| 167 |
prefs->SetValue("2-test-device-1-I-1", "1"); |
| 168 |
prefs->SetValue("2-test-device-1-O-1", "3"); |
| 169 |
|
| 170 |
TestMockPlugin plugin(NULL, ola::OLA_PLUGIN_ARTNET); |
| 171 |
MockDevice device1(&plugin, "test-device-1"); |
| 172 |
TestMockInputPort input_port(&device1, 1, NULL); |
| 173 |
TestMockOutputPort output_port(&device1, 1); |
| 174 |
device1.AddPort(&input_port); |
| 175 |
device1.AddPort(&output_port); |
| 176 |
|
| 177 |
OLA_ASSERT(manager.RegisterDevice(&device1)); |
| 178 |
OLA_ASSERT_EQ(1u, manager.DeviceCount()); |
| 179 |
OLA_ASSERT(input_port.GetUniverse()); |
| 180 |
OLA_ASSERT_EQ(input_port.GetUniverse()->UniverseId(), 1u); |
| 181 |
OLA_ASSERT(output_port.GetUniverse()); |
| 182 |
OLA_ASSERT_EQ(output_port.GetUniverse()->UniverseId(), 3u); |
| 183 |
|
| 184 |
// Now check that patching a universe saves the settings |
| 185 |
Universe *uni = uni_store.GetUniverseOrCreate(10); |
| 186 |
OLA_ASSERT(uni); |
| 187 |
input_port.SetUniverse(uni); |
| 188 |
|
| 189 |
// unregister all |
| 190 |
manager.UnregisterAllDevices(); |
| 191 |
OLA_ASSERT_EQ(0u, manager.DeviceCount()); |
| 192 |
|
| 193 |
OLA_ASSERT_EQ(string("10"), prefs->GetValue("2-test-device-1-I-1")); |
| 194 |
OLA_ASSERT_EQ(string("3"), prefs->GetValue("2-test-device-1-O-1")); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
/* |
| 199 |
* Test that port priorities are restored correctly. |
| 200 |
*/ |
| 201 |
void DeviceManagerTest::testRestorePriorities() { |
| 202 |
ola::MemoryPreferencesFactory prefs_factory; |
| 203 |
UniverseStore uni_store(NULL, NULL); |
| 204 |
ola::PortBroker broker; |
| 205 |
PortManager port_manager(&uni_store, &broker); |
| 206 |
DeviceManager manager(&prefs_factory, &port_manager); |
| 207 |
OLA_ASSERT_EQ(0u, manager.DeviceCount()); |
| 208 |
|
| 209 |
ola::Preferences *prefs = prefs_factory.NewPreference("port"); |
| 210 |
OLA_ASSERT(prefs); |
| 211 |
prefs->SetValue("2-test_device_1-I-1_priority_mode", "0"); |
| 212 |
prefs->SetValue("2-test_device_1-I-1_priority_value", "120"); |
| 213 |
prefs->SetValue("2-test_device_1-O-1_priority_mode", "0"); |
| 214 |
prefs->SetValue("2-test_device_1-O-1_priority_value", "140"); |
| 215 |
prefs->SetValue("2-test_device_1-I-2_priority_mode", "1"); // override mode |
| 216 |
prefs->SetValue("2-test_device_1-I-2_priority_value", "160"); |
| 217 |
prefs->SetValue("2-test_device_1-O-2_priority_mode", "1"); // override mode |
| 218 |
prefs->SetValue("2-test_device_1-O-2_priority_value", "180"); |
| 219 |
prefs->SetValue("2-test_device_1-I-3_priority_mode", "0"); // inherit mode |
| 220 |
// invalid priority |
| 221 |
prefs->SetValue("2-test_device_1-I-3_priority_value", "210"); |
| 222 |
prefs->SetValue("2-test_device_1-O-3_priority_mode", "0"); // inherit mode |
| 223 |
prefs->SetValue("2-test_device_1-O-3_priority_value", "180"); |
| 224 |
|
| 225 |
TestMockPlugin plugin(NULL, ola::OLA_PLUGIN_ARTNET); |
| 226 |
MockDevice device1(&plugin, "test_device_1"); |
| 227 |
// these ports don't support priorities. |
| 228 |
TestMockInputPort input_port(&device1, 1, NULL); |
| 229 |
TestMockOutputPort output_port(&device1, 1); |
| 230 |
// these devices support priorities |
| 231 |
TestMockPriorityInputPort input_port2(&device1, 2, NULL); |
| 232 |
TestMockPriorityOutputPort output_port2(&device1, 2); |
| 233 |
TestMockPriorityInputPort input_port3(&device1, 3, NULL); |
| 234 |
TestMockPriorityOutputPort output_port3(&device1, 3); |
| 235 |
device1.AddPort(&input_port); |
| 236 |
device1.AddPort(&output_port); |
| 237 |
device1.AddPort(&input_port2); |
| 238 |
device1.AddPort(&output_port2); |
| 239 |
device1.AddPort(&input_port3); |
| 240 |
device1.AddPort(&output_port3); |
| 241 |
|
| 242 |
OLA_ASSERT(manager.RegisterDevice(&device1)); |
| 243 |
OLA_ASSERT_EQ(1u, manager.DeviceCount()); |
| 244 |
OLA_ASSERT_EQ(ola::CAPABILITY_STATIC, input_port.PriorityCapability()); |
| 245 |
OLA_ASSERT_EQ(ola::PRIORITY_MODE_STATIC, input_port.GetPriorityMode()); |
| 246 |
OLA_ASSERT_EQ((uint8_t) 120, input_port.GetPriority()); |
| 247 |
|
| 248 |
OLA_ASSERT_EQ(ola::CAPABILITY_NONE, output_port.PriorityCapability()); |
| 249 |
OLA_ASSERT_EQ(ola::PRIORITY_MODE_INHERIT, output_port.GetPriorityMode()); |
| 250 |
OLA_ASSERT_EQ((uint8_t) 100, output_port.GetPriority()); |
| 251 |
|
| 252 |
// these ports support priorities |
| 253 |
OLA_ASSERT_EQ(ola::CAPABILITY_FULL, input_port2.PriorityCapability()); |
| 254 |
OLA_ASSERT_EQ(ola::PRIORITY_MODE_STATIC, input_port2.GetPriorityMode()); |
| 255 |
OLA_ASSERT_EQ((uint8_t) 160, input_port2.GetPriority()); |
| 256 |
|
| 257 |
OLA_ASSERT_EQ(ola::CAPABILITY_FULL, output_port2.PriorityCapability()); |
| 258 |
OLA_ASSERT_EQ(ola::PRIORITY_MODE_STATIC, output_port2.GetPriorityMode()); |
| 259 |
OLA_ASSERT_EQ((uint8_t) 180, output_port2.GetPriority()); |
| 260 |
|
| 261 |
OLA_ASSERT_EQ(ola::CAPABILITY_FULL, input_port3.PriorityCapability()); |
| 262 |
OLA_ASSERT_EQ(ola::PRIORITY_MODE_INHERIT, input_port3.GetPriorityMode()); |
| 263 |
OLA_ASSERT_EQ((uint8_t) 200, input_port3.GetPriority()); |
| 264 |
|
| 265 |
OLA_ASSERT_EQ(ola::CAPABILITY_FULL, output_port3.PriorityCapability()); |
| 266 |
OLA_ASSERT_EQ(ola::PRIORITY_MODE_INHERIT, output_port3.GetPriorityMode()); |
| 267 |
OLA_ASSERT_EQ((uint8_t) 180, output_port3.GetPriority()); |
| 268 |
|
| 269 |
// Now make some changes |
| 270 |
input_port2.SetPriorityMode(ola::PRIORITY_MODE_INHERIT); |
| 271 |
output_port2.SetPriorityMode(ola::PRIORITY_MODE_INHERIT); |
| 272 |
input_port3.SetPriorityMode(ola::PRIORITY_MODE_STATIC); |
| 273 |
input_port3.SetPriority(40); |
| 274 |
output_port3.SetPriorityMode(ola::PRIORITY_MODE_STATIC); |
| 275 |
output_port3.SetPriority(60); |
| 276 |
|
| 277 |
// unregister all |
| 278 |
manager.UnregisterAllDevices(); |
| 279 |
OLA_ASSERT_EQ(0u, manager.DeviceCount()); |
| 280 |
|
| 281 |
OLA_ASSERT_EQ(string("0"), |
| 282 |
prefs->GetValue("2-test_device_1-I-2_priority_mode")); |
| 283 |
OLA_ASSERT_EQ(string("0"), |
| 284 |
prefs->GetValue("2-test_device_1-O-2_priority_mode")); |
| 285 |
OLA_ASSERT_EQ(string("1"), |
| 286 |
prefs->GetValue("2-test_device_1-I-3_priority_mode")); |
| 287 |
OLA_ASSERT_EQ(string("40"), |
| 288 |
prefs->GetValue("2-test_device_1-I-3_priority_value")); |
| 289 |
OLA_ASSERT_EQ(string("1"), |
| 290 |
prefs->GetValue("2-test_device_1-O-3_priority_mode")); |
| 291 |
OLA_ASSERT_EQ(string("60"), |
| 292 |
prefs->GetValue("2-test_device_1-O-3_priority_value")); |
| 293 |
} |