PropertyIntCtrlCommonTests.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <AzToolsFramework/UI/PropertyEditor/PropertyIntCtrlCommon.h>
  10. #include <AzToolsFramework/UnitTest/AzToolsFrameworkTestHelpers.h>
  11. #include <AzCore/std/typetraits/is_signed.h>
  12. #include "IntegerPrimtitiveTestConfig.h"
  13. #include <QApplication>
  14. #include <sstream>
  15. namespace UnitTest
  16. {
  17. template<typename ValueType, template <typename> class HandlerType>
  18. class IntrCtrlHandlerAPI
  19. : public HandlerType<ValueType>
  20. {
  21. using Parent = HandlerType<ValueType>;
  22. using Parent::CreateGUI;
  23. using Parent::ConsumeAttribute;
  24. using Parent::ReadValuesIntoGUI;
  25. using Parent::WriteGUIValuesIntoProperty;
  26. using Parent::ModifyTooltip;
  27. public:
  28. QWidget* CreateGUI(QWidget* pParent)
  29. {
  30. return Parent::CreateGUI(pParent);
  31. }
  32. void ConsumeAttribute(IntrCtrlHandlerAPI* GUI, AZ::u32 attrib, AzToolsFramework::PropertyAttributeReader* attrValue, const char* debugName)
  33. {
  34. Parent::ConsumeAttribute(GUI, attrib, attrValue, debugName);
  35. }
  36. bool ReadValuesIntoGUI(size_t index, IntrCtrlHandlerAPI* GUI, const ValueType& instance, AzToolsFramework::InstanceDataNode* node)
  37. {
  38. return Parent::ReadValuesIntoGUI(index, GUI, instance, node);
  39. }
  40. void WriteGUIValuesIntoProperty(size_t index, IntrCtrlHandlerAPI* GUI, ValueType& instance, AzToolsFramework::InstanceDataNode* node)
  41. {
  42. Parent::WriteGUIValuesIntoProperty(index, GUI, instance, node);
  43. }
  44. bool ModifyTooltip(QWidget* widget, QString& toolTipString)
  45. {
  46. return Parent::ModifyTooltip(widget, toolTipString);
  47. }
  48. };
  49. template<typename ValueType, typename WidgetType, template <typename> class HandlerType>
  50. struct PropertyCtrlFixture
  51. : public ToolsApplicationFixture<>
  52. {
  53. using HandlerAPI = IntrCtrlHandlerAPI<ValueType, HandlerType>;
  54. void SetUpEditorFixtureImpl() override
  55. {
  56. // note: must set a widget as the active window and add widgets
  57. // as children to ensure focus in/out events fire correctly
  58. m_dummyWidget = AZStd::make_unique<QWidget>();
  59. QApplication::setActiveWindow(m_dummyWidget.get());
  60. m_handler = AZStd::make_unique<HandlerAPI>();
  61. m_widget = static_cast<WidgetType*>(m_handler->CreateGUI(m_dummyWidget.get()));
  62. }
  63. void TearDownEditorFixtureImpl() override
  64. {
  65. QApplication::setActiveWindow(nullptr);
  66. m_dummyWidget.reset();
  67. m_handler.reset();
  68. }
  69. static void SetWidgetRangeToNonExtremeties(WidgetType* widget)
  70. {
  71. widget->setMinimum(widget->minimum() + 1);
  72. widget->setMaximum(widget->maximum() - 1);
  73. }
  74. void PropertyCtrlHandlersCreated()
  75. {
  76. using ::testing::Ne;
  77. EXPECT_THAT(m_handler, Ne(nullptr));
  78. }
  79. void PropertyCtrlWidgetsCreated()
  80. {
  81. using ::testing::Ne;
  82. EXPECT_THAT(m_widget, Ne(nullptr));
  83. }
  84. void Widget_Minimum_ExpectQtWidgetLimits_Min()
  85. {
  86. EXPECT_EQ(m_widget->minimum(), AzToolsFramework::QtWidgetLimits<ValueType>::Min());
  87. }
  88. void Widget_Maximum_ExpectQtWidgetLimits_Max()
  89. {
  90. EXPECT_EQ(m_widget->maximum(), AzToolsFramework::QtWidgetLimits<ValueType>::Max());
  91. }
  92. void HandlerMinMaxLimit_ModifyHandler_ExpectSuccessAndValidRangeLimitToolTipString()
  93. {
  94. // Given a widget
  95. auto& widget = m_widget;
  96. auto& handler = m_handler;
  97. QString tooltip;
  98. // Retrieve the tooltip string for this widget
  99. auto success = handler->ModifyTooltip(widget, tooltip);
  100. const QString minString = QLocale().toString(widget->minimum());
  101. const QString maxString = QLocale().toString(widget->maximum());
  102. const AZStd::string expected = AZStd::string::format("[%s, %s]", minString.toStdString().c_str(), maxString.toStdString().c_str());
  103. // Expect the operation to be successful and a valid limit tooltip string generated
  104. EXPECT_TRUE(success);
  105. EXPECT_STREQ(tooltip.toStdString().c_str(), expected.c_str());
  106. }
  107. void HandlerMinMaxLessLimit_ModifyHandler_ExpectSuccessAndValidLessLimitToolTipString()
  108. {
  109. // Given a widget
  110. auto& widget = m_widget;
  111. auto& handler = m_handler;
  112. QString tooltip;
  113. // That is not at the extremeties of the type range limit
  114. SetWidgetRangeToNonExtremeties(widget);
  115. // Retrieve the tooltip string for this widget
  116. auto success = handler->ModifyTooltip(widget, tooltip);
  117. const QString minString = QLocale().toString(widget->minimum());
  118. const QString maxString = QLocale().toString(widget->maximum());
  119. const AZStd::string expected = AZStd::string::format("[%s, %s]", minString.toStdString().c_str(), maxString.toStdString().c_str());
  120. // Expect the operation to be successful and a valid less than limit tooltip string generated
  121. EXPECT_TRUE(success);
  122. EXPECT_STREQ(tooltip.toStdString().c_str(), expected.c_str());
  123. }
  124. void EmitWidgetValueChanged()
  125. {
  126. emit m_widget->valueChanged(ValueType(0));
  127. }
  128. void EmitWidgetEditingFinished()
  129. {
  130. emit m_widget->editingFinished();
  131. }
  132. AZStd::unique_ptr<QWidget> m_dummyWidget;
  133. AZStd::unique_ptr<HandlerAPI> m_handler;
  134. WidgetType* m_widget;
  135. };
  136. } // namespace UnitTest