Merge lp:~chipaca/ubuntu-push/notifications into lp:ubuntu-push

Proposed by John Lenton
Status: Merged
Approved by: John Lenton
Approved revision: no longer in the source branch.
Merged at revision: 18
Proposed branch: lp:~chipaca/ubuntu-push/notifications
Merge into: lp:ubuntu-push
Prerequisite: lp:~chipaca/ubuntu-push/multi-valued
Diff against target: 256 lines (+197/-8)
3 files modified
connectivity/connectivity_test.go (+6/-8)
notifications/raw.go (+97/-0)
notifications/raw_test.go (+94/-0)
To merge this branch: bzr merge lp:~chipaca/ubuntu-push/notifications
Reviewer Review Type Date Requested Status
Samuele Pedroni Approve
Review via email: mp+202477@code.launchpad.net

Commit message

notifications! first, a low-level api.

Description of the change

Implemented a low-level notifications API.

To post a comment you must log in.
Revision history for this message
Samuele Pedroni (pedronis) wrote :

we have a lot of things that are notifications, wondering if we need a more precise package name

// New returns a new Notifications that'll use the provided bus.Endpoint

it's called Raw and takes a bus.Bus

maybe the Raw* types need a short descr as well

of course this will need some integration testing involving qa

review: Approve
Revision history for this message
John Lenton (chipaca) wrote :

Fixed the comment, added a short desc to the Raw* types.
I'm all ears for suggestions for names...

Revision history for this message
Samuele Pedroni (pedronis) :
review: Approve
Revision history for this message
Ubuntu One Auto Pilot (otto-pilot) wrote :

The attempt to merge lp:~chipaca/ubuntu-push/notifications into lp:ubuntu-push failed. Below is the output from the failed tests.

mkdir -p /mnt/tarmac/cache/ubuntu-push/go-ws/bin
mkdir -p /mnt/tarmac/cache/ubuntu-push/go-ws/pkg
go get -u launchpad.net/godeps
go get -d -u launchpad.net/gocheck launchpad.net/go-dbus/v1
/mnt/tarmac/cache/ubuntu-push/go-ws/bin/godeps -u dependencies.tsv
go install launchpad.net/gocheck launchpad.net/go-dbus/v1
go test launchpad.net/ubuntu-push/...
ok launchpad.net/ubuntu-push/bus 0.009s
ok launchpad.net/ubuntu-push/bus/testing 0.024s
ok launchpad.net/ubuntu-push/config 0.009s
ok launchpad.net/ubuntu-push/connectivity 0.102s
? launchpad.net/ubuntu-push/connectivity/example [no test files]
ok launchpad.net/ubuntu-push/connectivity/webchecker 0.018s
ok launchpad.net/ubuntu-push/logger 0.006s
ok launchpad.net/ubuntu-push/networkmanager 0.036s
ok launchpad.net/ubuntu-push/notifications 0.016s
ok launchpad.net/ubuntu-push/protocol 0.012s
ok launchpad.net/ubuntu-push/server/acceptance 0.006s
? launchpad.net/ubuntu-push/server/acceptance/cmd [no test files]
ok launchpad.net/ubuntu-push/server/api 0.015s
ok launchpad.net/ubuntu-push/server/broker 0.007s
ok launchpad.net/ubuntu-push/server/dev 0.027s
ok launchpad.net/ubuntu-push/server/listener 0.358s
ok launchpad.net/ubuntu-push/server/session 0.074s
ok launchpad.net/ubuntu-push/server/store 0.006s
? launchpad.net/ubuntu-push/testing [no test files]
ok launchpad.net/ubuntu-push/testing/condition 0.004s
ok launchpad.net/ubuntu-push/whoopsie/identifier 0.018s
ok launchpad.net/ubuntu-push/whoopsie/identifier/testing 0.018s
scripts/check_fmt launchpad.net/ubuntu-push
pkg launchpad.net/ubuntu-push/connectivity has some gofmt non-compliant files:
connectivity_test.go

make: *** [check-format] Error 1

18. By John Lenton

