Merge lp:~chipaca/ubuntu-push/use-of-accounts into lp:ubuntu-push/automatic

Proposed by John Lenton
Status: Merged
Approved by: John Lenton
Approved revision: 371
Merged at revision: 369
Proposed branch: lp:~chipaca/ubuntu-push/use-of-accounts
Merge into: lp:ubuntu-push/automatic
Prerequisite: lp:~chipaca/ubuntu-push/com.ubuntu.touch.AccountsService
Diff against target: 411 lines (+124/-15)
6 files modified
bus/haptic/haptic.go (+9/-2)
bus/haptic/haptic_test.go (+35/-6)
client/service/postal.go (+12/-2)
client/service/postal_test.go (+9/-0)
sounds/sounds.go (+15/-2)
sounds/sounds_test.go (+44/-3)
To merge this branch: bzr merge lp:~chipaca/ubuntu-push/use-of-accounts
Reviewer Review Type Date Requested Status
Samuele Pedroni Approve
Review via email: mp+251945@code.launchpad.net

Commit message

use accounts' settings from sound and haptic.

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

should the accounts mocking be shared?

Revision history for this message
Samuele Pedroni (pedronis) :
review: Approve
371. By John Lenton

Merged com.ubuntu.touch.AccountsService into use-of-accounts.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'bus/haptic/haptic.go'
2--- bus/haptic/haptic.go 2014-08-08 01:07:38 +0000
3+++ bus/haptic/haptic.go 2015-03-05 16:21:53 +0000
4@@ -20,6 +20,7 @@
5
6 import (
7 "launchpad.net/ubuntu-push/bus"
8+ "launchpad.net/ubuntu-push/bus/accounts"
9 "launchpad.net/ubuntu-push/click"
10 "launchpad.net/ubuntu-push/launch_helper"
11 "launchpad.net/ubuntu-push/logger"
12@@ -36,12 +37,13 @@
13 type Haptic struct {
14 bus bus.Endpoint
15 log logger.Logger
16+ acc accounts.Accounts
17 fallback *launch_helper.Vibration
18 }
19
20 // New returns a new Haptic that'll use the provided bus.Endpoint
21-func New(endp bus.Endpoint, log logger.Logger, fallback *launch_helper.Vibration) *Haptic {
22- return &Haptic{endp, log, fallback}
23+func New(endp bus.Endpoint, log logger.Logger, acc accounts.Accounts, fallback *launch_helper.Vibration) *Haptic {
24+ return &Haptic{endp, log, acc, fallback}
25 }
26
27 // Present presents the notification via a vibrate pattern
28@@ -50,6 +52,11 @@
29 panic("please check notification is not nil before calling present")
30 }
31
32+ if !haptic.acc.Vibrate() {
33+ haptic.log.Debugf("[%s] vibrate disabled by user.", nid)
34+ return false
35+ }
36+
37 vib := notification.Vibration(haptic.fallback)
38 if vib == nil {
39 haptic.log.Debugf("[%s] notification has no Vibrate.", nid)
40
41=== modified file 'bus/haptic/haptic_test.go'
42--- bus/haptic/haptic_test.go 2014-08-08 01:07:38 +0000
43+++ bus/haptic/haptic_test.go 2015-03-05 16:21:53 +0000
44@@ -35,20 +35,36 @@
45 type hapticSuite struct {
46 log *helpers.TestLogger
47 app *click.AppId
48-}
49+ acc *mockAccounts
50+}
51+
52+type mockAccounts struct {
53+ vib bool
54+ sil bool
55+ snd string
56+ err error
57+}
58+
59+func (m *mockAccounts) Start() error { return m.err }
60+func (m *mockAccounts) Cancel() error { return m.err }
61+func (m *mockAccounts) SilentMode() bool { return m.sil }
62+func (m *mockAccounts) Vibrate() bool { return m.vib }
63+func (m *mockAccounts) MessageSoundFile() string { return m.snd }
64+func (m *mockAccounts) String() string { return "<mockAccounts>" }
65
66 var _ = Suite(&hapticSuite{})
67
68 func (hs *hapticSuite) SetUpTest(c *C) {
69 hs.log = helpers.NewTestLogger(c, "debug")
70 hs.app = clickhelp.MustParseAppId("com.example.test_test-app_0")
71+ hs.acc = &mockAccounts{true, false, "xyzzy", nil}
72 }
73
74 // checks that Present() actually calls VibratePattern
75 func (hs *hapticSuite) TestPresentPresents(c *C) {
76 endp := testibus.NewTestingEndpoint(nil, condition.Work(true))
77
78- ec := New(endp, hs.log, nil)
79+ ec := New(endp, hs.log, hs.acc, nil)
80 notif := launch_helper.Notification{RawVibration: json.RawMessage(`{"pattern": [200, 100], "repeat": 2}`)}
81 c.Check(ec.Present(hs.app, "nid", &notif), Equals, true)
82 callArgs := testibus.GetCallArgs(endp)
83@@ -61,7 +77,7 @@
84 func (hs *hapticSuite) TestPresentDefaultsRepeatTo1(c *C) {
85 endp := testibus.NewTestingEndpoint(nil, condition.Work(true))
86
87- ec := New(endp, hs.log, nil)
88+ ec := New(endp, hs.log, hs.acc, nil)
89 // note: no Repeat:
90 notif := launch_helper.Notification{RawVibration: json.RawMessage(`{"pattern": [200, 100]}`)}
91 c.Check(ec.Present(hs.app, "nid", &notif), Equals, true)
92@@ -76,7 +92,7 @@
93 func (hs *hapticSuite) TestSkipIfMissing(c *C) {
94 endp := testibus.NewTestingEndpoint(nil, condition.Work(true))
95
96- ec := New(endp, hs.log, nil)
97+ ec := New(endp, hs.log, hs.acc, nil)
98 // no Vibration in the notificaton
99 c.Check(ec.Present(hs.app, "", &launch_helper.Notification{}), Equals, false)
100 // empty Vibration
101@@ -85,11 +101,24 @@
102 c.Check(ec.Present(hs.app, "", &launch_helper.Notification{RawVibration: json.RawMessage(`{}`)}), Equals, false)
103 }
104
105+// check that Present() does not present if the accounts' Vibrate() returns false
106+func (hs *hapticSuite) TestPresentSkipsIfVibrateDisabled(c *C) {
107+ endp := testibus.NewTestingEndpoint(nil, condition.Work(true))
108+ fallback := &launch_helper.Vibration{Pattern: []uint32{200, 100}, Repeat: 2}
109+
110+ ec := New(endp, hs.log, hs.acc, fallback)
111+ notif := launch_helper.Notification{RawVibration: json.RawMessage(`true`)}
112+ c.Assert(ec.Present(hs.app, "nid", &notif), Equals, true)
113+ // ok!
114+ hs.acc.vib = false
115+ c.Check(ec.Present(hs.app, "nid", &notif), Equals, false)
116+}
117+
118 // check that Present() panics if the notification is nil
119 func (hs *hapticSuite) TestPanicsIfNil(c *C) {
120 endp := testibus.NewTestingEndpoint(nil, condition.Work(true))
121
122- ec := New(endp, hs.log, nil)
123+ ec := New(endp, hs.log, hs.acc, nil)
124 // no notification at all
125 c.Check(func() { ec.Present(hs.app, "", nil) }, Panics, `please check notification is not nil before calling present`)
126 }
127@@ -99,7 +128,7 @@
128 endp := testibus.NewTestingEndpoint(nil, condition.Work(true))
129 fallback := &launch_helper.Vibration{Pattern: []uint32{200, 100}, Repeat: 2}
130
131- ec := New(endp, hs.log, fallback)
132+ ec := New(endp, hs.log, hs.acc, fallback)
133 notif := launch_helper.Notification{RawVibration: json.RawMessage(`false`)}
134 c.Check(ec.Present(hs.app, "nid", &notif), Equals, false)
135 notif = launch_helper.Notification{RawVibration: json.RawMessage(`true`)}
136
137=== modified file 'client/service/postal.go'
138--- client/service/postal.go 2015-01-21 17:21:42 +0000
139+++ client/service/postal.go 2015-03-05 16:21:53 +0000
140@@ -24,6 +24,7 @@
141 "code.google.com/p/go-uuid/uuid"
142
143 "launchpad.net/ubuntu-push/bus"
144+ "launchpad.net/ubuntu-push/bus/accounts"
145 "launchpad.net/ubuntu-push/bus/emblemcounter"
146 "launchpad.net/ubuntu-push/bus/haptic"
147 "launchpad.net/ubuntu-push/bus/notifications"
148@@ -75,6 +76,7 @@
149 // the endpoints are only exposed for testing from client
150 // XXX: uncouple some more so this isn't necessary
151 EmblemCounterEndp bus.Endpoint
152+ AccountsEndp bus.Endpoint
153 HapticEndp bus.Endpoint
154 NotificationsEndp bus.Endpoint
155 UnityGreeterEndp bus.Endpoint
156@@ -82,6 +84,7 @@
157 // presenters:
158 Presenters []Presenter
159 emblemCounter *emblemcounter.EmblemCounter
160+ accounts accounts.Accounts
161 haptic *haptic.Haptic
162 notifications *notifications.RawNotifications
163 sound *sounds.Sound
164@@ -117,6 +120,7 @@
165 svc.fallbackSound = setup.FallbackSound
166 svc.NotificationsEndp = bus.SessionBus.Endpoint(notifications.BusAddress, log)
167 svc.EmblemCounterEndp = bus.SessionBus.Endpoint(emblemcounter.BusAddress, log)
168+ svc.AccountsEndp = bus.SystemBus.Endpoint(accounts.BusAddress, log)
169 svc.HapticEndp = bus.SessionBus.Endpoint(haptic.BusAddress, log)
170 svc.UnityGreeterEndp = bus.SessionBus.Endpoint(unitygreeter.BusAddress, log)
171 svc.WindowStackEndp = bus.SessionBus.Endpoint(windowstack.BusAddress, log)
172@@ -158,8 +162,13 @@
173 svc.urlDispatcher = urldispatcher.New(svc.Log)
174 svc.notifications = notifications.Raw(svc.NotificationsEndp, svc.Log)
175 svc.emblemCounter = emblemcounter.New(svc.EmblemCounterEndp, svc.Log)
176- svc.haptic = haptic.New(svc.HapticEndp, svc.Log, svc.fallbackVibration)
177- svc.sound = sounds.New(svc.Log, svc.fallbackSound)
178+ svc.accounts = accounts.New(svc.AccountsEndp, svc.Log)
179+ err = svc.accounts.Start()
180+ if err != nil {
181+ return err
182+ }
183+ svc.haptic = haptic.New(svc.HapticEndp, svc.Log, svc.accounts, svc.fallbackVibration)
184+ svc.sound = sounds.New(svc.Log, svc.accounts, svc.fallbackSound)
185 svc.messagingMenu = messaging.New(svc.Log)
186 svc.Presenters = []Presenter{
187 svc.notifications,
188@@ -228,6 +237,7 @@
189 }{
190 {"notifications", svc.NotificationsEndp},
191 {"emblemcounter", svc.EmblemCounterEndp},
192+ {"accounts", svc.AccountsEndp},
193 {"haptic", svc.HapticEndp},
194 {"unitygreeter", svc.UnityGreeterEndp},
195 {"windowstack", svc.WindowStackEndp},
196
197=== modified file 'client/service/postal_test.go'
198--- client/service/postal_test.go 2015-02-24 14:10:05 +0000
199+++ client/service/postal_test.go 2015-03-05 16:21:53 +0000
200@@ -169,6 +169,8 @@
201 hapticBus bus.Endpoint
202 unityGreeterBus bus.Endpoint
203 winStackBus bus.Endpoint
204+ accountsBus bus.Endpoint
205+ accountsCh chan []interface{}
206 fakeLauncher *fakeHelperLauncher
207 getTempDir func(string) (string, error)
208 oldIsBlisted func(*click.AppId) bool
209@@ -194,6 +196,7 @@
210 ps.bus = testibus.NewTestingEndpoint(condition.Work(true), condition.Work(true))
211 ps.notifBus = testibus.NewTestingEndpoint(condition.Work(true), condition.Work(true))
212 ps.counterBus = testibus.NewTestingEndpoint(condition.Work(true), condition.Work(true))
213+ ps.accountsBus = testibus.NewTestingEndpoint(condition.Work(true), condition.Work(true), map[string]dbus.Variant{"IncomingMessageVibrate": dbus.Variant{true}})
214 ps.hapticBus = testibus.NewTestingEndpoint(condition.Work(true), condition.Work(true))
215 ps.unityGreeterBus = testibus.NewTestingEndpoint(condition.Work(true), condition.Work(true), false)
216 ps.winStackBus = testibus.NewTestingEndpoint(condition.Work(true), condition.Work(true), []windowstack.WindowsInfo{})
217@@ -206,11 +209,15 @@
218 tmpDir := filepath.Join(d, pkgName)
219 return tmpDir, os.MkdirAll(tmpDir, 0700)
220 }
221+
222+ ps.accountsCh = make(chan []interface{})
223+ testibus.SetWatchSource(ps.accountsBus, "PropertiesChanged", ps.accountsCh)
224 }
225
226 func (ps *postalSuite) TearDownTest(c *C) {
227 isBlacklisted = ps.oldIsBlisted
228 launch_helper.GetTempDir = ps.getTempDir
229+ close(ps.accountsCh)
230 }
231
232 func (ts *trivialPostalSuite) SetUpTest(c *C) {
233@@ -227,6 +234,7 @@
234 pst.Bus = ps.bus
235 pst.NotificationsEndp = ps.notifBus
236 pst.EmblemCounterEndp = ps.counterBus
237+ pst.AccountsEndp = ps.accountsBus
238 pst.HapticEndp = ps.hapticBus
239 pst.UnityGreeterEndp = ps.unityGreeterBus
240 pst.WindowStackEndp = ps.winStackBus
241@@ -544,6 +552,7 @@
242 svc := NewPostalService(ps.cfg, ps.log)
243 svc.Bus = endp
244 svc.EmblemCounterEndp = endp
245+ svc.AccountsEndp = ps.accountsBus
246 svc.HapticEndp = endp
247 svc.NotificationsEndp = endp
248 svc.UnityGreeterEndp = ps.unityGreeterBus
249
250=== modified file 'sounds/sounds.go'
251--- sounds/sounds.go 2014-08-08 09:03:42 +0000
252+++ sounds/sounds.go 2015-03-05 16:21:53 +0000
253@@ -25,6 +25,7 @@
254
255 "launchpad.net/go-xdg/v0"
256
257+ "launchpad.net/ubuntu-push/bus/accounts"
258 "launchpad.net/ubuntu-push/click"
259 "launchpad.net/ubuntu-push/launch_helper"
260 "launchpad.net/ubuntu-push/logger"
261@@ -33,15 +34,17 @@
262 type Sound struct {
263 player string
264 log logger.Logger
265+ acc accounts.Accounts
266 fallback string
267 dataDirs func() []string
268 dataFind func(string) (string, error)
269 }
270
271-func New(log logger.Logger, fallback string) *Sound {
272+func New(log logger.Logger, acc accounts.Accounts, fallback string) *Sound {
273 return &Sound{
274 player: "paplay",
275 log: log,
276+ acc: acc,
277 fallback: fallback,
278 dataDirs: xdg.Data.Dirs,
279 dataFind: xdg.Data.Find,
280@@ -53,7 +56,17 @@
281 panic("please check notification is not nil before calling present")
282 }
283
284- sound := notification.Sound(snd.fallback)
285+ if snd.acc.SilentMode() {
286+ snd.log.Debugf("[%s] no sounds: silent mode on.", nid)
287+ return false
288+ }
289+
290+ fallback := snd.acc.MessageSoundFile()
291+ if fallback == "" {
292+ fallback = snd.fallback
293+ }
294+
295+ sound := notification.Sound(fallback)
296 if sound == "" {
297 snd.log.Debugf("[%s] notification has no Sound: %#v", nid, sound)
298 return false
299
300=== modified file 'sounds/sounds_test.go'
301--- sounds/sounds_test.go 2014-08-08 09:03:42 +0000
302+++ sounds/sounds_test.go 2015-03-05 16:21:53 +0000
303@@ -36,25 +36,42 @@
304 type soundsSuite struct {
305 log *helpers.TestLogger
306 app *click.AppId
307+ acc *mockAccounts
308 }
309
310 var _ = Suite(&soundsSuite{})
311
312+type mockAccounts struct {
313+ vib bool
314+ sil bool
315+ snd string
316+ err error
317+}
318+
319+func (m *mockAccounts) Start() error { return m.err }
320+func (m *mockAccounts) Cancel() error { return m.err }
321+func (m *mockAccounts) SilentMode() bool { return m.sil }
322+func (m *mockAccounts) Vibrate() bool { return m.vib }
323+func (m *mockAccounts) MessageSoundFile() string { return m.snd }
324+func (m *mockAccounts) String() string { return "<mockAccounts>" }
325+
326 func (ss *soundsSuite) SetUpTest(c *C) {
327 ss.log = helpers.NewTestLogger(c, "debug")
328 ss.app = clickhelp.MustParseAppId("com.example.test_test_0")
329+ ss.acc = &mockAccounts{true, false, "", nil}
330 }
331
332 func (ss *soundsSuite) TestNew(c *C) {
333- s := New(ss.log, "foo")
334+ s := New(ss.log, ss.acc, "foo")
335 c.Check(s.log, Equals, ss.log)
336 c.Check(s.player, Equals, "paplay")
337 c.Check(s.fallback, Equals, "foo")
338+ c.Check(s.acc, Equals, ss.acc)
339 }
340
341 func (ss *soundsSuite) TestPresent(c *C) {
342 s := &Sound{
343- player: "echo", log: ss.log,
344+ player: "echo", log: ss.log, acc: ss.acc,
345 dataFind: func(s string) (string, error) { return s, nil },
346 }
347
348@@ -65,7 +82,7 @@
349
350 func (ss *soundsSuite) TestPresentSimple(c *C) {
351 s := &Sound{
352- player: "echo", log: ss.log,
353+ player: "echo", log: ss.log, acc: ss.acc,
354 dataFind: func(s string) (string, error) { return s, nil },
355 fallback: "fallback",
356 }
357@@ -73,12 +90,18 @@
358 c.Check(s.Present(ss.app, "",
359 &launch_helper.Notification{RawSound: json.RawMessage(`true`)}), Equals, true)
360 c.Check(ss.log.Captured(), Matches, `(?sm).* playing sound com.example.test/fallback using echo`)
361+ ss.acc.snd = "from-prefs"
362+ ss.log.ResetCapture()
363+ c.Check(s.Present(ss.app, "",
364+ &launch_helper.Notification{RawSound: json.RawMessage(`true`)}), Equals, true)
365+ c.Check(ss.log.Captured(), Matches, `(?sm).* playing sound com.example.test/from-prefs using echo`)
366 }
367
368 func (ss *soundsSuite) TestPresentFails(c *C) {
369 s := &Sound{
370 player: "/",
371 log: ss.log,
372+ acc: ss.acc,
373 dataFind: func(string) (string, error) { return "", errors.New("nope") },
374 dataDirs: func() []string { return []string{""} },
375 }
376@@ -107,6 +130,7 @@
377 s := &Sound{
378 player: "/",
379 log: ss.log,
380+ acc: ss.acc,
381 dataFind: func(string) (string, error) { return "", errors.New("nope") },
382 dataDirs: func() []string { return []string{""} },
383 }
384@@ -120,6 +144,7 @@
385 s := &Sound{
386 player: "/",
387 log: ss.log,
388+ acc: ss.acc,
389 dataFind: func(string) (string, error) { return "", errors.New("nope") },
390 dataDirs: func() []string { return []string{""} },
391 }
392@@ -128,3 +153,19 @@
393 c.Check(err, IsNil)
394 c.Check(sound, Equals, "bar")
395 }
396+
397+func (ss *soundsSuite) TestSkipIfSilentMode(c *C) {
398+ s := &Sound{
399+ player: "echo",
400+ log: ss.log,
401+ acc: ss.acc,
402+ fallback: "fallback",
403+ dataFind: func(s string) (string, error) { return s, nil },
404+ }
405+
406+ c.Check(s.Present(ss.app, "",
407+ &launch_helper.Notification{RawSound: json.RawMessage(`true`)}), Equals, true)
408+ ss.acc.sil = true
409+ c.Check(s.Present(ss.app, "",
410+ &launch_helper.Notification{RawSound: json.RawMessage(`true`)}), Equals, false)
411+}

Subscribers

People subscribed via source and target branches