NativeWindow.cpp 7.9 KB

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