Debian Codegraph @

bus/notifications/raw_test.go

raw
1 /*
2 Copyright 2013-2014 Canonical Ltd.
3
4 This program is free software: you can redistribute it and/or modify it
5 under the terms of the GNU General Public License version 3, as published
6 by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranties of
10 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along
14 with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 // Package notifications wraps a couple of Notifications's DBus API points:
18 // the org.freedesktop.Notifications.Notify call, and listening for the
19 // ActionInvoked signal.
20 package notifications
21
22 import (
23 "encoding/json"
24 "testing"
25 "time"
26
27 "github.com/godbus/dbus/v5"
28 . "gopkg.in/check.v1"
29
30 "gitlab.com/ubports/development/core/lomiri-push-service/bus"
31 testibus "gitlab.com/ubports/development/core/lomiri-push-service/bus/testing"
32 "gitlab.com/ubports/development/core/lomiri-push-service/click"
33 clickhelp "gitlab.com/ubports/development/core/lomiri-push-service/click/testing"
34 "gitlab.com/ubports/development/core/lomiri-push-service/launch_helper"
35 helpers "gitlab.com/ubports/development/core/lomiri-push-service/testing"
36 "gitlab.com/ubports/development/core/lomiri-push-service/testing/condition"
37 )
38
39 // hook up gocheck
40 func TestRaw(t *testing.T) { TestingT(t) }
41
42 type RawSuite struct {
43 log *helpers.TestLogger
44 app *click.AppId
45 snd *mockSound
46 }
47
48 type mockSound struct{}
49
50 func (m *mockSound) Present(app *click.AppId, nid string, notification *launch_helper.Notification) bool {
51 return false
52 }
53 func (m *mockSound) GetSound(app *click.AppId, nid string, notification *launch_helper.Notification) string {
54 return "/usr/share/sounds/ubuntu/notifications/Xylo.ogg"
55 }
56
57 func (s *RawSuite) SetUpTest(c *C) {
58 s.log = helpers.NewTestLogger(c, "debug")
59 s.app = clickhelp.MustParseAppId("com.example.test_test-app_0")
60 s.snd = &mockSound{}
61 }
62
63 var _ = Suite(&RawSuite{})
64
65 func (s *RawSuite) TestNotifies(c *C) {
66 endp := testibus.NewTestingEndpoint(nil, condition.Work(true), uint32(1))
67 raw := Raw(endp, s.log, nil)
68 nid, err := raw.Notify("", 0, "", "", "", nil, nil, 0)
69 c.Check(err, IsNil)
70 c.Check(nid, Equals, uint32(1))
71 }
72
73 func (s *RawSuite) TestNotifiesFails(c *C) {
74 endp := testibus.NewTestingEndpoint(nil, condition.Work(false))
75 raw := Raw(endp, s.log, nil)
76 _, err := raw.Notify("", 0, "", "", "", nil, nil, 0)
77 c.Check(err, NotNil)
78 }
79
80 func (s *RawSuite) TestNotifyFailsIfNoBus(c *C) {
81 raw := Raw(nil, s.log, nil)
82 _, err := raw.Notify("", 0, "", "", "", nil, nil, 0)
83 c.Check(err, ErrorMatches, `.*unconfigured .*`)
84 }
85
86 func (s *RawSuite) TestNotifiesFailsWeirdly(c *C) {
87 endp := testibus.NewMultiValuedTestingEndpoint(nil, condition.Work(true), []interface{}{1, 2})
88 raw := Raw(endp, s.log, nil)
89 _, err := raw.Notify("", 0, "", "", "", nil, nil, 0)
90 c.Check(err, NotNil)
91 }
92
93 func (s *RawSuite) TestWatchActions(c *C) {
94 act := &RawAction{
95 App: clickhelp.MustParseAppId("_foo"),
96 Nid: "notif-id",
97 ActionId: 1,
98 Action: "hello",
99 RawId: 0,
100 }
101 jAct, err := json.Marshal(act)
102 c.Assert(err, IsNil)
103 endp := testibus.NewMultiValuedTestingEndpoint(nil, condition.Work(true),
104 []interface{}{uint32(1), string(jAct)})
105 raw := Raw(endp, s.log, nil)
106 ch, err := raw.WatchActions()
107 c.Assert(err, IsNil)
108 // check we get the right action reply
109 act.RawId = 1 // checking the RawId is overwritten with the one in the ActionInvoked
110 select {
111 case p := <-ch:
112 c.Check(p, DeepEquals, act)
113 case <-time.NewTimer(time.Second / 10).C:
114 c.Error("timed out?")
115 }
116 // and that the channel is closed if/when the watch fails
117 _, ok := <-ch
118 c.Check(ok, Equals, false)
119 }
120
121 type tst struct {
122 errstr string
123 endp bus.Endpoint
124 works bool
125 src chan []interface{}
126 }
127
128 func (s *RawSuite) TestWatchActionsToleratesDBusWeirdness(c *C) {
129 X := func(errstr string, args ...interface{}) tst {
130 endp := testibus.NewMultiValuedTestingEndpoint(nil, condition.Work(true))
131 src := make(chan []interface{}, 1)
132 testibus.SetWatchSource(endp, "ActionInvoked", src)
133 src <- args
134 return tst{errstr, endp, errstr == "", src}
135 }
136
137 ts := []tst{
138 X("delivered 0 things instead of 2"),
139 X("delivered 1 things instead of 2", 2),
140 X("1st param not a uint32", 1, "foo"),
141 X("2nd param not a string", uint32(1), nil),
142 X("2nd param not a json-encoded RawAction", uint32(1), ``),
143 X("", uint32(1), `{}`),
144 }
145
146 for i, t := range ts {
147 raw := Raw(t.endp, s.log, nil)
148 ch, err := raw.WatchActions()
149 c.Assert(err, IsNil)
150 select {
151 case p := <-ch:
152 if !t.works {
153 c.Errorf("got something on the channel! %#v (iter: %d)", p, i)
154 }
155 case <-time.After(time.Second / 10):
156 if t.works {
157 c.Errorf("failed to get something on the channel (iter: %d)", i)
158 }
159 }
160 c.Check(s.log.Captured(), Matches, `(?ms).*`+t.errstr+`.*`)
161 s.log.ResetCapture()
162 close(t.src)
163 }
164
165 }
166
167 func (s *RawSuite) TestWatchActionsFails(c *C) {
168 endp := testibus.NewTestingEndpoint(nil, condition.Work(false))
169 raw := Raw(endp, s.log, nil)
170 _, err := raw.WatchActions()
171 c.Check(err, NotNil)
172 }
173
174 func (s *RawSuite) TestPresentNotifies(c *C) {
175 endp := testibus.NewTestingEndpoint(nil, condition.Work(true), uint32(1))
176 raw := Raw(endp, s.log, nil)
177 worked := raw.Present(s.app, "notifId", &launch_helper.Notification{Card: &launch_helper.Card{Summary: "summary", Popup: true}})
178 c.Check(worked, Equals, true)
179 }
180
181 func (s *RawSuite) TestPresentWithSoundNotifies(c *C) {
182 endp := testibus.NewTestingEndpoint(nil, condition.Work(true), uint32(1))
183 raw := &RawNotifications{bus: endp, log: s.log, sound: s.snd}
184 id := "notifId"
185 notification := &launch_helper.Notification{
186 Card: &launch_helper.Card{
187 Summary: "summary", Popup: true,
188 },
189 RawSound: json.RawMessage(`true`),
190 }
191 worked := raw.Present(s.app, id, notification)
192 sound := s.snd.GetSound(s.app, id, notification)
193 c.Check(worked, Equals, true)
194 c.Check(s.log.Captured(), Matches, `(?m).*notification will play sound: `+sound+`.*`)
195 }
196
197 func (s *RawSuite) TestPresentOneAction(c *C) {
198 endp := testibus.NewTestingEndpoint(nil, condition.Work(true), uint32(1))
199 raw := Raw(endp, s.log, nil)
200 worked := raw.Present(s.app, "notifId", &launch_helper.Notification{Card: &launch_helper.Card{Summary: "summary", Popup: true, Actions: []string{"Yes"}}})
201 c.Check(worked, Equals, true)
202 callArgs := testibus.GetCallArgs(endp)
203 c.Assert(callArgs, HasLen, 1)
204 c.Assert(callArgs[0].Member, Equals, "Notify")
205 c.Assert(len(callArgs[0].Args), Equals, 8)
206 actions, ok := callArgs[0].Args[5].([]string)
207 c.Assert(ok, Equals, true)
208 c.Assert(actions, HasLen, 2)
209 c.Check(actions[0], Equals, `{"app":"com.example.test_test-app_0","act":"Yes","nid":"notifId"}`)
210 c.Check(actions[1], Equals, "Yes")
211 hints, ok := callArgs[0].Args[6].(map[string]dbus.Variant)
212 c.Assert(ok, Equals, true)
213 // with one action, there should be 2 hints set:
214 c.Assert(hints, HasLen, 2)
215 c.Check(hints["x-lomiri-switch-to-application"].Value(), NotNil)
216 c.Check(hints["x-lomiri-secondary-icon"].Value(), NotNil)
217 c.Check(hints["x-lomiri-snap-decisions"].Value(), IsNil)
218 c.Check(hints["x-lomiri-private-button-tint"].Value(), IsNil)
219 }
220
221 func (s *RawSuite) TestPresentTwoActions(c *C) {
222 endp := testibus.NewTestingEndpoint(nil, condition.Work(true), uint32(1))
223 raw := Raw(endp, s.log, nil)
224 worked := raw.Present(s.app, "notifId", &launch_helper.Notification{Card: &launch_helper.Card{Summary: "summary", Popup: true, Actions: []string{"Yes", "No"}}})
225 c.Check(worked, Equals, true)
226 callArgs := testibus.GetCallArgs(endp)
227 c.Assert(callArgs, HasLen, 1)
228 c.Assert(callArgs[0].Member, Equals, "Notify")
229 c.Assert(len(callArgs[0].Args), Equals, 8)
230 actions, ok := callArgs[0].Args[5].([]string)
231 c.Assert(ok, Equals, true)
232 c.Assert(actions, HasLen, 2)
233 c.Check(actions[0], Equals, `{"app":"com.example.test_test-app_0","act":"Yes","nid":"notifId"}`)
234 c.Check(actions[1], Equals, "Yes")
235 // note that the rest are ignored.
236 hints, ok := callArgs[0].Args[6].(map[string]dbus.Variant)
237 c.Assert(ok, Equals, true)
238 c.Assert(hints, HasLen, 2)
239 c.Check(hints["x-lomiri-switch-to-application"].Value(), NotNil)
240 c.Check(hints["x-lomiri-secondary-icon"].Value(), NotNil)
241 }
242
243 func (s *RawSuite) TestPresentUsesSymbolic(c *C) {
244 endp := testibus.NewTestingEndpoint(nil, condition.Work(true), uint32(1))
245 raw := Raw(endp, s.log, nil)
246 worked := raw.Present(s.app, "notifId", &launch_helper.Notification{Card: &launch_helper.Card{Summary: "summary", Popup: true}})
247 c.Assert(worked, Equals, true)
248 callArgs := testibus.GetCallArgs(endp)
249 c.Assert(callArgs, HasLen, 1)
250 c.Assert(callArgs[0].Args, HasLen, 8)
251 hints, ok := callArgs[0].Args[6].(map[string]dbus.Variant)
252 c.Assert(ok, Equals, true)
253 var sec string
254 hints["x-lomiri-secondary-icon"].Store(&sec)
255 c.Check(sec, Equals, "-symbolic")
256 }
257
258 func (s *RawSuite) TestPresentNoNotificationPanics(c *C) {
259 endp := testibus.NewTestingEndpoint(nil, condition.Work(true), uint32(1))
260 raw := Raw(endp, s.log, nil)
261 c.Check(func() { raw.Present(s.app, "notifId", nil) }, Panics, `please check notification is not nil before calling present`)
262 }
263
264 func (s *RawSuite) TestPresentNoCardDoesNotNotify(c *C) {
265 endp := testibus.NewTestingEndpoint(nil, condition.Work(true), uint32(1))
266 raw := Raw(endp, s.log, nil)
267 worked := raw.Present(s.app, "notifId", &launch_helper.Notification{})
268 c.Check(worked, Equals, false)
269 }
270
271 func (s *RawSuite) TestPresentNoSummaryDoesNotNotify(c *C) {
272 endp := testibus.NewTestingEndpoint(nil, condition.Work(true), uint32(1))
273 raw := Raw(endp, s.log, nil)
274 worked := raw.Present(s.app, "notifId", &launch_helper.Notification{Card: &launch_helper.Card{}})
275 c.Check(worked, Equals, false)
276 }
277
278 func (s *RawSuite) TestPresentNoPopupNoNotify(c *C) {
279 endp := testibus.NewTestingEndpoint(nil, condition.Work(true), uint32(1))
280 raw := Raw(endp, s.log, nil)
281 worked := raw.Present(s.app, "notifId", &launch_helper.Notification{Card: &launch_helper.Card{Summary: "summary"}})
282 c.Check(worked, Equals, false)
283 }