window_occlusion_impl_unittest_win.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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_extra/window_occlusion_impl_win.h"
  5. #include "base/containers/contains.h"
  6. #include "base/win/scoped_gdi_object.h"
  7. #include "testing/gtest/include/gtest/gtest.h"
  8. #include "ui/aura/test/aura_test_base.h"
  9. #include "ui/aura/window_tree_host_platform.h"
  10. #include "ui/gfx/geometry/rect.h"
  11. #include "ui/platform_window/platform_window_init_properties.h"
  12. namespace aura_extra {
  13. // A single set of arguments that are passed to WindowEvaluator::Evaluate().
  14. // Used to mock calls to WindowEvaluator::Evaluate().
  15. struct EvaluatorArgs {
  16. bool is_relevant;
  17. gfx::Rect window_rect;
  18. HWND hwnd;
  19. };
  20. // Iterates over a provided set of mock windows and their properties.
  21. class MockNativeWindowIterator : public NativeWindowIterator {
  22. public:
  23. MockNativeWindowIterator(
  24. const std::vector<EvaluatorArgs>& evaluator_args_list)
  25. : args_list_(evaluator_args_list) {}
  26. MockNativeWindowIterator(const MockNativeWindowIterator&) = delete;
  27. MockNativeWindowIterator& operator=(const MockNativeWindowIterator&) = delete;
  28. void Iterate(WindowEvaluator* evaluator) override {
  29. for (EvaluatorArgs args : args_list_) {
  30. if (!evaluator->EvaluateWindow(args.is_relevant, args.window_rect,
  31. args.hwnd))
  32. return;
  33. }
  34. }
  35. private:
  36. std::vector<EvaluatorArgs> args_list_;
  37. };
  38. // Test implementation of WindowBoundsDelegate using a flat_map of aura::Window
  39. // to gfx::Rect.
  40. class MockWindowBoundsDelegateImpl : public WindowBoundsDelegate {
  41. public:
  42. MockWindowBoundsDelegateImpl() = default;
  43. MockWindowBoundsDelegateImpl(const MockWindowBoundsDelegateImpl&) = delete;
  44. MockWindowBoundsDelegateImpl& operator=(const MockWindowBoundsDelegateImpl&) =
  45. delete;
  46. // WindowBoundsDelegate implementation:
  47. gfx::Rect GetBoundsInPixels(aura::WindowTreeHost* window) override {
  48. return root_window_bounds_[window];
  49. }
  50. void AddWindowWithBounds(aura::WindowTreeHost* window,
  51. const gfx::Rect& window_bounds_in_pixels) {
  52. root_window_bounds_[window] = window_bounds_in_pixels;
  53. }
  54. private:
  55. base::flat_map<aura::WindowTreeHost*, gfx::Rect> root_window_bounds_;
  56. };
  57. // The int argument here is an offset in pixels for tests that need to offset
  58. // windows. This allows for variable offsets in the parameterized tests.
  59. using OffsetAndBoundsPair = std::pair<int, gfx::Rect>;
  60. class WindowOcclusionWinTest
  61. : public aura::test::AuraTestBase,
  62. public ::testing::WithParamInterface<OffsetAndBoundsPair> {
  63. public:
  64. WindowOcclusionWinTest() {}
  65. WindowOcclusionWinTest(const WindowOcclusionWinTest&) = delete;
  66. WindowOcclusionWinTest& operator=(const WindowOcclusionWinTest&) = delete;
  67. void TearDown() override {
  68. Clear();
  69. aura::test::AuraTestBase::TearDown();
  70. }
  71. aura::WindowTreeHost* AddRootAuraWindowWithBounds(const gfx::Rect& bounds) {
  72. std::unique_ptr<aura::WindowTreeHost> window_tree_host =
  73. aura::WindowTreeHost::Create(ui::PlatformWindowInitProperties{bounds});
  74. window_tree_host->window()->Show();
  75. EvaluatorArgs args{true, bounds, window_tree_host->GetAcceleratedWidget()};
  76. evaluator_args_list_.push_back(args);
  77. mock_bounds_delegate_->AddWindowWithBounds(window_tree_host.get(), bounds);
  78. aura::WindowTreeHost* host = window_tree_host.get();
  79. window_tree_hosts_.push_back(std::move(window_tree_host));
  80. return host;
  81. }
  82. void AddMockNativeWindowWithBounds(gfx::Rect bounds) {
  83. EvaluatorArgs args{true, bounds, 0};
  84. evaluator_args_list_.push_back(args);
  85. }
  86. base::flat_map<aura::WindowTreeHost*, aura::Window::OcclusionState>
  87. ComputeOcclusion() {
  88. std::unique_ptr<NativeWindowIterator> iterator =
  89. std::make_unique<MockNativeWindowIterator>(evaluator_args_list_);
  90. std::vector<aura::WindowTreeHost*> window_tree_hosts;
  91. for (auto& host : window_tree_hosts_)
  92. window_tree_hosts.push_back(host.get());
  93. return ComputeNativeWindowOcclusionStatusImpl(
  94. window_tree_hosts, std::move(iterator),
  95. std::unique_ptr<WindowBoundsDelegate>(mock_bounds_delegate_.release()));
  96. }
  97. void Clear() {
  98. evaluator_args_list_.clear();
  99. window_tree_hosts_.clear();
  100. }
  101. private:
  102. std::vector<EvaluatorArgs> evaluator_args_list_;
  103. std::vector<std::unique_ptr<aura::WindowTreeHost>> window_tree_hosts_;
  104. std::unique_ptr<MockWindowBoundsDelegateImpl> mock_bounds_delegate_ =
  105. std::make_unique<MockWindowBoundsDelegateImpl>();
  106. };
  107. // An aura window completely covered by a native window should be occluded.
  108. TEST_P(WindowOcclusionWinTest, SimpleOccluded) {
  109. OffsetAndBoundsPair param = GetParam();
  110. AddMockNativeWindowWithBounds(param.second);
  111. aura::WindowTreeHost* window = AddRootAuraWindowWithBounds(param.second);
  112. base::flat_map<aura::WindowTreeHost*, aura::Window::OcclusionState> result =
  113. ComputeOcclusion();
  114. EXPECT_EQ(result.size(), 1U);
  115. ASSERT_TRUE(base::Contains(result, window));
  116. EXPECT_EQ(result[window], aura::Window::OcclusionState::OCCLUDED);
  117. }
  118. // An aura window not occluded at all by a native window should be visible.
  119. TEST_P(WindowOcclusionWinTest, SimpleVisible) {
  120. OffsetAndBoundsPair param = GetParam();
  121. aura::WindowTreeHost* window = AddRootAuraWindowWithBounds(param.second);
  122. AddMockNativeWindowWithBounds(param.second);
  123. base::flat_map<aura::WindowTreeHost*, aura::Window::OcclusionState> result =
  124. ComputeOcclusion();
  125. EXPECT_EQ(result.size(), 1U);
  126. ASSERT_TRUE(base::Contains(result, window));
  127. EXPECT_EQ(result[window], aura::Window::OcclusionState::VISIBLE);
  128. Clear();
  129. }
  130. // An aura window occluded by an aura window should be occluded.
  131. TEST_P(WindowOcclusionWinTest, OccludedByAuraWindow) {
  132. OffsetAndBoundsPair param = GetParam();
  133. aura::WindowTreeHost* window1 = AddRootAuraWindowWithBounds(param.second);
  134. aura::WindowTreeHost* window2 = AddRootAuraWindowWithBounds(param.second);
  135. std::vector<aura::WindowTreeHost*> windows({window1, window2});
  136. base::flat_map<aura::WindowTreeHost*, aura::Window::OcclusionState> result =
  137. ComputeOcclusion();
  138. EXPECT_EQ(result.size(), 2U);
  139. ASSERT_TRUE(base::Contains(result, window1));
  140. EXPECT_EQ(result[window1], aura::Window::OcclusionState::VISIBLE);
  141. ASSERT_TRUE(base::Contains(result, window2));
  142. EXPECT_EQ(result[window2], aura::Window::OcclusionState::OCCLUDED);
  143. }
  144. // An aura window occluded by two native windows should be occluded.
  145. TEST_P(WindowOcclusionWinTest, OccludedByMultipleWindows) {
  146. OffsetAndBoundsPair param = GetParam();
  147. gfx::Rect left_half = param.second;
  148. left_half.Offset(-left_half.width() / 2, 0);
  149. gfx::Rect right_half = param.second;
  150. right_half.Offset(right_half.width() / 2, 0);
  151. AddMockNativeWindowWithBounds(left_half);
  152. AddMockNativeWindowWithBounds(right_half);
  153. aura::WindowTreeHost* window = AddRootAuraWindowWithBounds(param.second);
  154. base::flat_map<aura::WindowTreeHost*, aura::Window::OcclusionState> result =
  155. ComputeOcclusion();
  156. EXPECT_EQ(result.size(), 1U);
  157. ASSERT_TRUE(base::Contains(result, window));
  158. EXPECT_EQ(result[window], aura::Window::OcclusionState::OCCLUDED);
  159. }
  160. // An aura window partially occluded by an aura window should be visible.
  161. TEST_P(WindowOcclusionWinTest, PartiallyOverlappedAuraWindows) {
  162. OffsetAndBoundsPair param = GetParam();
  163. aura::WindowTreeHost* window1 = AddRootAuraWindowWithBounds(param.second);
  164. gfx::Rect offset_bounds = param.second;
  165. offset_bounds.Offset(param.first, param.first);
  166. aura::WindowTreeHost* window2 = AddRootAuraWindowWithBounds(offset_bounds);
  167. base::flat_map<aura::WindowTreeHost*, aura::Window::OcclusionState> result =
  168. ComputeOcclusion();
  169. EXPECT_EQ(result.size(), 2U);
  170. ASSERT_TRUE(base::Contains(result, window1));
  171. EXPECT_EQ(result[window1], aura::Window::OcclusionState::VISIBLE);
  172. ASSERT_TRUE(base::Contains(result, window2));
  173. EXPECT_EQ(result[window2], aura::Window::OcclusionState::VISIBLE);
  174. }
  175. // An aura window partially occluded by a native window should be visible.
  176. TEST_P(WindowOcclusionWinTest, PartiallyOverlappedWindows) {
  177. OffsetAndBoundsPair param = GetParam();
  178. aura::WindowTreeHost* window = AddRootAuraWindowWithBounds(param.second);
  179. gfx::Rect offset_bounds = param.second;
  180. offset_bounds.Offset(param.first, param.first);
  181. AddMockNativeWindowWithBounds(offset_bounds);
  182. base::flat_map<aura::WindowTreeHost*, aura::Window::OcclusionState> result =
  183. ComputeOcclusion();
  184. EXPECT_EQ(result.size(), 1U);
  185. ASSERT_TRUE(base::Contains(result, window));
  186. EXPECT_EQ(result[window], aura::Window::OcclusionState::VISIBLE);
  187. }
  188. // If for some reason the bounds of an aura::Window are empty, this signals some
  189. // sort of failure in the call to GetWindowRect() This tests that in this case
  190. // the window is marked as aura::Window::OcclusionState::VISIBLE to avoid
  191. // falsely marking it as occluded.
  192. TEST_P(WindowOcclusionWinTest, EmptyWindowIsVisible) {
  193. aura::WindowTreeHost* window =
  194. AddRootAuraWindowWithBounds(gfx::Rect(0, 0, 0, 0));
  195. base::flat_map<aura::WindowTreeHost*, aura::Window::OcclusionState> result =
  196. ComputeOcclusion();
  197. EXPECT_EQ(result.size(), 1U);
  198. ASSERT_TRUE(base::Contains(result, window));
  199. EXPECT_EQ(result[window], aura::Window::OcclusionState::VISIBLE);
  200. }
  201. INSTANTIATE_TEST_SUITE_P(All,
  202. WindowOcclusionWinTest,
  203. ::testing::Values(
  204. OffsetAndBoundsPair(5, gfx::Rect(0, 0, 100, 100)),
  205. OffsetAndBoundsPair(10, gfx::Rect(0, 0, 100, 200)),
  206. OffsetAndBoundsPair(15, gfx::Rect(0, 0, 200, 100)),
  207. OffsetAndBoundsPair(20, gfx::Rect(0, 0, 200, 200)),
  208. OffsetAndBoundsPair(25,
  209. gfx::Rect(0, 50, 100, 100)),
  210. OffsetAndBoundsPair(50,
  211. gfx::Rect(0, 50, 100, 200)),
  212. OffsetAndBoundsPair(75,
  213. gfx::Rect(0, 50, 200, 100)),
  214. OffsetAndBoundsPair(100,
  215. gfx::Rect(0, 50, 200, 200)),
  216. OffsetAndBoundsPair(125,
  217. gfx::Rect(100, 0, 100, 100)),
  218. OffsetAndBoundsPair(150,
  219. gfx::Rect(100, 0, 100, 200)),
  220. OffsetAndBoundsPair(200,
  221. gfx::Rect(100, 0, 200, 100)),
  222. OffsetAndBoundsPair(250,
  223. gfx::Rect(100, 0, 200, 200)),
  224. OffsetAndBoundsPair(300,
  225. gfx::Rect(100, 50, 100, 100)),
  226. OffsetAndBoundsPair(400,
  227. gfx::Rect(100, 50, 100, 200)),
  228. OffsetAndBoundsPair(500,
  229. gfx::Rect(100, 50, 200, 100)),
  230. OffsetAndBoundsPair(
  231. 750,
  232. gfx::Rect(100, 50, 200, 200))));
  233. class WindowFitnessFunctionTest : public testing::Test {
  234. public:
  235. HWND CreateNativeWindow(gfx::Rect bounds) {
  236. HWND hwnd = CreateWindow(L"STATIC", L"TestWindow", WS_OVERLAPPED,
  237. bounds.x(), bounds.y(), bounds.width(),
  238. bounds.height(), (HWND)NULL, NULL, NULL, NULL);
  239. return hwnd;
  240. }
  241. // Adds the windows style |style| to |hwnd|.
  242. void AddStyle(HWND hwnd, int style_type, DWORD style) {
  243. SetWindowLong(hwnd, style_type, GetWindowLong(hwnd, style_type) | style);
  244. }
  245. void RemoveStyle(HWND hwnd, int style_type, DWORD style) {
  246. SetWindowLong(hwnd, style_type, GetWindowLong(hwnd, style_type) & ~style);
  247. }
  248. };
  249. TEST_F(WindowFitnessFunctionTest, FitnessTest) {
  250. HWND hwnd = CreateNativeWindow(gfx::Rect(0, 0, 100, 100));
  251. gfx::Rect rect;
  252. // The window doesn't have the WS_VISIBLE style yet, so it should not pass.
  253. EXPECT_FALSE(IsWindowVisibleAndFullyOpaque(hwnd, &rect));
  254. AddStyle(hwnd, GWL_STYLE, WS_VISIBLE);
  255. EXPECT_TRUE(IsWindowVisibleAndFullyOpaque(hwnd, &rect));
  256. AddStyle(hwnd, GWL_STYLE, WS_MINIMIZE);
  257. EXPECT_FALSE(IsWindowVisibleAndFullyOpaque(hwnd, &rect));
  258. RemoveStyle(hwnd, GWL_STYLE, WS_MINIMIZE);
  259. EXPECT_TRUE(IsWindowVisibleAndFullyOpaque(hwnd, &rect));
  260. AddStyle(hwnd, GWL_EXSTYLE, WS_EX_TRANSPARENT);
  261. EXPECT_FALSE(IsWindowVisibleAndFullyOpaque(hwnd, &rect));
  262. RemoveStyle(hwnd, GWL_EXSTYLE, WS_EX_TRANSPARENT);
  263. EXPECT_TRUE(IsWindowVisibleAndFullyOpaque(hwnd, &rect));
  264. AddStyle(hwnd, GWL_EXSTYLE, WS_EX_TOOLWINDOW);
  265. EXPECT_FALSE(IsWindowVisibleAndFullyOpaque(hwnd, &rect));
  266. RemoveStyle(hwnd, GWL_EXSTYLE, WS_EX_TOOLWINDOW);
  267. EXPECT_TRUE(IsWindowVisibleAndFullyOpaque(hwnd, &rect));
  268. AddStyle(hwnd, GWL_EXSTYLE, WS_EX_LAYERED);
  269. SetLayeredWindowAttributes(hwnd, RGB(10, 10, 10), NULL, LWA_COLORKEY);
  270. EXPECT_FALSE(IsWindowVisibleAndFullyOpaque(hwnd, &rect));
  271. RemoveStyle(hwnd, GWL_EXSTYLE, WS_EX_LAYERED);
  272. EXPECT_TRUE(IsWindowVisibleAndFullyOpaque(hwnd, &rect));
  273. AddStyle(hwnd, GWL_EXSTYLE, WS_EX_LAYERED);
  274. SetLayeredWindowAttributes(hwnd, NULL, 0, LWA_ALPHA);
  275. EXPECT_FALSE(IsWindowVisibleAndFullyOpaque(hwnd, &rect));
  276. RemoveStyle(hwnd, GWL_EXSTYLE, WS_EX_LAYERED);
  277. EXPECT_TRUE(IsWindowVisibleAndFullyOpaque(hwnd, &rect));
  278. AddStyle(hwnd, GWL_EXSTYLE, WS_EX_LAYERED);
  279. SetLayeredWindowAttributes(hwnd, NULL, 255, LWA_ALPHA);
  280. EXPECT_FALSE(IsWindowVisibleAndFullyOpaque(hwnd, &rect));
  281. RemoveStyle(hwnd, GWL_EXSTYLE, WS_EX_LAYERED);
  282. EXPECT_TRUE(IsWindowVisibleAndFullyOpaque(hwnd, &rect));
  283. // Any complex region should fail, as we only consider simple rectangular
  284. // windows. In this case, we make the region a triangle.
  285. POINT point1;
  286. point1.x = 0;
  287. point1.y = 0;
  288. POINT point2;
  289. point2.x = 50;
  290. point2.y = 0;
  291. POINT point3;
  292. point3.x = 0;
  293. point3.y = 50;
  294. POINT points[] = {point1, point2, point3};
  295. base::win::ScopedRegion complex_region(CreatePolygonRgn(points, 3, WINDING));
  296. SetWindowRgn(hwnd, complex_region.get(), TRUE);
  297. EXPECT_FALSE(IsWindowVisibleAndFullyOpaque(hwnd, &rect));
  298. // A rectangular region should pass.
  299. base::win::ScopedRegion rectangular_region(CreateRectRgn(200, 200, 200, 200));
  300. SetWindowRgn(hwnd, rectangular_region.get(), TRUE);
  301. EXPECT_TRUE(IsWindowVisibleAndFullyOpaque(hwnd, &rect));
  302. }
  303. class MockWindowEvaluator : public WindowEvaluator {
  304. public:
  305. // At the end of the test, the window_stack must be empty, otherwise not all
  306. // the windows were seen.
  307. ~MockWindowEvaluator() { EXPECT_TRUE(window_stack.empty()); }
  308. // Tests that EnumWindows goes front to back by creating a stack of aura
  309. // windows and popping the top window off the stack as its HWND is seen in
  310. // this callback. If the stack isn't empty at the end, EnumWindows did not see
  311. // all the windows, or did not see them in the correct order.
  312. bool EvaluateWindow(bool is_relevant,
  313. const gfx::Rect& window_rect_in_pixels,
  314. HWND hwnd) override {
  315. if (window_stack.empty())
  316. return FALSE;
  317. HWND top_window_hwnd =
  318. window_stack.top()->GetHost()->GetAcceleratedWidget();
  319. if (hwnd == top_window_hwnd)
  320. window_stack.pop();
  321. return TRUE;
  322. }
  323. void AddToStack(aura::Window* window) { window_stack.push(window); }
  324. private:
  325. base::stack<aura::Window*> window_stack;
  326. };
  327. // Tests the functionality of EnumWindows. Specifically:
  328. // 1) EnumWindows enumerates all windows on the desktop.
  329. // 2) EnumWindows enumerates from front to back in the Z-order.
  330. // This needs to be tested because 2) is undocumented behavior. However, this
  331. // behavior has been observed in the community and tested to be true.
  332. // ComputeNativeWindowOcclusionStatus() relies on this assumption.
  333. class EnumWindowsTest : public aura::test::AuraTestBase {
  334. public:
  335. EnumWindowsTest() {}
  336. EnumWindowsTest(const EnumWindowsTest&) = delete;
  337. EnumWindowsTest& operator=(const EnumWindowsTest&) = delete;
  338. void TearDown() override {
  339. window_tree_hosts_.clear();
  340. aura::test::AuraTestBase::TearDown();
  341. }
  342. void CreateAuraWindowWithBounds(const gfx::Rect& bounds) {
  343. std::unique_ptr<aura::WindowTreeHost> host =
  344. aura::WindowTreeHost::Create(ui::PlatformWindowInitProperties{bounds});
  345. host->window()->Show();
  346. evaluator_.AddToStack(host->window());
  347. window_tree_hosts_.push_back(std::move(host));
  348. }
  349. void TestIterator() {
  350. // The iterator will validate that the OS returns the full set of windows in
  351. // the expected order, as encoded by |window_stack| in |evaluator_|
  352. WindowsDesktopWindowIterator iterator;
  353. iterator.Iterate(&evaluator_);
  354. }
  355. private:
  356. std::vector<std::unique_ptr<aura::WindowTreeHost>> window_tree_hosts_;
  357. MockWindowEvaluator evaluator_;
  358. };
  359. TEST_F(EnumWindowsTest, EnumWindowsGoesFrontToBack) {
  360. CreateAuraWindowWithBounds(gfx::Rect(0, 0, 100, 100));
  361. CreateAuraWindowWithBounds(gfx::Rect(50, 50, 500, 500));
  362. CreateAuraWindowWithBounds(gfx::Rect(20, 20, 300, 50));
  363. CreateAuraWindowWithBounds(gfx::Rect(200, 200, 10, 10));
  364. CreateAuraWindowWithBounds(gfx::Rect(0, 0, 100, 100));
  365. TestIterator();
  366. }
  367. } // namespace aura_extra