Debian Codegraph @

bus/accounts/accounts_test.go

raw
1 /*
2 Copyright 2013-2015 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 accounts
18
19 import (
20 "testing"
21
22 "github.com/godbus/dbus/v5"
23 . "gopkg.in/check.v1"
24
25 testibus "gitlab.com/ubports/development/core/lomiri-push-service/bus/testing"
26 helpers "gitlab.com/ubports/development/core/lomiri-push-service/testing"
27 "gitlab.com/ubports/development/core/lomiri-push-service/testing/condition"
28 )
29
30 // hook up gocheck
31 func TestAcc(t *testing.T) { TestingT(t) }
32
33 type AccSuite struct {
34 log *helpers.TestLogger
35 }
36
37 var _ = Suite(&AccSuite{})
38
39 type TestCancellable struct {
40 canceled bool
41 }
42
43 func (t *TestCancellable) Cancel() {
44 t.canceled = true
45 }
46
47 func (s *AccSuite) SetUpTest(c *C) {
48 s.log = helpers.NewTestLogger(c, "debug")
49 }
50
51 func (s *AccSuite) TestBusAddressPathUidLoaded(c *C) {
52 c.Check(BusAddress.Path, Matches, `.*\d+`)
53 }
54
55 func (s *AccSuite) TestStartReportsWatchError(c *C) {
56 endp := testibus.NewTestingEndpoint(nil, condition.Work(false))
57 a := New(endp, s.log).(*accounts)
58 c.Assert(a, NotNil)
59
60 err := a.Start()
61 c.Check(err, NotNil)
62 }
63
64 func (s *AccSuite) TestStartSetsCancellable(c *C) {
65 endp := testibus.NewTestingEndpoint(nil, condition.Work(true), true)
66 a := New(endp, s.log).(*accounts)
67 c.Assert(a, NotNil)
68
69 c.Check(a.cancellable, IsNil)
70 err := a.Start()
71 c.Check(err, IsNil)
72 c.Check(a.cancellable, NotNil)
73 a.Cancel()
74 }
75
76 func (s *AccSuite) TestStartPanicsIfCalledTwice(c *C) {
77 endp := testibus.NewTestingEndpoint(nil, condition.Work(true), true, true)
78 a := New(endp, s.log).(*accounts)
79 c.Assert(a, NotNil)
80
81 c.Check(a.cancellable, IsNil)
82 err := a.Start()
83 c.Check(err, IsNil)
84 c.Check(func() { a.startWatch() }, PanicMatches, `.* twice\?`)
85 a.Cancel()
86 }
87
88 func (s *AccSuite) TestUpdateCallsUpdaters(c *C) {
89 endp := testibus.NewTestingEndpoint(nil, condition.Work(true),
90 map[string]dbus.Variant{"x": dbus.MakeVariant("hello")})
91 a := New(endp, s.log).(*accounts)
92 c.Assert(a, NotNil)
93 var x dbus.Variant
94 a.updaters = map[string]func(dbus.Variant){
95 "x": func(v dbus.Variant) { x = v },
96 }
97 a.update()
98
99 c.Check(x.Value(), Equals, "hello")
100 }
101
102 func (s *AccSuite) TestUpdateSilentModeBails(c *C) {
103 a := New(nil, s.log).(*accounts)
104 a.updateSilentMode(dbus.MakeVariant("rubbish"))
105 c.Check(s.log.Captured(), Matches, `(?ms)ERROR SilentMode needed a bool.`)
106 }
107
108 func (s *AccSuite) TestUpdateSilentModeWorks(c *C) {
109 a := New(nil, s.log).(*accounts)
110 c.Check(a.silent, Equals, false)
111 a.updateSilentMode(dbus.MakeVariant(true))
112 c.Check(a.silent, Equals, true)
113 }
114
115 func (s *AccSuite) TestUpdateVibrateBails(c *C) {
116 a := New(nil, s.log).(*accounts)
117 a.updateVibrate(dbus.MakeVariant("rubbish"))
118 c.Check(s.log.Captured(), Matches, `(?ms)ERROR IncomingMessageVibrate needed a bool.`)
119 }
120
121 func (s *AccSuite) TestUpdateVibrateWorks(c *C) {
122 a := New(nil, s.log).(*accounts)
123 c.Check(a.vibrate, Equals, false)
124 a.updateVibrate(dbus.MakeVariant(true))
125 c.Check(a.vibrate, Equals, true)
126 }
127
128 func (s *AccSuite) TestUpdateVibrateSilentModeBails(c *C) {
129 a := New(nil, s.log).(*accounts)
130 a.updateVibrateSilentMode(dbus.MakeVariant("rubbish"))
131 c.Check(s.log.Captured(), Matches, `(?ms)ERROR IncomingMessageVibrateSilentMode needed a bool.`)
132 }
133
134 func (s *AccSuite) TestUpdateVibrateSilentModeWorks(c *C) {
135 a := New(nil, s.log).(*accounts)
136 c.Check(a.vibrateSilentMode, Equals, false)
137 a.updateVibrateSilentMode(dbus.MakeVariant(true))
138 c.Check(a.vibrateSilentMode, Equals, true)
139 }
140
141 func (s *AccSuite) TestUpdateMessageSoundBails(c *C) {
142 a := New(nil, s.log).(*accounts)
143 a.updateMessageSound(dbus.MakeVariant(42))
144 c.Check(s.log.Captured(), Matches, `(?ms)ERROR IncomingMessageSound needed a string.`)
145 }
146
147 func (s *AccSuite) TestUpdateMessageSoundWorks(c *C) {
148 a := New(nil, s.log).(*accounts)
149 c.Check(a.messageSound, Equals, "")
150 a.updateMessageSound(dbus.MakeVariant("xyzzy"))
151 c.Check(a.messageSound, Equals, "xyzzy")
152 }
153
154 func (s *AccSuite) TestUpdateMessageSoundPrunesXDG(c *C) {
155 a := New(nil, s.log).(*accounts)
156 a.updateMessageSound(dbus.MakeVariant("/usr/share/xyzzy"))
157 c.Check(a.messageSound, Equals, "xyzzy")
158 }
159
160 func (s *AccSuite) TestPropsHandler(c *C) {
161 endp := testibus.NewTestingEndpoint(nil, condition.Work(false))
162
163 // testing a series of bad args for propsHandler: none,
164 New(endp, s.log).(*accounts).propsHandler()
165 c.Check(s.log.Captured(), Matches, `(?ms).*ERROR PropertiesChanged delivered 0 things.*`)
166 s.log.ResetCapture()
167
168 // bad type for all,
169 New(endp, s.log).(*accounts).propsHandler(nil, nil, nil)
170 c.Check(s.log.Captured(), Matches, `(?ms).*ERROR PropertiesChanged 1st param not a string.*`)
171 s.log.ResetCapture()
172
173 // wrong interface,
174 New(endp, s.log).(*accounts).propsHandler("xyzzy", nil, nil)
175 c.Check(s.log.Captured(), Matches, `(?ms).*DEBUG PropertiesChanged for "xyzzy", ignoring\..*`)
176 s.log.ResetCapture()
177
178 // bad type for 2nd and 3rd,
179 New(endp, s.log).(*accounts).propsHandler(accountsSoundIface, nil, nil)
180 c.Check(s.log.Captured(), Matches, `(?ms).*ERROR PropertiesChanged 2nd param not a map.*`)
181 s.log.ResetCapture()
182
183 // not-seen-in-the-wild 'changed' argument (first non-error outcome),
184 New(endp, s.log).(*accounts).propsHandler(accountsSoundIface, map[interface{}]interface{}{"x": "y"}, nil)
185 // tracking the update() via the GetAll call it generates (which will fail because of the testibus of Work(false) above)
186 c.Check(s.log.Captured(), Matches, `(?ms).*INFO PropertiesChanged provided 'changed'.*ERROR when calling GetAll.*`)
187 s.log.ResetCapture()
188
189 // bad type for 3rd (with empty 2nd),
190 New(endp, s.log).(*accounts).propsHandler(accountsSoundIface, map[interface{}]interface{}{}, nil)
191 c.Check(s.log.Captured(), Matches, `(?ms).*ERROR PropertiesChanged 3rd param not a list of properties.*`)
192 s.log.ResetCapture()
193
194 // bad type for elements of 3rd,
195 New(endp, s.log).(*accounts).propsHandler(accountsSoundIface, map[interface{}]interface{}{}, []interface{}{42})
196 c.Check(s.log.Captured(), Matches, `(?ms).*ERROR PropertiesChanged 3rd param's only entry not a string.*`)
197 s.log.ResetCapture()
198
199 // empty 3rd (not an error; hard to test "do ),
200 New(endp, s.log).(*accounts).propsHandler(accountsSoundIface, map[interface{}]interface{}{}, []interface{}{})
201 c.Check(s.log.Captured(), Matches, `(?ms).*DEBUG PropertiesChanged 3rd param is empty.*`)
202 s.log.ResetCapture()
203
204 // more than one 2rd (also not an error; again looking at the GetAll failure to confirm update() got called),
205 New(endp, s.log).(*accounts).propsHandler(accountsSoundIface, map[interface{}]interface{}{}, []interface{}{"hi", "there"})
206 c.Check(s.log.Captured(), Matches, `(?ms).*INFO.* reverting to full update.*ERROR when calling GetAll.*`)
207 s.log.ResetCapture()
208
209 // bus trouble for a single entry in the 3rd,
210 New(endp, s.log).(*accounts).propsHandler(accountsSoundIface, map[interface{}]interface{}{}, []interface{}{"SilentMode"})
211 c.Check(s.log.Captured(), Matches, `(?ms).*ERROR when calling Get for SilentMode.*`)
212 s.log.ResetCapture()
213
214 // and finally, the common case: a single entry in the 3rd param, that gets updated individually.
215 xOuter := dbus.MakeVariant("x")
216 a := New(testibus.NewTestingEndpoint(nil, condition.Work(true), xOuter), s.log).(*accounts)
217 called := false
218 a.updaters = map[string]func(dbus.Variant){"xyzzy": func(x dbus.Variant) {
219 c.Check(x, Equals, xOuter)
220 called = true
221 }}
222 a.propsHandler(accountsSoundIface, map[interface{}]interface{}{}, []interface{}{"xyzzy"})
223 c.Check(called, Equals, true)
224 }
225
226 func (s *AccSuite) TestSilentMode(c *C) {
227 a := New(nil, s.log).(*accounts)
228 c.Check(a.SilentMode(), Equals, false)
229 a.silent = true
230 c.Check(a.SilentMode(), Equals, true)
231 }
232
233 func (s *AccSuite) TestVibrate(c *C) {
234 a := New(nil, s.log).(*accounts)
235 c.Check(a.Vibrate(), Equals, false)
236 a.vibrate = true
237 c.Check(a.Vibrate(), Equals, true)
238 a.silent = true
239 c.Check(a.Vibrate(), Equals, false)
240 a.vibrateSilentMode = true
241 c.Check(a.Vibrate(), Equals, true)
242 a.vibrate = false
243 c.Check(a.Vibrate(), Equals, true)
244 }
245
246 func (s *AccSuite) TestMessageSoundFile(c *C) {
247 a := New(nil, s.log).(*accounts)
248 c.Check(a.MessageSoundFile(), Equals, "")
249 a.messageSound = "xyzzy"
250 c.Check(a.MessageSoundFile(), Equals, "xyzzy")
251 }
252
253 func (s *AccSuite) TestString(c *C) {
254 a := New(nil, s.log).(*accounts)
255 a.vibrate = true
256 a.messageSound = "x"
257 c.Check(a.String(), Equals, `&accounts{silent: false, vibrate: true, vibratesilent: false, messageSound: "x"}`)
258 }