Skip to content

Commit 83e33ec

Browse files
author
Zbigniew Szymański
committed
Add test for messages notifications controller.
1 parent 1aaf5f0 commit 83e33ec

File tree

4 files changed

+131
-7
lines changed

4 files changed

+131
-7
lines changed

app/src/main/scala/com/waz/zclient/WireApplication.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,18 @@ object WireApplication {
3535
var APP_INSTANCE: WireApplication = _
3636

3737
lazy val Global = new Module {
38+
implicit def context = inject[Context]
39+
implicit def eventContext = EventContext.Global
40+
3841
bind[Signal[Option[ZMessaging]]] to ZMessaging.currentUi.currentZms
42+
bind[Signal[ZMessaging]] to ZMessaging.currentUi.currentZms.collect { case Some(zms) => zms }
3943
bind[PreferenceService] to new PreferenceService(inject[Context])
4044
bind[GlobalCallingController] to new GlobalCallingController(inject[Context])
4145
bind[GlobalCameraController] to new GlobalCameraController(inject[Context], new AndroidCameraFactory)(EventContext.Global)
4246
bind[MediaManagerService] to ZMessaging.currentGlobal.mediaManager
4347

4448
//notifications
45-
bind[MessageNotificationsController] to new MessageNotificationsController(inject[Context])
49+
bind[MessageNotificationsController] to new MessageNotificationsController()
4650
bind[ImageNotificationsController] to new ImageNotificationsController(inject[Context])
4751
bind[CallingNotificationsController] to new CallingNotificationsController(inject[Context])
4852
}

app/src/main/scala/com/waz/zclient/notifications/controllers/MessageNotificationsController.scala

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ import com.waz.ZLog.ImplicitTag._
3333
import com.waz.ZLog.verbose
3434
import com.waz.api.NotificationsHandler.NotificationType
3535
import com.waz.api.NotificationsHandler.NotificationType._
36+
import com.waz.bitmap
3637
import com.waz.service.ZMessaging
3738
import com.waz.service.push.NotificationService.NotificationInfo
3839
import com.waz.threading.Threading
39-
import com.waz.utils.events.Signal
40+
import com.waz.utils.events.{EventContext, Signal}
4041
import com.waz.zclient._
4142
import com.waz.zclient.controllers.userpreferences.UserPreferencesController
4243
import com.waz.zclient.controllers.vibrator.VibratorController
@@ -48,13 +49,12 @@ import org.threeten.bp.Instant
4849

4950
import scala.concurrent.duration._
5051

51-
class MessageNotificationsController(cxt: WireContext)(implicit inj: Injector) extends Injectable {
52+
class MessageNotificationsController(implicit inj: Injector, cxt: Context, eventContext: EventContext) extends Injectable {
5253

5354
import MessageNotificationsController._
54-
implicit val eventContext = cxt.eventContext
55-
implicit val context = cxt
55+
def context = cxt
5656

57-
val zms = inject[Signal[Option[ZMessaging]]].collect { case Some(z) => z }
57+
val zms = inject[Signal[ZMessaging]]
5858

5959
val notsService = zms.map(_.notifications)
6060

@@ -221,7 +221,7 @@ class MessageNotificationsController(cxt: WireContext)(implicit inj: Injector) e
221221
builder.build
222222
}
223223

224-
private def getMessage(n: NotificationInfo, multiple: Boolean, singleConversationInBatch: Boolean, singleUserInBatch: Boolean) = {
224+
private[notifications] def getMessage(n: NotificationInfo, multiple: Boolean, singleConversationInBatch: Boolean, singleUserInBatch: Boolean) = {
225225
val message = n.message.replaceAll("\\r\\n|\\r|\\n", " ")
226226

227227
def getHeader(testPrefix: Boolean = false, singleUser: Boolean = false) = getDefaultNotificationMessageLineHeader(n, multiple, textPrefix = testPrefix, singleConversationInBatch = singleConversationInBatch, singleUser = singleUser)
@@ -284,6 +284,7 @@ class MessageNotificationsController(cxt: WireContext)(implicit inj: Injector) e
284284
try {
285285
val icon: Drawable = cxt.getPackageManager.getApplicationIcon(cxt.getPackageName)
286286
icon match {
287+
case null => bitmap.EmptyBitmap
287288
case drawable: BitmapDrawable =>
288289
drawable.getBitmap
289290
case _ =>

app/src/test/AndroidManifest.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="utf-8"?><!--
2+
Wire
3+
Copyright (C) 2016 Wire Swiss GmbH
4+
This program is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
This program is distributed in the hope that it will be useful,
9+
but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
GNU General Public License for more details.
12+
You should have received a copy of the GNU General Public License
13+
along with this program. If not, see <http://www.gnu.org/licenses/>.
14+
-->
15+
<manifest package="com.waz.zclient" xmlns:android="http://schemas.android.com/apk/res/android">
16+
<uses-sdk android:minSdkVersion="17" />`
17+
<application
18+
android:theme="@style/Theme.Light"/>
19+
</manifest>
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* Wire
3+
* Copyright (C) 2016 Wire Swiss GmbH
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package com.waz.zclient.notifications.controllers
19+
20+
import java.util.concurrent.TimeUnit
21+
22+
import android.app.NotificationManager
23+
import android.content.Context
24+
import com.waz.RobolectricUtils
25+
import com.waz.model.ConversationData.ConversationType
26+
import com.waz.model._
27+
import com.waz.service.ZMessaging
28+
import com.waz.testutils.MockZMessaging
29+
import com.waz.api.NotificationsHandler.NotificationType
30+
import com.waz.service.push.NotificationService.NotificationInfo
31+
import com.waz.utils.events.Signal
32+
import com.waz.zclient.{Module, R}
33+
import junit.framework.Assert._
34+
import org.junit.runner.RunWith
35+
import org.junit.{Before, Test}
36+
import org.robolectric.annotation.Config
37+
import org.robolectric.{Robolectric, RobolectricTestRunner}
38+
import org.scalatest.junit.JUnitSuite
39+
import org.scalatest.{Informer, Informing}
40+
import org.threeten.bp.Instant
41+
42+
import scala.concurrent.duration.Duration
43+
44+
@RunWith(classOf[RobolectricTestRunner])
45+
@Config(manifest = "src/test/AndroidManifest.xml", resourceDir = "../../build/intermediates/res/merged/dev/debug")
46+
class MessageNotificationsControllerTest extends JUnitSuite with RobolectricUtils with Informing {
47+
48+
import com.waz.utils.events.EventContext.Implicits.global
49+
50+
implicit val timeout = Duration(1000, TimeUnit.MILLISECONDS)
51+
52+
def notManager = context.getSystemService(Context.NOTIFICATION_SERVICE).asInstanceOf[NotificationManager]
53+
def notManagerShadow = Robolectric.shadowOf(notManager)
54+
55+
lazy val user = UserData(UserId(), "TestUser")
56+
lazy val conv = ConversationData(ConvId(user.id.str), RConvId(), None, user.id, ConversationType.OneToOne)
57+
58+
var zms: ZMessaging = _
59+
var controller: MessageNotificationsController = _
60+
61+
@Before
62+
def setUp(): Unit = {
63+
try {
64+
context.getResources.getString(R.string.pref_options_ringtones_text_key) // force resource loading
65+
} catch { case e: Throwable => e.printStackTrace() }
66+
67+
zms = new MockZMessaging() {
68+
insertUser(user)
69+
insertConv(conv)
70+
}
71+
72+
implicit val module = new Module {
73+
bind[Signal[ZMessaging]] to Signal.const(zms)
74+
bind[NotificationManager] to notManager
75+
}
76+
77+
controller = new MessageNotificationsController()
78+
}
79+
80+
@Test
81+
def getMessageForLike(): Unit = {
82+
val msg = controller.getMessage(NotificationInfo(NotificationType.LIKE, "", false, ConvId()), false, true, true)
83+
assertFalse(msg.toString.isEmpty)
84+
}
85+
86+
@Test
87+
def displayNotificationForReceivedLike(): Unit = {
88+
assertTrue(notManagerShadow.getAllNotifications.isEmpty)
89+
90+
zms.notifStorage.insert(NotificationData(Uid().str, "", conv.id, user.id, NotificationType.LIKE, Instant.now))
91+
92+
withDelay {
93+
assertEquals(1, notManagerShadow.getAllNotifications.size())
94+
}
95+
}
96+
97+
override protected def info: Informer = new Informer {
98+
override def apply(message: String, payload: Option[Any]): Unit = println(message)
99+
}
100+
}

0 commit comments

Comments
 (0)