91.112.1
by John R. Lenton
sounds! |
1 |
/*
|
2 |
Copyright 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 sounds |
|
18 |
||
19 |
import ( |
|
91.177.1
by John R. Lenton
support simple sounds |
20 |
"encoding/json"
|
91.112.8
by John R. Lenton
make sounds relative to XDG dirs |
21 |
"errors"
|
22 |
"os"
|
|
23 |
"path"
|
|
91.112.1
by John R. Lenton
sounds! |
24 |
"testing"
|
25 |
||
26 |
. "launchpad.net/gocheck" |
|
27 |
||
91.114.1
by John R. Lenton
zomg, that's a big diff |
28 |
"launchpad.net/ubuntu-push/click"
|
91.123.1
by Samuele Pedroni (Canonical Services Ltd.)
don't have ubuntu-push/testing depend on click and c libs |
29 |
clickhelp "launchpad.net/ubuntu-push/click/testing" |
91.112.1
by John R. Lenton
sounds! |
30 |
"launchpad.net/ubuntu-push/launch_helper"
|
31 |
helpers "launchpad.net/ubuntu-push/testing" |
|
32 |
)
|
|
33 |
||
34 |
func TestSounds(t *testing.T) { TestingT(t) } |
|
35 |
||
36 |
type soundsSuite struct { |
|
37 |
log *helpers.TestLogger |
|
91.114.1
by John R. Lenton
zomg, that's a big diff |
38 |
app *click.AppId |
91.241.1
by John R. Lenton
use accounts from sound and haptic |
39 |
acc *mockAccounts |
91.112.1
by John R. Lenton
sounds! |
40 |
}
|
41 |
||
42 |
var _ = Suite(&soundsSuite{}) |
|
43 |
||
91.241.1
by John R. Lenton
use accounts from sound and haptic |
44 |
type mockAccounts struct { |
45 |
vib bool |
|
46 |
sil bool |
|
47 |
snd string |
|
48 |
err error |
|
49 |
}
|
|
50 |
||
51 |
func (m *mockAccounts) Start() error { return m.err } |
|
52 |
func (m *mockAccounts) Cancel() error { return m.err } |
|
53 |
func (m *mockAccounts) SilentMode() bool { return m.sil } |
|
54 |
func (m *mockAccounts) Vibrate() bool { return m.vib } |
|
55 |
func (m *mockAccounts) MessageSoundFile() string { return m.snd } |
|
56 |
func (m *mockAccounts) String() string { return "<mockAccounts>" } |
|
57 |
||
91.112.1
by John R. Lenton
sounds! |
58 |
func (ss *soundsSuite) SetUpTest(c *C) { |
59 |
ss.log = helpers.NewTestLogger(c, "debug") |
|
91.123.1
by Samuele Pedroni (Canonical Services Ltd.)
don't have ubuntu-push/testing depend on click and c libs |
60 |
ss.app = clickhelp.MustParseAppId("com.example.test_test_0") |
91.241.1
by John R. Lenton
use accounts from sound and haptic |
61 |
ss.acc = &mockAccounts{true, false, "", nil} |
91.112.1
by John R. Lenton
sounds! |
62 |
}
|
63 |
||
64 |
func (ss *soundsSuite) TestNew(c *C) { |
|
91.241.1
by John R. Lenton
use accounts from sound and haptic |
65 |
s := New(ss.log, ss.acc, "foo") |
91.112.1
by John R. Lenton
sounds! |
66 |
c.Check(s.log, Equals, ss.log) |
67 |
c.Check(s.player, Equals, "paplay") |
|
91.177.1
by John R. Lenton
support simple sounds |
68 |
c.Check(s.fallback, Equals, "foo") |
91.241.1
by John R. Lenton
use accounts from sound and haptic |
69 |
c.Check(s.acc, Equals, ss.acc) |
91.112.1
by John R. Lenton
sounds! |
70 |
}
|
71 |
||
72 |
func (ss *soundsSuite) TestPresent(c *C) { |
|
91.112.8
by John R. Lenton
make sounds relative to XDG dirs |
73 |
s := &Sound{ |
91.241.1
by John R. Lenton
use accounts from sound and haptic |
74 |
player: "echo", log: ss.log, acc: ss.acc, |
91.112.8
by John R. Lenton
make sounds relative to XDG dirs |
75 |
dataFind: func(s string) (string, error) { return s, nil }, |
76 |
}
|
|
91.112.1
by John R. Lenton
sounds! |
77 |
|
91.114.1
by John R. Lenton
zomg, that's a big diff |
78 |
c.Check(s.Present(ss.app, "", |
91.177.1
by John R. Lenton
support simple sounds |
79 |
&launch_helper.Notification{RawSound: json.RawMessage(`"hello"`)}), Equals, true) |
91.112.8
by John R. Lenton
make sounds relative to XDG dirs |
80 |
c.Check(ss.log.Captured(), Matches, `(?sm).* playing sound com.example.test/hello using echo`) |
91.112.1
by John R. Lenton
sounds! |
81 |
}
|
82 |
||
91.177.1
by John R. Lenton
support simple sounds |
83 |
func (ss *soundsSuite) TestPresentSimple(c *C) { |
84 |
s := &Sound{ |
|
91.241.1
by John R. Lenton
use accounts from sound and haptic |
85 |
player: "echo", log: ss.log, acc: ss.acc, |
91.177.1
by John R. Lenton
support simple sounds |
86 |
dataFind: func(s string) (string, error) { return s, nil }, |
87 |
fallback: "fallback", |
|
88 |
}
|
|
89 |
||
90 |
c.Check(s.Present(ss.app, "", |
|
91 |
&launch_helper.Notification{RawSound: json.RawMessage(`true`)}), Equals, true) |
|
92 |
c.Check(ss.log.Captured(), Matches, `(?sm).* playing sound com.example.test/fallback using echo`) |
|
91.241.1
by John R. Lenton
use accounts from sound and haptic |
93 |
ss.acc.snd = "from-prefs" |
94 |
ss.log.ResetCapture() |
|
95 |
c.Check(s.Present(ss.app, "", |
|
96 |
&launch_helper.Notification{RawSound: json.RawMessage(`true`)}), Equals, true) |
|
97 |
c.Check(ss.log.Captured(), Matches, `(?sm).* playing sound com.example.test/from-prefs using echo`) |
|
91.177.1
by John R. Lenton
support simple sounds |
98 |
}
|
99 |
||
91.112.1
by John R. Lenton
sounds! |
100 |
func (ss *soundsSuite) TestPresentFails(c *C) { |
91.114.1
by John R. Lenton
zomg, that's a big diff |
101 |
s := &Sound{ |
102 |
player: "/", |
|
103 |
log: ss.log, |
|
91.241.1
by John R. Lenton
use accounts from sound and haptic |
104 |
acc: ss.acc, |
91.114.1
by John R. Lenton
zomg, that's a big diff |
105 |
dataFind: func(string) (string, error) { return "", errors.New("nope") }, |
106 |
dataDirs: func() []string { return []string{""} }, |
|
107 |
}
|
|
91.112.1
by John R. Lenton
sounds! |
108 |
|
109 |
// nil notification
|
|
91.149.4
by John R. Lenton
a bit of refactoring is good for the soul |
110 |
c.Check(func() { s.Present(ss.app, "", nil) }, Panics, `please check notification is not nil before calling present`) |
91.112.1
by John R. Lenton
sounds! |
111 |
// no Sound
|
91.114.1
by John R. Lenton
zomg, that's a big diff |
112 |
c.Check(s.Present(ss.app, "", &launch_helper.Notification{}), Equals, false) |
91.112.1
by John R. Lenton
sounds! |
113 |
// bad player
|
91.177.1
by John R. Lenton
support simple sounds |
114 |
c.Check(s.Present(ss.app, "", &launch_helper.Notification{RawSound: json.RawMessage(`"hello"`)}), Equals, false) |
91.112.8
by John R. Lenton
make sounds relative to XDG dirs |
115 |
s.player = "echo" |
116 |
// no file found
|
|
91.177.1
by John R. Lenton
support simple sounds |
117 |
c.Check(s.Present(ss.app, "", &launch_helper.Notification{RawSound: json.RawMessage(`"hello"`)}), Equals, false) |
91.112.8
by John R. Lenton
make sounds relative to XDG dirs |
118 |
|
119 |
// and now, just to prove it would've worked,
|
|
120 |
||
121 |
d := c.MkDir() |
|
122 |
f, err := os.Create(path.Join(d, "hello")) |
|
123 |
c.Assert(err, IsNil) |
|
124 |
f.Close() |
|
125 |
s.dataDirs = func() []string { return []string{"", d} } |
|
91.177.1
by John R. Lenton
support simple sounds |
126 |
c.Check(s.Present(ss.app, "", &launch_helper.Notification{RawSound: json.RawMessage(`"hello"`)}), Equals, true) |
91.112.1
by John R. Lenton
sounds! |
127 |
}
|
91.170.1
by Roberto Alsina
check for .. in sound paths |
128 |
|
129 |
func (ss *soundsSuite) TestBadPathFails(c *C) { |
|
130 |
s := &Sound{ |
|
131 |
player: "/", |
|
132 |
log: ss.log, |
|
91.241.1
by John R. Lenton
use accounts from sound and haptic |
133 |
acc: ss.acc, |
91.170.1
by Roberto Alsina
check for .. in sound paths |
134 |
dataFind: func(string) (string, error) { return "", errors.New("nope") }, |
135 |
dataDirs: func() []string { return []string{""} }, |
|
136 |
}
|
|
137 |
||
138 |
sound, err := s.cleanPath("../../foo") |
|
139 |
c.Check(err, NotNil) |
|
140 |
c.Check(sound, Equals, "") |
|
141 |
}
|
|
142 |
||
143 |
func (ss *soundsSuite) TestGoodPathSucceeds(c *C) { |
|
144 |
s := &Sound{ |
|
145 |
player: "/", |
|
146 |
log: ss.log, |
|
91.241.1
by John R. Lenton
use accounts from sound and haptic |
147 |
acc: ss.acc, |
91.170.1
by Roberto Alsina
check for .. in sound paths |
148 |
dataFind: func(string) (string, error) { return "", errors.New("nope") }, |
149 |
dataDirs: func() []string { return []string{""} }, |
|
150 |
}
|
|
151 |
||
152 |
sound, err := s.cleanPath("foo/../bar") |
|
91.170.4
by Roberto Alsina
oops |
153 |
c.Check(err, IsNil) |
91.170.1
by Roberto Alsina
check for .. in sound paths |
154 |
c.Check(sound, Equals, "bar") |
155 |
}
|
|
91.241.1
by John R. Lenton
use accounts from sound and haptic |
156 |
|
157 |
func (ss *soundsSuite) TestSkipIfSilentMode(c *C) { |
|
158 |
s := &Sound{ |
|
159 |
player: "echo", |
|
160 |
log: ss.log, |
|
161 |
acc: ss.acc, |
|
162 |
fallback: "fallback", |
|
163 |
dataFind: func(s string) (string, error) { return s, nil }, |
|
164 |
}
|
|
165 |
||
166 |
c.Check(s.Present(ss.app, "", |
|
167 |
&launch_helper.Notification{RawSound: json.RawMessage(`true`)}), Equals, true) |
|
168 |
ss.acc.sil = true |
|
169 |
c.Check(s.Present(ss.app, "", |
|
170 |
&launch_helper.Notification{RawSound: json.RawMessage(`true`)}), Equals, false) |
|
171 |
}
|