| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (c) 2010 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 import hashlib | 6 import hashlib |
| 7 import logging | 7 import logging |
| 8 import os | 8 import os |
| 9 import shutil | 9 import shutil |
| 10 import sys | 10 import sys |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 def _ComputeMD5sum(self, filename): | 22 def _ComputeMD5sum(self, filename): |
| 23 """Determine md5 checksum for the contents of |filename|.""" | 23 """Determine md5 checksum for the contents of |filename|.""" |
| 24 md5 = hashlib.md5() | 24 md5 = hashlib.md5() |
| 25 md5.update(open(filename, 'rb').read()) | 25 md5.update(open(filename, 'rb').read()) |
| 26 return md5.hexdigest() | 26 return md5.hexdigest() |
| 27 | 27 |
| 28 def testNoDownloadWaitingNeeded(self): | 28 def testNoDownloadWaitingNeeded(self): |
| 29 """Make sure "wait for downloads" returns quickly if we have none.""" | 29 """Make sure "wait for downloads" returns quickly if we have none.""" |
| 30 self.WaitForAllDownloadsToComplete() | 30 self.WaitForAllDownloadsToComplete() |
| 31 | 31 |
| 32 def _DownloadAndWaitForStart(self, file_url): | |
| 33 """Trigger download for the given url and wait for downloads to start. | |
| 34 | |
| 35 It waits for download by looking at the download info from Chrome, so | |
| 36 anything which isn't registered by the history service won't be noticed. | |
| 37 This is not thread-safe, but it's fine to call this method to start | |
| 38 downloading multiple files in parallel. That is after starting a | |
| 39 download, it's fine to start another one even if the first one hasn't | |
| 40 completed. | |
| 41 """ | |
| 42 num_downloads = len(self.GetDownloadsInfo().Downloads()) | |
| 43 self.NavigateToURL(file_url) # Trigger download. | |
| 44 # It might take a while for the download to kick in, hold on until then. | |
| 45 self.assertTrue(self.WaitUntil( | |
| 46 lambda: len(self.GetDownloadsInfo().Downloads()) == num_downloads + 1)) | |
| 47 | |
| 48 def testZip(self): | 32 def testZip(self): |
| 49 """Download a zip and verify that it downloaded correctly. | 33 """Download a zip and verify that it downloaded correctly. |
| 50 Also verify that the download shelf showed up. | 34 Also verify that the download shelf showed up. |
| 51 """ | 35 """ |
| 52 test_dir = os.path.join(os.path.abspath(self.DataDir()), 'downloads') | 36 test_dir = os.path.join(os.path.abspath(self.DataDir()), 'downloads') |
| 53 checksum_file = os.path.join(test_dir, 'a_zip_file.md5sum') | 37 checksum_file = os.path.join(test_dir, 'a_zip_file.md5sum') |
| 54 file_url = 'file://%s' % os.path.join(test_dir, 'a_zip_file.zip') | 38 file_url = 'file://%s' % os.path.join(test_dir, 'a_zip_file.zip') |
| 55 golden_md5sum = urllib.urlopen(checksum_file).read() | 39 golden_md5sum = urllib.urlopen(checksum_file).read() |
| 56 downloaded_pkg = os.path.join(self.GetDownloadDirectory().value(), | 40 downloaded_pkg = os.path.join(self.GetDownloadDirectory().value(), |
| 57 'a_zip_file.zip') | 41 'a_zip_file.zip') |
| 58 os.path.exists(downloaded_pkg) and os.remove(downloaded_pkg) | 42 os.path.exists(downloaded_pkg) and os.remove(downloaded_pkg) |
| 59 | 43 |
| 60 self._DownloadAndWaitForStart(file_url) | 44 self.DownloadAndWaitForStart(file_url) |
| 61 | 45 |
| 62 # Wait for the download to finish. | 46 # Wait for the download to finish. |
| 63 self.WaitForAllDownloadsToComplete() | 47 self.WaitForAllDownloadsToComplete() |
| 64 | 48 |
| 65 # Verify that the download shelf is visible | 49 # Verify that the download shelf is visible |
| 66 self.assertTrue(self.IsDownloadShelfVisible()) | 50 self.assertTrue(self.IsDownloadShelfVisible()) |
| 67 | 51 |
| 68 # Verify that the file was correctly downloaded | 52 # Verify that the file was correctly downloaded |
| 69 self.assertTrue(os.path.exists(downloaded_pkg)) | 53 self.assertTrue(os.path.exists(downloaded_pkg)) |
| 70 # print 'Download size is %d' % os.path.getsize(downloaded_pkg) | 54 # print 'Download size is %d' % os.path.getsize(downloaded_pkg) |
| (...skipping 17 matching lines...) Expand all Loading... |
| 88 num_times = 5 | 72 num_times = 5 |
| 89 assert num_times > 1, 'needs to be > 1 to work' | 73 assert num_times > 1, 'needs to be > 1 to work' |
| 90 renamed_files = [] | 74 renamed_files = [] |
| 91 for i in range(num_times): | 75 for i in range(num_times): |
| 92 expected_filename = os.path.join(download_dir, 'a_zip_file.zip') | 76 expected_filename = os.path.join(download_dir, 'a_zip_file.zip') |
| 93 if i > 0: # Files after first download are renamed. | 77 if i > 0: # Files after first download are renamed. |
| 94 expected_filename = os.path.join(download_dir, | 78 expected_filename = os.path.join(download_dir, |
| 95 'a_zip_file (%d).zip' % i) | 79 'a_zip_file (%d).zip' % i) |
| 96 renamed_files.append(expected_filename) | 80 renamed_files.append(expected_filename) |
| 97 os.path.exists(expected_filename) and os.remove(expected_filename) | 81 os.path.exists(expected_filename) and os.remove(expected_filename) |
| 98 self._DownloadAndWaitForStart(file_url) | 82 self.DownloadAndWaitForStart(file_url) |
| 99 | 83 |
| 100 self.WaitForAllDownloadsToComplete() | 84 self.WaitForAllDownloadsToComplete() |
| 101 | 85 |
| 102 # Verify that all files exist and have the right name | 86 # Verify that all files exist and have the right name |
| 103 for filename in renamed_files: | 87 for filename in renamed_files: |
| 104 self.assertTrue(os.path.exists(filename)) | 88 self.assertTrue(os.path.exists(filename)) |
| 105 os.path.exists(filename) and os.remove(filename) | 89 os.path.exists(filename) and os.remove(filename) |
| 106 | 90 |
| 107 def _EvalDataFrom(self, filename): | 91 def _EvalDataFrom(self, filename): |
| 108 """Return eval of python code from given file. | 92 """Return eval of python code from given file. |
| (...skipping 26 matching lines...) Expand all Loading... |
| 135 | 119 |
| 136 # Temp dir for hosting crazy filenames. | 120 # Temp dir for hosting crazy filenames. |
| 137 temp_dir = tempfile.mkdtemp(prefix='download') | 121 temp_dir = tempfile.mkdtemp(prefix='download') |
| 138 try: | 122 try: |
| 139 for filename in crazy_filenames: | 123 for filename in crazy_filenames: |
| 140 file_path = os.path.join(temp_dir, filename) | 124 file_path = os.path.join(temp_dir, filename) |
| 141 _CreateFile(file_path) | 125 _CreateFile(file_path) |
| 142 file_url = self.GetFileURLForPath(file_path) | 126 file_url = self.GetFileURLForPath(file_path) |
| 143 downloaded_file = os.path.join(download_dir, filename) | 127 downloaded_file = os.path.join(download_dir, filename) |
| 144 os.path.exists(downloaded_file) and os.remove(downloaded_file) | 128 os.path.exists(downloaded_file) and os.remove(downloaded_file) |
| 145 self._DownloadAndWaitForStart(file_url) | 129 self.DownloadAndWaitForStart(file_url) |
| 146 self.WaitForAllDownloadsToComplete() | 130 self.WaitForAllDownloadsToComplete() |
| 147 | 131 |
| 148 # Verify downloads. | 132 # Verify downloads. |
| 149 downloads = self.GetDownloadsInfo().Downloads() | 133 downloads = self.GetDownloadsInfo().Downloads() |
| 150 self.assertEqual(len(downloads), len(crazy_filenames)) | 134 self.assertEqual(len(downloads), len(crazy_filenames)) |
| 151 | 135 |
| 152 for filename in crazy_filenames: | 136 for filename in crazy_filenames: |
| 153 downloaded_file = os.path.join(download_dir, filename) | 137 downloaded_file = os.path.join(download_dir, filename) |
| 154 self.assertTrue(os.path.exists(downloaded_file)) | 138 self.assertTrue(os.path.exists(downloaded_file)) |
| 155 self.assertEqual( # Verify checksum. | 139 self.assertEqual( # Verify checksum. |
| 156 self._ComputeMD5sum(downloaded_file), | 140 self._ComputeMD5sum(downloaded_file), |
| 157 self._ComputeMD5sum(os.path.join(temp_dir, filename))) | 141 self._ComputeMD5sum(os.path.join(temp_dir, filename))) |
| 158 os.path.exists(downloaded_file) and os.remove(downloaded_file) | 142 os.path.exists(downloaded_file) and os.remove(downloaded_file) |
| 159 finally: | 143 finally: |
| 160 shutil.rmtree(temp_dir) | 144 shutil.rmtree(temp_dir) |
| 161 | 145 |
| 162 | 146 |
| 163 if __name__ == '__main__': | 147 if __name__ == '__main__': |
| 164 pyauto_functional.Main() | 148 pyauto_functional.Main() |
| OLD | NEW |