From c9c4f80cd0e782305ba33e80a36baf5231ee1179 Mon Sep 17 00:00:00 2001 From: Louise Poubel Date: Wed, 10 Feb 2021 15:23:27 -0800 Subject: [PATCH] Backport Publisher test from v4 (#173) Signed-off-by: Louise Poubel Co-authored-by: Ashton Larkin <42042756+adlarkin@users.noreply.github.com> --- src/plugins/publisher/CMakeLists.txt | 2 +- src/plugins/publisher/Publisher_TEST.cc | 161 +++++++++++++----------- 2 files changed, 91 insertions(+), 72 deletions(-) diff --git a/src/plugins/publisher/CMakeLists.txt b/src/plugins/publisher/CMakeLists.txt index b6dfa223b..be3d01892 100644 --- a/src/plugins/publisher/CMakeLists.txt +++ b/src/plugins/publisher/CMakeLists.txt @@ -4,6 +4,6 @@ ign_gui_add_plugin(Publisher QT_HEADERS Publisher.hh TEST_SOURCES - # Publisher_TEST.cc + Publisher_TEST.cc ) diff --git a/src/plugins/publisher/Publisher_TEST.cc b/src/plugins/publisher/Publisher_TEST.cc index 0f9afe7ad..f70b6386a 100644 --- a/src/plugins/publisher/Publisher_TEST.cc +++ b/src/plugins/publisher/Publisher_TEST.cc @@ -19,73 +19,98 @@ #ifdef _MSC_VER #pragma warning(push, 0) #endif -#include +#include #ifdef _MSC_VER #pragma warning(pop) #endif #include +#include -#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(); + ASSERT_NE(nullptr, win); + + // Get plugin + auto plugins = win->findChildren(); + 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 = + "" + "" + "Publisher!" + "" + ""; + + 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(); + 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(); + auto plugins = win->findChildren(); 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("msgTypeEdit"); - ASSERT_TRUE(msgTypeEdit != nullptr); - EXPECT_EQ(msgTypeEdit->text(), "ignition.msgs.StringMsg"); + EXPECT_EQ(plugin->MsgType(), "ignition.msgs.StringMsg"); // Message - auto msgEdit = plugin->findChild("msgEdit"); - ASSERT_TRUE(msgEdit != nullptr); - EXPECT_EQ(msgEdit->toPlainText(), "data: \"Hello\""); + EXPECT_EQ(plugin->MsgData(), "data: \"Hello\""); // Topic - auto topicEdit = plugin->findChild("topicEdit"); - ASSERT_TRUE(topicEdit != nullptr); - EXPECT_EQ(topicEdit->text(), "/echo"); + EXPECT_EQ(plugin->Topic(), "/echo"); // Frequency - auto freqSpin = plugin->findChild("frequencySpinBox"); - ASSERT_TRUE(freqSpin != nullptr); - EXPECT_DOUBLE_EQ(freqSpin->value(), 1.0); - - // Button - auto pubButton = plugin->findChild("publishButton"); - ASSERT_TRUE(pubButton != nullptr); - EXPECT_EQ(pubButton->text(), "Publish"); + EXPECT_DOUBLE_EQ(plugin->Frequency(), 1.0); // Subscribe bool received = false; @@ -101,7 +126,7 @@ TEST(PublisherTest, Publish) EXPECT_FALSE(received); // Publish - pubButton->click(); + plugin->OnPublish(true); int sleep = 0; int maxSleep = 30; @@ -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 @@ -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) @@ -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 @@ -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) @@ -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 = @@ -199,38 +223,33 @@ TEST(PublisherTest, ParamsFromSDF) ""; 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(); ASSERT_NE(nullptr, win); + // Show, but don't exec, so we don't block + win->QuickWindow()->show(); + // Get plugin - auto plugins = win->findChildren(); + auto plugins = win->findChildren(); EXPECT_EQ(plugins.size(), 1); + auto plugin = plugins[0]; EXPECT_EQ(plugin->Title(), "Publisher"); // Message type - auto msgTypeEdit = plugin->findChild("msgTypeEdit"); - ASSERT_NE(nullptr, msgTypeEdit); - EXPECT_EQ(msgTypeEdit->text(), "ignition.msgs.Fruits"); + EXPECT_EQ(plugin->MsgType(), "ignition.msgs.Fruits"); // Message - auto msgEdit = plugin->findChild("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("topicEdit"); - ASSERT_NE(nullptr, topicEdit); - EXPECT_EQ(topicEdit->text(), "/fruit"); + EXPECT_EQ(plugin->Topic(), "/fruit"); // Frequency - auto freqSpin = plugin->findChild("frequencySpinBox"); - ASSERT_NE(nullptr, freqSpin); - EXPECT_DOUBLE_EQ(freqSpin->value(), 0.1); + EXPECT_DOUBLE_EQ(plugin->Frequency(), 0.1); }