Skip to content

Commit

Permalink
Add test helper to check event
Browse files Browse the repository at this point in the history
Signed-off-by: Louise Poubel <[email protected]>
  • Loading branch information
chapulina committed May 14, 2021
1 parent 142c0e5 commit 23c0578
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/plugins/scene3d/Scene3D.qml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*
*/
import QtQuick 2.0
import QtQuick 2.9
import QtQuick.Controls 2.0
import RenderWindow 1.0
import QtGraphicalEffects 1.0
Expand All @@ -28,6 +28,21 @@ Rectangle {
*/
property bool gammaCorrect: false

/**
* Get mouse position on 3D widget
*/
MouseArea {
id: mouseArea
anchors.fill: parent
hoverEnabled: true
acceptedButtons: Qt.NoButton
onEntered: {
Scene3D.OnFocusWindow()
}
onPositionChanged: {
Scene3D.OnHovered(mouseArea.mouseX, mouseArea.mouseY);
}
}

RenderWindow {
id: renderWindow
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ execute_process(COMMAND cmake -E remove_directory ${CMAKE_BINARY_DIR}/test_resul
execute_process(COMMAND cmake -E make_directory ${CMAKE_BINARY_DIR}/test_results)
include_directories(${GTEST_INCLUDE_DIRS})

add_subdirectory(helpers)
add_subdirectory(integration)
add_subdirectory(performance)
add_subdirectory(plugins)
Expand Down
14 changes: 14 additions & 0 deletions test/helpers/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
set (qt_headers
TestHelper.hh
)

QT5_WRAP_CPP(headers_MOC ${qt_headers})

add_library(${PROJECT_NAME}_test_helpers SHARED
${headers_MOC}
)
target_link_libraries(${PROJECT_NAME}_test_helpers
PUBLIC
${PROJECT_LIBRARY_TARGET_NAME}
)

57 changes: 57 additions & 0 deletions test/helpers/TestHelper.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (C) 2021 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.
*
*/
#ifndef IGNITION_GUI_TESTHELPER_HH_
#define IGNITION_GUI_TESTHELPER_HH_

#include <ignition/gui/Application.hh>
#include <ignition/gui/Export.hh>
#include <ignition/gui/MainWindow.hh>

namespace ignition
{
namespace gui
{
/// \brief
class IGNITION_GUI_VISIBLE TestHelper : public QObject
{
Q_OBJECT

/// \brief Constructor
public: TestHelper()
{
App()->findChild<MainWindow *>()->installEventFilter(this);
};

/// \brief Destructor
public: ~TestHelper() = default;

/// \brief Documentation inherited
public: bool eventFilter(QObject *_obj, QEvent *_event) override
{
if (this->forwardEvent)
this->forwardEvent(_event);

// Standard event processing
return QObject::eventFilter(_obj, _event);
}

public: std::function<void (QEvent *)> forwardEvent;
};
}
}

#endif
1 change: 1 addition & 0 deletions test/integration/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ ign_build_tests(
TYPE INTEGRATION
SOURCES ${tests}
LIB_DEPS
${PROJECT_NAME}_test_helpers
ignition-plugin${IGN_PLUGIN_VER}::loader
ignition-rendering${IGN_RENDERING_VER}::ignition-rendering${IGN_RENDERING_VER})
60 changes: 60 additions & 0 deletions test/integration/scene3d.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
#include <ignition/utilities/ExtraTestMacros.hh>

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

Expand Down Expand Up @@ -123,3 +125,61 @@ TEST(Scene3DTest, IGN_UTILS_TEST_ENABLED_ONLY_ON_LINUX(Config))
EXPECT_EQ(math::Pose3d(1, 2, 3, 0, 0, 1.57), camera->WorldPose());
}

/////////////////////////////////////////////////
TEST(Scene3DTest, IGN_UTILS_TEST_ENABLED_ONLY_ON_LINUX(Events))
{
common::Console::SetVerbosity(4);

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

// Load plugin
const char *pluginStr =
"<plugin filename=\"Scene3D\">"
"<engine>ogre</engine>"
"<scene>banana</scene>"
"<ambient_light>1.0 0 0</ambient_light>"
"<background_color>0 1 0</background_color>"
"<camera_pose>1 2 3 0 0 1.57</camera_pose>"
"</plugin>";

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

// 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();

// Flags to check if events were received
bool receivedRenderEvent{false};

// Helper to filter events
auto testHelper = std::make_unique<TestHelper>();
testHelper->forwardEvent = [&](QEvent *_event)
{
if (_event->type() == events::Render::kType)
{
receivedRenderEvent = true;
}
};

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

EXPECT_TRUE(receivedRenderEvent);

// TODO(anyone) Test more events
}


0 comments on commit 23c0578

Please sign in to comment.