2 # Copyright 2015 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
7 Unit tests for the contents of shared_prefs.py (mostly SharedPrefs).
15 from pylib
import constants
16 from pylib
.device
import device_utils
17 from pylib
.device
import shared_prefs
19 sys
.path
.append(os
.path
.join(
20 constants
.DIR_SOURCE_ROOT
, 'third_party', 'pymock'))
24 def MockDeviceWithFiles(files
=None):
28 def file_exists(path
):
31 def write_file(path
, contents
, **_kwargs
):
32 files
[path
] = contents
34 def read_file(path
, **_kwargs
):
37 device
= mock
.MagicMock(spec
=device_utils
.DeviceUtils
)
38 device
.FileExists
= mock
.Mock(side_effect
=file_exists
)
39 device
.WriteFile
= mock
.Mock(side_effect
=write_file
)
40 device
.ReadFile
= mock
.Mock(side_effect
=read_file
)
44 class SharedPrefsTest(unittest
.TestCase
):
47 self
.device
= MockDeviceWithFiles({
48 '/data/data/com.some.package/shared_prefs/prefs.xml':
49 "<?xml version='1.0' encoding='utf-8' standalone='yes' ?>\n"
51 ' <int name="databaseVersion" value="107" />\n'
52 ' <boolean name="featureEnabled" value="false" />\n'
53 ' <string name="someHashValue">249b3e5af13d4db2</string>\n'
55 self
.expected_data
= {'databaseVersion': 107,
56 'featureEnabled': False,
57 'someHashValue': '249b3e5af13d4db2'}
59 def testPropertyLifetime(self
):
60 prefs
= shared_prefs
.SharedPrefs(
61 self
.device
, 'com.some.package', 'prefs.xml')
62 self
.assertEquals(len(prefs
), 0) # collection is empty before loading
63 prefs
.SetInt('myValue', 444)
64 self
.assertEquals(len(prefs
), 1)
65 self
.assertEquals(prefs
.GetInt('myValue'), 444)
66 self
.assertTrue(prefs
.HasProperty('myValue'))
67 prefs
.Remove('myValue')
68 self
.assertEquals(len(prefs
), 0)
69 self
.assertFalse(prefs
.HasProperty('myValue'))
70 with self
.assertRaises(KeyError):
71 prefs
.GetInt('myValue')
73 def testPropertyType(self
):
74 prefs
= shared_prefs
.SharedPrefs(
75 self
.device
, 'com.some.package', 'prefs.xml')
76 prefs
.SetInt('myValue', 444)
77 self
.assertEquals(prefs
.PropertyType('myValue'), 'int')
78 with self
.assertRaises(TypeError):
79 prefs
.GetString('myValue')
80 with self
.assertRaises(TypeError):
81 prefs
.SetString('myValue', 'hello')
84 prefs
= shared_prefs
.SharedPrefs(
85 self
.device
, 'com.some.package', 'prefs.xml')
86 self
.assertEquals(len(prefs
), 0) # collection is empty before loading
88 self
.assertEquals(len(prefs
), len(self
.expected_data
))
89 self
.assertEquals(prefs
.AsDict(), self
.expected_data
)
90 self
.assertFalse(prefs
.changed
)
93 prefs
= shared_prefs
.SharedPrefs(
94 self
.device
, 'com.some.package', 'prefs.xml')
96 self
.assertEquals(prefs
.AsDict(), self
.expected_data
)
97 self
.assertFalse(prefs
.changed
)
99 self
.assertEquals(len(prefs
), 0) # collection is empty now
100 self
.assertTrue(prefs
.changed
)
102 def testCommit(self
):
103 prefs
= shared_prefs
.SharedPrefs(
104 self
.device
, 'com.some.package', 'other_prefs.xml')
105 self
.assertFalse(self
.device
.FileExists(prefs
.path
)) # file does not exist
107 self
.assertEquals(len(prefs
), 0) # file did not exist, collection is empty
108 prefs
.SetInt('magicNumber', 42)
109 prefs
.SetFloat('myMetric', 3.14)
110 prefs
.SetLong('bigNumner', 6000000000)
111 prefs
.SetStringSet('apps', ['gmail', 'chrome', 'music'])
112 self
.assertFalse(self
.device
.FileExists(prefs
.path
)) # still does not exist
113 self
.assertTrue(prefs
.changed
)
115 self
.assertTrue(self
.device
.FileExists(prefs
.path
)) # should exist now
116 self
.device
.KillAll
.assert_called_once_with(prefs
.package
, as_root
=True,
118 self
.assertFalse(prefs
.changed
)
120 prefs
= shared_prefs
.SharedPrefs(
121 self
.device
, 'com.some.package', 'other_prefs.xml')
122 self
.assertEquals(len(prefs
), 0) # collection is empty before loading
124 self
.assertEquals(prefs
.AsDict(), {
127 'bigNumner': 6000000000,
128 'apps': ['gmail', 'chrome', 'music']}) # data survived roundtrip
130 def testAsContextManager_onlyReads(self
):
131 with shared_prefs
.SharedPrefs(
132 self
.device
, 'com.some.package', 'prefs.xml') as prefs
:
133 self
.assertEquals(prefs
.AsDict(), self
.expected_data
) # loaded and ready
134 self
.assertEquals(self
.device
.WriteFile
.call_args_list
, []) # did not write
136 def testAsContextManager_readAndWrite(self
):
137 with shared_prefs
.SharedPrefs(
138 self
.device
, 'com.some.package', 'prefs.xml') as prefs
:
139 prefs
.SetBoolean('featureEnabled', True)
140 prefs
.Remove('someHashValue')
141 prefs
.SetString('newString', 'hello')
143 self
.assertTrue(self
.device
.WriteFile
.called
) # did write
144 with shared_prefs
.SharedPrefs(
145 self
.device
, 'com.some.package', 'prefs.xml') as prefs
:
147 self
.assertTrue(prefs
.GetBoolean('featureEnabled'))
148 self
.assertFalse(prefs
.HasProperty('someHashValue'))
149 self
.assertEquals(prefs
.GetString('newString'), 'hello')
150 self
.assertTrue(prefs
.HasProperty('databaseVersion')) # still there
152 def testAsContextManager_commitAborted(self
):
153 with self
.assertRaises(TypeError):
154 with shared_prefs
.SharedPrefs(
155 self
.device
, 'com.some.package', 'prefs.xml') as prefs
:
156 prefs
.SetBoolean('featureEnabled', True)
157 prefs
.Remove('someHashValue')
158 prefs
.SetString('newString', 'hello')
159 prefs
.SetInt('newString', 123) # oops!
161 self
.assertEquals(self
.device
.WriteFile
.call_args_list
, []) # did not write
162 with shared_prefs
.SharedPrefs(
163 self
.device
, 'com.some.package', 'prefs.xml') as prefs
:
164 # contents were not modified
165 self
.assertEquals(prefs
.AsDict(), self
.expected_data
)
167 if __name__
== '__main__':
168 logging
.getLogger().setLevel(logging
.DEBUG
)
169 unittest
.main(verbosity
=2)