Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / build / android / pylib / android_commands_unittest.py
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.
4
5 import os
6 import shutil
7 import sys
8 import unittest
9
10 sys.path.append(os.path.join(os.pardir, os.path.dirname(__file__)))
11
12 from pylib import android_commands
13
14 # pylint: disable=W0212,W0702
15
16 class TestGetFilesChanged(unittest.TestCase):
17
18   def setUp(self):
19     if not os.getenv('BUILDTYPE'):
20       os.environ['BUILDTYPE'] = 'Debug'
21
22     devices = android_commands.GetAttachedDevices()
23     self.assertGreater(len(devices), 0, 'No device attached!')
24     self.ac = android_commands.AndroidCommands(device=devices[0])
25     self.host_data_dir = os.path.realpath('test_push_data')
26     self.device_data_dir = '%s/test_push_data' % (
27         self.ac.RunShellCommand('realpath %s' %
28             self.ac.GetExternalStorage())[0])
29
30     os.mkdir(self.host_data_dir)
31     for i in xrange(1, 10):
32       with open('%s/%d.txt' % (self.host_data_dir, i), 'w') as f:
33         f.write('file #%d' % i)
34
35     self.ac.RunShellCommand('mkdir %s' % self.device_data_dir)
36
37   def testGetFilesChangedAllNeeded(self):
38     """ Tests GetFilesChanged when none of the files are on the device.
39     """
40     expected = [('%s/%d.txt' % (self.host_data_dir, i),
41                  '%s/%d.txt' % (self.device_data_dir, i))
42                 for i in xrange(1, 10)]
43     actual = self.ac.GetFilesChanged(self.host_data_dir, self.device_data_dir)
44     self.assertSequenceEqual(expected, actual)
45
46   def testGetFilesChangedSomeIdentical(self):
47     """ Tests GetFilesChanged when some of the files are on the device.
48     """
49     for i in xrange(1, 5):
50       self.ac._adb.Push('%s/%d.txt' % (self.host_data_dir, i),
51                         self.device_data_dir)
52     expected = [('%s/%d.txt' % (self.host_data_dir, i),
53                  '%s/%d.txt' % (self.device_data_dir, i))
54                 for i in xrange(5, 10)]
55     actual = self.ac.GetFilesChanged(self.host_data_dir, self.device_data_dir)
56     self.assertSequenceEqual(expected, actual)
57
58   def testGetFilesChangedAllIdentical(self):
59     """ Tests GetFilesChanged when all of the files are on the device.
60     """
61     for i in xrange(1, 10):
62       self.ac._adb.Push('%s/%d.txt' % (self.host_data_dir, i),
63                         self.device_data_dir)
64     expected = []
65     actual = self.ac.GetFilesChanged(self.host_data_dir, self.device_data_dir)
66     self.assertSequenceEqual(expected, actual)
67
68   def testGetFilesChangedRename(self):
69     """ Tests GetFilesChanged when one of the files has been renamed.
70
71         This tests both with and without the ignore_filenames flag set.
72     """
73     for i in xrange(5, 10):
74       self.ac._adb.Push('%s/%d.txt' % (self.host_data_dir, i),
75                         self.device_data_dir)
76     os.rename('%s/5.txt' % (self.host_data_dir),
77               '%s/99.txt' % (self.host_data_dir))
78
79     expected = [('%s/%d.txt' % (self.host_data_dir, i),
80                  '%s/%d.txt' % (self.device_data_dir, i))
81                 for i in xrange(1, 5)]
82     actual = self.ac.GetFilesChanged(self.host_data_dir, self.device_data_dir,
83                                      ignore_filenames=True)
84     self.assertSequenceEqual(expected, actual)
85
86     expected.append(('%s/99.txt' % self.host_data_dir,
87                      '%s/99.txt' % self.device_data_dir))
88     actual = self.ac.GetFilesChanged(self.host_data_dir, self.device_data_dir)
89     self.assertSequenceEqual(expected, actual)
90
91   def testGetFilesChangedCopy(self):
92     """ Tests GetFilesChanged when one of the files has been copied.
93
94         This tests both with and without the ignore_filenames flag set.
95     """
96     for i in xrange(5, 10):
97       self.ac._adb.Push('%s/%d.txt' % (self.host_data_dir, i),
98                         self.device_data_dir)
99     shutil.copy('%s/5.txt' % self.host_data_dir,
100                 '%s/99.txt' % self.host_data_dir)
101
102     expected = [('%s/%d.txt' % (self.host_data_dir, i),
103                  '%s/%d.txt' % (self.device_data_dir, i))
104                 for i in xrange(1, 5)]
105     actual = self.ac.GetFilesChanged(self.host_data_dir, self.device_data_dir,
106                                      ignore_filenames=True)
107     self.assertSequenceEqual(expected, actual)
108
109     expected.append(('%s/99.txt' % self.host_data_dir,
110                      '%s/99.txt' % self.device_data_dir))
111     actual = self.ac.GetFilesChanged(self.host_data_dir, self.device_data_dir)
112     self.assertSequenceEqual(expected, actual)
113
114   def testGetFilesChangedIndividual(self):
115     """ Tests GetFilesChanged when provided one file.
116     """
117     expected = [('%s/1.txt' % self.host_data_dir,
118                  '%s/1.txt' % self.device_data_dir)]
119     actual = self.ac.GetFilesChanged('%s/1.txt' % self.host_data_dir,
120                                      '%s/1.txt' % self.device_data_dir)
121     self.assertSequenceEqual(expected, actual)
122
123   def testGetFilesChangedFileToDirectory(self):
124     """ Tests GetFilesChanged when provided a file from the host and a
125         directory on the device.
126     """
127     expected = [('%s/1.txt' % self.host_data_dir,
128                  '%s' % self.device_data_dir)]
129     actual = self.ac.GetFilesChanged('%s/1.txt' % self.host_data_dir,
130                                      '%s' % self.device_data_dir)
131     self.assertSequenceEqual(expected, actual)
132
133   def tearDown(self):
134     try:
135       shutil.rmtree(self.host_data_dir)
136       self.ac.RunShellCommand('rm -rf %s' % self.device_data_dir)
137     except:
138       pass
139
140 if __name__ == '__main__':
141   unittest.main()
142