| 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 filecmp | 6 import filecmp |
| 7 import logging | 7 import logging |
| 8 import os | 8 import os |
| 9 import shutil | 9 import shutil |
| 10 import sys | 10 import sys |
| 11 import tempfile | 11 import tempfile |
| 12 import time | 12 import time |
| 13 import urllib | 13 import urllib |
| 14 | 14 |
| 15 import pyauto_functional # Must be imported before pyauto | 15 import pyauto_functional # Must be imported before pyauto |
| 16 import pyauto | 16 import pyauto |
| 17 | 17 |
| 18 | 18 |
| 19 class DownloadsTest(pyauto.PyUITest): | 19 class DownloadsTest(pyauto.PyUITest): |
| 20 """TestCase for Downloads.""" | 20 """TestCase for Downloads.""" |
| 21 | 21 |
| 22 def _GetDangerousDownload(self): |
| 23 """Returns the file url for a dangerous download for this OS.""" |
| 24 sub_path = os.path.join(self.DataDir(), 'downloads', 'dangerous') |
| 25 if self.IsMac(): |
| 26 return os.path.join(sub_path, 'dangerous.dmg') |
| 27 return os.path.join(sub_path, 'dangerous.exe') |
| 28 |
| 22 def _EqualFileContents(self, file1, file2): | 29 def _EqualFileContents(self, file1, file2): |
| 23 """Determine if 2 given files have the same contents.""" | 30 """Determine if 2 given files have the same contents.""" |
| 24 if not (os.path.exists(file1) and os.path.exists(file2)): | 31 if not (os.path.exists(file1) and os.path.exists(file2)): |
| 25 return False | 32 return False |
| 26 return filecmp.cmp(file1, file2, shallow=False) | 33 return filecmp.cmp(file1, file2, shallow=False) |
| 27 | 34 |
| 28 def _GetDownloadId(self, download_index=0): | 35 def _GetDownloadId(self, download_index=0): |
| 29 """Return the download id for the download at the given index. | 36 """Return the download id for the download at the given index. |
| 30 | 37 |
| 31 Args: | 38 Args: |
| (...skipping 28 matching lines...) Expand all Loading... |
| 60 | 67 |
| 61 # Verify that the download shelf is visible | 68 # Verify that the download shelf is visible |
| 62 self.assertTrue(self.IsDownloadShelfVisible()) | 69 self.assertTrue(self.IsDownloadShelfVisible()) |
| 63 | 70 |
| 64 # Verify that the file was correctly downloaded | 71 # Verify that the file was correctly downloaded |
| 65 self.assertTrue(os.path.exists(downloaded_pkg)) | 72 self.assertTrue(os.path.exists(downloaded_pkg)) |
| 66 self.assertTrue(self._EqualFileContents(file_path, downloaded_pkg)) | 73 self.assertTrue(self._EqualFileContents(file_path, downloaded_pkg)) |
| 67 | 74 |
| 68 def testDownloadDangerousFiles(self): | 75 def testDownloadDangerousFiles(self): |
| 69 """Verify that we can download and save dangerous files.""" | 76 """Verify that we can download and save dangerous files.""" |
| 70 test_dir = os.path.abspath('.') | 77 file_path = self._GetDangerousDownload() |
| 71 # This file is a .py file which is "dangerous" | |
| 72 file_path = os.path.join(test_dir, 'downloads.py') | |
| 73 file_url = self.GetFileURLForPath(file_path) | 78 file_url = self.GetFileURLForPath(file_path) |
| 74 downloaded_pkg = os.path.join(self.GetDownloadDirectory().value(), | 79 downloaded_pkg = os.path.join(self.GetDownloadDirectory().value(), |
| 75 'downloads.py') | 80 os.path.basename(file_path)) |
| 76 os.path.exists(downloaded_pkg) and os.remove(downloaded_pkg) | 81 os.path.exists(downloaded_pkg) and os.remove(downloaded_pkg) |
| 77 | 82 |
| 78 self.DownloadAndWaitForStart(file_url) | 83 self.DownloadAndWaitForStart(file_url) |
| 79 self.PerformActionOnDownload(self._GetDownloadId(), | 84 self.PerformActionOnDownload(self._GetDownloadId(), |
| 80 'save_dangerous_download') | 85 'save_dangerous_download') |
| 81 self.WaitForDownloadToComplete(downloaded_pkg) | 86 self.WaitForDownloadToComplete(downloaded_pkg) |
| 82 | 87 |
| 83 # Verify that the file was downloaded. | 88 # Verify that the file was downloaded. |
| 84 self.assertTrue(os.path.exists(downloaded_pkg)) | 89 self.assertTrue(os.path.exists(downloaded_pkg)) |
| 85 self.assertTrue(self._EqualFileContents(file_path, downloaded_pkg)) | 90 self.assertTrue(self._EqualFileContents(file_path, downloaded_pkg)) |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 'Download shelf persisted browser restart.') | 242 'Download shelf persisted browser restart.') |
| 238 # Verify that the download history persists. | 243 # Verify that the download history persists. |
| 239 downloads = self.GetDownloadsInfo().Downloads() | 244 downloads = self.GetDownloadsInfo().Downloads() |
| 240 self.assertEqual(1, len(downloads)) | 245 self.assertEqual(1, len(downloads)) |
| 241 self.assertEqual('a_zip_file.zip', downloads[0]['file_name']) | 246 self.assertEqual('a_zip_file.zip', downloads[0]['file_name']) |
| 242 self.assertEqual(file_url, downloads[0]['url']) | 247 self.assertEqual(file_url, downloads[0]['url']) |
| 243 | 248 |
| 244 | 249 |
| 245 if __name__ == '__main__': | 250 if __name__ == '__main__': |
| 246 pyauto_functional.Main() | 251 pyauto_functional.Main() |
| OLD | NEW |