[r=pedronis] notifications! first, a low-level api.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'connectivity/connectivity_test.go'
2--- connectivity/connectivity_test.go 2014-01-20 17:37:55 +0000
3+++ connectivity/connectivity_test.go 2014-01-22 12:27:44 +0000
4@@ -128,11 +128,11 @@
5 */
6
7 func (s *ConnSuite) TestSteps(c *C) {
8- webget_works := func(ch chan<- bool) { ch <- true }
9- webget_fails := func(ch chan<- bool) { ch <- false }
10+ var webget_p condition.Interface = condition.Work(true)
11+ recheck_timeout := 50 * time.Millisecond
12
13 cfg := Config{
14- RecheckTimeout: config.ConfigTimeDuration{10 * time.Millisecond},
15+ RecheckTimeout: config.ConfigTimeDuration{recheck_timeout},
16 }
17 ch := make(chan networkmanager.State, 10)
18 cs := &connectedState{
19@@ -140,7 +140,7 @@
20 networkStateCh: ch,
21 timer: time.NewTimer(time.Second),
22 log: nullog,
23- webget: webget_works,
24+ webget: func(ch chan<- bool) { ch <- webget_p.OK() },
25 lastSent: false,
26 }
27 ch <- networkmanager.ConnectedGlobal
28@@ -156,7 +156,7 @@
29 c.Check(f, Equals, true) // and if the web check works, go back to connected
30
31 // same scenario, but with failing web check
32- cs.webget = webget_fails
33+ webget_p = condition.Fail2Work(1)
34 ch <- networkmanager.ConnectedGlobal
35 f, e = cs.connectedStateStep()
36 c.Check(e, IsNil)
37@@ -164,7 +164,7 @@
38
39 // the next call to Step will time out
40 _ch := make(chan bool, 1)
41- _t := time.NewTimer(10 * time.Millisecond)
42+ _t := time.NewTimer(recheck_timeout / 2)
43
44 go func() {
45 f, e := cs.connectedStateStep()
46@@ -178,8 +178,6 @@
47 case <-_t.C:
48 }
49
50- // put it back together again
51- cs.webget = webget_works
52 // now an recheckTimeout later, we'll get true
53 c.Check(<-_ch, Equals, true)
54
55
56=== added directory 'notifications'
57=== added file 'notifications/raw.go'
58--- notifications/raw.go 1970-01-01 00:00:00 +0000
59+++ notifications/raw.go 2014-01-22 12:27:44 +0000
60@@ -0,0 +1,97 @@
61+/*
62+ Copyright 2013-2014 Canonical Ltd.
63+
64+ This program is free software: you can redistribute it and/or modify it
65+ under the terms of the GNU General Public License version 3, as published
66+ by the Free Software Foundation.
67+
68+ This program is distributed in the hope that it will be useful, but
69+ WITHOUT ANY WARRANTY; without even the implied warranties of
70+ MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
71+ PURPOSE. See the GNU General Public License for more details.
72+
73+ You should have received a copy of the GNU General Public License along
74+ with this program. If not, see <http://www.gnu.org/licenses/>.
75+*/
76+
77+// Package notifications wraps a couple of Notifications's DBus API points:
78+// the org.freedesktop.Notifications.Notify call, and listening for the
79+// ActionInvoked signal.
80+package notifications
81+
82+// this is the lower-level api
83+
84+import (
85+ "launchpad.net/go-dbus/v1"
86+ "launchpad.net/ubuntu-push/bus"
87+ "launchpad.net/ubuntu-push/logger"
88+)
89+
90+// Notifications lives on a well-knwon bus.Address
91+var BusAddress bus.Address = bus.Address{
92+ Interface: "org.freedesktop.Notifications",
93+ Path: "/org/freedesktop/Notifications",
94+ Name: "org.freedesktop.Notifications",
95+}
96+
97+/*****************************************************************
98+ * RawNotifications
99+ */
100+
101+// convenience type for the (uint32, string) ActionInvoked signal data
102+type RawActionReply struct {
103+ NotificationId uint32
104+ ActionId string
105+}
106+
107+// a raw notification provides a low-level interface to the f.d.o. dbus
108+// notifications api
109+type RawNotifications struct {
110+ bus bus.Endpoint
111+ log logger.Logger
112+}
113+
114+// Raw returns a new RawNotifications connected to the provided bus.Bus
115+func Raw(bt bus.Bus, log logger.Logger) (*RawNotifications, error) {
116+ endp, err := bt.Connect(BusAddress, log)
117+ if err != nil {
118+ return nil, err
119+ }
120+
121+ return &RawNotifications{endp, log}, nil
122+}
123+
124+/*
125+ public methods
126+*/
127+
128+// Notify fires a notification
129+func (raw *RawNotifications) Notify(
130+ app_name string, reuse_id uint32,
131+ icon, summary, body string,
132+ actions []string, hints map[string]*dbus.Variant,
133+ timeout int32) (uint32, error) {
134+ // that's a long argument list! Take a breather.
135+ //
136+ rv, err := raw.bus.Call("Notify", app_name, reuse_id, icon,
137+ summary, body, actions, hints, timeout)
138+ if err != nil {
139+ return 0, err
140+ }
141+ return rv.(uint32), nil
142+}
143+
144+// WatchActions listens for ActionInvoked signals from the notification daemon
145+// and sends them over the channel provided
146+func (raw *RawNotifications) WatchActions() (<-chan RawActionReply, error) {
147+ ch := make(chan RawActionReply)
148+ err := raw.bus.WatchSignal("ActionInvoked",
149+ func(ns ...interface{}) {
150+ ch <- RawActionReply{ns[0].(uint32), ns[1].(string)}
151+ }, func() { close(ch) })
152+ if err != nil {
153+ raw.log.Debugf("Failed to set up the watch: %s", err)
154+ return nil, err
155+ }
156+ return ch, nil
157+}
158
159=== added file 'notifications/raw_test.go'
160--- notifications/raw_test.go 1970-01-01 00:00:00 +0000
161+++ notifications/raw_test.go 2014-01-22 12:27:44 +0000
162@@ -0,0 +1,94 @@
163+/*
164+ Copyright 2013-2014 Canonical Ltd.
165+
166+ This program is free software: you can redistribute it and/or modify it
167+ under the terms of the GNU General Public License version 3, as published
168+ by the Free Software Foundation.
169+
170+ This program is distributed in the hope that it will be useful, but
171+ WITHOUT ANY WARRANTY; without even the implied warranties of
172+ MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
173+ PURPOSE. See the GNU General Public License for more details.
174+
175+ You should have received a copy of the GNU General Public License along
176+ with this program. If not, see <http://www.gnu.org/licenses/>.
177+*/
178+
179+// Package notifications wraps a couple of Notifications's DBus API points:
180+// the org.freedesktop.Notifications.Notify call, and listening for the
181+// ActionInvoked signal.
182+package notifications
183+
184+import (
185+ "io/ioutil"
186+ . "launchpad.net/gocheck"
187+ testibus "launchpad.net/ubuntu-push/bus/testing"
188+ "launchpad.net/ubuntu-push/logger"
189+ "launchpad.net/ubuntu-push/testing/condition"
190+ "testing"
191+ "time"
192+)
193+
194+// hook up gocheck
195+func TestRaw(t *testing.T) { TestingT(t) }
196+
197+type RawSuite struct{}
198+
199+var _ = Suite(&RawSuite{})
200+
201+var nullog = logger.NewSimpleLogger(ioutil.Discard, "error")
202+
203+func (s *RawSuite) TestConnects(c *C) {
204+ bus := testibus.NewTestingBus(condition.Work(false), condition.Work(false))
205+ _, err := Raw(bus, nullog)
206+ c.Check(err, NotNil)
207+ bus = testibus.NewTestingBus(condition.Work(true), condition.Work(false))
208+ _, err = Raw(bus, nullog)
209+ c.Check(err, IsNil)
210+}
211+
212+func (s *RawSuite) TestNotifies(c *C) {
213+ bus := testibus.NewTestingBus(condition.Work(true), condition.Work(true),
214+ uint32(1))
215+ raw, err := Raw(bus, nullog)
216+ c.Assert(err, IsNil)
217+ nid, err := raw.Notify("", 0, "", "", "", nil, nil, 0)
218+ c.Check(err, IsNil)
219+ c.Check(nid, Equals, uint32(1))
220+}
221+
222+func (s *RawSuite) TestNotifiesFails(c *C) {
223+ bus := testibus.NewTestingBus(condition.Work(true), condition.Work(false))
224+ raw, err := Raw(bus, nullog)
225+ c.Assert(err, IsNil)
226+ _, err = raw.Notify("", 0, "", "", "", nil, nil, 0)
227+ c.Check(err, NotNil)
228+}
229+
230+func (s *RawSuite) TestWatchActions(c *C) {
231+ bus := testibus.NewMultiValuedTestingBus(condition.Work(true), condition.Work(true),
232+ []interface{}{uint32(1), "hello"})
233+ raw, err := Raw(bus, nullog)
234+ c.Assert(err, IsNil)
235+ ch, err := raw.WatchActions()
236+ c.Assert(err, IsNil)
237+ // check we get the right action reply
238+ select {
239+ case p := <-ch:
240+ c.Check(p.NotificationId, Equals, uint32(1))
241+ c.Check(p.ActionId, Equals, "hello")
242+ case <-time.NewTimer(time.Second / 10).C:
243+ c.Error("timed out?")
244+ }
245+ // and that the channel is closed if/when the watch fails
246+ _, ok := <-ch
247+ c.Check(ok, Equals, false)
248+}
249+
250+func (s *RawSuite) TestWatchActionsFails(c *C) {
251+ bus := testibus.NewTestingBus(condition.Work(true), condition.Work(false))
252+ raw, err := Raw(bus, nullog)
253+ c.Assert(err, IsNil)
254+ _, err = raw.WatchActions()
255+ c.Check(err, NotNil)
256+}

Subscribers

People subscribed via source and target branches