85b32cc4087db282b363a32310fffa96ad2b8932
[WebKit-https.git] / Tools / Scripts / webkitpy / benchmark_runner / browser_driver / osx_browser_driver.py
1
2 import logging
3 import os
4 import subprocess
5
6 from AppKit import NSRunningApplication
7 from AppKit import NSScreen
8 from Quartz.CoreGraphics import CGEventCreateMouseEvent
9 from Quartz.CoreGraphics import CGEventPost
10 from Quartz.CoreGraphics import kCGEventMouseMoved
11 from Quartz.CoreGraphics import kCGHIDEventTap
12 from Quartz.CoreGraphics import kCGMouseButtonLeft
13 from browser_driver import BrowserDriver
14
15
16 _log = logging.getLogger(__name__)
17
18
19 class OSXBrowserDriver(BrowserDriver):
20     bundleIdentifier = None
21
22     def prepareEnv(self):
23         self.closeBrowsers()
24         self.moveCursor(0, 0)
25
26     def closeBrowsers(self):
27         self.terminateProcesses(self.bundleIdentifier)
28
29     @classmethod
30     def launchProcess(cls, buildDir, appName, url, args):
31         if not buildDir:
32             buildDir = '/Applications/'
33         appPath = os.path.join(buildDir, appName)
34
35         _log.info('Launching "%s" with url "%s"' % (appPath, url))
36
37         # FIXME: May need to be modified for a local build such as setting up DYLD libraries
38         args = ['open', '-a', appPath] + args
39         cls.launchProcessWithCaffinate(args)
40
41     @classmethod
42     def terminateProcesses(cls, bundleIdentifier):
43         _log.info('Closing all terminating all processes with the bundle identifier %s' % bundleIdentifier)
44         processes = NSRunningApplication.runningApplicationsWithBundleIdentifier_(bundleIdentifier)
45         for process in processes:
46             process.terminate()
47
48     @classmethod
49     def launchProcessWithCaffinate(cls, args, env=None):
50         process = subprocess.Popen(args, env=env)
51         subprocess.Popen(["/usr/bin/caffeinate", "-disw", str(process.pid)])
52         return process
53
54     @classmethod
55     def moveCursor(cls, x, y):
56         moveEvent = CGEventCreateMouseEvent(None, kCGEventMouseMoved, (x, y), kCGMouseButtonLeft)
57         CGEventPost(kCGHIDEventTap, moveEvent)
58
59     @classmethod
60     def screenSize(cls):
61         return NSScreen.mainScreen().frame().size