Debian Codegraph @

olad/plugin_api/PortTest.cpp

raw
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 * PortTest.cpp
17 * Test fixture for the Port classes
18 * Copyright (C) 2005 Simon Newton
19 */
20
21 #include <cppunit/extensions/HelperMacros.h>
22 #include <string>
23 #include <vector>
24
25 #include "olad/PluginAdaptor.h"
26 #include "olad/PortBroker.h"
27 #include "olad/Preferences.h"
28 #include "olad/plugin_api/TestCommon.h"
29 #include "olad/plugin_api/UniverseStore.h"
30 #include "ola/testing/TestUtils.h"
31
32
33 using ola::Clock;
34 using ola::TimeStamp;
35 using std::string;
36
37 class PortTest: public CppUnit::TestFixture {
38 CPPUNIT_TEST_SUITE(PortTest);
39 CPPUNIT_TEST(testOutputPortPriorities);
40 CPPUNIT_TEST(testInputPortPriorities);
41 CPPUNIT_TEST_SUITE_END();
42
43 public:
44 void testOutputPortPriorities();
45 void testInputPortPriorities();
46
47 private:
48 Clock m_clock;
49 };
50
51
52 CPPUNIT_TEST_SUITE_REGISTRATION(PortTest);
53
54
55 /*
56 * Check that we can set the priority & mode of output ports
57 */
58 void PortTest::testOutputPortPriorities() {
59 TestMockOutputPort output_port(NULL, 1);
60
61 OLA_ASSERT_EQ(ola::dmx::SOURCE_PRIORITY_DEFAULT, output_port.GetPriority());
62 OLA_ASSERT_EQ(ola::PRIORITY_MODE_INHERIT, output_port.GetPriorityMode());
63
64 // test the setting of priorities
65 OLA_ASSERT(output_port.SetPriority(120));
66 OLA_ASSERT_EQ((uint8_t) 120, output_port.GetPriority());
67 OLA_ASSERT_FALSE(output_port.SetPriority(201));
68 OLA_ASSERT_EQ((uint8_t) 120, output_port.GetPriority());
69 OLA_ASSERT(output_port.SetPriority(0));
70 OLA_ASSERT_EQ((uint8_t) 0, output_port.GetPriority());
71
72 // test the setting of modes
73 output_port.SetPriorityMode(ola::PRIORITY_MODE_STATIC);
74 OLA_ASSERT_EQ(ola::PRIORITY_MODE_STATIC, output_port.GetPriorityMode());
75
76 output_port.SetPriorityMode(ola::PRIORITY_MODE_INHERIT);
77 OLA_ASSERT_EQ(ola::PRIORITY_MODE_INHERIT, output_port.GetPriorityMode());
78 }
79
80
81 /*
82 * Test that we can set the priorities & modes of input ports
83 */
84 void PortTest::testInputPortPriorities() {
85 unsigned int universe_id = 1;
86 ola::MemoryPreferences preferences("foo");
87 ola::UniverseStore store(&preferences, NULL);
88 ola::PortBroker broker;
89 ola::PortManager port_manager(&store, &broker);
90
91 MockDevice device(NULL, "foo");
92 TimeStamp time_stamp;
93 MockSelectServer ss(&time_stamp);
94 ola::PluginAdaptor plugin_adaptor(NULL, &ss, NULL, NULL, NULL, NULL);
95 // This port operates in static priority mode
96 TestMockInputPort input_port(&device, 1, &plugin_adaptor);
97 port_manager.PatchPort(&input_port, universe_id);
98
99 ola::DmxBuffer buffer("foo bar baz");
100 m_clock.CurrentMonotonicTime(&time_stamp);
101 input_port.WriteDMX(buffer);
102 input_port.DmxChanged();
103
104 ola::Universe *universe = store.GetUniverseOrCreate(universe_id);
105 OLA_ASSERT(universe);
106
107 OLA_ASSERT_EQ(ola::dmx::SOURCE_PRIORITY_DEFAULT, universe->ActivePriority());
108
109 // change the priority
110 uint8_t new_priority = 120;
111 port_manager.SetPriorityStatic(&input_port, new_priority);
112
113 m_clock.CurrentMonotonicTime(&time_stamp);
114 input_port.WriteDMX(buffer);
115 input_port.DmxChanged();
116 OLA_ASSERT_EQ(new_priority, universe->ActivePriority());
117
118 new_priority = 0;
119 port_manager.SetPriorityStatic(&input_port, new_priority);
120
121 m_clock.CurrentMonotonicTime(&time_stamp);
122 input_port.WriteDMX(buffer);
123 input_port.DmxChanged();
124 OLA_ASSERT_EQ(new_priority, universe->ActivePriority());
125 port_manager.UnPatchPort(&input_port);
126
127 // now try a port that supported priorities
128 TestMockPriorityInputPort input_port2(&device, 2, &plugin_adaptor);
129 port_manager.PatchPort(&input_port2, universe_id);
130
131 // the default mode is static, lets change it to inherit
132 input_port2.SetPriorityMode(ola::PRIORITY_MODE_INHERIT);
133 input_port2.SetInheritedPriority(99);
134 m_clock.CurrentMonotonicTime(&time_stamp);
135 input_port2.WriteDMX(buffer);
136 input_port2.DmxChanged();
137 OLA_ASSERT_EQ((uint8_t) 99, universe->ActivePriority());
138
139 input_port2.SetInheritedPriority(123);
140 m_clock.CurrentMonotonicTime(&time_stamp);
141 input_port2.WriteDMX(buffer);
142 input_port2.DmxChanged();
143 OLA_ASSERT_EQ((uint8_t) 123, universe->ActivePriority());
144
145 // now try static mode
146 new_priority = 108;
147 port_manager.SetPriorityStatic(&input_port2, new_priority);
148 m_clock.CurrentMonotonicTime(&time_stamp);
149 input_port2.WriteDMX(buffer);
150 input_port2.DmxChanged();
151 OLA_ASSERT_EQ(new_priority, universe->ActivePriority());
152 }