Unverified Commit 5d064696 authored by Leonardo Aramaki's avatar Leonardo Aramaki Committed by GitHub

Merge pull request #571 from RocketChat/fix/notifications

Fix broken notifications
parents 839f60b0 68755814
...@@ -150,8 +150,11 @@ dependencies { ...@@ -150,8 +150,11 @@ dependencies {
compile 'com.github.matrixxun:MaterialBadgeTextView:c5a27e8243' compile 'com.github.matrixxun:MaterialBadgeTextView:c5a27e8243'
compile 'com.github.chrisbanes:PhotoView:2.0.0' compile 'com.github.chrisbanes:PhotoView:2.0.0'
testCompile 'junit:junit:4.12' testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:2.8.9" testCompile 'org.robolectric:robolectric:3.3'
testCompile "org.jetbrains.kotlin:kotlin-test:$rootProject.ext.kotlinVersion" testCompile "org.jetbrains.kotlin:kotlin-test:$rootProject.ext.kotlinVersion"
testCompile "org.jetbrains.kotlin:kotlin-test-junit:$rootProject.ext.kotlinVersion" testCompile "org.jetbrains.kotlin:kotlin-test-junit:$rootProject.ext.kotlinVersion"
testCompile "com.nhaarman:mockito-kotlin:1.1.0"
testCompile 'org.amshove.kluent:kluent:1.14'
testCompile "org.jetbrains.kotlin:kotlin-reflect:$rootProject.ext.kotlinVersion"
} }
apply plugin: 'com.google.gms.google-services' apply plugin: 'com.google.gms.google-services'
...@@ -17,6 +17,7 @@ import java.util.UUID; ...@@ -17,6 +17,7 @@ import java.util.UUID;
import chat.rocket.android.helper.Logger; import chat.rocket.android.helper.Logger;
import chat.rocket.android.helper.TextUtils; import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.log.RCLog; import chat.rocket.android.log.RCLog;
import chat.rocket.android.push.PushManager;
import chat.rocket.core.utils.Pair; import chat.rocket.core.utils.Pair;
import io.reactivex.BackpressureStrategy; import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable; import io.reactivex.Flowable;
...@@ -104,9 +105,12 @@ public class RocketChatCache { ...@@ -104,9 +105,12 @@ public class RocketChatCache {
} }
} }
public @NonNull String getSiteUrlFor(String hostname) { public @Nullable String getSiteUrlFor(String hostname) {
try { try {
String selectedServerHostname = getSelectedServerHostname(); String selectedServerHostname = getSelectedServerHostname();
if (getLoginHostnamesJson() == null || getLoginHostnamesJson().isEmpty()) {
return null;
}
return new JSONObject(getLoginHostnamesJson()) return new JSONObject(getLoginHostnamesJson())
.optString(hostname, selectedServerHostname); .optString(hostname, selectedServerHostname);
} catch (JSONException e) { } catch (JSONException e) {
......
package chat.rocket.android;
import android.content.Context
import chat.rocket.core.utils.Pair
import org.hamcrest.CoreMatchers.equalTo
import org.json.JSONObject
import org.junit.Assert.assertThat
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.*
import org.mockito.junit.MockitoJUnitRunner
@RunWith(MockitoJUnitRunner::class)
class RocketChatCacheTest {
lateinit var cache: RocketChatCache
@Before
fun setup() {
val mockedContext = mock(Context::class.java)
val mockAppContext = mock(Context::class.java)
`when`(mockedContext.applicationContext).thenReturn(mockAppContext)
cache = spy(RocketChatCache(mockedContext))
}
@Test
fun getServerList_ShouldReturnHostnameList() {
val hostnameList = JSONObject()
.put("http://open.rocket.chat", "images/logo/logo.png")
.put("http://192.168.0.6:3000", "images/icon.svg")
.toString()
doReturn(hostnameList).`when`(cache).getString("KEY_HOSTNAME_LIST", null)
val expectedServerList = mutableListOf(
Pair("http://192.168.0.6:3000", "http://192.168.0.6:3000/images/icon.svg"),
Pair("http://open.rocket.chat", "http://open.rocket.chat/images/logo/logo.png"))
val serverList = cache.serverList
assertThat(serverList, equalTo(expectedServerList))
}
}
\ No newline at end of file
package chat.rocket.android.push
import android.app.Application
import android.content.Context
import android.content.res.Resources
import android.os.Bundle
import chat.rocket.android.BuildConfig
import chat.rocket.android.R
import chat.rocket.android.push.PushManager.PushMessage
import com.nhaarman.mockito_kotlin.*
import org.amshove.kluent.`should equal`
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.anyString
import org.mockito.MockitoAnnotations
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import kotlin.test.assertTrue
@RunWith(RobolectricTestRunner::class)
@Config(constants = BuildConfig::class,
application = PushManagerTest.StubApplication::class,
sdk = intArrayOf(23))
class PushManagerTest {
val EJSON = """
{
"host":"https://open.rocket.chat/",
"rid":"FaXMyHqbNJbPq6Ym9uWiyfQkgekhXywvKw",
"sender":{
"_id":"uWiFa3adOi0adac",
"username":"jean-luc.picard",
"name":"Jean-Luc Picard"
},
"type":"d",
"name":null
}
"""
val EJSON_NO_SENDER = """
{
"host":"https://open.rocket.chat/",
"rid":"FaXMyHqbNJbPq6Ym9uWiyfQkgekhXywvKw",
"sender":null,
"type":"d",
"name":null
}
"""
lateinit var data: Bundle
lateinit var pushManager: PushManager
lateinit var context: Context
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
data = Bundle()
data.putString("message", "Hello")
data.putString("title", "jean-luc.picard")
data.putString("ejson", EJSON)
data.putString("notId", "1")
context = spy(RuntimeEnvironment.application)
pushManager = spy(PushManager)
val res = mock<Resources> {
on { getColor(any()) } doReturn 0
on { getIdentifier(
anyString(), anyString(), any()) } doReturn R.drawable.notification_background
on { getConfiguration() } doReturn RuntimeEnvironment.application.resources.configuration
}
whenever(context.resources).doReturn(res)
whenever(context.applicationContext).doReturn(context)
}
@Test
fun `should create PushMessage without throwing`() {
PushMessage(null, null, null, null, null, "xxx",
null, null)
}
@Test
fun `given data shoud show notification`() {
pushManager.handle(context, data)
val push = PushMessage(title = data["title"] as String,
message = data["message"] as String, ejson = EJSON, notificationId = "1")
verify(pushManager, times(1)).showNotification(context, push)
}
@Test
fun `given required data is missing do not show notification`() {
val bundle = Bundle()
pushManager.handle(context, bundle)
verify(pushManager, never()).showNotification(any(), any())
bundle.putString("title", "jean-luc.picard")
bundle.putString("message", "Hello!")
pushManager.handle(context, bundle)
verify(pushManager, never()).showNotification(any(), any())
bundle.clear()
bundle.putString("ejson", EJSON)
bundle.putString("message", "Hello!")
pushManager.handle(context, bundle)
verify(pushManager, never()).showNotification(any(), any())
bundle.clear()
bundle.putString("ejson", EJSON)
bundle.putString("title", "jean-luc.picard")
pushManager.handle(context, bundle)
verify(pushManager, never()).showNotification(any(), any())
}
@Test
fun `given data should deserialize correctly`() {
pushManager.handle(context, data)
val push = PushMessage(
data.getString("title"),
data.getString("message"),
null,
EJSON,
null,
data.getString("notId"),
null,
null
)
verify(pushManager, times(1)).showNotification(context, push)
push.title `should equal` "jean-luc.picard"
push.message `should equal` "Hello"
val sender = push.sender
assertTrue(sender != null)
sender?._id `should equal` "uWiFa3adOi0adac"
sender?.name `should equal` "Jean-Luc Picard"
sender?.username `should equal` "jean-luc.picard"
}
@Test
fun `given that only sender is missing show notification`() {
val bundle = Bundle()
bundle.putString("title", "jean-luc.picard")
bundle.putString("message", "Hello")
bundle.putString("ejson", EJSON_NO_SENDER)
pushManager.handle(context, bundle)
verify(pushManager, times(1)).showNotification(any(), any())
}
internal class StubApplication : Application()
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment