~brendan-donegan/checkbox/bug1085219

« back to all changes in this revision

Viewing changes to scripts/window_test

  • Committer: Tarmac
  • Author(s): Brendan Donegan
  • Date: 2012-11-28 17:36:49 UTC
  • mfrom: (1834.1.3 3d_window_move_resize)
  • Revision ID: tarmac-20121128173649-s9rp3oztdj79msli
"[r=zkrynicki][bug=][author=brendan-donegan] automatic merge by tarmac"

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
import sys
29
29
 
30
30
from signal import SIGTSTP, SIGCONT, SIGTERM
31
 
from subprocess import Popen, PIPE
 
31
from subprocess import check_call, check_output, Popen, PIPE
32
32
from argparse import ArgumentParser
33
33
 
34
34
 
35
35
class AppThread(threading.Thread):
 
36
 
36
37
    def __init__(self, app_name):
37
38
        self._appname = app_name
38
39
        self.stdout = None
56
57
    app_thread.start()
57
58
 
58
59
    # Wait until we have a pid
59
 
    while app_thread.pid == None:
 
60
    while app_thread.pid is None:
60
61
        continue
61
62
    pid = app_thread.pid
62
63
 
86
87
 
87
88
    for thread in threads:
88
89
        # Wait until we have a pid
89
 
        while thread.pid == None:
 
90
        while thread.pid is None:
90
91
            continue
91
92
 
92
93
    # Wait a bit and kill the process
111
112
    app_thread.start()
112
113
 
113
114
    # Wait until we have a pid
114
 
    while app_thread.pid == None:
 
115
    while app_thread.pid is None:
115
116
        continue
116
117
    pid = app_thread.pid
117
118
 
139
140
    return status
140
141
 
141
142
 
 
143
def move_window(app, timeout):
 
144
    status = 0
 
145
 
 
146
    # Start the process in a separate thread
 
147
    app_thread = AppThread(app)
 
148
    app_thread.start()
 
149
 
 
150
    while app_thread.pid is None:
 
151
        continue
 
152
 
 
153
    pid = app_thread.pid
 
154
 
 
155
    time.sleep(3)
 
156
 
 
157
    window_list = check_output(['wmctrl', '-l'], universal_newlines=True)
 
158
    window_id = ''
 
159
 
 
160
    for line in window_list.split('\n'):
 
161
        if app in line:
 
162
            window_id = line.split()[0]
 
163
 
 
164
    if window_id:
 
165
        # Get the screen information from GDK
 
166
        from gi.repository import Gdk
 
167
 
 
168
        screen = Gdk.Screen.get_default()
 
169
        geom = screen.get_monitor_geometry(screen.get_primary_monitor())
 
170
 
 
171
        # Find out the window information from xwininfo
 
172
        win_x = ''
 
173
        win_y = ''
 
174
        win_width = ''
 
175
        win_height = ''
 
176
 
 
177
        for line in check_output(['xwininfo', '-name', app],
 
178
                                 universal_newlines=True).split('\n'):
 
179
            if 'Absolute upper-left X' in line:
 
180
                win_x = line.split(': ')[-1].strip()
 
181
            elif 'Absolute upper-left Y' in line:
 
182
                win_y = line.split(': ')[-1].strip()
 
183
            elif 'Width' in line:
 
184
                win_width = line.split(': ')[-1].strip()
 
185
            elif 'Height' in line:
 
186
                win_height = line.split(': ')[-1].strip()
 
187
 
 
188
        move_line = ["0", win_x, win_y, win_width, win_height]
 
189
 
 
190
        directions = {'RIGHT': geom.width,
 
191
                      'DOWN': geom.height,
 
192
                      'LEFT': win_x,
 
193
                      'UP': win_y,
 
194
                      'STOP': None}
 
195
        current = 'RIGHT'
 
196
 
 
197
        while current != 'STOP':
 
198
            if current == 'RIGHT':
 
199
                # Check if top right corner of window reached top right point
 
200
                if int(move_line[1]) + int(win_width) != directions[current]:
 
201
                    new_x = int(move_line[1]) + 1
 
202
                    move_line[1] = str(new_x)
 
203
                else:
 
204
                    current = 'DOWN'
 
205
            elif current == 'DOWN':
 
206
                if int(move_line[2]) + int(win_height) != directions[current]:
 
207
                    new_y = int(move_line[2]) + 1
 
208
                    move_line[2] = str(new_y)
 
209
                else:
 
210
                    current = 'LEFT'
 
211
            elif current == 'LEFT':
 
212
                if int(move_line[1]) != int(directions[current]):
 
213
                    new_x = int(move_line[1]) - 1
 
214
                    move_line[1] = str(new_x)
 
215
                else:
 
216
                    current = 'UP'
 
217
            elif current == 'UP':
 
218
                if int(move_line[2]) != int(directions[current]):
 
219
                    new_y = int(move_line[2]) - 1
 
220
                    move_line[2] = str(new_y)
 
221
                else:
 
222
                    current = 'STOP'
 
223
 
 
224
            check_call(['wmctrl', '-i', '-r', window_id,
 
225
                        '-e', ','.join(move_line)])
 
226
 
 
227
        os.kill(pid, SIGTERM)
 
228
    else:
 
229
        print("Could not get window handle for %s" % app, file=sys.stderr)
 
230
        status = 1
 
231
 
 
232
    return status
 
233
 
 
234
 
142
235
def print_open_close(iterations, timeout, *args):
143
236
    status = 0
144
237
    print('Opening and closing a 3D window')
178
271
    return status
179
272
 
180
273
 
 
274
def print_move_window(iterations, timeout, *args):
 
275
    status = 0
 
276
    print('Moving a 3D window across the screen')
 
277
 
 
278
    for it in range(iterations):
 
279
        print('Iteration %d of %d:' % (it + 1, iterations))
 
280
        exit_status = move_window('glxgears',
 
281
                                  timeout)
 
282
 
 
283
    print('')
 
284
    return status
 
285
 
 
286
 
181
287
def main():
182
288
    tests = {'open-close': print_open_close,
183
289
             'suspend-resume': print_suspend_resume,
184
 
             'open-close-multi': print_open_close_multi}
 
290
             'open-close-multi': print_open_close_multi,
 
291
             'move': print_move_window}
185
292
 
186
 
    usage = 'Usage: %{prog}s [OPTIONS]'
187
 
    parser = ArgumentParser(usage)
 
293
    parser = ArgumentParser(description='Script that performs window operation')
188
294
    parser.add_argument('-t', '--test',
189
295
                        default='all',
190
296
                        help='The name of the test to run. \
205
311
                        default=3,
206
312
                        help='The time in seconds between each test. \
207
313
                              Default is 3')
208
 
    parser.add_argument('-w', '--windows_number',
 
314
    parser.add_argument('-w', '--windows-number',
209
315
                        type=int,
 
316
                        default=4,
210
317
                        help='The number of windows to open.')
211
318
 
212
319
    args = parser.parse_args()
213
320
 
214
 
    if args.windows_number:
215
 
        if args.test not in ('open-close-multi', 'all'):
216
 
            parser.error('-w or --windows_number can only be used with '
217
 
                         'the "open-close-multi" test or with the '
218
 
                         '"all" test')
219
 
        windows_number = args.windows_number
220
 
    else:
221
 
        windows_number = 4
222
 
 
223
321
    status = 0
224
 
    iterations = args.iterations
225
 
    timeout = args.timeout
226
 
    windows_number = args.windows_number
227
322
 
228
323
    test = tests.get(args.test)
229
324
 
230
325
    if test:
231
 
        status = test(iterations, timeout, windows_number)
 
326
        status = test(args.iterations, args.timeout, args.windows_number)
232
327
    else:
233
328
        if args.test == 'all':
234
329
            for test in tests:
235
 
                exit_status = tests[test](iterations, timeout,
236
 
                                          windows_number)
 
330
                exit_status = tests[test](args.iterations, args.timeout,
 
331
                                          args.windows_number)
237
332
                if exit_status != 0:
238
333
                    status = exit_status
239
334
        else: