Skip to content

Commit

Permalink
Backport Publisher test from v4 (#173)
Browse files Browse the repository at this point in the history
Signed-off-by: Louise Poubel <louise@openrobotics.org>

Co-authored-by: Ashton Larkin <42042756+adlarkin@users.noreply.github.com>
  • Loading branch information
chapulina and adlarkin authored Feb 10, 2021
1 parent 64c907c commit c9c4f80
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 72 deletions.
2 changes: 1 addition & 1 deletion src/plugins/publisher/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ ign_gui_add_plugin(Publisher
QT_HEADERS
Publisher.hh
TEST_SOURCES
# Publisher_TEST.cc
Publisher_TEST.cc
)

161 changes: 90 additions & 71 deletions src/plugins/publisher/Publisher_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,73 +19,98 @@
#ifdef _MSC_VER
#pragma warning(push, 0)
#endif
#include <ignition/msgs.hh>
#include <ignition/msgs/stringmsg.pb.h>
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#include <ignition/transport/Node.hh>
#include <ignition/utilities/ExtraTestMacros.hh>

#include "ignition/gui/Iface.hh"
#include "test_config.h" // NOLINT(build/include)
#include "ignition/gui/Application.hh"
#include "ignition/gui/Plugin.hh"
#include "ignition/gui/MainWindow.hh"

#include "Publisher.hh"

int g_argc = 1;
char **g_argv = new char *[g_argc];

using namespace ignition;
using namespace gui;

/////////////////////////////////////////////////
TEST(PublisherTest, Load)
// See https://github.com/ignitionrobotics/ign-gui/issues/75
TEST(PublisherTest, IGN_UTILS_TEST_ENABLED_ONLY_ON_LINUX(Load))
{
EXPECT_TRUE(initApp());
common::Console::SetVerbosity(4);

Application app(g_argc, g_argv);
app.AddPluginPath(std::string(PROJECT_BINARY_PATH) + "/lib");

EXPECT_TRUE(app.LoadPlugin("Publisher"));

// Get main window
auto win = app.findChild<MainWindow *>();
ASSERT_NE(nullptr, win);

// Get plugin
auto plugins = win->findChildren<Plugin *>();
EXPECT_EQ(plugins.size(), 1);

EXPECT_TRUE(loadPlugin("Publisher"));
auto plugin = plugins[0];
EXPECT_EQ(plugin->Title(), "Publisher");

EXPECT_TRUE(stop());
// Cleanup
plugins.clear();
}

/////////////////////////////////////////////////
TEST(PublisherTest, Publish)
TEST(PublisherTest, IGN_UTILS_TEST_ENABLED_ONLY_ON_LINUX(Publish))
{
setVerbosity(4);
EXPECT_TRUE(initApp());
common::Console::SetVerbosity(4);

Application app(g_argc, g_argv);
app.AddPluginPath(std::string(PROJECT_BINARY_PATH) + "/lib");

// Load plugin
EXPECT_TRUE(loadPlugin("Publisher"));
const char *pluginStr =
"<plugin filename=\"Publisher\">"
"<ignition-gui>"
"<title>Publisher!</title>"
"</ignition-gui>"
"</plugin>";

tinyxml2::XMLDocument pluginDoc;
EXPECT_EQ(tinyxml2::XML_SUCCESS, pluginDoc.Parse(pluginStr));
EXPECT_TRUE(app.LoadPlugin("Publisher",
pluginDoc.FirstChildElement("plugin")));

// Get main window
auto win = app.findChild<MainWindow *>();
ASSERT_NE(nullptr, win);

// Create main window
EXPECT_TRUE(createMainWindow());
auto win = mainWindow();
EXPECT_TRUE(win != nullptr);
// Show, but don't exec, so we don't block
win->QuickWindow()->show();

// Get plugin
auto plugins = win->findChildren<Plugin *>();
auto plugins = win->findChildren<plugins::Publisher *>();
EXPECT_EQ(plugins.size(), 1);

auto plugin = plugins[0];
EXPECT_EQ(plugin->Title(), "Publisher");
EXPECT_EQ(plugin->Title(), "Publisher!");

// Message type
auto msgTypeEdit = plugin->findChild<QLineEdit *>("msgTypeEdit");
ASSERT_TRUE(msgTypeEdit != nullptr);
EXPECT_EQ(msgTypeEdit->text(), "ignition.msgs.StringMsg");
EXPECT_EQ(plugin->MsgType(), "ignition.msgs.StringMsg");

// Message
auto msgEdit = plugin->findChild<QTextEdit *>("msgEdit");
ASSERT_TRUE(msgEdit != nullptr);
EXPECT_EQ(msgEdit->toPlainText(), "data: \"Hello\"");
EXPECT_EQ(plugin->MsgData(), "data: \"Hello\"");

// Topic
auto topicEdit = plugin->findChild<QLineEdit *>("topicEdit");
ASSERT_TRUE(topicEdit != nullptr);
EXPECT_EQ(topicEdit->text(), "/echo");
EXPECT_EQ(plugin->Topic(), "/echo");

// Frequency
auto freqSpin = plugin->findChild<QDoubleSpinBox *>("frequencySpinBox");
ASSERT_TRUE(freqSpin != nullptr);
EXPECT_DOUBLE_EQ(freqSpin->value(), 1.0);

// Button
auto pubButton = plugin->findChild<QPushButton *>("publishButton");
ASSERT_TRUE(pubButton != nullptr);
EXPECT_EQ(pubButton->text(), "Publish");
EXPECT_DOUBLE_EQ(plugin->Frequency(), 1.0);

// Subscribe
bool received = false;
Expand All @@ -101,7 +126,7 @@ TEST(PublisherTest, Publish)
EXPECT_FALSE(received);

// Publish
pubButton->click();
plugin->OnPublish(true);

int sleep = 0;
int maxSleep = 30;
Expand All @@ -112,12 +137,11 @@ TEST(PublisherTest, Publish)
sleep++;
}

EXPECT_EQ(pubButton->text(), "Stop publishing");
EXPECT_TRUE(received);
received = false;

// Stop publishing
pubButton->click();
plugin->OnPublish(false);

sleep = 0;
// cppcheck-suppress knownConditionTrueFalse
Expand All @@ -128,12 +152,11 @@ TEST(PublisherTest, Publish)
sleep++;
}

