OLD | NEW |
1 #!/usr/bin/python | 1 #!/usr/bin/python |
2 | 2 |
3 # Copyright (c) 2010 The Chromium Authors. All rights reserved. | 3 # Copyright (c) 2010 The Chromium Authors. All rights reserved. |
4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
6 | 6 |
| 7 import copy |
7 import os | 8 import os |
| 9 import types |
8 | 10 |
9 import pyauto_functional | 11 import pyauto_functional |
10 import pyauto_utils | 12 import pyauto_utils |
11 | 13 |
12 | 14 |
13 """Commonly used functions for PyAuto tests.""" | 15 """Commonly used functions for PyAuto tests.""" |
14 | 16 |
15 def DownloadFileFromDownloadsDataDir(test, file_name): | 17 def DownloadFileFromDownloadsDataDir(test, file_name): |
16 """Download a file from downloads data directory. | 18 """Download a file from downloads data directory. |
17 | 19 |
(...skipping 17 matching lines...) Expand all Loading... |
35 """Delete a file from the downloads directory. | 37 """Delete a file from the downloads directory. |
36 | 38 |
37 Arg: | 39 Arg: |
38 test: derived from pyauto.PyUITest - base class for UI test cases | 40 test: derived from pyauto.PyUITest - base class for UI test cases |
39 file_name: name of file to remove | 41 file_name: name of file to remove |
40 """ | 42 """ |
41 downloaded_pkg = os.path.join(test.GetDownloadDirectory().value(), | 43 downloaded_pkg = os.path.join(test.GetDownloadDirectory().value(), |
42 file_name) | 44 file_name) |
43 pyauto_utils.RemovePath(downloaded_pkg) | 45 pyauto_utils.RemovePath(downloaded_pkg) |
44 pyauto_utils.RemovePath(downloaded_pkg + '.crdownload') | 46 pyauto_utils.RemovePath(downloaded_pkg + '.crdownload') |
| 47 |
| 48 |
| 49 def StripUnmatchedKeys(item_to_strip, reference_item): |
| 50 """Returns a copy of 'item_to_strip' where unmatched key-value pairs in |
| 51 every dictionary are removed. |
| 52 |
| 53 This will examine each dictionary in 'item_to_strip' recursively, and will |
| 54 remove keys that are not found in the corresponding dictionary in |
| 55 'reference_item'. This is useful for testing equality of a subset of data. |
| 56 |
| 57 Items may contain dictionaries, lists, or primitives, but only corresponding |
| 58 dictionaries will be stripped. A corresponding entry is one which is found |
| 59 in the same index in the corresponding parent array or at the same key in the |
| 60 corresponding parent dictionary. |
| 61 |
| 62 Arg: |
| 63 item_to_strip: item to copy and remove all unmatched key-value pairs |
| 64 reference_item: item that serves as a reference for which keys-value pairs |
| 65 to strip from 'item_to_strip' |
| 66 |
| 67 Returns: |
| 68 a copy of 'item_to_strip' where all key-value pairs that do not have a |
| 69 matching key in 'reference_item' are removed |
| 70 |
| 71 Example: |
| 72 item_to_strip = {'tabs': 3, |
| 73 'time': 5908} |
| 74 reference_item = {'tabs': 2} |
| 75 StripUnmatchedKeys(item_to_strip, reference_item) will return {'tabs': 3} |
| 76 """ |
| 77 def StripList(list1, list2): |
| 78 return_list = copy.deepcopy(list2) |
| 79 for i in range(min(len(list1), len(list2))): |
| 80 return_list[i] = StripUnmatchedKeys(list1[i], list2[i]) |
| 81 return return_list |
| 82 |
| 83 def StripDict(dict1, dict2): |
| 84 return_dict = {} |
| 85 for key in dict1: |
| 86 if key in dict2: |
| 87 return_dict[key] = StripUnmatchedKeys(dict1[key], dict2[key]) |
| 88 return return_dict |
| 89 |
| 90 item_to_strip_type = type(item_to_strip) |
| 91 if item_to_strip_type is type(reference_item): |
| 92 if item_to_strip_type is types.ListType: |
| 93 return StripList(item_to_strip, reference_item) |
| 94 elif item_to_strip_type is types.DictType: |
| 95 return StripDict(item_to_strip, reference_item) |
| 96 return copy.deepcopy(item_to_strip) |
OLD | NEW |