| 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 |
* ClientTest.cpp |
| 17 |
* Test fixture for the Client class |
| 18 |
* Copyright (C) 2005 Simon Newton |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <cppunit/extensions/HelperMacros.h> |
| 22 |
#include <string> |
| 23 |
|
| 24 |
#include "common/protocol/Ola.pb.h" |
| 25 |
#include "common/protocol/OlaService.pb.h" |
| 26 |
#include "common/rpc/RpcController.h" |
| 27 |
#include "common/rpc/RpcService.h" |
| 28 |
#include "ola/Clock.h" |
| 29 |
#include "ola/Constants.h" |
| 30 |
#include "ola/DmxBuffer.h" |
| 31 |
#include "ola/rdm/UID.h" |
| 32 |
#include "ola/testing/TestUtils.h" |
| 33 |
#include "olad/DmxSource.h" |
| 34 |
#include "olad/plugin_api/Client.h" |
| 35 |
|
| 36 |
|
| 37 |
static unsigned int TEST_UNIVERSE = 1; |
| 38 |
static unsigned int TEST_UNIVERSE2 = 2; |
| 39 |
static const char TEST_DATA[] = "this is some test data"; |
| 40 |
static const char TEST_DATA2[] = "another set of test data"; |
| 41 |
|
| 42 |
using ola::Client; |
| 43 |
using ola::DmxBuffer; |
| 44 |
using std::string; |
| 45 |
|
| 46 |
class ClientTest: public CppUnit::TestFixture { |
| 47 |
CPPUNIT_TEST_SUITE(ClientTest); |
| 48 |
CPPUNIT_TEST(testSendDMX); |
| 49 |
CPPUNIT_TEST(testGetSetDMX); |
| 50 |
CPPUNIT_TEST_SUITE_END(); |
| 51 |
|
| 52 |
public: |
| 53 |
ClientTest() : m_test_uid(ola::OPEN_LIGHTING_ESTA_CODE, 0) {} |
| 54 |
void testSendDMX(); |
| 55 |
void testGetSetDMX(); |
| 56 |
|
| 57 |
private: |
| 58 |
ola::Clock m_clock; |
| 59 |
ola::rdm::UID m_test_uid; |
| 60 |
}; |
| 61 |
|
| 62 |
|
| 63 |
CPPUNIT_TEST_SUITE_REGISTRATION(ClientTest); |
| 64 |
|
| 65 |
/* |
| 66 |
* Mock out the ClientStub for testing |
| 67 |
*/ |
| 68 |
class MockClientStub: public ola::proto::OlaClientService_Stub { |
| 69 |
public: |
| 70 |
MockClientStub(): ola::proto::OlaClientService_Stub(NULL) {} |
| 71 |
|
| 72 |
void UpdateDmxData(ola::rpc::RpcController *controller, |
| 73 |
const ola::proto::DmxData *request, |
| 74 |
ola::proto::Ack *response, |
| 75 |
ola::rpc::RpcService::CompletionCallback *done); |
| 76 |
}; |
| 77 |
|
| 78 |
void MockClientStub::UpdateDmxData( |
| 79 |
ola::rpc::RpcController* controller, |
| 80 |
const ola::proto::DmxData *request, |
| 81 |
OLA_UNUSED ola::proto::Ack *response, |
| 82 |
ola::rpc::RpcService::CompletionCallback *done) { |
| 83 |
OLA_ASSERT(controller); |
| 84 |
OLA_ASSERT_FALSE(controller->Failed()); |
| 85 |
OLA_ASSERT_EQ(TEST_UNIVERSE, (unsigned int) request->universe()); |
| 86 |
OLA_ASSERT(TEST_DATA == request->data()); |
| 87 |
done->Run(); |
| 88 |
} |
| 89 |
|
| 90 |
/* |
| 91 |
* Check that the SendDMX method works correctly. |
| 92 |
*/ |
| 93 |
void ClientTest::testSendDMX() { |
| 94 |
// check we survive a null pointer |
| 95 |
const DmxBuffer buffer(TEST_DATA); |
| 96 |
uint8_t priority = 100; |
| 97 |
Client client(NULL, m_test_uid); |
| 98 |
client.SendDMX(TEST_UNIVERSE, priority, buffer); |
| 99 |
|
| 100 |
// check the stub is called correctly |
| 101 |
Client client2(new MockClientStub(), m_test_uid); |
| 102 |
client2.SendDMX(TEST_UNIVERSE, priority, buffer); |
| 103 |
} |
| 104 |
|
| 105 |
/* |
| 106 |
* Check that the DMX get/set works correctly. |
| 107 |
*/ |
| 108 |
void ClientTest::testGetSetDMX() { |
| 109 |
DmxBuffer buffer(TEST_DATA); |
| 110 |
const DmxBuffer empty; |
| 111 |
Client client(NULL, m_test_uid); |
| 112 |
|
| 113 |
ola::TimeStamp timestamp; |
| 114 |
m_clock.CurrentMonotonicTime(×tamp); |
| 115 |
ola::DmxSource source(buffer, timestamp, 100); |
| 116 |
|
| 117 |
// check get/set works |
| 118 |
client.DMXReceived(TEST_UNIVERSE, source); |
| 119 |
const ola::DmxSource &source2 = client.SourceData(TEST_UNIVERSE); |
| 120 |
OLA_ASSERT(source2.IsSet()); |
| 121 |
OLA_ASSERT(source2.Data() == buffer); |
| 122 |
OLA_ASSERT_EQ(timestamp, source2.Timestamp()); |
| 123 |
OLA_ASSERT_EQ((uint8_t) 100, source2.Priority()); |
| 124 |
|
| 125 |
// check update works |
| 126 |
ola::DmxBuffer old_data(buffer); |
| 127 |
buffer.Set(TEST_DATA2); |
| 128 |
OLA_ASSERT(source2.Data() == old_data); |
| 129 |
OLA_ASSERT_EQ(timestamp, source2.Timestamp()); |
| 130 |
OLA_ASSERT_EQ((uint8_t) 100, source2.Priority()); |
| 131 |
|
| 132 |
source.UpdateData(buffer, timestamp, 120); |
| 133 |
client.DMXReceived(TEST_UNIVERSE, source); |
| 134 |
const ola::DmxSource source3 = client.SourceData(TEST_UNIVERSE); |
| 135 |
OLA_ASSERT(source3.IsSet()); |
| 136 |
OLA_ASSERT(buffer == source3.Data()); |
| 137 |
OLA_ASSERT_EQ(timestamp, source3.Timestamp()); |
| 138 |
OLA_ASSERT_EQ((uint8_t) 120, source3.Priority()); |
| 139 |
|
| 140 |
// check fetching an unknown universe results in an empty buffer |
| 141 |
const ola::DmxSource source4 = client.SourceData(TEST_UNIVERSE2); |
| 142 |
OLA_ASSERT_FALSE(source4.IsSet()); |
| 143 |
OLA_ASSERT(empty == source4.Data()); |
| 144 |
} |