1 # Copyright (c) 2011 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 """DownloadInfo: python representation for downloads visible to Chrome.
7 Obtain one of these from PyUITestSuite::GetDownloadsInfo() call.
9 class MyDownloadsTest(pyauto.PyUITest):
10 def testDownload(self):
11 self.DownloadAndWaitForStart('http://my.url/package.zip')
12 self.WaitForAllDownloadsToComplete()
13 info = self.GetDownloadsInfo()
14 print info.Downloads()
15 self.assertEqual(info.Downloads()[0]['file_name'], 'packge.zip')
17 See more tests in chrome/test/functional/downloads.py.
21 import simplejson
as json
24 from pyauto_errors
import JSONInterfaceError
27 class DownloadInfo(object):
28 """Represent info about Downloads.
30 The info is represented as a list of DownloadItems. Each DownloadItem is a
31 dictionary with various attributes about a download, like id, file_name,
32 path, state, and so on.
34 def __init__(self
, downloads_dict
):
35 """Initialize a DownloadInfo from a string of json.
38 downloads_dict: a dict returned by the IPC command 'GetDownloadsInfo'.
39 A typical dict representing one download looks like:
40 {'downloads': [{'url': 'http://blah/a_file.zip',
41 'file_name': 'a_file.zip',
47 pyauto_errors.JSONInterfaceError if the automation call returns an error.
49 # JSON string prepared in GetDownloadsInfo() in automation_provider.cc
50 self
.downloadsdict
= downloads_dict
51 if self
.downloadsdict
.has_key('error'):
52 raise JSONInterfaceError(self
.downloadsdict
['error'])
55 """Info about all downloads.
57 This includes downloads in all states (COMPLETE, IN_PROGRESS, ...).
60 [downloaditem1, downloaditem2, ...]
62 return self
.downloadsdict
.get('downloads', [])
64 def DownloadsInProgress(self
):
65 """Info about all downloads in progress.
68 [downloaditem1, downloaditem2, ...]
70 return [x
for x
in self
.Downloads() if x
['state'] == 'IN_PROGRESS']
72 def DownloadsComplete(self
):
73 """Info about all downloads that have completed.
76 [downloaditem1, downloaditem2, ...]
78 return [x
for x
in self
.Downloads() if x
['state'] == 'COMPLETE']