native_window_occlusion_tracker_unittest.cc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ui/aura/native_window_occlusion_tracker_win.h"
  5. #include <winuser.h>
  6. #include "base/win/scoped_gdi_object.h"
  7. #include "base/win/scoped_hdc.h"
  8. #include "base/win/windows_version.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. #include "ui/aura/test/aura_test_base.h"
  11. #include "ui/gfx/geometry/rect.h"
  12. #include "ui/gfx/win/window_impl.h"
  13. #include "dwmapi.h"
  14. namespace aura {
  15. // Test wrapper around native window HWND.
  16. class TestNativeWindow : public gfx::WindowImpl {
  17. public:
  18. TestNativeWindow() {}
  19. TestNativeWindow(const TestNativeWindow&) = delete;
  20. TestNativeWindow& operator=(const TestNativeWindow&) = delete;
  21. ~TestNativeWindow() override;
  22. private:
  23. // Overridden from gfx::WindowImpl:
  24. BOOL ProcessWindowMessage(HWND window,
  25. UINT message,
  26. WPARAM w_param,
  27. LPARAM l_param,
  28. LRESULT& result,
  29. DWORD msg_map_id) override {
  30. return FALSE; // Results in DefWindowProc().
  31. }
  32. };
  33. TestNativeWindow::~TestNativeWindow() {
  34. if (hwnd())
  35. DestroyWindow(hwnd());
  36. }
  37. // Test wrapper around native window HWND.
  38. class TestWin32Window {
  39. public:
  40. TestWin32Window() {}
  41. TestWin32Window(const TestWin32Window&) = delete;
  42. TestWin32Window& operator=(const TestWin32Window&) = delete;
  43. ~TestWin32Window();
  44. HWND Create(DWORD style);
  45. private:
  46. HWND hwnd_ = NULL;
  47. };
  48. TestWin32Window::~TestWin32Window() {
  49. if (hwnd_)
  50. DestroyWindow(hwnd_);
  51. }
  52. HWND TestWin32Window::Create(DWORD style) {
  53. const wchar_t class_name[] = L"TestWin32Window";
  54. WNDCLASSEX wcex = {sizeof(wcex)};
  55. wcex.lpfnWndProc = DefWindowProc;
  56. wcex.hInstance = ::GetModuleHandle(nullptr);
  57. wcex.lpszClassName = class_name;
  58. wcex.style = CS_HREDRAW | CS_VREDRAW;
  59. RegisterClassEx(&wcex);
  60. hwnd_ = CreateWindowEx(0, class_name, class_name, style, 0, 0, 100, 100,
  61. nullptr, nullptr, GetModuleHandle(nullptr), nullptr);
  62. ShowWindow(hwnd_, SW_SHOWNORMAL);
  63. EXPECT_TRUE(UpdateWindow(hwnd_));
  64. return hwnd_;
  65. }
  66. // This class currently tests the behavior of
  67. // NativeWindowOcclusionTrackerWin::IsWindowVisibleAndFullyOpaque with hwnds
  68. // with various attributes (e.g., minimized, transparent, etc).
  69. class NativeWindowOcclusionTrackerTest : public test::AuraTestBase {
  70. public:
  71. NativeWindowOcclusionTrackerTest() {}
  72. NativeWindowOcclusionTrackerTest(const NativeWindowOcclusionTrackerTest&) =
  73. delete;
  74. NativeWindowOcclusionTrackerTest& operator=(
  75. const NativeWindowOcclusionTrackerTest&) = delete;
  76. TestNativeWindow* native_win() { return native_win_.get(); }
  77. HWND CreateNativeWindow(DWORD style, DWORD ex_style) {
  78. native_win_ = std::make_unique<TestNativeWindow>();
  79. native_win_->set_window_style(WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN |
  80. style);
  81. native_win_->set_window_ex_style(ex_style);
  82. gfx::Rect bounds(0, 0, 100, 100);
  83. native_win_->Init(nullptr, bounds);
  84. HWND hwnd = native_win_->hwnd();
  85. base::win::ScopedRegion region(CreateRectRgn(0, 0, 0, 0));
  86. if (GetWindowRgn(hwnd, region.get()) == COMPLEXREGION) {
  87. // On Windows 7, the newly created window has a complex region, which
  88. // means it will be ignored during the occlusion calculation. So, force
  89. // it to have a simple region so that we get test coverage on win 7.
  90. RECT bounding_rect;
  91. EXPECT_TRUE(GetWindowRect(hwnd, &bounding_rect));
  92. base::win::ScopedRegion rectangular_region(
  93. CreateRectRgnIndirect(&bounding_rect));
  94. SetWindowRgn(hwnd, rectangular_region.get(), /*redraw=*/TRUE);
  95. }
  96. ShowWindow(hwnd, SW_SHOWNORMAL);
  97. EXPECT_TRUE(UpdateWindow(hwnd));
  98. return hwnd;
  99. }
  100. // Wrapper around IsWindowVisibleAndFullyOpaque so only the test class
  101. // needs to be a friend of NativeWindowOcclusionTrackerWin.
  102. bool CheckWindowVisibleAndFullyOpaque(HWND hwnd, gfx::Rect* win_rect) {
  103. bool ret = NativeWindowOcclusionTrackerWin::IsWindowVisibleAndFullyOpaque(
  104. hwnd, win_rect);
  105. // In general, if IsWindowVisibleAndFullyOpaque returns false, the
  106. // returned rect should not be altered.
  107. if (!ret)
  108. EXPECT_EQ(*win_rect, gfx::Rect(0, 0, 0, 0));
  109. return ret;
  110. }
  111. private:
  112. std::unique_ptr<TestNativeWindow> native_win_;
  113. };
  114. TEST_F(NativeWindowOcclusionTrackerTest, VisibleOpaqueWindow) {
  115. HWND hwnd = CreateNativeWindow(/*style=*/0, /*ex_style=*/0);
  116. gfx::Rect returned_rect;
  117. // Normal windows should be visible.
  118. EXPECT_TRUE(CheckWindowVisibleAndFullyOpaque(hwnd, &returned_rect));
  119. // Check that the returned rect == the actual window rect of the hwnd.
  120. RECT win_rect;
  121. ASSERT_TRUE(GetWindowRect(hwnd, &win_rect));
  122. EXPECT_EQ(returned_rect, gfx::Rect(win_rect));
  123. }
  124. TEST_F(NativeWindowOcclusionTrackerTest, MinimizedWindow) {
  125. HWND hwnd = CreateNativeWindow(/*style=*/0, /*ex_style=*/0);
  126. gfx::Rect win_rect;
  127. ShowWindow(hwnd, SW_MINIMIZE);
  128. // Minimized windows are not considered visible.
  129. EXPECT_FALSE(CheckWindowVisibleAndFullyOpaque(hwnd, &win_rect));
  130. }
  131. TEST_F(NativeWindowOcclusionTrackerTest, TransparentWindow) {
  132. HWND hwnd = CreateNativeWindow(/*style=*/0, WS_EX_TRANSPARENT);
  133. gfx::Rect win_rect;
  134. // Transparent windows are not considered visible and opaque.
  135. EXPECT_FALSE(CheckWindowVisibleAndFullyOpaque(hwnd, &win_rect));
  136. }
  137. TEST_F(NativeWindowOcclusionTrackerTest, ToolWindow) {
  138. HWND hwnd = CreateNativeWindow(/*style=*/0, WS_EX_TOOLWINDOW);
  139. gfx::Rect win_rect;
  140. // Tool windows are not considered visible and opaque.
  141. EXPECT_FALSE(CheckWindowVisibleAndFullyOpaque(hwnd, &win_rect));
  142. }
  143. TEST_F(NativeWindowOcclusionTrackerTest, LayeredAlphaWindow) {
  144. HWND hwnd = CreateNativeWindow(/*style=*/0, WS_EX_LAYERED);
  145. gfx::Rect win_rect;
  146. BYTE alpha = 1;
  147. DWORD flags = LWA_ALPHA;
  148. COLORREF color_ref = RGB(1, 1, 1);
  149. SetLayeredWindowAttributes(hwnd, color_ref, alpha, flags);
  150. // Layered windows with alpha < 255 are not considered visible and opaque.
  151. EXPECT_FALSE(CheckWindowVisibleAndFullyOpaque(hwnd, &win_rect));
  152. }
  153. TEST_F(NativeWindowOcclusionTrackerTest, UpdatedLayeredAlphaWindow) {
  154. HWND hwnd = CreateNativeWindow(/*style=*/0, WS_EX_LAYERED);
  155. gfx::Rect win_rect;
  156. base::win::ScopedCreateDC hdc(::CreateCompatibleDC(nullptr));
  157. BLENDFUNCTION blend = {AC_SRC_OVER, 0x00, 0xFF, AC_SRC_ALPHA};
  158. ::UpdateLayeredWindow(hwnd, hdc.Get(), nullptr, nullptr, nullptr, nullptr,
  159. RGB(0xFF, 0xFF, 0xFF), &blend, ULW_OPAQUE);
  160. // Layered windows set up with UpdateLayeredWindow instead of
  161. // SetLayeredWindowAttributes should not be considered visible and opaque.
  162. EXPECT_FALSE(CheckWindowVisibleAndFullyOpaque(hwnd, &win_rect));
  163. }
  164. TEST_F(NativeWindowOcclusionTrackerTest, LayeredNonAlphaWindow) {
  165. HWND hwnd = CreateNativeWindow(/*style=*/0, WS_EX_LAYERED);
  166. gfx::Rect win_rect;
  167. BYTE alpha = 1;
  168. DWORD flags = 0;
  169. COLORREF color_ref = RGB(1, 1, 1);
  170. SetLayeredWindowAttributes(hwnd, color_ref, alpha, flags);
  171. // Layered non alpha windows are considered visible and opaque.
  172. EXPECT_TRUE(CheckWindowVisibleAndFullyOpaque(hwnd, &win_rect));
  173. }
  174. TEST_F(NativeWindowOcclusionTrackerTest, ComplexRegionWindow) {
  175. HWND hwnd = CreateNativeWindow(/*style=*/0, /*ex_style=*/0);
  176. gfx::Rect win_rect;
  177. // Create a region with rounded corners, which should be a complex region.
  178. base::win::ScopedRegion region(CreateRoundRectRgn(1, 1, 100, 100, 5, 5));
  179. SetWindowRgn(hwnd, region.get(), /*redraw=*/TRUE);
  180. // Windows with complex regions are not considered visible and fully opaque.
  181. EXPECT_FALSE(CheckWindowVisibleAndFullyOpaque(hwnd, &win_rect));
  182. }
  183. TEST_F(NativeWindowOcclusionTrackerTest, PopupChromeWindow) {
  184. HWND hwnd = CreateNativeWindow(WS_POPUP, /*ex_style=*/0);
  185. gfx::Rect win_rect;
  186. // Chrome Popup Windows of class Chrome_WidgetWin_ are considered visible.
  187. EXPECT_TRUE(CheckWindowVisibleAndFullyOpaque(hwnd, &win_rect));
  188. }
  189. TEST_F(NativeWindowOcclusionTrackerTest, PopupWindow) {
  190. TestWin32Window test_window;
  191. HWND hwnd = test_window.Create(WS_POPUPWINDOW);
  192. gfx::Rect win_rect;
  193. // Normal Popup Windows are not considered visible.
  194. EXPECT_FALSE(CheckWindowVisibleAndFullyOpaque(hwnd, &win_rect));
  195. }
  196. TEST_F(NativeWindowOcclusionTrackerTest, CloakedWindow) {
  197. // Cloaking is only supported in Windows 8 and above.
  198. if (base::win::GetVersion() < base::win::Version::WIN8)
  199. return;
  200. HWND hwnd = CreateNativeWindow(/*style=*/0, /*ex_style=*/0);
  201. gfx::Rect win_rect;
  202. BOOL cloak = TRUE;
  203. DwmSetWindowAttribute(hwnd, DWMWA_CLOAK, &cloak, sizeof(cloak));
  204. // Cloaked Windows are not considered visible.
  205. EXPECT_FALSE(CheckWindowVisibleAndFullyOpaque(hwnd, &win_rect));
  206. }
  207. } // namespace aura