blob: 44d5705aeddc710f14991f7a78f98caaa1df02c7 [file] [log] [blame]
# Copyright (c) 2011 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""DownloadInfo: python representation for downloads visible to Chrome.
Obtain one of these from PyUITestSuite::GetDownloadsInfo() call.
class MyDownloadsTest(pyauto.PyUITest):
def testDownload(self):
info = self.GetDownloadsInfo()
print info.Downloads()
self.assertEqual(info.Downloads()[0]['file_name'], '')
See more tests in chrome/test/functional/
import os
import simplejson as json
import sys
from pyauto_errors import JSONInterfaceError
class DownloadInfo(object):
"""Represent info about Downloads.
The info is represented as a list of DownloadItems. Each DownloadItem is a
dictionary with various attributes about a download, like id, file_name,
path, state, and so on.
def __init__(self, downloads_dict):
"""Initialize a DownloadInfo from a string of json.
downloads_dict: a dict returned by the IPC command 'GetDownloadsInfo'.
A typical dict representing one download looks like:
{'downloads': [{'url': 'http://blah/',
'file_name': '',
'state': 'COMPLETED',
..., } ] }
pyauto_errors.JSONInterfaceError if the automation call returns an error.
# JSON string prepared in GetDownloadsInfo() in
self.downloadsdict = downloads_dict
if self.downloadsdict.has_key('error'):
raise JSONInterfaceError(self.downloadsdict['error'])
def Downloads(self):
"""Info about all downloads.
This includes downloads in all states (COMPLETE, IN_PROGRESS, ...).
[downloaditem1, downloaditem2, ...]
return self.downloadsdict.get('downloads', [])
def DownloadsInProgress(self):
"""Info about all downloads in progress.
[downloaditem1, downloaditem2, ...]
return [x for x in self.Downloads() if x['state'] == 'IN_PROGRESS']
def DownloadsComplete(self):
"""Info about all downloads that have completed.
[downloaditem1, downloaditem2, ...]
return [x for x in self.Downloads() if x['state'] == 'COMPLETE']