| 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 messaging |
| 18 |
|
| 19 |
import ( |
| 20 |
"sort" |
| 21 |
"strconv" |
| 22 |
"sync" |
| 23 |
"time" |
| 24 |
|
| 25 |
. "gopkg.in/check.v1" |
| 26 |
"testing" |
| 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 |
"gitlab.com/ubports/development/core/lomiri-push-service/messaging/cmessaging" |
| 32 |
"gitlab.com/ubports/development/core/lomiri-push-service/messaging/reply" |
| 33 |
helpers "gitlab.com/ubports/development/core/lomiri-push-service/testing" |
| 34 |
) |
| 35 |
|
| 36 |
// hook up gocheck |
| 37 |
func Test(t *testing.T) { TestingT(t) } |
| 38 |
|
| 39 |
type MessagingSuite struct { |
| 40 |
log *helpers.TestLogger |
| 41 |
app *click.AppId |
| 42 |
} |
| 43 |
|
| 44 |
var _ = Suite(&MessagingSuite{}) |
| 45 |
|
| 46 |
func (ms *MessagingSuite) SetUpSuite(c *C) { |
| 47 |
cAddNotification = func(a string, n string, c *launch_helper.Card, payload *cmessaging.Payload) { |
| 48 |
ms.log.Debugf("ADD: app: %s, not: %s, card: %v, chan: %v", a, n, c, payload) |
| 49 |
} |
| 50 |
cRemoveNotification = func(a, n string) { |
| 51 |
ms.log.Debugf("REMOVE: app: %s, not: %s", a, n) |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
func (ms *MessagingSuite) TearDownSuite(c *C) { |
| 56 |
cAddNotification = cmessaging.AddNotification |
| 57 |
cRemoveNotification = cmessaging.RemoveNotification |
| 58 |
cNotificationExists = cmessaging.NotificationExists |
| 59 |
} |
| 60 |
|
| 61 |
func (ms *MessagingSuite) SetUpTest(c *C) { |
| 62 |
ms.log = helpers.NewTestLogger(c, "debug") |
| 63 |
ms.app = clickhelp.MustParseAppId("com.example.test_test_0") |
| 64 |
// just in case |
| 65 |
cNotificationExists = nil |
| 66 |
} |
| 67 |
|
| 68 |
func (ms *MessagingSuite) TestPresentPresents(c *C) { |
| 69 |
mmu := New(ms.log) |
| 70 |
card := launch_helper.Card{Summary: "ehlo", Persist: true} |
| 71 |
notif := launch_helper.Notification{Card: &card} |
| 72 |
|
| 73 |
c.Check(mmu.Present(ms.app, "notif-id", ¬if), Equals, true) |
| 74 |
|
| 75 |
c.Check(ms.log.Captured(), Matches, `(?s).* ADD:.*notif-id.*`) |
| 76 |
} |
| 77 |
|
| 78 |
func (ms *MessagingSuite) TestPresentDoesNotPresentsIfNoSummary(c *C) { |
| 79 |
mmu := New(ms.log) |
| 80 |
card := launch_helper.Card{Persist: true} |
| 81 |
notif := launch_helper.Notification{Card: &card} |
| 82 |
|
| 83 |
c.Check(mmu.Present(ms.app, "notif-id", ¬if), Equals, false) |
| 84 |
|
| 85 |
c.Check(ms.log.Captured(), Matches, "(?sm).*has no persistable card.*") |
| 86 |
} |
| 87 |
|
| 88 |
func (ms *MessagingSuite) TestPresentDoesNotPresentsIfNotPersist(c *C) { |
| 89 |
mmu := New(ms.log) |
| 90 |
card := launch_helper.Card{Summary: "ehlo"} |
| 91 |
notif := launch_helper.Notification{Card: &card} |
| 92 |
|
| 93 |
c.Check(mmu.Present(ms.app, "notif-id", ¬if), Equals, false) |
| 94 |
|
| 95 |
c.Check(ms.log.Captured(), Matches, "(?sm).*has no persistable card.*") |
| 96 |
} |
| 97 |
|
| 98 |
func (ms *MessagingSuite) TestPresentPanicsIfNil(c *C) { |
| 99 |
mmu := New(ms.log) |
| 100 |
c.Check(func() { mmu.Present(ms.app, "notif-id", nil) }, Panics, `please check notification is not nil before calling present`) |
| 101 |
} |
| 102 |
|
| 103 |
func (ms *MessagingSuite) TestPresentDoesNotPresentsIfNilCard(c *C) { |
| 104 |
mmu := New(ms.log) |
| 105 |
c.Check(mmu.Present(ms.app, "notif-id", &launch_helper.Notification{}), Equals, false) |
| 106 |
c.Check(ms.log.Captured(), Matches, "(?sm).*no persistable card.*") |
| 107 |
} |
| 108 |
|
| 109 |
func (ms *MessagingSuite) TestPresentWithActions(c *C) { |
| 110 |
mmu := New(ms.log) |
| 111 |
card := launch_helper.Card{Summary: "ehlo", Persist: true, Actions: []string{"action-1"}} |
| 112 |
notif := launch_helper.Notification{Card: &card, Tag: "a-tag"} |
| 113 |
|
| 114 |
c.Check(mmu.Present(ms.app, "notif-id", ¬if), Equals, true) |
| 115 |
|
| 116 |
c.Check(ms.log.Captured(), Matches, `(?s).* ADD:.*notif-id.*`) |
| 117 |
|
| 118 |
payload, _ := mmu.notifications["notif-id"] |
| 119 |
c.Check(payload.Ch, Equals, mmu.Ch) |
| 120 |
c.Check(len(payload.Actions), Equals, 2) |
| 121 |
c.Check(payload.Tag, Equals, "a-tag") |
| 122 |
rawAction := "{\"app\":\"com.example.test_test_0\",\"act\":\"action-1\",\"nid\":\"notif-id\"}" |
| 123 |
c.Check(payload.Actions[0], Equals, rawAction) |
| 124 |
c.Check(payload.Actions[1], Equals, "action-1") |
| 125 |
} |
| 126 |
|
| 127 |
func (msg *MessagingSuite) checkTags(c *C, got, expected []string) { |
| 128 |
sort.Strings(got) |
| 129 |
sort.Strings(expected) |
| 130 |
c.Check(got, DeepEquals, expected) |
| 131 |
} |
| 132 |
|
| 133 |
func (ms *MessagingSuite) TestTagsListsTags(c *C) { |
| 134 |
mmu := New(ms.log) |
| 135 |
f := func(s string) *launch_helper.Notification { |
| 136 |
card := launch_helper.Card{Summary: "tag: \"" + s + "\"", Persist: true} |
| 137 |
return &launch_helper.Notification{Card: &card, Tag: s} |
| 138 |
} |
| 139 |
|
| 140 |
existsCount := 0 |
| 141 |
// patch cNotificationExists to return true |
| 142 |
cNotificationExists = func(did string, nid string) bool { |
| 143 |
existsCount++ |
| 144 |
return true |
| 145 |
} |
| 146 |
|
| 147 |
c.Check(mmu.Tags(ms.app), IsNil) |
| 148 |
c.Assert(mmu.Present(ms.app, "notif1", f("one")), Equals, true) |
| 149 |
ms.checkTags(c, mmu.Tags(ms.app), []string{"one"}) |
| 150 |
c.Check(existsCount, Equals, 1) |
| 151 |
existsCount = 0 |
| 152 |
|
| 153 |
c.Assert(mmu.Present(ms.app, "notif2", f("")), Equals, true) |
| 154 |
ms.checkTags(c, mmu.Tags(ms.app), []string{"one", ""}) |
| 155 |
c.Check(existsCount, Equals, 2) |
| 156 |
|
| 157 |
// and an empty notification doesn't count |
| 158 |
c.Assert(mmu.Present(ms.app, "notif3", &launch_helper.Notification{Tag: "X"}), Equals, false) |
| 159 |
ms.checkTags(c, mmu.Tags(ms.app), []string{"one", ""}) |
| 160 |
// and they go away if we remove one |
| 161 |
mmu.RemoveNotification("notif1", false) |
| 162 |
ms.checkTags(c, mmu.Tags(ms.app), []string{""}) |
| 163 |
mmu.RemoveNotification("notif2", false) |
| 164 |
c.Check(mmu.Tags(ms.app), IsNil) |
| 165 |
} |
| 166 |
|
| 167 |
func (ms *MessagingSuite) TestClearClears(c *C) { |
| 168 |
app1 := ms.app |
| 169 |
app2 := clickhelp.MustParseAppId("com.example.test_test-2_0") |
| 170 |
app3 := clickhelp.MustParseAppId("com.example.test_test-3_0") |
| 171 |
mm := New(ms.log) |
| 172 |
f := func(app *click.AppId, nid string, tag string, withCard bool) bool { |
| 173 |
notif := launch_helper.Notification{Tag: tag} |
| 174 |
card := launch_helper.Card{Summary: "tag: \"" + tag + "\"", Persist: true} |
| 175 |
if withCard { |
| 176 |
notif.Card = &card |
| 177 |
} |
| 178 |
return mm.Present(app, nid, ¬if) |
| 179 |
} |
| 180 |
// create a bunch |
| 181 |
c.Assert(f(app1, "notif1", "one", true), Equals, true) |
| 182 |
c.Assert(f(app1, "notif2", "two", true), Equals, true) |
| 183 |
c.Assert(f(app1, "notif3", "", true), Equals, true) |
| 184 |
c.Assert(f(app2, "notif4", "one", true), Equals, true) |
| 185 |
c.Assert(f(app2, "notif5", "two", true), Equals, true) |
| 186 |
c.Assert(f(app3, "notif6", "one", true), Equals, true) |
| 187 |
c.Assert(f(app3, "notif7", "", true), Equals, true) |
| 188 |
|
| 189 |
// patch cNotificationExists to return true in order to make sure that messages |
| 190 |
// do not get deleted by the doCleanUpTags() call in the Tags() function |
| 191 |
cNotificationExists = func(did string, nid string) bool { |
| 192 |
return true |
| 193 |
} |
| 194 |
|
| 195 |
// that is: |
| 196 |
// app 1: "one", "two", ""; |
| 197 |
// app 2: "one", "two"; |
| 198 |
// app 3: "one", "" |
| 199 |
ms.checkTags(c, mm.Tags(app1), []string{"one", "two", ""}) |
| 200 |
ms.checkTags(c, mm.Tags(app2), []string{"one", "two"}) |
| 201 |
ms.checkTags(c, mm.Tags(app3), []string{"one", ""}) |
| 202 |
|
| 203 |
// clearing a non-existent tag does nothing |
| 204 |
c.Check(mm.Clear(app1, "foo"), Equals, 0) |
| 205 |
c.Check(mm.Tags(app1), HasLen, 3) |
| 206 |
c.Check(mm.Tags(app2), HasLen, 2) |
| 207 |
c.Check(mm.Tags(app3), HasLen, 2) |
| 208 |
|
| 209 |
// asking to clear a tag that exists only for another app does nothing |
| 210 |
c.Check(mm.Clear(app3, "two"), Equals, 0) |
| 211 |
c.Check(mm.Tags(app1), HasLen, 3) |
| 212 |
c.Check(mm.Tags(app2), HasLen, 2) |
| 213 |
c.Check(mm.Tags(app3), HasLen, 2) |
| 214 |
|
| 215 |
// asking to clear a list of tags, only one of which is yours, only clears yours |
| 216 |
c.Check(mm.Clear(app3, "one", "two"), Equals, 1) |
| 217 |
c.Check(mm.Tags(app1), HasLen, 3) |
| 218 |
c.Check(mm.Tags(app2), HasLen, 2) |
| 219 |
c.Check(mm.Tags(app3), HasLen, 1) |
| 220 |
|
| 221 |
// clearing with no args just empties it |
| 222 |
c.Check(mm.Clear(app1), Equals, 3) |
| 223 |
c.Check(mm.Tags(app1), IsNil) |
| 224 |
c.Check(mm.Tags(app2), HasLen, 2) |
| 225 |
c.Check(mm.Tags(app3), HasLen, 1) |
| 226 |
|
| 227 |
// asking to clear all the tags from an already tagless app does nothing |
| 228 |
c.Check(mm.Clear(app1), Equals, 0) |
| 229 |
c.Check(mm.Tags(app1), IsNil) |
| 230 |
c.Check(mm.Tags(app2), HasLen, 2) |
| 231 |
c.Check(mm.Tags(app3), HasLen, 1) |
| 232 |
|
| 233 |
// check we work ok with a "" tag, too. |
| 234 |
c.Check(mm.Clear(app1, ""), Equals, 0) |
| 235 |
c.Check(mm.Clear(app2, ""), Equals, 0) |
| 236 |
c.Check(mm.Clear(app3, ""), Equals, 1) |
| 237 |
c.Check(mm.Tags(app1), IsNil) |
| 238 |
c.Check(mm.Tags(app2), HasLen, 2) |
| 239 |
c.Check(mm.Tags(app3), HasLen, 0) |
| 240 |
} |
| 241 |
|
| 242 |
func (ms *MessagingSuite) TestRemoveNotification(c *C) { |
| 243 |
mmu := New(ms.log) |
| 244 |
card := launch_helper.Card{Summary: "ehlo", Persist: true, Actions: []string{"action-1"}} |
| 245 |
actions := []string{"{\"app\":\"com.example.test_test_0\",\"act\":\"action-1\",\"nid\":\"notif-id\"}", "action-1"} |
| 246 |
mmu.addNotification(ms.app, "notif-id", "a-tag", &card, actions, nil) |
| 247 |
|
| 248 |
// check it's there |
| 249 |
payload, ok := mmu.notifications["notif-id"] |
| 250 |
c.Check(ok, Equals, true) |
| 251 |
c.Check(payload.Actions, DeepEquals, actions) |
| 252 |
c.Check(payload.Tag, Equals, "a-tag") |
| 253 |
c.Check(payload.Ch, Equals, mmu.Ch) |
| 254 |
// remove the notification (internal only) |
| 255 |
mmu.RemoveNotification("notif-id", false) |
| 256 |
// check it's gone |
| 257 |
_, ok = mmu.notifications["notif-id"] |
| 258 |
c.Check(ok, Equals, false) |
| 259 |
} |
| 260 |
|
| 261 |
func (ms *MessagingSuite) TestRemoveNotificationsFromUI(c *C) { |
| 262 |
mmu := New(ms.log) |
| 263 |
card := launch_helper.Card{Summary: "ehlo", Persist: true, Actions: []string{"action-1"}} |
| 264 |
actions := []string{"{\"app\":\"com.example.test_test_0\",\"act\":\"action-1\",\"nid\":\"notif-id\"}", "action-1"} |
| 265 |
mmu.addNotification(ms.app, "notif-id", "a-tag", &card, actions, nil) |
| 266 |
|
| 267 |
// check it's there |
| 268 |
_, ok := mmu.notifications["notif-id"] |
| 269 |
c.Assert(ok, Equals, true) |
| 270 |
// remove the notification (both internal and from UI) |
| 271 |
mmu.RemoveNotification("notif-id", true) |
| 272 |
// check it's gone |
| 273 |
_, ok = mmu.notifications["notif-id"] |
| 274 |
c.Check(ok, Equals, false) |
| 275 |
|
| 276 |
// and check it's been removed from the UI too |
| 277 |
c.Check(ms.log.Captured(), Matches, `(?s).* REMOVE:.*notif-id.*`) |
| 278 |
} |
| 279 |
|
| 280 |
func (ms *MessagingSuite) TestCleanupStaleNotification(c *C) { |
| 281 |
mmu := New(ms.log) |
| 282 |
card := launch_helper.Card{Summary: "ehlo", Persist: true, Actions: []string{"action-1"}} |
| 283 |
actions := []string{"{\"app\":\"com.example.test_test_0\",\"act\":\"action-1\",\"nid\":\"notif-id\"}", "action-1"} |
| 284 |
mmu.addNotification(ms.app, "notif-id", "", &card, actions, nil) |
| 285 |
|
| 286 |
// check it's there |
| 287 |
_, ok := mmu.notifications["notif-id"] |
| 288 |
c.Check(ok, Equals, true) |
| 289 |
|
| 290 |
// patch cNotificationExists to return true |
| 291 |
cNotificationExists = func(did string, nid string) bool { |
| 292 |
return true |
| 293 |
} |
| 294 |
// remove the notification |
| 295 |
mmu.cleanUpNotifications() |
| 296 |
// check it's still there |
| 297 |
_, ok = mmu.notifications["notif-id"] |
| 298 |
c.Check(ok, Equals, true) |
| 299 |
|
| 300 |
// patch cNotificationExists to return false |
| 301 |
cNotificationExists = func(did string, nid string) bool { |
| 302 |
return false |
| 303 |
} |
| 304 |
// remove the notification |
| 305 |
mmu.cleanUpNotifications() |
| 306 |
// check it's gone |
| 307 |
_, ok = mmu.notifications["notif-id"] |
| 308 |
c.Check(ok, Equals, false) |
| 309 |
} |
| 310 |
|
| 311 |
func (ms *MessagingSuite) TestCleanupInAddNotification(c *C) { |
| 312 |
mmu := New(ms.log) |
| 313 |
|
| 314 |
var wg sync.WaitGroup |
| 315 |
|
| 316 |
var cleanUpAsynchronously = func() { |
| 317 |
wg.Add(1) |
| 318 |
go func() { |
| 319 |
defer wg.Done() |
| 320 |
mmu.cleanUpNotifications() |
| 321 |
}() |
| 322 |
} |
| 323 |
|
| 324 |
showNotification := func(number int) { |
| 325 |
action := "action-" + strconv.Itoa(number) |
| 326 |
notificationId := "notif-id-" + strconv.Itoa(number) |
| 327 |
card := launch_helper.Card{Summary: "ehlo", Persist: true, Actions: []string{action}} |
| 328 |
actions := []string{"{\"app\":\"com.example.test_test_0\",\"act\":\"" + action + "\",\"nid\":\"" + notificationId + "\"}", action} |
| 329 |
mmu.addNotification(ms.app, notificationId, "", &card, actions, cleanUpAsynchronously) |
| 330 |
} |
| 331 |
|
| 332 |
// Add 20 notifications |
| 333 |
for i := 0; i < 20; i++ { |
| 334 |
showNotification(i) |
| 335 |
} |
| 336 |
|
| 337 |
// wait for the cleanup goroutine in addNotification to finish in case it gets called (which it shouldn't!) |
| 338 |
wg.Wait() |
| 339 |
|
| 340 |
// check that we have got 20 notifications |
| 341 |
c.Check(mmu.notifications, HasLen, 20) |
| 342 |
|
| 343 |
// patch cNotificationExists to return true |
| 344 |
cNotificationExists = func(did string, nid string) bool { |
| 345 |
return true |
| 346 |
} |
| 347 |
|
| 348 |
// adding another notification should not remove the current ones |
| 349 |
showNotification(21) |
| 350 |
|
| 351 |
// wait for the cleanup goroutine in addNotification to finish in case it gets called (which it shouldn't!) |
| 352 |
wg.Wait() |
| 353 |
|
| 354 |
// check we that have 21 notifications now |
| 355 |
c.Check(mmu.notifications, HasLen, 21) |
| 356 |
|
| 357 |
// patch cNotificationExists to return false for all but the next one we are going to add |
| 358 |
cNotificationExists = func(did string, nid string) bool { |
| 359 |
return nid == "notif-id-22" |
| 360 |
} |
| 361 |
|
| 362 |
// adding another notification should not remove the current ones as mmu.lastCleanupTime is too recent |
| 363 |
showNotification(22) |
| 364 |
|
| 365 |
// wait for the cleanup goroutine in addNotification to finish in case it gets called (which it shouldn't!) |
| 366 |
wg.Wait() |
| 367 |
|
| 368 |
// check we that have got 22 notifications now |
| 369 |
c.Check(mmu.notifications, HasLen, 22) |
| 370 |
|
| 371 |
// set back the lastCleanupTime to 11 minutes ago |
| 372 |
mmu.lastCleanupTime = mmu.lastCleanupTime.Add(-11 * time.Minute) |
| 373 |
|
| 374 |
// patch cNotificationExists to return false for all but the next one we are going to add |
| 375 |
cNotificationExists = func(did string, nid string) bool { |
| 376 |
return nid == "notif-id-23" |
| 377 |
} |
| 378 |
|
| 379 |
// adding another notification should remove all previous ones now |
| 380 |
showNotification(23) |
| 381 |
|
| 382 |
// wait for the cleanup goroutine in addNotification to finish |
| 383 |
wg.Wait() |
| 384 |
|
| 385 |
// check that all notifications except the last one have been removed |
| 386 |
c.Check(mmu.notifications, HasLen, 1) |
| 387 |
} |
| 388 |
|
| 389 |
func (ms *MessagingSuite) TestGetCh(c *C) { |
| 390 |
mmu := New(ms.log) |
| 391 |
mmu.Ch = make(chan *reply.MMActionReply) |
| 392 |
c.Check(mmu.GetCh(), Equals, mmu.Ch) |
| 393 |
} |