| 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 emblemcounter |
| 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 |
"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 |
"gitlab.com/ubports/development/core/lomiri-push-service/nih" |
| 30 |
helpers "gitlab.com/ubports/development/core/lomiri-push-service/testing" |
| 31 |
"gitlab.com/ubports/development/core/lomiri-push-service/testing/condition" |
| 32 |
) |
| 33 |
|
| 34 |
func TestEmblemCounter(t *testing.T) { TestingT(t) } |
| 35 |
|
| 36 |
type ecSuite struct { |
| 37 |
log *helpers.TestLogger |
| 38 |
app *click.AppId |
| 39 |
} |
| 40 |
|
| 41 |
var _ = Suite(&ecSuite{}) |
| 42 |
|
| 43 |
func (ecs *ecSuite) SetUpTest(c *C) { |
| 44 |
ecs.log = helpers.NewTestLogger(c, "debug") |
| 45 |
ecs.app = clickhelp.MustParseAppId("com.example.test_test-app_0") |
| 46 |
} |
| 47 |
|
| 48 |
// checks that SetCounter() actually calls SetProperty on the launcher |
| 49 |
func (ecs *ecSuite) TestSetCounterSetsTheCounter(c *C) { |
| 50 |
endp := testibus.NewTestingEndpoint(nil, condition.Work(true)) |
| 51 |
quoted := string(nih.Quote([]byte(ecs.app.Base()))) |
| 52 |
|
| 53 |
ec := New(endp, ecs.log) |
| 54 |
c.Check(ec.SetCounter(ecs.app, 42, true), Equals, true) |
| 55 |
callArgs := testibus.GetCallArgs(endp) |
| 56 |
c.Assert(callArgs, HasLen, 2) |
| 57 |
c.Check(callArgs[0].Member, Equals, "::SetProperty") |
| 58 |
c.Check(callArgs[1].Member, Equals, "::SetProperty") |
| 59 |
c.Check(callArgs[0].Args, DeepEquals, []interface{}{"count", "/" + quoted, dbus.MakeVariant(int32(42))}) |
| 60 |
c.Check(callArgs[1].Args, DeepEquals, []interface{}{"countVisible", "/" + quoted, dbus.MakeVariant(true)}) |
| 61 |
} |
| 62 |
|
| 63 |
// checks that Present() actually calls SetProperty on the launcher |
| 64 |
func (ecs *ecSuite) TestPresentPresents(c *C) { |
| 65 |
endp := testibus.NewTestingEndpoint(nil, condition.Work(true)) |
| 66 |
quoted := string(nih.Quote([]byte(ecs.app.Base()))) |
| 67 |
|
| 68 |
ec := New(endp, ecs.log) |
| 69 |
notif := launch_helper.Notification{EmblemCounter: &launch_helper.EmblemCounter{Count: 42, Visible: true}} |
| 70 |
c.Check(ec.Present(ecs.app, "nid", ¬if), Equals, true) |
| 71 |
callArgs := testibus.GetCallArgs(endp) |
| 72 |
c.Assert(callArgs, HasLen, 2) |
| 73 |
c.Check(callArgs[0].Member, Equals, "::SetProperty") |
| 74 |
c.Check(callArgs[1].Member, Equals, "::SetProperty") |
| 75 |
c.Check(callArgs[0].Args, DeepEquals, []interface{}{"count", "/" + quoted, dbus.MakeVariant(int32(42))}) |
| 76 |
c.Check(callArgs[1].Args, DeepEquals, []interface{}{"countVisible", "/" + quoted, dbus.MakeVariant(true)}) |
| 77 |
} |
| 78 |
|
| 79 |
// check that Present() doesn't call SetProperty if no EmblemCounter in the Notification |
| 80 |
func (ecs *ecSuite) TestSkipIfMissing(c *C) { |
| 81 |
quoted := string(nih.Quote([]byte(ecs.app.Base()))) |
| 82 |
endp := testibus.NewTestingEndpoint(nil, condition.Work(true)) |
| 83 |
ec := New(endp, ecs.log) |
| 84 |
|
| 85 |
// nothing happens if no EmblemCounter in Notification |
| 86 |
c.Check(ec.Present(ecs.app, "nid", &launch_helper.Notification{}), Equals, false) |
| 87 |
c.Assert(testibus.GetCallArgs(endp), HasLen, 0) |
| 88 |
|
| 89 |
// but an empty EmblemCounter is acted on |
| 90 |
c.Check(ec.Present(ecs.app, "nid", &launch_helper.Notification{EmblemCounter: &launch_helper.EmblemCounter{}}), Equals, true) |
| 91 |
callArgs := testibus.GetCallArgs(endp) |
| 92 |
c.Assert(callArgs, HasLen, 2) |
| 93 |
c.Check(callArgs[0].Member, Equals, "::SetProperty") |
| 94 |
c.Check(callArgs[1].Member, Equals, "::SetProperty") |
| 95 |
c.Check(callArgs[0].Args, DeepEquals, []interface{}{"count", "/" + quoted, dbus.MakeVariant(int32(0))}) |
| 96 |
c.Check(callArgs[1].Args, DeepEquals, []interface{}{"countVisible", "/" + quoted, dbus.MakeVariant(false)}) |
| 97 |
} |
| 98 |
|
| 99 |
// check that Present() panics if the notification is nil |
| 100 |
func (ecs *ecSuite) TestPanicsIfNil(c *C) { |
| 101 |
endp := testibus.NewTestingEndpoint(nil, condition.Work(true)) |
| 102 |
ec := New(endp, ecs.log) |
| 103 |
|
| 104 |
// nothing happens if no EmblemCounter in Notification |
| 105 |
c.Check(func() { ec.Present(ecs.app, "nid", nil) }, Panics, `please check notification is not nil before calling present`) |
| 106 |
} |