| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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 |
| 5 #include "ui/base/x/x11_window_cache.h" |
| 6 |
| 7 #include "base/macros.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "ui/gfx/x/x11_types.h" |
| 10 |
| 11 #include <X11/Xlib.h> |
| 12 #include <X11/Xatom.h> |
| 13 |
| 14 namespace ui { |
| 15 |
| 16 class XWindowCacheTest : public testing::Test { |
| 17 public: |
| 18 XWindowCacheTest() |
| 19 : display_(gfx::GetXDisplay()), |
| 20 root_(DefaultRootWindow(display_)), |
| 21 testing_atom_(XInternAtom(display_, "CHROMIUM_TESTING_ATOM", False)) {} |
| 22 ~XWindowCacheTest() override {} |
| 23 |
| 24 protected: |
| 25 void SetUp() override { XSynchronize(display_, True); } |
| 26 |
| 27 void TearDown() override { XSynchronize(display_, False); } |
| 28 |
| 29 XID CreateWindow(XID parent) { |
| 30 XSetWindowAttributes swa; |
| 31 swa.override_redirect = True; |
| 32 return XCreateWindow(display_, parent, 0, 0, 1, 1, 0, CopyFromParent, |
| 33 InputOutput, CopyFromParent, CWOverrideRedirect, &swa); |
| 34 } |
| 35 |
| 36 void SetProperty8(XID window, XID property, uint8_t value) { |
| 37 XChangeProperty(display_, window, property, XA_CARDINAL, 8, PropModeReplace, |
| 38 &value, 1); |
| 39 } |
| 40 |
| 41 template <typename T> |
| 42 void VerifyStackingOrder(const XWindowCache::Window* window, |
| 43 const T& container) { |
| 44 EXPECT_TRUE(window); |
| 45 EXPECT_EQ(window->children.size(), container.size()); |
| 46 auto it = window->children.begin(); |
| 47 for (XID id : container) |
| 48 EXPECT_EQ(id, (*it++)->id); |
| 49 } |
| 50 |
| 51 XDisplay* display_; |
| 52 XID root_; |
| 53 XAtom testing_atom_; |
| 54 |
| 55 private: |
| 56 DISALLOW_COPY_AND_ASSIGN(XWindowCacheTest); |
| 57 }; |
| 58 |
| 59 TEST_F(XWindowCacheTest, BasicTest) { |
| 60 XID parent_xid = CreateWindow(root_); |
| 61 XID child1_xid = CreateWindow(parent_xid); |
| 62 XID child2_xid = CreateWindow(parent_xid); |
| 63 |
| 64 XWindowCache cache(display_, parent_xid); |
| 65 EXPECT_TRUE(cache.BlockUntilTreeIsCached()); |
| 66 auto parent_window = cache.GetWindow(parent_xid); |
| 67 EXPECT_TRUE(parent_window); |
| 68 EXPECT_EQ(parent_window->children.size(), 2U); |
| 69 auto it = parent_window->children.begin(); |
| 70 EXPECT_EQ((*it++)->id, child2_xid); |
| 71 EXPECT_EQ((*it++)->id, child1_xid); |
| 72 |
| 73 XDestroyWindow(display_, parent_xid); |
| 74 } |
| 75 |
| 76 TEST_F(XWindowCacheTest, NestingTest) { |
| 77 XID parent_xid = CreateWindow(root_); |
| 78 XID child_xid = CreateWindow(parent_xid); |
| 79 XID nested_child_xid = CreateWindow(child_xid); |
| 80 SetProperty8(nested_child_xid, testing_atom_, 0x42); |
| 81 |
| 82 XWindowCache cache(display_, parent_xid); |
| 83 EXPECT_TRUE(cache.BlockUntilTreeIsCached()); |
| 84 |
| 85 auto parent_window = cache.GetWindow(parent_xid); |
| 86 EXPECT_TRUE(parent_window); |
| 87 EXPECT_EQ(parent_window->children.size(), 1U); |
| 88 EXPECT_EQ(parent_window->properties.size(), 0U); |
| 89 |
| 90 auto child_window = parent_window->children.front(); |
| 91 EXPECT_TRUE(child_window); |
| 92 EXPECT_EQ(child_window->children.size(), 1U); |
| 93 EXPECT_EQ(child_window->properties.size(), 0U); |
| 94 |
| 95 auto nested_child_window = child_window->children.front(); |
| 96 EXPECT_TRUE(nested_child_window); |
| 97 EXPECT_EQ(nested_child_window->children.size(), 0U); |
| 98 EXPECT_EQ(nested_child_window->properties.size(), 1U); |
| 99 |
| 100 auto prop = nested_child_window->GetProperty(testing_atom_); |
| 101 EXPECT_TRUE(prop); |
| 102 EXPECT_TRUE(prop->cached_property); |
| 103 EXPECT_EQ(prop->type, XA_CARDINAL); |
| 104 EXPECT_EQ(prop->data_format, 8); |
| 105 EXPECT_EQ(prop->data_length, 1); |
| 106 EXPECT_EQ(prop->data.bits_8[0], 0x42); |
| 107 |
| 108 XDestroyWindow(display_, parent_xid); |
| 109 } |
| 110 |
| 111 TEST_F(XWindowCacheTest, CreateNotifyAndDestroyNotifyTest) { |
| 112 XID parent_xid = CreateWindow(root_); |
| 113 |
| 114 XWindowCache cache(display_, parent_xid); |
| 115 EXPECT_TRUE(cache.BlockUntilTreeIsCached()); |
| 116 |
| 117 XID child1_xid = CreateWindow(parent_xid); |
| 118 XID child2_xid = CreateWindow(parent_xid); |
| 119 |
| 120 EXPECT_TRUE(cache.Synchronize()); |
| 121 EXPECT_TRUE(cache.BlockUntilTreeIsCached()); |
| 122 |
| 123 auto parent_window = cache.GetWindow(parent_xid); |
| 124 EXPECT_TRUE(parent_window); |
| 125 EXPECT_EQ(parent_window->children.size(), 2U); |
| 126 auto it = parent_window->children.begin(); |
| 127 EXPECT_EQ((*it++)->id, child2_xid); |
| 128 EXPECT_EQ((*it++)->id, child1_xid); |
| 129 |
| 130 XDestroyWindow(display_, child1_xid); |
| 131 XDestroyWindow(display_, child2_xid); |
| 132 |
| 133 EXPECT_TRUE(cache.Synchronize()); |
| 134 EXPECT_TRUE(cache.BlockUntilTreeIsCached()); |
| 135 |
| 136 parent_window = cache.GetWindow(parent_xid); |
| 137 EXPECT_TRUE(parent_window); |
| 138 EXPECT_EQ(parent_window->children.size(), 0U); |
| 139 |
| 140 XDestroyWindow(display_, parent_xid); |
| 141 } |
| 142 |
| 143 TEST_F(XWindowCacheTest, PropertyNotifyTest) { |
| 144 XID window_xid = CreateWindow(root_); |
| 145 SetProperty8(window_xid, testing_atom_, 0x42); |
| 146 |
| 147 XWindowCache cache(display_, window_xid); |
| 148 EXPECT_TRUE(cache.BlockUntilTreeIsCached()); |
| 149 |
| 150 auto window = cache.GetWindow(window_xid); |
| 151 EXPECT_TRUE(window); |
| 152 EXPECT_EQ(window->children.size(), 0U); |
| 153 EXPECT_EQ(window->properties.size(), 1U); |
| 154 |
| 155 auto prop = window->GetProperty(testing_atom_); |
| 156 EXPECT_TRUE(prop); |
| 157 EXPECT_TRUE(prop->cached_property); |
| 158 EXPECT_EQ(prop->type, XA_CARDINAL); |
| 159 EXPECT_EQ(prop->data_format, 8); |
| 160 EXPECT_EQ(prop->data_length, 1); |
| 161 EXPECT_EQ(prop->data.bits_8[0], 0x42); |
| 162 |
| 163 SetProperty8(window_xid, testing_atom_, 0x24); |
| 164 EXPECT_TRUE(cache.Synchronize()); |
| 165 EXPECT_TRUE(cache.BlockUntilTreeIsCached()); |
| 166 |
| 167 prop = window->GetProperty(testing_atom_); |
| 168 EXPECT_TRUE(prop); |
| 169 EXPECT_TRUE(prop->cached_property); |
| 170 EXPECT_EQ(prop->type, XA_CARDINAL); |
| 171 EXPECT_EQ(prop->data_format, 8); |
| 172 EXPECT_EQ(prop->data_length, 1); |
| 173 EXPECT_EQ(prop->data.bits_8[0], 0x24); |
| 174 |
| 175 XDestroyWindow(display_, window_xid); |
| 176 } |
| 177 |
| 178 TEST_F(XWindowCacheTest, MapNotifyUnmapNotifyTest) { |
| 179 XID window_xid = CreateWindow(root_); |
| 180 |
| 181 XWindowCache cache(display_, window_xid); |
| 182 EXPECT_TRUE(cache.BlockUntilTreeIsCached()); |
| 183 |
| 184 auto window = cache.GetWindow(window_xid); |
| 185 EXPECT_TRUE(window); |
| 186 EXPECT_TRUE(window->cached_attributes); |
| 187 EXPECT_FALSE(window->is_mapped); |
| 188 |
| 189 XMapWindow(display_, window_xid); |
| 190 EXPECT_TRUE(cache.Synchronize()); |
| 191 EXPECT_TRUE(window->is_mapped); |
| 192 |
| 193 XUnmapWindow(display_, window_xid); |
| 194 EXPECT_TRUE(cache.Synchronize()); |
| 195 EXPECT_FALSE(window->is_mapped); |
| 196 |
| 197 XDestroyWindow(display_, window_xid); |
| 198 } |
| 199 |
| 200 TEST_F(XWindowCacheTest, CirculateNotifyTest) { |
| 201 XID parent_xid = CreateWindow(root_); |
| 202 |
| 203 std::list<XID> children_xids; |
| 204 for (int i = 0; i < 10; i++) { |
| 205 XID child = CreateWindow(parent_xid); |
| 206 children_xids.push_front(child); |
| 207 XMapWindow(display_, child); |
| 208 } |
| 209 |
| 210 XWindowCache cache(display_, parent_xid); |
| 211 EXPECT_TRUE(cache.BlockUntilTreeIsCached()); |
| 212 VerifyStackingOrder(cache.GetWindow(parent_xid), children_xids); |
| 213 |
| 214 XID child; |
| 215 XCirculateSubwindowsUp(display_, parent_xid); |
| 216 child = children_xids.back(); |
| 217 children_xids.pop_back(); |
| 218 children_xids.push_front(child); |
| 219 EXPECT_TRUE(cache.Synchronize()); |
| 220 VerifyStackingOrder(cache.GetWindow(parent_xid), children_xids); |
| 221 |
| 222 XCirculateSubwindowsDown(display_, parent_xid); |
| 223 child = children_xids.front(); |
| 224 children_xids.pop_front(); |
| 225 children_xids.push_back(child); |
| 226 EXPECT_TRUE(cache.Synchronize()); |
| 227 VerifyStackingOrder(cache.GetWindow(parent_xid), children_xids); |
| 228 |
| 229 XDestroyWindow(display_, parent_xid); |
| 230 } |
| 231 |
| 232 TEST_F(XWindowCacheTest, ConfigureNotifyTest) { |
| 233 XID window_xid = CreateWindow(root_); |
| 234 |
| 235 XWindowCache cache(display_, window_xid); |
| 236 EXPECT_TRUE(cache.BlockUntilTreeIsCached()); |
| 237 auto window = cache.GetWindow(window_xid); |
| 238 EXPECT_TRUE(window); |
| 239 EXPECT_TRUE(window->cached_attributes); |
| 240 EXPECT_TRUE(window->cached_geometry); |
| 241 |
| 242 EXPECT_EQ(window->x, 0); |
| 243 EXPECT_EQ(window->y, 0); |
| 244 XMoveWindow(display_, window_xid, 1, 1); |
| 245 EXPECT_TRUE(cache.Synchronize()); |
| 246 EXPECT_EQ(window->x, 1); |
| 247 EXPECT_EQ(window->y, 1); |
| 248 |
| 249 EXPECT_EQ(window->width, 1U); |
| 250 EXPECT_EQ(window->height, 1U); |
| 251 XResizeWindow(display_, window_xid, 2, 2); |
| 252 EXPECT_TRUE(cache.Synchronize()); |
| 253 EXPECT_EQ(window->width, 2U); |
| 254 EXPECT_EQ(window->height, 2U); |
| 255 |
| 256 EXPECT_EQ(window->border_width, 0U); |
| 257 XSetWindowBorderWidth(display_, window_xid, 1); |
| 258 EXPECT_TRUE(cache.Synchronize()); |
| 259 EXPECT_EQ(window->border_width, 1U); |
| 260 |
| 261 XDestroyWindow(display_, window_xid); |
| 262 } |
| 263 |
| 264 TEST_F(XWindowCacheTest, StackingOrderTest) { |
| 265 XID parent_xid = CreateWindow(root_); |
| 266 |
| 267 std::vector<XID> children_xids; |
| 268 for (int i = 0; i < 10; i++) { |
| 269 XID child = CreateWindow(parent_xid); |
| 270 children_xids.push_back(child); |
| 271 XMapWindow(display_, child); |
| 272 } |
| 273 std::reverse(children_xids.begin(), children_xids.end()); |
| 274 |
| 275 XWindowCache cache(display_, parent_xid); |
| 276 EXPECT_TRUE(cache.BlockUntilTreeIsCached()); |
| 277 VerifyStackingOrder(cache.GetWindow(parent_xid), children_xids); |
| 278 |
| 279 // XRaiseWindow |
| 280 // Raise window 5 |
| 281 XID child = children_xids[5]; |
| 282 XRaiseWindow(display_, child); |
| 283 children_xids.erase(children_xids.begin() + 5); |
| 284 children_xids.insert(children_xids.begin(), child); |
| 285 EXPECT_TRUE(cache.Synchronize()); |
| 286 VerifyStackingOrder(cache.GetWindow(parent_xid), children_xids); |
| 287 |
| 288 // XLowerWindow |
| 289 // Lower window 5 |
| 290 child = children_xids[5]; |
| 291 XLowerWindow(display_, child); |
| 292 children_xids.erase(children_xids.begin() + 5); |
| 293 children_xids.insert(children_xids.end(), child); |
| 294 EXPECT_TRUE(cache.Synchronize()); |
| 295 VerifyStackingOrder(cache.GetWindow(parent_xid), children_xids); |
| 296 |
| 297 // XRestackWindows |
| 298 // Stack window 2 below window 6 |
| 299 XID restack_windows[2] = {children_xids[6], children_xids[2]}; |
| 300 child = children_xids[2]; |
| 301 children_xids.erase(children_xids.begin() + 2); |
| 302 children_xids.insert(children_xids.begin() + 6, child); |
| 303 XRestackWindows(display_, restack_windows, 2); |
| 304 EXPECT_TRUE(cache.Synchronize()); |
| 305 VerifyStackingOrder(cache.GetWindow(parent_xid), children_xids); |
| 306 |
| 307 // XConfigureWindow |
| 308 // Stack window 2 below window 6 |
| 309 XWindowChanges changes; |
| 310 child = children_xids[2]; |
| 311 changes.sibling = children_xids[6]; |
| 312 changes.stack_mode = Below; |
| 313 children_xids.erase(children_xids.begin() + 2); |
| 314 children_xids.insert(children_xids.begin() + 6, child); |
| 315 XConfigureWindow(display_, child, CWSibling | CWStackMode, &changes); |
| 316 EXPECT_TRUE(cache.Synchronize()); |
| 317 VerifyStackingOrder(cache.GetWindow(parent_xid), children_xids); |
| 318 |
| 319 XDestroyWindow(display_, parent_xid); |
| 320 } |
| 321 |
| 322 TEST_F(XWindowCacheTest, GravityNotifyTest) { |
| 323 XID parent_xid = CreateWindow(root_); |
| 324 XID child_xid = CreateWindow(parent_xid); |
| 325 |
| 326 XSetWindowAttributes swa; |
| 327 swa.bit_gravity = ForgetGravity; |
| 328 swa.win_gravity = NorthEastGravity; |
| 329 XChangeWindowAttributes(display_, child_xid, CWBitGravity | CWWinGravity, |
| 330 &swa); |
| 331 |
| 332 XResizeWindow(display_, parent_xid, 100, 100); |
| 333 XResizeWindow(display_, child_xid, 25, 25); |
| 334 XMoveWindow(display_, child_xid, 75, 0); |
| 335 |
| 336 XWindowCache cache(display_, parent_xid); |
| 337 EXPECT_TRUE(cache.BlockUntilTreeIsCached()); |
| 338 |
| 339 auto parent = cache.GetWindow(parent_xid); |
| 340 EXPECT_TRUE(parent); |
| 341 EXPECT_EQ(parent->x, 0); |
| 342 EXPECT_EQ(parent->y, 0); |
| 343 EXPECT_EQ(parent->width, 100U); |
| 344 EXPECT_EQ(parent->height, 100U); |
| 345 auto child = cache.GetWindow(child_xid); |
| 346 EXPECT_TRUE(child); |
| 347 EXPECT_EQ(child->x, 75); |
| 348 EXPECT_EQ(child->y, 0); |
| 349 |
| 350 XResizeWindow(display_, parent_xid, 50, 50); |
| 351 EXPECT_TRUE(cache.Synchronize()); |
| 352 |
| 353 EXPECT_EQ(child->x, 25); |
| 354 EXPECT_EQ(child->y, 0); |
| 355 |
| 356 XDestroyWindow(display_, parent_xid); |
| 357 } |
| 358 |
| 359 TEST_F(XWindowCacheTest, ReparentNotifyTest) { |
| 360 // Start with this tree: |
| 361 // |
| 362 // child1 -- grandchild |
| 363 // / |
| 364 // parent |
| 365 // \ |
| 366 // child2 |
| 367 XID parent_xid = CreateWindow(root_); |
| 368 XID child1_xid = CreateWindow(parent_xid); |
| 369 XID child2_xid = CreateWindow(parent_xid); |
| 370 XID grandchild_xid = CreateWindow(child1_xid); |
| 371 |
| 372 XWindowCache cache(display_, parent_xid); |
| 373 EXPECT_TRUE(cache.BlockUntilTreeIsCached()); |
| 374 auto parent = cache.GetWindow(parent_xid); |
| 375 auto child1 = cache.GetWindow(child1_xid); |
| 376 auto child2 = cache.GetWindow(child2_xid); |
| 377 auto grandchild = cache.GetWindow(grandchild_xid); |
| 378 |
| 379 EXPECT_TRUE(parent); |
| 380 EXPECT_TRUE(child1); |
| 381 EXPECT_TRUE(child2); |
| 382 EXPECT_TRUE(grandchild); |
| 383 EXPECT_EQ(parent->children.size(), 2U); |
| 384 EXPECT_EQ(child1->children.size(), 1U); |
| 385 EXPECT_EQ(child2->children.size(), 0U); |
| 386 EXPECT_EQ(grandchild->children.size(), 0U); |
| 387 EXPECT_EQ(child1->children.front(), grandchild); |
| 388 EXPECT_EQ(grandchild->parent, child1); |
| 389 |
| 390 // Reparent grandchild so the tree now looks like this: |
| 391 // |
| 392 // child1 |
| 393 // / |
| 394 // parent |
| 395 // \ |
| 396 // child2 -- grandchild |
| 397 XReparentWindow(display_, grandchild_xid, child2_xid, 0, 0); |
| 398 EXPECT_TRUE(cache.Synchronize()); |
| 399 |
| 400 EXPECT_EQ(parent->children.size(), 2U); |
| 401 EXPECT_EQ(child1->children.size(), 0U); |
| 402 EXPECT_EQ(child2->children.size(), 1U); |
| 403 EXPECT_EQ(grandchild->children.size(), 0U); |
| 404 EXPECT_EQ(child2->children.front(), grandchild); |
| 405 EXPECT_EQ(grandchild->parent, child2); |
| 406 |
| 407 XDestroyWindow(display_, parent_xid); |
| 408 } |
| 409 |
| 410 } // namespace ui |
| OLD | NEW |