Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(276)

Side by Side Diff: ui/views/test/x11_property_change_waiter.cc

Issue 264713007: Add unittests for X11TopmostWindowFinder (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2014 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/views/test/x11_property_change_waiter.h"
6
7 #include <X11/Xlib.h>
8
9 #include "base/run_loop.h"
10 #include "ui/events/platform/platform_event_source.h"
11 #include "ui/events/platform/scoped_event_dispatcher.h"
12 #include "ui/gfx/x/x11_atom_cache.h"
13
14 namespace views {
15
16 X11PropertyChangeWaiter::X11PropertyChangeWaiter(XID window,
17 const char* property)
18 : x_window_(window),
19 property_(property),
20 wait_(true),
21 old_event_mask_(0) {
22 Display* display = gfx::GetXDisplay();
23
24 // Ensure that we are listening to PropertyNotify events for |window|. This
25 // is not the case for windows which were not created by
26 // DesktopWindowTreeHostX11.
27 XWindowAttributes attributes;
28 XGetWindowAttributes(display, x_window_, &attributes);
29 old_event_mask_ = attributes.your_event_mask;
30 XSelectInput(display, x_window_, old_event_mask_ | PropertyChangeMask);
31
32 const char* kAtomsToCache[] = { property, NULL };
33 atom_cache_.reset(new ui::X11AtomCache(display, kAtomsToCache));
34
35 // Override the dispatcher so that we get events before
36 // DesktopWindowTreeHostX11 does. We must do this because
37 // DesktopWindowTreeHostX11 stops propagation.
38 dispatcher_ = ui::PlatformEventSource::GetInstance()->
39 OverrideDispatcher(this).Pass();
40 }
41
42 X11PropertyChangeWaiter::~X11PropertyChangeWaiter() {
43 XSelectInput(gfx::GetXDisplay(), x_window_, old_event_mask_);
44 }
45
46 void X11PropertyChangeWaiter::Wait() {
47 if (!wait_)
48 return;
49
50 // PropertyChangeWaiter (or one of its subclasses) may be constructed after
51 // the property change has occurred.
52 if (!ShouldKeepOnWaiting(NULL)) {
53 wait_ = false;
54 return;
55 }
56
57 base::RunLoop run_loop;
58 quit_closure_ = run_loop.QuitClosure();
59 run_loop.Run();
60
61 dispatcher_.reset();
62 }
63
64 bool X11PropertyChangeWaiter::ShouldKeepOnWaiting(
65 const ui::PlatformEvent& event) {
66 // Stop waiting once we get a property change.
67 return event != NULL;
68 }
69
70 bool X11PropertyChangeWaiter::CanDispatchEvent(const ui::PlatformEvent& event) {
71 NOTREACHED();
72 return true;
73 }
74
75 uint32_t X11PropertyChangeWaiter::DispatchEvent(
76 const ui::PlatformEvent& event) {
77 if (!wait_ ||
78 event->type != PropertyNotify ||
79 event->xproperty.window != x_window_ ||
80 event->xproperty.atom != atom_cache_->GetAtom(property_) ||
81 ShouldKeepOnWaiting(event)) {
82 return ui::POST_DISPATCH_PERFORM_DEFAULT;
83 }
84
85 wait_ = false;
86 if (!quit_closure_.is_null())
87 quit_closure_.Run();
88 return ui::POST_DISPATCH_PERFORM_DEFAULT;
89 }
90
91 } // namespace views
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698