Files
o3de/Gems/EMotionFX/Code/Tests/UI/ModalPopupHandler.cpp
T
2021-03-08 14:30:57 -08:00

146 lines
4.1 KiB
C++

/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include "Tests/UI/ModalPopupHandler.h"
#include "Tests/UI/UIFixture.h"
#include <QApplication>
#include <QTimer>
#include <QMenu>
#include <QObject>
#include <QtTest>
namespace EMotionFX
{
ModalPopupHandler::ModalPopupHandler(QObject* pParent)
: QObject(pParent)
{
}
void ModalPopupHandler::ShowContextMenuAndTriggerAction(QWidget* widget, const QString& actionName, int timeout, ActionCompletionCallback completionCallback)
{
MenuActiveCallback menuCallback = [=](QMenu* menu)
{
ASSERT_TRUE(menu) << "Failed to find context menu.";
m_seenTargetWidget = true;
QAction* action = menu->findChild<QAction*>(actionName);
ASSERT_TRUE(action) << "Unable to find context menu action " << actionName.toUtf8().data();
action->trigger();
menu->close();
if (m_actionCompletionCallback)
{
m_actionCompletionCallback(actionName);
}
};
m_totalTime = 0;
m_actionCompletionCallback = completionCallback;
m_MenuActiveCallback = menuCallback;
m_timeout = timeout;
// Kick a timer off to check whether the menu is open.
QTimer::singleShot(WaitTickTime, this, SLOT(CheckForContextMenu()));
// Open the modal menu.
UIFixture::BringUpContextMenu(widget, QPoint(10, 10), widget->mapToGlobal(QPoint(10, 10)));
}
void ModalPopupHandler::CheckForContextMenu()
{
m_totalTime += WaitTickTime;
if (m_totalTime >= m_timeout)
{
if (m_actionCompletionCallback)
{
m_actionCompletionCallback("");
return;
}
}
// Check for the active widget being a popup widget.
QWidget* qpop = QApplication::activePopupWidget();
if (!qpop)
{
QTimer::singleShot(WaitTickTime, this, SLOT(CheckForContextMenu()));
return;
}
// If the active widget is not a menu, keep waiting.
QMenu* menu = qobject_cast<QMenu*>(qpop);
if (!menu)
{
QTimer::singleShot(WaitTickTime, this, SLOT(CheckForContextMenu()));
return;
}
// The menu is now active, inform the calling object.
m_MenuActiveCallback(menu);
}
void ModalPopupHandler::CheckForPopupWidget()
{
m_totalTime += WaitTickTime;
if (m_totalTime >= m_timeout)
{
if (m_actionCompletionCallback)
{
m_actionCompletionCallback("");
m_complete = true;
return;
}
}
// Check for the active widget being a popup widget.
QWidget* modalWidget = QApplication::activeModalWidget();
if (!modalWidget)
{
QTimer::singleShot(WaitTickTime, this, &ModalPopupHandler::CheckForPopupWidget);
return;
}
m_seenTargetWidget = true;
// Inform the calling object.
m_widgetActiveCallback(modalWidget);
}
bool ModalPopupHandler::GetSeenTargetWidget()
{
return m_seenTargetWidget;
}
void ModalPopupHandler::ResetSeenTargetWidget()
{
m_seenTargetWidget = false;
}
bool ModalPopupHandler::GetIsComplete()
{
return m_complete;
}
void ModalPopupHandler::WaitForCompletion(const int timeout)
{
static_cast<void>(QTest::qWaitFor([&]() {
return m_complete;
}, timeout));
}
} // namespace EMotionFX
#include <Tests/UI/moc_ModalPopupHandler.cpp>