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. 4 5 """DownloadInfo: python representation for downloads visible to Chrome. 6 7 Obtain one of these from PyUITestSuite::GetDownloadsInfo() call. 8 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') 16 17 See more tests in chrome/test/functional/downloads.py. 18 """ 19 20 import os 21 import simplejson as json 22 import sys 23 24 from pyauto_errors import JSONInterfaceError 25 26 27 class DownloadInfo(object): 28 """Represent info about Downloads. 29 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. 33 """ 34 def __init__(self, downloads_dict): 35 """Initialize a DownloadInfo from a string of json. 36 37 Args: 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', 42 'state': 'COMPLETED', 43 ..., 44 ..., } ] } 45 46 Raises: 47 pyauto_errors.JSONInterfaceError if the automation call returns an error. 48 """ 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']) 53 54 def Downloads(self): 55 """Info about all downloads. 56 57 This includes downloads in all states (COMPLETE, IN_PROGRESS, ...). 58 59 Returns: 60 [downloaditem1, downloaditem2, ...] 61 """ 62 return self.downloadsdict.get('downloads', []) 63 64 def DownloadsInProgress(self): 65 """Info about all downloads in progress. 66 67 Returns: 68 [downloaditem1, downloaditem2, ...] 69 """ 70 return [x for x in self.Downloads() if x['state'] == 'IN_PROGRESS'] 71 72 def DownloadsComplete(self): 73 """Info about all downloads that have completed. 74 75 Returns: 76 [downloaditem1, downloaditem2, ...] 77 """ 78 return [x for x in self.Downloads() if x['state'] == 'COMPLETE'] 79