OLD | NEW |
---|---|
1 # Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import contextlib | 5 import contextlib |
6 import logging | |
6 import os | 7 import os |
7 import sys | 8 import sys |
8 import time | 9 import time |
9 | 10 |
10 _SRC_DIR = os.path.abspath(os.path.join( | 11 _SRC_DIR = os.path.abspath(os.path.join( |
11 os.path.dirname(__file__), '..', '..', '..')) | 12 os.path.dirname(__file__), '..', '..', '..')) |
12 | 13 |
13 sys.path.append(os.path.join(_SRC_DIR, 'third_party', 'catapult', 'devil')) | 14 sys.path.append(os.path.join(_SRC_DIR, 'third_party', 'catapult', 'devil')) |
15 from devil.android import device_utils | |
14 from devil.android.sdk import intent | 16 from devil.android.sdk import intent |
15 | 17 |
16 sys.path.append(os.path.join(_SRC_DIR, 'build', 'android')) | 18 sys.path.append(os.path.join(_SRC_DIR, 'build', 'android')) |
17 from pylib import constants | 19 from pylib import constants |
18 from pylib import flag_changer | 20 from pylib import flag_changer |
19 | 21 |
20 import devtools_monitor | 22 import devtools_monitor |
21 | 23 |
22 DEVTOOLS_PORT = 9222 | 24 DEVTOOLS_PORT = 9222 |
23 DEVTOOLS_HOSTNAME = 'localhost' | 25 DEVTOOLS_HOSTNAME = 'localhost' |
24 DEFAULT_CHROME_PACKAGE = 'chrome' | 26 DEFAULT_CHROME_PACKAGE = 'chrome' |
25 | 27 |
28 | |
29 class DeviceSetupException(Exception): | |
30 def __init__(self, msg): | |
31 super(DeviceSetupException, self).__init__(msg) | |
32 logging.error(msg) | |
33 | |
34 | |
35 def GetFirstDevice(): | |
36 """Returns the first connected device. | |
37 | |
38 Raises: | |
39 DeviceSetupException if there is no such device. | |
40 """ | |
41 devices = device_utils.DeviceUtils.HealthyDevices() | |
42 if not devices: | |
43 raise DeviceSetupException('No devices found') | |
44 return devices[0] | |
45 | |
46 | |
26 @contextlib.contextmanager | 47 @contextlib.contextmanager |
27 def FlagReplacer(device, command_line_path, new_flags): | 48 def FlagReplacer(device, command_line_path, new_flags): |
28 """Replaces chrome flags in a context, restores them afterwards. | 49 """Replaces chrome flags in a context, restores them afterwards. |
29 | 50 |
30 Args: | 51 Args: |
31 device: Device to target, from DeviceUtils. Can be None, in which case this | 52 device: Device to target, from DeviceUtils. Can be None, in which case this |
32 context manager is a no-op. | 53 context manager is a no-op. |
33 command_line_path: Full path to the command-line file. | 54 command_line_path: Full path to the command-line file. |
34 new_flags: Flags to replace. | 55 new_flags: Flags to replace. |
35 """ | 56 """ |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
76 | 97 |
77 Sets up and restores any device and tracing appropriately | 98 Sets up and restores any device and tracing appropriately |
78 | 99 |
79 Args: | 100 Args: |
80 device: Android device, or None for a local run (in which case chrome needs | 101 device: Android device, or None for a local run (in which case chrome needs |
81 to have been started with --remote-debugging-port=XXX). | 102 to have been started with --remote-debugging-port=XXX). |
82 package: the key for chrome package info. | 103 package: the key for chrome package info. |
83 | 104 |
84 Returns: | 105 Returns: |
85 A context manager type which evaluates to a DevToolsConnection. | 106 A context manager type which evaluates to a DevToolsConnection. |
107 | |
Benoit L
2016/01/21 14:14:38
nit: no blank line here.
mattcary
2016/01/21 16:11:35
Done.
| |
86 """ | 108 """ |
87 package_info = constants.PACKAGE_INFO[package] | 109 package_info = constants.PACKAGE_INFO[package] |
88 command_line_path = '/data/local/chrome-command-line' | 110 command_line_path = '/data/local/chrome-command-line' |
89 new_flags = ['--enable-test-events', | 111 new_flags = ['--enable-test-events', |
90 '--remote-debugging-port=%d' % port] | 112 '--remote-debugging-port=%d' % port] |
91 if device: | 113 if device: |
92 _SetUpDevice(device, package_info) | 114 _SetUpDevice(device, package_info) |
93 with FlagReplacer(device, command_line_path, new_flags): | 115 with FlagReplacer(device, command_line_path, new_flags): |
94 if device: | 116 if device: |
95 start_intent = intent.Intent( | 117 start_intent = intent.Intent( |
(...skipping 17 matching lines...) Expand all Loading... | |
113 package: the key for chrome package info. | 135 package: the key for chrome package info. |
114 fn: the function to execute that launches chrome and performs the | 136 fn: the function to execute that launches chrome and performs the |
115 appropriate instrumentation. The function will receive a | 137 appropriate instrumentation. The function will receive a |
116 DevToolsConnection as its sole parameter. | 138 DevToolsConnection as its sole parameter. |
117 | 139 |
118 Returns: | 140 Returns: |
119 As fn() returns. | 141 As fn() returns. |
120 """ | 142 """ |
121 with DeviceConnection(device, package) as connection: | 143 with DeviceConnection(device, package) as connection: |
122 return fn(connection) | 144 return fn(connection) |
OLD | NEW |