OLD | NEW |
---|---|
(Empty) | |
1 # Copyright 2013 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 import ctypes | |
6 | |
7 | |
8 def VerifyWindows(windows): | |
9 """Verifies the current windows according to the expectation dictionaries. | |
10 | |
11 Args: | |
12 windows: A dictionary whose keys are titles of windows and values are | |
13 expectation dictionaries. An expectation dictionary is a dictionary with | |
14 the following key and value: | |
15 'exists' a boolean indicating whether the window should exist. | |
16 """ | |
17 titles = WindowTitles() | |
18 for window_title, expectation in windows.iteritems(): | |
19 if window_title in titles: | |
20 assert expectation['exists'], "Window '%s' exists" % window_title | |
gab
2013/08/13 17:49:27
Isn't there a way to test both cases in 1 line, so
| |
21 else: | |
22 assert not expectation['exists'], \ | |
23 "Window '%s' does not exist" % window_title | |
24 | |
25 | |
26 def WindowTitles(): | |
27 """Retutn the titles of all top-level windows.""" | |
28 titles = [] | |
29 # Create a callback function to pass to the EnumWindows function. | |
30 # The callback receives the handle to each top-level window. | |
31 def EnumerateWindowCallback(hwnd, _): | |
32 # If the window is visible, add its title to |titles|. | |
33 if ctypes.windll.user32.IsWindowVisible(hwnd): | |
34 length = ctypes.windll.user32.GetWindowTextLengthW(hwnd) | |
35 buffer = ctypes.create_unicode_buffer(length + 1) | |
36 ctypes.windll.user32.GetWindowTextW(hwnd, buffer, length + 1) | |
37 titles.append(buffer.value) | |
38 # Return True to continue enumerating windows. | |
39 return True | |
40 # Create a function prototype for the callback, which takes two pointers and | |
41 # returns a boolean. | |
42 callback_prototype = ctypes.WINFUNCTYPE(ctypes.c_bool, | |
43 ctypes.POINTER(ctypes.c_int), | |
44 ctypes.POINTER(ctypes.c_int)) | |
45 callback = callback_prototype(EnumerateWindowCallback) | |
46 # Enumerate all top-level windows. | |
47 ctypes.windll.user32.EnumWindows(callback, 0) | |
48 return titles | |
OLD | NEW |