| 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 ( |
| 20 |
"encoding/json" |
| 21 |
"errors" |
| 22 |
"os" |
| 23 |
"path" |
| 24 |
"testing" |
| 25 |
|
| 26 |
. "gopkg.in/check.v1" |
| 27 |
|
| 28 |
"gitlab.com/ubports/development/core/lomiri-push-service/click" |
| 29 |
clickhelp "gitlab.com/ubports/development/core/lomiri-push-service/click/testing" |
| 30 |
"gitlab.com/ubports/development/core/lomiri-push-service/launch_helper" |
| 31 |
helpers "gitlab.com/ubports/development/core/lomiri-push-service/testing" |
| 32 |
) |
| 33 |
|
| 34 |
func TestSounds(t *testing.T) { TestingT(t) } |
| 35 |
|
| 36 |
type soundsSuite struct { |
| 37 |
log *helpers.TestLogger |
| 38 |
app *click.AppId |
| 39 |
acc *mockAccounts |
| 40 |
} |
| 41 |
|
| 42 |
var _ = Suite(&soundsSuite{}) |
| 43 |
|
| 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() { } |
| 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 |
|
| 58 |
func (ss *soundsSuite) SetUpTest(c *C) { |
| 59 |
ss.log = helpers.NewTestLogger(c, "debug") |
| 60 |
ss.app = clickhelp.MustParseAppId("com.example.test_test_0") |
| 61 |
ss.acc = &mockAccounts{true, false, "", nil} |
| 62 |
} |
| 63 |
|
| 64 |
func (ss *soundsSuite) TestNew(c *C) { |
| 65 |
s := New(ss.log, ss.acc, "foo") |
| 66 |
c.Check(s.log, Equals, ss.log) |
| 67 |
c.Check(s.player, Equals, "paplay") |
| 68 |
c.Check(s.fallback, Equals, "foo") |
| 69 |
c.Check(s.acc, Equals, ss.acc) |
| 70 |
} |
| 71 |
|
| 72 |
func (ss *soundsSuite) TestPresent(c *C) { |
| 73 |
s := &sound{ |
| 74 |
player: "echo", log: ss.log, acc: ss.acc, |
| 75 |
dataFind: func(s string) (string, error) { return s, nil }, |
| 76 |
} |
| 77 |
|
| 78 |
c.Check(s.Present(ss.app, "", |
| 79 |
&launch_helper.Notification{RawSound: json.RawMessage(`"hello"`)}), Equals, true) |
| 80 |
c.Check(ss.log.Captured(), Matches, `(?sm).* playing sound com.example.test/hello using echo`) |
| 81 |
} |
| 82 |
|
| 83 |
func (ss *soundsSuite) TestPresentSimple(c *C) { |
| 84 |
s := &sound{ |
| 85 |
player: "echo", log: ss.log, acc: ss.acc, |
| 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`) |
| 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`) |
| 98 |
} |
| 99 |
|
| 100 |
func (ss *soundsSuite) TestPresentFails(c *C) { |
| 101 |
s := &sound{ |
| 102 |
player: "/", |
| 103 |
log: ss.log, |
| 104 |
acc: ss.acc, |
| 105 |
dataFind: func(string) (string, error) { return "", errors.New("nope") }, |
| 106 |
dataDirs: func() []string { return []string{""} }, |
| 107 |
} |
| 108 |
|
| 109 |
// nil notification |
| 110 |
c.Check(func() { s.Present(ss.app, "", nil) }, Panics, `please check notification is not nil before calling present`) |
| 111 |
// no Sound |
| 112 |
c.Check(s.Present(ss.app, "", &launch_helper.Notification{}), Equals, false) |
| 113 |
// bad player |
| 114 |
c.Check(s.Present(ss.app, "", &launch_helper.Notification{RawSound: json.RawMessage(`"hello"`)}), Equals, false) |
| 115 |
s.player = "echo" |
| 116 |
// no file found |
| 117 |
c.Check(s.Present(ss.app, "", &launch_helper.Notification{RawSound: json.RawMessage(`"hello"`)}), Equals, false) |
| 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} } |
| 126 |
c.Check(s.Present(ss.app, "", &launch_helper.Notification{RawSound: json.RawMessage(`"hello"`)}), Equals, true) |
| 127 |
} |
| 128 |
|
| 129 |
func (ss *soundsSuite) TestBadPathFails(c *C) { |
| 130 |
s := &sound{ |
| 131 |
player: "/", |
| 132 |
log: ss.log, |
| 133 |
acc: ss.acc, |
| 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, |
| 147 |
acc: ss.acc, |
| 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") |
| 153 |
c.Check(err, IsNil) |
| 154 |
c.Check(sound, Equals, "bar") |
| 155 |
} |
| 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 |
} |