Adding Peter Thatcher to the owners file.
[chromium-blink-merge.git] / build / android / pylib / android_commands_unittest.py
blob21c34f93663f9e6826562cdc362b84cdfee7f81a
1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 import os
6 import shutil
7 import sys
8 import unittest
10 sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir))
12 from pylib import android_commands
14 # pylint: disable=W0212,W0702
16 class TestDeviceTempFile(unittest.TestCase):
17 def setUp(self):
18 if not os.getenv('BUILDTYPE'):
19 os.environ['BUILDTYPE'] = 'Debug'
21 devices = android_commands.GetAttachedDevices()
22 self.assertGreater(len(devices), 0, 'No device attached!')
23 self.ac = android_commands.AndroidCommands(device=devices[0])
25 def testTempFileDeleted(self):
26 """Tests that DeviceTempFile deletes files when closed."""
27 temp_file = android_commands.DeviceTempFile(self.ac)
28 self.assertFalse(self.ac.FileExistsOnDevice(temp_file.name))
29 self.ac.SetFileContents(temp_file.name, "contents")
30 self.assertTrue(self.ac.FileExistsOnDevice(temp_file.name))
31 temp_file.close()
32 self.assertFalse(self.ac.FileExistsOnDevice(temp_file.name))
34 with android_commands.DeviceTempFile(self.ac) as with_temp_file:
35 self.assertFalse(self.ac.FileExistsOnDevice(with_temp_file.name))
36 self.ac.SetFileContents(with_temp_file.name, "contents")
37 self.assertTrue(self.ac.FileExistsOnDevice(with_temp_file.name))
39 self.assertFalse(self.ac.FileExistsOnDevice(with_temp_file.name))
41 def testTempFileNotWritten(self):
42 """Tests that device temp files work successfully even if not written to."""
43 temp_file = android_commands.DeviceTempFile(self.ac)
44 temp_file.close()
45 self.assertFalse(self.ac.FileExistsOnDevice(temp_file.name))
47 with android_commands.DeviceTempFile(self.ac) as with_temp_file:
48 pass
49 self.assertFalse(self.ac.FileExistsOnDevice(with_temp_file.name))
51 def testNaming(self):
52 """Tests that returned filenames are as requested."""
53 temp_file = android_commands.DeviceTempFile(self.ac, prefix="cat")
54 self.assertTrue(os.path.basename(temp_file.name).startswith("cat"))
56 temp_file = android_commands.DeviceTempFile(self.ac, suffix="dog")
57 self.assertTrue(temp_file.name.endswith("dog"))
59 temp_file = android_commands.DeviceTempFile(
60 self.ac, prefix="cat", suffix="dog")
61 self.assertTrue(os.path.basename(temp_file.name).startswith("cat"))
62 self.assertTrue(temp_file.name.endswith("dog"))
65 class TestGetFilesChanged(unittest.TestCase):
67 def setUp(self):
68 if not os.getenv('BUILDTYPE'):
69 os.environ['BUILDTYPE'] = 'Debug'
71 devices = android_commands.GetAttachedDevices()
72 self.assertGreater(len(devices), 0, 'No device attached!')
73 self.ac = android_commands.AndroidCommands(device=devices[0])
74 self.host_data_dir = os.path.realpath('test_push_data')
75 self.device_data_dir = '%s/test_push_data' % (
76 self.ac.RunShellCommand('realpath %s' %
77 self.ac.GetExternalStorage())[0])
79 os.mkdir(self.host_data_dir)
80 for i in xrange(1, 10):
81 with open('%s/%d.txt' % (self.host_data_dir, i), 'w') as f:
82 f.write('file #%d' % i)
84 self.ac.RunShellCommand('mkdir %s' % self.device_data_dir)
86 def testGetFilesChangedAllNeeded(self):
87 """ Tests GetFilesChanged when none of the files are on the device.
88 """
89 expected = [('%s/%d.txt' % (self.host_data_dir, i),
90 '%s/%d.txt' % (self.device_data_dir, i))
91 for i in xrange(1, 10)]
92 actual = self.ac.GetFilesChanged(self.host_data_dir, self.device_data_dir)
93 self.assertSequenceEqual(expected, actual)
95 def testGetFilesChangedSomeIdentical(self):
96 """ Tests GetFilesChanged when some of the files are on the device.
97 """
98 for i in xrange(1, 5):
99 self.ac._adb.Push('%s/%d.txt' % (self.host_data_dir, i),
100 self.device_data_dir)
101 expected = [('%s/%d.txt' % (self.host_data_dir, i),
102 '%s/%d.txt' % (self.device_data_dir, i))
103 for i in xrange(5, 10)]
104 actual = self.ac.GetFilesChanged(self.host_data_dir, self.device_data_dir)
105 self.assertSequenceEqual(expected, actual)
107 def testGetFilesChangedAllIdentical(self):
108 """ Tests GetFilesChanged when all of the files are on the device.
110 for i in xrange(1, 10):
111 self.ac._adb.Push('%s/%d.txt' % (self.host_data_dir, i),
112 self.device_data_dir)
113 expected = []
114 actual = self.ac.GetFilesChanged(self.host_data_dir, self.device_data_dir)
115 self.assertSequenceEqual(expected, actual)
117 def testGetFilesChangedRename(self):
118 """ Tests GetFilesChanged when one of the files has been renamed.
120 This tests both with and without the ignore_filenames flag set.
122 for i in xrange(5, 10):
123 self.ac._adb.Push('%s/%d.txt' % (self.host_data_dir, i),
124 self.device_data_dir)
125 os.rename('%s/5.txt' % (self.host_data_dir),
126 '%s/99.txt' % (self.host_data_dir))
128 expected = [('%s/%d.txt' % (self.host_data_dir, i),
129 '%s/%d.txt' % (self.device_data_dir, i))
130 for i in xrange(1, 5)]
131 actual = self.ac.GetFilesChanged(self.host_data_dir, self.device_data_dir,
132 ignore_filenames=True)
133 self.assertSequenceEqual(expected, actual)
135 expected.append(('%s/99.txt' % self.host_data_dir,
136 '%s/99.txt' % self.device_data_dir))
137 actual = self.ac.GetFilesChanged(self.host_data_dir, self.device_data_dir)
138 self.assertSequenceEqual(expected, actual)
140 def testGetFilesChangedCopy(self):
141 """ Tests GetFilesChanged when one of the files has been copied.
143 This tests both with and without the ignore_filenames flag set.
145 for i in xrange(5, 10):
146 self.ac._adb.Push('%s/%d.txt' % (self.host_data_dir, i),
147 self.device_data_dir)
148 shutil.copy('%s/5.txt' % self.host_data_dir,
149 '%s/99.txt' % self.host_data_dir)
151 expected = [('%s/%d.txt' % (self.host_data_dir, i),
152 '%s/%d.txt' % (self.device_data_dir, i))
153 for i in xrange(1, 5)]
154 actual = self.ac.GetFilesChanged(self.host_data_dir, self.device_data_dir,
155 ignore_filenames=True)
156 self.assertSequenceEqual(expected, actual)
158 expected.append(('%s/99.txt' % self.host_data_dir,
159 '%s/99.txt' % self.device_data_dir))
160 actual = self.ac.GetFilesChanged(self.host_data_dir, self.device_data_dir)
161 self.assertSequenceEqual(expected, actual)
163 def testGetFilesChangedIndividual(self):
164 """ Tests GetFilesChanged when provided one file.
166 expected = [('%s/1.txt' % self.host_data_dir,
167 '%s/1.txt' % self.device_data_dir)]
168 actual = self.ac.GetFilesChanged('%s/1.txt' % self.host_data_dir,
169 '%s/1.txt' % self.device_data_dir)
170 self.assertSequenceEqual(expected, actual)
172 def testGetFilesChangedFileToDirectory(self):
173 """ Tests GetFilesChanged when provided a file from the host and a
174 directory on the device.
176 expected = [('%s/1.txt' % self.host_data_dir,
177 '%s' % self.device_data_dir)]
178 actual = self.ac.GetFilesChanged('%s/1.txt' % self.host_data_dir,
179 '%s' % self.device_data_dir)
180 self.assertSequenceEqual(expected, actual)
182 def tearDown(self):
183 try:
184 shutil.rmtree(self.host_data_dir)
185 self.ac.RunShellCommand('rm -rf %s' % self.device_data_dir)
186 except:
187 pass
189 if __name__ == '__main__':
190 unittest.main()