| 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 launch_helper |
| 18 |
|
| 19 |
import ( |
| 20 |
"encoding/json" |
| 21 |
"time" |
| 22 |
|
| 23 |
. "gopkg.in/check.v1" |
| 24 |
) |
| 25 |
|
| 26 |
type outSuite struct{} |
| 27 |
|
| 28 |
var _ = Suite(&outSuite{}) |
| 29 |
|
| 30 |
func (*outSuite) TestCardGetTimestamp(c *C) { |
| 31 |
t := time.Now().Add(-2 * time.Second) |
| 32 |
var card Card |
| 33 |
err := json.Unmarshal([]byte(`{"timestamp": 12}`), &card) |
| 34 |
c.Assert(err, IsNil) |
| 35 |
c.Check(card, DeepEquals, Card{RawTimestamp: 12}) |
| 36 |
c.Check(time.Unix((&Card{}).Timestamp(), 0).After(t), Equals, true) |
| 37 |
c.Check((&Card{RawTimestamp: 42}).Timestamp(), Equals, int64(42)) |
| 38 |
} |
| 39 |
|
| 40 |
func (*outSuite) TestBadVibeBegetsNilVibe(c *C) { |
| 41 |
fbck := &Vibration{Repeat: 2} |
| 42 |
for _, s := range []string{ |
| 43 |
`{}`, |
| 44 |
`{"vibrate": "foo"}`, |
| 45 |
`{"vibrate": {}}`, |
| 46 |
`{"vibrate": false}`, // not bad, but rather pointless |
| 47 |
`{"vibrate": {"repeat": 2}}`, // no pattern |
| 48 |
`{"vibrate": {"repeat": "foo"}}`, |
| 49 |
`{"vibrate": {"pattern": "foo"}}`, |
| 50 |
`{"vibrate": {"pattern": ["foo"]}}`, |
| 51 |
`{"vibrate": {"pattern": null}}`, |
| 52 |
`{"vibrate": {"pattern": [-1]}}`, |
| 53 |
`{"vibrate": {"pattern": [1], "repeat": -1}}`, |
| 54 |
} { |
| 55 |
var notif *Notification |
| 56 |
err := json.Unmarshal([]byte(s), ¬if) |
| 57 |
c.Assert(err, IsNil) |
| 58 |
c.Assert(notif, NotNil) |
| 59 |
c.Check(notif.Vibration(fbck), IsNil, Commentf("not nil Vibration() for: %s", s)) |
| 60 |
c.Check(notif.Vibration(fbck), IsNil, Commentf("not nil second call to Vibration() for: %s", s)) |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
func (*outSuite) TestGoodVibe(c *C) { |
| 65 |
var notif *Notification |
| 66 |
err := json.Unmarshal([]byte(`{"vibrate": {"pattern": [1,2,3], "repeat": 2}}`), ¬if) |
| 67 |
c.Assert(err, IsNil) |
| 68 |
c.Assert(notif, NotNil) |
| 69 |
c.Check(notif.Vibration(nil), DeepEquals, &Vibration{Pattern: []uint32{1, 2, 3}, Repeat: 2}) |
| 70 |
} |
| 71 |
|
| 72 |
func (*outSuite) TestGoodSimpleVibe(c *C) { |
| 73 |
var notif *Notification |
| 74 |
fallback := &Vibration{Pattern: []uint32{100, 100}, Repeat: 3} |
| 75 |
err := json.Unmarshal([]byte(`{"vibrate": true}`), ¬if) |
| 76 |
c.Assert(err, IsNil) |
| 77 |
c.Assert(notif, NotNil) |
| 78 |
c.Check(notif.Vibration(fallback), Equals, fallback) |
| 79 |
} |
| 80 |
|
| 81 |
func (*outSuite) TestBadSoundBegetsNoSound(c *C) { |
| 82 |
c.Check((&Notification{RawSound: json.RawMessage("foo")}).Sound("x"), Equals, "") |
| 83 |
} |
| 84 |
|
| 85 |
func (*outSuite) TestNilSoundBegetsNoSound(c *C) { |
| 86 |
c.Check((&Notification{RawSound: nil}).Sound("x"), Equals, "") |
| 87 |
} |
| 88 |
|
| 89 |
func (*outSuite) TestGoodSound(c *C) { |
| 90 |
c.Check((&Notification{RawSound: json.RawMessage(`"foo"`)}).Sound("x"), Equals, "foo") |
| 91 |
} |
| 92 |
|
| 93 |
func (*outSuite) TestGoodSimpleSound(c *C) { |
| 94 |
c.Check((&Notification{RawSound: json.RawMessage(`true`)}).Sound("x"), Equals, "x") |
| 95 |
c.Check((&Notification{RawSound: json.RawMessage(`false`)}).Sound("x"), Equals, "") |
| 96 |
} |