NativeWindow.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. #include <AzCore/UnitTest/TestTypes.h>
  9. #include <AzFramework/Components/NativeUISystemComponent.h>
  10. #include <AzFramework/Windowing/NativeWindow.h>
  11. using namespace AZ;
  12. using namespace AzFramework;
  13. namespace UnitTest
  14. {
  15. class NativeWindowTest : public LeakDetectionFixture
  16. {
  17. public:
  18. NativeWindowTest()
  19. : LeakDetectionFixture()
  20. {
  21. m_nativeUiComponent = AZStd::make_unique<AzFramework::NativeUISystemComponent>();
  22. }
  23. private:
  24. AZStd::unique_ptr<AzFramework::NativeUISystemComponent> m_nativeUiComponent;
  25. };
  26. class NativeWindowListener
  27. : public WindowNotificationBus::Handler
  28. {
  29. public:
  30. NativeWindowListener(NativeWindowHandle windowHandle)
  31. : m_windowHandle(windowHandle)
  32. {
  33. AzFramework::WindowNotificationBus::Handler::BusConnect(m_windowHandle);
  34. }
  35. ~NativeWindowListener() override
  36. {
  37. AzFramework::WindowNotificationBus::Handler::BusDisconnect(m_windowHandle);
  38. }
  39. // WindowNotificationBus::Handler overrides...
  40. void OnWindowResized(uint32_t width, uint32_t height) override
  41. {
  42. AZ_UNUSED(width);
  43. AZ_UNUSED(height);
  44. m_wasOnWindowResizedReceived = true;
  45. }
  46. void OnWindowClosed() override
  47. {
  48. m_wasOnWindowClosedReceived = true;
  49. }
  50. NativeWindowHandle m_windowHandle = nullptr;
  51. bool m_wasOnWindowResizedReceived = false;
  52. bool m_wasOnWindowClosedReceived = false;
  53. };
  54. // Test that a window can be created and will start in the non-active state
  55. #if AZ_TRAIT_DISABLE_FAILED_NATIVE_WINDOWS_TESTS
  56. TEST_F(NativeWindowTest, DISABLED_CreateWindow)
  57. #else
  58. TEST_F(NativeWindowTest, CreateWindow)
  59. #endif // AZ_TRAIT_DISABLE_FAILED_NATIVE_WINDOWS_TESTS
  60. {
  61. const uint32_t PosX = 0;
  62. const uint32_t PosY = 0;
  63. const uint32_t Width = 1280;
  64. const uint32_t Height = 720;
  65. WindowGeometry geometry(PosX, PosY, Width, Height);
  66. AZStd::unique_ptr<NativeWindow> nativeWindow = AZStd::make_unique<AzFramework::NativeWindow>(
  67. "Test Window", geometry);
  68. EXPECT_FALSE(nativeWindow == nullptr) << "NativeWindow was not allocated correctly.";
  69. EXPECT_FALSE(nativeWindow->IsActive()) << "NativeWindow was in active state after construction.";
  70. }
  71. // Test that a window can be created and activated
  72. #if AZ_TRAIT_DISABLE_FAILED_NATIVE_WINDOWS_TESTS
  73. TEST_F(NativeWindowTest, DISABLED_ActivateWindow)
  74. #else
  75. TEST_F(NativeWindowTest, ActivateWindow)
  76. #endif // AZ_TRAIT_DISABLE_FAILED_NATIVE_WINDOWS_TESTS
  77. {
  78. const uint32_t PosX = 0;
  79. const uint32_t PosY = 0;
  80. const uint32_t Width = 1280;
  81. const uint32_t Height = 720;
  82. WindowGeometry geometry(PosX, PosY, Width, Height);
  83. AZStd::unique_ptr<NativeWindow> nativeWindow = AZStd::make_unique<AzFramework::NativeWindow>(
  84. "Test Window", geometry);
  85. EXPECT_FALSE(nativeWindow == nullptr) << "NativeWindow was not allocated correctly.";
  86. nativeWindow->Activate();
  87. EXPECT_TRUE(nativeWindow->IsActive()) << "NativeWindow was in inactive state after Activate called.";
  88. // The window will get deactivated automatically when the NativeWindow is destructed
  89. }
  90. // Test that a window can be created, activated and deactivated
  91. #if AZ_TRAIT_DISABLE_FAILED_NATIVE_WINDOWS_TESTS
  92. TEST_F(NativeWindowTest, DISABLED_DectivateWindow)
  93. #else
  94. TEST_F(NativeWindowTest, DectivateWindow)
  95. #endif // AZ_TRAIT_DISABLE_FAILED_NATIVE_WINDOWS_TESTS
  96. {
  97. const uint32_t PosX = 0;
  98. const uint32_t PosY = 0;
  99. const uint32_t Width = 1280;
  100. const uint32_t Height = 720;
  101. WindowGeometry geometry(PosX, PosY, Width, Height);
  102. AZStd::unique_ptr<NativeWindow> nativeWindow = AZStd::make_unique<AzFramework::NativeWindow>(
  103. "Test Window", geometry);
  104. EXPECT_FALSE(nativeWindow == nullptr) << "NativeWindow was not allocated correctly.";
  105. nativeWindow->Activate();
  106. EXPECT_TRUE(nativeWindow->IsActive()) << "NativeWindow was in inactive state after Activate called.";
  107. nativeWindow->Deactivate();
  108. EXPECT_FALSE(nativeWindow->IsActive()) << "NativeWindow was in active state after Deactivate called.";
  109. }
  110. // Test that a window responds to the GetClientAreaSize bus request
  111. #if AZ_TRAIT_DISABLE_FAILED_NATIVE_WINDOWS_TESTS
  112. TEST_F(NativeWindowTest, DISABLED_GetClientAreaSize)
  113. #else
  114. TEST_F(NativeWindowTest, GetClientAreaSize)
  115. #endif // AZ_TRAIT_DISABLE_FAILED_NATIVE_WINDOWS_TESTS
  116. {
  117. const uint32_t PosX = 0;
  118. const uint32_t PosY = 0;
  119. const uint32_t Width = 1280;
  120. const uint32_t Height = 720;
  121. WindowGeometry geometry(PosX, PosY, Width, Height);
  122. AZStd::unique_ptr<NativeWindow> nativeWindow = AZStd::make_unique<AzFramework::NativeWindow>(
  123. "Test Window", geometry);
  124. EXPECT_FALSE(nativeWindow == nullptr) << "NativeWindow was not allocated correctly.";
  125. nativeWindow->Activate();
  126. EXPECT_TRUE(nativeWindow->IsActive()) << "NativeWindow was in inactive state after activation.";
  127. NativeWindowHandle windowHandle = nativeWindow->GetWindowHandle();
  128. WindowSize windowSize;
  129. WindowRequestBus::EventResult(windowSize, windowHandle, &WindowRequestBus::Events::GetClientAreaSize);
  130. EXPECT_TRUE(windowSize.m_width > 0) << "NativeWindow was created with wrong geometry.";
  131. EXPECT_TRUE(windowSize.m_height > 0) << "NativeWindow was created with wrong geometry.";
  132. EXPECT_TRUE(windowSize.m_width <= geometry.m_width) << "NativeWindow was created with wrong geometry.";
  133. EXPECT_TRUE(windowSize.m_height <= geometry.m_height) << "NativeWindow was created with wrong geometry.";
  134. }
  135. // Test that a window sends the correct notifications
  136. #if AZ_TRAIT_DISABLE_FAILED_NATIVE_WINDOWS_TESTS
  137. TEST_F(NativeWindowTest, DISABLED_Notifications)
  138. #else
  139. TEST_F(NativeWindowTest, Notifications)
  140. #endif // AZ_TRAIT_DISABLE_FAILED_NATIVE_WINDOWS_TESTS
  141. {
  142. const uint32_t PosX = 0;
  143. const uint32_t PosY = 0;
  144. const uint32_t Width = 1280;
  145. const uint32_t Height = 720;
  146. WindowGeometry geometry(PosX, PosY, Width, Height);
  147. AZStd::unique_ptr<NativeWindow> nativeWindow = AZStd::make_unique<AzFramework::NativeWindow>(
  148. "Test Window", geometry);
  149. EXPECT_FALSE(nativeWindow == nullptr) << "NativeWindow was not allocated correctly.";
  150. NativeWindowHandle windowHandle = nativeWindow->GetWindowHandle();
  151. EXPECT_FALSE(windowHandle == nullptr) << "NativeWindow has invalid handle.";
  152. NativeWindowListener listener(windowHandle);
  153. EXPECT_FALSE(listener.m_wasOnWindowResizedReceived) << "No notifications should be received yet.";
  154. EXPECT_FALSE(listener.m_wasOnWindowClosedReceived) << "No notifications should be received yet.";
  155. nativeWindow->Activate();
  156. WindowSize windowSize;
  157. WindowRequestBus::EventResult(windowSize, windowHandle, &WindowRequestBus::Events::GetClientAreaSize);
  158. bool windowSizeChanged = windowSize.m_width != geometry.m_width || windowSize.m_height != geometry.m_height;
  159. EXPECT_TRUE(nativeWindow->IsActive()) << "NativeWindow was in inactive state after activation.";
  160. EXPECT_TRUE(listener.m_wasOnWindowResizedReceived || !windowSizeChanged) << "Expected the OnWindowResized notification to have occurred.";
  161. EXPECT_FALSE(listener.m_wasOnWindowClosedReceived) << "Did not expect the OnWindowClosed notification to have occurred.";
  162. listener.m_wasOnWindowResizedReceived = false;
  163. nativeWindow->Deactivate();
  164. EXPECT_FALSE(nativeWindow->IsActive()) << "NativeWindow was in active state after deactivation.";
  165. EXPECT_FALSE(listener.m_wasOnWindowResizedReceived) << "Did not expect the OnWindowResized notification to have occurred.";
  166. EXPECT_TRUE(listener.m_wasOnWindowClosedReceived) << "Expected the OnWindowClosed notification to have occurred.";
  167. }
  168. } // namespace UnitTest