Merge lp:~mandel/ubuntu-download-manager/update-go-bindings into lp:ubuntu-download-manager

Proposed by Manuel de la Peña
Status: Merged
Merged at revision: 295
Proposed branch: lp:~mandel/ubuntu-download-manager/update-go-bindings
Merge into: lp:ubuntu-download-manager
Prerequisite: lp:~mandel/ubuntu-download-manager/filter-all-per-app
Diff against target: 875 lines (+583/-67)
4 files modified
bindings/golang/common.go (+78/-0)
bindings/golang/common_test.go (+83/-0)
bindings/golang/downloader.go (+116/-67)
bindings/golang/downloader_test.go (+306/-0)
To merge this branch: bzr merge lp:~mandel/ubuntu-download-manager/update-go-bindings
Reviewer Review Type Date Requested Status
PS Jenkins bot continuous-integration Approve
Sergio Schvezov Needs Fixing
Ubuntu One hackers Pending
Review via email: mp+217255@code.launchpad.net

Commit message

Add the SetLocationDir method that was missing from the golang API. Add missing tests to ensure the correct use of dbus.

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Sergio Schvezov (sergiusens) wrote :

433 + . "gopkg.in/check.v1"

just use the real name, it's packaged as such in the archives.

review: Needs Fixing
259. By Manuel de la Peña

Merged filter-all-per-app into update-go-bindings.

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
260. By Manuel de la Peña

Merged filter-all-per-app into update-go-bindings.

261. By Manuel de la Peña

Merged filter-all-per-app into update-go-bindings.

262. By Manuel de la Peña

Reduce the diff in the following branches by making the generalized moves in this branch.

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Needs Fixing (continuous-integration)
263. By Manuel de la Peña

Merged filter-all-per-app into update-go-bindings.

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Needs Fixing (continuous-integration)
264. By Manuel de la Peña

