Chromium Code Reviews| Index: build/android/pylib/instrumentation/instrumentation_test_instance.py |
| diff --git a/build/android/pylib/instrumentation/instrumentation_test_instance.py b/build/android/pylib/instrumentation/instrumentation_test_instance.py |
| index 2800b6f6a9b84127c8a6170125944ebaf3dbf183..3fdccfa8930a755fba4bcea406932a5d1af94d5d 100644 |
| --- a/build/android/pylib/instrumentation/instrumentation_test_instance.py |
| +++ b/build/android/pylib/instrumentation/instrumentation_test_instance.py |
| @@ -4,6 +4,7 @@ |
| import collections |
| import copy |
| +import json |
| import logging |
| import os |
| import pickle |
| @@ -500,6 +501,9 @@ class InstrumentationTestInstance(test_instance.TestInstance): |
| self._should_save_logcat = None |
| self._initializeLogAttributes(args) |
| + self._edit_shared_prefs = [] |
| + self._initializeEditPrefsAttributes(args) |
| + |
| def _initializeApkAttributes(self, args, error_func): |
| if args.apk_under_test: |
| apk_under_test_path = args.apk_under_test |
| @@ -675,6 +679,30 @@ class InstrumentationTestInstance(test_instance.TestInstance): |
| def _initializeLogAttributes(self, args): |
| self._should_save_logcat = bool(args.json_results_file) |
| + def _initializeEditPrefsAttributes(self, args): |
| + if not hasattr(args, 'shared_prefs_file'): |
| + return |
| + if not isinstance(args.shared_prefs_file, str): |
| + logging.warning("Given non-string for a filepath") |
| + return |
| + |
| + # json.load() loads strings as unicode, which causes issues when trying |
| + # to edit string values in preference files, so convert to Python strings |
| + def unicode_to_str(data): |
|
jbudorick
2017/02/15 22:38:41
Can we do this with one of the optional arguments
bsheedy
2017/02/16 00:14:39
Yes and no. Setting the encoding argument is only
|
| + if isinstance(data, dict): |
| + return {unicode_to_str(key): unicode_to_str(value) |
| + for key, value in data.iteritems()} |
| + elif isinstance(data, list): |
| + return [unicode_to_str(element) for element in data] |
| + elif isinstance(data, unicode): |
| + return data.encode('utf-8') |
| + return data |
| + |
| + filepath = os.path.abspath(os.path.join( |
|
jbudorick
2017/02/15 22:38:41
Remove this after the realpath change mentioned in
bsheedy
2017/02/16 18:04:50
Done.
|
| + host_paths.DIR_SOURCE_ROOT, args.shared_prefs_file)) |
| + with open(filepath) as prefs_file: |
| + self._edit_shared_prefs = unicode_to_str(json.load(prefs_file)) |
| + |
| @property |
| def additional_apks(self): |
| return self._additional_apks |
| @@ -704,6 +732,10 @@ class InstrumentationTestInstance(test_instance.TestInstance): |
| return self._driver_name |
| @property |
| + def edit_shared_prefs(self): |
| + return self._edit_shared_prefs |
| + |
| + @property |
| def flags(self): |
| return self._flags |