Add Apps.AppListSearchQueryLength UMA histogram.
[chromium-blink-merge.git] / build / android / pylib / device / shared_prefs_test.py
blobc5f0ec3cc71b754fda4c79265bc0e7f402456ef7
1 #!/usr/bin/env python
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.
6 """
7 Unit tests for the contents of shared_prefs.py (mostly SharedPrefs).
8 """
10 import logging
11 import os
12 import sys
13 import unittest
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'))
21 import mock
24 def MockDeviceWithFiles(files=None):
25 if files is None:
26 files = {}
28 def file_exists(path):
29 return path in files
31 def write_file(path, contents, **_kwargs):
32 files[path] = contents
34 def read_file(path, **_kwargs):
35 return files[path]
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)
41 return device
44 class SharedPrefsTest(unittest.TestCase):
46 def setUp(self):
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"
50 '<map>\n'
51 ' <int name="databaseVersion" value="107" />\n'
52 ' <boolean name="featureEnabled" value="false" />\n'
53 ' <string name="someHashValue">249b3e5af13d4db2</string>\n'
54 '</map>'})
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')
83 def testLoad(self):
84 prefs = shared_prefs.SharedPrefs(
85 self.device, 'com.some.package', 'prefs.xml')
86 self.assertEquals(len(prefs), 0) # collection is empty before loading
87 prefs.Load()
88 self.assertEquals(len(prefs), len(self.expected_data))
89 self.assertEquals(prefs.AsDict(), self.expected_data)
90 self.assertFalse(prefs.changed)
92 def testClear(self):
93 prefs = shared_prefs.SharedPrefs(
94 self.device, 'com.some.package', 'prefs.xml')
95 prefs.Load()
96 self.assertEquals(prefs.AsDict(), self.expected_data)
97 self.assertFalse(prefs.changed)
98 prefs.Clear()
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
106 prefs.Load()
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)
114 prefs.Commit()
115 self.assertTrue(self.device.FileExists(prefs.path)) # should exist now
116 self.device.KillAll.assert_called_once_with(prefs.package, as_root=True,
117 quiet=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
123 prefs.Load()
124 self.assertEquals(prefs.AsDict(), {
125 'magicNumber': 42,
126 'myMetric': 3.14,
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:
146 # changes persisted
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)