Fix criss-cross issues.

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== added file 'bindings/golang/common.go'
--- bindings/golang/common.go 1970-01-01 00:00:00 +0000
+++ bindings/golang/common.go 2014-05-09 08:51:26 +0000
@@ -0,0 +1,78 @@
1/*
2 * Copyright 2014 Canonical Ltd.
3 *
4 * Authors:
5 * Manuel de la Pena: manuel.delapena@canonical.com
6 *
7 * This file is part of ubuntu-download-manager.
8 *
9 * ubuntu-download-manager is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 3.
12 *
13 * ubuntu-download-manager is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22// Package udm provides a go interface to work with the ubuntu download manager
23package udm
24
25import (
26 "launchpad.net/go-dbus/v1"
27)
28
29// Progress provides how much progress has been performed in a download that was
30// already started.
31type Progress struct {
32 Received uint64
33 Total uint64
34}
35
36// internal interface used to simplify testing
37type watch interface {
38 Cancel() error
39 Chanel() chan *dbus.Message
40}
41
42// small wrapper used to simplify testing by using the watch interface
43type watchWrapper struct {
44 watch *dbus.SignalWatch
45}
46
47func newWatchWrapper(sw *dbus.SignalWatch) *watchWrapper {
48 w := watchWrapper{}
49 return &w
50}
51
52func (w *watchWrapper) Cancel() error {
53 return w.watch.Cancel()
54}
55
56func (w *watchWrapper) Chanel() chan *dbus.Message {
57 return w.watch.C
58}
59
60// interface added to simplify testing
61type proxy interface {
62 Call(iface, method string, args ...interface{}) (*dbus.Message, error)
63}
64
65var readArgs = func(msg *dbus.Message, args ...interface{}) error {
66 return msg.Args(args)
67}
68
69func connectToSignal(conn *dbus.Connection, path dbus.ObjectPath, signal string) (watch, error) {
70 sw, err := conn.WatchSignal(&dbus.MatchRule{
71 Type: dbus.TypeSignal,
72 Sender: DOWNLOAD_SERVICE,
73 Interface: DOWNLOAD_INTERFACE,
74 Member: signal,
75 Path: path})
76 w := newWatchWrapper(sw)
77 return w, err
78}
079
=== added file 'bindings/golang/common_test.go'
--- bindings/golang/common_test.go 1970-01-01 00:00:00 +0000
+++ bindings/golang/common_test.go 2014-05-09 08:51:26 +0000
@@ -0,0 +1,83 @@
1/*
2 * Copyright 2014 Canonical Ltd.
3 *
4 * Authors:
5 * Manuel de la Pena: manuel.delapena@canonical.com
6 *
7 * This file is part of ubuntu-download-manager.
8 *
9 * ubuntu-download-manager is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 3.
12 *
13 * ubuntu-download-manager is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22package udm
23
24import (
25 . "gopkg.in/check.v1"
26 "launchpad.net/go-dbus/v1"
27 "testing"
28)
29
30func Test(t *testing.T) { TestingT(t) }
31
32type fakeProxy struct {
33 Interface string
34 MethodName string
35 Args []interface{}
36 Err error
37 Result *dbus.Message
38}
39
40func (f *fakeProxy) Call(iface, method string, args ...interface{}) (*dbus.Message, error) {
41 // store the called method and return Result
42 f.Interface = iface
43 f.MethodName = method
44 f.Args = args
45 if f.Err == nil {
46 return f.Result, nil
47 }
48 return nil, f.Err
49}
50
51type FakeWatch struct {
52 Canceled bool
53 Ch chan *dbus.Message
54}
55
56func newFakeWatch() *FakeWatch {
57 ch := make(chan *dbus.Message)
58 fw := FakeWatch{false, ch}
59 return &fw
60}
61
62func (w *FakeWatch) Cancel() error {
63 w.Canceled = true
64 return nil
65}
66
67func (w *FakeWatch) Chanel() chan *dbus.Message {
68 return w.Ch
69}
70
71// returns a new error that can be used in the tests
72func newDBusError() *dbus.Message {
73 msg := dbus.NewMethodCallMessage("com.destination", "/path", "com.interface", "method")
74 msg.Type = dbus.TypeError
75 msg.ErrorName = "com.testing.udm"
76 return msg
77}
78
79func newDBusReturn() *dbus.Message {
80 msg := dbus.NewMethodCallMessage("com.destination", "/path", "com.interface", "method")
81 msg.Type = dbus.TypeMethodReturn
82 return msg
83}
084
=== renamed file 'bindings/golang/udm.go' => 'bindings/golang/downloader.go'
--- bindings/golang/udm.go 2014-03-19 11:57:35 +0000
+++ bindings/golang/downloader.go 2014-05-09 08:51:26 +0000
@@ -24,6 +24,7 @@
2424
25import (25import (
26 "errors"26 "errors"
27 "fmt"
27 "launchpad.net/go-dbus/v1"28 "launchpad.net/go-dbus/v1"
28 "runtime"29 "runtime"
29)30)
@@ -51,13 +52,6 @@
51 POST_DOWNLOAD_COMMAND = "post-download-command"52 POST_DOWNLOAD_COMMAND = "post-download-command"
52)53)
5354
54// Progress provides how much progress has been performed in a download that was
55// already started.
56type Progress struct {
57 Received uint64
58 Total uint64
59}
60
61// Download is the common interface of a download. It provides all the required55// Download is the common interface of a download. It provides all the required
62// methods to interact with a download created by udm.56// methods to interact with a download created by udm.
63type Download interface {57type Download interface {
@@ -67,6 +61,7 @@
67 SetThrottle(uint64) error61 SetThrottle(uint64) error
68 Throttle() (uint64, error)62 Throttle() (uint64, error)
69 AllowMobileDownload(bool) error63 AllowMobileDownload(bool) error
64 SetDestinationDir(string) error
70 IsMobileDownload() (bool, error)65 IsMobileDownload() (bool, error)
71 Start() error66 Start() error
72 Pause() error67 Pause() error
@@ -81,42 +76,25 @@
81 Error() chan error76 Error() chan error
82}77}
8378
84// Manager is the single point of entry of the API. Allows to interact with the
85// general setting of udm as well as to create downloads at will.
86type Manager interface {
87 CreateDownload(string, string, string, map[string]interface{}, map[string]string) (Download, error)
88 CreateMmsDownload(string, string, int, string, string) (Download, error)
89}
90
91// FileDownload represents a single file being downloaded by udm.79// FileDownload represents a single file being downloaded by udm.
92type FileDownload struct {80type FileDownload struct {
93 conn *dbus.Connection81 conn *dbus.Connection
94 proxy *dbus.ObjectProxy82 proxy proxy
95 path dbus.ObjectPath83 path dbus.ObjectPath
96 started chan bool84 started chan bool
97 started_w *dbus.SignalWatch85 started_w watch
98 paused chan bool86 paused chan bool
99 paused_w *dbus.SignalWatch87 paused_w watch
100 resumed chan bool88 resumed chan bool
101 resumed_w *dbus.SignalWatch89 resumed_w watch
102 canceled chan bool90 canceled chan bool
103 canceled_w *dbus.SignalWatch91 canceled_w watch
104 finished chan string92 finished chan string
105 finished_w *dbus.SignalWatch93 finished_w watch
106 errors chan error94 errors chan error
107 error_w *dbus.SignalWatch95 error_w watch
108 progress chan Progress96 progress chan Progress
109 progress_w *dbus.SignalWatch97 progress_w watch
110}
111
112func connectToSignal(conn *dbus.Connection, path dbus.ObjectPath, signal string) (*dbus.SignalWatch, error) {
113 w, err := conn.WatchSignal(&dbus.MatchRule{
114 Type: dbus.TypeSignal,
115 Sender: DOWNLOAD_SERVICE,
116 Interface: DOWNLOAD_INTERFACE,
117 Member: signal,
118 Path: path})
119 return w, err
120}98}
12199
122func (down *FileDownload) free() {100func (down *FileDownload) free() {
@@ -197,10 +175,15 @@
197// TotalSize returns the total size of the file being downloaded.175// TotalSize returns the total size of the file being downloaded.
198func (down *FileDownload) TotalSize() (size uint64, err error) {176func (down *FileDownload) TotalSize() (size uint64, err error) {
199 reply, err := down.proxy.Call(DOWNLOAD_INTERFACE, "totalSize")177 reply, err := down.proxy.Call(DOWNLOAD_INTERFACE, "totalSize")
200 if err != nil || reply.Type == dbus.TypeError {178 if err != nil {
201 return 0, err179 return 0, err
202 }180 }
203 if err = reply.Args(&size); err != nil {181
182 if reply.Type == dbus.TypeError {
183 return 0, fmt.Errorf("DBus Error: %", reply.ErrorName)
184 }
185
186 if err = readArgs(reply, &size); err != nil {
204 return 0, err187 return 0, err
205 }188 }
206 return size, nil189 return size, nil
@@ -209,10 +192,15 @@
209// Process returns the process so far in downloading the file.192// Process returns the process so far in downloading the file.
210func (down *FileDownload) Progress() (progress uint64, err error) {193func (down *FileDownload) Progress() (progress uint64, err error) {
211 reply, err := down.proxy.Call(DOWNLOAD_INTERFACE, "progress")194 reply, err := down.proxy.Call(DOWNLOAD_INTERFACE, "progress")
212 if err != nil || reply.Type == dbus.TypeError {195 if err != nil {
213 return 0, err196 return 0, err
214 }197 }
215 if err = reply.Args(&progress); err != nil {198
199 if reply.Type == dbus.TypeError {
200 return 0, fmt.Errorf("DBus Error: %", reply.ErrorName)
201 }
202
203 if err = readArgs(reply, &progress); err != nil {
216 return 0, err204 return 0, err
217 }205 }
218 return progress, nil206 return progress, nil
@@ -221,10 +209,15 @@
221// Metadata returns the metadata that was provided at creating time to the download.209// Metadata returns the metadata that was provided at creating time to the download.
222func (down *FileDownload) Metadata() (metadata map[string]string, err error) {210func (down *FileDownload) Metadata() (metadata map[string]string, err error) {
223 reply, err := down.proxy.Call(DOWNLOAD_INTERFACE, "metadata")211 reply, err := down.proxy.Call(DOWNLOAD_INTERFACE, "metadata")
224 if err != nil || reply.Type == dbus.TypeError {212 if err != nil {
225 return nil, err213 return nil, err
226 }214 }
227 if err = reply.Args(&metadata); err != nil {215
216 if reply.Type == dbus.TypeError {
217 return nil, fmt.Errorf("DBus Error: %", reply.ErrorName)
218 }
219
220 if err = readArgs(reply, &metadata); err != nil {
228 return nil, err221 return nil, err
229 }222 }
230 return metadata, nil223 return metadata, nil
@@ -233,19 +226,29 @@
233// SetThrottle sets the network throttle to be used in the download.226// SetThrottle sets the network throttle to be used in the download.
234func (down *FileDownload) SetThrottle(throttle uint64) (err error) {227func (down *FileDownload) SetThrottle(throttle uint64) (err error) {
235 reply, err := down.proxy.Call(DOWNLOAD_INTERFACE, "setThrottle", throttle)228 reply, err := down.proxy.Call(DOWNLOAD_INTERFACE, "setThrottle", throttle)
236 if err != nil || reply.Type == dbus.TypeError {229 if err != nil {
237 return err230 return err
238 }231 }
232
233 if reply.Type == dbus.TypeError {
234 return fmt.Errorf("DBus Error: %", reply.ErrorName)
235 }
236
239 return nil237 return nil
240}238}
241239
242// Throttle returns the network throttle that is currently used in the download.240// Throttle returns the network throttle that is currently used in the download.
243func (down *FileDownload) Throttle() (throttle uint64, err error) {241func (down *FileDownload) Throttle() (throttle uint64, err error) {
244 reply, err := down.proxy.Call(DOWNLOAD_INTERFACE, "throttle")242 reply, err := down.proxy.Call(DOWNLOAD_INTERFACE, "throttle")
245 if err != nil || reply.Type == dbus.TypeError {243 if err != nil {
246 return 0, err244 return 0, err
247 }245 }
248 if err = reply.Args(&throttle); err != nil {246
247 if reply.Type == dbus.TypeError {
248 return 0, fmt.Errorf("DBus Error: %", reply.ErrorName)
249 }
250
251 if err = readArgs(reply, &throttle); err != nil {
249 return 0, err252 return 0, err
250 }253 }
251 return throttle, nil254 return throttle, nil
@@ -255,26 +258,52 @@
255// connection.258// connection.
256func (down *FileDownload) AllowMobileDownload(allowed bool) (err error) {259func (down *FileDownload) AllowMobileDownload(allowed bool) (err error) {
257 reply, err := down.proxy.Call(DOWNLOAD_INTERFACE, "allowGSMDownload", allowed)260 reply, err := down.proxy.Call(DOWNLOAD_INTERFACE, "allowGSMDownload", allowed)
258 if err != nil || reply.Type == dbus.TypeError {261 if err != nil {
259 return err262 return err
260 }263 }
264
265 if reply.Type == dbus.TypeError {
266 return fmt.Errorf("DBus Error: %", reply.ErrorName)
267 }
268
269 return nil
270}
271
272// SetDestinationDir permits unconfined applications to set the destination
273// directory of the download. This method must be called BEFORE the download
274// is started else an error will be returned.
275func (down *FileDownload) SetDestinationDir(path string) (err error) {
276 reply, err := down.proxy.Call(DOWNLOAD_INTERFACE, "setDestinationDir", path)
277 if err != nil {
278 return err
279 }
280
281 if reply.Type == dbus.TypeError {
282 return fmt.Errorf("DBus Error: %", reply.ErrorName)
283 }
284
261 return nil285 return nil
262}286}
263287
264// IsMobileDownload returns if the download will be performed over the mobile data.288// IsMobileDownload returns if the download will be performed over the mobile data.
265func (down *FileDownload) IsMobileDownload() (allowed bool, err error) {289func (down *FileDownload) IsMobileDownload() (allowed bool, err error) {
266 reply, err := down.proxy.Call(DOWNLOAD_INTERFACE, "isGSMDownloadAllowed", allowed)290 reply, err := down.proxy.Call(DOWNLOAD_INTERFACE, "isGSMDownloadAllowed", allowed)
267 if err != nil || reply.Type == dbus.TypeError {291 if err != nil {
268 return false, err292 return false, err
269 }293 }
270 if err = reply.Args(&allowed); err != nil {294
295 if reply.Type == dbus.TypeError {
296 return false, fmt.Errorf("DBus Error: %", reply.ErrorName)
297 }
298
299 if err = readArgs(reply, &allowed); err != nil {
271 return false, err300 return false, err
272 }301 }
273 return allowed, nil302 return allowed, nil
274}303}
275304
276// Start tells udm that the download is ready to be peformed and that the client is305// Start tells udm that the download is ready to be peformed and that the client is
277// ready to recieve signals. The following is a commong pattern to be used when306// ready to recieve signals. The following is a common pattern to be used when
278// creating downloads in udm.307// creating downloads in udm.
279//308//
280// man, err := udm.NewDownloadManager()309// man, err := udm.NewDownloadManager()
@@ -317,27 +346,42 @@
317// <- finished_signal346// <- finished_signal
318func (down *FileDownload) Start() (err error) {347func (down *FileDownload) Start() (err error) {
319 reply, err := down.proxy.Call(DOWNLOAD_INTERFACE, "start")348 reply, err := down.proxy.Call(DOWNLOAD_INTERFACE, "start")
320 if err != nil || reply.Type == dbus.TypeError {349 if err != nil {
321 return err350 return err
322 }351 }
352
353 if reply.Type == dbus.TypeError {
354 return fmt.Errorf("DBus Error: %", reply.ErrorName)
355 }
356
323 return nil357 return nil
324}358}
325359
326// Pause pauses a download that was started and if not nothing is done.360// Pause pauses a download that was started and if not nothing is done.
327func (down *FileDownload) Pause() (err error) {361func (down *FileDownload) Pause() (err error) {
328 reply, err := down.proxy.Call(DOWNLOAD_INTERFACE, "pause")362 reply, err := down.proxy.Call(DOWNLOAD_INTERFACE, "pause")
329 if err != nil || reply.Type == dbus.TypeError {363 if err != nil {
330 return err364 return err
331 }365 }
366
367 if reply.Type == dbus.TypeError {
368 return fmt.Errorf("DBus Error: %", reply.ErrorName)
369 }
370
332 return nil371 return nil
333}372}
334373
335// Resumes a download that was paused or does nothing otherwise.374// Resumes a download that was paused or does nothing otherwise.
336func (down *FileDownload) Resume() (err error) {375func (down *FileDownload) Resume() (err error) {
337 reply, err := down.proxy.Call(DOWNLOAD_INTERFACE, "resume")376 reply, err := down.proxy.Call(DOWNLOAD_INTERFACE, "resume")
338 if err != nil || reply.Type == dbus.TypeError {377 if err != nil {
339 return err378 return err
340 }379 }
380
381 if reply.Type == dbus.TypeError {
382 return fmt.Errorf("DBus Error: %", reply.ErrorName)
383 }
384
341 return nil385 return nil
342}386}
343387
@@ -345,17 +389,22 @@
345// that were created.389// that were created.
346func (down *FileDownload) Cancel() (err error) {390func (down *FileDownload) Cancel() (err error) {
347 reply, err := down.proxy.Call(DOWNLOAD_INTERFACE, "cancel")391 reply, err := down.proxy.Call(DOWNLOAD_INTERFACE, "cancel")
348 if err != nil || reply.Type == dbus.TypeError {392 if err != nil {
349 return err393 return err
350 }394 }
395
396 if reply.Type == dbus.TypeError {
397 return fmt.Errorf("DBus Error: %", reply.ErrorName)
398 }
399
351 return nil400 return nil
352}401}
353402
354func (down *FileDownload) connectToStarted() {403func (down *FileDownload) connectToStarted() {
355 go func() {404 go func() {
356 for msg := range down.started_w.C {405 for msg := range down.started_w.Chanel() {
357 var started bool406 var started bool
358 msg.Args(&started)407 readArgs(msg, &started)
359 down.started <- started408 down.started <- started
360 }409 }
361 close(down.started)410 close(down.started)
@@ -369,9 +418,9 @@
369418
370func (down *FileDownload) connectToPaused() {419func (down *FileDownload) connectToPaused() {
371 go func() {420 go func() {
372 for msg := range down.paused_w.C {421 for msg := range down.paused_w.Chanel() {
373 var paused bool422 var paused bool
374 msg.Args(&paused)423 readArgs(msg, &paused)
375 down.paused <- paused424 down.paused <- paused
376 }425 }
377 close(down.paused)426 close(down.paused)
@@ -385,10 +434,10 @@
385434
386func (down *FileDownload) connectToProgress() {435func (down *FileDownload) connectToProgress() {
387 go func() {436 go func() {
388 for msg := range down.progress_w.C {437 for msg := range down.progress_w.Chanel() {
389 var received uint64438 var received uint64
390 var total uint64439 var total uint64
391 msg.Args(&received, &total)440 readArgs(msg, &received, &total)
392 down.progress <- Progress{received, total}441 down.progress <- Progress{received, total}
393 }442 }
394 close(down.progress)443 close(down.progress)
@@ -403,9 +452,9 @@
403452
404func (down *FileDownload) connectToResumed() {453func (down *FileDownload) connectToResumed() {
405 go func() {454 go func() {
406 for msg := range down.resumed_w.C {455 for msg := range down.resumed_w.Chanel() {
407 var resumed bool456 var resumed bool
408 msg.Args(&resumed)457 readArgs(msg, &resumed)
409 down.resumed <- resumed458 down.resumed <- resumed
410 }459 }
411 close(down.resumed)460 close(down.resumed)
@@ -419,9 +468,9 @@
419468
420func (down *FileDownload) connectToCanceled() {469func (down *FileDownload) connectToCanceled() {
421 go func() {470 go func() {
422 for msg := range down.canceled_w.C {471 for msg := range down.canceled_w.Chanel() {
423 var canceled bool472 var canceled bool
424 msg.Args(&canceled)473 readArgs(msg, &canceled)
425 down.canceled <- canceled474 down.canceled <- canceled
426 }475 }
427 close(down.canceled)476 close(down.canceled)
@@ -435,9 +484,9 @@
435484
436func (down *FileDownload) connectToFinished() {485func (down *FileDownload) connectToFinished() {
437 go func() {486 go func() {
438 for msg := range down.finished_w.C {487 for msg := range down.finished_w.Chanel() {
439 var path string488 var path string
440 msg.Args(&path)489 readArgs(msg, &path)
441 down.finished <- path490 down.finished <- path
442 }491 }
443 close(down.finished)492 close(down.finished)
@@ -451,9 +500,9 @@
451500
452func (down *FileDownload) connectToError() {501func (down *FileDownload) connectToError() {
453 go func() {502 go func() {
454 for msg := range down.error_w.C {503 for msg := range down.error_w.Chanel() {
455 var reason string504 var reason string
456 msg.Args(&reason)505 readArgs(msg, &reason)
457 down.errors <- errors.New(reason)506 down.errors <- errors.New(reason)
458 }507 }
459 close(down.errors)508 close(down.errors)
@@ -527,7 +576,7 @@
527 if err != nil || reply.Type == dbus.TypeError {576 if err != nil || reply.Type == dbus.TypeError {
528 return nil, err577 return nil, err
529 }578 }
530 if err = reply.Args(&path); err != nil {579 if err = readArgs(reply, &path); err != nil {
531 return nil, err580 return nil, err
532 }581 }
533 down, err = newFileDownload(man.conn, path)582 down, err = newFileDownload(man.conn, path)
@@ -543,7 +592,7 @@
543 if err != nil || reply.Type == dbus.TypeError {592 if err != nil || reply.Type == dbus.TypeError {
544 return nil, err593 return nil, err
545 }594 }
546 if err = reply.Args(&path); err != nil {595 if err = readArgs(reply, &path); err != nil {
547 return nil, err596 return nil, err
548 }597 }
549 down, err = newFileDownload(man.conn, path)598 down, err = newFileDownload(man.conn, path)
550599
=== added file 'bindings/golang/downloader_test.go'
--- bindings/golang/downloader_test.go 1970-01-01 00:00:00 +0000
+++ bindings/golang/downloader_test.go 2014-05-09 08:51:26 +0000
@@ -0,0 +1,306 @@
1/*
2 * Copyright 2014 Canonical Ltd.
3 *
4 * Authors:
5 * Manuel de la Pena: manuel.delapena@canonical.com
6 *
7 * This file is part of ubuntu-download-manager.
8 *
9 * ubuntu-download-manager is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 3.
12 *
13 * ubuntu-download-manager is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22package udm
23
24import (
25 "errors"
26 . "gopkg.in/check.v1"
27 "launchpad.net/go-dbus/v1"
28 "reflect"
29)
30
31type DownloadSuite struct {
32 proxy *fakeProxy
33 download *FileDownload
34 msg_args []interface{}
35 msg_args_err error
36 started_ch chan bool
37 started_w watch
38 paused_ch chan bool
39 paused_w watch
40 resumed_ch chan bool
41 resumed_w watch
42 canceled_ch chan bool
43 canceled_w watch
44 finished_ch chan string
45 finished_w watch
46 errors_ch chan error
47 errors_w watch
48 progress_ch chan Progress
49 progress_w watch
50}
51
52var _ = Suite(&DownloadSuite{})
53
54func (s *DownloadSuite) SetUpTest(c *C) {
55 s.proxy = &fakeProxy{}
56 s.started_ch = make(chan bool)
57 s.started_w = newFakeWatch()
58 s.paused_ch = make(chan bool)
59 s.paused_w = newFakeWatch()
60 s.resumed_ch = make(chan bool)
61 s.resumed_w = newFakeWatch()
62 s.canceled_ch = make(chan bool)
63 s.canceled_w = newFakeWatch()
64 s.finished_ch = make(chan string)
65 s.finished_w = newFakeWatch()
66 s.errors_ch = make(chan error)
67 s.errors_w = newFakeWatch()
68 s.progress_ch = make(chan Progress)
69 s.progress_w = newFakeWatch()
70 s.download = &FileDownload{nil, s.proxy, "", s.started_ch, s.started_w, s.paused_ch, s.paused_w, s.resumed_ch, s.resumed_w, s.canceled_ch, s.canceled_w, s.finished_ch, s.finished_w, s.errors_ch, s.errors_w, s.progress_ch, s.progress_w}
71
72 readArgs = func(msg *dbus.Message, args ...interface{}) error {
73 for i, arg := range args {
74 v := reflect.ValueOf(arg)
75 e := v.Elem()
76 switch s.msg_args[i].(type) {
77 default:
78 return errors.New("unexpected type")
79 case bool:
80 e.SetBool(s.msg_args[i].(bool))
81 case uint64:
82 e.SetUint(s.msg_args[i].(uint64))
83 }
84 }
85 return s.msg_args_err
86 }
87}
88
89func (s *DownloadSuite) TestTotalSizeError(c *C) {
90 s.proxy.Result = newDBusError()
91 size, err := s.download.TotalSize()
92 c.Assert(s.proxy.Interface, Equals, DOWNLOAD_INTERFACE)
93 c.Assert(s.proxy.MethodName, Equals, "totalSize")
94 c.Assert(size, Equals, uint64(0))
95 c.Assert(err, NotNil)
96}
97
98func (s *DownloadSuite) TestTotalSize(c *C) {
99 expected_size := uint64(98)
100 s.proxy.Result = newDBusReturn()
101 s.msg_args = make([]interface{}, 1)
102 s.msg_args[0] = expected_size
103 s.msg_args_err = nil
104 size, err := s.download.TotalSize()
105 c.Assert(s.proxy.Interface, Equals, DOWNLOAD_INTERFACE)
106 c.Assert(s.proxy.MethodName, Equals, "totalSize")
107 c.Assert(err, IsNil)
108 c.Assert(size, Equals, expected_size)
109}
110
111func (s *DownloadSuite) TestProgressError(c *C) {
112 s.proxy.Result = newDBusError()
113 progress, err := s.download.Progress()
114 c.Assert(s.proxy.Interface, Equals, DOWNLOAD_INTERFACE)
115 c.Assert(s.proxy.MethodName, Equals, "progress")
116 c.Assert(progress, Equals, uint64(0))
117 c.Assert(err, NotNil)
118}
119
120func (s *DownloadSuite) TestProgress(c *C) {
121 expected_progress := uint64(98)
122 s.proxy.Result = newDBusReturn()
123 s.msg_args = make([]interface{}, 1)
124 s.msg_args[0] = expected_progress
125 s.msg_args_err = nil
126 progress, err := s.download.Progress()
127 c.Assert(s.proxy.Interface, Equals, DOWNLOAD_INTERFACE)
128 c.Assert(s.proxy.MethodName, Equals, "progress")
129 c.Assert(err, IsNil)
130 c.Assert(progress, Equals, expected_progress)
131}
132
133func (s *DownloadSuite) TestMetadataError(c *C) {
134 s.proxy.Result = newDBusError()
135 metadata, err := s.download.Metadata()
136 c.Assert(s.proxy.Interface, Equals, DOWNLOAD_INTERFACE)
137 c.Assert(s.proxy.MethodName, Equals, "metadata")
138 c.Assert(metadata, IsNil)
139 c.Assert(err, NotNil)
140}
141
142func (s *DownloadSuite) TestSetThrotthleError(c *C) {
143 throttle := uint64(9)
144 s.proxy.Result = newDBusError()
145 err := s.download.SetThrottle(throttle)
146 c.Assert(s.proxy.Interface, Equals, DOWNLOAD_INTERFACE)
147 c.Assert(s.proxy.MethodName, Equals, "setThrottle")
148 c.Assert(err, NotNil)
149 c.Assert(s.proxy.Args[0], Equals, throttle)
150}
151
152func (s *DownloadSuite) TestSetThrottle(c *C) {
153 s.proxy.Result = newDBusReturn()
154 err := s.download.SetThrottle(uint64(9))
155 c.Assert(s.proxy.Interface, Equals, DOWNLOAD_INTERFACE)
156 c.Assert(s.proxy.MethodName, Equals, "setThrottle")
157 c.Assert(err, IsNil)
158}
159
160func (s *DownloadSuite) TestThrottleError(c *C) {
161 s.proxy.Result = newDBusError()
162 size, err := s.download.Throttle()
163 c.Assert(s.proxy.Interface, Equals, DOWNLOAD_INTERFACE)
164 c.Assert(s.proxy.MethodName, Equals, "throttle")
165 c.Assert(size, Equals, uint64(0))
166 c.Assert(err, NotNil)
167}
168
169func (s *DownloadSuite) TestThrottle(c *C) {
170 expected_throttle := uint64(98)
171 s.proxy.Result = newDBusReturn()
172 s.msg_args = make([]interface{}, 1)
173 s.msg_args[0] = expected_throttle
174 s.msg_args_err = nil
175 throttle, err := s.download.Throttle()
176 c.Assert(s.proxy.Interface, Equals, DOWNLOAD_INTERFACE)
177 c.Assert(s.proxy.MethodName, Equals, "throttle")
178 c.Assert(err, IsNil)
179 c.Assert(throttle, Equals, expected_throttle)
180}
181
182func (s *DownloadSuite) TestAllowMobileDownloadError(c *C) {
183 s.proxy.Result = newDBusError()
184 err := s.download.AllowMobileDownload(true)
185 c.Assert(s.proxy.Interface, Equals, DOWNLOAD_INTERFACE)
186 c.Assert(s.proxy.MethodName, Equals, "allowGSMDownload")
187 c.Assert(err, NotNil)
188}
189
190func (s *DownloadSuite) TestAllowMobileDownload(c *C) {
191 expected_allowed := true
192 s.proxy.Result = newDBusReturn()
193 err := s.download.AllowMobileDownload(expected_allowed)
194 c.Assert(s.proxy.Interface, Equals, DOWNLOAD_INTERFACE)
195 c.Assert(s.proxy.MethodName, Equals, "allowGSMDownload")
196 c.Assert(err, IsNil)
197 c.Assert(s.proxy.Args[0], Equals, expected_allowed)
198}
199
200func (s *DownloadSuite) TestSetDestinationDirError(c *C) {
201 s.proxy.Result = newDBusError()
202 err := s.download.SetDestinationDir("/new/path")
203 c.Assert(s.proxy.Interface, Equals, DOWNLOAD_INTERFACE)
204 c.Assert(s.proxy.MethodName, Equals, "setDestinationDir")
205 c.Assert(err, NotNil)
206}
207
208func (s *DownloadSuite) TestSetDestinationDir(c *C) {
209 expected_dir := "/path/to/use"
210 s.proxy.Result = newDBusReturn()
211 err := s.download.SetDestinationDir(expected_dir)
212 c.Assert(s.proxy.Interface, Equals, DOWNLOAD_INTERFACE)
213 c.Assert(s.proxy.MethodName, Equals, "setDestinationDir")
214 c.Assert(err, IsNil)
215 c.Assert(s.proxy.Args[0], Equals, expected_dir)
216}
217
218func (s *DownloadSuite) TestIsMobileDownloadError(c *C) {
219 s.proxy.Result = newDBusError()
220 allowed, err := s.download.IsMobileDownload()
221 c.Assert(s.proxy.Interface, Equals, DOWNLOAD_INTERFACE)
222 c.Assert(s.proxy.MethodName, Equals, "isGSMDownloadAllowed")
223 c.Assert(allowed, Equals, false)
224 c.Assert(err, NotNil)
225}
226
227func (s *DownloadSuite) TestIsMobileDownload(c *C) {
228 expected_allowed := true
229 s.proxy.Result = newDBusReturn()
230 s.msg_args = make([]interface{}, 1)
231 s.msg_args[0] = expected_allowed
232 s.msg_args_err = nil
233 allowed, err := s.download.IsMobileDownload()
234 c.Assert(s.proxy.Interface, Equals, DOWNLOAD_INTERFACE)
235 c.Assert(s.proxy.MethodName, Equals, "isGSMDownloadAllowed")
236 c.Assert(err, IsNil)
237 c.Assert(allowed, Equals, expected_allowed)
238}
239
240func (s *DownloadSuite) TestStartDBusError(c *C) {
241 s.proxy.Result = newDBusError()
242 err := s.download.Start()
243 c.Assert(s.proxy.Interface, Equals, DOWNLOAD_INTERFACE)
244 c.Assert(s.proxy.MethodName, Equals, "start")
245 c.Assert(err, NotNil)
246}
247
248func (s *DownloadSuite) TestStartError(c *C) {
249 s.proxy.Result = newDBusReturn()
250 s.proxy.Err = errors.New("Fake error")
251 err := s.download.Start()
252 c.Assert(s.proxy.Interface, Equals, DOWNLOAD_INTERFACE)
253 c.Assert(s.proxy.MethodName, Equals, "start")
254 c.Assert(err, NotNil)
255}
256
257func (s *DownloadSuite) TestPauseDBusError(c *C) {
258 s.proxy.Result = newDBusError()
259 err := s.download.Pause()
260 c.Assert(s.proxy.Interface, Equals, DOWNLOAD_INTERFACE)
261 c.Assert(s.proxy.MethodName, Equals, "pause")
262 c.Assert(err, NotNil)
263}
264
265func (s *DownloadSuite) TestPauseError(c *C) {
266 s.proxy.Result = newDBusReturn()
267 s.proxy.Err = errors.New("Fake error")
268 err := s.download.Pause()
269 c.Assert(s.proxy.Interface, Equals, DOWNLOAD_INTERFACE)
270 c.Assert(s.proxy.MethodName, Equals, "pause")
271 c.Assert(err, NotNil)
272}
273
274func (s *DownloadSuite) TestResumeDBusError(c *C) {
275 s.proxy.Result = newDBusError()
276 err := s.download.Resume()
277 c.Assert(s.proxy.Interface, Equals, DOWNLOAD_INTERFACE)
278 c.Assert(s.proxy.MethodName, Equals, "resume")
279 c.Assert(err, NotNil)
280}
281
282func (s *DownloadSuite) TestResumeError(c *C) {
283 s.proxy.Result = newDBusReturn()
284 s.proxy.Err = errors.New("Fake error")
285 err := s.download.Resume()
286 c.Assert(s.proxy.Interface, Equals, DOWNLOAD_INTERFACE)
287 c.Assert(s.proxy.MethodName, Equals, "resume")
288 c.Assert(err, NotNil)
289}
290
291func (s *DownloadSuite) TestCancelDBusError(c *C) {
292 s.proxy.Result = newDBusError()
293 err := s.download.Cancel()
294 c.Assert(s.proxy.Interface, Equals, DOWNLOAD_INTERFACE)
295 c.Assert(s.proxy.MethodName, Equals, "cancel")
296 c.Assert(err, NotNil)
297}
298
299func (s *DownloadSuite) TestCancelError(c *C) {
300 s.proxy.Result = newDBusReturn()
301 s.proxy.Err = errors.New("Fake error")
302 err := s.download.Cancel()
303 c.Assert(s.proxy.Interface, Equals, DOWNLOAD_INTERFACE)
304 c.Assert(s.proxy.MethodName, Equals, "cancel")
305 c.Assert(err, NotNil)
306}

Subscribers

People subscribed via source and target branches