25#include <gtest/gtest.h>
41const char *DEVICE_NAME =
"MockSerialDevice";
60 serialConnection->setDefaultBaudRate(m_DefaultBaudRate);
73class SerialConfigTest :
public ::testing::Test
79 m_ConfigFile =
"/tmp/test_connection_serial_" + std::to_string(getpid()) +
"_config.xml";
80 setenv(
"INDICONFIG", m_ConfigFile.c_str(), 1);
84 void TearDown()
override
91 std::remove(m_ConfigFile.c_str());
93 std::remove((m_ConfigFile +
".default").c_str());
97 void writeOldLayoutConfig(
const std::string &onBaudRate)
99 std::ofstream out(m_ConfigFile);
100 out <<
"<INDIDriver>\n";
101 out <<
"<newSwitchVector device='" << DEVICE_NAME <<
"' name='CONNECTION_MODE'>\n";
102 out <<
" <oneSwitch name='CONNECTION_SERIAL'>On</oneSwitch>\n";
103 out <<
"</newSwitchVector>\n";
104 out <<
"<newTextVector device='" << DEVICE_NAME <<
"' name='DEVICE_PORT'>\n";
105 out <<
" <oneText name='PORT'>/dev/null</oneText>\n";
106 out <<
"</newTextVector>\n";
107 out <<
"<newSwitchVector device='" << DEVICE_NAME <<
"' name='DEVICE_BAUD_RATE'>\n";
108 for (
const char *rate : {
"9600",
"19200",
"38400",
"57600",
"115200",
"230400"})
109 out <<
" <oneSwitch name='" << rate <<
"'>" << (onBaudRate == rate ?
"On" :
"Off") <<
"</oneSwitch>\n";
110 out <<
"</newSwitchVector>\n";
111 out <<
"</INDIDriver>\n";
116 auto device = std::make_unique<SerialDevice>(defaultBaudRate);
118 device->ISGetProperties(
nullptr);
122 std::string savedBaudRate()
const
130 std::string m_ConfigFile;
133TEST_F(SerialConfigTest, DefaultBaudRateWithoutConfig)
136 EXPECT_EQ(
device->serialConnection->baud(), 4800u);
139TEST_F(SerialConfigTest, OldLayoutConfigLoadsBaudRateByName)
142 writeOldLayoutConfig(
"115200");
144 EXPECT_EQ(
device->serialConnection->baud(), 115200u);
147TEST_F(SerialConfigTest, OldLayoutConfigOverridesDriverDefault)
149 writeOldLayoutConfig(
"57600");
151 EXPECT_EQ(
device->serialConnection->baud(), 57600u);
154TEST_F(SerialConfigTest, SavingNewBaudRateOverOldLayoutConfig)
156 writeOldLayoutConfig(
"57600");
159 const char *names[] = {
"4800"};
161 ASSERT_TRUE(
device->ISNewSwitch(DEVICE_NAME,
"DEVICE_BAUD_RATE", states,
const_cast<char **
>(names), 1));
162 ASSERT_EQ(
device->serialConnection->baud(), 4800u);
165 ASSERT_TRUE(
device->saveConfig(
true,
"DEVICE_BAUD_RATE"));
166 EXPECT_EQ(savedBaudRate(),
"4800");
171 EXPECT_STREQ(port,
"/dev/null");
176 EXPECT_EQ(restarted->serialConnection->baud(), 4800u);
179TEST_F(SerialConfigTest, SavingOverCurrentLayoutConfig)
182 ASSERT_TRUE(
device->saveConfig(
true));
183 ASSERT_EQ(savedBaudRate(),
"9600");
185 const char *names[] = {
"2400"};
187 ASSERT_TRUE(
device->ISNewSwitch(DEVICE_NAME,
"DEVICE_BAUD_RATE", states,
const_cast<char **
>(names), 1));
188 ASSERT_TRUE(
device->saveConfig(
true,
"DEVICE_BAUD_RATE"));
189 EXPECT_EQ(savedBaudRate(),
"2400");
198 ::testing::InitGoogleTest(&argc, argv);
199 return RUN_ALL_TESTS();
The Serial class manages connection with serial devices including Bluetooth. Serial communication is ...
BaudRate
Supported baud rates.
void setDeviceName(const char *dev)
Set the device name.
Class to provide extended functionality for devices in addition to the functionality provided by INDI...
virtual bool saveConfig(bool silent=false, const char *property=nullptr)
Save the current properties in a configuration file.
void registerConnection(Connection::Interface *newConnection)
registerConnection Add new connection plugin to the existing connection pool. The connection type sha...
virtual const char * getDefaultName()=0
virtual bool initProperties()
Initialize properties initial state and value. The child class must implement this function.
int IUGetConfigOnSwitchName(const char *dev, const char *property, char *name, size_t size)
IUGetConfigOnSwitchLabel Opens configuration file and returns the name of the ON switch property,...
int IUGetConfigText(const char *dev, const char *property, const char *member, char *value, int len)
IUGetConfigText Opens configuration file and reads single text property.
static const loggerConf file_off
void configure(const std::string &outputFile, const loggerConf configuration, const int fileVerbosityLevel, const int screenVerbosityLevel)
Method to configure the logger. Called by the DEBUG_CONF() macro. To make implementation easier,...
static Logger & getInstance()
Method to get a reference to the object (i.e., Singleton) It is a static method.
TEST_F(AlignmentPluginTest, Test_BuiltIn_ErrorRecovery)