| 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 haptic |
| 18 |
|
| 19 |
import ( |
| 20 |
"encoding/json" |
| 21 |
"testing" |
| 22 |
|
| 23 |
. "gopkg.in/check.v1" |
| 24 |
|
| 25 |
testibus "gitlab.com/ubports/development/core/lomiri-push-service/bus/testing" |
| 26 |
"gitlab.com/ubports/development/core/lomiri-push-service/click" |
| 27 |
clickhelp "gitlab.com/ubports/development/core/lomiri-push-service/click/testing" |
| 28 |
"gitlab.com/ubports/development/core/lomiri-push-service/launch_helper" |
| 29 |
helpers "gitlab.com/ubports/development/core/lomiri-push-service/testing" |
| 30 |
"gitlab.com/ubports/development/core/lomiri-push-service/testing/condition" |
| 31 |
) |
| 32 |
|
| 33 |
func TestHaptic(t *testing.T) { TestingT(t) } |
| 34 |
|
| 35 |
type hapticSuite struct { |
| 36 |
log *helpers.TestLogger |
| 37 |
app *click.AppId |
| 38 |
acc *mockAccounts |
| 39 |
} |
| 40 |
|
| 41 |
type mockAccounts struct { |
| 42 |
vib bool |
| 43 |
sil bool |
| 44 |
snd string |
| 45 |
err error |
| 46 |
} |
| 47 |
|
| 48 |
func (m *mockAccounts) Start() error { return m.err } |
| 49 |
func (m *mockAccounts) Cancel() { } |
| 50 |
func (m *mockAccounts) SilentMode() bool { return m.sil } |
| 51 |
func (m *mockAccounts) Vibrate() bool { return m.vib } |
| 52 |
func (m *mockAccounts) MessageSoundFile() string { return m.snd } |
| 53 |
func (m *mockAccounts) String() string { return "<mockAccounts>" } |
| 54 |
|
| 55 |
var _ = Suite(&hapticSuite{}) |
| 56 |
|
| 57 |
func (hs *hapticSuite) SetUpTest(c *C) { |
| 58 |
hs.log = helpers.NewTestLogger(c, "debug") |
| 59 |
hs.app = clickhelp.MustParseAppId("com.example.test_test-app_0") |
| 60 |
hs.acc = &mockAccounts{true, false, "xyzzy", nil} |
| 61 |
} |
| 62 |
|
| 63 |
// checks that Present() actually calls VibratePattern |
| 64 |
func (hs *hapticSuite) TestPresentPresents(c *C) { |
| 65 |
endp := testibus.NewTestingEndpoint(nil, condition.Work(true)) |
| 66 |
|
| 67 |
ec := New(endp, hs.log, hs.acc, nil) |
| 68 |
notif := launch_helper.Notification{RawVibration: json.RawMessage(`{"pattern": [200, 100], "repeat": 2}`)} |
| 69 |
c.Check(ec.Present(hs.app, "nid", ¬if), Equals, true) |
| 70 |
callArgs := testibus.GetCallArgs(endp) |
| 71 |
c.Assert(callArgs, HasLen, 1) |
| 72 |
c.Check(callArgs[0].Member, Equals, "vibratePattern") |
| 73 |
c.Check(callArgs[0].Args, DeepEquals, []interface{}{[]uint32{200, 100}, uint32(2)}) |
| 74 |
} |
| 75 |
|
| 76 |
// check that Present() defaults Repeat to 1 |
| 77 |
func (hs *hapticSuite) TestPresentDefaultsRepeatTo1(c *C) { |
| 78 |
endp := testibus.NewTestingEndpoint(nil, condition.Work(true)) |
| 79 |
|
| 80 |
ec := New(endp, hs.log, hs.acc, nil) |
| 81 |
// note: no Repeat: |
| 82 |
notif := launch_helper.Notification{RawVibration: json.RawMessage(`{"pattern": [200, 100]}`)} |
| 83 |
c.Check(ec.Present(hs.app, "nid", ¬if), Equals, true) |
| 84 |
callArgs := testibus.GetCallArgs(endp) |
| 85 |
c.Assert(callArgs, HasLen, 1) |
| 86 |
c.Check(callArgs[0].Member, Equals, "vibratePattern") |
| 87 |
// note: Repeat of 1: |
| 88 |
c.Check(callArgs[0].Args, DeepEquals, []interface{}{[]uint32{200, 100}, uint32(1)}) |
| 89 |
} |
| 90 |
|
| 91 |
// check that Present() doesn't call VibratePattern if things are not right |
| 92 |
func (hs *hapticSuite) TestSkipIfMissing(c *C) { |
| 93 |
endp := testibus.NewTestingEndpoint(nil, condition.Work(true)) |
| 94 |
|
| 95 |
ec := New(endp, hs.log, hs.acc, nil) |
| 96 |
// no Vibration in the notificaton |
| 97 |
c.Check(ec.Present(hs.app, "", &launch_helper.Notification{}), Equals, false) |
| 98 |
// empty Vibration |
| 99 |
c.Check(ec.Present(hs.app, "", &launch_helper.Notification{RawVibration: nil}), Equals, false) |
| 100 |
// empty empty vibration |
| 101 |
c.Check(ec.Present(hs.app, "", &launch_helper.Notification{RawVibration: json.RawMessage(`{}`)}), Equals, false) |
| 102 |
} |
| 103 |
|
| 104 |
// check that Present() does not present if the accounts' Vibrate() returns false |
| 105 |
func (hs *hapticSuite) TestPresentSkipsIfVibrateDisabled(c *C) { |
| 106 |
endp := testibus.NewTestingEndpoint(nil, condition.Work(true)) |
| 107 |
fallback := &launch_helper.Vibration{Pattern: []uint32{200, 100}, Repeat: 2} |
| 108 |
|
| 109 |
ec := New(endp, hs.log, hs.acc, fallback) |
| 110 |
notif := launch_helper.Notification{RawVibration: json.RawMessage(`true`)} |
| 111 |
c.Assert(ec.Present(hs.app, "nid", ¬if), Equals, true) |
| 112 |
// ok! |
| 113 |
hs.acc.vib = false |
| 114 |
c.Check(ec.Present(hs.app, "nid", ¬if), Equals, false) |
| 115 |
} |
| 116 |
|
| 117 |
// check that Present() panics if the notification is nil |
| 118 |
func (hs *hapticSuite) TestPanicsIfNil(c *C) { |
| 119 |
endp := testibus.NewTestingEndpoint(nil, condition.Work(true)) |
| 120 |
|
| 121 |
ec := New(endp, hs.log, hs.acc, nil) |
| 122 |
// no notification at all |
| 123 |
c.Check(func() { ec.Present(hs.app, "", nil) }, Panics, `please check notification is not nil before calling present`) |
| 124 |
} |
| 125 |
|
| 126 |
// check that Present() uses the fallback if appropriate |
| 127 |
func (hs *hapticSuite) TestPresentPresentsFallback(c *C) { |
| 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, hs.acc, fallback) |
| 132 |
notif := launch_helper.Notification{RawVibration: json.RawMessage(`false`)} |
| 133 |
c.Check(ec.Present(hs.app, "nid", ¬if), Equals, false) |
| 134 |
notif = launch_helper.Notification{RawVibration: json.RawMessage(`true`)} |
| 135 |
c.Check(ec.Present(hs.app, "nid", ¬if), Equals, true) |
| 136 |
callArgs := testibus.GetCallArgs(endp) |
| 137 |
c.Assert(callArgs, HasLen, 1) |
| 138 |
c.Check(callArgs[0].Member, Equals, "vibratePattern") |
| 139 |
c.Check(callArgs[0].Args, DeepEquals, []interface{}{[]uint32{200, 100}, uint32(2)}) |
| 140 |
} |