OLD | NEW |
1 #!/usr/bin/env python | |
2 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
3 # 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 |
4 # found in the LICENSE file. | 3 # found in the LICENSE file. |
5 | 4 |
6 """Client side of the mock adb (i.e. the one called instead of the actual adb). | 5 """Holds the constants for pretty printing actions.xml.""" |
7 | 6 |
8 This file is meant to be put in front of the PATH during integration tests, in | |
9 order to route all the adb calls here and serve them using a pre-configured | |
10 dictionary (epxected commands -> planned responses). | |
11 mock_adb.py is the counterpart of this file, and is meant to be used in the | |
12 unittests for configuring the behavior (i.e. the dictionary) of this script. | |
13 """ | |
14 | |
15 import json | |
16 import optparse | |
17 import os | 7 import os |
18 import sys | 8 import sys |
19 | 9 |
| 10 # Import the metrics/common module for pretty print xml. |
| 11 sys.path.append(os.path.join(os.path.dirname(__file__), '..', 'common')) |
| 12 import pretty_print_xml |
20 | 13 |
21 def main(argv): | 14 # Desired order for tag and tag attributes. |
22 # Load the dictionary of expected_cmd -> planned_response from the json file | 15 # { tag_name: [attribute_name, ...] } |
23 # which mock_adb.py creates. | 16 ATTRIBUTE_ORDER = { |
24 with open(os.environ['MOCK_ADB_CFG']) as f: | 17 'action': ['name'], |
25 responses = json.load(f) | 18 'owner': [], |
| 19 'description': [], |
| 20 'obsolete': [], |
| 21 } |
26 | 22 |
27 # Swallow the irrelevant adb extra arguments (e.g., device id). | 23 # Tag names for top-level nodes whose children we don't want to indent. |
28 parser = optparse.OptionParser() | 24 TAGS_THAT_DONT_INDENT = ['actions'] |
29 parser.add_option('-s') | |
30 options, args = parser.parse_args(argv[1:]) | |
31 adb_args = ' '.join(args) | |
32 | 25 |
33 response = '' | 26 # Extra vertical spacing rules for special tag names. |
34 for (cmd, planned_response) in responses.iteritems(): | 27 # {tag_name: (newlines_after_open, newlines_before_close, newlines_after_close)} |
35 if adb_args.startswith(cmd): | 28 TAGS_THAT_HAVE_EXTRA_NEWLINE = { |
36 response = planned_response | 29 'actions': (2, 1, 1), |
37 print response | 30 'action': (1, 1, 1), |
| 31 } |
38 | 32 |
39 if __name__ == '__main__': | 33 # Tags that we allow to be squished into a single line for brevity. |
40 main(sys.argv) | 34 TAGS_THAT_ALLOW_SINGLE_LINE = ['owner', 'description', 'obsolete'] |
| 35 |
| 36 def GetPrintStyle(): |
| 37 """Returns an XmlStyle object for pretty printing actions.""" |
| 38 return pretty_print_xml.XmlStyle(ATTRIBUTE_ORDER, |
| 39 TAGS_THAT_HAVE_EXTRA_NEWLINE, |
| 40 TAGS_THAT_DONT_INDENT, |
| 41 TAGS_THAT_ALLOW_SINGLE_LINE) |
OLD | NEW |