Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add key publisher test #477

Merged
merged 2 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/plugins/key_publisher/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ ign_gui_add_plugin(KeyPublisher
KeyPublisher.cc
QT_HEADERS
KeyPublisher.hh
TEST_SOURCES # todo
# KeyPublisher_TEST.cc
)
TEST_SOURCES
KeyPublisher_TEST.cc
)
12 changes: 11 additions & 1 deletion src/plugins/key_publisher/KeyPublisher.hh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@
#ifndef IGNITION_GUI_PLUGINS_KEYPUBLISHER_HH_
#define IGNITION_GUI_PLUGINS_KEYPUBLISHER_HH_

#ifndef _WIN32
# define KeyPublisher_EXPORTS_API
#else
# if (defined(KeyPublisher_EXPORTS))
# define KeyPublisher_EXPORTS_API __declspec(dllexport)
# else
# define KeyPublisher_EXPORTS_API __declspec(dllimport)
# endif
#endif

#include <ignition/gui/qt.h>

#include <memory>
Expand All @@ -35,7 +45,7 @@ namespace gui
///
/// ## Configuration
/// This plugin doesn't accept any custom configuration.
class KeyPublisher : public ignition::gui::Plugin
class KeyPublisher_EXPORTS_API KeyPublisher : public ignition::gui::Plugin
{
Q_OBJECT

Expand Down
133 changes: 133 additions & 0 deletions src/plugins/key_publisher/KeyPublisher_TEST.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Copyright (C) 2022 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <gtest/gtest.h>
#ifdef _MSC_VER
#pragma warning(push, 0)
#endif
#include <ignition/msgs/int32.pb.h>
#ifdef _MSC_VER
#pragma warning(pop)
#endif

#include <ignition/common/Console.hh>
#include <ignition/common/Filesystem.hh>
#include <ignition/utilities/ExtraTestMacros.hh>

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

#include "KeyPublisher.hh"

int g_argc = 1;
char* g_argv[] =
{
reinterpret_cast<char*>(const_cast<char*>("./KeyPublisher_TEST")),
};

using namespace ignition;
using namespace gui;

class KeyPublisherTest : public ::testing::Test
{
// Set up function.
protected: void SetUp() override
{
common::Console::SetVerbosity(4);

this->app.AddPluginPath(
common::joinPaths(std::string(PROJECT_BINARY_PATH), "lib"));

// Load plugin
EXPECT_TRUE(this->app.LoadPlugin("KeyPublisher"));

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

// Get plugin
this->plugins = win->findChildren<KeyPublisher *>();
ASSERT_EQ(plugins.size(), 1);
this->plugin = plugins[0];
EXPECT_EQ(this->plugin->Title(), "Key publisher");

// Subscribes to keyboard/keypress topic
const std::string kTopic{"keyboard/keypress"};
node.Subscribe(kTopic, &KeyPublisherTest::VerifyKeypressCb, this);
}

// Tear down function
protected: void TearDown() override
{
// Cleanup
plugins.clear();
}

// Callback function to verify key message was sent correctly
protected: void VerifyKeypressCb(const msgs::Int32 &_msg)
{
this->received = true;
EXPECT_EQ(_msg.data(), this->currentKey);
}

protected: void VerifyKeyEvent(int _key)
{
this->received = false;
this->currentKey = _key;
auto event = new QKeyEvent(QKeyEvent::KeyPress, _key, Qt::NoModifier);
this->app.sendEvent(this->win->QuickWindow(), event);

int sleep = 0;
int maxSleep = 30;
while (!this->received && sleep < maxSleep)
{
std::this_thread::sleep_for(std::chrono::milliseconds(100));
QCoreApplication::processEvents();
++sleep;
}

EXPECT_LT(sleep, maxSleep);
EXPECT_TRUE(this->received);
}

// Provides an API to load plugins and configuration files.
protected: Application app{g_argc, g_argv};

// Instance of the main window.
protected: MainWindow *win;

// List of plugins.
protected: QList<KeyPublisher *> plugins;
protected: KeyPublisher *plugin;

// Checks if a new key has been received.
protected: bool received = false;
protected: transport::Node node;

// Current key
protected: int currentKey = 0;
Comment on lines +110 to +124
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private instead of protected ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If private then the members won't be accessible, specifically we can't call:

this->VerifyKeyEvent(Qt::Key_W);
this->VerifyKeyEvent(Qt::Key_A);
this->VerifyKeyEvent(Qt::Key_D);

};

/////////////////////////////////////////////////
TEST_F(KeyPublisherTest, IGN_UTILS_TEST_DISABLED_ON_WIN32(KeyPublisher))
{
this->VerifyKeyEvent(Qt::Key_W);
this->VerifyKeyEvent(Qt::Key_A);
this->VerifyKeyEvent(Qt::Key_D);
}