|
| 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