Commit 6f58807f authored by Leonardo Aramaki's avatar Leonardo Aramaki

Refactored constant names to comply to the style guide; Prefixing key

for selected room with hostname to support the multi-server feature;
implemented saving the logged hostnames to prefs and it retrieval;
added the relevant tests
parent 0edf9911
......@@ -102,6 +102,8 @@ android {
release {
manifest.srcFile 'src/release/AndroidManifest.xml'
}
test.java.srcDirs += 'src/test/kotlin'
}
}
......@@ -174,6 +176,11 @@ dependencies {
provided 'io.reactivex:rxjava:1.3.0'
provided "com.github.akarnokd:rxjava2-interop:0.10.2"
provided 'com.hadisatrio:Optional:v1.0.1'
testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:2.7.19"
testCompile "org.jetbrains.kotlin:kotlin-test:$rootProject.ext.kotlinVersion"
testCompile "org.jetbrains.kotlin:kotlin-test-junit:$rootProject.ext.kotlinVersion"
}
apply plugin: 'com.google.gms.google-services'
......@@ -5,18 +5,29 @@ import android.content.SharedPreferences;
import com.hadisatrio.optional.Optional;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import chat.rocket.android.log.RCLog;
import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.annotations.NonNull;
import io.reactivex.annotations.Nullable;
/**
* sharedpreference-based cache.
*/
public class RocketChatCache {
private static final String KEY_SELECTED_SERVER_HOSTNAME = "selectedServerHostname";
private static final String KEY_SELECTED_ROOM_ID = "selectedRoomId";
private static final String KEY_PUSH_ID = "pushId";
private static final String KEY_SELECTED_SERVER_HOSTNAME = "KEY_SELECTED_SERVER_HOSTNAME";
private static final String KEY_SELECTED_ROOM_ID = "KEY_SELECTED_ROOM_ID";
private static final String KEY_PUSH_ID = "KEY_PUSH_ID";
private static final String KEY_HOSTNAME_LIST = "KEY_HOSTNAME_LIST";
private Context context;
......@@ -32,12 +43,47 @@ public class RocketChatCache {
setString(KEY_SELECTED_SERVER_HOSTNAME, hostname);
}
public void addHostname(@NonNull String hostname, @Nullable String hostnameAvatarUri) {
String hostnameList = getString(KEY_HOSTNAME_LIST, null);
try {
JSONObject json;
if (hostnameList == null) {
json = new JSONObject();
} else {
json = new JSONObject(hostnameList);
}
// Replace server avatar uri if exists.
json.put(hostname, hostnameAvatarUri == null ? JSONObject.NULL : hostnameAvatarUri);
setString(KEY_HOSTNAME_LIST, json.toString());
} catch (JSONException e) {
RCLog.e(e);
}
}
public List<String> getServerList() {
String json = getString(KEY_HOSTNAME_LIST, null);
if (json == null) {
return Collections.emptyList();
}
try {
JSONObject jsonObj = new JSONObject(json);
List<String> serverList = new ArrayList<>();
for (Iterator<String> iter = jsonObj.keys(); iter.hasNext();) {
serverList.add(iter.next());
}
return serverList;
} catch (JSONException e) {
RCLog.e(e);
}
return Collections.emptyList();
}
public String getSelectedRoomId() {
return getString(KEY_SELECTED_ROOM_ID, null);
return getString(getSelectedServerHostname() + KEY_SELECTED_ROOM_ID, null);
}
public void setSelectedRoomId(String roomId) {
setString(KEY_SELECTED_ROOM_ID, roomId);
setString(getSelectedServerHostname() + KEY_SELECTED_ROOM_ID, roomId);
}
public String getOrCreatePushId() {
......@@ -58,7 +104,7 @@ public class RocketChatCache {
}
public Flowable<Optional<String>> getSelectedRoomIdPublisher() {
return getValuePublisher(KEY_SELECTED_ROOM_ID);
return getValuePublisher(getSelectedServerHostname() + KEY_SELECTED_ROOM_ID);
}
private SharedPreferences getSharedPreferences() {
......@@ -69,7 +115,7 @@ public class RocketChatCache {
return getSharedPreferences().edit();
}
private String getString(String key, String defaultValue) {
public String getString(String key, String defaultValue) {
return getSharedPreferences().getString(key, defaultValue);
}
......
package chat.rocket.android;
import android.content.Context
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("demo.rocket.chat", "imageuri")
.put("192.168.0.6:3000", "imageuri")
.toString()
doReturn(hostnameList).`when`(cache).getString("KEY_HOSTNAME_LIST", null)
val expectedServerList = mutableListOf("demo.rocket.chat", "192.168.0.6:3000")
val serverList = cache.serverList
expectedServerList.sort()
serverList.sort()
assertThat(cache.serverList, equalTo(expectedServerList))
}
}
\ 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