/* * 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. * */ #pragma once #include #include #include #include "IntegerPrimtitiveTestConfig.h" #include #include namespace UnitTest { template class HandlerType> class IntrCtrlHandlerAPI : public HandlerType { using Parent = HandlerType; using Parent::CreateGUI; using Parent::ConsumeAttribute; using Parent::ReadValuesIntoGUI; using Parent::WriteGUIValuesIntoProperty; using Parent::ModifyTooltip; public: QWidget* CreateGUI(QWidget* pParent) { return Parent::CreateGUI(pParent); } void ConsumeAttribute(IntrCtrlHandlerAPI* GUI, AZ::u32 attrib, AzToolsFramework::PropertyAttributeReader* attrValue, const char* debugName) { Parent::ConsumeAttribute(GUI, attrib, attrValue, debugName); } bool ReadValuesIntoGUI(size_t index, IntrCtrlHandlerAPI* GUI, const ValueType& instance, AzToolsFramework::InstanceDataNode* node) { return Parent::ReadValuesIntoGUI(index, GUI, instance, node); } void WriteGUIValuesIntoProperty(size_t index, IntrCtrlHandlerAPI* GUI, ValueType& instance, AzToolsFramework::InstanceDataNode* node) { Parent::WriteGUIValuesIntoProperty(index, GUI, instance, node); } bool ModifyTooltip(QWidget* widget, QString& toolTipString) { return Parent::ModifyTooltip(widget, toolTipString); } }; template class HandlerType> struct PropertyCtrlFixture : public ToolsApplicationFixture { using HandlerAPI = IntrCtrlHandlerAPI; void SetUpEditorFixtureImpl() override { // note: must set a widget as the active window and add widgets // as children to ensure focus in/out events fire correctly m_dummyWidget = AZStd::make_unique(); QApplication::setActiveWindow(m_dummyWidget.get()); m_handler = AZStd::make_unique(); m_widget = static_cast(m_handler->CreateGUI(m_dummyWidget.get())); } void TearDownEditorFixtureImpl() override { QApplication::setActiveWindow(nullptr); m_dummyWidget.reset(); m_handler.reset(); } static void SetWidgetRangeToNonExtremeties(WidgetType* widget) { widget->setMinimum(widget->minimum() + 1); widget->setMaximum(widget->maximum() - 1); } static std::string GetToolTipStringAtLimits() { if constexpr (std::is_signed::value) { return "[-INF, INF]"; } else { return "[0, INF]"; } } void PropertyCtrlHandlersCreated() { using ::testing::Ne; EXPECT_THAT(m_handler, Ne(nullptr)); } void PropertyCtrlWidgetsCreated() { using ::testing::Ne; EXPECT_THAT(m_widget, Ne(nullptr)); } void Widget_Minimum_ExpectQtWidgetLimits_Min() { EXPECT_EQ(m_widget->minimum(), AzToolsFramework::QtWidgetLimits::Min()); } void Widget_Maximum_ExpectQtWidgetLimits_Max() { EXPECT_EQ(m_widget->maximum(), AzToolsFramework::QtWidgetLimits::Max()); } void HandlerMinMaxLimit_ModifyHandler_ExpectSuccessAndValidRangeLimitToolTipString() { // Given a widget auto& widget = m_widget; auto& handler = m_handler; QString tooltip; std::string expected; // Retrieve the tooltip string for this widget auto success = handler->ModifyTooltip(widget, tooltip); expected = GetToolTipStringAtLimits(); // Expect the operation to be successful and a valid limit tooltip string generated EXPECT_TRUE(success); EXPECT_STREQ(tooltip.toStdString().c_str(), expected.c_str()); } void HandlerMinMaxLessLimit_ModifyHandler_ExpectSuccessAndValidLessLimitToolTipString() { // Given a widget auto& widget = m_widget; auto& handler = m_handler; QString tooltip; std::stringstream expected; // That is not at the extremeties of the type range limit SetWidgetRangeToNonExtremeties(widget); // Retrieve the tooltip string for this widget auto success = handler->ModifyTooltip(widget, tooltip); expected << "[" << widget->minimum() << ", " << widget->maximum() << "]"; // Expect the operation to be successful and a valid less than limit tooltip string generated EXPECT_TRUE(success); EXPECT_STREQ(tooltip.toStdString().c_str(), expected.str().c_str()); } AZStd::unique_ptr m_dummyWidget; AZStd::unique_ptr m_handler; WidgetType* m_widget; }; } // namespace UnitTest