EXPECT_EQ(pubButton->text(), "Publish");
EXPECT_FALSE(received);

// Publish once
freqSpin->setValue(0.0);
pubButton->click();
plugin->SetFrequency(0.0);
plugin->OnPublish(true);

sleep = 0;
while (!received && sleep < maxSleep)
Expand All @@ -143,14 +166,14 @@ TEST(PublisherTest, Publish)
sleep++;
}

EXPECT_EQ(pubButton->text(), "Publish");
EXPECT_TRUE(received);
plugin->OnPublish(false);
received = false;

// Bad message type
freqSpin->setValue(1.0);
msgTypeEdit->setText("banana.message");
pubButton->click();
plugin->SetFrequency(1.0);
plugin->SetMsgType("banana.message");
plugin->OnPublish(true);

sleep = 0;
// cppcheck-suppress knownConditionTrueFalse
Expand All @@ -160,13 +183,13 @@ TEST(PublisherTest, Publish)
QCoreApplication::processEvents();
sleep++;
}
EXPECT_EQ(pubButton->text(), "Publish");
EXPECT_FALSE(received);
plugin->OnPublish(false);

// Bad message type - msg combination
msgTypeEdit->setText("ignition.msgs.StringMsg");
msgEdit->setPlainText("banana: apple");
pubButton->click();
plugin->SetMsgType("ignition.msgs.StringMsg");
plugin->SetMsgData("banana: apple");
plugin->OnPublish(true);

sleep = 0;
while (!received && sleep < maxSleep)
Expand All @@ -175,19 +198,20 @@ TEST(PublisherTest, Publish)
QCoreApplication::processEvents();
sleep++;
}
EXPECT_EQ(pubButton->text(), "Publish");
EXPECT_FALSE(received);
plugin->OnPublish(false);

// Cleanup
plugins.clear();
EXPECT_TRUE(stop());
}

/////////////////////////////////////////////////
TEST(PublisherTest, ParamsFromSDF)
//////////////////////////////////////////////////
TEST(PublisherTest, IGN_UTILS_TEST_ENABLED_ONLY_ON_LINUX(ParamsFromSDF))
{
setVerbosity(4);
EXPECT_TRUE(initApp());
common::Console::SetVerbosity(4);

Application app(g_argc, g_argv);
app.AddPluginPath(std::string(PROJECT_BINARY_PATH) + "/lib");

// Load plugin
const char *pluginStr =
Expand All @@ -199,38 +223,33 @@ TEST(PublisherTest, ParamsFromSDF)
"</plugin>";

tinyxml2::XMLDocument pluginDoc;
pluginDoc.Parse(pluginStr);
EXPECT_TRUE(ignition::gui::loadPlugin("Publisher",
EXPECT_EQ(tinyxml2::XML_SUCCESS, pluginDoc.Parse(pluginStr));
EXPECT_TRUE(app.LoadPlugin("Publisher",
pluginDoc.FirstChildElement("plugin")));

// Create main window
EXPECT_TRUE(createMainWindow());
auto win = mainWindow();
// Get main window
auto win = app.findChild<MainWindow *>();
ASSERT_NE(nullptr, win);

// Show, but don't exec, so we don't block
win->QuickWindow()->show();

// Get plugin
auto plugins = win->findChildren<Plugin *>();
auto plugins = win->findChildren<plugins::Publisher *>();
EXPECT_EQ(plugins.size(), 1);

auto plugin = plugins[0];
EXPECT_EQ(plugin->Title(), "Publisher");

// Message type
auto msgTypeEdit = plugin->findChild<QLineEdit *>("msgTypeEdit");
ASSERT_NE(nullptr, msgTypeEdit);
EXPECT_EQ(msgTypeEdit->text(), "ignition.msgs.Fruits");
EXPECT_EQ(plugin->MsgType(), "ignition.msgs.Fruits");

// Message
auto msgEdit = plugin->findChild<QTextEdit *>("msgEdit");
ASSERT_NE(nullptr, msgEdit);
EXPECT_EQ(msgEdit->toPlainText(), "number: 1 fruit {name:\"banana\"}");
EXPECT_EQ(plugin->MsgData(), "number: 1 fruit {name:\"banana\"}");

// Topic
auto topicEdit = plugin->findChild<QLineEdit *>("topicEdit");
ASSERT_NE(nullptr, topicEdit);
EXPECT_EQ(topicEdit->text(), "/fruit");
EXPECT_EQ(plugin->Topic(), "/fruit");

// Frequency
auto freqSpin = plugin->findChild<QDoubleSpinBox *>("frequencySpinBox");
ASSERT_NE(nullptr, freqSpin);
EXPECT_DOUBLE_EQ(freqSpin->value(), 0.1);
EXPECT_DOUBLE_EQ(plugin->Frequency(), 0.1);
}

0 comments on commit c9c4f80

Please sign in to comment.