Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(30)

Side by Side Diff: build/android/pylib/device/shared_prefs_test.py

Issue 2101243005: Add a snapshot of flutter/engine/src/build to our sdk (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: add README.dart Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « build/android/pylib/device/shared_prefs.py ('k') | build/android/pylib/device_settings.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
5
6 """
7 Unit tests for the contents of shared_prefs.py (mostly SharedPrefs).
8 """
9
10 import logging
11 import os
12 import sys
13 import unittest
14
15 from pylib import constants
16 from pylib.device import device_utils
17 from pylib.device import shared_prefs
18
19 sys.path.append(os.path.join(
20 constants.DIR_SOURCE_ROOT, 'third_party', 'pymock'))
21 import mock
22
23
24 def MockDeviceWithFiles(files=None):
25 if files is None:
26 files = {}
27
28 def file_exists(path):
29 return path in files
30
31 def write_file(path, contents, **_kwargs):
32 files[path] = contents
33
34 def read_file(path, **_kwargs):
35 return files[path]
36
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
42
43
44 class SharedPrefsTest(unittest.TestCase):
45
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'}
58
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')
72
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')
82
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)
91
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)
101
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)
119
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
129
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
135
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')
142
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
151
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!
160
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)
166
167 if __name__ == '__main__':
168 logging.getLogger().setLevel(logging.DEBUG)
169 unittest.main(verbosity=2)
OLDNEW
« no previous file with comments | « build/android/pylib/device/shared_prefs.py ('k') | build/android/pylib/device_settings.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698